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 * ---------------------------------------------------------------- */
12
13/* ----------------------------------------------------------------
14 * STRUCTS
15 * ---------------------------------------------------------------- */
21
22/* ----------------------------------------------------------------
23 * VARIABLES
24 * ---------------------------------------------------------------- */
26
29
31 .state = FSM_STATE_INIT,
32 .subState = FSM_SUBSTATE_NONE,
33 .prevState = FSM_STATE_INIT,
34 .prevSubState = FSM_SUBSTATE_NONE,
35};
36
37/* ----------------------------------------------------------------
38 * STATIC FUNCTIONS
39 * ---------------------------------------------------------------- */
40/**
41 * @brief Run one complete PUBSUB cycle: acquire producer data and dispatch
42 * it to the consumer mailboxes, then call consumeData().
43 */
49
50/* ----------------------------------------------------------------
51 * SERVICE TRANSITION TABLE
52 *
53 * Rows with guard == NULL run their stateFunction every tick while the FSM
54 * is in the matching state/sub-state. The service table keeps the PUBSUB
55 * cycle alive in every active state.
56 * ---------------------------------------------------------------- */
58 {
59 .fromState = FSM_STATE_READY,
60 .fromSubState = FSM_SUBSTATE_NONE,
61 .toState = FSM_STATE_READY,
62 .toSubState = FSM_SUBSTATE_NONE,
63 .guard = NULL,
64 .action = NULL,
65 .stateFunction = FSM_pubsubCycle,
66 },
67 {
68 .fromState = FSM_STATE_FLIGHT,
69 .fromSubState = FSM_SUBSTATE_NONE,
70 .toState = FSM_STATE_FLIGHT,
71 .toSubState = FSM_SUBSTATE_NONE,
72 .guard = NULL,
73 .action = NULL,
74 .stateFunction = FSM_pubsubCycle,
75 },
76 {
77 .fromState = FSM_STATE_FLIGHT,
78 .fromSubState = FSM_SUBSTATE_FLIGHT_ASCENDING,
79 .toState = FSM_STATE_FLIGHT,
81 .guard = NULL,
82 .action = NULL,
83 .stateFunction = FSM_pubsubCycle,
84 },
85 {
86 .fromState = FSM_STATE_FLIGHT,
87 .fromSubState = FSM_SUBSTATE_FLIGHT_DESCENDING,
88 .toState = FSM_STATE_FLIGHT,
90 .guard = NULL,
91 .action = NULL,
92 .stateFunction = FSM_pubsubCycle,
93 },
94 {
95 .fromState = FSM_STATE_FLIGHT,
97 .toState = FSM_STATE_FLIGHT,
99 .guard = NULL,
100 .action = NULL,
101 .stateFunction = FSM_pubsubCycle,
102 },
103 {
104 .fromState = FSM_STATE_LANDED,
105 .fromSubState = FSM_SUBSTATE_NONE,
106 .toState = FSM_STATE_LANDED,
107 .toSubState = FSM_SUBSTATE_NONE,
108 .guard = NULL,
109 .action = NULL,
110 .stateFunction = FSM_pubsubCycle,
111 },
112};
113
114/* ----------------------------------------------------------------
115 * PUBLIC FUNCTIONS
116 * ---------------------------------------------------------------- */
121
134
135SPP_RetVal_t FSM_addTable(const FSM_Transition_t *p_transitionTable, const spp_uint8_t tableSize)
136{
137 if (p_transitionTable == NULL)
138 {
140 }
141
142 if (tableSize == 0U || tableSize > K_FSM_MAX_TABLE_SIZE)
143 {
145 }
146
148 {
150 }
151
152 s_tables[s_tableCount].p_table = p_transitionTable;
153 s_tables[s_tableCount].size = tableSize;
154 s_tableCount++;
155
156 return K_SPP_OK;
157}
158
159void FSM_tick(void)
160{
161 for (spp_uint8_t t = 0U; t < s_tableCount; t++)
162 {
163 for (spp_uint8_t i = 0U; i < s_tables[t].size; i++)
164 {
165 const FSM_Transition_t *p_t = &s_tables[t].p_table[i];
166
168 {
169 continue;
170 }
171
172 if (p_t->guard == NULL)
173 {
174 if (p_t->stateFunction != NULL)
175 {
176 p_t->stateFunction();
177 }
178 continue;
179 }
180
181 if (!p_t->guard())
182 {
183 continue;
184 }
185
190
191 if (p_t->action != NULL)
192 {
193 p_t->action();
194 }
195 if (p_t->stateFunction != NULL)
196 {
197 p_t->stateFunction();
198 }
199
200 return;
201 }
202 }
203}
static FsmErrors_t s_fsmErrors
Definition custom.c:48
SPP_RetVal_t FSM_init(void)
Initialise the FSM with the internal service table. The service table runs the PUBSUB produce/consume...
Definition fsm.c:122
spp_uint16_t * SPP_CORE_FSM_getErrorsBit(void)
Get the pointer to the FSM errors bit.
Definition fsm.c:117
static spp_uint8_t s_tableCount
Definition fsm.c:28
SPP_RetVal_t FSM_addTable(const FSM_Transition_t *p_transitionTable, const spp_uint8_t tableSize)
Append a transition table to the FSM.
Definition fsm.c:135
static FSM_Handle_t s_fsmHandle
Definition fsm.c:30
static FSM_Table_t s_tables[K_FSM_MAX_TABLES]
Definition fsm.c:27
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:159
static void FSM_pubsubCycle(void)
Run one complete PUBSUB cycle: acquire producer data and dispatch it to the consumer mailboxes,...
Definition fsm.c:44
static const FSM_Transition_t s_fsmBaseTable[]
Definition fsm.c:57
Finite State Machine (FSM) module for the SPP core.
@ FSM_STATE_LANDED
Definition fsm.h:32
@ FSM_STATE_FLIGHT
Definition fsm.h:31
@ FSM_STATE_READY
Definition fsm.h:30
@ FSM_STATE_INIT
Definition fsm.h:29
@ FSM_SUBSTATE_FLIGHT_ASCENDING
Definition fsm.h:41
@ FSM_SUBSTATE_FLIGHT_PARACHUTE_DEPLOYED
Definition fsm.h:43
@ FSM_SUBSTATE_NONE
Definition fsm.h:40
@ FSM_SUBSTATE_FLIGHT_DESCENDING
Definition fsm.h:42
#define K_FSM_MAX_TABLE_SIZE
Definition fsm.h:16
#define K_FSM_MAX_TABLES
Definition fsm.h:17
void SPP_SERVICES_PUBSUB_callConsumers(void)
Dispatches one buffered packet and executes initialized consumers.
Definition pubsub.c:152
void SPP_SERVICES_PUBSUB_callProducers(void)
Calls acquireData on every successfully initialized producer.
Definition pubsub.c:115
Publish-subscribe packet router.
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_REGISTRY_FULL
Definition returnTypes.h:36
@ 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:64
FSM_SubState_t prevSubState
Definition fsm.h:68
FSM_State_t prevState
Definition fsm.h:67
FSM_State_t state
Definition fsm.h:65
FSM_SubState_t subState
Definition fsm.h:66
const FSM_Transition_t * p_table
Definition fsm.c:18
spp_uint8_t size
Definition fsm.c:19
A single row in the FSM transition table.
Definition fsm.h:50
FSM_SubState_t fromSubState
Definition fsm.h:52
spp_bool_t(*) guard(void)
Definition fsm.h:55
void(*) stateFunction(void)
Definition fsm.h:57
FSM_State_t fromState
Definition fsm.h:51
FSM_State_t toState
Definition fsm.h:53
void(*) action(void)
Definition fsm.h:56
FSM_SubState_t toSubState
Definition fsm.h:54
uint16_t spp_uint16_t
Definition types.h:29
uint8_t spp_uint8_t
Definition types.h:27