Solaris Software
Toggle main menu visibility
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
* ---------------------------------------------------------------- */
10
#include "
spp/services/fsm/fsm.h
"
11
#include "
spp/core/pubsub/pubsub.h
"
12
13
/* ----------------------------------------------------------------
14
* STRUCTS
15
* ---------------------------------------------------------------- */
16
typedef
struct
17
{
18
const
FSM_Transition_t
*
p_table
;
19
spp_uint8_t
size
;
20
}
FSM_Table_t
;
21
22
/* ----------------------------------------------------------------
23
* VARIABLES
24
* ---------------------------------------------------------------- */
25
static
spp_uint16_t
s_fsmErrors
= {0};
26
27
static
FSM_Table_t
s_tables
[
K_FSM_MAX_TABLES
];
28
static
spp_uint8_t
s_tableCount
= 0U;
29
30
static
FSM_Handle_t
s_fsmHandle
= {
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
*/
44
static
void
FSM_pubsubCycle
(
void
)
45
{
46
SPP_SERVICES_PUBSUB_callProducers
();
47
SPP_SERVICES_PUBSUB_callConsumers
();
48
}
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
* ---------------------------------------------------------------- */
57
static
const
FSM_Transition_t
s_fsmBaseTable
[] = {
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
,
80
.toSubState =
FSM_SUBSTATE_FLIGHT_ASCENDING
,
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
,
89
.toSubState =
FSM_SUBSTATE_FLIGHT_DESCENDING
,
90
.guard = NULL,
91
.action = NULL,
92
.stateFunction =
FSM_pubsubCycle
,
93
},
94
{
95
.fromState =
FSM_STATE_FLIGHT
,
96
.fromSubState =
FSM_SUBSTATE_FLIGHT_PARACHUTE_DEPLOYED
,
97
.toState =
FSM_STATE_FLIGHT
,
98
.toSubState =
FSM_SUBSTATE_FLIGHT_PARACHUTE_DEPLOYED
,
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
* ---------------------------------------------------------------- */
117
spp_uint16_t
*
SPP_CORE_FSM_getErrorsBit
(
void
)
118
{
119
return
&
s_fsmErrors
;
120
}
121
122
SPP_RetVal_t
FSM_init
(
void
)
123
{
124
s_tableCount
= 0U;
125
126
s_fsmHandle
.
state
=
FSM_STATE_INIT
;
127
s_fsmHandle
.
subState
=
FSM_SUBSTATE_NONE
;
128
s_fsmHandle
.
prevState
=
FSM_STATE_INIT
;
129
s_fsmHandle
.
prevSubState
=
FSM_SUBSTATE_NONE
;
130
131
return
FSM_addTable
(
s_fsmBaseTable
,
132
(
spp_uint8_t
)(
sizeof
(
s_fsmBaseTable
) /
sizeof
(
s_fsmBaseTable
[0])));
133
}
134
135
SPP_RetVal_t
FSM_addTable
(
const
FSM_Transition_t
*p_transitionTable,
const
spp_uint8_t
tableSize)
136
{
137
if
(p_transitionTable == NULL)
138
{
139
return
K_SPP_ERROR_NULL_POINTER
;
140
}
141
142
if
(tableSize == 0U || tableSize >
K_FSM_MAX_TABLE_SIZE
)
143
{
144
return
K_SPP_ERROR_INVALID_PARAMETER
;
145
}
146
147
if
(
s_tableCount
>=
K_FSM_MAX_TABLES
)
148
{
149
return
K_SPP_ERROR_REGISTRY_FULL
;
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
159
void
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
167
if
(p_t->
fromState
!=
s_fsmHandle
.
state
|| p_t->
fromSubState
!=
s_fsmHandle
.
subState
)
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
186
s_fsmHandle
.
prevState
=
s_fsmHandle
.
state
;
187
s_fsmHandle
.
prevSubState
=
s_fsmHandle
.
subState
;
188
s_fsmHandle
.
state
= p_t->
toState
;
189
s_fsmHandle
.
subState
= p_t->
toSubState
;
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
}
s_fsmErrors
static FsmErrors_t s_fsmErrors
Definition
custom.c:48
FSM_init
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_CORE_FSM_getErrorsBit
spp_uint16_t * SPP_CORE_FSM_getErrorsBit(void)
Get the pointer to the FSM errors bit.
Definition
fsm.c:117
s_tableCount
static spp_uint8_t s_tableCount
Definition
fsm.c:28
FSM_addTable
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
s_fsmHandle
static FSM_Handle_t s_fsmHandle
Definition
fsm.c:30
s_tables
static FSM_Table_t s_tables[K_FSM_MAX_TABLES]
Definition
fsm.c:27
FSM_tick
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
FSM_pubsubCycle
static void FSM_pubsubCycle(void)
Run one complete PUBSUB cycle: acquire producer data and dispatch it to the consumer mailboxes,...
Definition
fsm.c:44
s_fsmBaseTable
static const FSM_Transition_t s_fsmBaseTable[]
Definition
fsm.c:57
fsm.h
Finite State Machine (FSM) module for the SPP core.
FSM_STATE_LANDED
@ FSM_STATE_LANDED
Definition
fsm.h:32
FSM_STATE_FLIGHT
@ FSM_STATE_FLIGHT
Definition
fsm.h:31
FSM_STATE_READY
@ FSM_STATE_READY
Definition
fsm.h:30
FSM_STATE_INIT
@ FSM_STATE_INIT
Definition
fsm.h:29
FSM_SUBSTATE_FLIGHT_ASCENDING
@ FSM_SUBSTATE_FLIGHT_ASCENDING
Definition
fsm.h:41
FSM_SUBSTATE_FLIGHT_PARACHUTE_DEPLOYED
@ FSM_SUBSTATE_FLIGHT_PARACHUTE_DEPLOYED
Definition
fsm.h:43
FSM_SUBSTATE_NONE
@ FSM_SUBSTATE_NONE
Definition
fsm.h:40
FSM_SUBSTATE_FLIGHT_DESCENDING
@ FSM_SUBSTATE_FLIGHT_DESCENDING
Definition
fsm.h:42
K_FSM_MAX_TABLE_SIZE
#define K_FSM_MAX_TABLE_SIZE
Definition
fsm.h:16
K_FSM_MAX_TABLES
#define K_FSM_MAX_TABLES
Definition
fsm.h:17
SPP_SERVICES_PUBSUB_callConsumers
void SPP_SERVICES_PUBSUB_callConsumers(void)
Dispatches one buffered packet and executes initialized consumers.
Definition
pubsub.c:152
SPP_SERVICES_PUBSUB_callProducers
void SPP_SERVICES_PUBSUB_callProducers(void)
Calls acquireData on every successfully initialized producer.
Definition
pubsub.c:115
pubsub.h
Publish-subscribe packet router.
SPP_RetVal_t
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition
returnTypes.h:24
K_SPP_OK
@ K_SPP_OK
Definition
returnTypes.h:25
K_SPP_ERROR_REGISTRY_FULL
@ K_SPP_ERROR_REGISTRY_FULL
Definition
returnTypes.h:36
K_SPP_ERROR_NULL_POINTER
@ K_SPP_ERROR_NULL_POINTER
Definition
returnTypes.h:30
K_SPP_ERROR_INVALID_PARAMETER
@ K_SPP_ERROR_INVALID_PARAMETER
Definition
returnTypes.h:32
FSM_Handle_t
FSM handle — holds the current and previous state of the machine.
Definition
fsm.h:64
FSM_Handle_t::prevSubState
FSM_SubState_t prevSubState
Definition
fsm.h:68
FSM_Handle_t::prevState
FSM_State_t prevState
Definition
fsm.h:67
FSM_Handle_t::state
FSM_State_t state
Definition
fsm.h:65
FSM_Handle_t::subState
FSM_SubState_t subState
Definition
fsm.h:66
FSM_Table_t
Definition
fsm.c:17
FSM_Table_t::p_table
const FSM_Transition_t * p_table
Definition
fsm.c:18
FSM_Table_t::size
spp_uint8_t size
Definition
fsm.c:19
FSM_Transition_t
A single row in the FSM transition table.
Definition
fsm.h:50
FSM_Transition_t::fromSubState
FSM_SubState_t fromSubState
Definition
fsm.h:52
FSM_Transition_t::guard
spp_bool_t(*) guard(void)
Definition
fsm.h:55
FSM_Transition_t::stateFunction
void(*) stateFunction(void)
Definition
fsm.h:57
FSM_Transition_t::fromState
FSM_State_t fromState
Definition
fsm.h:51
FSM_Transition_t::toState
FSM_State_t toState
Definition
fsm.h:53
FSM_Transition_t::action
void(*) action(void)
Definition
fsm.h:56
FSM_Transition_t::toSubState
FSM_SubState_t toSubState
Definition
fsm.h:54
spp_uint16_t
uint16_t spp_uint16_t
Definition
types.h:29
spp_uint8_t
uint8_t spp_uint8_t
Definition
types.h:27
solaris-v2
spp
services
fsm
fsm.c
Generated by
1.18.0