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 A2DP_SERVICE_STATE_MACHINE_H
17 #define A2DP_SERVICE_STATE_MACHINE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 
23 #include "a2dp_def.h"
24 #include "raw_address.h"
25 #include "state_machine.h"
26 
27 namespace OHOS {
28 namespace bluetooth {
29 const std::string A2DP_STATE_CONNECTING = "A2dpServiceConnecting";
30 const std::string A2DP_STATE_CONNECTED = "A2dpServiceConnected";
31 const std::string A2DP_STATE_DISCONNECTING = "A2dpServiceDisconnecting";
32 const std::string A2DP_STATE_DISCONNECTED = "A2dpServiceDisconnected";
33 
34 class A2dpState : public utility::StateMachine::State {
35 public:
36     /**
37      * @brief Construct a service State object.
38      * @param name State's name.
39      * @param stateMachine State is owned by which StateMachine.
40      * @since 6.0
41      */
A2dpState(const std::string & name,utility::StateMachine & stateMachine)42     A2dpState(const std::string &name, utility::StateMachine &stateMachine) : State(name, stateMachine)
43     {}
44 
45     /**
46      * @brief Destruct a Service State object.
47      * @since 6.0
48      */
49     virtual ~A2dpState() = default;
50 };
51 
52 class A2dpDisconnected : public A2dpState {
53 public:
54     /**
55      * @brief Construct a disconnected State object.
56      * @param name State's name.
57      * @param stateMachine State is owned by which StateMachine.
58      * @since 6.0
59      */
A2dpDisconnected(const std::string & name,utility::StateMachine & stateMachine)60     A2dpDisconnected(const std::string &name, utility::StateMachine &stateMachine) : A2dpState(name, stateMachine){};
61     ~A2dpDisconnected() = default;
62 
63     /**
64      * @brief Operation should be executed when Entry the state.
65      * @since 6.0
66      */
Entry()67     void Entry(){};
68 
69     /**
70      * @brief Operation should be executed when Exit the state.
71      * @since 6.0
72      */
Exit()73     void Exit(){};
74 
75     /**
76      * @brief Dispatch the message of service.
77      * @param[in] The message of service related
78      * @since 6.0
79      */
80     bool Dispatch(const utility::Message &msg);
81 };
82 
83 class A2dpDisconnecting : public A2dpState {
84 public:
85     /**
86      * @brief Construct a disconnecting State object.
87      * @param name State's name.
88      * @param stateMachine State is owned by which StateMachine.
89      * @since 6.0
90      */
A2dpDisconnecting(const std::string & name,utility::StateMachine & stateMachine)91     A2dpDisconnecting(const std::string &name, utility::StateMachine &stateMachine) : A2dpState(name, stateMachine)
92     {}
93 
94     ~A2dpDisconnecting() = default;
95 
96     /**
97      * @brief Operation should be executed when Entry the state.
98      * @since 6.0
99      */
Entry()100     void Entry(){};
101 
102     /**
103      * @brief Operation should be executed when Exit the state.
104      * @since 6.0
105      */
Exit()106     void Exit(){};
107 
108     /**
109      * @brief Dispatch the message of service.
110      * @param[in] The message of service related
111      * @since 6.0
112      */
113     bool Dispatch(const utility::Message &msg);
114 };
115 
116 class A2dpConnected : public A2dpState {
117 public:
118     /**
119      * @brief Construct a connected State object.
120      * @param name State's name.
121      * @param stateMachine State is owned by which StateMachine.
122      * @since 6.0
123      */
A2dpConnected(const std::string & name,utility::StateMachine & stateMachine)124     A2dpConnected(const std::string &name, utility::StateMachine &stateMachine) : A2dpState(name, stateMachine)
125     {}
126 
127     ~A2dpConnected() = default;
128 
129     /**
130      * @brief Operation should be executed when Entry the state.
131      * @since 6.0
132      */
Entry()133     void Entry(){};
134 
135     /**
136      * @brief Operation should be executed when Exit the state.
137      * @since 6.0
138      */
Exit()139     void Exit(){};
140 
141     /**
142      * @brief Dispatch the message of service.
143      * @param[in] The message of service related
144      * @since 6.0
145      */
146     bool Dispatch(const utility::Message &msg);
147 
148 private:
149     /**
150      * @brief Dispatch the message of service.
151      * @param[in] rawAddr The address of remote device
152      * @param[in] value The status of playing
153      * @since 6.0
154      */
155     void UpdateDeviceInformation(RawAddress rawAddr, bool value, uint8_t role);
156 };
157 
158 class A2dpConnecting : public A2dpState {
159 public:
160     /**
161      * @brief Construct a connecting State object.
162      * @param name State's name.
163      * @param stateMachine State is owned by which StateMachine.
164      * @since 6.0
165      */
A2dpConnecting(const std::string & name,utility::StateMachine & stateMachine)166     A2dpConnecting(const std::string &name, utility::StateMachine &stateMachine) : A2dpState(name, stateMachine)
167     {}
168 
169     ~A2dpConnecting() = default;
170 
171     /**
172      * @brief Operation should be executed when Entry the state.
173      * @since 6.0
174      */
Entry()175     void Entry(){};
176 
177     /**
178      * @brief Operation should be executed when Exit the state.
179      * @since 6.0
180      */
Exit()181     void Exit(){};
182 
183     /**
184      * @brief Dispatch the message of service.
185      * @param[in] The message of service related
186      * @since 6.0
187      */
188     bool Dispatch(const utility::Message &msg);
189 };
190 
191 class A2dpStateManager : public utility::StateMachine {
192 public:
193     /**
194      * @brief Construct a state machine object.
195      *
196      * @since 6.0
197      */
A2dpStateManager()198     A2dpStateManager()
199     {
200         std::unique_ptr<StateMachine::State> disconnected =
201             std::make_unique<A2dpDisconnected>(A2DP_STATE_DISCONNECTED, *this);
202         std::unique_ptr<StateMachine::State> disconnecting =
203             std::make_unique<A2dpDisconnecting>(A2DP_STATE_DISCONNECTING, *this);
204         std::unique_ptr<StateMachine::State> connected = std::make_unique<A2dpConnected>(A2DP_STATE_CONNECTED, *this);
205         std::unique_ptr<StateMachine::State> connecting =
206             std::make_unique<A2dpConnecting>(A2DP_STATE_CONNECTING, *this);
207         Move(disconnected);
208         Move(disconnecting);
209         Move(connected);
210         Move(connecting);
211         InitState(A2DP_STATE_DISCONNECTED);
212     }
213 
214     /**
215      * @brief Destruct a State machine object.
216      * @since 6.0
217      */
218     ~A2dpStateManager() = default;
219 
220     /**
221      * @brief Set the role of service.
222      * @param[in] The role of service
223      * @since 6.0
224      */
225     void SetRole(uint8_t role);
226 
227 private:
228     uint8_t role_ = A2DP_ROLE_SOURCE;
229 };
230 }  // namespace bluetooth
231 }  // namespace OHOS
232 
233 #endif  // A2DP_SERVICE_STATE_MACHINE_H
234