Skip to content

Commit

Permalink
app: transport: Rewrite state handling
Browse files Browse the repository at this point in the history
Rewrite of the state machine, which affects pretty much all code
in the module.
A few new states have been added, and all state transitions
will now happen from within the state handlers to avoid
races and ensure run-to-completion for all events.

Signed-off-by: Jan Tore Guggedal <[email protected]>
  • Loading branch information
jtguggedal committed Jun 13, 2024
1 parent ef836d2 commit d92a645
Show file tree
Hide file tree
Showing 11 changed files with 733 additions and 153 deletions.
2 changes: 2 additions & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ CONFIG_FPU=y
CONFIG_ZBUS=y
CONFIG_ZBUS_MSG_SUBSCRIBER=y
CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_POOL_SIZE=32

# Zephyr State Machine Framework
CONFIG_SMF=y
CONFIG_SMF_ANCESTOR_SUPPORT=y
CONFIG_SMF_INITIAL_TRANSITION=y
Expand Down
5 changes: 3 additions & 2 deletions app/src/common/message_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ enum network_status {
};

enum cloud_status {
CLOUD_CONNECTED = 0x1,
CLOUD_DISCONNECTED,
CLOUD_DISCONNECTED = 0x1,
CLOUD_CONNECTED_READY_TO_SEND,
CLOUD_CONNECTED_PAUSED,
};

enum trigger_type {
Expand Down
59 changes: 59 additions & 0 deletions app/src/common/modules_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef _MODULES_COMMON_H_
#define _MODULES_COMMON_H_

#ifdef __cplusplus
extern "C" {
#endif

/** @brief Set the initial state for a module.
*
* @note The macro requires that a state machine object named s_obj is defined and that a
* state array named states is defined.
*
* @param _state State to set as initial.
*
* @return see smf_set_initial().
*/
#define STATE_SET_INITIAL(_state) smf_set_initial(SMF_CTX(&s_obj), &states[_state])

/** @brief Set the state for a module.
*
* @note The macro requires that a state machine object named s_obj is defined and that a
* state array named states is defined.
*
* @param _state State to set.
*
* @return see smf_set_state().
*/
#define STATE_SET(_state) smf_set_state(SMF_CTX(&s_obj), &states[_state])

/** @brief Set the state for a module and handle the event.
*
* @note The macro requires that a state machine object named s_obj is defined and that a
* state array named states is defined.
*
* @param _state State to set.
*
* @return see smf_set_handled().
*/
#define STATE_EVENT_HANDLED(_state) smf_set_handled(SMF_CTX(&s_obj))

/** @brief Run the state machine for a module.
*
* @note The macro requires that a state machine object named s_obj is defined.
*
* @return see smf_run_state().
*/
#define STATE_RUN() smf_run_state(SMF_CTX(&s_obj))

#ifdef __cplusplus
}
#endif

#endif /* _MODULES_COMMON_H_ */
2 changes: 1 addition & 1 deletion app/src/modules/app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static void app_task(void)
return;
}

if (cloud_status == CLOUD_CONNECTED) {
if (cloud_status == CLOUD_CONNECTED_READY_TO_SEND) {
LOG_DBG("Cloud connected");
LOG_DBG("Getting latest device configuration from device shadow");

Expand Down
2 changes: 1 addition & 1 deletion app/src/modules/fota/fota.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void fota_callback(const struct zbus_channel *chan)
{
const enum cloud_status *status = zbus_chan_const_msg(chan);

if (*status == CLOUD_CONNECTED)
if (*status == CLOUD_CONNECTED_READY_TO_SEND)
{
LOG_DBG("Cloud connected, initializing FOTA");
err = nrf_cloud_fota_poll_init(&ctx);
Expand Down
Loading

0 comments on commit d92a645

Please sign in to comment.