From cbbde07cc6352246e7a6aa3bec52cd390731cc26 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Thu, 12 May 2022 13:06:35 +0200 Subject: [PATCH] gcoap: add helper function to get request header from a request memo --- sys/include/net/gcoap.h | 17 +++++++++++++++++ sys/net/application_layer/gcoap/forward_proxy.c | 7 +------ sys/net/application_layer/gcoap/gcoap.c | 15 +++------------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/sys/include/net/gcoap.h b/sys/include/net/gcoap.h index 331e9c01dd78..9b6696b4185a 100644 --- a/sys/include/net/gcoap.h +++ b/sys/include/net/gcoap.h @@ -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 diff --git a/sys/net/application_layer/gcoap/forward_proxy.c b/sys/net/application_layer/gcoap/forward_proxy.c index c67030aa1364..c84c39dabef4 100644 --- a/sys/net/application_layer/gcoap/forward_proxy.c +++ b/sys/net/application_layer/gcoap/forward_proxy.c @@ -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); diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 3765c7710950..6f7beb9a49d7 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -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)) { @@ -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) {