inc/ecu/fsm.h Source File

ECU: inc/ecu/fsm.h Source File
ECU
fsm.h
Go to the documentation of this file.
1 
14 #ifndef ECU_FSM_H_
15 #define ECU_FSM_H_
16 
17 /*------------------------------------------------------------*/
18 /*------------------------- INCLUDES -------------------------*/
19 /*------------------------------------------------------------*/
20 
21 /* STDLib. */
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 /* ECU. */
27 #include "ecu/utils.h"
28 
29 /*------------------------------------------------------------*/
30 /*---------------------- DEFINES AND MACROS ------------------*/
31 /*------------------------------------------------------------*/
32 
46 #define ECU_FSM_GET_CONTEXT(ecu_fsm_ptr_, type_, member_) \
47  ECU_CONTAINER_OF(ecu_fsm_ptr_, type_, member_)
48 
53 #define ECU_FSM_STATE_ENTRY_UNUSED \
54  ((void (*)(struct ecu_fsm *))0)
55 
60 #define ECU_FSM_STATE_EXIT_UNUSED \
61  ((void (*)(struct ecu_fsm *))0)
62 
82 #define ECU_FSM_STATE_CTOR(entry_, exit_, handler_) \
83  { \
84  .entry = (entry_), \
85  .exit = (exit_), \
86  .handler = (handler_) \
87  }
88 
89 /*------------------------------------------------------------*/
90 /*---------------------------- FSM ---------------------------*/
91 /*------------------------------------------------------------*/
92 
93 /* Forward declaration for ecu_fsm_state. */
94 struct ecu_fsm;
95 
105 {
106  /* Normally, pointers are const-qualified to only allow states to
107  be created at compile-time. Const is stripped only for unit tests
108  since test code framework requires state creation at run-time. */
109 #ifdef ECU_UNIT_TEST
111  void (*entry)(struct ecu_fsm *fsm);
112 
114  void (*exit)(struct ecu_fsm *fsm);
115 
117  void (*handler)(struct ecu_fsm *fsm, const void *event);
118 #else
120  void (*const entry)(struct ecu_fsm *fsm);
121 
123  void (*const exit)(struct ecu_fsm *fsm);
124 
126  void (*const handler)(struct ecu_fsm *fsm, const void *event);
127 #endif
128 };
129 
137 struct ecu_fsm
138 {
140  const struct ecu_fsm_state *state;
141 
143  uint32_t transition;
144 };
145 
146 /*------------------------------------------------------------*/
147 /*-------------------- FSM MEMBER FUNCTIONS ------------------*/
148 /*------------------------------------------------------------*/
149 
150 #ifdef __cplusplus
151 extern "C" {
152 #endif
153 
169 extern void ecu_fsm_ctor(struct ecu_fsm *me, const struct ecu_fsm_state *state);
187 extern void ecu_fsm_change_state(struct ecu_fsm *me, const struct ecu_fsm_state *state);
188 
201 extern void ecu_fsm_dispatch(struct ecu_fsm *me, const void *event);
202 
216 extern void ecu_fsm_start(struct ecu_fsm *me);
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif /* ECU_FSM_H_ */
void ecu_fsm_start(struct ecu_fsm *me)
Runs the initial state's entry handler and manages all state transition logic if any state changes we...
void ecu_fsm_ctor(struct ecu_fsm *me, const struct ecu_fsm_state *state)
Fsm constructor.
void ecu_fsm_change_state(struct ecu_fsm *me, const struct ecu_fsm_state *state)
Transitions fsm into a new state.
void ecu_fsm_dispatch(struct ecu_fsm *me, const void *event)
Relays event to fsm where it is processed by the current state's handler function....
Single state in fsm, initialized via ECU_FSM_STATE_CTOR(). Pointers are const-qualified to only allow...
Definition: fsm.h:105
void(*const exit)(struct ecu_fsm *fsm)
Executes when state exits. Optional.
Definition: fsm.h:123
void(*const entry)(struct ecu_fsm *fsm)
Executes when state first entered. Optional.
Definition: fsm.h:120
void(*const handler)(struct ecu_fsm *fsm, const void *event)
Processes events dispatched to this state. Mandatory.
Definition: fsm.h:126
Finite state machine. Users create their own FSMs by containing this as an intrusive member.
Definition: fsm.h:138
const struct ecu_fsm_state * state
Current state the fsm is in.
Definition: fsm.h:140
uint32_t transition
Bitmap representing fsm's state transition context.
Definition: fsm.h:143