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  * @file bluetooth_avrcp_tg.h
18  *
19  * @brief Declares the class of the AVRCP target framework, including attributes and methods.
20  *
21  * @since 6
22  */
23 
24 #ifndef BLUETOOTH_AVRCP_TG_H
25 #define BLUETOOTH_AVRCP_TG_H
26 
27 #include <vector>
28 
29 #include "bluetooth_def.h"
30 #include "bluetooth_types.h"
31 #include "bluetooth_remote_device.h"
32 #include "bluetooth_no_destructor.h"
33 
34 namespace OHOS {
35 namespace Bluetooth {
36 /**
37  * @brief This class provides a set of methods for operating the AVRCP target.
38  *
39  * @since 6
40  */
41 class BLUETOOTH_API AvrcpTarget {
42 public:
43     /**
44      * @brief This abstract class declares a set of methods for observing the <b>AvrcpTarget::IObserver</b> class.
45      *
46      * @since 6
47      */
48     class IObserver {
49     public:
50         /**
51          * @brief A constructor used to create an <b>AvrcpController::IObserver</b> instance.
52          *
53          * @since 6
54          */
55         IObserver() = default;
56 
57         /**
58          * @brief A destructor used to delete the <b>AvrcpController::IObserver</b> instance.
59          *
60          * @since 6
61          */
62         virtual ~IObserver() = default;
63 
64         /**
65          * @brief Observes the state of the connection.
66          *
67          * @param[in] device The bluetooth device.
68          * @param[in] state  The connection state. Refer to <b>BTConnectState</b>.
69          * @param[in] cause  The connection change cause.
70          *
71          * @since 12
72          */
73         virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) = 0;
74     };
75 
76     /**
77      * @brief Gets the static instance of the <b>AvrcpTarget</b> class.
78      *
79      * @return The static instance.
80      *
81      * @since 6
82      */
83     static AvrcpTarget *GetProfile(void);
84 
85     /******************************************************************
86      * REGISTER / UNREGISTER OBSERVER                                 *
87      ******************************************************************/
88 
89     /**
90      * @brief Registers the observer.
91      *
92      * @param[in] observer The pointer to the <b>AvrcpTarget::IObserver</b>.
93      * @since 6
94      */
95     void RegisterObserver(std::shared_ptr<AvrcpTarget::IObserver> observer);
96 
97     /**
98      * @brief Unregisters the observer.
99      *
100      * @param[in] observer The pointer to the <b>AvrcpTarget::IObserver</b>.
101      * @since 6
102      */
103     void UnregisterObserver(std::shared_ptr<AvrcpTarget::IObserver> observer);
104 
105     /******************************************************************
106      * CONNECTION                                                     *
107      ******************************************************************/
108 
109     /**
110      * @brief Sets the active device.
111      *
112      * @detail Only one CT can interact witch TG.
113      *
114      * @since 6
115      */
116     void SetActiveDevice(const BluetoothRemoteDevice &device);
117 
118     /**
119      * @brief Gets the connected devices.
120      *
121      * @return The list of the instance of the <b>BluetoothRemoteDevice</b> class.
122      *
123      * @since 6
124      */
125     std::vector<BluetoothRemoteDevice> GetConnectedDevices(void);
126 
127     /**
128      * @brief Gets the devices of the specified states.
129      *
130      * @param[in] states The connection states. Refer to <b>BTConnectState</b>.
131      * @return The list of the instance of the <b>BluetoothRemoteDevice</b> class.
132      *
133      * @since 6
134      */
135     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states);
136 
137     /**
138      * @brief Gets the connection state of the specified bluetooth device.
139      *
140      * @param[in] rawAddr The address of the bluetooth device.
141      * @return The connection state. Refer to <b>BTConnectState</b>.
142      *
143      * @since 6
144      */
145     int GetDeviceState(const BluetoothRemoteDevice &device);
146 
147         /**
148      * @brief Connects to the AVRCP CT service.
149      *
150      * @param[in] device The bluetooth device.
151      * @return The result of the method execution.
152      * @retval true  command send success.
153      * @retval false command seend failed.
154      *
155      * @since 6
156      */
157     bool Connect(const BluetoothRemoteDevice &device);
158 
159     /**
160      * @brief Disconnects from the AVRCP CT service.
161      *
162      * @param[in] device The bluetooth device.
163      * @return The result of the method execution.
164      * @retval true  command send success.
165      * @retval false command seend failed.
166      *
167      * @since 6
168      */
169     bool Disconnect(const BluetoothRemoteDevice &device);
170     /******************************************************************
171      * NOTIFICATION                                                   *
172      ******************************************************************/
173 
174     /**
175      * @brief Notifies the playback status is changed.
176      *
177      * @param[in] uid         The unique ID of media item.
178      * @param[in] playbackPos Current playback position in millisecond.
179      *
180      * @since 6
181      */
182     void NotifyPlaybackStatusChanged(uint8_t playStatus, uint32_t playbackPos);
183 
184     /**
185      * @brief Notifies the track is changed.
186      *
187      * @param[in] uid         The unique ID of media item.
188      * @param[in] playbackPos Current playback position in millisecond.
189      *
190      * @since 6
191      */
192     void NotifyTrackChanged(uint64_t uid, uint32_t playbackPos);
193 
194     /**
195      * @brief Notifies the track reached end is changed.
196      *
197      * @param[in] playbackPos Current playback position in millisecond.
198      *
199      * @since 6
200      */
201     void NotifyTrackReachedEnd(uint32_t playbackPos);
202 
203     /**
204      * @brief Notifies the track reached start is changed.
205      *
206      * @param[in] playbackPos Current playback position in millisecond.
207      *
208      * @since 6
209      */
210     void NotifyTrackReachedStart(uint32_t playbackPos);
211 
212     /**
213      * @brief Notifies the player application setting is changed.
214      *
215      * @param[in] playbackPos Current playback position in millisecond.
216      *
217      * @since 6
218      */
219     void NotifyPlaybackPosChanged(uint32_t playbackPos);
220 
221     /**
222      * @brief Notifies the player application setting is changed.
223      *
224      * @since 6
225      */
226     void NotifyPlayerAppSettingChanged(const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values);
227 
228     /**
229      * @brief Notifies the addressed player is changed.
230      *
231      * @since 6
232      */
233     void NotifyNowPlayingContentChanged(void);
234 
235     /**
236      * @brief Notifies that has the available player.
237      *
238      * @since 6
239      */
240     void NotifyAvailablePlayersChanged(void);
241 
242     /**
243      * @brief Notifies the addressed player is changed.
244      *
245      * @param[in] playerId   The unique media player id.
246      * @param[in] uidCounter The UID counter shall be incremented every time the TG makes an update.
247      *
248      * @since 6
249      */
250     void NotifyAddressedPlayerChanged(uint16_t playerId, uint16_t uidCounter);
251 
252     /**
253      * @brief Notifies the uids is changed.
254      *
255      * @param[in] uidCounter The UID counter shall be incremented every time the TG makes an update.
256      *
257      * @since 6
258      */
259     void NotifyUidChanged(uint16_t uidCounter);
260 
261     /**
262      * @brief Notifies the absolute volume is changed.
263      *
264      * @param[in] volume The percentage of the absolute volume. Refer to <b>AvrcAbsoluteVolume</b>.
265      *
266      * @since 6
267      */
268     void NotifyVolumeChanged(uint8_t volume);
269 
270     /**
271      * @brief The external process calls the A2dpsrc profile interface before the Bluetooth process starts. At this
272      * time, it needs to monitor the start of the Bluetooth process, and then call this interface to initialize the
273      * A2dpsrc proflie.
274      */
275     void Init();
276 
277     /**
278      * @brief audio set device absolute volume.
279      *
280      * @param[in] device The remote device.
281      * @param[in] volumeLevel device absolute volume.
282      * @return error code
283      * @since 6
284      */
285     int32_t SetDeviceAbsoluteVolume(const BluetoothRemoteDevice &device, int32_t volumeLevel);
286 
287     /**
288      * @brief set device absolute ability.
289      *
290      * @param[in] device The remote device.
291      * @param[in] ability device absolute ability see {DeviceAbsVolumeAbility}.
292      * @return error code
293      * @since 6
294      */
295     int32_t SetDeviceAbsVolumeAbility(const BluetoothRemoteDevice &device, int32_t ability);
296 
297     /**
298      * @brief get device absolute volume ability.
299      *
300      * @param[in] device The remote device.
301      * @param[out] ability device absolute ability see {DeviceAbsVolumeAbility}.
302      * @return error code
303      * @since 6
304      */
305     int32_t GetDeviceAbsVolumeAbility(const BluetoothRemoteDevice &device, int32_t &ability);
306 
307 private:
308     /**
309      * @brief A constructor used to create an <b>AvrcpTarget</b> instance.
310      *
311      * @since 6
312      */
313     AvrcpTarget();
314 
315     /**
316      * @brief A destructor used to delete the <b>AvrcpTarget</b> instance.
317      *
318      * @since 6
319      */
320     ~AvrcpTarget();
321 
322     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AvrcpTarget);
323     BLUETOOTH_DECLARE_IMPL();
324 
325 #ifdef DTFUZZ_TEST
326     friend class BluetoothNoDestructor<AvrcpTarget>;
327 #endif
328 };
329 }  // namespace Bluetooth
330 }  // namespace OHOS
331 
332 #endif  // !BLUETOOTH_AVRCP_TG_H
333