From 200d5d23be28ef2f5b807067817b73ab1132442d Mon Sep 17 00:00:00 2001 From: Giacomo Dematteis Date: Mon, 28 Apr 2025 14:15:12 +0200 Subject: [PATCH] app: refactor and align code Refactor and align code across modules. Signed-off-by: Giacomo Dematteis --- app/src/modules/cloud/cloud.c | 184 ++++++++---------- app/src/modules/environmental/environmental.c | 34 ++-- app/src/modules/fota/fota.c | 126 ++++++------ app/src/modules/location/location.c | 58 +++--- app/src/modules/network/network.c | 30 ++- app/src/modules/power/power.c | 38 ++-- 6 files changed, 219 insertions(+), 251 deletions(-) diff --git a/app/src/modules/cloud/cloud.c b/app/src/modules/cloud/cloud.c index 1346a494..71605515 100644 --- a/app/src/modules/cloud/cloud.c +++ b/app/src/modules/cloud/cloud.c @@ -99,31 +99,9 @@ ZBUS_CHAN_DEFINE(PRIV_CLOUD_CHAN, static void backoff_timer_work_fn(struct k_work *work); static K_WORK_DELAYABLE_DEFINE(backoff_timer_work, backoff_timer_work_fn); -/* Forward declarations of state handlers */ -static void state_running_entry(void *o); -static void state_running_run(void *o); - -static void state_disconnected_entry(void *o); -static void state_disconnected_run(void *o); - -static void state_connecting_entry(void *o); - -static void state_connecting_attempt_entry(void *o); - -static void state_connecting_backoff_entry(void *o); -static void state_connecting_backoff_run(void *o); -static void state_connecting_backoff_exit(void *o); - -static void state_connected_entry(void *o); -static void state_connected_exit(void *o); - -static void state_connected_ready_entry(void *o); -static void state_connected_ready_run(void *o); +/* State machine */ -static void state_connected_paused_entry(void *o); -static void state_connected_paused_run(void *o); - -/* Defining the hierarchical cloud module states: */ +/* Cloud module states */ enum cloud_module_state { /* The cloud module has started and is running */ STATE_RUNNING, @@ -146,7 +124,47 @@ enum cloud_module_state { STATE_CONNECTED_PAUSED, }; -/* Construct state table */ +/* State object. + * Used to transfer context data between state changes. + */ +struct cloud_state_object { + /* This must be first */ + struct smf_ctx ctx; + + /* Last channel type that a message was received on */ + const struct zbus_channel *chan; + + /* Last received message */ + uint8_t msg_buf[MAX_MSG_SIZE]; + + /* Network status */ + enum network_msg_type nw_status; + + /* Connection attempt counter. Reset when entering STATE_CONNECTING */ + uint32_t connection_attempts; + + /* Connection backoff time */ + uint32_t backoff_time; +}; + +/* Forward declarations of state handlers */ +static void state_running_entry(void *obj); +static void state_running_run(void *obj); +static void state_disconnected_entry(void *obj); +static void state_disconnected_run(void *obj); +static void state_connecting_entry(void *obj); +static void state_connecting_attempt_entry(void *obj); +static void state_connecting_backoff_entry(void *obj); +static void state_connecting_backoff_run(void *obj); +static void state_connecting_backoff_exit(void *obj); +static void state_connected_entry(void *obj); +static void state_connected_exit(void *obj); +static void state_connected_ready_entry(void *obj); +static void state_connected_ready_run(void *obj); +static void state_connected_paused_entry(void *obj); +static void state_connected_paused_run(void *obj); + +/* State machine definition */ static const struct smf_state states[] = { [STATE_RUNNING] = SMF_CREATE_STATE(state_running_entry, state_running_run, NULL, @@ -190,31 +208,7 @@ static const struct smf_state states[] = { NULL), }; -/* Cloud module state object. - * Used to transfer data between state changes. - */ -struct cloud_state { - /* This must be first */ - struct smf_ctx ctx; - - /* Last channel type that a message was received on */ - const struct zbus_channel *chan; - - /* Last received message */ - uint8_t msg_buf[MAX_MSG_SIZE]; - - /* Network status */ - enum network_msg_type nw_status; - - /* Connection attempt counter. Reset when entering STATE_CONNECTING */ - uint32_t connection_attempts; - - /* Connection backoff time */ - uint32_t backoff_time; -}; - -/* Static helper function */ -static void task_wdt_callback(int channel_id, void *user_data) +static void cloud_wdt_callback(int channel_id, void *user_data) { LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", channel_id, k_thread_name_get((k_tid_t)user_data)); @@ -222,7 +216,7 @@ static void task_wdt_callback(int channel_id, void *user_data) SEND_FATAL_ERROR_WATCHDOG_TIMEOUT(); } -static void connect_to_cloud(const struct cloud_state *state_object) +static void connect_to_cloud(const struct cloud_state_object *state_object) { int err; char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN]; @@ -286,15 +280,13 @@ static void backoff_timer_work_fn(struct k_work *work) } } -/* Zephyr State Machine Framework handlers */ +/* State handlers */ -/* Handler for STATE_RUNNING */ - -static void state_running_entry(void *o) +static void state_running_entry(void *obj) { int err; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); @@ -307,9 +299,9 @@ static void state_running_entry(void *o) } } -static void state_running_run(void *o) +static void state_running_run(void *obj) { - const struct cloud_state *state_object = (const struct cloud_state *)o; + struct cloud_state_object const *state_object = obj; if (state_object->chan == &NETWORK_CHAN) { struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf); @@ -322,15 +314,14 @@ static void state_running_run(void *o) } } -/* Handlers for STATE_DISCONNECTED. */ -static void state_disconnected_entry(void *o) +static void state_disconnected_entry(void *obj) { int err; struct cloud_msg cloud_msg = { .type = CLOUD_DISCONNECTED, }; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); @@ -343,9 +334,9 @@ static void state_disconnected_entry(void *o) } } -static void state_disconnected_run(void *o) +static void state_disconnected_run(void *obj) { - const struct cloud_state *state_object = (const struct cloud_state *)o; + struct cloud_state_object const *state_object = obj; struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf); if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) { @@ -355,23 +346,19 @@ static void state_disconnected_run(void *o) } } -/* Handlers for STATE_CONNECTING */ - -static void state_connecting_entry(void *o) +static void state_connecting_entry(void *obj) { /* Reset connection attempts counter */ - struct cloud_state *state_object = o; + struct cloud_state_object *state_object = obj; LOG_DBG("%s", __func__); state_object->connection_attempts = 0; } -/* Handler for STATE_CONNECTING_ATTEMPT */ - -static void state_connecting_attempt_entry(void *o) +static void state_connecting_attempt_entry(void *obj) { - struct cloud_state *state_object = o; + struct cloud_state_object *state_object = obj; LOG_DBG("%s", __func__); @@ -380,12 +367,10 @@ static void state_connecting_attempt_entry(void *o) connect_to_cloud(state_object); } -/* Handler for STATE_CONNECTING_BACKOFF */ - -static void state_connecting_backoff_entry(void *o) +static void state_connecting_backoff_entry(void *obj) { int err; - struct cloud_state *state_object = o; + struct cloud_state_object *state_object = obj; LOG_DBG("%s", __func__); @@ -398,9 +383,9 @@ static void state_connecting_backoff_entry(void *o) } } -static void state_connecting_backoff_run(void *o) +static void state_connecting_backoff_run(void *obj) { - const struct cloud_state *state_object = (const struct cloud_state *)o; + struct cloud_state_object const *state_object = obj; if (state_object->chan == &PRIV_CLOUD_CHAN) { const enum priv_cloud_msg msg = *(const enum priv_cloud_msg *)state_object->msg_buf; @@ -413,19 +398,18 @@ static void state_connecting_backoff_run(void *o) } } -static void state_connecting_backoff_exit(void *o) +static void state_connecting_backoff_exit(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); (void)k_work_cancel_delayable(&backoff_timer_work); } -/* Handler for STATE_CONNECTED. */ -static void state_connected_entry(void *o) +static void state_connected_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); LOG_INF("Connected to Cloud"); @@ -444,11 +428,11 @@ static void state_connected_entry(void *o) #endif /* CONFIG_MEMFAULT */ } -static void state_connected_exit(void *o) +static void state_connected_exit(void *obj) { int err; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); @@ -459,8 +443,6 @@ static void state_connected_exit(void *o) } } -/* Handlers for STATE_CONNECTED_READY */ - static void shadow_get(bool delta_only) { int err; @@ -531,14 +513,14 @@ static void shadow_get(bool delta_only) } } -static void state_connected_ready_entry(void *o) +static void state_connected_ready_entry(void *obj) { int err; struct cloud_msg cloud_msg = { .type = CLOUD_CONNECTED, }; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); @@ -553,10 +535,10 @@ static void state_connected_ready_entry(void *o) shadow_get(false); } -static void state_connected_ready_run(void *o) +static void state_connected_ready_run(void *obj) { int err; - const struct cloud_state *state_object = (const struct cloud_state *)o; + struct cloud_state_object const *state_object = obj; bool confirmable = IS_ENABLED(CONFIG_APP_CLOUD_CONFIRMABLE_MESSAGES); if (state_object->chan == &NETWORK_CHAN) { @@ -701,14 +683,14 @@ static void state_connected_ready_run(void *o) /* Handlers for STATE_CONNECTED_PAUSED */ -static void state_connected_paused_entry(void *o) +static void state_connected_paused_entry(void *obj) { int err; struct cloud_msg cloud_msg = { .type = CLOUD_DISCONNECTED, }; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); @@ -721,9 +703,9 @@ static void state_connected_paused_entry(void *o) } } -static void state_connected_paused_run(void *o) +static void state_connected_paused_run(void *obj) { - const struct cloud_state *state_object = (const struct cloud_state *)o; + struct cloud_state_object const *state_object = obj; struct network_msg msg = MSG_TO_NETWORK_MSG(state_object->msg_buf); if ((state_object->chan == &NETWORK_CHAN) && (msg.type == NETWORK_CONNECTED)) { @@ -733,9 +715,7 @@ static void state_connected_paused_run(void *o) } } -/* End of state handlers */ - -static void cloud_thread(void) +static void cloud_module_thread(void) { int err; int task_wdt_id; @@ -743,11 +723,11 @@ static void cloud_thread(void) const uint32_t execution_time_ms = (CONFIG_APP_CLOUD_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); - struct cloud_state cloud_state = { 0 }; + struct cloud_state_object cloud_state = { 0 }; - LOG_DBG("cloud module task started"); + LOG_DBG("Cloud module task started"); - task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); + task_wdt_id = task_wdt_add(wdt_timeout_ms, cloud_wdt_callback, (void *)k_current_get()); if (task_wdt_id < 0) { LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); SEND_FATAL_ERROR(); @@ -787,6 +767,6 @@ static void cloud_thread(void) } } -K_THREAD_DEFINE(cloud_thread_id, +K_THREAD_DEFINE(cloud_module_thread_id, CONFIG_APP_CLOUD_THREAD_STACK_SIZE, - cloud_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); + cloud_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); diff --git a/app/src/modules/environmental/environmental.c b/app/src/modules/environmental/environmental.c index 9caa7aa0..c4820f2d 100644 --- a/app/src/modules/environmental/environmental.c +++ b/app/src/modules/environmental/environmental.c @@ -16,7 +16,7 @@ #include "environmental.h" /* Register log module */ -LOG_MODULE_REGISTER(environmental_module, CONFIG_APP_ENVIRONMENTAL_LOG_LEVEL); +LOG_MODULE_REGISTER(environmental, CONFIG_APP_ENVIRONMENTAL_LOG_LEVEL); /* Define channels provided by this module */ ZBUS_CHAN_DEFINE(ENVIRONMENTAL_CHAN, @@ -41,18 +41,17 @@ BUILD_ASSERT(CONFIG_APP_ENVIRONMENTAL_WATCHDOG_TIMEOUT_SECONDS > /* State machine */ -/* Defininig the module states. - * - * STATE_RUNNING: The environmental module is waiting for sensor type values to be requested. +/* Environmental module states. */ enum environmental_module_state { + /* The module is running and waiting for sensor value requests */ STATE_RUNNING, }; -/* User defined state object. - * Used to transfer data between state changes. +/* State object. + * Used to transfer context data between state changes. */ -struct environmental_state { +struct environmental_state_object { /* This must be first */ struct smf_ctx ctx; @@ -72,8 +71,9 @@ struct environmental_state { }; /* Forward declarations of state handlers */ -static void state_running_run(void *o); +static void state_running_run(void *obj); +/* State machine definition */ static const struct smf_state states[] = { [STATE_RUNNING] = SMF_CREATE_STATE(NULL, state_running_run, NULL, NULL, NULL), @@ -133,7 +133,7 @@ static void sample_sensors(const struct device *const bme680) } } -static void task_wdt_callback(int channel_id, void *user_data) +static void env_wdt_callback(int channel_id, void *user_data) { LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", channel_id, k_thread_name_get((k_tid_t)user_data)); @@ -143,9 +143,9 @@ static void task_wdt_callback(int channel_id, void *user_data) /* State handlers */ -static void state_running_run(void *o) +static void state_running_run(void *obj) { - const struct environmental_state *state_object = (const struct environmental_state *)o; + struct environmental_state_object const *state_object = obj; if (&ENVIRONMENTAL_CHAN == state_object->chan) { struct environmental_msg msg = MSG_TO_ENVIRONMENTAL_MSG(state_object->msg_buf); @@ -157,9 +157,7 @@ static void state_running_run(void *o) } } -/* End of state handling */ - -static void environmental_task(void) +static void env_module_thread(void) { int err; int task_wdt_id; @@ -168,13 +166,13 @@ static void environmental_task(void) const uint32_t execution_time_ms = (CONFIG_APP_ENVIRONMENTAL_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); - struct environmental_state environmental_state = { + struct environmental_state_object environmental_state = { .bme680 = DEVICE_DT_GET(DT_NODELABEL(bme680)), }; LOG_DBG("Environmental module task started"); - task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); + task_wdt_id = task_wdt_add(wdt_timeout_ms, env_wdt_callback, (void *)k_current_get()); if (task_wdt_id < 0) { LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); SEND_FATAL_ERROR(); @@ -212,6 +210,6 @@ static void environmental_task(void) } } -K_THREAD_DEFINE(environmental_task_id, +K_THREAD_DEFINE(environmental_module_thread_id, CONFIG_APP_ENVIRONMENTAL_THREAD_STACK_SIZE, - environmental_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); + env_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); diff --git a/app/src/modules/fota/fota.c b/app/src/modules/fota/fota.c index e70acfbe..bac77b9a 100644 --- a/app/src/modules/fota/fota.c +++ b/app/src/modules/fota/fota.c @@ -46,10 +46,9 @@ ZBUS_CHAN_ADD_OBS(FOTA_CHAN, fota, 0); #define MAX_MSG_SIZE sizeof(enum fota_msg_type) -/* FOTA support context */ -static void fota_reboot(enum nrf_cloud_fota_reboot_status status); -static void fota_status(enum nrf_cloud_fota_status status, const char *const status_details); +/* State machine */ +/* FOTA module states */ enum fota_module_state { /* The module is initialized and running */ STATE_RUNNING, @@ -69,10 +68,10 @@ enum fota_module_state { STATE_CANCELING, }; -/* User defined state object. - * Used to transfer data between state changes. +/* State object. + * Used to transfer context data between state changes. */ -struct fota_state { +struct fota_state_object { /* This must be first */ struct smf_ctx ctx; @@ -86,29 +85,22 @@ struct fota_state { struct nrf_cloud_fota_poll_ctx fota_ctx; }; -/* Forward declarations */ -static void state_running_entry(void *o); -static void state_running_run(void *o); - -static void state_waiting_for_poll_request_entry(void *o); -static void state_waiting_for_poll_request_run(void *o); - -static void state_polling_for_update_entry(void *o); -static void state_polling_for_update_run(void *o); - -static void state_downloading_update_entry(void *o); -static void state_downloading_update_run(void *o); - -static void state_waiting_for_image_apply_entry(void *o); -static void state_waiting_for_image_apply_run(void *o); - -static void state_image_applying_entry(void *o); -static void state_image_applying_run(void *o); - -static void state_reboot_pending_entry(void *o); - -static void state_canceling_entry(void *o); -static void state_canceling_run(void *o); +/* Forward declarations of state handlers */ +static void state_running_entry(void *obj); +static void state_running_run(void *obj); +static void state_waiting_for_poll_request_entry(void *obj); +static void state_waiting_for_poll_request_run(void *obj); +static void state_polling_for_update_entry(void *obj); +static void state_polling_for_update_run(void *obj); +static void state_downloading_update_entry(void *obj); +static void state_downloading_update_run(void *obj); +static void state_waiting_for_image_apply_entry(void *obj); +static void state_waiting_for_image_apply_run(void *obj); +static void state_image_applying_entry(void *obj); +static void state_image_applying_run(void *obj); +static void state_reboot_pending_entry(void *obj); +static void state_canceling_entry(void *obj); +static void state_canceling_run(void *obj); static const struct smf_state states[] = { [STATE_RUNNING] = @@ -161,7 +153,7 @@ static const struct smf_state states[] = { NULL), }; -/* Private functions */ +/* FOTA support functions */ static void fota_reboot(enum nrf_cloud_fota_reboot_status status) { @@ -230,7 +222,7 @@ static void fota_status(enum nrf_cloud_fota_status status, const char *const sta } } -static void task_wdt_callback(int channel_id, void *user_data) +static void fota_wdt_callback(int channel_id, void *user_data) { LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", channel_id, k_thread_name_get((k_tid_t)user_data)); @@ -240,10 +232,10 @@ static void task_wdt_callback(int channel_id, void *user_data) /* State handlers */ -static void state_running_entry(void *o) +static void state_running_entry(void *obj) { int err; - struct fota_state *state_object = o; + struct fota_state_object *state_object = obj; LOG_DBG("%s", __func__); @@ -262,9 +254,9 @@ static void state_running_entry(void *o) } } -static void state_running_run(void *o) +static void state_running_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type msg_type = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -275,16 +267,16 @@ static void state_running_run(void *o) } } -static void state_waiting_for_poll_request_entry(void *o) +static void state_waiting_for_poll_request_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); } -static void state_waiting_for_poll_request_run(void *o) +static void state_waiting_for_poll_request_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type msg_type = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -299,9 +291,9 @@ static void state_waiting_for_poll_request_run(void *o) } } -static void state_polling_for_update_entry(void *o) +static void state_polling_for_update_entry(void *obj) { - struct fota_state *state_object = o; + struct fota_state_object *state_object = obj; LOG_DBG("%s", __func__); @@ -338,9 +330,9 @@ static void state_polling_for_update_entry(void *o) } } -static void state_polling_for_update_run(void *o) +static void state_polling_for_update_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -365,16 +357,16 @@ static void state_polling_for_update_run(void *o) } } -static void state_downloading_update_entry(void *o) +static void state_downloading_update_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); } -static void state_downloading_update_run(void *o) +static void state_downloading_update_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -402,16 +394,16 @@ static void state_downloading_update_run(void *o) } } -static void state_waiting_for_image_apply_entry(void *o) +static void state_waiting_for_image_apply_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); } -static void state_waiting_for_image_apply_run(void *o) +static void state_waiting_for_image_apply_run(void *obj) { - struct fota_state *state_object = o; + struct fota_state_object *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -422,9 +414,9 @@ static void state_waiting_for_image_apply_run(void *o) } } -static void state_image_applying_entry(void *o) +static void state_image_applying_entry(void *obj) { - struct fota_state *state_object = o; + struct fota_state_object *state_object = obj; LOG_DBG("Applying downloaded firmware image"); @@ -437,9 +429,9 @@ static void state_image_applying_entry(void *o) } } -static void state_image_applying_run(void *o) +static void state_image_applying_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type evt = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -450,18 +442,18 @@ static void state_image_applying_run(void *o) } } -static void state_reboot_pending_entry(void *o) +static void state_reboot_pending_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("Waiting for the application to reboot in order to apply the update"); } -static void state_canceling_entry(void *o) +static void state_canceling_entry(void *obj) { int err; - ARG_UNUSED(o); + ARG_UNUSED(obj); LOG_DBG("%s", __func__); LOG_DBG("Canceling download"); @@ -473,9 +465,9 @@ static void state_canceling_entry(void *o) } } -static void state_canceling_run(void *o) +static void state_canceling_run(void *obj) { - const struct fota_state *state_object = (const struct fota_state *)o; + struct fota_state_object const *state_object = obj; if (&FOTA_CHAN == state_object->chan) { const enum fota_msg_type msg = MSG_TO_FOTA_TYPE(state_object->msg_buf); @@ -487,9 +479,7 @@ static void state_canceling_run(void *o) } } -/* End of state handlers */ - -static void fota_task(void) +static void fota_module_thread(void) { int err; int task_wdt_id; @@ -497,14 +487,14 @@ static void fota_task(void) const uint32_t execution_time_ms = (CONFIG_APP_FOTA_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); - struct fota_state fota_state = { + struct fota_state_object fota_state = { .fota_ctx.reboot_fn = fota_reboot, .fota_ctx.status_fn = fota_status, }; LOG_DBG("FOTA module task started"); - task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); + task_wdt_id = task_wdt_add(wdt_timeout_ms, fota_wdt_callback, (void *)k_current_get()); if (task_wdt_id < 0) { LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); SEND_FATAL_ERROR(); @@ -539,6 +529,6 @@ static void fota_task(void) } } -K_THREAD_DEFINE(fota_task_id, +K_THREAD_DEFINE(fota_module_thread_id, CONFIG_APP_FOTA_THREAD_STACK_SIZE, - fota_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); + fota_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); diff --git a/app/src/modules/location/location.c b/app/src/modules/location/location.c index 71903ada..27e82a28 100644 --- a/app/src/modules/location/location.c +++ b/app/src/modules/location/location.c @@ -46,28 +46,19 @@ ZBUS_CHAN_ADD_OBS(LOCATION_CHAN, location, 0); /* Forward declarations */ static void location_event_handler(const struct location_event_data *event_data); -static void state_running_entry(void *o); -static void state_running_run(void *o); +/* State machine */ -/* Single state for the location module */ +/* Location module states. + */ enum location_module_state { + /* The module is running */ STATE_RUNNING, }; -/* Construct state table */ -static const struct smf_state states[] = { - [STATE_RUNNING] = - SMF_CREATE_STATE(state_running_entry, - state_running_run, - NULL, - NULL, - NULL), -}; - -/* Location module state object. - * Used to transfer data between state changes. +/* State object. + * Used to transfer context data between state changes. */ -struct location_state { +struct location_state_object { /* This must be first */ struct smf_ctx ctx; @@ -78,6 +69,21 @@ struct location_state { uint8_t msg_buf[MAX_MSG_SIZE]; }; +/* Forward declarations of state handlers */ +static void state_running_entry(void *obj); +static void state_running_run(void *obj); + +/* State machine definition */ +/* States are defined in the state object */ +static const struct smf_state states[] = { + [STATE_RUNNING] = + SMF_CREATE_STATE(state_running_entry, + state_running_run, + NULL, + NULL, + NULL), +}; + static void on_cfun(int mode, void *ctx) { ARG_UNUSED(ctx); @@ -112,7 +118,7 @@ static void on_cfun(int mode, void *ctx) NRF_MODEM_LIB_ON_CFUN(att_location_init_hook, on_cfun, NULL); -static void task_wdt_callback(int channel_id, void *user_data) +static void location_wdt_callback(int channel_id, void *user_data) { LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", channel_id, k_thread_name_get((k_tid_t)user_data)); @@ -154,10 +160,10 @@ void handle_location_chan(enum location_msg_type location_msg_type) } } -/* State machine handlers */ -static void state_running_entry(void *o) +/* State handlers */ +static void state_running_entry(void *obj) { - ARG_UNUSED(o); + ARG_UNUSED(obj); int err; @@ -173,12 +179,12 @@ static void state_running_entry(void *o) LOG_DBG("Location library initialized"); } -static void state_running_run(void *o) +static void state_running_run(void *obj) { - const struct location_state *state = (const struct location_state *)o; + struct location_state_object const *state_object = obj; - if (state->chan == &LOCATION_CHAN) { - handle_location_chan(MSG_TO_LOCATION_TYPE(state->msg_buf)); + if (state_object->chan == &LOCATION_CHAN) { + handle_location_chan(MSG_TO_LOCATION_TYPE(state_object->msg_buf)); } } @@ -291,11 +297,11 @@ static void location_module_thread(void) const uint32_t execution_time_ms = (CONFIG_APP_LOCATION_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); - struct location_state location_state = { 0 }; + struct location_state_object location_state = { 0 }; LOG_DBG("Location module task started"); - task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); + task_wdt_id = task_wdt_add(wdt_timeout_ms, location_wdt_callback, (void *)k_current_get()); if (task_wdt_id < 0) { LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); SEND_FATAL_ERROR(); diff --git a/app/src/modules/network/network.c b/app/src/modules/network/network.c index bf8ae740..b92cc374 100644 --- a/app/src/modules/network/network.c +++ b/app/src/modules/network/network.c @@ -57,23 +57,19 @@ static struct net_mgmt_event_callback conn_cb; enum network_module_state { /* The module is running */ STATE_RUNNING, - - /* The device is not connected to a network */ - STATE_DISCONNECTED, - - /* The device is disconencted from network and is not searching */ - STATE_DISCONNECTED_IDLE, - - /* The device is disconnected and the modem is searching for available networks */ - STATE_DISCONNECTED_SEARCHING, - - /* The device is connected to a network */ - STATE_CONNECTED, - - /* The device has initiated detachment from network, but the modem has not confirmed - * detachment yet. - */ - STATE_DISCONNECTING, + /* The device is not connected to a network */ + STATE_DISCONNECTED, + /* The device is disconnected from network and is not searching */ + STATE_DISCONNECTED_IDLE, + /* The device is disconnected and the modem is searching for networks */ + STATE_DISCONNECTED_SEARCHING, + /* The device is connected to a network */ + STATE_CONNECTED, + + /* The device has initiated detachment from network, but the modem has not confirmed + * detachment yet. + */ + STATE_DISCONNECTING, }; /* State object. diff --git a/app/src/modules/power/power.c b/app/src/modules/power/power.c index 41dd8714..b28173d8 100644 --- a/app/src/modules/power/power.c +++ b/app/src/modules/power/power.c @@ -70,18 +70,16 @@ static void sample(int64_t *ref_time); /* State machine */ -/* Defininig the module states. - * - * STATE_RUNNING: The power module is initializing and waiting battery percentage to be requested. +/* Power module states. */ enum power_module_state { STATE_RUNNING, }; -/* User defined state object. - * Used to transfer data between state changes. +/* State object. + * Used to transfer context data between state changes. */ -struct power_state { +struct power_state_object { /* This must be first */ struct smf_ctx ctx; @@ -96,17 +94,19 @@ struct power_state { }; /* Forward declarations of state handlers */ -static void state_running_entry(void *o); -static void state_running_run(void *o); +static void state_running_entry(void *obj); +static void state_running_run(void *obj); +/* State machine definition */ static const struct smf_state states[] = { [STATE_RUNNING] = SMF_CREATE_STATE(state_running_entry, state_running_run, NULL, NULL, NULL), }; + /* State handlers */ -static void state_running_entry(void *o) +static void state_running_entry(void *obj) { int err; struct sensor_value value; @@ -114,7 +114,7 @@ static void state_running_entry(void *o) .model = &battery_model }; int32_t chg_status; - struct power_state *state_object = o; + struct power_state_object *state_object = obj; static struct gpio_callback event_cb; if (!device_is_ready(charger)) { @@ -156,9 +156,9 @@ static void state_running_entry(void *o) } } -static void state_running_run(void *o) +static void state_running_run(void *obj) { - struct power_state *state_object = o; + struct power_state_object *state_object = obj; if (&POWER_CHAN == state_object->chan) { struct power_msg msg = MSG_TO_POWER_MSG(state_object->msg_buf); @@ -170,8 +170,6 @@ static void state_running_run(void *o) } } -/* End of state handling */ - static int uart_disable(void) { int err; @@ -366,7 +364,7 @@ static void sample(int64_t *ref_time) } } -static void task_wdt_callback(int channel_id, void *user_data) +static void power_wdt_callback(int channel_id, void *user_data) { LOG_ERR("Watchdog expired, Channel: %d, Thread: %s", channel_id, k_thread_name_get((k_tid_t)user_data)); @@ -374,7 +372,7 @@ static void task_wdt_callback(int channel_id, void *user_data) SEND_FATAL_ERROR_WATCHDOG_TIMEOUT(); } -static void power_task(void) +static void power_module_thread(void) { int err; int task_wdt_id; @@ -383,11 +381,11 @@ static void power_task(void) const uint32_t execution_time_ms = (CONFIG_APP_POWER_MSG_PROCESSING_TIMEOUT_SECONDS * MSEC_PER_SEC); const k_timeout_t zbus_wait_ms = K_MSEC(wdt_timeout_ms - execution_time_ms); - struct power_state power_state; + struct power_state_object power_state; LOG_DBG("Power module task started"); - task_wdt_id = task_wdt_add(wdt_timeout_ms, task_wdt_callback, (void *)k_current_get()); + task_wdt_id = task_wdt_add(wdt_timeout_ms, power_wdt_callback, (void *)k_current_get()); if (task_wdt_id < 0) { LOG_ERR("Failed to add task to watchdog: %d", task_wdt_id); SEND_FATAL_ERROR(); @@ -425,6 +423,6 @@ static void power_task(void) } } -K_THREAD_DEFINE(power_task_id, +K_THREAD_DEFINE(power_module_thread_id, CONFIG_APP_POWER_THREAD_STACK_SIZE, - power_task, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0); + power_module_thread, NULL, NULL, NULL, K_LOWEST_APPLICATION_THREAD_PRIO, 0, 0);