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