Skip to content

Commit 1d3e727

Browse files
committed
[nrf fromtree] net: lib: coap_client: Don't clear internal structures on response
When response is received and handled, don't just clear the structure but instead mark it as ongoing=false. So if we later on receive a duplicate response for it, we can still respond with Ack or Rst. This is achieved by using release_internal_request() when we don't expect any response for it and reset_internal_request() when we really fill up a new request. Signed-off-by: Seppo Takalo <[email protected]> (cherry picked from commit 41ee35a)
1 parent e951add commit 1d3e727

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

subsys/net/lib/coap/coap_client.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,24 @@ static int receive(int sock, void *buf, size_t max_len, int flags,
6161
return err;
6262
}
6363

64+
/** Reset all fields to zero.
65+
* Use when a new request is filled in.
66+
*/
6467
static void reset_internal_request(struct coap_client_internal_request *request)
6568
{
66-
request->offset = 0;
67-
request->last_id = 0;
68-
request->last_response_id = -1;
69+
*request = (struct coap_client_internal_request){
70+
.last_response_id = -1,
71+
};
72+
}
73+
74+
/** Release a request structure.
75+
* Use when a request is no longer needed, but we might still receive
76+
* responses for it, which must be handled.
77+
*/
78+
static void release_internal_request(struct coap_client_internal_request *request)
79+
{
6980
request->request_ongoing = false;
70-
request->is_observe = false;
7181
request->pending.timeout = 0;
72-
request->recv_blk_ctx = (struct coap_block_context){ 0 };
73-
request->send_blk_ctx = (struct coap_block_context){ 0 };
7482
}
7583

7684
static int coap_client_schedule_poll(struct coap_client *client, int sock,
@@ -417,6 +425,7 @@ int coap_client_req(struct coap_client *client, int sock, const struct sockaddr
417425

418426
coap_pending_cycle(&internal_req->pending);
419427
internal_req->is_observe = coap_request_is_observe(&internal_req->request);
428+
LOG_DBG("Request is_observe %d", internal_req->is_observe);
420429
}
421430

422431
ret = send_request(sock, internal_req->request.data, internal_req->request.offset, 0,
@@ -513,7 +522,7 @@ static void coap_client_resend_handler(struct coap_client *client)
513522
ret = resend_request(client, &client->requests[i]);
514523
if (ret < 0) {
515524
report_callback_error(&client->requests[i], ret);
516-
reset_internal_request(&client->requests[i]);
525+
release_internal_request(&client->requests[i]);
517526
}
518527
}
519528
}
@@ -745,7 +754,7 @@ static int handle_response(struct coap_client *client, const struct coap_packet
745754
return 0;
746755
}
747756
report_callback_error(internal_req, -ECONNRESET);
748-
reset_internal_request(internal_req);
757+
release_internal_request(internal_req);
749758
return 0;
750759
}
751760

@@ -931,7 +940,7 @@ static int handle_response(struct coap_client *client, const struct coap_packet
931940
}
932941
fail:
933942
if (ret < 0 || !internal_req->is_observe) {
934-
reset_internal_request(internal_req);
943+
release_internal_request(internal_req);
935944
}
936945
return ret;
937946
}
@@ -949,7 +958,7 @@ static void cancel_requests_with(struct coap_client *client, int error)
949958
* request was cancelled anyway.
950959
*/
951960
report_callback_error(&client->requests[i], error);
952-
reset_internal_request(&client->requests[i]);
961+
release_internal_request(&client->requests[i]);
953962
}
954963
}
955964
k_mutex_unlock(&client->lock);

0 commit comments

Comments
 (0)