1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

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 <benpicco@googlemail.com>
This commit is contained in:
Marian Buschsieweke 2024-12-11 18:30:50 +01:00
parent 6b1279348a
commit fa011a8a1a
No known key found for this signature in database
GPG Key ID: 758BD52517F79C41

View File

@ -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 <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)
{