diff --git a/examples/gcoap/README.md b/examples/gcoap/README.md index a795838534..69660d0b8d 100644 --- a/examples/gcoap/README.md +++ b/examples/gcoap/README.md @@ -14,13 +14,14 @@ Build with `Makefile.slip`. Follow the setup instructions in README-slip.md, whi ## Current Status gcoap includes server and client capability. Available features include: +* Message Type: Supports non-confirmable (NON) messaging. Additionally provides a callback on timeout. +* Observe extension: Provides server-side registration and notifications. * Server and Client provide helper functions for writing the response/request. See the CoAP topic in the source documentation for details. See the gcoap example for sample implementations. * Server allows an application to register a 'listener', which includes an array of endpoint paths and function callbacks used to write a response. * Server listens on a port at startup; defaults to 5683. -* Client operates asynchronously; sends request and then handles response in a user provided callback. Also executes callback on timeout. +* Client operates asynchronously; sends request and then handles response in a user provided callback. * Client generates token; length defined at compile time. -* Message Type: Supports non-confirmable (NON) messaging. -* Options: Supports Content-Format for response payload. +* Options: Supports Content-Format for payload. ## Example Use diff --git a/examples/gcoap/gcoap_cli.c b/examples/gcoap/gcoap_cli.c index 636e6998ad..c754109f2a 100644 --- a/examples/gcoap/gcoap_cli.c +++ b/examples/gcoap/gcoap_cli.c @@ -26,6 +26,9 @@ #include "od.h" #include "fmt.h" +#define ENABLE_DEBUG (0) +#include "debug.h" + static void _resp_handler(unsigned req_state, coap_pkt_t* pdu); static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len); @@ -149,15 +152,33 @@ int gcoap_cli_cmd(int argc, char **argv) argv[4]); } printf("gcoap_cli: sending msg ID %u, %u bytes\n", coap_get_id(&pdu), - (unsigned) len); + (unsigned) len); if (!_send(&buf[0], len, argv[2], argv[3])) { puts("gcoap_cli: msg send failed"); } + else { + /* send Observe notification for /cli/stats */ + switch (gcoap_obs_init(&pdu, &buf[0], GCOAP_PDU_BUF_SIZE, + &_resources[0])) { + case GCOAP_OBS_INIT_OK: + DEBUG("gcoap_cli: creating /cli/stats notification\n"); + size_t payload_len = fmt_u16_dec((char *)pdu.payload, req_count); + len = gcoap_finish(&pdu, payload_len, COAP_FORMAT_TEXT); + gcoap_obs_send(&buf[0], len, &_resources[0]); + break; + case GCOAP_OBS_INIT_UNUSED: + DEBUG("gcoap_cli: no observer for /cli/stats\n"); + break; + case GCOAP_OBS_INIT_ERR: + DEBUG("gcoap_cli: error initializing /cli/stats notification\n"); + break; + } + } return 0; } else { printf("usage: %s [data]\n", - argv[0]); + argv[0]); return 1; } }