Skip to content

Commit

Permalink
net: coap: Add API to send reset message
Browse files Browse the repository at this point in the history
Add helper API to construct CoAP Reset message.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
  • Loading branch information
SeppoTakalo authored and mmahadevan108 committed Oct 31, 2024
1 parent 1890dbd commit e96e95b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/zephyr/net/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,21 @@ int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len,
int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
uint8_t *data, uint16_t max_len, uint8_t code);

/**
* @brief Create a new CoAP Reset message for given request.
*
* This function works like @ref coap_packet_init, filling CoAP header type,
* and CoAP header message id fields.
*
* @param cpkt New packet to be initialized using the storage from @a data.
* @param req CoAP request packet that is being acknowledged
* @param data Data that will contain a CoAP packet information
* @param max_len Maximum allowable length of data
*
* @return 0 in case of success or negative in case of error.
*/
int coap_rst_init(struct coap_packet *cpkt, const struct coap_packet *req,
uint8_t *data, uint16_t max_len);
/**
* @brief Returns a randomly generated array of 8 bytes, that can be
* used as a message's token.
Expand Down
13 changes: 13 additions & 0 deletions subsys/net/lib/coap/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
token, code, id);
}

int coap_rst_init(struct coap_packet *cpkt, const struct coap_packet *req,
uint8_t *data, uint16_t max_len)
{
uint16_t id;
uint8_t ver;

ver = coap_header_get_version(req);
id = coap_header_get_id(req);

return coap_packet_init(cpkt, data, max_len, ver, COAP_TYPE_RESET, 0,
NULL, 0, id);
}

static void option_header_set_delta(uint8_t *opt, uint8_t delta)
{
*opt = (delta & 0xF) << 4;
Expand Down

0 comments on commit e96e95b

Please sign in to comment.