net/coap: move/rename function to add query option
This commit is contained in:
parent
0d3180d534
commit
0ed4fe2da6
@ -142,12 +142,10 @@ extern "C" {
|
||||
#define NANOCOAP_BLOCK_SIZE_EXP_MAX (6)
|
||||
#endif
|
||||
|
||||
#if defined(MODULE_GCOAP) || defined(DOXYGEN)
|
||||
/** @brief Maximum length of a query string written to a message */
|
||||
#ifndef NANOCOAP_QS_MAX
|
||||
#define NANOCOAP_QS_MAX (64)
|
||||
#endif
|
||||
#endif
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
@ -964,6 +962,20 @@ static inline ssize_t coap_opt_add_format(coap_pkt_t *pkt, uint16_t format)
|
||||
*/
|
||||
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);
|
||||
|
||||
/**
|
||||
* @brief Adds a single Uri-Query option in the form 'key=value' into pkt
|
||||
*
|
||||
* @param[in,out] pkt 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 ((pkt != NULL) && (key != NULL))
|
||||
*
|
||||
* @return overall length of new query string
|
||||
* @return -1 on error
|
||||
*/
|
||||
ssize_t coap_opt_add_uquery(coap_pkt_t *pkt, const char *key, const char *val);
|
||||
|
||||
/**
|
||||
* @brief Encode the given string as option(s) into pkt
|
||||
*
|
||||
|
||||
@ -846,6 +846,29 @@ ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string
|
||||
return write_len;
|
||||
}
|
||||
|
||||
ssize_t coap_opt_add_uquery(coap_pkt_t *pdu, const char *key, const char *val)
|
||||
{
|
||||
char qs[NANOCOAP_QS_MAX];
|
||||
size_t len = strlen(key);
|
||||
size_t val_len = (val) ? (strlen(val) + 1) : 0;
|
||||
|
||||
/* test if the query string fits, account for the zero termination */
|
||||
if ((len + val_len + 1) >= NANOCOAP_QS_MAX) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(&qs[0], key, len);
|
||||
if (val) {
|
||||
qs[len] = '=';
|
||||
/* the `=` character was already counted in `val_len`, so subtract it here */
|
||||
memcpy(&qs[len + 1], val, (val_len - 1));
|
||||
len += val_len;
|
||||
}
|
||||
qs[len] = '\0';
|
||||
|
||||
return coap_opt_add_string(pdu, COAP_OPT_URI_QUERY, qs, '&');
|
||||
}
|
||||
|
||||
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len)
|
||||
{
|
||||
return _add_opt_pkt(pkt, optnum, val, val_len);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user