• 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 #ifndef ADAPTER_STATE_MACHINE_H
17 #define ADAPTER_STATE_MACHINE_H
18 
19 #include "util/dispatcher.h"
20 #include "util/state_machine.h"
21 #include "util/timer.h"
22 
23 #include "interface_adapter.h"
24 
25 namespace bluetooth {
26 // adapter state machine each state name
27 const std::string TURNING_ON_STATE = "TurningOn";
28 const std::string TURN_ON_STATE = "TurnOn";
29 const std::string TURNING_OFF_STATE = "TurningOff";
30 const std::string TURN_OFF_STATE = "TurnOff";
31 
32 /**
33  * @brief Represents adapter state machine.
34  *
35  * @since 6
36  */
37 class AdapterStateMachine : public utility::StateMachine {
38 public:
39     // define adapter state machine message kinds
40     enum AdapterStateMessage {
41         MSG_USER_ENABLE_REQ,
42         MSG_USER_DISABLE_REQ,
43         MSG_ADAPTER_ENABLE_CMP,
44         MSG_ADAPTER_DISABLE_CMP,
45         MSG_PROFILE_ENABLE_CMP,
46         MSG_PROFILE_DISABLE_CMP,
47         MSG_ADAPTER_ENABLE_TIME_OUT,
48         MSG_PROFILE_ENABLE_TIME_OUT,
49         MSG_ADAPTER_DISABLE_TIME_OUT,
50         MSG_PROFILE_DISABLE_TIME_OUT,
51     };
52 
53     /**
54      * @brief A constructor used to create an <b>AdapterStateMachine</b> instance.
55      *
56      * @param dispatch Adapter manager dispatch.
57      * @since 6
58      */
AdapterStateMachine(utility::Dispatcher & dispatch)59     explicit AdapterStateMachine(utility::Dispatcher &dispatch) : dispatch_(dispatch){};
60 
61     /**
62      * @brief A destructor used to delete the <b>AdapterStateMachine</b> instance.
63      *
64      * @since 6
65      */
66     ~AdapterStateMachine() = default;
67 
68     /**
69      * @brief A constructor used to create an <b>AdapterStateMachine</b> instance.
70      *
71      * @param adapter Adapter pointer(classic adapter or ble adapter).
72      * @since 6
73      */
74     void Init(IAdapter &adapter);
75 
76     /**
77      * @brief Get adapter state machine dispatch.
78      *
79      * @return Returns adapter state machine dispatch.
80      * @since 6
81      */
82     utility::Dispatcher &GetDispatch() const;
83 
84 private:
85     utility::Dispatcher &dispatch_;
86     AdapterStateMachine() = delete;
87 };
88 
89 /**
90  * @brief Represents basic adapter state.
91  *
92  * @since 6
93  */
94 class AdapterState : public utility::StateMachine::State {
95 public:
96     /**
97      * @brief A constructor used to create an <b>AdapterState</b> instance.
98      *
99      * @param name Name of adapter state.
100      * @param stateMachine The state machine which this state belong to.
101      * @param adapter Adapter pointer(classic adapter or ble adapter).
102      * @since 6
103      */
AdapterState(const std::string & name,AdapterStateMachine & stateMachine,IAdapter & adapter)104     AdapterState(const std::string &name, AdapterStateMachine &stateMachine, IAdapter &adapter)
105         : State(name, stateMachine), adapter_(adapter), adapterStateMachine_(stateMachine){};
106     /**
107      * @brief A destructor used to delete the <b>AdapterState</b> instance.
108      *
109      * @since 6
110      */
111     ~AdapterState() = default;
112 
113 protected:
114     IAdapter &adapter_;
115     AdapterStateMachine &adapterStateMachine_;
116 };
117 
118 class AdapterTurningOnState : public AdapterState {
119 public:
120     /**
121      * @brief A constructor used to create an <b>AdapterTurningOnState</b> instance.
122      *
123      * @param stateMachine The state machine which this state belong to.
124      * @param adapter Adapter pointer(classic adapter or ble adapter).
125      * @since 6
126      */
127     AdapterTurningOnState(AdapterStateMachine &stateMachine, IAdapter &adapter);
128 
129     /**
130      * @brief A destructor used to delete the <b>AdapterTurningOnState</b> instance.
131      *
132      * @since 6
133      */
134     ~AdapterTurningOnState() = default;
135     /**
136      * @brief Entry adapter turning on state.
137      *
138      * @since 6
139      */
140     virtual void Entry();
141 
142     /**
143      * @brief Exit adapter turning on state.
144      *
145      * @since 6
146      */
Exit()147     virtual void Exit(){};
148 
149     /**
150      * @brief Adapter turning on state's dispatch.
151      *
152      * @param msg Message context which is used in dispath.
153      * @return Returns <b>true</b> if the operation is accepted;
154      *         returns <b>false</b> if the operation is rejected.
155      * @since 6
156      */
157     virtual bool Dispatch(const utility::Message &msg);
158 
159 private:
160     // adapter turning off state timer
161     std::unique_ptr<utility::Timer> adapterTimer_ = nullptr;
162     // profile turning off state timer
163     std::unique_ptr<utility::Timer> profileTimer_ = nullptr;
164 };
165 
166 class AdapterTurnOnState : public AdapterState {
167 public:
168     /**
169      * @brief A constructor used to create an <b>AdapterTurnOnState</b> instance.
170      *
171      * @param stateMachine The state machine which this state belong to.
172      * @param adapter Adapter pointer(classic adapter or ble adapter).
173      * @since 6
174      */
AdapterTurnOnState(AdapterStateMachine & stateMachine,IAdapter & adapter)175     AdapterTurnOnState(AdapterStateMachine &stateMachine, IAdapter &adapter)
176         : AdapterState(TURN_ON_STATE, stateMachine, adapter){};
177 
178     /**
179      * @brief A destructor used to delete the <b>AdapterTurnOnState</b> instance.
180      *
181      * @since 6
182      */
183     ~AdapterTurnOnState() = default;
184     /**
185      * @brief Entry adapter turn on state.
186      *
187      * @since 6
188      */
189     virtual void Entry();
190 
191     /**
192      * @brief Exit adapter turn on state.
193      *
194      * @since 6
195      */
Exit()196     virtual void Exit(){};
197 
198     /**
199      * @brief Adapter turn on state's dispatch.
200      *
201      * @param msg Message context which is used in dispath.
202      * @return Returns <b>true</b> if the operation is accepted;
203      *         returns <b>false</b> if the operation is rejected.
204      * @since 6
205      */
206     virtual bool Dispatch(const utility::Message &msg);
207 };
208 
209 class AdapterTurningOffState : public AdapterState {
210 public:
211     /**
212      * @brief A constructor used to create an <b>AdapterTurningOffState</b> instance.
213      *
214      * @param stateMachine The state machine which this state belong to.
215      * @param adapter Adapter pointer(classic adapter or ble adapter).
216      * @since 6
217      */
218     AdapterTurningOffState(AdapterStateMachine &stateMachine, IAdapter &adapter);
219 
220     /**
221      * @brief A destructor used to delete the <b>AdapterTurningOffState</b> instance.
222      *
223      * @since 6
224      */
225     ~AdapterTurningOffState() = default;
226 
227     /**
228      * @brief Entry adapter turning off state.
229      *
230      * @since 6
231      */
232     virtual void Entry();
233 
234     /**
235      * @brief Exit adapter turning off state.
236      *
237      * @since 6
238      */
Exit()239     virtual void Exit(){};
240 
241     /**
242      * @brief Adapter turning off state's dispatch.
243      *
244      * @param msg Message context which is used in dispath.
245      * @return Returns <b>true</b> if the operation is accepted;
246      *         returns <b>false</b> if the operation is rejected.
247      * @since 6
248      */
249     virtual bool Dispatch(const utility::Message &msg);
250 
251 private:
252     // adapter turning off state timer
253     std::unique_ptr<utility::Timer> adapterTimer_ = nullptr;
254     // profile turning off state timer
255     std::unique_ptr<utility::Timer> profileTimer_ = nullptr;
256 };
257 
258 class AdapterTurnOffState : public AdapterState {
259 public:
260     /**
261      * @brief A constructor used to create an <b>AdapterTurnOffState</b> instance.
262      *
263      * @param stateMachine The state machine which this state belong to.
264      * @param adapter Adapter pointer(classic adapter or ble adapter).
265      * @since 6
266      */
AdapterTurnOffState(AdapterStateMachine & stateMachine,IAdapter & adapter)267     AdapterTurnOffState(AdapterStateMachine &stateMachine, IAdapter &adapter)
268         : AdapterState(TURN_OFF_STATE, stateMachine, adapter){};
269 
270     /**
271      * @brief A destructor used to delete the <b>AdapterTurnOffState</b> instance.
272      *
273      * @since 6
274      */
275     ~AdapterTurnOffState() = default;
276 
277     /**
278      * @brief Entry adapter turn off state.
279      *
280      * @since 6
281      */
282     virtual void Entry();
283 
284     /**
285      * @brief Exit adapter turn off state.
286      *
287      * @since 6
288      */
Exit()289     virtual void Exit(){};
290 
291     /**
292      * @brief Adapter turn off state's dispatch.
293      *
294      * @param msg Message context which is used in dispath.
295      * @return Returns <b>true</b> if the operation is accepted;
296      *         returns <b>false</b> if the operation is rejected.
297      * @since 6
298      */
299     virtual bool Dispatch(const utility::Message &msg);
300 };
301 }  // namespace bluetooth
302 
303 #endif  // ADAPTER_STATE_MACHINE_H