Skip to content

Commit d6706a1

Browse files
committed
net: nrf_provisioning: Don't dead-block on send failure
When sending a response, a coap_client might fail. Don't dead-block, but instead return the error to upper layer. If sending of response failed while we kept the socket open, try reconnecting the DTLS session. This allows falling back from IPv6 to IPv4. Signed-off-by: Seppo Takalo <[email protected]>
1 parent 7ce4395 commit d6706a1

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

subsys/net/lib/nrf_provisioning/src/nrf_provisioning_coap.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ static int send_coap_request(struct coap_client *client, uint8_t method, const c
309309
const uint8_t *payload, size_t len,
310310
struct nrf_provisioning_coap_context *const coap_ctx, bool confirmable)
311311
{
312+
int ret;
312313
int retries = 0;
313314
struct coap_transmission_parameters params = coap_get_transmission_parameters();
314315
static struct coap_client_option block2_option;
@@ -336,18 +337,21 @@ static int send_coap_request(struct coap_client *client, uint8_t method, const c
336337
client_request.num_options = 1;
337338
}
338339

339-
while (coap_client_req(client, coap_ctx->connect_socket, NULL, &client_request, NULL) ==
340-
-EAGAIN) {
340+
while ((ret = coap_client_req(client, coap_ctx->connect_socket, NULL, &client_request,
341+
NULL)) == -EAGAIN) {
341342
if (retries > RETRY_AMOUNT) {
342343
break;
343344
}
344345
LOG_DBG("CoAP client busy");
345346
k_sleep(K_MSEC(params.ack_timeout));
346347
retries++;
347348
}
348-
k_sem_take(&coap_response, K_FOREVER);
349349

350-
return 0;
350+
if (ret == 0) {
351+
return k_sem_take(&coap_response, K_SECONDS(240));
352+
}
353+
354+
return ret;
351355
}
352356

353357
static int max_token_len(void)
@@ -583,6 +587,7 @@ int nrf_provisioning_coap_req(struct nrf_provisioning_coap_context *const coap_c
583587
char *auth_token = NULL;
584588
struct cdc_context cdc_ctx;
585589
bool finished = false;
590+
int retries = 0;
586591

587592
coap_ctx->rx_buf = rx_buf;
588593
coap_ctx->rx_buf_len = sizeof(rx_buf);
@@ -644,7 +649,7 @@ int nrf_provisioning_coap_req(struct nrf_provisioning_coap_context *const coap_c
644649
LOG_INF("Finished");
645650
finished = true;
646651
}
647-
652+
retry_response:
648653
if (!socket_keep_open) {
649654
ret = socket_connect(&coap_ctx->connect_socket);
650655
if (ret < 0) {
@@ -660,6 +665,14 @@ int nrf_provisioning_coap_req(struct nrf_provisioning_coap_context *const coap_c
660665
LOG_INF("Sending response to server");
661666
ret = send_response(&client, coap_ctx, &cdc_ctx);
662667
if (ret < 0) {
668+
if (socket_keep_open && retries++ == 0) {
669+
/* Try reconnecting */
670+
socket_close(&coap_ctx->connect_socket);
671+
socket_keep_open = false;
672+
goto retry_response;
673+
}
674+
LOG_ERR("Failed to send response, ret %d", ret);
675+
663676
break;
664677
}
665678
/* Provisioning finished */

subsys/net/lib/nrf_provisioning/src/nrf_provisioning_codec.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ int nrf_provisioning_codec_process_commands(void)
542542

543543
/* Can't send anything if modem connection is not restored */
544544
if (mret < 0) {
545+
LOG_ERR("Failed to restore functional mode, error: %d", mret);
545546
return mret;
546547
}
547548

0 commit comments

Comments
 (0)