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
12
/* ----------------------------------------------------------------
13
* VARIABLES
14
* ---------------------------------------------------------------- */
15
16
static
spp_uint16_t
s_fsmErrors
= {0};
17
18
static
const
FSM_Transition_t
*
p_s_transitionTable
= NULL;
19
20
static
FSM_Handle_t
s_fsmHandle
= {
21
.state =
FSM_STATE_INIT
,
22
.subState =
FSM_SUBSTATE_NONE
,
23
.prevState =
FSM_STATE_INIT
,
24
.prevSubState =
FSM_SUBSTATE_NONE
,
25
};
26
27
static
spp_uint8_t
s_tableSize
= 0;
28
29
30
/* ----------------------------------------------------------------
31
* PUBLIC FUNCTIONS
32
* ---------------------------------------------------------------- */
33
spp_uint16_t
*
SPP_CORE_FSM_getErrorsBit
(
void
)
34
{
35
return
&
s_fsmErrors
;
36
}
37
38
SPP_RetVal_t
FSM_init
(
const
FSM_Transition_t
*p_transitionTable,
const
spp_uint8_t
tableSize)
39
{
40
if
(p_transitionTable == NULL)
41
{
42
return
K_SPP_ERROR_NULL_POINTER
;
43
}
44
45
// Check the size of the table
46
if
(tableSize >
K_FSM_MAX_TABLE_SIZE
|| tableSize == 0)
47
{
48
return
K_SPP_ERROR_INVALID_PARAMETER
;
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
58
void
FSM_tick
(
void
)
59
{
60
for
(
spp_uint8_t
i = 0; i <
s_tableSize
; i++)
61
{
62
// TODO: review if this is correct
63
const
FSM_Transition_t
*p_t = &
p_s_transitionTable
[i];
64
65
if
(p_t->
fromState
!=
s_fsmHandle
.
state
|| p_t->
fromSubState
!=
s_fsmHandle
.
subState
)
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
80
s_fsmHandle
.
prevState
=
s_fsmHandle
.
state
;
81
s_fsmHandle
.
prevSubState
=
s_fsmHandle
.
subState
;
82
s_fsmHandle
.
state
= p_t->
toState
;
83
s_fsmHandle
.
subState
= p_t->
toSubState
;
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
}
s_fsmErrors
static FsmErrors_t s_fsmErrors
Definition
custom.c:57
FSM_init
SPP_RetVal_t FSM_init(const FSM_Transition_t *p_transitionTable, const spp_uint8_t tableSize)
Initialize the FSM.
Definition
fsm.c:38
s_tableSize
static spp_uint8_t s_tableSize
Definition
fsm.c:27
SPP_CORE_FSM_getErrorsBit
spp_uint16_t * SPP_CORE_FSM_getErrorsBit(void)
Get the pointer to the FSM errors bit.
Definition
fsm.c:33
p_s_transitionTable
static const FSM_Transition_t * p_s_transitionTable
Definition
fsm.c:18
s_fsmHandle
static FSM_Handle_t s_fsmHandle
Definition
fsm.c:20
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:58
fsm.h
Finite State Machine (FSM) module for the SPP core.
FSM_STATE_INIT
@ FSM_STATE_INIT
Definition
fsm.h:28
FSM_SUBSTATE_NONE
@ FSM_SUBSTATE_NONE
Definition
fsm.h:39
K_FSM_MAX_TABLE_SIZE
#define K_FSM_MAX_TABLE_SIZE
Definition
fsm.h:16
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_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:63
FSM_Handle_t::prevSubState
FSM_SubState_t prevSubState
Definition
fsm.h:67
FSM_Handle_t::prevState
FSM_State_t prevState
Definition
fsm.h:66
FSM_Handle_t::state
FSM_State_t state
Definition
fsm.h:64
FSM_Handle_t::subState
FSM_SubState_t subState
Definition
fsm.h:65
FSM_Transition_t
A single row in the FSM transition table.
Definition
fsm.h:49
FSM_Transition_t::fromSubState
FSM_SubState_t fromSubState
Definition
fsm.h:51
FSM_Transition_t::guard
spp_bool_t(*) guard(void)
Definition
fsm.h:54
FSM_Transition_t::stateFunction
void(*) stateFunction(void)
Definition
fsm.h:56
FSM_Transition_t::fromState
FSM_State_t fromState
Definition
fsm.h:50
FSM_Transition_t::toState
FSM_State_t toState
Definition
fsm.h:52
FSM_Transition_t::action
void(*) action(void)
Definition
fsm.h:55
FSM_Transition_t::toSubState
FSM_SubState_t toSubState
Definition
fsm.h:53
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