1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

examples/nanocoap_server: Add echo resource and fix README

This commit is contained in:
Leandro Lanzieri 2019-03-11 13:23:52 +01:00
parent 6bb4158c31
commit 1885e1dd55
2 changed files with 25 additions and 1 deletions

View File

@ -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.

View File

@ -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 },