From ea616abfda854b1bc7afc244c6faa9fb9ff6e991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20G=C3=BCndo=C4=9Fan?= Date: Thu, 2 Apr 2020 00:33:29 +0200 Subject: [PATCH] nanocoap: allow coap_opt_add_string() for zero-terminated strings --- sys/include/net/nanocoap.h | 27 ++++++++++++++++++- sys/net/application_layer/nanocoap/nanocoap.c | 21 ++++++++------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index a6612fd918..5dbc5e28be 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -1046,6 +1046,27 @@ static inline ssize_t coap_opt_add_uri_query(coap_pkt_t *pkt, const char *key, */ ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri); +/** + * @brief Encode the given array of characters as option(s) into pkt + * + * Use separator to split array of characters into multiple options. + * + * @post pkt.payload advanced to first byte after option(s) + * @post pkt.payload_len reduced by option(s) length + * + * @param[in,out] pkt pkt referencing target buffer + * @param[in] optnum option number to use + * @param[in] chars array of characters to encode as option + * @param[in] chars_len length of @p chars + * @param[in] separator character used in @p string to separate parts + * + * @return number of bytes written to buffer + * @return <0 on error + * @return -ENOSPC if no available options or insufficient buffer space + */ +ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars, + size_t chars_len, char separator); + /** * @brief Encode the given string as option(s) into pkt * @@ -1063,7 +1084,11 @@ ssize_t coap_opt_add_proxy_uri(coap_pkt_t *pkt, const char *uri); * @return <0 on error * @return -ENOSPC if no available options or insufficient buffer space */ -ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator); +static inline ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, + const char *string, char separator) +{ + return coap_opt_add_chars(pkt, optnum, string, strlen(string), separator); +} /** * @brief Adds one or multiple Uri-Path options in the form '/path' into pkt diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index 4611e10973..580899c219 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -819,28 +819,31 @@ static ssize_t _add_opt_pkt(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val return optlen; } -ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, - char separator) +ssize_t coap_opt_add_chars(coap_pkt_t *pkt, uint16_t optnum, const char *chars, + size_t chars_len, char separator) { - size_t unread_len = strlen(string); - if (!unread_len) { + /* chars_len denotes the length of the chars buffer and is + * gradually decremented below while iterating over the buffer */ + if (!chars_len) { return 0; } - char *uripos = (char *)string; + + char *uripos = (char *)chars; + char *endpos = ((char *)chars + chars_len); size_t write_len = 0; - while (unread_len) { + while (chars_len) { size_t part_len; if (*uripos == separator) { uripos++; } uint8_t *part_start = (uint8_t *)uripos; - while (unread_len) { + while (chars_len) { /* must decrement separately from while loop test to ensure * the value remains non-negative */ - unread_len--; - if ((*uripos == separator) || (*uripos == '\0')) { + chars_len--; + if ((*uripos == separator) || (uripos == endpos)) { break; } uripos++;