Skip to content

Commit c3b19df

Browse files
juhaylinennordicjm
authored andcommitted
net: nrf_provisioning: Improve error handling
The provisioning thread should continue running even if the connection to the server is refused. Return errno value if socket call fails. Remove duplicate error prints. Signed-off-by: Juha Ylinen <[email protected]>
1 parent 5a901d1 commit c3b19df

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,9 @@ int nrf_provisioning_req(void)
629629
__ASSERT(false, "Invalid exchange, abort");
630630
LOG_ERR("Invalid exchange");
631631
} else if (ret == -ECONNREFUSED) {
632-
LOG_ERR("Connection refused, client exits");
632+
LOG_ERR("Connection refused");
633633
LOG_WRN("Please check the CA certificate stored in sectag "
634634
STRINGIFY(CONFIG_NRF_PROVISIONING_ROOT_CA_SEC_TAG)"");
635-
k_mutex_lock(&np_mtx, K_FOREVER);
636-
initialized = false;
637-
k_mutex_unlock(&np_mtx);
638-
break;
639635
} else if (ret < 0) {
640636
LOG_ERR("Provisioning failed, error: %d", ret);
641637
} else if (ret > 0) {

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

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int dtls_setup(int fd)
112112
err = zsock_setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
113113
if (err) {
114114
LOG_ERR("Failed to setup peer verification, err %d", errno);
115-
return err;
115+
return -errno;
116116
}
117117

118118
/* Associate the socket with the security tag
@@ -121,7 +121,7 @@ static int dtls_setup(int fd)
121121
err = zsock_setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag, sizeof(tls_sec_tag));
122122
if (err) {
123123
LOG_ERR("Failed to setup TLS sec tag, err %d", errno);
124-
return err;
124+
return -errno;
125125
}
126126

127127
if (IS_ENABLED(CONFIG_NRF_PROVISIONING_COAP_DTLS_SESSION_CACHE)) {
@@ -134,13 +134,13 @@ static int dtls_setup(int fd)
134134
&session_cache, sizeof(session_cache));
135135
if (err) {
136136
LOG_ERR("Failed to set TLS_SESSION_CACHE option: %d", errno);
137-
return err;
137+
return -errno;
138138
}
139139

140140
err = zsock_setsockopt(fd, SOL_TLS, TLS_HOSTNAME, COAP_HOST, strlen(COAP_HOST));
141141
if (err) {
142142
LOG_ERR("Failed to setup TLS hostname, err %d", errno);
143-
return err;
143+
return -errno;
144144
}
145145

146146
/* Enable connection ID */
@@ -149,7 +149,7 @@ static int dtls_setup(int fd)
149149
err = zsock_setsockopt(fd, SOL_TLS, TLS_DTLS_CID, &dtls_cid, sizeof(dtls_cid));
150150
if (err) {
151151
LOG_ERR("Failed to enable connection ID, err %d", errno);
152-
return err;
152+
return -errno;
153153
}
154154

155155
if (IS_ENABLED(CONFIG_SOC_NRF9120)) {
@@ -159,7 +159,7 @@ static int dtls_setup(int fd)
159159
if (err) {
160160
LOG_ERR("Failed to set socket option SO_KEEPOPEN, err %d", errno);
161161
socket_keep_open = false;
162-
return err;
162+
return -errno;
163163
}
164164
socket_keep_open = true;
165165
}
@@ -207,25 +207,19 @@ static int socket_connect(int *const fd)
207207
*fd = zsock_socket(address->ai_family, address->ai_socktype, IPPROTO_DTLS_1_2);
208208
if (*fd < 0) {
209209
LOG_ERR("Failed to create UDP socket %d", errno);
210-
ret = -ENOTCONN;
210+
ret = -errno;
211211
goto clean_up;
212212
}
213213

214214
/* Setup DTLS socket options */
215215
ret = dtls_setup(*fd);
216216
if (ret) {
217-
LOG_ERR("Failed to setup TLS socket option");
218-
ret = -EACCES;
219217
goto clean_up;
220218
}
221219

222220
if (zsock_connect(*fd, address->ai_addr, address->ai_addrlen) < 0) {
223221
LOG_ERR("Failed to connect UDP socket %d", errno);
224-
if (errno == ETIMEDOUT) {
225-
ret = -ETIMEDOUT;
226-
} else {
227-
ret = -ECONNREFUSED;
228-
}
222+
ret = -errno;
229223
goto clean_up;
230224
}
231225
LOG_INF("Connected");
@@ -573,8 +567,7 @@ int nrf_provisioning_coap_req(struct nrf_provisioning_coap_context *const coap_c
573567
coap_ctx->rx_buf_len = sizeof(rx_buf);
574568

575569
ret = socket_connect(&coap_ctx->connect_socket);
576-
if (ret) {
577-
LOG_ERR("Failed to connect socket");
570+
if (ret < 0) {
578571
return ret;
579572
}
580573

@@ -634,7 +627,6 @@ int nrf_provisioning_coap_req(struct nrf_provisioning_coap_context *const coap_c
634627
if (!socket_keep_open) {
635628
ret = socket_connect(&coap_ctx->connect_socket);
636629
if (ret < 0) {
637-
LOG_ERR("Failed to connect socket, error: %d", ret);
638630
break;
639631
}
640632

0 commit comments

Comments
 (0)