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 (next_state_id != ifsm_state::No_State_Change)
106 {
107 ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
108 etl::ifsm_state* p_next_state = state_list[next_state_id];
109
110 // Have we changed state?
111 if (p_next_state != p_state)
112 {
113 etl::ifsm_state* p_root = common_ancestor(p_state, p_next_state);
114 do_exits(p_root, p_state);
115
116 p_state = p_next_state;
117
118 next_state_id = do_enters(p_root, p_next_state, true);
119
120 if (next_state_id != ifsm_state::No_State_Change)
121 {
122 ETL_ASSERT_OR_RETURN(next_state_id < number_of_states, ETL_ERROR(etl::fsm_state_id_exception));
123 p_state = state_list[next_state_id];
124 }
125 }
126 }
127 }
128 else
129 {
130 ETL_ASSERT_FAIL(ETL_ERROR(etl::fsm_not_started));
131 }
132 }
133
134 private:
135
136 //*******************************************
138 //*******************************************
139 static etl::ifsm_state* common_ancestor(etl::ifsm_state* s1, etl::ifsm_state* s2)
140 {
141 size_t depth1 = get_depth(s1);
142 size_t depth2 = get_depth(s2);
143
144 // Adjust s1 and s2 to the same depth.
145 if (depth1 > depth2)
146 {
147 s1 = adjust_depth(s1, depth1 - depth2);
148 }
149 else
150 {
151 s2 = adjust_depth(s2, depth2 - depth1);
152 }
153
154 // Now they're aligned to the same depth they can step towards the root together.
155 while (s1 != s2)
156 {
157 s1 = s1->p_parent;
158 s2 = s2->p_parent;
159 }
160
161 return s1;
162 }
163
164 //*******************************************
166 //*******************************************
167 static size_t get_depth(etl::ifsm_state* s)
168 {
169 size_t depth = 0UL;
170
171 while (s != ETL_NULLPTR)
172 {
173 s = s->p_parent;
174 ++depth;
175 }
176
177 return depth;
178 }
179
180 //*******************************************
182 //*******************************************
183 static etl::ifsm_state* adjust_depth(etl::ifsm_state* s, size_t offset)
184 {
185 while (offset != 0U)
186 {
187 s = s->p_parent;
188 --offset;
189 }
190
191 return s;
192 }
193
194 //*******************************************
196 //*******************************************
197 static etl::fsm_state_id_t do_enters(const etl::ifsm_state* p_root, etl::ifsm_state* p_target, bool activate_default_children)
198 {
199 ETL_ASSERT(p_target != ETL_NULLPTR, ETL_ERROR(etl::fsm_null_state_exception));
200
201 // We need to go recursively up the tree if the target and root don't match
202 if ((p_root != p_target) && (p_target->p_parent != ETL_NULLPTR))
203 {
204 if (p_target->p_parent != p_root)
205 {
206 // The parent we're calling shouldn't activate its defaults, or this state will be deactivated.
207 do_enters(p_root, p_target->p_parent, false);
208 }
209
210 // Set us as our parent's active child
211 p_target->p_parent->p_active_child = p_target;
212 }
213
214 etl::fsm_state_id_t next_state = p_target->on_enter_state();
215 ETL_ASSERT(ifsm_state::No_State_Change == next_state, ETL_ERROR(etl::fsm_state_composite_state_change_forbidden));
216
217 // Activate default child if we need to activate any initial states in an active composite state.
218 if (activate_default_children)
219 {
220 while (p_target->p_default_child != ETL_NULLPTR)
221 {
222 p_target = p_target->p_default_child;
223 p_target->p_parent->p_active_child = p_target;
224 next_state = p_target->on_enter_state();
225 ETL_ASSERT(ifsm_state::No_State_Change == next_state, ETL_ERROR(etl::fsm_state_composite_state_change_forbidden));
226 }
227
228 next_state = p_target->get_state_id();
229 }
230
231 return next_state;
232 }
233
234 //*******************************************
236 //*******************************************
237 static void do_exits(const etl::ifsm_state* p_root, etl::ifsm_state* p_source)
238 {
239 etl::ifsm_state* p_current = p_source;
240
241 // Iterate down to the lowest child
242 while (p_current->p_active_child != ETL_NULLPTR)
243 {
244 p_current = p_current->p_active_child;
245 }
246
247 // Run exit state on all states up to the root
248 while (p_current != p_root)
249 {
250 p_current->on_exit_state();
251 p_current = p_current->p_parent;
252 }
253 }
254 };
255}
256#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
etl::fsm_state_id_t get_state_id() const
Gets the id for this state.
Definition fsm.h:242
Definition message.h:73
Definition message.h:91
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
bitset_ext
Definition absolute.h:38
pair holds two objects of arbitrary type
Definition utility.h:164