Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lwm2m boostrap fixes #1271

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ int lwm2m_engine_pause(void)
}

suspend_engine_thread = true;
lwm2m_engine_wake_up();

while (active_engine_thread) {
k_msleep(10);
Expand All @@ -1183,6 +1184,7 @@ int lwm2m_engine_resume(void)
}

k_thread_resume(engine_thread_id);
lwm2m_engine_wake_up();
while (!active_engine_thread) {
k_msleep(10);
}
Expand Down
28 changes: 18 additions & 10 deletions subsys/net/lib/lwm2m/lwm2m_rd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ static int sm_send_registration_msg(void);
static bool sm_is_suspended(void);
static void lwm2m_rd_client_service(struct k_work *work);
static int64_t calc_next_event(void);
static void set_sm_state_delayed(uint8_t sm_state, int64_t delay_ms);
static void set_sm_state(uint8_t sm_state);

/* The states for the RD client state machine */
/*
Expand Down Expand Up @@ -171,7 +173,7 @@ static void next_event_at(int64_t timestamp)
(void)lwm2m_engine_call_at(lwm2m_rd_client_service, timestamp);
}

static void set_sm_state(uint8_t sm_state)
static void set_sm_state_delayed(uint8_t sm_state, int64_t delay_ms)
{
k_mutex_lock(&client.mutex, K_FOREVER);
enum lwm2m_rd_client_event event = LWM2M_RD_CLIENT_EVENT_NONE;
Expand Down Expand Up @@ -232,10 +234,15 @@ static void set_sm_state(uint8_t sm_state)
lwm2m_close_socket(client.ctx);
}
}
next_event_at(0);
next_event_at(k_uptime_get() + delay_ms);
k_mutex_unlock(&client.mutex);
}

static void set_sm_state(uint8_t sm_state)
{
set_sm_state_delayed(sm_state, 0);
}

static bool sm_is_bootstrap(void)
{
#if defined(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP)
Expand Down Expand Up @@ -457,8 +464,7 @@ int engine_trigger_bootstrap(void)
rd_client_message_free();
client.use_bootstrap = true;
client.trigger_update = false;
client.engine_state = ENGINE_INIT;
next_event_at(0);
set_sm_state(ENGINE_INIT);
k_mutex_unlock(&client.mutex);
return 0;
#else
Expand Down Expand Up @@ -838,7 +844,10 @@ static int sm_do_bootstrap_reg(void)
void engine_bootstrap_finish(void)
{
LOG_INF("Bootstrap data transfer done!");
set_sm_state(ENGINE_BOOTSTRAP_TRANS_DONE);
/* Delay the state transition, so engine have some time to send ACK
* before we close the socket
*/
set_sm_state_delayed(ENGINE_BOOTSTRAP_TRANS_DONE, 1000);
}

static int sm_bootstrap_trans_done(void)
Expand Down Expand Up @@ -1003,10 +1012,9 @@ static void sm_handle_registration_update_failure(void)
{
k_mutex_lock(&client.mutex, K_FOREVER);
LOG_WRN("Registration Update fail -> trigger full registration");
client.engine_state = ENGINE_SEND_REGISTRATION;
lwm2m_engine_context_close(client.ctx);
set_sm_state(ENGINE_SEND_REGISTRATION);
k_mutex_unlock(&client.mutex);
next_event_at(0);
}

static int sm_send_registration_msg(void)
Expand Down Expand Up @@ -1470,7 +1478,7 @@ int lwm2m_rd_client_pause(void)
}

suspended_client_state = get_sm_state();
client.engine_state = ENGINE_SUSPENDED;
set_sm_state(ENGINE_SUSPENDED);

k_mutex_unlock(&client.mutex);

Expand Down Expand Up @@ -1557,8 +1565,8 @@ int lwm2m_rd_client_connection_resume(struct lwm2m_ctx *client_ctx)
} else {
client.engine_state = ENGINE_DO_REGISTRATION;
}
next_event_at(0);
}
next_event_at(0);

return 0;
}
Expand All @@ -1574,7 +1582,7 @@ int lwm2m_rd_client_timeout(struct lwm2m_ctx *client_ctx)
}
k_mutex_lock(&client.mutex, K_FOREVER);
LOG_WRN("Confirmable Timeout -> Re-connect and register");
client.engine_state = ENGINE_DO_REGISTRATION;
set_sm_state(ENGINE_DO_REGISTRATION);
next_event_at(0);
k_mutex_unlock(&client.mutex);
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME=y
26 changes: 17 additions & 9 deletions tests/net/lib/lwm2m/lwm2m_rd_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <zephyr/fff.h>
#include <zephyr/logging/log.h>
#include <zephyr/ztest.h>

#if defined(CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME)
#include "timer_model.h"
#endif
#include <lwm2m_rd_client.h>

LOG_MODULE_REGISTER(lwm2m_rd_client_test);
Expand Down Expand Up @@ -127,6 +129,12 @@ static void lwm2m_observe_cb(enum lwm2m_observe_event event, struct lwm2m_obj_pa

static void my_suite_before(void *data)
{
#if defined(CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME)
/* It is enough that some slow-down is happening on sleeps, it does not have to be
* real time
*/
hwtimer_set_rt_ratio(100.0);
#endif
/* Register resets */
DO_FOREACH_FAKE(RESET_FAKE);

Expand Down Expand Up @@ -185,7 +193,7 @@ ZTEST(lwm2m_rd_client, test_start_registration_ok)

coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT, 2), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT, 1), NULL);
zassert_true(!lwm2m_rd_client_is_registred(&ctx), NULL);
}

Expand All @@ -212,7 +220,7 @@ ZTEST(lwm2m_rd_client, test_timeout_resume_registration)
NULL);

zassert(lwm2m_rd_client_timeout(&ctx) == 0, "");
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 2),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 1),
NULL);

}
Expand Down Expand Up @@ -282,7 +290,7 @@ ZTEST(lwm2m_rd_client, test_start_registration_update)
NULL);

lwm2m_rd_client_update();
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 3),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 2),
NULL);
}

Expand Down Expand Up @@ -339,7 +347,7 @@ ZTEST(lwm2m_rd_client, test_start_registration_update_fail)
RESET_FAKE(coap_header_get_code);

lwm2m_rd_client_update();
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE, 3),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE, 2),
NULL);
}

Expand Down Expand Up @@ -478,11 +486,11 @@ ZTEST(lwm2m_rd_client, test_suspend_resume_registration)
zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);

zassert_true(lwm2m_rd_client_pause() == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED, 2), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED, 1), NULL);
zassert_true(lwm2m_rd_client_is_suspended(&ctx), NULL);

zassert_true(lwm2m_rd_client_resume() == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 4),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 3),
NULL);
zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);
}
Expand Down Expand Up @@ -510,7 +518,7 @@ ZTEST(lwm2m_rd_client, test_socket_error)
NULL);

ctx.fault_cb(EIO);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE, 2), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 3),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE, 1), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 2),
NULL);
}