Embedded Template Library 1.0
Loading...
Searching...
No Matches
hfsm.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2021 Jeremy Overesch, John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_HFSM_INCLUDED
30#define ETL_HFSM_INCLUDED
31
32#include "fsm.h"
33
34namespace etl
35{
36 //***************************************************************************
40 //***************************************************************************
41 class hfsm : public etl::fsm
42 {
43 public:
44
45 //*******************************************
47 //*******************************************
48 hfsm(etl::message_router_id_t id)
49 : fsm(id)
50 {
51 }
52
53 //*******************************************
59 //*******************************************
60 void start(bool call_on_enter_state = true) ETL_OVERRIDE
61 {
62 private_fsm::fsm_reentrancy_guard transition_lock(is_processing_state_change);
63
64 // Can only be started once.
65 if (!is_started())
66 {
67 etl::ifsm_state* p_first_state = state_list[0];
68 ETL_ASSERT(p_first_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
69 p_state = p_first_state;
70
71 if (call_on_enter_state)
72 {
73 do_enters_result result = do_enters(ETL_NULLPTR, p_first_state, true);
74
75 if (result.active_state_id != ifsm_state::No_State_Change)
76 {
77 // If the active_state_id is not No_State_Change, it means that an on_enter changed the target state.
78 // Set the state pointer as the active state to use it as the new origin for the transition to the
79 // updated target state.
80 ETL_ASSERT(result.active_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
81 p_state = state_list[result.active_state_id];
82
83 process_state_change(result.next_state_id);
84 }
85 else
86 {
87 if (have_changed_state(result.next_state_id))
88 {
89 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
90 p_state = state_list[result.next_state_id];
91 }
92 }
93 }
94 }
95 }
96
97 //*******************************************
100 //*******************************************
101 virtual void reset(bool call_on_exit_state = false) ETL_OVERRIDE
102 {
103 private_fsm::fsm_reentrancy_guard transition_lock(is_processing_state_change);
104
105 if (is_started() && call_on_exit_state)
106 {
107 do_exits(ETL_NULLPTR, p_state);
108 }
109
110 p_state = ETL_NULLPTR;
111 }
112
113 private:
114
115 //*******************************************
117 //*******************************************
118 static etl::ifsm_state* common_ancestor(etl::ifsm_state* s1, etl::ifsm_state* s2)
119 {
120 size_t depth1 = get_depth(s1);
121 size_t depth2 = get_depth(s2);
122
123 // Adjust s1 and s2 to the same depth.
124 if (depth1 > depth2)
125 {
126 s1 = adjust_depth(s1, depth1 - depth2);
127 }
128 else
129 {
130 s2 = adjust_depth(s2, depth2 - depth1);
131 }
132
133 // Now they're aligned to the same depth they can step towards the root together.
134 while (s1 != s2)
135 {
136 s1 = s1->p_parent;
137 s2 = s2->p_parent;
138 }
139
140 return s1;
141 }
142
143 //*******************************************
145 //*******************************************
146 static size_t get_depth(etl::ifsm_state* s)
147 {
148 size_t depth = 0UL;
149
150 while (s != ETL_NULLPTR)
151 {
152 s = s->p_parent;
153 ++depth;
154 }
155
156 return depth;
157 }
158
159 //*******************************************
161 //*******************************************
162 static etl::ifsm_state* adjust_depth(etl::ifsm_state* s, size_t offset)
163 {
164 while (offset != 0U)
165 {
166 s = s->p_parent;
167 --offset;
168 }
169
170 return s;
171 }
172
173 //*******************************************
175 //*******************************************
176 struct do_enters_result
177 {
178 // State which is presently being targeted as the next state
179 etl::fsm_state_id_t next_state_id;
180 // State which was active when the on_enter triggered a state change
181 etl::fsm_state_id_t active_state_id;
182 };
183
184 //*******************************************
186 //*******************************************
187 static do_enters_result do_enters(const etl::ifsm_state* p_root, etl::ifsm_state* p_target, bool activate_default_children)
188 {
189 ETL_ASSERT(p_target != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
190
191 // We need to go recursively up the tree if the target and root don't match
192 if ((p_root != p_target) && (p_target->p_parent != ETL_NULLPTR))
193 {
194 if (p_target->p_parent != p_root)
195 {
196 // The parent we're calling shouldn't activate its defaults, or this state will be deactivated
197 do_enters_result result = do_enters(p_root, p_target->p_parent, false);
198
199 // Short circuit the do enters if the parent state decided that a different state should be entered
200 if (result.active_state_id != ifsm_state::No_State_Change)
201 {
202 return result;
203 }
204 }
205
206 // Set us as our parent's active child
207 p_target->p_parent->p_active_child = p_target;
208 }
209
210 etl::fsm_state_id_t next_state = p_target->on_enter_state();
211
212 // Short circuit the activation of any child states if the target state changed
213 if (next_state != ifsm_state::No_State_Change)
214 {
215 return {next_state, p_target->get_state_id()};
216 }
217
218 // Activate default child if we need to activate any initial states in an active composite state
219 if (activate_default_children)
220 {
221 while (p_target->p_default_child != ETL_NULLPTR)
222 {
223 p_target = p_target->p_default_child;
224 p_target->p_parent->p_active_child = p_target;
225 next_state = p_target->on_enter_state();
226
227 // Short circuit the activation of any child states if the target state changed
228 if (next_state != ifsm_state::No_State_Change)
229 {
230 return {next_state, p_target->get_state_id()};
231 }
232 }
233
234 next_state = p_target->get_state_id();
235 }
236
237 // Wrapping No_State_Change in a static_cast gets rid of the "undefined reference" error when compiling on C++11
238 return {next_state, static_cast<fsm_state_id_t>(ifsm_state::No_State_Change)};
239 }
240
241 //*******************************************
243 //*******************************************
244 static void do_exits(const etl::ifsm_state* p_root, etl::ifsm_state* p_source)
245 {
246 etl::ifsm_state* p_current = p_source;
247
248 // Iterate down to the lowest child
249 while (p_current->p_active_child != ETL_NULLPTR)
250 {
251 p_current = p_current->p_active_child;
252 }
253
254 // Run exit state on all states up to the root
255 while (p_current != p_root)
256 {
257 p_current->on_exit_state();
258 p_current = p_current->p_parent;
259 }
260 }
261
262 //*******************************************
264 //*******************************************
265 etl::fsm_state_id_t process_state_change(etl::fsm_state_id_t next_state_id) ETL_OVERRIDE
266 {
267 if (is_self_transition(next_state_id))
268 {
269 p_state->on_exit_state();
270 next_state_id = p_state->on_enter_state();
271 }
272
273 while (have_changed_state(next_state_id))
274 {
275 ETL_ASSERT_OR_RETURN_VALUE(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception), p_state->get_state_id());
276
277 etl::ifsm_state* p_next_state = state_list[next_state_id];
278 etl::ifsm_state* p_root = common_ancestor(p_state, p_next_state);
279
280 do_exits(p_root, p_state);
281
282 do_enters_result result = do_enters(p_root, p_next_state, true);
283 next_state_id = result.next_state_id;
284
285 if (result.active_state_id != ifsm_state::No_State_Change)
286 {
287 // If the active_state_id is not No_State_Change, it means that an on_enter changed the target state.
288 // Set the state pointer as the active state to use it as the new origin for the transition to the
289 // updated target state.
290 ETL_ASSERT(result.active_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
291 p_state = state_list[result.active_state_id];
292 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
293 p_next_state = state_list[result.next_state_id];
294 }
295 else if (result.next_state_id != ifsm_state::No_State_Change)
296 {
297 // If the next state is different, means that default children were activated.
298 // Assign both p_state and p_next_state to get out of the loop.
299 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
300 p_state = state_list[result.next_state_id];
301 p_next_state = state_list[result.next_state_id];
302 }
303 else
304 {
305 p_state = p_next_state;
306 }
307 }
308
309 return p_state->get_state_id();
310 }
311 };
312}
313#endif
Exception for null state pointer.
Definition fsm.h:115
Exception for invalid state id.
Definition fsm.h:128
The FSM class.
Definition fsm.h:460
fsm(etl::message_router_id_t id)
Constructor.
Definition fsm.h:469
bool is_started() const
Checks if the FSM has been started.
Definition fsm.h:629
void start(bool call_on_enter_state=true) ETL_OVERRIDE
Definition hfsm.h:60
virtual void reset(bool call_on_exit_state=false) ETL_OVERRIDE
Definition hfsm.h:101
hfsm(etl::message_router_id_t id)
Constructor.
Definition hfsm.h:48
Interface class for FSM states.
Definition fsm.h:333
etl::fsm_state_id_t get_state_id() const
Gets the id for this state.
Definition fsm.h:359
RAII detection mechanism to catch reentrant calls to methods that might transition the state machine ...
Definition fsm.h:232
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
bitset_ext
Definition absolute.h:39
uint_least8_t fsm_state_id_t
Allow alternative type for state id.
Definition fsm.h:78