Skip to content

Commit

Permalink
tests/net/nanocoap_cli: add observe command
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 28, 2025
1 parent 1a6805d commit cd7c38e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/net/nanocoap_cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ifeq (,$(filter $(BOARD),$(LOW_MEMORY_BOARDS)))
USEMODULE += nanocoap_dtls
USEMODULE += prng_sha256prng

USEMODULE += nanocoap_sock_observe
USEMODULE += nanocoap_vfs
USEMODULE += vfs_default
# USEMODULE += vfs_auto_format
Expand Down
47 changes: 47 additions & 0 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,50 @@ static int _cmd_get_non(int argc, char **argv)
}

SHELL_COMMAND(get_non, "non-confirmable get", _cmd_get_non);

static int _observe_cb(void *arg, coap_pkt_t *pkt)
{
(void)arg;

if (coap_get_code_class(pkt) != COAP_CLASS_SUCCESS) {
printf("observe: error\n");
}

od_hex_dump(pkt->payload, pkt->payload_len, OD_WIDTH_DEFAULT);

return pkt->payload_len;
}

static int _cmd_observe(int argc, char **argv)
{
static coap_observe_client_t ctx;
bool observe = true;
int res;

if ((argc < 2) || (argc > 3)) {
printf("usage: %s <url> [on|off]\n", argv[0]);
return 1;
}
if (argc > 2 && !strcmp("off", argv[2])) {
observe = false;
}

if (ctx.cb && observe) {
puts("CLI can observe only a single resource at a time");
return -1;
}

if (observe) {
res = nanocoap_sock_observe_url(argv[1], &ctx, _observe_cb, NULL);
}
else {
res = nanocoap_sock_unobserve_url(argv[1], &ctx);
}

if (res < 0) {
printf("error: %d\n", res);
}
return res;
}

SHELL_COMMAND(observe, "observe URL", _cmd_observe);

0 comments on commit cd7c38e

Please sign in to comment.