Skip to content

Commit

Permalink
fixup! sys/net/nanocoap: improve separate response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Jan 22, 2025
1 parent b045d8c commit 13fe953
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static ssize_t _separate_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap
static event_callback_t event_timed = EVENT_CALLBACK_INIT(_send_response, &_separate_ctx);

if (event_timeout_is_pending(&event_timeout)) {
if (nanocoap_is_duplicate_in_separate_ctx(&_separate_ctx, pkt, context)) {
if (nanocoap_server_is_remote_in_response_ctx(&_separate_ctx, context)) {
/* duplicate of the request a separate response is already scheduled
* for --> resending the ACK */
return coap_build_empty_ack(pkt, (void *)buf);
Expand Down
17 changes: 8 additions & 9 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,17 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
coap_pkt_t *pkt, const coap_request_ctx_t *req);

/**
* @brief Check if given request is a duplicate of the one a separate response
* was prepared for
* @brief Check if a given separate response context was prepared for the
* remote endpoint of a given request
*
* @param[in] ctx Context to check against
* @param[in] pkt Possible duplication
* @param[in] req Request context of the possible duplicate
* @param[in] ctx Separate response context to check
* @param[in] req Request from the remote to check for
*
* @retval true @p pkt is a duplicate
* @retval false @p pkt is *NOT* a duplicate
* @retval true The remote endpoint given by @p req is in @p ctx
* @retval false @p ctx was prepared for a different remote endpoint
*/
bool nanocoap_is_duplicate_in_separate_ctx(const nanocoap_server_response_ctx_t *ctx,
coap_pkt_t *pkt, const coap_request_ctx_t *req);
bool nanocoap_server_is_remote_in_response_ctx(const nanocoap_server_response_ctx_t *ctx,
const coap_request_ctx_t *req);

/**
* @brief Build and send a separate response to a CoAP request
Expand Down
9 changes: 2 additions & 7 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,14 +1142,9 @@ int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx,
return 0;
}

bool nanocoap_is_duplicate_in_separate_ctx(const nanocoap_server_response_ctx_t *ctx,
coap_pkt_t *pkt, const coap_request_ctx_t *req)
bool nanocoap_is_remote_already_registered(const nanocoap_server_response_ctx_t *ctx,
const coap_request_ctx_t *req)
{
/* We assume NSTART == 1 and do not actually look into the packet here.
* But having the API prepared to allow also comparing token and message ID
* is probably a good thing. */
(void)pkt;

return sock_udp_ep_equal(&ctx->remote, req->remote);
}

Expand Down

0 comments on commit 13fe953

Please sign in to comment.