Skip to content

Commit

Permalink
nanocoap: Add coap_opt_add_opaque
Browse files Browse the repository at this point in the history
This option complements the existing coap_opt_add_{uint,string} and even
more special-purpose functions; its implementation is trivial given the
existing static _add_opt_pkt function.

The method is useful when working with ETags (ETag, If-Match options).
  • Loading branch information
chrysn committed Apr 12, 2019
1 parent 364499f commit a514609
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sys/include/net/nanocoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,23 @@ size_t coap_put_block1_ok(uint8_t *pkt_pos, coap_block1_t *block1, uint16_t last
*/
ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator);

/**
* @brief Encode the given buffer as an opaque data option into pkt
*
* @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] val pointer to the value to be set
* @param[in] val_len length of val
*
* @return number of bytes written to buffer
* @return <0 on error
* @return -ENOSPC if no available options
*/
ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, const uint8_t *val, size_t val_len);

/**
* @brief Encode the given uint option into pkt
*
Expand Down
5 changes: 5 additions & 0 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ ssize_t coap_opt_add_string(coap_pkt_t *pkt, uint16_t optnum, const char *string
return write_len;
}

ssize_t coap_opt_add_opaque(coap_pkt_t *pkt, uint16_t optnum, uint8_t *val, size_t val_len)
{
return _add_opt_pkt(pkt, optnum, val, val_len);
}

ssize_t coap_opt_add_uint(coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
{
uint32_t tmp = value;
Expand Down

0 comments on commit a514609

Please sign in to comment.