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 POWER_STATE_MACHINE_H
17 #define POWER_STATE_MACHINE_H
18 
19 #include "power_device.h"
20 #include "state_machine.h"
21 
22 namespace OHOS {
23 namespace bluetooth {
24 const std::string ACTIVE_STATE = "Active";
25 const std::string SNIFF_STATE = "Sniff";
26 const std::string ACTIV_ACTIVING_STATE = "A_Activing";
27 const std::string ACTIV_SNIFFING_STATE = "A_Sniffing";
28 const std::string SNIFF_SNIFFING_STATE = "S_Sniffing";
29 const std::string SNIFF_ACTIVING_STATE = "S_Activing";
30 
31 /**
32  * @brief Represents power state machine.
33  *
34  * @since 6
35  */
36 class PowerStateMachine : public utility::StateMachine {
37 public:
38     enum PowerStateMessage {
39         MSG_PM_SET_SNIFF,
40         MSG_PM_SET_ACTIVE,
41         MSG_PM_MODE_CHANGE_ACTIVE,
42         MSG_PM_MODE_CHANGE_SNIFF,
43         MSG_PM_SET_SUBRATING_COMPLETE
44     };
45     /**
46      * @brief A constructor used to create an <b>PowerStateMachine</b> instance.
47      *
48      * @since 6
49      */
PowerStateMachine()50     PowerStateMachine(){};
51 
52     /**
53      * @brief A destructor used to delete the <b>PowerStateMachine</b> instance.
54      *
55      * @since 6
56      */
57     ~PowerStateMachine() = default;
58 
59     /**
60      * @brief A constructor used to create an <b>PowerStateMachine</b> instance.
61      *
62      * @param pd power device.
63      * @since 6
64      */
65     void Init(PowerDevice &pd);
66 };
67 
68 class PowerState : public utility::StateMachine::State {
69 public:
70     /**
71      * @brief A constructor used to create an <b>PowerState</b> instance.
72      *
73      * @param pm power manager.
74      * @since 6
75      */
PowerState(const std::string & name,PowerStateMachine & psm,PowerDevice & pd)76     PowerState(const std::string &name, PowerStateMachine &psm, PowerDevice &pd) : State(name, psm), pd_(pd){};
77 
78     /**
79      * @brief A constructor used to create an <b>PowerState</b> instance.
80      *
81      * @param pm power manager.
82      * @since 6
83      */
PowerState(const std::string & name,PowerStateMachine & psm,PowerDevice & pd,utility::StateMachine::State & fstate)84     PowerState(const std::string &name, PowerStateMachine &psm, PowerDevice &pd, utility::StateMachine::State &fstate)
85         : State(name, psm, fstate), pd_(pd){};
86 
87     /**
88      * @brief A destructor used to create an <b>PowerState</b> instance.
89      *
90      * @since 6
91      */
92     ~PowerState() = default;
93 
94 protected:
95     PowerDevice &pd_;
96 };
97 
98 class PowerActiveState : public PowerState {
99 public:
100     /**
101      * @brief A constructor used to create an <b>PowerActiveState</b> instance.
102      *
103      * @param psm StateMachine.
104      * @param pd Power Device.
105      * @since 6
106      */
PowerActiveState(PowerStateMachine & psm,PowerDevice & pd)107     PowerActiveState(PowerStateMachine &psm, PowerDevice &pd) : PowerState(ACTIVE_STATE, psm, pd){};
108 
109     /**
110      * @brief A destructor used to create an <b>PowerActiveState</b> instance.
111      *
112      * @since 6
113      */
114     ~PowerActiveState() = default;
115 
116     /**
117      * @brief Operation should be executed when Entry the state.
118      *
119      * @since 6
120      */
121     virtual void Entry();
122 
123     /**
124      * @brief Operation should be executed when Exit the state.
125      *
126      * @since 6
127      */
128     virtual void Exit();
129 
130     /**
131      * @brief State dispatch message.
132      *
133      * @param msg Message.
134      * @since 6
135      */
136     virtual bool Dispatch(const utility::Message &msg);
137 };
138 
139 class PowerActiveActivingState : public PowerState {
140 public:
141     /**
142      * @brief A constructor used to create an <b>PowerActiveActivingState</b> instance.
143      *
144      * @param psm StateMachine.
145      * @param pd Power Device.
146      * @param fstate Father State.
147      * @since 6
148      */
PowerActiveActivingState(PowerStateMachine & psm,PowerDevice & pd,utility::StateMachine::State & fstate)149     PowerActiveActivingState(PowerStateMachine &psm, PowerDevice &pd, utility::StateMachine::State &fstate)
150         : PowerState(ACTIV_ACTIVING_STATE, psm, pd, fstate){};
151 
152     /**
153      * @brief A destructor used to create an <b>PowerActiveActivingState</b> instance.
154      *
155      * @since 6
156      */
157     ~PowerActiveActivingState() = default;
158     virtual void Entry();
159     virtual void Exit();
160     virtual bool Dispatch(const utility::Message &msg);
161 };
162 
163 class PowerActiveSniffingState : public PowerState {
164 public:
165     /**
166      * @brief A constructor used to create an <b>PowerActiveSniffingState</b> instance.
167      *
168      * @param psm StateMachine.
169      * @param pd Power Device.
170      * @param fstate Father State.
171      * @since 6
172      */
PowerActiveSniffingState(PowerStateMachine & psm,PowerDevice & pd,utility::StateMachine::State & fstate)173     PowerActiveSniffingState(PowerStateMachine &psm, PowerDevice &pd, utility::StateMachine::State &fstate)
174         : PowerState(ACTIV_SNIFFING_STATE, psm, pd, fstate){};
175 
176     /**
177      * @brief A destructor used to create an <b>PowerActiveSniffingState</b> instance.
178      *
179      * @since 6
180      */
181     ~PowerActiveSniffingState() = default;
182     virtual void Entry();
183     virtual void Exit();
184     virtual bool Dispatch(const utility::Message &msg);
185 };
186 
187 class PowerSniffState : public PowerState {
188 public:
189     /**
190      * @brief A constructor used to create an <b>PowerSniffState</b> instance.
191      *
192      * @param psm StateMachine.
193      * @param pd Power Device.
194      * @since 6
195      */
PowerSniffState(PowerStateMachine & psm,PowerDevice & pd)196     PowerSniffState(PowerStateMachine &psm, PowerDevice &pd) : PowerState(SNIFF_STATE, psm, pd){};
197 
198     /**
199      * @brief A destructor used to create an <b>PowerSniffState</b> instance.
200      *
201      * @since 6
202      */
203     ~PowerSniffState() = default;
204     virtual void Entry();
205     virtual void Exit();
206     virtual bool Dispatch(const utility::Message &msg);
207 };
208 
209 class PowerSniffActivingState : public PowerState {
210 public:
211     /**
212      * @brief A constructor used to create an <b>PowerSniffActivingState</b> instance.
213      *
214      * @param psm StateMachine.
215      * @param pd Power Device.
216      * @param fstate Father State.
217      * @since 6
218      */
PowerSniffActivingState(PowerStateMachine & psm,PowerDevice & pd,utility::StateMachine::State & fstate)219     PowerSniffActivingState(PowerStateMachine &psm, PowerDevice &pd, utility::StateMachine::State &fstate)
220         : PowerState(SNIFF_ACTIVING_STATE, psm, pd, fstate){};
221 
222     /**
223      * @brief A destructor used to create an <b>PowerSniffActivingState</b> instance.
224      *
225      * @since 6
226      */
227     ~PowerSniffActivingState() = default;
228     virtual void Entry();
229     virtual void Exit();
230     virtual bool Dispatch(const utility::Message &msg);
231 };
232 
233 class PowerSniffSniffingState : public PowerState {
234 public:
235     /**
236      * @brief A constructor used to create an <b>PowerSniffSniffingState</b> instance.
237      *
238      * @param psm StateMachine.
239      * @param pd Power Device.
240      * @param fstate Father State.
241      * @since 6
242      */
PowerSniffSniffingState(PowerStateMachine & psm,PowerDevice & pd,utility::StateMachine::State & fstate)243     PowerSniffSniffingState(PowerStateMachine &psm, PowerDevice &pd, utility::StateMachine::State &fstate)
244         : PowerState(SNIFF_SNIFFING_STATE, psm, pd, fstate){};
245 
246     /**
247      * @brief A destructor used to create an <b>PowerSniffSniffingState</b> instance.
248      *
249      * @since 6
250      */
251     ~PowerSniffSniffingState() = default;
252     virtual void Entry();
253     virtual void Exit();
254     virtual bool Dispatch(const utility::Message &msg);
255 };
256 }  // namespace bluetooth
257 }  // namespace OHOS
258 
259 #endif  // ADAPTER_STATE_MACHINE_H