Skip to content

Commit

Permalink
tests/net/nanocoap_cli: allow specifying the token
Browse files Browse the repository at this point in the history
This adds the client_token shell command that allows to specify the
CoAP Token. This is particularly useful to test extended length Tokens,
as enabled with module `nanocoap_token_ext`.
  • Loading branch information
maribu committed Dec 11, 2024
1 parent 562036e commit 39d2437
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ static ssize_t _send(coap_pkt_t *pkt, size_t len,
return nanocoap_request(pkt, NULL, &remote, len);
}

#if MODULE_NANOCOAP_TOKEN_EXT
# define CLIENT_TOKEN_LENGTH_MAX 16
#else
# define CLIENT_TOKEN_LENGTH_MAX COAP_TOKEN_LENGTH_MAX
#endif
static uint8_t _client_token[CLIENT_TOKEN_LENGTH_MAX] = {0xDA, 0xEC};
static uint8_t _client_token_len = 2;

static int _cmd_client(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
Expand All @@ -88,7 +96,6 @@ static int _cmd_client(int argc, char **argv)
uint8_t buf[buflen];
coap_pkt_t pkt;
size_t len;
uint8_t token[2] = {0xDA, 0xEC};

if (argc == 1) {
/* show help for commands */
Expand All @@ -109,7 +116,8 @@ static int _cmd_client(int argc, char **argv)

/* parse options */
if (argc == 5 || argc == 6) {
ssize_t hdrlen = coap_build_hdr(pkt.hdr, COAP_TYPE_CON, &token[0], 2,
ssize_t hdrlen = coap_build_hdr(pkt.hdr, COAP_TYPE_CON,
_client_token, _client_token_len,
code_pos+1, 1);
coap_pkt_init(&pkt, &buf[0], buflen, hdrlen);
coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, argv[4], '/');
Expand Down Expand Up @@ -164,9 +172,69 @@ static int _cmd_client(int argc, char **argv)
argv[0]);
return 1;
}

SHELL_COMMAND(client, "CoAP client", _cmd_client);

static bool _get_nibble(uint8_t *dest, char _c)
{
uint8_t c = _c;
if (((uint8_t)'0' <= c) && (c <= (uint8_t)'9')) {
*dest = c - (uint8_t)'0';
return true;
}

if (((uint8_t)'a' <= c) && (c <= (uint8_t)'f')) {
*dest = c - (uint8_t)'a' + 10;
return true;
}

if (((uint8_t)'A' <= c) && (c <= (uint8_t)'F')) {
*dest = c - (uint8_t)'A' + 10;
return true;
}

return false;
}

static int _cmd_client_token(int argc, char **argv){
if (argc != 2) {
printf("Usage: %s <TOKEN_HEX>\n", argv[0]);
return 1;
}

size_t len = strlen(argv[1]);
if (len & 1) {
puts("Token invalid: Must be an even number of hex chars");
return 1;
}

if (len > 2 * sizeof(_client_token)) {
printf("Token too long (%u B, but max is %u B)\n",
(unsigned)(len / 2), (unsigned)sizeof(_client_token));
return 1;
}

_client_token_len = len / 2;

for (size_t pos = 0; pos < _client_token_len; pos++) {
uint8_t high, low;
if (!_get_nibble(&high, argv[1][pos << 1])) {
puts("Token invalid: Must be given in hex");
return 1;
}

if (!_get_nibble(&low, argv[1][(pos << 1) + 1])) {
puts("Token invalid: Must be given in hex");
return 1;
}

_client_token[pos] = (high << 4) | low;
}

return 0;
}
SHELL_COMMAND(client_token, "Set Token for CoAP client", _cmd_client_token);


Check warning on line 237 in tests/net/nanocoap_cli/nanocli_client.c

View workflow job for this annotation

GitHub Actions / static-tests

too many consecutive empty lines
static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf,
size_t len, int more)
{
Expand Down

0 comments on commit 39d2437

Please sign in to comment.