Skip to content

Commit

Permalink
fix: use Token in the Coap::token function
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbruijn committed Jun 14, 2020
1 parent 171415a commit dc5d89d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ Coap::Coap(std::uint8_t data) {}

Coap::Coap(std::uint8_t data, Header header) {}

Coap& Coap::token() {
// set Header TokenLength
Coap& Coap::token(Token value) {
/**
* - Write length to header.
* - Write token to data.
*/
return *this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/coap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>

#include "header.hpp"
#include "token.hpp"

namespace coap {

Expand All @@ -27,7 +28,7 @@ class Coap {

Coap(std::uint8_t data, Header header);

Coap& token();
Coap& token(Token value);
Coap& option();
Coap& payload();
};
Expand Down
38 changes: 36 additions & 2 deletions test/coap.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#include "coap.hpp"

#include <algorithm> //
#include <cstdint>
#include <iostream>
#include <iterator> //
#include <random> //
#include <vector>

#include "catch2/catch.hpp"

namespace coap::test {

auto randomNumberInRange = [](std::uint8_t lowest_value,
std::uint8_t highest_value) {
return [distribution = std::uniform_int_distribution<std::uint8_t>(
lowest_value, highest_value),
random_engine = std::mt19937{std::random_device{}()}]() mutable {
return distribution(random_engine);
};
};

std::string const kTestGroup("CoAP ");
std::string const kTags("[coap]");

Expand All @@ -12,11 +28,29 @@ using Coap = coap::Coap;

TEST_CASE(kTestGroup + "correctly parses code", kTags) {
Coap coap(20);
coap.option().payload().token();
coap::Token::Value token{{0xff, 0x18}, 4};
coap::Token::Value token2{{0xff, 0x18}, 4};

REQUIRE(token == token2);

coap::Token::Vector v = coap::Token(token);
printf("Test token of %lu bytes: ", v.size());
for (auto const& e : v) {
printf("%02hhx, ", e);
}
printf("\n");

// token.
coap.option().payload().token( //
token);
// coap::Token({0xaa, 0x11}));

Coap coap2(20, coap::Header(coap::Type::Value::kConfirmable,
coap::Code::Value::kPost, 123));
coap2.option().payload().token();
// coap2.option().payload().token();

std::vector<std::uint8_t> numbers;
std::generate_n(std::back_inserter(numbers), 100, randomNumberInRange(0, 10));
}

} // namespace coap::test

0 comments on commit dc5d89d

Please sign in to comment.