Skip to content

Commit

Permalink
gcoap: add helper function to get request header from a request memo
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed May 12, 2022
1 parent 02fb040 commit 584681f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
17 changes: 17 additions & 0 deletions sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,23 @@ ssize_t gcoap_encode_link(const coap_resource_t *resource, char *buf,
sock_dtls_t *gcoap_get_sock_dtls(void);
#endif

/**
* @brief Get the header of a request from a @ref gcoap_request_memo_t
*
* @param[in] memo A request memo. Must not be NULL.
*
* @return The request header for the given request memo.
*/
static inline coap_hdr_t *gcoap_request_memo_get_hdr(const gcoap_request_memo_t *memo)
{
if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
return (coap_hdr_t *)&memo->msg.hdr_buf[0];
}
else {
return (coap_hdr_t *)memo->msg.data.pdu_buf;
}
}

#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 1 addition & 6 deletions sys/net/application_layer/gcoap/forward_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,8 @@ static void _forward_resp_handler(const gcoap_request_memo_t *memo,
&cep->ep);
#if IS_USED(MODULE_NANOCOAP_CACHE)
coap_pkt_t req;
if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
req.hdr = (coap_hdr_t *) &memo->msg.hdr_buf[0];
}
else {
req.hdr = (coap_hdr_t *) memo->msg.data.pdu_buf;
}

req.hdr = gcoap_request_memo_get_hdr(memo);
size_t pdu_len = pdu->payload_len +
(pdu->payload - (uint8_t *)pdu->hdr);
nanocoap_cache_process(cep->cache_key, coap_get_code(&req), pdu, pdu_len);
Expand Down
8 changes: 2 additions & 6 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,12 +833,8 @@ static void _expire_request(gcoap_request_memo_t *memo)
/* Pass response to handler */
if (memo->resp_handler) {
coap_pkt_t req;
if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
req.hdr = (coap_hdr_t *)&memo->msg.hdr_buf[0]; /* for reference */
}
else {
req.hdr = (coap_hdr_t *)memo->msg.data.pdu_buf;
}

req.hdr = gcoap_request_memo_get_hdr(memo);
memo->resp_handler(memo, &req, NULL);
}
if (memo->send_limit != GCOAP_SEND_LIMIT_NON) {
Expand Down

0 comments on commit 584681f

Please sign in to comment.