Solaris Software
Loading...
Searching...
No Matches
fsm.c
Go to the documentation of this file.
1/**
2 * @file fsm.c
3 * @brief Finite State Machine (FSM) implementation for the SPP core.
4 */
5
6
7/* ----------------------------------------------------------------
8 * INCLUDES
9 * ---------------------------------------------------------------- */
11
12/* ----------------------------------------------------------------
13* VARIABLES
14* ---------------------------------------------------------------- */
15
17
19
21 .state = FSM_STATE_INIT,
22 .subState = FSM_SUBSTATE_NONE,
23 .prevState = FSM_STATE_INIT,
24 .prevSubState = FSM_SUBSTATE_NONE,
25};
26
28
29
30/* ----------------------------------------------------------------
31 * PUBLIC FUNCTIONS
32 * ---------------------------------------------------------------- */
37
38SPP_RetVal_t FSM_init(const FSM_Transition_t *p_transitionTable, const spp_uint8_t tableSize)
39{
40 if (p_transitionTable == NULL)
41 {
43 }
44
45 // Check the size of the table
46 if (tableSize > K_FSM_MAX_TABLE_SIZE || tableSize == 0)
47 {
49 }
50
51 // Assign the table parameters to the static variables
52 p_s_transitionTable = p_transitionTable;
53 s_tableSize = tableSize;
54
55 return K_SPP_OK;
56}
57
58void FSM_tick(void)
59{
60 for (spp_uint8_t i = 0; i < s_tableSize; i++)
61 {
62 // TODO: review if this is correct
64
66 {
67 continue;
68 }
69
70 if (p_t->guard == NULL)
71 {
72 p_t->stateFunction();
73 continue;
74 }
75 else if (!p_t->guard())
76 {
77 continue;
78 }
79
84
85 if (p_t->action != NULL)
86 {
87 p_t->action();
88 }
89 if (p_t->stateFunction != NULL)
90 {
91 p_t->stateFunction();
92 }
93
94 break;
95 }
96}
static FsmErrors_t s_fsmErrors
Definition custom.c:57
SPP_RetVal_t FSM_init(const FSM_Transition_t *p_transitionTable, const spp_uint8_t tableSize)
Initialize the FSM.
Definition fsm.c:38
static spp_uint8_t s_tableSize
Definition fsm.c:27
spp_uint16_t * SPP_CORE_FSM_getErrorsBit(void)
Get the pointer to the FSM errors bit.
Definition fsm.c:33
static const FSM_Transition_t * p_s_transitionTable
Definition fsm.c:18
static FSM_Handle_t s_fsmHandle
Definition fsm.c:20
void FSM_tick(void)
Evaluate the transition table and advance the FSM if a guard passes. Call this once per superloop tic...
Definition fsm.c:58
Finite State Machine (FSM) module for the SPP core.
@ FSM_STATE_INIT
Definition fsm.h:28
@ FSM_SUBSTATE_NONE
Definition fsm.h:39
#define K_FSM_MAX_TABLE_SIZE
Definition fsm.h:16
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
@ K_SPP_ERROR_INVALID_PARAMETER
Definition returnTypes.h:32
FSM handle — holds the current and previous state of the machine.
Definition fsm.h:63
FSM_SubState_t prevSubState
Definition fsm.h:67
FSM_State_t prevState
Definition fsm.h:66
FSM_State_t state
Definition fsm.h:64
FSM_SubState_t subState
Definition fsm.h:65
A single row in the FSM transition table.
Definition fsm.h:49
FSM_SubState_t fromSubState
Definition fsm.h:51
spp_bool_t(*) guard(void)
Definition fsm.h:54
void(*) stateFunction(void)
Definition fsm.h:56
FSM_State_t fromState
Definition fsm.h:50
FSM_State_t toState
Definition fsm.h:52
void(*) action(void)
Definition fsm.h:55
FSM_SubState_t toSubState
Definition fsm.h:53
uint16_t spp_uint16_t
Definition types.h:29
uint8_t spp_uint8_t
Definition types.h:27