Skip to content

Commit 5e43418

Browse files
Hubert Miśjukkar
authored andcommitted
net: coap: acknowledgement initialization helper
When handling CoAP Confirmable requests, there is a common Acknowledgement initialization procedure that repeats for each response packet initialization. This patch adds a function that simplifies Acknowledgement initialization procedure encapsulating repeating code. Signed-off-by: Hubert Miś <[email protected]>
1 parent 9831ddd commit 5e43418

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

include/net/coap.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,24 @@ int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len,
376376
uint8_t ver, uint8_t type, uint8_t token_len,
377377
const uint8_t *token, uint8_t code, uint16_t id);
378378

379+
/**
380+
* @brief Create a new CoAP Acknowledgment message for given request.
381+
*
382+
* This function works like @ref coap_packet_init, filling CoAP header type,
383+
* CoAP header token, and CoAP header message id fields according to
384+
* acknowledgment rules.
385+
*
386+
* @param cpkt New packet to be initialized using the storage from @a data.
387+
* @param req CoAP request packet that is being acknowledged
388+
* @param data Data that will contain a CoAP packet information
389+
* @param max_len Maximum allowable length of data
390+
* @param code CoAP header code
391+
*
392+
* @return 0 in case of success or negative in case of error.
393+
*/
394+
int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
395+
uint8_t *data, uint16_t max_len, uint8_t code);
396+
379397
/**
380398
* @brief Returns a randomly generated array of 8 bytes, that can be
381399
* used as a message's token.

subsys/net/lib/coap/coap.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len,
146146
return 0;
147147
}
148148

149+
int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
150+
uint8_t *data, uint16_t max_len, uint8_t code)
151+
{
152+
uint16_t id;
153+
uint8_t ver;
154+
uint8_t tkl;
155+
uint8_t token[COAP_TOKEN_MAX_LEN];
156+
157+
ver = coap_header_get_version(req);
158+
id = coap_header_get_id(req);
159+
tkl = code ? coap_header_get_token(req, token) : 0;
160+
161+
return coap_packet_init(cpkt, data, max_len, ver, COAP_TYPE_ACK, tkl,
162+
token, code, id);
163+
}
164+
149165
static void option_header_set_delta(uint8_t *opt, uint8_t delta)
150166
{
151167
*opt = (delta & 0xF) << 4;

0 commit comments

Comments
 (0)