SMF uses a data structure of type struct smf_ctx to track the current state of the state machine. In the Asset Tracker Template, this structure is embedded within a larger structure called the state object that holds the following:
- SMF's context structure.
- The zbus channel of the latest message received.
- The contents of the latest message received.
- Any extended state variables needed by the module.
For example, the Network module defines its state object structure:
struct network_state_object {
/* This must be first */
struct smf_ctx ctx;
/* Last channel type that a message was received on */
const struct zbus_channel *chan;
/* Buffer for last ZBus message */
uint8_t msg_buf[MAX_MSG_SIZE];
/* Any extended state variables */
};
The structure is allocated in the module's thread as network_state and passed to all SMF functions.