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 // Can only be started once.
63 if (p_state == ETL_NULLPTR)
64 {
65 p_state = state_list[0];
66 ETL_ASSERT(p_state != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
67
69 {
70 etl::fsm_state_id_t next_state = do_enters(ETL_NULLPTR, p_state, true);
71
72 if (next_state != ifsm_state::No_State_Change)
73 {
74 p_state = state_list[next_state];
75 }
76 }
77 }
78 }
79
80 //*******************************************
83 //*******************************************
84 virtual void reset(bool call_on_exit_state = false) ETL_OVERRIDE
85 {
86 if ((p_state != ETL_NULLPTR) && call_on_exit_state)
87 {
88 do_exits(ETL_NULLPTR, p_state);
89 }
90
91 p_state = ETL_NULLPTR;
92 }
93
94 using fsm::receive;
95
96 //*******************************************
98 //*******************************************
99 void receive(const etl::imessage& message) ETL_OVERRIDE
100 {
101 if (is_started())
102 {
103 etl::fsm_state_id_t next_state_id = p_state->process_event(message);
104
105 if (have_changed_state(next_state_id))
106 {
107 ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
108
109 etl::ifsm_state* p_next_state = state_list[next_state_id];
110 etl::ifsm_state* p_root = common_ancestor(p_state, p_next_state);
111
112 do_exits(p_root, p_state);
113 next_state_id = do_enters(p_root, p_next_state, true);
114
115 ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
116 p_state = state_list[next_state_id];
117 }
118 else if (is_self_transition(next_state_id))
119 {
120 p_state->on_exit_state();
121 p_state->on_enter_state();
122 }
123 }
124 else
125 {
126 ETL_ASSERT_FAIL(ETL_ERROR(etl::fsm_not_started));
127 }
128 }
129
130 private:
131
132 //*******************************************
134 //*******************************************
135 static etl::ifsm_state* common_ancestor(etl::ifsm_state* s1, etl::ifsm_state* s2)
136 {
137 size_t depth1 = get_depth(s1);
138 size_t depth2 = get_depth(s2);
139
140 // Adjust s1 and s2 to the same depth.
141 if (depth1 > depth2)
142 {
143 s1 = adjust_depth(s1, depth1 - depth2);
144 }
145 else
146 {
147 s2 = adjust_depth(s2, depth2 - depth1);
148 }
149
150 // Now they're aligned to the same depth they can step towards the root together.
151 while (s1 != s2)
152 {
153 s1 = s1->p_parent;
154 s2 = s2->p_parent;
155 }
156
157 return s1;
158 }
159
160 //*******************************************
162 //*******************************************
163 static size_t get_depth(etl::ifsm_state* s)
164 {
165 size_t depth = 0UL;
166
167 while (s != ETL_NULLPTR)
168 {
169 s = s->p_parent;
170 ++depth;
171 }
172
173 return depth;
174 }
175
176 //*******************************************
178 //*******************************************
179 static etl::ifsm_state* adjust_depth(etl::ifsm_state* s, size_t offset)
180 {
181 while (offset != 0U)
182 {
183 s = s->p_parent;
184 --offset;
185 }
186
187 return s;
188 }
189
190 //*******************************************
192 //*******************************************
194 {
195 ETL_ASSERT(p_target != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
196
197 // We need to go recursively up the tree if the target and root don't 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 state will be deactivated.
203 do_enters(p_root, p_target->p_parent, false);
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 ETL_ASSERT(ifsm_state::No_State_Change == next_state, ETL_ERROR(etl::fsm_state_composite_state_change_forbidden));
212
213 // Activate default child if we need to activate any initial states in an active composite state.
215 {
216 while (p_target->p_default_child != ETL_NULLPTR)
217 {
218 p_target = p_target->p_default_child;
219 p_target->p_parent->p_active_child = p_target;
220 next_state = p_target->on_enter_state();
221 ETL_ASSERT(ifsm_state::No_State_Change == next_state, ETL_ERROR(etl::fsm_state_composite_state_change_forbidden));
222 }
223
224 next_state = p_target->get_state_id();
225 }
226
227 return next_state;
228 }
229
230 //*******************************************
232 //*******************************************
233 static void do_exits(const etl::ifsm_state* p_root, etl::ifsm_state* p_source)
234 {
235 etl::ifsm_state* p_current = p_source;
236
237 // Iterate down to the lowest child
238 while (p_current->p_active_child != ETL_NULLPTR)
239 {
240 p_current = p_current->p_active_child;
241 }
242
243 // Run exit state on all states up to the root
244 while (p_current != p_root)
245 {
246 p_current->on_exit_state();
247 p_current = p_current->p_parent;
248 }
249 }
250 };
251}
252#endif
Exception for message received but not started.
Definition fsm.h:176
Exception for null state pointer.
Definition fsm.h:112
Exception for forbidden state changes.
Definition fsm.h:164
Exception for invalid state id.
Definition fsm.h:125
The FSM class.
Definition fsm.h:344
void receive(const etl::imessage &message) ETL_OVERRIDE
Top level message handler for the FSM.
Definition fsm.h:417
bool is_started() const
Checks if the FSM has been started.
Definition fsm.h:495
Definition hfsm.h:42
void receive(const etl::imessage &message) ETL_OVERRIDE
Top level message handler for the HFSM.
Definition hfsm.h:99
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:84
hfsm(etl::message_router_id_t id)
Constructor.
Definition hfsm.h:48
Interface class for FSM states.
Definition fsm.h:216
Definition message.h:73
Definition message.h:91
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314
uint_least8_t fsm_state_id_t
Allow alternative type for state id.
Definition fsm.h:75