Skip to content

Commit

Permalink
Merge pull request #7402 from haukepetersen/add_gcoap_qshandling
Browse files Browse the repository at this point in the history
net/gcoap: add Uri-Query strings for requests
  • Loading branch information
haukepetersen authored Aug 8, 2017
2 parents ea4a8e3 + 8f577ec commit fb07d98
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/nanocoap/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PKG_NAME=nanocoap
PKG_URL=https://github.com/kaspar030/sock
PKG_VERSION=1bba8bb16463914266dd5a04fe2a3eac4ee129ae
PKG_VERSION=fc0a9470536bc760998827a6dc05d7dedf7b93ad
PKG_LICENSE=LGPL-2.1

.PHONY: all
Expand Down
21 changes: 19 additions & 2 deletions sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ extern "C" {
* @brief Size of the buffer used to write options, other than Uri-Path, in a
* request
*
* Accommodates Content-Format.
* Accommodates Content-Format and Uri-Queries
*/
#define GCOAP_REQ_OPTIONS_BUF (8)
#define GCOAP_REQ_OPTIONS_BUF (40)

/**
* @brief Size of the buffer used to write options in a response
Expand Down Expand Up @@ -654,6 +654,23 @@ uint8_t gcoap_op_state(void);
*/
int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);

/**
* @brief Adds a single Uri-Query option to a CoAP request
*
* To add multiple Uri-Query options, simply call this function multiple times.
* The Uri-Query options will be added in the order those calls.
*
* @param[out] pdu The package that is being build
* @param[in] key Key to add to the query string
* @param[in] val Value to assign to @p key (may be NULL)
*
* @pre ((pdu != NULL) && (key != NULL))
*
* @return overall length of new query string
* @return -1 on error
*/
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);

#ifdef __cplusplus
}
#endif
Expand Down
39 changes: 36 additions & 3 deletions sys/net/application_layer/coap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,24 @@ static ssize_t _write_options(coap_pkt_t *pdu, uint8_t *buf, size_t len)
DEBUG("gcoap: _write_options: path does not start with '/'\n");
return -EINVAL;
}
bufpos += coap_put_option_url(bufpos, last_optnum, (char *)&pdu->url[0]);
bufpos += coap_put_option_uri(bufpos, last_optnum, (char *)pdu->url,
COAP_OPT_URI_PATH);
last_optnum = COAP_OPT_URI_PATH;
}
}

/* Content-Format */
if (pdu->content_type != COAP_FORMAT_NONE) {
bufpos += coap_put_option_ct(bufpos, last_optnum, pdu->content_type);
/* uncomment when add an option after Content-Format */
/* last_optnum = COAP_OPT_CONTENT_FORMAT; */
last_optnum = COAP_OPT_CONTENT_FORMAT;
}

/* Uri-query for requests */
if (coap_get_code_class(pdu) == COAP_CLASS_REQ) {
bufpos += coap_put_option_uri(bufpos, last_optnum, (char *)pdu->qs,
COAP_OPT_URI_QUERY);
/* uncomment when further options are added below ... */
/* last_optnum = COAP_OPT_URI_QUERY; */
}

/* write payload marker */
Expand Down Expand Up @@ -602,6 +610,7 @@ int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code,

pdu->hdr = (coap_hdr_t *)buf;
memset(pdu->url, 0, NANOCOAP_URL_MAX);
memset(pdu->qs, 0, NANOCOAP_QS_MAX);

/* generate token */
#if GCOAP_TOKENLEN
Expand Down Expand Up @@ -839,4 +848,28 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf)
return (int)pos;
}

int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
{
size_t qs_len = strlen((char *)pdu->qs);
size_t key_len = strlen(key);
size_t val_len = (val) ? (strlen(val) + 1) : 0;

/* make sure if url_len + the new query string fit into the url buffer */
if ((qs_len + key_len + val_len + 2) >= NANOCOAP_QS_MAX) {
return -1;
}

pdu->qs[qs_len++] = '&';
memcpy(&pdu->qs[qs_len], key, key_len);
qs_len += key_len;
if (val) {
pdu->qs[qs_len++] = '=';
memcpy(&pdu->qs[qs_len], val, val_len);
qs_len += val_len;
}
pdu->qs[qs_len] = '\0';

return (int)qs_len;
}

/** @} */

0 comments on commit fb07d98

Please sign in to comment.