From 2106ce42121405b227b773816971a7b4bf42f3de Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 26 Jan 2022 15:06:22 +0100 Subject: [PATCH] tests/nanocoap_cli: add url command --- tests/nanocoap_cli/main.c | 2 ++ tests/nanocoap_cli/nanocli_client.c | 49 ++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/tests/nanocoap_cli/main.c b/tests/nanocoap_cli/main.c index af8e3e9d48..9e89d46071 100644 --- a/tests/nanocoap_cli/main.c +++ b/tests/nanocoap_cli/main.c @@ -29,11 +29,13 @@ static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; extern int nanotest_client_cmd(int argc, char **argv); +extern int nanotest_client_url_cmd(int argc, char **argv); extern int nanotest_server_cmd(int argc, char **argv); static int _list_all_inet6(int argc, char **argv); static const shell_command_t shell_commands[] = { { "client", "CoAP client", nanotest_client_cmd }, + { "url", "CoAP client URL request", nanotest_client_url_cmd }, { "server", "CoAP server", nanotest_server_cmd }, { "inet6", "IPv6 addresses", _list_all_inet6 }, { NULL, NULL, NULL } diff --git a/tests/nanocoap_cli/nanocli_client.c b/tests/nanocoap_cli/nanocli_client.c index bb63e20ac8..258c7d958c 100644 --- a/tests/nanocoap_cli/nanocli_client.c +++ b/tests/nanocoap_cli/nanocli_client.c @@ -86,7 +86,7 @@ static ssize_t _send(coap_pkt_t *pkt, size_t len, char *addr_str, char *port_str int nanotest_client_cmd(int argc, char **argv) { /* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */ - char *method_codes[] = {"get", "post", "put"}; + const char *method_codes[] = {"get", "post", "put"}; unsigned buflen = 128; uint8_t buf[buflen]; coap_pkt_t pkt; @@ -167,3 +167,50 @@ int nanotest_client_cmd(int argc, char **argv) argv[0]); return 1; } + +static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more) +{ + (void)arg; + (void)more; + + printf("offset %03u: ", (unsigned)offset); + for (unsigned i = 0; i < len; ++i) { + putchar(buf[i]); + } + puts(""); + + return 0; +} + +int nanotest_client_url_cmd(int argc, char **argv) +{ + /* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */ + const char *method_codes[] = {"get", "post", "put"}; + + if (argc < 3) { + goto error; + } + + int code_pos = -1; + for (size_t i = 0; i < ARRAY_SIZE(method_codes); i++) { + if (strcmp(argv[1], method_codes[i]) == 0) { + code_pos = i; + break; + } + } + if (code_pos == -1) { + goto error; + } + + if (code_pos != 0) { + printf("TODO: implement %s request\n", method_codes[code_pos]); + return -1; + } + + uint8_t buffer[NANOCOAP_BLOCKWISE_BUF(COAP_BLOCKSIZE_32)]; + return nanocoap_get_blockwise_url(argv[2], COAP_BLOCKSIZE_32, buffer, + _blockwise_cb, NULL); +error: + printf("usage: %s [data]\n", argv[0]); + return -1; +}