Skip to content

Commit

Permalink
block handling: Update lg_crcv->last_used when sending Block1
Browse files Browse the repository at this point in the history
This prevents premature de-allocation of the lg_crcv when large
files are getting uploaded.
  • Loading branch information
mrdeep1 committed Jul 16, 2024
1 parent bfdbdc3 commit 7551d2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/coap3/coap_block_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ struct coap_lg_xmit_t {
coap_l_block1_t b1;
coap_l_block2_t b2;
} b;
coap_lg_crcv_t *lg_crcv; /**< The lg_crcv associated with this blocked xmit */
coap_pdu_t pdu; /**< skeletal PDU */
coap_tick_t last_payload; /**< Last time MAX_PAYLOAD was sent or 0 */
coap_tick_t last_sent; /**< Last time any data sent */
Expand Down
33 changes: 22 additions & 11 deletions src/coap_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -2285,20 +2285,31 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu,
coap_remove_option(&lg_crcv->pdu, COAP_OPTION_BLOCK1);
}

if (lg_xmit)
lg_xmit->lg_crcv = lg_crcv;

return lg_crcv;
}

void
coap_block_delete_lg_crcv(coap_session_t *session,
coap_lg_crcv_t *lg_crcv) {
size_t i;
coap_lg_xmit_t *lg_xmit;

#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
(void)session;
#endif
if (lg_crcv == NULL)
return;

LL_FOREACH(session->lg_xmit, lg_xmit) {
if (lg_xmit->lg_crcv == lg_crcv) {
lg_xmit->lg_crcv = NULL;
break;
}
}

if (lg_crcv->pdu.token)
coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.max_hdr_size);
coap_free_type(COAP_STRING, lg_crcv->body_data);
Expand Down Expand Up @@ -3326,6 +3337,10 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
size_t chunk = (size_t)1 << (lg_xmit->blk_size + 4);
coap_block_b_t block;

lg_crcv = lg_xmit->lg_crcv;
if (lg_crcv)
coap_ticks(&lg_crcv->last_used);

if (COAP_RESPONSE_CLASS(rcvd->code) == 2 &&
coap_get_block_b(session, rcvd, lg_xmit->option, &block)) {

Expand Down Expand Up @@ -3391,7 +3406,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,

if (lg_xmit->pdu.code == COAP_REQUEST_CODE_FETCH) {
/* Need to handle Observe for large FETCH */
LL_FOREACH(session->lg_crcv, lg_crcv) {
if (lg_crcv) {
if (coap_binary_equal(lg_xmit->b.b1.app_token, lg_crcv->app_token)) {
coap_bin_const_t *new_token;
coap_bin_const_t ctoken = { len, buf };
Expand All @@ -3404,7 +3419,6 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
len = new_token->length;
memcpy(buf, new_token->s, len);
}
break;
}
}
}
Expand Down Expand Up @@ -3571,15 +3585,12 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
/* There has been an internal error of some sort */
rcvd->code = COAP_RESPONSE_CODE(500);
lg_xmit_finished:
if (session->lg_crcv) {
LL_FOREACH(session->lg_crcv, lg_crcv) {
if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
STATE_TOKEN_BASE(lg_crcv->state_token)) {
/* In case of observe */
lg_crcv->state_token = lg_xmit->b.b1.state_token;
lg_crcv->retry_counter = lg_xmit->b.b1.count;
break;
}
if (lg_crcv) {
if (STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) ==
STATE_TOKEN_BASE(lg_crcv->state_token)) {
/* In case of observe */
lg_crcv->state_token = lg_xmit->b.b1.state_token;
lg_crcv->retry_counter = lg_xmit->b.b1.count;
}
}
if (!lg_crcv) {
Expand Down

0 comments on commit 7551d2e

Please sign in to comment.