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`.

Co-authored-by: benpicco <[email protected]>
  • Loading branch information
maribu and benpicco committed Dec 11, 2024
1 parent d6fa738 commit b9eb0bf
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 @@ -89,7 +97,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 @@ -110,7 +117,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 @@ -165,9 +173,33 @@ static int _cmd_client(int argc, char **argv)
argv[0]);
return 1;
}

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

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

ssize_t tkl = scn_buf_hex(_client_token, sizeof(_client_token),
argv[1], strlen(argv[1]));

if (tkl == -EOVERFLOW) {
puts("Token too long");
return 1;
}

if (tkl < 0) {
puts("Failed to parse token");
return 1;
}

_client_token_len = tkl;

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

static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf,
size_t len, int more)
{
Expand Down

0 comments on commit b9eb0bf

Please sign in to comment.