• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief This file is a state machine of service manager.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file sys_state_machine.h
27  *
28  * @brief system state machine interface.
29  *
30  * @since 6
31  */
32 
33 #ifndef SYS_STATE_MACHINE_H
34 #define SYS_STATE_MACHINE_H
35 
36 #include "adapter_manager.h"
37 #include "util/state_machine.h"
38 
39 namespace bluetooth {
40 // system state machine each state name
41 const std::string SYS_STATE_STOPPED = "Stopped";
42 const std::string SYS_STATE_STOPPING = "Stopping";
43 const std::string SYS_STATE_STARTED = "Started";
44 const std::string SYS_STATE_RESETTING = "Resetting";
45 const std::string SYS_STATE_FRESETTING = "FactoryResetting";
46 
47 /**
48  * @brief Represents system state machine.
49  *
50  * @since 6
51  */
52 class SysStateMachine : public utility::StateMachine {
53 public:
54     // define system state machine message kinds
55     enum SysStateMessage {
56         MSG_SYS_STOP_REQ,
57         MSG_SYS_RESET_REQ,
58         MSG_SYS_FACTORY_RESET_REQ,
59         MSG_SYS_START_CMP,
60         MSG_SYS_STOP_CMP,
61         MSG_SYS_RESET_CMP,
62         MSG_SYS_FACTORY_RESET_CMP,
63         MSG_SYS_CLEAR_ALL_STORAGE_CMP,
64         MSG_SYS_ADAPTER_STATE_CHANGE_REQ,
65     };
66 
67     /**
68      * @brief A constructor used to create an <b>SysStateMachine</b> instance.
69      *
70      * @since 6
71      */
72     SysStateMachine() = default;
73 
74     /**
75      * @brief A destructor used to delete the <b>SysStateMachine</b> instance.
76      *
77      * @since 6
78      */
79     ~SysStateMachine() = default;
80 
81     /**
82      * @brief Init system state machine.
83      *
84      * @param am Adapter manager pointer.
85      * @since 6
86      */
87     void Init(AdapterManager &am);
88 };
89 
90 /**
91  * @brief Represents basic system state.
92  *
93  * @since 6
94  */
95 class SysState : public utility::StateMachine::State {
96 public:
97     /**
98      * @brief A constructor used to create an <b>SysState</b> instance.
99      *
100      * @param name The system state name.
101      * @param sysStateMachine The state machine which this state belong to.
102      * @param am Adapter manager pointer.
103      * @since 6
104      */
SysState(const std::string & name,SysStateMachine & sysStateMachine,AdapterManager & am)105     SysState(const std::string &name, SysStateMachine &sysStateMachine, AdapterManager &am)
106         : State(name, sysStateMachine), am_(am){};
107 
108     /**
109      * @brief A destructor used to delete the <b>SysState</b> instance.
110      *
111      * @since 6
112      */
113     ~SysState() = default;
114 
115 protected:
116     AdapterManager &am_;
117 };
118 
119 class SysStoppingBaseState : public SysState {
120 public:
121     /**
122      * @brief A constructor used to create an <b>SysStoppingBaseState</b> instance.
123      *
124      * @param name The system state name.
125      * @param sysStateMachine The state machine which this state belong to.
126      * @param am Adapter manager pointer.
127      * @since 6
128      */
SysStoppingBaseState(const std::string & name,SysStateMachine & sysStateMachine,AdapterManager & am)129     SysStoppingBaseState(const std::string &name, SysStateMachine &sysStateMachine, AdapterManager &am)
130         : SysState(name, sysStateMachine, am){};
131 
132     /**
133      * @brief A destructor used to delete the <b>SysStoppingBaseState</b> instance.
134      *
135      * @since 6
136      */
137     ~SysStoppingBaseState() = default;
138 
139 protected:
140     void StoppingBaseProcess(int stateBREDR, int stateBLE);
141 };
142 
143 class SysStoppedState : public SysState {
144 public:
145     /**
146      * @brief A constructor used to create an <b>SysStoppedState</b> instance.
147      *
148      * @param sysStateMachine The state machine which this state belong to.
149      * @param am Adapter manager pointer.
150      * @since 6
151      */
SysStoppedState(SysStateMachine & sysStateMachine,AdapterManager & am)152     SysStoppedState(SysStateMachine &sysStateMachine, AdapterManager &am)
153         : SysState(SYS_STATE_STOPPED, sysStateMachine, am){};
154 
155     /**
156      * @brief A destructor used to delete the <b>SysStoppedState</b> instance.
157      *
158      * @since 6
159      */
160     ~SysStoppedState() = default;
161 
162     /**
163      * @brief Entry system stop state.
164      *
165      * @since 6
166      */
167     virtual void Entry();
168 
169     /**
170      * @brief Exit system stop state.
171      *
172      * @since 6
173      */
Exit()174     virtual void Exit(){};
175 
176     /**
177      * @brief System stop state's dispatch.
178      *
179      * @param msg Message context which is used in dispath.
180      * @return Returns <b>true</b> if the operation is accepted;
181      *         returns <b>false</b> if the operation is rejected.
182      * @since 6
183      */
184     virtual bool Dispatch(const utility::Message &msg);
185 };
186 
187 class SysStartedState : public SysState {
188 public:
189     /**
190      * @brief A constructor used to create an <b>SysStartedState</b> instance.
191      *
192      * @param sysStateMachine The state machine which this state belong to.
193      * @param am Adapter manager pointer.
194      * @since 6
195      */
SysStartedState(SysStateMachine & sysStateMachine,AdapterManager & am)196     SysStartedState(SysStateMachine &sysStateMachine, AdapterManager &am)
197         : SysState(SYS_STATE_STARTED, sysStateMachine, am){};
198 
199     /**
200      * @brief A destructor used to delete the <b>SysStartedState</b> instance.
201      *
202      * @since 6
203      */
204     ~SysStartedState() = default;
205 
206     /**
207      * @brief Entry system start state.
208      *
209      * @since 6
210      */
211     virtual void Entry();
212 
213     /**
214      * @brief Exit system start state.
215      *
216      * @since 6
217      */
Exit()218     virtual void Exit(){};
219 
220     /**
221      * @brief System start state's dispatch.
222      *
223      * @param msg Message context which is used in dispath.
224      * @return Returns <b>true</b> if the operation is accepted;
225      *         returns <b>false</b> if the operation is rejected.
226      * @since 6
227      */
228     virtual bool Dispatch(const utility::Message &msg);
229 };
230 
231 class SysStoppingState : public SysStoppingBaseState {
232 public:
233     /**
234      * @brief A constructor used to create an <b>SysStoppingState</b> instance.
235      *
236      * @param sysStateMachine The state machine which this state belong to.
237      * @param am Adapter manager pointer.
238      * @since 6
239      */
SysStoppingState(SysStateMachine & sysStateMachine,AdapterManager & am)240     SysStoppingState(SysStateMachine &sysStateMachine, AdapterManager &am)
241         : SysStoppingBaseState(SYS_STATE_STOPPING, sysStateMachine, am){};
242 
243     /**
244      * @brief A destructor used to delete the <b>SysStoppingState</b> instance.
245      *
246      * @since 6
247      */
248     ~SysStoppingState() = default;
249 
250     /**
251      * @brief Entry system stopping state.
252      *
253      * @since 6
254      */
255     virtual void Entry();
256 
257     /**
258      * @brief Exit system stopping state.
259      *
260      * @since 6
261      */
262     virtual void Exit();
263 
264     /**
265      * @brief System stopping state's dispatch.
266      *
267      * @param msg Message context which is used in dispath.
268      * @return Returns <b>true</b> if the operation is accepted;
269      *         returns <b>false</b> if the operation is rejected.
270      * @since 6
271      */
272     virtual bool Dispatch(const utility::Message &msg);
273 };
274 
275 class SysResettingState : public SysStoppingBaseState {
276 public:
277     /**
278      * @brief A constructor used to create an <b>SysResettingState</b> instance.
279      *
280      * @param sysStateMachine The state machine which this state belong to.
281      * @param am Adapter manager pointer.
282      * @since 6
283      */
SysResettingState(SysStateMachine & sysStateMachine,AdapterManager & am)284     SysResettingState(SysStateMachine &sysStateMachine, AdapterManager &am)
285         : SysStoppingBaseState(SYS_STATE_RESETTING, sysStateMachine, am){};
286 
287     /**
288      * @brief A destructor used to delete the <b>SysResettingState</b> instance.
289      *
290      * @since 6
291      */
292     ~SysResettingState() = default;
293 
294     /**
295      * @brief Entry system resetting state.
296      *
297      * @since 6
298      */
299     virtual void Entry();
300 
301     /**
302      * @brief Exit system resetting state.
303      *
304      * @since 6
305      */
Exit()306     virtual void Exit(){};
307 
308     /**
309      * @brief System resetting state's dispatch.
310      *
311      * @param msg Message context which is used in dispath.
312      * @return Returns <b>true</b> if the operation is accepted;
313      *         returns <b>false</b> if the operation is rejected.
314      * @since 6
315      */
316     virtual bool Dispatch(const utility::Message &msg);
317 };
318 
319 class SysFactoryResettingState : public SysStoppingBaseState {
320 public:
321     /**
322      * @brief A constructor used to create an <b>SysFactoryResettingState</b> instance.
323      *
324      * @param sysStateMachine The state machine which this state belong to.
325      * @param am Adapter manager pointer.
326      * @since 6
327      */
SysFactoryResettingState(SysStateMachine & sysStateMachine,AdapterManager & am)328     SysFactoryResettingState(SysStateMachine &sysStateMachine, AdapterManager &am)
329         : SysStoppingBaseState(SYS_STATE_FRESETTING, sysStateMachine, am){};
330 
331     /**
332      * @brief A destructor used to delete the <b>SysFactoryResettingState</b> instance.
333      *
334      * @since 6
335      */
336     ~SysFactoryResettingState() = default;
337 
338     /**
339      * @brief Entry system factory resetting state.
340      *
341      * @since 6
342      */
343     virtual void Entry();
344 
345     /**
346      * @brief Exit system factory resetting state.
347      *
348      * @since 6
349      */
350     virtual void Exit();
351 
352     /**
353      * @brief System factory resetting state's dispatch.
354      *
355      * @param msg Message context which is used in dispath.
356      * @return Returns <b>true</b> if the operation is accepted;
357      *         returns <b>false</b> if the operation is rejected.
358      * @since 6
359      */
360     virtual bool Dispatch(const utility::Message &msg);
361 };
362 }  // namespace bluetooth
363 
364 #endif  // SYS_STATE_MACHINE_H