SMF supports defining a hierarchy of states. For example, the FOTA module's states can be graphically described as follows:
In the diagram, the black dots with arrows indicate initial transitions. In this case, the initial state of the machine is set to the top-level STATE_RUNNING state. In the state definitions, an initial transition is configured such that the state machine ends up in STATE_WAITING_FOR_POLL_REQUEST when first initialized. See FOTA module for the complete set of transitions.
In SMF, a single state is defined using the SMF_CREATE_STATE macro. You can specify the following parameters:
- Entry function: Called when entering the state.
- Run function: Called when processing a message while in the state.
- Exit function: Called when leaving the state.
- Parent state: Another state to which this state is subordinate.
- Initial state transition: Another state to transition to immediately after entry.
The following shows the definitions of two of the states in the FOTA module from src/modules/fota/fota.c, with the parent state and initial transition as shown in the diagram:
static const struct smf_state states[] = {
[STATE_RUNNING] =
SMF_CREATE_STATE(state_running_entry,
state_running_run,
NULL, /* No exit function */
NULL, /* No parent state */
&states[STATE_WAITING_FOR_POLL_REQUEST]),
[STATE_WAITING_FOR_POLL_REQUEST] =
SMF_CREATE_STATE(NULL, /* No entry function */
state_waiting_for_poll_request_run,
NULL, /* No exit function */
&states[STATE_RUNNING],
NULL), /* No initial transition */
/* ... */
};