Skip to content

Commit

Permalink
net: lwm2m: Fix lwm2m_socket_start() error handling
Browse files Browse the repository at this point in the history
In case lwm2m_socket_start() internal error, it should only do cleanup
on the socket, i. e. call lwm2m_socket_close(), not lwm2m_engine_stop().
The latter resets the entire lwm2m_context, which results in removal of
active observations.

This should not be done, as it collides with the RD client logic, where
connection resumption may skip the full registration phase, in result
not notifying the server that it should restart the observations.

At the same time, the RD client should clean the lwm2m_context when it's
done trying to update the registration and proceeds with regular
registration/bootstrap in the network error handler. In that case, only
the socket was closed, so the lwm2m_context needs to be reset
separately.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
  • Loading branch information
rlubos authored and nashif committed Jun 6, 2023
1 parent 773198d commit 2e0b0af
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,8 +875,8 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
} else if ((client_ctx->remote_addr).sa_family == AF_INET6) {
addr_len = sizeof(struct sockaddr_in6);
} else {
lwm2m_engine_stop(client_ctx);
return -EPROTONOSUPPORT;
ret = -EPROTONOSUPPORT;
goto error;
}

if (zsock_connect(client_ctx->sock_fd, &client_ctx->remote_addr, addr_len) < 0) {
Expand All @@ -901,7 +901,7 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
LOG_INF("Connected, sock id %d", client_ctx->sock_fd);
return 0;
error:
lwm2m_engine_stop(client_ctx);
lwm2m_socket_close(client_ctx);
return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_rd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1219,13 +1219,15 @@ static void sm_do_network_error(void)

#if defined(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP)
if (client.ctx->bootstrap_mode) {
lwm2m_engine_context_close(client.ctx);
set_sm_state(ENGINE_DO_BOOTSTRAP_REG);
return;
}
#endif

if (!client.last_update || (k_uptime_get() - client.last_update) / 1000 > client.lifetime) {
/* do full registration as there is no active registration or lifetime exceeded */
lwm2m_engine_context_close(client.ctx);
set_sm_state(ENGINE_DO_REGISTRATION);
return;
}
Expand Down

0 comments on commit 2e0b0af

Please sign in to comment.