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 820ca61 commit cbbde07
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 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 @@ -314,13 +314,8 @@ static void _forward_resp_handler(const gcoap_request_memo_t *memo,
}
else {
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
15 changes: 3 additions & 12 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,8 @@ static void _find_req_memo(gcoap_request_memo_t **memo_ptr, coap_pkt_t *src_pdu,
}

gcoap_request_memo_t *memo = &_coap_state.open_reqs[i];
if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
memo_pdu->hdr = (coap_hdr_t *) &memo->msg.hdr_buf[0];
}
else {
memo_pdu->hdr = (coap_hdr_t *) memo->msg.data.pdu_buf;
}

memo_pdu->hdr = gcoap_request_memo_get_hdr(memo);
if (by_mid) {
if ((src_pdu->hdr->id == memo_pdu->hdr->id)
&& sock_udp_ep_equal(&memo->remote_ep, remote)) {
Expand All @@ -833,12 +828,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 cbbde07

Please sign in to comment.