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 The framework interface and callback function of a2dp sink are defined.
21  *
22  * @since 6
23  */
24 
25 #ifndef BLUETOOTH_A2DP_SNK_H
26 #define BLUETOOTH_A2DP_SNK_H
27 
28 #include <cstdio>
29 #include "bluetooth_types.h"
30 #include "bluetooth_remote_device.h"
31 #include "bluetooth_no_destructor.h"
32 
33 namespace OHOS {
34 namespace Bluetooth {
35 /**
36  * @brief A2dp sink API callback function.
37  *
38  * @since 6.0
39  */
40 class A2dpSinkObserver {
41 public:
42     /**
43      * @brief A destructor used to delete the a2dp sink Observer instance.
44      *
45      * @since 6.0
46      */
47     virtual ~A2dpSinkObserver() = default;
48 
49     /**
50      * @brief ConnectionState Changed observer
51      * @param device bluetooth device address
52      * @param state Connecton state
53      * @param cause Connecton cause
54      * @since 12
55      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state,int cause)56     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
57     {};
58 };
59 
60 /**
61  * @brief A2dp sink API.
62  *
63  * @since 6.0
64  */
65 class BLUETOOTH_API A2dpSink {
66 public:
67     /**
68      * @brief Get a2dp sink instance.
69      *
70      * @return Returns an instance of a2dp sink.
71      * @since 6.0
72      */
73     static A2dpSink *GetProfile();
74 
75     /**
76      * @brief Get devices by connection states.
77      *
78      * @param states The connection states of the bluetooth device.
79      * @return Returns devices that match the connection states.
80      * @since 6.0
81      */
82     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) const;
83 
84     /**
85      * @brief Get device connection state by address.
86      *
87      * @param device The address of the peer bluetooth device.
88      * @return Returns <b>A2DP_DISCONNECTED</b> if device connect state is disconnected;
89      *         Returns <b>A2DP_DISCONNECTING</b> if device connect state is disconnecting;
90      *         Returns <b>A2DP_CONNECTED</b> if device connect state is connected;
91      *         Returns <b>A2DP_CONNECTING</b> if device connect state is connecting;
92      *         Returns <b>A2DP_INVALID_STATUS</b> if can not find peer device.
93      * @since 6.0
94      */
95     int GetDeviceState(const BluetoothRemoteDevice &device) const;
96 
97     /**
98      * @brief Get device playing state by address when peer device is on connected.
99      *
100      * @param device The address of the peer bluetooth device.
101      * @return Returns <b>1</b> if device is on playing;
102      *         Returns <b>0</b> if device is not on playing.
103      * @since 6.0
104      */
105     int GetPlayingState(const BluetoothRemoteDevice &device) const;
106 
107     /**
108      * @brief Get device playing state by address when peer device is on connected.
109      *
110      * @param device The address of the peer bluetooth device.
111      * @param state The playing state of the peer bluetooth device.
112      * @return Returns <b>1</b> if device is on playing;
113      *         Returns <b>0</b> if device is not on playing.
114      * @since 6.0
115      */
116     int GetPlayingState(const BluetoothRemoteDevice &device, int &state) const;
117 
118     /**
119      * @brief Connect to the peer bluetooth device.
120      *
121      * @param device The address of the peer bluetooth device.
122      * @return Returns <b>true</b> Perform normal connection processing.
123      *         Returns <b>false</b> Target device is on connected,or connecting,
124                                     or device is not allowed to connect,or the connection fails.
125      * @since 6.0
126      */
127     bool Connect(const BluetoothRemoteDevice &device);
128 
129     /**
130      * @brief Disconnect with the peer bluetooth service.
131      *
132      * @param device The address of the peer bluetooth device.
133      * @return Returns <b>true</b> if perform normal disconnection processing.
134      *         Returns <b>false</b> if target device is on disconnected,or disconnecting,or disconnection fails.
135      * @since 6.0
136      */
137     bool Disconnect(const BluetoothRemoteDevice &device);
138 
139     /**
140      * @brief Set connection strategy for peer bluetooth device.
141      *        If peer device is connected and the policy is set not allowed,then perform disconnect operation.
142      *        If peer device is disconnected and the policy is set allowed,then perform connect operation.
143      *
144      * @param device The address of the peer bluetooth device.
145      * @param strategy The device connect strategy.
146      * @return Returns <b>true</b> if the operation is successful.
147      *         Returns <b>false</b> if the operation fails.
148      * @since 6.0
149      */
150     bool SetConnectStrategy(const BluetoothRemoteDevice &device, int strategy);
151 
152     /**
153      * @brief Get connection strategy of peer bluetooth device.
154      *
155      * @param device The address of the peer bluetooth device.
156      * @return Returns <b>A2DP_CONNECT_POLICY_AGREE</b> if the peer device is allowed to connect.
157      *         Returns <b>A2DP_CONNECT_POLICY_DISAGREE</b> if the peer device is not allowed to connect.
158      * @since 6.0
159      */
160     int GetConnectStrategy(const BluetoothRemoteDevice &device) const;
161 
162     /**
163      * @brief Send delay reporting.
164      *
165      * @param device The address of the peer bluetooth device.
166      * @param delayValue The delay value.
167      * @return Returns <b>true</b> Set delay reporting success.
168      *         Returns <b>false</b> Set delay reporting failed,
169      */
170     bool SendDelay(const BluetoothRemoteDevice &device, uint16_t delayValue);
171 
172     /**
173      * @brief Register callback function of framework.
174      *
175      * @param observer Reference to the a2dp sink observer.
176      * @since 6.0
177      */
178     void RegisterObserver(std::shared_ptr<A2dpSinkObserver> observer);
179 
180     /**
181      * @brief Deregister callback function of framework.
182      *
183      * @param observer Reference to the a2dp sink observer.
184      * @since 6.0
185      */
186     void DeregisterObserver(std::shared_ptr<A2dpSinkObserver> observer);
187 
188     /**
189      * @brief The external process calls the A2dpSink profile interface before the Bluetooth process starts. At this
190      * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
191      * A2dpSink proflie.
192      */
193     void Init();
194 
195 private:
196     /**
197      * @brief A constructor used to create a a2dp sink instance.
198      *
199      * @since 6.0
200      */
201     A2dpSink(void);
202 
203     /**
204      * @brief A destructor used to delete the a2dp sink instance.
205      *
206      * @since 6.0
207      */
208     ~A2dpSink(void);
209     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(A2dpSink);
210     BLUETOOTH_DECLARE_IMPL();
211 
212 #ifdef DTFUZZ_TEST
213     friend class BluetoothNoDestructor<A2dpSink>;
214 #endif
215 };
216 }  // namespace Bluetooth
217 }  // namespace OHOS
218 #endif  // BLUETOOTH_A2DP_SNK_H