From c0ab893666048ff7784ed9e4a9d6cf44d629909b Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 18 Jan 2019 15:05:15 +0100 Subject: [PATCH] coap-server.c: add ct to description of new resources When a new resource is created with PUT, the given Content-Format is stored as parameter ct with the resource description. This change is based on a fix provided by Peter van der Stok. --- examples/coap-server.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index 91874268..da1c75bd 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -412,7 +412,7 @@ hnd_put(coap_context_t *ctx UNUSED_PARAM, uint8_t *data; coap_block_t block1; dynamic_resource_t *resource_entry = NULL; - unsigned char buf[4]; + unsigned char buf[6]; /* space to hold encoded/decoded uints */ coap_opt_iterator_t opt_iter; coap_opt_t *option; @@ -452,9 +452,18 @@ hnd_put(coap_context_t *ctx UNUSED_PARAM, else { dynamic_entry[i].media_type = COAP_MEDIATYPE_TEXT_PLAIN; } - - } - else { + /* Store media type of new resource in ct. We can use buf here + * as coap_add_attr() will copy the passed string. */ + memset(buf, 0, sizeof(buf)); + snprintf((char *)buf, sizeof(buf), "%d", dynamic_entry[i].media_type); + /* ensure that buf is always zero-terminated */ + assert(buf[sizeof(buf) - 1] == '\0'); + buf[sizeof(buf) - 1] = '\0'; + coap_add_attr(resource, + coap_make_str_const("ct"), + coap_make_str_const(buf), + 0); + } else { dynamic_count--; response->code = COAP_RESPONSE_CODE(500); return;