diff --git a/tests/net/nanocoap_cli/nanocli_client.c b/tests/net/nanocoap_cli/nanocli_client.c index 3a67e1511334..967108b42528 100644 --- a/tests/net/nanocoap_cli/nanocli_client.c +++ b/tests/net/nanocoap_cli/nanocli_client.c @@ -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. */ @@ -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 */ @@ -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], '/'); @@ -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 \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); + + static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more) {