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
189 //*******************************************
191 //*******************************************
192 static do_enters_result do_enters(const etl::ifsm_state* p_root, etl::ifsm_state* p_target, bool activate_default_children)
193 {
194 ETL_ASSERT(p_target != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
195
196 // We need to go recursively up the tree if the target and root don't
197 // match
198 if ((p_root != p_target) && (p_target->p_parent != ETL_NULLPTR))
199 {
200 if (p_target->p_parent != p_root)
201 {
202 // The parent we're calling shouldn't activate its defaults, or this
203 // state will be deactivated
204 do_enters_result result = do_enters(p_root, p_target->p_parent, false);
205
206 // Short circuit the do enters if the parent state decided that a
207 // different state should be entered
208 if (result.active_state_id != ifsm_state::No_State_Change)
209 {
210 return result;
211 }
212 }
213
214 // Set us as our parent's active child
215 p_target->p_parent->p_active_child = p_target;
216 }
217
218 etl::fsm_state_id_t next_state = p_target->on_enter_state();
219
220 // Short circuit the activation of any child states if the target state
221 // changed
222 if (next_state != ifsm_state::No_State_Change)
223 {
224 return {next_state, p_target->get_state_id()};
225 }
226
227 // Activate default child if we need to activate any initial states in an
228 // active composite state
229 if (activate_default_children)
230 {
231 while (p_target->p_default_child != ETL_NULLPTR)
232 {
233 p_target = p_target->p_default_child;
234 p_target->p_parent->p_active_child = p_target;
235 next_state = p_target->on_enter_state();
236
237 // Short circuit the activation of any child states if the target
238 // state changed
239 if (next_state != ifsm_state::No_State_Change)
240 {
241 return {next_state, p_target->get_state_id()};
242 }
243 }
244
245 next_state = p_target->get_state_id();
246 }
247
248 // Wrapping No_State_Change in a static_cast gets rid of the "undefined
249 // reference" error when compiling on C++11
250 return {next_state, static_cast<fsm_state_id_t>(ifsm_state::No_State_Change)};
251 }
252
253 //*******************************************
255 //*******************************************
256 static void do_exits(const etl::ifsm_state* p_root, etl::ifsm_state* p_source)
257 {
258 etl::ifsm_state* p_current = p_source;
259
260 // Iterate down to the lowest child
261 while (p_current->p_active_child != ETL_NULLPTR)
262 {
263 p_current = p_current->p_active_child;
264 }
265
266 // Run exit state on all states up to the root
267 while (p_current != p_root)
268 {
269 p_current->on_exit_state();
270 p_current = p_current->p_parent;
271 }
272 }
273
274 //*******************************************
276 //*******************************************
277 etl::fsm_state_id_t process_state_change(etl::fsm_state_id_t next_state_id) ETL_OVERRIDE
278 {
279 if (is_self_transition(next_state_id))
280 {
281 p_state->on_exit_state();
282 next_state_id = p_state->on_enter_state();
283 }
284
285 while (have_changed_state(next_state_id))
286 {
287 ETL_ASSERT_OR_RETURN_VALUE(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception), p_state->get_state_id());
288
289 etl::ifsm_state* p_next_state = state_list[next_state_id];
290 etl::ifsm_state* p_root = common_ancestor(p_state, p_next_state);
291
292 do_exits(p_root, p_state);
293
294 do_enters_result result = do_enters(p_root, p_next_state, true);
295 next_state_id = result.next_state_id;
296
297 if (result.active_state_id != ifsm_state::No_State_Change)
298 {
299 // If the active_state_id is not No_State_Change, it means that an
300 // on_enter changed the target state. Set the state pointer as the
301 // active state to use it as the new origin for the transition to the
302 // updated target state.
303 ETL_ASSERT(result.active_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
304 p_state = state_list[result.active_state_id];
305 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
306 p_next_state = state_list[result.next_state_id];
307 }
308 else if (result.next_state_id != ifsm_state::No_State_Change)
309 {
310 // If the next state is different, means that default children were
311 // activated. Assign both p_state and p_next_state to get out of the
312 // loop.
313 ETL_ASSERT(result.next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
314 p_state = state_list[result.next_state_id];
315 p_next_state = state_list[result.next_state_id];
316 }
317 else
318 {
319 p_state = p_next_state;
320 }
321 }
322
323 return p_state->get_state_id();
324 }
325 };
326} // namespace etl
327#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:437
fsm(etl::message_router_id_t id)
Constructor.
Definition fsm.h:446
bool is_started() const
Checks if the FSM has been started.
Definition fsm.h:606
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:314
etl::fsm_state_id_t get_state_id() const
Gets the id for this state.
Definition fsm.h:335
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
bitset_ext
Definition absolute.h:40
uint_least8_t fsm_state_id_t
Allow alternative type for state id.
Definition fsm.h:57