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 OHOS {
40 namespace bluetooth {
41 // system state machine each state name
42 const std::string SYS_STATE_STOPPED = "Stopped";
43 const std::string SYS_STATE_STOPPING = "Stopping";
44 const std::string SYS_STATE_STARTED = "Started";
45 const std::string SYS_STATE_RESETTING = "Resetting";
46 const std::string SYS_STATE_FRESETTING = "FactoryResetting";
47 
48 /**
49  * @brief Represents system state machine.
50  *
51  * @since 6
52  */
53 class SysStateMachine : public utility::StateMachine {
54 public:
55     // define system state machine message kinds
56     enum SysStateMessage {
57         MSG_SYS_STOP_REQ,
58         MSG_SYS_RESET_REQ,
59         MSG_SYS_FACTORY_RESET_REQ,
60         MSG_SYS_START_CMP,
61         MSG_SYS_STOP_CMP,
62         MSG_SYS_RESET_CMP,
63         MSG_SYS_FACTORY_RESET_CMP,
64         MSG_SYS_CLEAR_ALL_STORAGE_CMP,
65         MSG_SYS_ADAPTER_STATE_CHANGE_REQ,
66     };
67 
68     /**
69      * @brief A constructor used to create an <b>SysStateMachine</b> instance.
70      *
71      * @since 6
72      */
73     SysStateMachine() = default;
74 
75     /**
76      * @brief A destructor used to delete the <b>SysStateMachine</b> instance.
77      *
78      * @since 6
79      */
80     ~SysStateMachine() = default;
81 
82     /**
83      * @brief Init system state machine.
84      *
85      * @param am Adapter manager pointer.
86      * @since 6
87      */
88     void Init(AdapterManager &am);
89 };
90 
91 /**
92  * @brief Represents basic system state.
93  *
94  * @since 6
95  */
96 class SysState : public utility::StateMachine::State {
97 public:
98     /**
99      * @brief A constructor used to create an <b>SysState</b> instance.
100      *
101      * @param name The system state name.
102      * @param sysStateMachine The state machine which this state belong to.
103      * @param am Adapter manager pointer.
104      * @since 6
105      */
SysState(const std::string & name,SysStateMachine & sysStateMachine,AdapterManager & am)106     SysState(const std::string &name, SysStateMachine &sysStateMachine, AdapterManager &am)
107         : State(name, sysStateMachine), am_(am){};
108 
109     /**
110      * @brief A destructor used to delete the <b>SysState</b> instance.
111      *
112      * @since 6
113      */
114     ~SysState() = default;
115 
116 protected:
117     AdapterManager &am_;
118 };
119 
120 class SysStoppingBaseState : public SysState {
121 public:
122     /**
123      * @brief A constructor used to create an <b>SysStoppingBaseState</b> instance.
124      *
125      * @param name The system state name.
126      * @param sysStateMachine The state machine which this state belong to.
127      * @param am Adapter manager pointer.
128      * @since 6
129      */
SysStoppingBaseState(const std::string & name,SysStateMachine & sysStateMachine,AdapterManager & am)130     SysStoppingBaseState(const std::string &name, SysStateMachine &sysStateMachine, AdapterManager &am)
131         : SysState(name, sysStateMachine, am){};
132 
133     /**
134      * @brief A destructor used to delete the <b>SysStoppingBaseState</b> instance.
135      *
136      * @since 6
137      */
138     ~SysStoppingBaseState() = default;
139 
140 protected:
141     void StoppingBaseProcess(int stateBREDR, int stateBLE);
142 };
143 
144 class SysStoppedState : public SysState {
145 public:
146     /**
147      * @brief A constructor used to create an <b>SysStoppedState</b> instance.
148      *
149      * @param sysStateMachine The state machine which this state belong to.
150      * @param am Adapter manager pointer.
151      * @since 6
152      */
SysStoppedState(SysStateMachine & sysStateMachine,AdapterManager & am)153     SysStoppedState(SysStateMachine &sysStateMachine, AdapterManager &am)
154         : SysState(SYS_STATE_STOPPED, sysStateMachine, am){};
155 
156     /**
157      * @brief A destructor used to delete the <b>SysStoppedState</b> instance.
158      *
159      * @since 6
160      */
161     ~SysStoppedState() = default;
162 
163     /**
164      * @brief Entry system stop state.
165      *
166      * @since 6
167      */
168     virtual void Entry();
169 
170     /**
171      * @brief Exit system stop state.
172      *
173      * @since 6
174      */
Exit()175     virtual void Exit(){};
176 
177     /**
178      * @brief System stop state's dispatch.
179      *
180      * @param msg Message context which is used in dispath.
181      * @return Returns <b>true</b> if the operation is accepted;
182      *         returns <b>false</b> if the operation is rejected.
183      * @since 6
184      */
185     virtual bool Dispatch(const utility::Message &msg);
186 };
187 
188 class SysStartedState : public SysState {
189 public:
190     /**
191      * @brief A constructor used to create an <b>SysStartedState</b> instance.
192      *
193      * @param sysStateMachine The state machine which this state belong to.
194      * @param am Adapter manager pointer.
195      * @since 6
196      */
SysStartedState(SysStateMachine & sysStateMachine,AdapterManager & am)197     SysStartedState(SysStateMachine &sysStateMachine, AdapterManager &am)
198         : SysState(SYS_STATE_STARTED, sysStateMachine, am){};
199 
200     /**
201      * @brief A destructor used to delete the <b>SysStartedState</b> instance.
202      *
203      * @since 6
204      */
205     ~SysStartedState() = default;
206 
207     /**
208      * @brief Entry system start state.
209      *
210      * @since 6
211      */
212     virtual void Entry();
213 
214     /**
215      * @brief Exit system start state.
216      *
217      * @since 6
218      */
Exit()219     virtual void Exit(){};
220 
221     /**
222      * @brief System start state's dispatch.
223      *
224      * @param msg Message context which is used in dispath.
225      * @return Returns <b>true</b> if the operation is accepted;
226      *         returns <b>false</b> if the operation is rejected.
227      * @since 6
228      */
229     virtual bool Dispatch(const utility::Message &msg);
230 };
231 
232 class SysStoppingState : public SysStoppingBaseState {
233 public:
234     /**
235      * @brief A constructor used to create an <b>SysStoppingState</b> instance.
236      *
237      * @param sysStateMachine The state machine which this state belong to.
238      * @param am Adapter manager pointer.
239      * @since 6
240      */
SysStoppingState(SysStateMachine & sysStateMachine,AdapterManager & am)241     SysStoppingState(SysStateMachine &sysStateMachine, AdapterManager &am)
242         : SysStoppingBaseState(SYS_STATE_STOPPING, sysStateMachine, am){};
243 
244     /**
245      * @brief A destructor used to delete the <b>SysStoppingState</b> instance.
246      *
247      * @since 6
248      */
249     ~SysStoppingState() = default;
250 
251     /**
252      * @brief Entry system stopping state.
253      *
254      * @since 6
255      */
256     virtual void Entry();
257 
258     /**
259      * @brief Exit system stopping state.
260      *
261      * @since 6
262      */
263     virtual void Exit();
264 
265     /**
266      * @brief System stopping state's dispatch.
267      *
268      * @param msg Message context which is used in dispath.
269      * @return Returns <b>true</b> if the operation is accepted;
270      *         returns <b>false</b> if the operation is rejected.
271      * @since 6
272      */
273     virtual bool Dispatch(const utility::Message &msg);
274 };
275 
276 class SysResettingState : public SysStoppingBaseState {
277 public:
278     /**
279      * @brief A constructor used to create an <b>SysResettingState</b> instance.
280      *
281      * @param sysStateMachine The state machine which this state belong to.
282      * @param am Adapter manager pointer.
283      * @since 6
284      */
SysResettingState(SysStateMachine & sysStateMachine,AdapterManager & am)285     SysResettingState(SysStateMachine &sysStateMachine, AdapterManager &am)
286         : SysStoppingBaseState(SYS_STATE_RESETTING, sysStateMachine, am){};
287 
288     /**
289      * @brief A destructor used to delete the <b>SysResettingState</b> instance.
290      *
291      * @since 6
292      */
293     ~SysResettingState() = default;
294 
295     /**
296      * @brief Entry system resetting state.
297      *
298      * @since 6
299      */
300     virtual void Entry();
301 
302     /**
303      * @brief Exit system resetting state.
304      *
305      * @since 6
306      */
Exit()307     virtual void Exit(){};
308 
309     /**
310      * @brief System resetting state's dispatch.
311      *
312      * @param msg Message context which is used in dispath.
313      * @return Returns <b>true</b> if the operation is accepted;
314      *         returns <b>false</b> if the operation is rejected.
315      * @since 6
316      */
317     virtual bool Dispatch(const utility::Message &msg);
318 };
319 
320 class SysFactoryResettingState : public SysStoppingBaseState {
321 public:
322     /**
323      * @brief A constructor used to create an <b>SysFactoryResettingState</b> instance.
324      *
325      * @param sysStateMachine The state machine which this state belong to.
326      * @param am Adapter manager pointer.
327      * @since 6
328      */
SysFactoryResettingState(SysStateMachine & sysStateMachine,AdapterManager & am)329     SysFactoryResettingState(SysStateMachine &sysStateMachine, AdapterManager &am)
330         : SysStoppingBaseState(SYS_STATE_FRESETTING, sysStateMachine, am){};
331 
332     /**
333      * @brief A destructor used to delete the <b>SysFactoryResettingState</b> instance.
334      *
335      * @since 6
336      */
337     ~SysFactoryResettingState() = default;
338 
339     /**
340      * @brief Entry system factory resetting state.
341      *
342      * @since 6
343      */
344     virtual void Entry();
345 
346     /**
347      * @brief Exit system factory resetting state.
348      *
349      * @since 6
350      */
351     virtual void Exit();
352 
353     /**
354      * @brief System factory resetting state's dispatch.
355      *
356      * @param msg Message context which is used in dispath.
357      * @return Returns <b>true</b> if the operation is accepted;
358      *         returns <b>false</b> if the operation is rejected.
359      * @since 6
360      */
361     virtual bool Dispatch(const utility::Message &msg);
362 };
363 }  // namespace bluetooth
364 }  // namespace OHOS
365 
366 #endif  // SYS_STATE_MACHINE_H