Skip to content

Commit

Permalink
Hack to reopen sockets after 55s of silence. This is to force DTLS se…
Browse files Browse the repository at this point in the history
…ssion resumption in NB-IoT networks
  • Loading branch information
JusbeR committed Jun 4, 2020
1 parent ff214d6 commit a35f03c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ static struct lwm2m_engine_obj_inst *get_engine_obj_inst(int obj_id,
/* Shared set of in-flight LwM2M messages */
static struct lwm2m_message messages[CONFIG_LWM2M_ENGINE_MAX_MESSAGES];

static s64_t last_msg_sent_time_ms = -1;

/* for debugging: to print IP addresses */
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr)
{
Expand Down Expand Up @@ -1015,13 +1017,26 @@ int lwm2m_init_message(struct lwm2m_message *msg)
return r;
}

void reopen_sockets(struct lwm2m_ctx *client_ctx);

int lwm2m_send_message(struct lwm2m_message *msg)
{
if (!msg || !msg->ctx) {
LOG_ERR("LwM2M message is invalid.");
return -EINVAL;
}


if (last_msg_sent_time_ms >= 0) {
s64_t reftime = last_msg_sent_time_ms;
s64_t elapsed_time = k_uptime_delta(&reftime);

if (elapsed_time > 55 * 1000) {
reopen_sockets(msg->ctx);
}
}
last_msg_sent_time_ms = k_uptime_get();

if (msg->type == COAP_TYPE_CON) {
coap_pending_cycle(msg->pending);
}
Expand Down Expand Up @@ -4354,4 +4369,25 @@ static int lwm2m_engine_init(struct device *dev)
return ret;
}

void reopen_sockets(struct lwm2m_ctx *ctx) {
for (int i = 0; i < sock_nfds; i++) {
if (sock_ctx[i] == ctx) {
//printk("JRJR: Found sock: %d\n", sock_fds[i].fd);
int err = close(sock_fds[i].fd);
if(err) {
LOG_ERR("Close failed: %d", err);
}
lwm2m_socket_del(ctx);
err = lwm2m_socket_start(ctx);
if(err) {
LOG_ERR("lwm2m_socket_start failed: %d", err);
// TODO: What to do here?
} else {
LOG_DBG("Socket reopened");
}
break; // TODO: Can we have many here?
}
}
}

SYS_INIT(lwm2m_engine_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

3 comments on commit a35f03c

@JPSELC
Copy link

@JPSELC JPSELC commented on a35f03c Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reopen_sockets should return an error, because if coverage is lost, the lwm2m_socket_start will fail and ctx will become a null pointer that will cause a crash.

@JusbeR
Copy link
Author

@JusbeR JusbeR commented on a35f03c Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reopen_sockets should return an error, because if coverage is lost, the lwm2m_socket_start will fail and ctx will become a null pointer that will cause a crash.

Yeah, good point, thanks. So far I have not found any better solution for this problem, so I guess I just have to make this "official" at some point(before our product goes to production line)

I have also been pondering if I should make a new function or configuration option gor lwm2m engine, for setting the timeout value. I think it would be good to have it dynamically configurable parameter instead of some compilation time flag. Then you could detect from engine events if things take too long(retries when DTLS context had disappeared) and reduce the value until it starts working. Kind of like poor man's "operator firewall settings detection" algorithm. Thoughts @JPSELC ?

@JPSELC
Copy link

@JPSELC JPSELC commented on a35f03c Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is as good as it gets right now. Ultimately, we're waiting on : https://datatracker.ietf.org/doc/draft-ietf-tls-dtls-connection-id/history/ but it looks like that might take a while.
We are using a variable for timeout which should be updateable over the air. I don't think networks change that parameter very often. But safest thing is to keep the value small.
Reducing it on the fly could get tricky, especially in situations where you have poor/borderline coverage and retransmits are necessary

Please sign in to comment.