Merge pull request #13726 from benpicco/nanocoap-payload_helper
nanocoap: add payload helper functions
This commit is contained in:
commit
595e8c631f
@ -78,6 +78,7 @@
|
|||||||
#define NET_NANOCOAP_H
|
#define NET_NANOCOAP_H
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@ -1565,6 +1566,63 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len);
|
|||||||
*/
|
*/
|
||||||
void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
|
void coap_pkt_init(coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Advance the payload pointer.
|
||||||
|
*
|
||||||
|
* @pre You added @p len bytes of data to `pkt->payload`.
|
||||||
|
*
|
||||||
|
* You can add payload to a CoAP request by writing data directly to
|
||||||
|
* `pkt->payload`.
|
||||||
|
* This convenience function takes care of advancing the payload pointer
|
||||||
|
* afterwards.
|
||||||
|
*
|
||||||
|
* @param[out] pkt pkt to which payload was added
|
||||||
|
* @param[in] len length of payload
|
||||||
|
*/
|
||||||
|
static inline void coap_payload_advance_bytes(coap_pkt_t *pkt, size_t len)
|
||||||
|
{
|
||||||
|
pkt->payload += len;
|
||||||
|
pkt->payload_len -= len;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Add payload data to the CoAP request.
|
||||||
|
*
|
||||||
|
* @pre @ref coap_opt_finish must have been called before with
|
||||||
|
* the @ref COAP_OPT_FINISH_PAYLOAD option.
|
||||||
|
*
|
||||||
|
* The function copies @p data into the payload buffer of @p pkt and
|
||||||
|
* advances the payload pointer.
|
||||||
|
*
|
||||||
|
* This is just a convenience function, you can also directly write
|
||||||
|
* to `pkt->payload` if you have a function that outputs payload to
|
||||||
|
* a buffer.
|
||||||
|
* In this case you should instead call @ref coap_payload_advance_bytes.
|
||||||
|
*
|
||||||
|
* @param[out] pkt pkt to add payload to
|
||||||
|
* @param[in] data payload data
|
||||||
|
* @param[in] len length of payload
|
||||||
|
*
|
||||||
|
* @returns number of payload bytes added on success
|
||||||
|
* @returns < 0 on error
|
||||||
|
*/
|
||||||
|
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Add a single character to the payload data of the CoAP request
|
||||||
|
*
|
||||||
|
* This function is used to add single characters to a CoAP payload data. It
|
||||||
|
* checks whether the character can be added to the buffer and ignores if the
|
||||||
|
* payload area is already exhausted.
|
||||||
|
*
|
||||||
|
* @param[out] pkt pkt to add payload to
|
||||||
|
* @param[in] c character to write
|
||||||
|
*
|
||||||
|
* @returns number of payload bytes added on success (always one)
|
||||||
|
* @returns < 0 on error
|
||||||
|
*/
|
||||||
|
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create CoAP reply (convenience function)
|
* @brief Create CoAP reply (convenience function)
|
||||||
*
|
*
|
||||||
|
|||||||
@ -936,6 +936,30 @@ ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
|
|||||||
return pkt->payload - (uint8_t *)pkt->hdr;
|
return pkt->payload - (uint8_t *)pkt->hdr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssize_t coap_payload_put_bytes(coap_pkt_t *pkt, const void *data, size_t len)
|
||||||
|
{
|
||||||
|
if (pkt->payload_len < len) {
|
||||||
|
return -ENOSPC;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(pkt->payload, data, len);
|
||||||
|
coap_payload_advance_bytes(pkt, len);
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t coap_payload_put_char(coap_pkt_t *pkt, char c)
|
||||||
|
{
|
||||||
|
if (pkt->payload_len < 1) {
|
||||||
|
return -ENOSPC;
|
||||||
|
}
|
||||||
|
|
||||||
|
*pkt->payload++ = c;
|
||||||
|
pkt->payload_len--;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
|
void coap_block_object_init(coap_block1_t *block, size_t blknum, size_t blksize,
|
||||||
int more)
|
int more)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user