inc/ecu/hsm.h Source File

ECU: inc/ecu/hsm.h Source File
ECU
hsm.h
Go to the documentation of this file.
1 
14 #ifndef ECU_HSM_H_
15 #define ECU_HSM_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_HSM_GET_CONTEXT(ecu_hsm_ptr_, type_, member_) \
47  ECU_CONTAINER_OF(ecu_hsm_ptr_, type_, member_)
48 
53 #define ECU_HSM_STATE_ENTRY_UNUSED \
54  ((void (*)(struct ecu_hsm *))0)
55 
60 #define ECU_HSM_STATE_EXIT_UNUSED \
61  ((void (*)(struct ecu_hsm *))0)
62 
69 #define ECU_HSM_STATE_INITIAL_UNUSED \
70  ((void (*)(struct ecu_hsm *))0)
71 
97 #define ECU_HSM_STATE_CTOR(entry_, exit_, initial_, handler_, parent_) \
98  { \
99  .entry = (entry_), \
100  .exit = (exit_), \
101  .initial = (initial_), \
102  .handler = (handler_), \
103  .parent = (parent_) \
104  }
105 
106 /*------------------------------------------------------------*/
107 /*---------------------------- HSM ---------------------------*/
108 /*------------------------------------------------------------*/
109 
110 /* Forward declaration for ecu_hsm_state. */
111 struct ecu_hsm;
112 
122 {
123 /* Normally, pointers are const-qualified to only allow states to
124 be created at compile-time. Const is stripped only for unit tests
125 since test code framework requires state creation at run-time. */
126 #ifdef ECU_UNIT_TEST
128  void (*entry)(struct ecu_hsm *me);
129 
131  void (*exit)(struct ecu_hsm *me);
132 
136  void (*initial)(struct ecu_hsm *me);
137 
141  bool (*handler)(struct ecu_hsm *me, const void *event);
142 
144  const struct ecu_hsm_state *parent;
145 #else
147  void (*const entry)(struct ecu_hsm *me);
148 
150  void (*const exit)(struct ecu_hsm *me);
151 
155  void (*const initial)(struct ecu_hsm *me);
156 
160  bool (*const handler)(struct ecu_hsm *me, const void *event);
161 
163  const struct ecu_hsm_state *const parent;
164 #endif
165 };
166 
174 struct ecu_hsm
175 {
177  const struct ecu_hsm_state *state;
178 
182  uint8_t height;
183 
185  uint32_t transition;
186 };
187 
188 /*------------------------------------------------------------*/
189 /*---------------------- GLOBAL VARIABLES --------------------*/
190 /*------------------------------------------------------------*/
191 
192 #ifdef __cplusplus
193 extern "C" {
194 #endif
195 
200 extern const struct ecu_hsm_state ECU_HSM_TOP_STATE;
201 
202 /*------------------------------------------------------------*/
203 /*-------------------- HSM MEMBER FUNCTIONS ------------------*/
204 /*------------------------------------------------------------*/
205 
221 extern void ecu_hsm_ctor(struct ecu_hsm *me,
222  const struct ecu_hsm_state *state,
223  uint8_t height);
244 extern void ecu_hsm_change_state(struct ecu_hsm *me, const struct ecu_hsm_state *state);
245 
261 extern void ecu_hsm_dispatch(struct ecu_hsm *me, const void *event);
262 
277 extern void ecu_hsm_start(struct ecu_hsm *me);
280 #ifdef __cplusplus
281 }
282 #endif
283 
284 #endif /* ECU_HSM_H_ */
const struct ecu_hsm_state ECU_HSM_TOP_STATE
The default top state. Serves as the root of every hsm.
void ecu_hsm_start(struct ecu_hsm *me)
Starts the hsm by entering from ECU_HSM_TOP_STATE to the target state supplied in ecu_hsm_ctor()....
void ecu_hsm_ctor(struct ecu_hsm *me, const struct ecu_hsm_state *state, uint8_t height)
Hsm constructor.
void ecu_hsm_dispatch(struct ecu_hsm *me, const void *event)
Relays event to hsm where it is processed by the current state's handler function....
void ecu_hsm_change_state(struct ecu_hsm *me, const struct ecu_hsm_state *state)
Transitions hsm into a new state.
Single state in hsm, initialized via ECU_HSM_STATE_CTOR(). Pointers are const-qualified to only allow...
Definition: hsm.h:122
bool(*const handler)(struct ecu_hsm *me, const void *event)
Processes events dispatched to this state. Mandatory. Return true if the dispatched event is processe...
Definition: hsm.h:160
void(*const entry)(struct ecu_hsm *me)
Executes when state first entered. Optional.
Definition: hsm.h:147
const struct ecu_hsm_state *const parent
This state's parent.
Definition: hsm.h:163
void(*const exit)(struct ecu_hsm *me)
Executes when state exits. Optional.
Definition: hsm.h:150
void(*const initial)(struct ecu_hsm *me)
Transitions to starting (child) state when a transition targets a composite state....
Definition: hsm.h:155
Hierarchical state machine. Users create their own HSMs by containing this as an intrusive member.
Definition: hsm.h:175
const struct ecu_hsm_state * state
Current state the hsm is in.
Definition: hsm.h:177
uint8_t height
Number of levels in the hsm, starting at 0. Used as a fail safe to avoid infinite loops when traversi...
Definition: hsm.h:182
uint32_t transition
Bitmap representing hsm's state transition context.
Definition: hsm.h:185