Skip to content

Commit

Permalink
coap_response_handler: Tidy up sent parameter
Browse files Browse the repository at this point in the history
sent PDU passed to a response handler is now reset to initial sent PDU
(token restored, Block options removed), but some or all of original
data may be missing if Block operations were needed for the request or
the repsonse.

Updated documentation to reflect this.
  • Loading branch information
mrdeep1 committed Oct 23, 2023
1 parent 203d228 commit 7688258
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions man/coap_handler.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ this is an ACK or RST response to the request. In general, matching of
Requests and Responses whould be done by generating unique Tokens for each
Request and then matching up based on the Token in _received_ Response.

*NOTE:* _sent_ (if not NULL) may not contain all or any of the data used for the
initial request if block transfers are being used. For Block1 requests, the
complete data will be lost once the data has been successfully transmitted as
acknowledged by the first response. For Block2 responses, _sent_ gets re-used
to request the next Block2 and so data information is lost.

*NOTE:* If the returned value is COAP_RESPONSE_FAIL, then a CoAP RST packet
will get sent to the server by libcoap. The returned value of COAP_RESPONSE_OK
indicates that all is OK.
Expand Down
32 changes: 32 additions & 0 deletions src/coap_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -3316,6 +3316,15 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
coap_show_pdu(COAP_LOG_DEBUG, rcvd);
}

if (sent) {
/* need to put back original token into sent */
if (p->b.b1.app_token)
coap_update_token(sent, p->b.b1.app_token->length,
p->b.b1.app_token->s);
if (sent->lg_xmit)
coap_remove_option(sent, sent->lg_xmit->option);
sent->lg_xmit = NULL;
}
LL_DELETE(session->lg_xmit, p);
coap_block_delete_lg_xmit(session, p);
return 0;
Expand Down Expand Up @@ -3693,6 +3702,14 @@ coap_handle_response_get_block(coap_context_t *context,
#endif /* ! COAP_Q_BLOCK_SUPPORT */
coap_log_debug("Client app version of updated PDU\n");
coap_show_pdu(COAP_LOG_DEBUG, rcvd);

if (sent) {
/* need to put back original token into sent */
if (p->app_token)
coap_update_token(sent, p->app_token->length,
p->app_token->s);
coap_remove_option(sent, p->block_option);
}
goto call_app_handler;
}
#if COAP_Q_BLOCK_SUPPORT
Expand Down Expand Up @@ -3730,6 +3747,13 @@ coap_handle_response_get_block(coap_context_t *context,
coap_log_debug("Client app version of updated PDU\n");
coap_show_pdu(COAP_LOG_DEBUG, rcvd);
}
if (sent) {
/* need to put back original token into sent */
if (p->app_token)
coap_update_token(sent, p->app_token->length,
p->app_token->s);
coap_remove_option(sent, p->block_option);
}
if (context->response_handler(session, sent, rcvd,
rcvd->mid) == COAP_RESPONSE_FAIL)
coap_send_rst(session, rcvd);
Expand Down Expand Up @@ -3879,6 +3903,14 @@ coap_handle_response_get_block(coap_context_t *context,
coap_log_debug("Client app version of updated PDU\n");
coap_show_pdu(COAP_LOG_DEBUG, rcvd);
}

if (sent) {
/* need to put back original token into sent */
if (p->app_token)
coap_update_token(sent, p->app_token->length,
p->app_token->s);
coap_remove_option(sent, p->block_option);
}
/* Expire this entry */
LL_DELETE(session->lg_crcv, p);
coap_block_delete_lg_crcv(session, p);
Expand Down

0 comments on commit 7688258

Please sign in to comment.