Skip to content

Commit

Permalink
WIP: tests/nanocoap_cli: add blockwise put test
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Apr 17, 2022
1 parent c58104e commit 3d23871
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/nanocoap_cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,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);
extern int nanotest_client_put_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 },
{ "put", "experimental put", nanotest_client_put_cmd },
{ "server", "CoAP server", nanotest_server_cmd },
{ "inet6", "IPv6 addresses", _list_all_inet6 },
{ NULL, NULL, NULL }
Expand Down
62 changes: 62 additions & 0 deletions tests/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "net/sock/udp.h"
#include "net/sock/util.h"

#include "od.h"

Expand Down Expand Up @@ -210,3 +211,64 @@ int nanotest_client_url_cmd(int argc, char **argv)
printf("usage: %s <get|post|put> <url> [data]\n", argv[0]);
return -1;
}

static const char song[] =
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n"
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n"
"\n"
"Hoarders can get piles of money,\n"
"That is true, hackers, that is true.\n"
"But they cannot help their neighbors;\n"
"That's not good, hackers, that's not good.\n"
"\n"
"When we have enough free software\n"
"At our call, hackers, at our call,\n"
"We'll kick out those dirty licenses\n"
"Ever more, hackers, ever more.\n"
"\n"
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n"
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n";

int nanotest_client_put_cmd(int argc, char **argv)
{
int res;
sock_udp_ep_t dst;
nanocoap_sock_t sock;
coap_block_request_t ctx;

if (argc < 3) {
printf("usage: %s <host> <path>\n", argv[0]);
return 1;
}

res = sock_udp_name2ep(&dst, argv[1]);
if (res) {
printf("invalid host: %s\n", argv[1]);
return res;
}

nanocoap_sock_connect(&sock, NULL, &dst);

nanocoap_block_request_init(&ctx, argv[2], COAP_METHOD_PUT, COAP_BLOCKSIZE_32);
const char *pos = song;
size_t len = sizeof(song);

while (len) {
res = nanocoap_sock_block_request(&sock, &ctx, pos, len,
len > coap_szx2size(COAP_BLOCKSIZE_32),
NULL, NULL);
if (res <= 0) {
puts(strerror(-res));
break;
}
len -= res;
pos += res;
}

nanocoap_sock_close(&sock);
return res;
}

0 comments on commit 3d23871

Please sign in to comment.