From fa011a8a1a5d6492a2377490065acb9d0743868d Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 11 Dec 2024 18:30:50 +0100 Subject: [PATCH] tests/net/nanocoap_cli: allow specifying the token 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 --- tests/net/nanocoap_cli/nanocli_client.c | 38 +++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/net/nanocoap_cli/nanocli_client.c b/tests/net/nanocoap_cli/nanocli_client.c index c6f64e418d00..a9df7ab54765 100644 --- a/tests/net/nanocoap_cli/nanocli_client.c +++ b/tests/net/nanocoap_cli/nanocli_client.c @@ -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. */ @@ -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 */ @@ -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], '/'); @@ -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 \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) {