1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-15 09:33:50 +01:00

tests/net/nanocoap_cli: add observe command

This commit is contained in:
Benjamin Valentin 2025-01-23 16:17:50 +01:00
parent 6cb40183a0
commit 2cf009ab3a
2 changed files with 51 additions and 0 deletions

View File

@ -48,6 +48,8 @@ LOW_MEMORY_BOARDS := \
bluepill-stm32f103c8 \
derfmega128 \
im880b \
nucleo-f070rb \
nucleo-f072rb \
nucleo-f302r8 \
saml10-xpro \
saml11-xpro \
@ -63,6 +65,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

View File

@ -393,3 +393,51 @@ static int _cmd_get_non(int argc, char **argv)
}
SHELL_COMMAND(get_non, "non-confirmable get", _cmd_get_non);
#ifdef MODULE_NANOCOAP_SOCK_OBSERVE
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);
#endif /* MODULE_NANOCOAP_SOCK_OBSERVE */