From 1885e1dd551488be9c85115d3e4970df56a1564d Mon Sep 17 00:00:00 2001 From: Leandro Lanzieri Date: Mon, 11 Mar 2019 13:23:52 +0100 Subject: [PATCH] examples/nanocoap_server: Add echo resource and fix README --- examples/nanocoap_server/README.md | 10 +++++++++- examples/nanocoap_server/coap_handler.c | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/examples/nanocoap_server/README.md b/examples/nanocoap_server/README.md index d2b373d25d..42a9bc4d26 100644 --- a/examples/nanocoap_server/README.md +++ b/examples/nanocoap_server/README.md @@ -63,15 +63,23 @@ The link-layer address in this case is "fe80::e42a:1aff:feca:10ec", the only Testing ======= -The CoAP server exposes 3 different resources: +The CoAP server exposes 6 different resources: * `/.well-known/core`: returns the list of available resources on the server. This is part of the CoAP specifications. It works only with GET requests. +* `/echo/`: will match any request that begins with '/echo/' and will echo + the remaining part of the URI path. Meant to show how the prefix works. It + works only with GET requests. * `/riot/board`: returns the name of the board running the server. It works only with GET requests. * `/riot/value`: returns the value of an internal variable of the server. It works with GET requests and also with PUT and POST requests, which means that this value can be updated from a client. +* `/riot/ver`: returns the current RIOT version. Meant to show a block2 reply. + It works only with GET requests. +* `/sha256`: creates a hash with the received payloads. It is meant to show + block1 support. It returns the hash when no more blocks are pending. Only + works with POST. There are multiple external CoAP clients you can use to easily test the server running on native. diff --git a/examples/nanocoap_server/coap_handler.c b/examples/nanocoap_server/coap_handler.c index 807c0afd40..8f910b5687 100644 --- a/examples/nanocoap_server/coap_handler.c +++ b/examples/nanocoap_server/coap_handler.c @@ -21,6 +21,21 @@ static const uint8_t block2_intro[] = "This is RIOT (Version: "; static const uint8_t block2_board[] = " running on a "; static const uint8_t block2_mcu[] = " board with a "; +static ssize_t _echo_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) +{ + (void)context; + char uri[NANOCOAP_URI_MAX]; + + if (coap_get_uri_path(pkt, (uint8_t *)uri) <= 0) { + return coap_reply_simple(pkt, COAP_CODE_INTERNAL_SERVER_ERROR, buf, + len, COAP_FORMAT_TEXT, NULL, 0); + } + char *sub_uri = uri + strlen("/echo/"); + size_t sub_uri_len = strlen(sub_uri); + return coap_reply_simple(pkt, COAP_CODE_CONTENT, buf, len, COAP_FORMAT_TEXT, + (uint8_t *)sub_uri, sub_uri_len); +} + static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) { (void)context; @@ -145,6 +160,7 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, void *context /* must be sorted by path (ASCII order) */ const coap_resource_t coap_resources[] = { COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER, + { "/echo/", COAP_GET | COAP_MATCH_SUBTREE, _echo_handler, NULL }, { "/riot/board", COAP_GET, _riot_board_handler, NULL }, { "/riot/value", COAP_GET | COAP_PUT | COAP_POST, _riot_value_handler, NULL }, { "/riot/ver", COAP_GET, _riot_block2_handler, NULL },