Skip to content

Commit 9d7797c

Browse files
tautologyclubplskeggs
authored andcommitted
[nrf fromtree] net: coap_client: handle observe requests as intended
The coap_client lib only handled "one-shot" requests properly. This patch allows it to keep listening for additional responses to a request, if the request was made with the CoAP OBSERVE option appended. An API for canceling such requests is also added. Signed-off-by: Benjamin Lindqvist <[email protected]> (cherry picked from commit 0d14143)
1 parent 41095df commit 9d7797c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

include/zephyr/net/coap_client.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ int coap_client_init(struct coap_client *client, const char *info);
137137
int coap_client_req(struct coap_client *client, int sock, const struct sockaddr *addr,
138138
struct coap_client_request *req, struct coap_transmission_parameters *params);
139139

140+
/**
141+
* @brief Cancel all current requests.
142+
*
143+
* This is intended for canceling long-running requests (e.g. GETs with the OBSERVE option set)
144+
* which has gone stale for some reason.
145+
*
146+
* @param client Client instance.
147+
*/
148+
void coap_client_cancel_requests(struct coap_client *client);
149+
150+
140151
/**
141152
* @}
142153
*/

subsys/net/lib/coap/coap_client.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,10 +811,19 @@ static int handle_response(struct coap_client *client, const struct coap_packet
811811
}
812812
fail:
813813
client->response_ready = false;
814-
internal_req->request_ongoing = false;
814+
if (ret != 0 || !coap_request_is_observe(&internal_req->request)) {
815+
internal_req->request_ongoing = false;
816+
}
815817
return ret;
816818
}
817819

820+
void coap_client_cancel_requests(struct coap_client *client)
821+
{
822+
for (int i = 0; i < ARRAY_SIZE(client->requests); i++) {
823+
client->requests[i].request_ongoing = false;
824+
}
825+
}
826+
818827
void coap_client_recv(void *coap_cl, void *a, void *b)
819828
{
820829
int ret;

0 commit comments

Comments
 (0)