-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyfsm.h
150 lines (133 loc) · 4.11 KB
/
tinyfsm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef _TINYFSM_H_
#define _TINYFSM_H_
/**
* @file tinyfsm.h
* @brief Tiny Finite State Machine (FSM) framework.
*
* @author Edwin Koch
* @date 10. June 2024
*
* @license MIT License
* See the LICENSE file in the project root for more information.
*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Typedef for FSM action function pointer.
*/
typedef void (*fsmAction_t)(void);
/**
* @brief Typedef for mutex object.
*/
typedef void* fsmMutex_t;
/**
* @ brief Mutex operations structure definition
*/
typedef struct{
unsigned int (*lock)(fsmMutex_t); /**< mutex lock function pointer */
unsigned int (*unlock)(fsmMutex_t); /**< mutex unlock function pointer */
}fsmMutexOps_t;
/**
* @brief FSM status codes.
*/
typedef enum {
FSM_RUNNING, /**< FSM is running */
FSM_ENDED, /**< FSM has ended */
FSM_NOT_INITIALIZED, /**< FSM is ot initialized */
FSM_MUTEX_LOCKED, /**< FSM mutex is locked */
FSM_FAULT_UNKNOWN_STATE_RETURN /**< FSM encountered an unknown state return */
} fsmStatus_t;
/**
* @brief FSM state semaphore codes.
*/
typedef enum {
STATE_START, /**< FSM is in start state */
STATE_TRANSITION, /**< FSM is in transition state */
STATE_NO_CHANGE, /**< FSM state has no change */
STATE_END_FSM /**< FSM has ended */
} fsmStateSemaphore_t;
/**
* @brief Typedef for FSM state routine function pointer.
*/
typedef void (*fsmStateRoutine_t)(void);
/**
* @brief FSM structure definition.
*/
typedef struct {
fsmStateRoutine_t entryState; /**< Entry state routine */
fsmAction_t entryAction; /**< Entry action */
fsmAction_t fsmEndingAction; /**< FSM ending action */
fsmStateRoutine_t previousState; /**< Previous state routine */
fsmStateRoutine_t currentState; /**< Current state routine */
fsmStateRoutine_t nextState; /**< Next state routine */
fsmStateSemaphore_t state; /**< Current state semaphore */
fsmAction_t action; /**< Current action */
fsmMutex_t mutexObject; /**< Pointer to mutex object */
fsmMutexOps_t mutexOps; /**< Mutex operations */
unsigned int initialized; /**< Flag to indicate if the FSM is initialized. 0 = false and 1 = true */
} fsm_t;
/**
* @brief Initialize the FSM.
*
* @param fsmObject Pointer to the FSM object.
* @param entryState Pointer to the entry state routine.
* @param entryAction Pointer to the entry action function.
* @param fsmEndingAction Pointer to the FSM endign action to be executed upon ending the FSM.
* @param mutexObject Mutex Object for thread safety functionality
* @param mutexOps Mutex operatrions for thread safety functionality
*/
void fsmInit(
fsm_t* fsmObject,
fsmStateRoutine_t entryState,
fsmAction_t entryAction,
fsmAction_t fsmEndingAction,
fsmMutex_t mutexObject,
fsmMutexOps_t mutexOps);
/**
* @brief Initialize the FSM for single threaded usecase.
*
* @param fsmObject Pointer to the FSM object.
* @param entryState Pointer to the entry state routine.
* @param entryAction Pointer to the entry action function.
* @param fsmEndingAction Pointer to the FSM endign action to be executed upon ending the FSM.
*/
void fsmInitSingleThreaded(
fsm_t* fsmObject,
fsmStateRoutine_t entryState,
fsmAction_t entryAction,
fsmAction_t fsmEndingAction);
/**
* @brief Run the FSM.
*
* @param fsmObject Pointer to the FSM object.
* @return Status of the FSM (fsmStatus_t).
*/
fsmStatus_t fsmRun(
fsm_t* fsmObject);
/**
* @brief Transition to the next state in the FSM.
*
* @param fsmObject Pointer to the FSM object.
* @param nextState Pointer to the next state routine.
* @param action Pointer to the action function to be executed during transition.
*/
void fsmTransitionState(
fsm_t* fsmObject,
fsmStateRoutine_t nextState,
fsmAction_t action);
/**
* @brief End the FSM.
*
* @param fsmObject Pointer to the FSM object.
*/
void fsmEndFSM(
fsm_t* fsmObject);
/**
* @brief No action function for the FSM.
*/
void fsmNoAction();
#ifdef __cplusplus
}
#endif
#endif // _TINYFSM_H_