From f3b4e44a267455b78e6e0d793f4102b6fb73c54f Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Wed, 11 Apr 2018 22:25:16 +0200 Subject: [PATCH] examples/nanocoap: add blockwise block2 example --- examples/nanocoap_server/Makefile | 2 +- examples/nanocoap_server/coap_handler.c | 39 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/examples/nanocoap_server/Makefile b/examples/nanocoap_server/Makefile index 7b6e5fa1b9..76e1e5190b 100644 --- a/examples/nanocoap_server/Makefile +++ b/examples/nanocoap_server/Makefile @@ -11,7 +11,7 @@ BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-mega2560 arduino-uno \ chronos msb-430 msb-430h nucleo-f031k6 \ nucleo-f042k6 nucleo-l031k6 nucleo-f030r8 \ nucleo-f303k8 nucleo-l053r8 stm32f0discovery \ - telosb waspmote-pro z1 + telosb waspmote-pro wsn430-v1_3b wsn430-v1_4 z1 # Include packages that pull up and auto-init the link layer. # NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present diff --git a/examples/nanocoap_server/coap_handler.c b/examples/nanocoap_server/coap_handler.c index c8457beaf0..156747c839 100644 --- a/examples/nanocoap_server/coap_handler.c +++ b/examples/nanocoap_server/coap_handler.c @@ -17,6 +17,10 @@ /* internal value that can be read/written via CoAP */ static uint8_t internal_value = 0; +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 _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) { (void)context; @@ -24,6 +28,40 @@ static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, vo COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD)); } +static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) +{ + (void)context; + coap_block_slicer_t slicer; + coap_block2_init(pkt, &slicer); + uint8_t *payload = buf + coap_get_total_hdr_len(pkt); + + uint8_t *bufpos = payload; + + bufpos += coap_put_option_ct(bufpos, 0, COAP_FORMAT_TEXT); + bufpos += coap_opt_put_block2(bufpos, COAP_OPT_CONTENT_FORMAT, &slicer, 1); + *bufpos++ = 0xff; + + /* Add actual content */ + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, block2_intro, sizeof(block2_intro)); + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, (uint8_t*)RIOT_VERSION, sizeof(RIOT_VERSION)); + bufpos += coap_blockwise_put_char(&slicer, bufpos, ')'); + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, block2_board, sizeof(block2_board)); + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, (uint8_t*)RIOT_BOARD, sizeof(RIOT_BOARD)); + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, block2_mcu, sizeof(block2_mcu)); + bufpos += coap_blockwise_put_bytes(&slicer, bufpos, (uint8_t*)RIOT_MCU, sizeof(RIOT_MCU)); + /* To demonstrate individual chars */ + bufpos += coap_blockwise_put_char(&slicer, bufpos, ' '); + bufpos += coap_blockwise_put_char(&slicer, bufpos, 'M'); + bufpos += coap_blockwise_put_char(&slicer, bufpos, 'C'); + bufpos += coap_blockwise_put_char(&slicer, bufpos, 'U'); + bufpos += coap_blockwise_put_char(&slicer, bufpos, '.'); + + + unsigned payload_len = bufpos - payload; + return coap_block2_build_reply(pkt, COAP_CODE_205, + buf, len, payload_len, &slicer); +} + static ssize_t _riot_value_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context) { (void) context; @@ -109,6 +147,7 @@ const coap_resource_t coap_resources[] = { COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER, { "/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 }, { "/sha256", COAP_POST, _sha256_handler, NULL }, };