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 //*******************************************
61 //*******************************************
62 void start(bool call_on_enter_state = true) ETL_OVERRIDE
63 {
64 private_fsm::fsm_reentrancy_guard transition_lock(is_processing_state_change);
65
66 // Can only be started once.
67 if (!is_started())
68 {
69 etl::ifsm_state* p_first_state = state_list[0];
70 ETL_ASSERT(p_first_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
71 p_state = p_first_state;
72
73 if (call_on_enter_state)
74 {
75 do_enters_result result = do_enters(ETL_NULLPTR, p_first_state, true);
76
77 if (result.active_state_id != ifsm_state::No_State_Change)
78 {
79 // If the active_state_id is not No_State_Change, it means that an
80 // on_enter changed the target state. Set the state pointer as the
81 // active state to use it as the new origin for the transition to
82 // the updated target state.
83 ETL_ASSERT(result.active_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
84 p_state = state_list[result.active_state_id];
85
86 process_state_change(result.next_state_id);
87 }
88 else
89 {
90 if (have_changed_state(result.next_state_id))
91 {
92 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
93 p_state = state_list[result.next_state_id];
94 }
95 }
96 }
97 }
98 }
99
100 //*******************************************
104 //*******************************************
105 virtual void reset(bool call_on_exit_state = false) ETL_OVERRIDE
106 {
107 private_fsm::fsm_reentrancy_guard transition_lock(is_processing_state_change);
108
109 if (is_started() && call_on_exit_state)
110 {
111 do_exits(ETL_NULLPTR, p_state);
112 }
113
114 p_state = ETL_NULLPTR;
115 }
116
117 private:
118
119 //*******************************************
121 //*******************************************
122 static etl::ifsm_state* common_ancestor(etl::ifsm_state* s1, etl::ifsm_state* s2)
123 {
124 size_t depth1 = get_depth(s1);
125 size_t depth2 = get_depth(s2);
126
127 // Adjust s1 and s2 to the same depth.
128 if (depth1 > depth2)
129 {
130 s1 = adjust_depth(s1, depth1 - depth2);
131 }
132 else
133 {
134 s2 = adjust_depth(s2, depth2 - depth1);
135 }
136
137 // Now they're aligned to the same depth they can step towards the root
138 // together.
139 while (s1 != s2)
140 {
141 s1 = s1->p_parent;
142 s2 = s2->p_parent;
143 }
144
145 return s1;
146 }
147
148 //*******************************************
150 //*******************************************
151 static size_t get_depth(etl::ifsm_state* s)
152 {
153 size_t depth = 0UL;
154
155 while (s != ETL_NULLPTR)
156 {
157 s = s->p_parent;
158 ++depth;
159 }
160
161 return depth;
162 }
163
164 //*******************************************
166 //*******************************************
167 static etl::ifsm_state* adjust_depth(etl::ifsm_state* s, size_t offset)
168 {
169 while (offset != 0U)
170 {
171 s = s->p_parent;
172 --offset;
173 }
174
175 return s;
176 }
177
178 //*******************************************
180 //*******************************************
181 struct do_enters_result
182 {
183 // State which is presently being targeted as the next state
184 etl::fsm_state_id_t next_state_id;
185 // State which was active when the on_enter triggered a state change
186 etl::fsm_state_id_t active_state_id;
187
188 do_enters_result(etl::fsm_state_id_t next_state_id_, etl::fsm_state_id_t active_state_id_)
189 : next_state_id(next_state_id_)
190 , active_state_id(active_state_id_)
191 {
192 }
193 };
194
195 //*******************************************
197 //*******************************************
198 static do_enters_result do_enters(const etl::ifsm_state* p_root, etl::ifsm_state* p_target, bool activate_default_children)
199 {
200 ETL_ASSERT(p_target != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
201
202 // We need to go recursively up the tree if the target and root don't
203 // match
204 if ((p_root != p_target) && (p_target->p_parent != ETL_NULLPTR))
205 {
206 if (p_target->p_parent != p_root)
207 {
208 // The parent we're calling shouldn't activate its defaults, or this
209 // state will be deactivated
210 do_enters_result result = do_enters(p_root, p_target->p_parent, false);
211
212 // Short circuit the do enters if the parent state decided that a
213 // different state should be entered
214 if (result.active_state_id != ifsm_state::No_State_Change)
215 {
216 return result;
217 }
218 }
219
220 // Set us as our parent's active child
221 p_target->p_parent->p_active_child = p_target;
222 }
223
224 etl::fsm_state_id_t next_state = p_target->on_enter_state();
225
226 // Short circuit the activation of any child states if the target state
227 // changed
228 if (next_state != ifsm_state::No_State_Change)
229 {
230 return do_enters_result(next_state, p_target->get_state_id());
231 }
232
233 // Activate default child if we need to activate any initial states in an
234 // active composite state
235 if (activate_default_children)
236 {
237 while (p_target->p_default_child != ETL_NULLPTR)
238 {
239 p_target = p_target->p_default_child;
240 p_target->p_parent->p_active_child = p_target;
241 next_state = p_target->on_enter_state();
242
243 // Short circuit the activation of any child states if the target
244 // state changed
245 if (next_state != ifsm_state::No_State_Change)
246 {
247 return do_enters_result(next_state, p_target->get_state_id());
248 }
249 }
250
251 next_state = p_target->get_state_id();
252 }
253
254 // Wrapping No_State_Change in a static_cast gets rid of the "undefined
255 // reference" error when compiling on C++11
256 return do_enters_result(next_state, static_cast<fsm_state_id_t>(ifsm_state::No_State_Change));
257 }
258
259 //*******************************************
261 //*******************************************
262 static void do_exits(const etl::ifsm_state* p_root, etl::ifsm_state* p_source)
263 {
264 etl::ifsm_state* p_current = p_source;
265
266 // Iterate down to the lowest child
267 while (p_current->p_active_child != ETL_NULLPTR)
268 {
269 p_current = p_current->p_active_child;
270 }
271
272 // Run exit state on all states up to the root
273 while (p_current != p_root)
274 {
275 p_current->on_exit_state();
276 p_current = p_current->p_parent;
277 }
278 }
279
280 //*******************************************
282 //*******************************************
283 etl::fsm_state_id_t process_state_change(etl::fsm_state_id_t next_state_id) ETL_OVERRIDE
284 {
285 if (is_self_transition(next_state_id))
286 {
287 p_state->on_exit_state();
288 next_state_id = p_state->on_enter_state();
289 }
290
291 while (have_changed_state(next_state_id))
292 {
293 ETL_ASSERT_OR_RETURN_VALUE(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception), p_state->get_state_id());
294
295 etl::ifsm_state* p_next_state = state_list[next_state_id];
296 etl::ifsm_state* p_root = common_ancestor(p_state, p_next_state);
297
298 do_exits(p_root, p_state);
299
300 do_enters_result result = do_enters(p_root, p_next_state, true);
301 next_state_id = result.next_state_id;
302
303 if (result.active_state_id != ifsm_state::No_State_Change)
304 {
305 // If the active_state_id is not No_State_Change, it means that an
306 // on_enter changed the target state. Set the state pointer as the
307 // active state to use it as the new origin for the transition to the
308 // updated target state.
309 ETL_ASSERT(result.active_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
310 p_state = state_list[result.active_state_id];
311 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
312 p_next_state = state_list[result.next_state_id];
313 }
314 else if (result.next_state_id != ifsm_state::No_State_Change)
315 {
316 // If the next state is different, means that default children were
317 // activated. Assign both p_state and p_next_state to get out of the
318 // loop.
319 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
320 p_state = state_list[result.next_state_id];
321 p_next_state = state_list[result.next_state_id];
322 }
323 else
324 {
325 p_state = p_next_state;
326 }
327 }
328
329 return p_state->get_state_id();
330 }
331 };
332} // namespace etl
333#endif
Exception for null state pointer.
Definition fsm.h:89
Exception for invalid state id.
Definition fsm.h:102
The FSM class.
Definition fsm.h:439
fsm(etl::message_router_id_t id)
Constructor.
Definition fsm.h:448
bool is_started() const
Checks if the FSM has been started.
Definition fsm.h:608
void start(bool call_on_enter_state=true) ETL_OVERRIDE
Definition hfsm.h:62
virtual void reset(bool call_on_exit_state=false) ETL_OVERRIDE
Definition hfsm.h:105
hfsm(etl::message_router_id_t id)
Constructor.
Definition hfsm.h:48
Interface class for FSM states.
Definition fsm.h:316
etl::fsm_state_id_t get_state_id() const
Gets the id for this state.
Definition fsm.h:337
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
Definition absolute.h:40
uint_least8_t fsm_state_id_t
Allow alternative type for state id.
Definition fsm.h:57