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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_avrcp_tg"
17 #endif
18 
19 #include <deque>
20 #include <list>
21 #include <mutex>
22 
23 #include "bluetooth_avrcp_ct.h"
24 #include "bluetooth_avrcp_ct_observer_stub.h"
25 #include "bluetooth_def.h"
26 #include "bluetooth_host.h"
27 #include "bluetooth_profile_manager.h"
28 #include "bluetooth_log.h"
29 #include "bluetooth_utils.h"
30 #include "bluetooth_observer_list.h"
31 #include "i_bluetooth_avrcp_ct.h"
32 #include "iservice_registry.h"
33 #include "system_ability_definition.h"
34 
35 namespace OHOS {
36 namespace Bluetooth {
37 using namespace OHOS::bluetooth;
38 std::mutex g_avrcpProxyMutex;
AvrcpCtResponse(uint8_t type,int resp)39 AvrcpCtResponse::AvrcpCtResponse(uint8_t type, int resp) : type_(type), resp_(resp)
40 {
41     HILOGI("enter");
42 }
43 
~AvrcpCtResponse()44 AvrcpCtResponse::~AvrcpCtResponse()
45 {
46     HILOGI("enter");
47 }
48 
49 struct AvrcpController::impl {
50 public:
51     class ObserverImpl : public BluetoothAvrcpCtObserverStub {
52     public:
ObserverImpl(AvrcpController::impl * impl)53         explicit ObserverImpl(AvrcpController::impl *impl) : impl_(impl)
54         {}
55         ~ObserverImpl() override = default;
56 
OnConnectionStateChanged(const RawAddress & rawAddr,int state,int cause)57         void OnConnectionStateChanged(const RawAddress &rawAddr, int state, int cause) override
58         {
59             HILOGD("enter, address: %{public}s, state: %{public}d, cause: %{public}d",
60                 GET_ENCRYPT_RAW_ADDR(rawAddr), state, cause);
61 
62             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
63             impl_->OnConnectionStateChanged(device, static_cast<int>(state), cause);
64 
65             return;
66         }
67 
OnPressButton(const RawAddress & rawAddr,uint8_t button,int result)68         void OnPressButton(const RawAddress &rawAddr, uint8_t button, int result) override
69         {
70             HILOGI("enter, address: %{public}s, button: %{public}d, res: %{public}d",
71                 GET_ENCRYPT_RAW_ADDR(rawAddr), button, result);
72 
73             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
74             impl_->OnPressButton(device, static_cast<uint8_t>(button), static_cast<int>(result));
75 
76             return;
77         }
78 
OnReleaseButton(const RawAddress & rawAddr,uint8_t button,int result)79         void OnReleaseButton(const RawAddress &rawAddr, uint8_t button,  int result) override
80         {
81             HILOGI("enter, address: %{public}s, button: %{public}d, res: %{public}d",
82                 GET_ENCRYPT_RAW_ADDR(rawAddr), button, result);
83 
84             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
85             impl_->OnReleaseButton(device, static_cast<uint8_t>(button), static_cast<int>(result));
86 
87             return;
88         }
89 
OnSetBrowsedPlayer(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numberOfItems,const std::vector<std::string> & folderNames,int result,int detail)90         void OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter,
91             uint32_t numberOfItems, const std::vector<std::string> &folderNames, int result, int detail) override
92         {
93             HILOGI("enter, address: %{public}s, res: %{public}d, detail: %{public}d",
94                 GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
95 
96             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
97 
98             impl_->OnSetBrowsedPlayer(device,
99                 static_cast<uint16_t>(uidCounter),
100                 static_cast<uint32_t>(numberOfItems),
101                 folderNames,
102                 result,
103                 detail);
104             return;
105         }
106 
OnGetCapabilities(const RawAddress & rawAddr,const std::vector<uint32_t> & companies,const std::vector<uint8_t> & events,int result)107         void OnGetCapabilities(const RawAddress &rawAddr, const std::vector<uint32_t> &companies,
108             const std::vector<uint8_t> &events, int result) override
109         {
110             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
111 
112             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
113             impl_->OnGetCapabilities(device, companies, events, result);
114 
115             return;
116         }
117 
OnGetPlayerAppSettingAttributes(const RawAddress & rawAddr,std::vector<uint8_t> attributes,int result)118         void OnGetPlayerAppSettingAttributes(
119             const RawAddress &rawAddr, std::vector<uint8_t> attributes, int result) override
120         {
121             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
122 
123             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
124 
125             impl_->OnGetPlayerAppSettingAttributes(device, attributes, static_cast<int>(result));
126 
127             return;
128         }
129 
OnGetPlayerAppSettingValues(const RawAddress & rawAddr,int attribute,const std::vector<uint8_t> & values,int result)130         void OnGetPlayerAppSettingValues(const RawAddress &rawAddr,
131             int attribute, const std::vector<uint8_t> &values, int result) override
132         {
133             HILOGI("enter, address: %{public}s, attribute: %{public}d, res: %{public}d",
134                 GET_ENCRYPT_RAW_ADDR(rawAddr), attribute, result);
135 
136             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
137 
138             impl_->OnGetPlayerAppSettingValues(
139                 device, static_cast<uint8_t>(attribute), values, static_cast<int>(result));
140 
141             return;
142         }
143 
OnGetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::vector<uint8_t> attributes,const std::vector<uint8_t> & values,int result)144         void OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::vector<uint8_t> attributes,
145             const std::vector<uint8_t> &values, int result) override
146         {
147             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
148 
149             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
150 
151             impl_->OnGetPlayerAppSettingCurrentValue(device, attributes, values, static_cast<int>(result));
152 
153             return;
154         }
155 
OnSetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,int result)156         void OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, int result) override
157         {
158             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
159 
160             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
161 
162             impl_->OnSetPlayerAppSettingCurrentValue(device, static_cast<int>(result));
163 
164             return;
165         }
166 
OnGetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> attribtues,const std::vector<std::string> & attributeName,int result)167         void OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr,
168             const std::vector<uint8_t> attribtues, const std::vector<std::string> &attributeName, int result) override
169         {
170             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
171 
172             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
173 
174             impl_->OnGetPlayerAppSettingAttributeText(device, attribtues, attributeName, static_cast<int>(result));
175 
176             return;
177         }
178 
OnGetPlayerAppSettingValueText(const RawAddress & rawAddr,const std::vector<uint8_t> & values,const std::vector<std::string> & valueName,int result)179         void OnGetPlayerAppSettingValueText(const RawAddress &rawAddr,
180             const std::vector<uint8_t> &values, const std::vector<std::string> &valueName, int result) override
181         {
182             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
183 
184             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
185 
186             impl_->OnGetPlayerAppSettingValueText(device, values, valueName, static_cast<int>(result));
187 
188             return;
189         }
190 
OnGetElementAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attribtues,const std::vector<std::string> & valueName,int result)191         void OnGetElementAttributes(const RawAddress &rawAddr,
192         const std::vector<uint32_t> &attribtues, const std::vector<std::string> &valueName, int result) override
193         {
194             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
195 
196             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
197 
198             impl_->OnGetElementAttributes(device, attribtues, valueName, static_cast<int>(result));
199 
200             return;
201         }
202 
OnGetPlayStatus(const RawAddress & rawAddr,uint32_t songLength,uint32_t songPosition,uint8_t playStatus,int result)203         void OnGetPlayStatus(const RawAddress &rawAddr,
204             uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result) override
205         {
206             HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d",
207                 GET_ENCRYPT_RAW_ADDR(rawAddr), playStatus, result);
208 
209             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
210             impl_->OnGetPlayStatus(device,
211                 static_cast<uint32_t>(songLength),
212                 static_cast<uint32_t>(songPosition),
213                 static_cast<uint8_t>(playStatus),
214                 static_cast<int>(result));
215 
216             return;
217         }
218 
OnPlayItem(const RawAddress & rawAddr,int status,int result)219         void OnPlayItem(const RawAddress &rawAddr, int status, int result) override
220         {
221             HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d",
222                 GET_ENCRYPT_RAW_ADDR(rawAddr), status, result);
223 
224             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
225             impl_->OnPlayItem(device, static_cast<int>(status), static_cast<int>(result));
226 
227             return;
228         }
229 
OnGetMediaPlayers(const RawAddress & rawAddr,uint16_t uidCounter,std::vector<BluetoothAvrcpMpItem> & items,int result,int detail)230         void OnGetMediaPlayers(const RawAddress &rawAddr, uint16_t uidCounter,
231             std::vector<BluetoothAvrcpMpItem> &items, int result, int detail) override
232         {
233             HILOGI("enter, address: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d",
234                 GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result, detail);
235 
236             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
237             std::vector<AvrcMpItem> myItems;
238             for (size_t i = 0; i < items.size(); i++) {
239                 AvrcMpItem myItem;
240                 myItem.itemType_ = items.at(i).itemType_;
241                 myItem.playerId_ = items.at(i).playerId_;
242                 myItem.majorType_ = items.at(i).majorType_;
243                 myItem.subType_ = items.at(i).subType_;
244                 myItem.playStatus_ = items.at(i).playStatus_;
245                 myItem.features_ = items.at(i).features_;
246                 myItem.name_ = items.at(i).name_;
247                 myItems.push_back(myItem);
248             }
249 
250             impl_->OnGetMediaPlayers(
251                 device, static_cast<uint32_t>(uidCounter), myItems, static_cast<int>(result), static_cast<int>(detail));
252 
253             return;
254         }
255 
OnGetFolderItems(const RawAddress & rawAddr,uint16_t uidCounter,std::vector<BluetoothAvrcpMeItem> & items,int result,int detail)256         void OnGetFolderItems(const RawAddress &rawAddr, uint16_t uidCounter, std::vector<BluetoothAvrcpMeItem> &items,
257             int result, int detail) override
258         {
259             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
260             std::vector<AvrcMeItem> myItems;
261             for (size_t i = 0; i < items.size(); i++) {
262                 AvrcMeItem myItem;
263                 myItem.uid_ = items.at(i).uid_;
264                 myItem.type_ = items.at(i).type_;
265                 myItem.playable_ = items.at(i).playable_;
266                 myItem.name_ = items.at(i).name_;
267                 myItem.attributes_ = items.at(i).attributes_;
268                 myItem.values_ = items.at(i).values_;
269 
270                 myItems.push_back(myItem);
271             }
272             impl_->OnGetFolderItems(
273                 device, static_cast<uint32_t>(uidCounter), myItems, static_cast<int>(result), static_cast<int>(detail));
274 
275             return;
276         }
277 
OnGetItemAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attribtues,const std::vector<std::string> & values,int result,int detail)278         void OnGetItemAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attribtues,
279             const std::vector<std::string> &values, int result, int detail) override
280         {
281             HILOGI("enter, address: %{public}s, res: %{public}d, detail: %{public}d",
282                 GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
283 
284             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
285 
286             impl_->OnGetItemAttributes(device, attribtues, values, static_cast<int>(result), static_cast<int>(detail));
287 
288             return;
289         }
290 
OnGetTotalNumberOfItems(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,int result,int detail)291         void OnGetTotalNumberOfItems(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems,
292             int result, int detail) override
293         {
294             HILOGI("address: %{public}s, uidCounter: %{public}d, numOfItems: %{public}d, res: %{public}d, "
295                 "detail: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, numOfItems, result, detail);
296 
297             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
298             impl_->OnGetTotalNumberOfItems(device,
299                 static_cast<uint32_t>(uidCounter),
300                 static_cast<uint32_t>(numOfItems),
301                 static_cast<uint8_t>(result),
302                 static_cast<int>(detail));
303 
304             return;
305         }
306 
OnSetAbsoluteVolume(const RawAddress & rawAddr,uint8_t volume,int result)307         void OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, int result) override
308         {
309             HILOGI("enter, address: %{public}s, volume: %{public}d, res: %{public}d",
310                 GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result);
311 
312             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
313             impl_->OnSetAbsoluteVolume(device, static_cast<uint8_t>(volume), result);
314 
315             return;
316         }
317 
OnPlaybackStatusChanged(const RawAddress & rawAddr,uint8_t playStatus,int result)318         void OnPlaybackStatusChanged(const RawAddress &rawAddr, uint8_t playStatus, int result) override
319         {
320             HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d",
321                 GET_ENCRYPT_RAW_ADDR(rawAddr), playStatus, result);
322 
323             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
324             impl_->OnPlaybackStatusChanged(device, static_cast<uint8_t>(playStatus), static_cast<int>(result));
325 
326             return;
327         }
328 
OnTrackChanged(const RawAddress & rawAddr,uint64_t uid,int result)329         void OnTrackChanged(const RawAddress &rawAddr, uint64_t uid, int result) override
330         {
331             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
332 
333             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
334             impl_->OnTrackChanged(device, static_cast<uint64_t>(uid), static_cast<int>(result));
335 
336             return;
337         }
338 
OnTrackReachedEnd(const RawAddress & rawAddr,int result)339         void OnTrackReachedEnd(const RawAddress &rawAddr, int result) override
340         {
341             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
342 
343             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
344             impl_->OnTrackReachedEnd(device, static_cast<int>(result));
345 
346             return;
347         }
348 
OnTrackReachedStart(const RawAddress & rawAddr,int result)349         void OnTrackReachedStart(const RawAddress &rawAddr, int result) override
350         {
351             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
352 
353             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
354             impl_->OnTrackReachedStart(device, static_cast<int>(result));
355 
356             return;
357         }
358 
OnPlaybackPosChanged(const RawAddress & rawAddr,uint32_t playbackPos,int result)359         void OnPlaybackPosChanged(const RawAddress &rawAddr, uint32_t playbackPos, int result) override
360         {
361             HILOGI("enter, address: %{public}s, playbackPos: %{public}d, res: %{public}d",
362                 GET_ENCRYPT_RAW_ADDR(rawAddr), playbackPos, result);
363 
364             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
365             impl_->OnPlaybackPosChanged(device, static_cast<uint32_t>(playbackPos), static_cast<int>(result));
366 
367             return;
368         }
369 
OnPlayerAppSettingChanged(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values,int result)370         void OnPlayerAppSettingChanged(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
371             const std::vector<uint8_t> &values, int result) override
372         {
373             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
374 
375             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
376             impl_->OnPlayerAppSettingChanged(device, attributes, values, static_cast<int>(result));
377 
378             return;
379         }
380 
OnNowPlayingContentChanged(const RawAddress & rawAddr,int result)381         void OnNowPlayingContentChanged(const RawAddress &rawAddr, int result) override
382         {
383             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
384 
385             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
386             impl_->OnNowPlayingContentChanged(device, static_cast<int>(result));
387 
388             return;
389         }
390 
OnAvailablePlayersChanged(const RawAddress & rawAddr,int result)391         void OnAvailablePlayersChanged(const RawAddress &rawAddr, int result) override
392         {
393             HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
394 
395             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
396             impl_->OnAvailablePlayersChanged(device, static_cast<int>(result));
397 
398             return;
399         }
400 
OnAddressedPlayerChanged(const RawAddress & rawAddr,uint16_t playerId,uint16_t uidCounter,int result)401         void OnAddressedPlayerChanged(
402             const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, int result) override
403         {
404             HILOGI("enter, address: %{public}s, playerId: %{public}d, uidCounter: %{public}d, res: %{public}d",
405                 GET_ENCRYPT_RAW_ADDR(rawAddr), playerId, uidCounter, result);
406 
407             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
408             impl_->OnAddressedPlayerChanged(
409                 device, static_cast<uint16_t>(playerId), static_cast<uint16_t>(uidCounter), static_cast<int>(result));
410 
411             return;
412         }
413 
OnUidChanged(const RawAddress & rawAddr,uint16_t uidCounter,int result)414         void OnUidChanged(const RawAddress &rawAddr, uint16_t uidCounter, int result) override
415         {
416             HILOGI("enter, address: %{public}s, uidCounter: %{public}d, res: %{public}d",
417                 GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result);
418 
419             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
420             impl_->OnUidChanged(device, static_cast<uint16_t>(uidCounter), static_cast<int>(result));
421 
422             return;
423         }
424 
OnVolumeChanged(const RawAddress & rawAddr,uint8_t volume,int result)425         void OnVolumeChanged(const RawAddress &rawAddr, uint8_t volume, int result) override
426         {
427             HILOGI("enter, address: %{public}s, volume: %{public}d, res: %{public}d",
428                 GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result);
429 
430             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
431             impl_->OnVolumeChanged(device, static_cast<uint8_t>(volume), static_cast<int>(result));
432 
433             return;
434         }
435 
436     private:
437         AvrcpController::impl *impl_;
438     };
439 
440     impl();
~implOHOS::Bluetooth::AvrcpController::impl441     ~impl()
442     {
443         HILOGI("enter");
444         BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
445         sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
446         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
447         proxy->UnregisterObserver(observer_);
448     }
449 
OnConnectionStateChangedOHOS::Bluetooth::AvrcpController::impl450     void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
451     {
452         HILOGI("enter, device: %{public}s, state: %{public}d, cause: %{public}d",
453             GET_ENCRYPT_ADDR(device), state, cause);
454 
455         std::lock_guard<std::mutex> lock(observerMutex_);
456 
457         observers_.ForEach([device, state, cause](std::shared_ptr<IObserver> observer) {
458             observer->OnConnectionStateChanged(device, state, cause);
459         });
460     }
461 
OnPressButtonOHOS::Bluetooth::AvrcpController::impl462     void OnPressButton(const BluetoothRemoteDevice &device, uint8_t button, int result)
463     {
464         HILOGI("enter, device: %{public}s, button: %{public}d, res: %{public}d",
465             GET_ENCRYPT_ADDR(device), button, result);
466 
467         std::lock_guard<std::mutex> lock(observerMutex_);
468 
469         observers_.ForEach([device, button, result](std::shared_ptr<IObserver> observer) {
470             AvrcpCtResponse resp(AVRC_ACTION_TYPE_PRESS_BUTTON, result);
471             resp.button_ = std::make_unique<AvrcpCtResponse::Button>(button);
472             observer->OnActionCompleted(device, resp);
473         });
474     }
475 
OnReleaseButtonOHOS::Bluetooth::AvrcpController::impl476     void OnReleaseButton(const BluetoothRemoteDevice &device, uint8_t button, int result)
477     {
478         HILOGI("enter, device: %{public}s, button: %{public}d, res: %{public}d",
479             GET_ENCRYPT_ADDR(device), button, result);
480 
481         std::lock_guard<std::mutex> lock(observerMutex_);
482 
483         observers_.ForEach([device, button, result](std::shared_ptr<IObserver> observer) {
484             AvrcpCtResponse resp(AVRC_ACTION_TYPE_RELEASE_BUTTON, result);
485             resp.button_ = std::make_unique<AvrcpCtResponse::Button>(button);
486             observer->OnActionCompleted(device, resp);
487         });
488     }
489 
OnSetBrowsedPlayerOHOS::Bluetooth::AvrcpController::impl490     void OnSetBrowsedPlayer(const BluetoothRemoteDevice &device, uint16_t uidCounter, uint32_t numOfItems,
491         const std::vector<std::string> &folderNames, int result, int detail)
492     {
493         HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d",
494             GET_ENCRYPT_ADDR(device), result, detail);
495 
496         std::lock_guard<std::mutex> lock(observerMutex_);
497 
498         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
499             AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_BROWSED_PLAYER, result);
500             observer->OnActionCompleted(device, resp);
501         });
502     }
503 
OnGetCapabilitiesOHOS::Bluetooth::AvrcpController::impl504     void OnGetCapabilities(const BluetoothRemoteDevice &device, const std::vector<uint32_t> &companies,
505         const std::vector<uint8_t> &events, int result)
506     {
507         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
508 
509         std::lock_guard<std::mutex> lock(observerMutex_);
510 
511         observers_.ForEach([device, companies, events, result](std::shared_ptr<IObserver> observer) {
512             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_CAPABILITIES, result);
513             if (companies.size() == 0) {
514                 resp.capabilities_ = std::make_unique<AvrcpCtResponse::Capabilities>(events);
515             } else {
516                 resp.capabilities_ = std::make_unique<AvrcpCtResponse::Capabilities>(companies);
517             }
518 
519             observer->OnActionCompleted(device, resp);
520         });
521     }
522 
OnGetPlayerAppSettingAttributesOHOS::Bluetooth::AvrcpController::impl523     void OnGetPlayerAppSettingAttributes(
524         const BluetoothRemoteDevice &device, std::vector<uint8_t> attributes, int result)
525     {
526         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
527 
528         std::lock_guard<std::mutex> lock(observerMutex_);
529 
530         observers_.ForEach([device, attributes, result](std::shared_ptr<IObserver> observer) {
531             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_ATTRIBUTES, result);
532             resp.playerAttrs_ = std::make_unique<AvrcpCtResponse::PlayerSettingAttributes>(attributes);
533             observer->OnActionCompleted(device, resp);
534         });
535     }
536 
OnGetPlayerAppSettingValuesOHOS::Bluetooth::AvrcpController::impl537     void OnGetPlayerAppSettingValues(
538         const BluetoothRemoteDevice &device, uint8_t attribute, std::vector<uint8_t> values, int result)
539     {
540         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
541 
542         std::lock_guard<std::mutex> lock(observerMutex_);
543 
544         observers_.ForEach([device, attribute, values, result](std::shared_ptr<IObserver> observer) {
545             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_VALUES, result);
546             resp.playerVals_ = std::make_unique<AvrcpCtResponse::PlayerSettingValues>(attribute, values);
547             observer->OnActionCompleted(device, resp);
548         });
549     }
550 
OnGetPlayerAppSettingCurrentValueOHOS::Bluetooth::AvrcpController::impl551     void OnGetPlayerAppSettingCurrentValue(
552         const BluetoothRemoteDevice &device, std::vector<uint8_t> attributes, std::vector<uint8_t> values, int result)
553     {
554         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
555 
556         std::lock_guard<std::mutex> lock(observerMutex_);
557 
558         observers_.ForEach([device, attributes, values, result](std::shared_ptr<IObserver> observer) {
559             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_CURRENT_VALUE, result);
560             resp.playerCurVal_ = std::make_unique<AvrcpCtResponse::PlayerSettingCurrentValue>(attributes, values);
561             observer->OnActionCompleted(device, resp);
562         });
563     }
564 
OnSetPlayerAppSettingCurrentValueOHOS::Bluetooth::AvrcpController::impl565     void OnSetPlayerAppSettingCurrentValue(const BluetoothRemoteDevice &device, int result)
566     {
567         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
568 
569         std::lock_guard<std::mutex> lock(observerMutex_);
570 
571         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
572             AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_PLAYER_APP_SETTING_CURRENT_VALUE, result);
573             observer->OnActionCompleted(device, resp);
574         });
575     }
576 
OnGetPlayerAppSettingAttributeTextOHOS::Bluetooth::AvrcpController::impl577     void OnGetPlayerAppSettingAttributeText(const BluetoothRemoteDevice &device, const std::vector<uint8_t> &attributes,
578         const std::vector<std::string> &valueName, int result)
579     {
580         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
581 
582         std::lock_guard<std::mutex> lock(observerMutex_);
583 
584         observers_.ForEach([device, attributes, valueName, result](std::shared_ptr<IObserver> observer) {
585             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_ATTRIBUTE_TEXT, result);
586             resp.playerText_ = std::make_unique<AvrcpCtResponse::PlayerGettingText>(attributes, valueName);
587             observer->OnActionCompleted(device, resp);
588         });
589     }
590 
OnGetPlayerAppSettingValueTextOHOS::Bluetooth::AvrcpController::impl591     void OnGetPlayerAppSettingValueText(const BluetoothRemoteDevice &device, const std::vector<uint8_t> &values,
592         const std::vector<std::string> &valueName, int result)
593     {
594         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
595 
596         std::lock_guard<std::mutex> lock(observerMutex_);
597 
598         observers_.ForEach([device, result, values, valueName](std::shared_ptr<IObserver> observer) {
599             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_VALUE_TEXT, result);
600             resp.playerText_ = std::make_unique<AvrcpCtResponse::PlayerGettingText>(values, valueName);
601             observer->OnActionCompleted(device, resp);
602         });
603     }
604 
OnGetElementAttributesOHOS::Bluetooth::AvrcpController::impl605     void OnGetElementAttributes(const BluetoothRemoteDevice &device, const std::vector<uint32_t> &attributes,
606         const std::vector<std::string> &valueName, int result)
607     {
608         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
609 
610         std::lock_guard<std::mutex> lock(observerMutex_);
611 
612         observers_.ForEach([device, result, attributes, valueName](std::shared_ptr<IObserver> observer) {
613             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_ELEMENT_ATTRIBUTES, result);
614             resp.eleSts_ = std::make_unique<AvrcpCtResponse::ElementAttributes>(attributes, valueName);
615             observer->OnActionCompleted(device, resp);
616         });
617     }
618 
OnGetPlayStatusOHOS::Bluetooth::AvrcpController::impl619     void OnGetPlayStatus(
620         const BluetoothRemoteDevice &device, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result)
621     {
622         HILOGI("enter, device: %{public}s, songLength: %{public}d, songPosition: %{public}d, playStatus: %{public}d,"
623             " res: %{public}d", GET_ENCRYPT_ADDR(device), songLength, songPosition, playStatus, result);
624 
625         std::lock_guard<std::mutex> lock(observerMutex_);
626 
627         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
628             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAY_STATUS, result);
629             observer->OnActionCompleted(device, resp);
630         });
631     }
632 
OnPlayItemOHOS::Bluetooth::AvrcpController::impl633     void OnPlayItem(const BluetoothRemoteDevice &device, int result, int detail)
634     {
635         HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d",
636             GET_ENCRYPT_ADDR(device), result, detail);
637 
638         std::lock_guard<std::mutex> lock(observerMutex_);
639 
640         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
641             AvrcpCtResponse resp(AVRC_ACTION_TYPE_PLAY_ITEM, result);
642             observer->OnActionCompleted(device, resp);
643         });
644     }
645 
OnGetMediaPlayersOHOS::Bluetooth::AvrcpController::impl646     void OnGetMediaPlayers(const BluetoothRemoteDevice &device, uint16_t uidCounter,
647         const std::vector<AvrcMpItem> &items, int result, int detail)
648     {
649         HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d",
650             GET_ENCRYPT_ADDR(device), uidCounter, result, detail);
651 
652         std::vector<AvrcpCtResponse::MediaPlayers::MediaPlayer> MediaPlayers;
653         for (int i = 0; i < static_cast<int>(items.size()); i++) {
654             AvrcpCtResponse::MediaPlayers::MediaPlayer mediaPlayer;
655             mediaPlayer.itemType_ = items.at(i).itemType_;
656             mediaPlayer.playerId_ = items.at(i).playerId_;
657             mediaPlayer.majorType_ = items.at(i).majorType_;
658             mediaPlayer.subType_ = items.at(i).subType_;
659             mediaPlayer.playStatus_ = items.at(i).playStatus_;
660             mediaPlayer.features_ = items.at(i).features_;
661             mediaPlayer.name_ = items.at(i).name_;
662 
663             MediaPlayers.push_back(mediaPlayer);
664         }
665 
666         std::lock_guard<std::mutex> lock(observerMutex_);
667         observers_.ForEach([device, result, MediaPlayers, uidCounter](std::shared_ptr<IObserver> observer) {
668             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_MEDIA_PLAYER_LIST, result);
669             resp.mediaPlayers_ = std::make_unique<AvrcpCtResponse::MediaPlayers>(uidCounter, MediaPlayers);
670             observer->OnActionCompleted(device, resp);
671         });
672     }
673 
OnGetFolderItemsOHOS::Bluetooth::AvrcpController::impl674     void OnGetFolderItems(const BluetoothRemoteDevice &device, uint16_t uidCounter,
675         const std::vector<AvrcMeItem> &items, int result, int detail)
676     {
677         HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d",
678             GET_ENCRYPT_ADDR(device), uidCounter, result, detail);
679 
680         std::vector<AvrcpCtResponse::MediaItems::MediaItem> mediaItems;
681         for (int i = 0; i < static_cast<int>(items.size()); i++) {
682             AvrcpCtResponse::MediaItems::MediaItem mediaItem;
683             mediaItem.uid_ = items.at(i).uid_;
684             mediaItem.type_ = items.at(i).type_;
685             mediaItem.playable_ = items.at(i).playable_;
686             mediaItem.name_ = items.at(i).name_;
687             mediaItem.attributes_ = items.at(i).attributes_;
688             mediaItem.values_ = items.at(i).values_;
689 
690             mediaItems.push_back(mediaItem);
691         }
692 
693         std::lock_guard<std::mutex> lock(observerMutex_);
694         observers_.ForEach([device, result, mediaItems, uidCounter](std::shared_ptr<IObserver> observer) {
695             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_FOLDER_ITEMS, result);
696             resp.mediaItems_ = std::make_unique<AvrcpCtResponse::MediaItems>(uidCounter, mediaItems);
697             observer->OnActionCompleted(device, resp);
698         });
699     }
700 
OnGetItemAttributesOHOS::Bluetooth::AvrcpController::impl701     void OnGetItemAttributes(const BluetoothRemoteDevice &device, const std::vector<uint32_t> &attributes,
702         const std::vector<std::string> &values, int result, int detail)
703     {
704         HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d",
705             GET_ENCRYPT_ADDR(device), result, detail);
706         std::vector<AvrcpCtResponse::ItemAttributes::ItemAttribute> itemAttrs;
707         for (int i = 0; i < static_cast<int>(attributes.size()); i++) {
708             AvrcpCtResponse::ItemAttributes::ItemAttribute itemAttr;
709             itemAttr.attribute_ = attributes.at(i);
710             itemAttr.value_ = values.at(i);
711             itemAttrs.push_back(itemAttr);
712         }
713 
714         std::lock_guard<std::mutex> lock(observerMutex_);
715         observers_.ForEach([device, result, itemAttrs](std::shared_ptr<IObserver> observer) {
716             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_ITEM_ATTRIBUTES, result);
717             resp.itemAttrs_ = std::make_unique<AvrcpCtResponse::ItemAttributes>(itemAttrs);
718             observer->OnActionCompleted(device, resp);
719         });
720     }
721 
OnGetTotalNumberOfItemsOHOS::Bluetooth::AvrcpController::impl722     void OnGetTotalNumberOfItems(
723         const BluetoothRemoteDevice &device, uint16_t uidCounter, uint32_t numOfItems, int result, int detail)
724     {
725         HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d",
726             GET_ENCRYPT_ADDR(device), result, detail);
727         std::lock_guard<std::mutex> lock(observerMutex_);
728 
729         observers_.ForEach([device, uidCounter, numOfItems, result](std::shared_ptr<IObserver> observer) {
730             AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_TOTAL_NUMBER_OF_ITEMS, result);
731             resp.totalItems_ = std::make_unique<AvrcpCtResponse::TotalNumberOfItems>(uidCounter, numOfItems);
732             observer->OnActionCompleted(device, resp);
733         });
734     }
735 
OnSetAbsoluteVolumeOHOS::Bluetooth::AvrcpController::impl736     void OnSetAbsoluteVolume(const BluetoothRemoteDevice &device, uint16_t volume, int result)
737     {
738         HILOGI("enter, device: %{public}s, volume: %{public}d, res: %{public}d",
739             GET_ENCRYPT_ADDR(device), volume, result);
740         std::lock_guard<std::mutex> lock(observerMutex_);
741 
742         observers_.ForEach([device, volume, result](std::shared_ptr<IObserver> observer) {
743             AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_ABSOLUTE_VOLUME, result);
744             resp.absVolume_ = std::make_unique<AvrcpCtResponse::AbsoluteVolume>(volume);
745             observer->OnActionCompleted(device, resp);
746         });
747     }
748 
OnPlaybackStatusChangedOHOS::Bluetooth::AvrcpController::impl749     void OnPlaybackStatusChanged(const BluetoothRemoteDevice &device, uint8_t playStatus, int result)
750     {
751         HILOGI("enter, device: %{public}s, playStatus: %{public}d, res: %{public}d",
752             GET_ENCRYPT_ADDR(device), playStatus, result);
753         std::lock_guard<std::mutex> lock(observerMutex_);
754 
755         observers_.ForEach([device, playStatus, result](std::shared_ptr<IObserver> observer) {
756             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_STATUS_CHANGED, result);
757             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(playStatus, 0x00);
758             observer->OnActionCompleted(device, resp);
759         });
760     }
761 
OnTrackChangedOHOS::Bluetooth::AvrcpController::impl762     void OnTrackChanged(const BluetoothRemoteDevice &device, uint64_t uid, int result)
763     {
764         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
765         std::lock_guard<std::mutex> lock(observerMutex_);
766 
767         observers_.ForEach([device, uid, result](std::shared_ptr<IObserver> observer) {
768             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_CHANGED, result);
769             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(uid);
770             observer->OnActionCompleted(device, resp);
771         });
772     }
773 
OnTrackReachedEndOHOS::Bluetooth::AvrcpController::impl774     void OnTrackReachedEnd(const BluetoothRemoteDevice &device, int result)
775     {
776         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
777         std::lock_guard<std::mutex> lock(observerMutex_);
778 
779         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
780             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_REACHED_END, result);
781             observer->OnActionCompleted(device, resp);
782         });
783     }
784 
OnTrackReachedStartOHOS::Bluetooth::AvrcpController::impl785     void OnTrackReachedStart(const BluetoothRemoteDevice &device, int result)
786     {
787         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
788         std::lock_guard<std::mutex> lock(observerMutex_);
789 
790         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
791             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_REACHED_START, result);
792             observer->OnActionCompleted(device, resp);
793         });
794     }
795 
OnPlaybackPosChangedOHOS::Bluetooth::AvrcpController::impl796     void OnPlaybackPosChanged(const BluetoothRemoteDevice &device, uint32_t playbackPos, int result)
797     {
798         HILOGI("enter, device: %{public}s, playbackPos: %{public}d, res: %{public}d",
799             GET_ENCRYPT_ADDR(device), playbackPos, result);
800         std::lock_guard<std::mutex> lock(observerMutex_);
801 
802         observers_.ForEach([device, playbackPos, result](std::shared_ptr<IObserver> observer) {
803             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_POS_CHANGED, result);
804             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(playbackPos);
805             observer->OnActionCompleted(device, resp);
806         });
807     }
808 
OnPlayerAppSettingChangedOHOS::Bluetooth::AvrcpController::impl809     void OnPlayerAppSettingChanged(const BluetoothRemoteDevice &device, const std::vector<uint8_t> &attributes,
810         const std::vector<uint8_t> &values, int result)
811     {
812         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
813         std::lock_guard<std::mutex> lock(observerMutex_);
814 
815         observers_.ForEach([device, attributes, values, result](std::shared_ptr<IObserver> observer) {
816             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYER_APPLICATION_SETTING_CHANGED, result);
817             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(attributes, values);
818             observer->OnActionCompleted(device, resp);
819         });
820     }
821 
OnNowPlayingContentChangedOHOS::Bluetooth::AvrcpController::impl822     void OnNowPlayingContentChanged(const BluetoothRemoteDevice &device, int result)
823     {
824         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
825         std::lock_guard<std::mutex> lock(observerMutex_);
826 
827         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
828             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_NOW_PLAYING_CONTENT_CHANGED, result);
829             observer->OnActionCompleted(device, resp);
830         });
831     }
832 
OnAvailablePlayersChangedOHOS::Bluetooth::AvrcpController::impl833     void OnAvailablePlayersChanged(const BluetoothRemoteDevice &device, int result)
834     {
835         HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result);
836         std::lock_guard<std::mutex> lock(observerMutex_);
837 
838         observers_.ForEach([device, result](std::shared_ptr<IObserver> observer) {
839             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_AVAILABLE_PLAYERS_CHANGED, result);
840             observer->OnActionCompleted(device, resp);
841         });
842     }
843 
OnAddressedPlayerChangedOHOS::Bluetooth::AvrcpController::impl844     void OnAddressedPlayerChanged(
845         const BluetoothRemoteDevice &device, uint16_t playerId, uint16_t uidCounter, int result)
846     {
847         HILOGI("enter, device: %{public}s, playerId: %{public}d, uidCounter: %{public}d, res: %{public}d",
848             GET_ENCRYPT_ADDR(device), playerId, uidCounter, result);
849         std::lock_guard<std::mutex> lock(observerMutex_);
850 
851         observers_.ForEach([device, uidCounter, result](std::shared_ptr<IObserver> observer) {
852             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_ADDRESSED_PLAYER_CHANGED, result);
853             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(uidCounter);
854             observer->OnActionCompleted(device, resp);
855         });
856     }
857 
OnUidChangedOHOS::Bluetooth::AvrcpController::impl858     void OnUidChanged(const BluetoothRemoteDevice &device, uint16_t uidCounter, int result)
859     {
860         HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d",
861             GET_ENCRYPT_ADDR(device), uidCounter, result);
862         std::lock_guard<std::mutex> lock(observerMutex_);
863 
864         observers_.ForEach([device, result, uidCounter](std::shared_ptr<IObserver> observer) {
865             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_UIDS_CHANGED, result);
866             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(uidCounter);
867             observer->OnActionCompleted(device, resp);
868         });
869     }
870 
OnVolumeChangedOHOS::Bluetooth::AvrcpController::impl871     void OnVolumeChanged(const BluetoothRemoteDevice &device, uint8_t volume, int result)
872     {
873         HILOGI("enter, device: %{public}s, volume: %{public}d, res: %{public}d",
874             GET_ENCRYPT_ADDR(device), volume, result);
875         std::lock_guard<std::mutex> lock(observerMutex_);
876 
877         observers_.ForEach([device, volume, result](std::shared_ptr<IObserver> observer) {
878             AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_VOLUME_CHANGED, result);
879             resp.notify_ = std::make_unique<AvrcpCtResponse::Notification>(0x00, volume);
880             observer->OnActionCompleted(device, resp);
881         });
882     }
883     std::mutex observerMutex_;
884     BluetoothObserverList<AvrcpController::IObserver> observers_;
885 
886     sptr<ObserverImpl> observer_;
887     int32_t profileRegisterId = 0;
888 };
889 
impl()890 AvrcpController::impl::impl()
891 {
892     observer_ = new (std::nothrow) ObserverImpl(this);
893     CHECK_AND_RETURN_LOG(observer_ != nullptr, "observer_ is nullptr");
894     profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_AVRCP_CT,
895         [this](sptr<IRemoteObject> remote) {
896         sptr<IBluetoothAvrcpCt> proxy = iface_cast<IBluetoothAvrcpCt>(remote);
897         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
898         proxy->RegisterObserver(observer_);
899     });
900 }
901 
GetProfile(void)902 AvrcpController *AvrcpController::GetProfile(void)
903 {
904     HILOGI("enter");
905 #ifdef DTFUZZ_TEST
906     static BluetoothNoDestructor<AvrcpController> instance;
907     return instance.get();
908 #else
909     static AvrcpController instance;
910     return &instance;
911 #endif
912 }
913 
914 /******************************************************************
915  * REGISTER / UNREGISTER OBSERVER                                 *
916  ******************************************************************/
917 
RegisterObserver(std::shared_ptr<IObserver> observer)918 void AvrcpController::RegisterObserver(std::shared_ptr<IObserver> observer)
919 {
920     HILOGD("enter");
921     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
922     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
923     pimpl->observers_.Register(observer);
924 }
925 
UnregisterObserver(std::shared_ptr<IObserver> observer)926 void AvrcpController::UnregisterObserver(std::shared_ptr<IObserver> observer)
927 {
928     HILOGD("enter");
929     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
930     CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
931     pimpl->observers_.Deregister(observer);
932 }
933 
934 /******************************************************************
935  * CONNECTION                                                     *
936  ******************************************************************/
937 
GetConnectedDevices(void)938 std::vector<BluetoothRemoteDevice> AvrcpController::GetConnectedDevices(void)
939 {
940     HILOGI("enter");
941     if (!IS_BT_ENABLED()) {
942         HILOGE("bluetooth is off.");
943         return std::vector<BluetoothRemoteDevice>();
944     }
945     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
946     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
947         std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
948 
949     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
950     std::vector<BluetoothRemoteDevice> devices;
951     std::vector<RawAddress> rawAddrs = proxy->GetConnectedDevices();
952     for (auto rawAddr : rawAddrs) {
953         BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
954         devices.push_back(device);
955     }
956     return devices;
957 }
958 
GetDevicesByStates(const std::vector<int> & states)959 std::vector<BluetoothRemoteDevice> AvrcpController::GetDevicesByStates(const std::vector<int> &states)
960 {
961     HILOGI("enter");
962 
963     if (!IS_BT_ENABLED()) {
964         HILOGE("bluetooth is off.");
965         return std::vector<BluetoothRemoteDevice>();
966     }
967     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
968     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
969         std::vector<BluetoothRemoteDevice>(), "failed: no proxy");
970 
971     std::vector<BluetoothRemoteDevice> devices;
972     if (proxy != nullptr) {
973         std::vector<int32_t> convertStates;
974         for (auto state : states) {
975             convertStates.push_back(static_cast<int32_t>(state));
976         }
977         std::vector<RawAddress> rawAddrs = proxy->GetDevicesByStates(convertStates);
978         for (auto rawAddr : rawAddrs) {
979             BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
980             devices.push_back(device);
981         }
982     }
983 
984     return devices;
985 }
986 
GetDeviceState(const BluetoothRemoteDevice & device)987 int AvrcpController::GetDeviceState(const BluetoothRemoteDevice &device)
988 {
989     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
990 
991     if (!IS_BT_ENABLED()) {
992         HILOGE("bluetooth is off.");
993         return static_cast<int>(BTConnectState::DISCONNECTED);
994     }
995 
996     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
997     CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
998         static_cast<int>(BTConnectState::DISCONNECTED), "failed: no proxy");
999 
1000     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1001     return proxy->GetDeviceState(rawAddr);
1002 }
1003 
Connect(const BluetoothRemoteDevice & device)1004 bool AvrcpController::Connect(const BluetoothRemoteDevice &device)
1005 {
1006     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1007 
1008     if (!IS_BT_ENABLED()) {
1009         HILOGE("bluetooth is off.");
1010         return false;
1011     }
1012 
1013     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1014     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
1015 
1016     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1017     int result = proxy->Connect(rawAddr);
1018     return result == RET_NO_ERROR;
1019 }
1020 
Disconnect(const BluetoothRemoteDevice & device)1021 bool AvrcpController::Disconnect(const BluetoothRemoteDevice &device)
1022 {
1023     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1024 
1025     if (!IS_BT_ENABLED()) {
1026         HILOGE("bluetooth is off.");
1027         return false;
1028     }
1029 
1030     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1031     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
1032 
1033     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1034     int result = proxy->Disconnect(rawAddr);
1035     return result == RET_NO_ERROR;
1036 }
1037 
1038 /******************************************************************
1039  * BUTTON OPERATION                                               *
1040  ******************************************************************/
1041 
PressButton(const BluetoothRemoteDevice & device,uint8_t button)1042 int AvrcpController::PressButton(const BluetoothRemoteDevice &device, uint8_t button)
1043 {
1044     HILOGI("enter, device: %{public}s, button: %{public}d", GET_ENCRYPT_ADDR(device), button);
1045 
1046     if (!IS_BT_ENABLED()) {
1047         HILOGE("bluetooth is off.");
1048         return RET_BAD_STATUS;
1049     }
1050 
1051     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1052     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1053 
1054     int result = RET_BAD_STATUS;
1055     switch (button) {
1056         case AVRC_KEY_OPERATION_VOLUME_UP:
1057         case AVRC_KEY_OPERATION_VOLUME_DOWN:
1058         case AVRC_KEY_OPERATION_MUTE:
1059         case AVRC_KEY_OPERATION_PLAY:
1060         case AVRC_KEY_OPERATION_STOP:
1061         case AVRC_KEY_OPERATION_PAUSE:
1062         case AVRC_KEY_OPERATION_REWIND:
1063         case AVRC_KEY_OPERATION_FAST_FORWARD:
1064         case AVRC_KEY_OPERATION_FORWARD:
1065         case AVRC_KEY_OPERATION_BACKWARD: {
1066             BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1067             result = proxy->PressButton(rawAddr, static_cast<int32_t>(button));
1068             break;
1069         }
1070         default:
1071             result = RET_NO_SUPPORT;
1072             if (button >= AVRC_KEY_OPERATION_INVALID) {
1073                 result = RET_BAD_PARAM;
1074             }
1075             break;
1076     }
1077     return result;
1078 }
1079 
ReleaseButton(const BluetoothRemoteDevice & device,uint8_t button)1080 int AvrcpController::ReleaseButton(const BluetoothRemoteDevice &device, uint8_t button)
1081 {
1082     HILOGI("enter, device: %{public}s, button: %{public}d", GET_ENCRYPT_ADDR(device), button);
1083 
1084     if (!IS_BT_ENABLED()) {
1085         HILOGE("bluetooth is off.");
1086         return RET_BAD_STATUS;
1087     }
1088 
1089     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1090     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1091 
1092     int result = RET_BAD_STATUS;
1093     switch (button) {
1094         case AVRC_KEY_OPERATION_VOLUME_UP:
1095         case AVRC_KEY_OPERATION_VOLUME_DOWN:
1096         case AVRC_KEY_OPERATION_MUTE:
1097         case AVRC_KEY_OPERATION_PLAY:
1098         case AVRC_KEY_OPERATION_STOP:
1099         case AVRC_KEY_OPERATION_PAUSE:
1100         case AVRC_KEY_OPERATION_REWIND:
1101         case AVRC_KEY_OPERATION_FAST_FORWARD:
1102         case AVRC_KEY_OPERATION_FORWARD:
1103         case AVRC_KEY_OPERATION_BACKWARD: {
1104             BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1105             result = proxy->ReleaseButton(rawAddr, static_cast<int32_t>(button));
1106             break;
1107         }
1108         default:
1109             result = RET_NO_SUPPORT;
1110             if (button >= AVRC_KEY_OPERATION_INVALID) {
1111                 result = RET_BAD_PARAM;
1112             }
1113             break;
1114     }
1115     return result;
1116 }
1117 
1118 /******************************************************************
1119  * TEMP UNIT INFO / SUB UNIT INFO                                 *
1120  ******************************************************************/
1121 
GetUnitInfo(const BluetoothRemoteDevice & device)1122 int AvrcpController::GetUnitInfo(const BluetoothRemoteDevice &device)
1123 {
1124     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1125 
1126     if (!IS_BT_ENABLED()) {
1127         HILOGE("bluetooth is off.");
1128         return RET_BAD_STATUS;
1129     }
1130 
1131     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1132     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1133 
1134     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1135     return proxy->GetUnitInfo(rawAddr);
1136 }
1137 
GetSubUnitInfo(const BluetoothRemoteDevice & device)1138 int AvrcpController::GetSubUnitInfo(const BluetoothRemoteDevice &device)
1139 {
1140     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1141 
1142     if (!IS_BT_ENABLED()) {
1143         HILOGE("bluetooth is off.");
1144         return RET_BAD_STATUS;
1145     }
1146 
1147     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1148     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1149 
1150     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1151     return proxy->GetSubUnitInfo(rawAddr);
1152 }
1153 
1154 /******************************************************************
1155  * Capabilities                                                   *
1156  ******************************************************************/
1157 
GetSupportedCompanies(const BluetoothRemoteDevice & device)1158 int AvrcpController::GetSupportedCompanies(const BluetoothRemoteDevice &device)
1159 {
1160     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1161 
1162     if (!IS_BT_ENABLED()) {
1163         HILOGE("bluetooth is off.");
1164         return RET_BAD_STATUS;
1165     }
1166 
1167     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1168     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1169 
1170     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1171     return proxy->GetSupportedCompanies(rawAddr);
1172 }
1173 
GetSupportedEvents(const BluetoothRemoteDevice & device)1174 int AvrcpController::GetSupportedEvents(const BluetoothRemoteDevice &device)
1175 {
1176     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1177 
1178     if (!IS_BT_ENABLED()) {
1179         HILOGE("bluetooth is off.");
1180         return RET_BAD_STATUS;
1181     }
1182 
1183     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1184     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1185 
1186     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1187     return proxy->GetSupportedEvents(rawAddr);
1188 }
1189 
1190 /******************************************************************
1191  * PLAYER APPLICATION SETTINGS                                     *
1192  ******************************************************************/
1193 
GetPlayerAppSettingAttributes(const BluetoothRemoteDevice & device)1194 int AvrcpController::GetPlayerAppSettingAttributes(const BluetoothRemoteDevice &device)
1195 {
1196     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1197 
1198     if (!IS_BT_ENABLED()) {
1199         HILOGE("bluetooth is off.");
1200         return RET_BAD_STATUS;
1201     }
1202 
1203     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1204     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1205 
1206     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1207     return proxy->GetPlayerAppSettingAttributes(rawAddr);
1208 }
1209 
GetPlayerAppSettingValues(const BluetoothRemoteDevice & device,uint8_t attribute)1210 int AvrcpController::GetPlayerAppSettingValues(const BluetoothRemoteDevice &device, uint8_t attribute)
1211 {
1212     HILOGI("enter, device: %{public}s, attribute: %{public}d", GET_ENCRYPT_ADDR(device), attribute);
1213 
1214     if (!IS_BT_ENABLED()) {
1215         HILOGE("bluetooth is off.");
1216         return RET_BAD_STATUS;
1217     }
1218 
1219     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1220     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1221 
1222     int result = RET_BAD_STATUS;
1223     do {
1224         if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) {
1225             result = RET_BAD_PARAM;
1226             break;
1227         }
1228         if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) {
1229             result = RET_BAD_PARAM;
1230             break;
1231         }
1232         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1233         result = proxy->GetPlayerAppSettingValues(rawAddr, static_cast<int32_t>(attribute));
1234     } while (false);
1235 
1236     return result;
1237 }
1238 
GetPlayerAppSettingCurrentValue(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & attributes)1239 int AvrcpController::GetPlayerAppSettingCurrentValue(
1240     const BluetoothRemoteDevice &device, const std::vector<uint8_t> &attributes)
1241 {
1242     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1243 
1244     if (!IS_BT_ENABLED()) {
1245         HILOGE("bluetooth is off.");
1246         return RET_NO_ERROR;
1247     }
1248 
1249     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1250     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1251 
1252     int result = RET_NO_ERROR;
1253     do {
1254         std::vector<int32_t> attrs;
1255         for (auto attribute : attributes) {
1256             if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) {
1257                 result = RET_BAD_PARAM;
1258                 break;
1259             }
1260             if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) {
1261                 result = RET_BAD_PARAM;
1262                 break;
1263             }
1264             attrs.push_back(attribute);
1265         }
1266         if (result != RET_NO_ERROR) {
1267             break;
1268         }
1269         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1270         result = proxy->GetPlayerAppSettingCurrentValue(rawAddr, attrs);
1271     } while (false);
1272 
1273     return result;
1274 }
1275 
SetPlayerAppSettingCurrentValue(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values)1276 int AvrcpController::SetPlayerAppSettingCurrentValue(
1277     const BluetoothRemoteDevice &device, const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values)
1278 {
1279     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1280 
1281     if (!IS_BT_ENABLED()) {
1282         HILOGE("bluetooth is off.");
1283         return RET_BAD_STATUS;
1284     }
1285 
1286     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1287     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1288 
1289     int result = RET_NO_ERROR;
1290     std::vector<int32_t> myAttributes;
1291     std::vector<int32_t> myValues;
1292     do {
1293         if (attributes.size() != values.size()) {
1294             break;
1295         }
1296         for (auto attribute : attributes) {
1297             if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) {
1298                 result = RET_BAD_PARAM;
1299                 break;
1300             }
1301             if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) {
1302                 result = RET_BAD_PARAM;
1303                 break;
1304             }
1305         }
1306         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1307         for (auto attribute : attributes) {
1308             myAttributes.push_back(static_cast<int32_t>(attribute));
1309         }
1310         for (auto value : values) {
1311             myValues.push_back(static_cast<int32_t>(value));
1312         }
1313         if (result != RET_NO_ERROR) {
1314             break;
1315         }
1316         result = proxy->SetPlayerAppSettingCurrentValue(rawAddr, myAttributes, myValues);
1317     } while (false);
1318 
1319     return result;
1320 }
1321 
GetPlayerApplicationSettingAttributeText(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & attributes)1322 int AvrcpController::GetPlayerApplicationSettingAttributeText(
1323     const BluetoothRemoteDevice &device, const std::vector<uint8_t> &attributes)
1324 {
1325     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1326 
1327     if (!IS_BT_ENABLED()) {
1328         HILOGE("bluetooth is off.");
1329         return RET_BAD_STATUS;
1330     }
1331 
1332     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1333     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1334 
1335     int result = RET_BAD_STATUS;
1336     do {
1337         std::vector<int32_t> attrs;
1338         for (auto attribute : attributes) {
1339             if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) {
1340                 break;
1341             }
1342             if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) {
1343                 break;
1344             }
1345             attrs.push_back(static_cast<int32_t>(attribute));
1346         }
1347         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1348         result = proxy->GetPlayerAppSettingAttributeText(rawAddr, attrs);
1349     } while (false);
1350 
1351     return result;
1352 }
1353 
GetPlayerApplicationSettingValueText(const BluetoothRemoteDevice & device,uint8_t attributeId,const std::vector<uint8_t> & values)1354 int AvrcpController::GetPlayerApplicationSettingValueText(
1355     const BluetoothRemoteDevice &device, uint8_t attributeId, const std::vector<uint8_t> &values)
1356 {
1357     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1358 
1359     if (!IS_BT_ENABLED()) {
1360         HILOGE("bluetooth is off.");
1361         return RET_BAD_STATUS;
1362     }
1363 
1364     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1365     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1366 
1367     int result = RET_BAD_STATUS;
1368     do {
1369         std::vector<int32_t> myValues;
1370         if (attributeId == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) {
1371             result = RET_BAD_PARAM;
1372             break;
1373         }
1374         if (attributeId >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attributeId <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) {
1375             result = RET_BAD_PARAM;
1376             break;
1377         }
1378         for (auto value : values) {
1379             myValues.push_back(static_cast<int32_t>(value));
1380         }
1381         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1382         result = proxy->GetPlayerAppSettingValueText(rawAddr, static_cast<int32_t>(attributeId), myValues);
1383     } while (false);
1384 
1385     return result;
1386 }
1387 
1388 /******************************************************************
1389  * MEDIA INFORMATION                                              *
1390  ******************************************************************/
1391 
GetElementAttributes(const BluetoothRemoteDevice & device,const std::vector<uint32_t> & attributes)1392 int AvrcpController::GetElementAttributes(const BluetoothRemoteDevice &device, const std::vector<uint32_t> &attributes)
1393 {
1394     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1395 
1396     if (!IS_BT_ENABLED()) {
1397         HILOGE("bluetooth is off.");
1398         return RET_BAD_STATUS;
1399     }
1400 
1401     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1402     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1403 
1404     std::vector<int32_t> attrs;
1405     for (auto attribute : attributes) {
1406         attrs.push_back(static_cast<int32_t>(attribute));
1407     }
1408     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1409     return proxy->GetElementAttributes(rawAddr, attrs);
1410 }
1411 
1412 /******************************************************************
1413  * PLAY                                                           *
1414  ******************************************************************/
1415 
GetPlayStatus(const BluetoothRemoteDevice & device)1416 int AvrcpController::GetPlayStatus(const BluetoothRemoteDevice &device)
1417 {
1418     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1419 
1420     if (!IS_BT_ENABLED()) {
1421         HILOGE("bluetooth is off.");
1422         return RET_BAD_STATUS;
1423     }
1424 
1425     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1426     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1427 
1428     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1429     return proxy->GetPlayStatus(rawAddr);
1430 }
1431 
PlayItem(const BluetoothRemoteDevice & device,uint64_t uid,uint16_t uidCounter)1432 int AvrcpController::PlayItem(const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter)
1433 {
1434     HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter);
1435 
1436     if (!IS_BT_ENABLED()) {
1437         HILOGE("bluetooth is off.");
1438         return RET_BAD_STATUS;
1439     }
1440     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1441     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1442 
1443     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1444     return proxy->PlayItem(rawAddr,
1445         static_cast<int32_t>(AVRC_MEDIA_SCOPE_NOW_PLAYING),
1446         static_cast<int64_t>(uid),
1447         static_cast<int32_t>(uidCounter));
1448 }
1449 
1450 /******************************************************************
1451  * OPERATE THE VIRTUAL FILE SYSTEM                                *
1452  ******************************************************************/
1453 
GetFolderItems(const BluetoothRemoteDevice & device,uint32_t startItem,uint32_t endItem,const std::vector<uint32_t> & attributes)1454 int AvrcpController::GetFolderItems(
1455     const BluetoothRemoteDevice &device, uint32_t startItem, uint32_t endItem, const std::vector<uint32_t> &attributes)
1456 {
1457     HILOGI("enter, device: %{public}s, startItem: %{public}d, endItem: %{public}d",
1458         GET_ENCRYPT_ADDR(device), startItem, endItem);
1459 
1460     if (!IS_BT_ENABLED()) {
1461         HILOGE("bluetooth is off.");
1462         return RET_BAD_STATUS;
1463     }
1464 
1465     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1466     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1467 
1468     int result = RET_NO_ERROR;
1469     do {
1470         if (startItem > endItem) {
1471             result = RET_BAD_PARAM;
1472             break;
1473         }
1474         for (auto attribute : attributes) {
1475             if (attribute <= AVRC_MEDIA_ATTRIBUTE_NOT_USED || attribute >= AVRC_MEDIA_ATTRIBUTE_RESERVED) {
1476                 result = RET_BAD_PARAM;
1477                 break;
1478             }
1479         }
1480         if (result != RET_NO_ERROR) {
1481             break;
1482         }
1483         BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1484         std::vector<int32_t> attrs;
1485         for (auto attribute : attributes) {
1486             attrs.push_back(static_cast<int32_t>(attribute));
1487         }
1488         result = proxy->GetFolderItems(
1489             rawAddr, static_cast<int32_t>(startItem), static_cast<int32_t>(endItem), attrs);
1490     } while (false);
1491 
1492     return result;
1493 }
1494 
GetMeidaPlayerList(const BluetoothRemoteDevice & device,uint32_t startItem,uint32_t endItem)1495 int AvrcpController::GetMeidaPlayerList(const BluetoothRemoteDevice &device, uint32_t startItem, uint32_t endItem)
1496 {
1497     HILOGI("enter, device: %{public}s, startItem: %{public}d, endItem: %{public}d",
1498         GET_ENCRYPT_ADDR(device), startItem, endItem);
1499     if (!IS_BT_ENABLED()) {
1500         HILOGE("bluetooth is off.");
1501         return RET_BAD_STATUS;
1502     }
1503 
1504     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1505     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1506 
1507     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1508     return proxy->GetMeidaPlayerList(rawAddr, static_cast<int32_t>(startItem), static_cast<int32_t>(endItem));
1509 }
1510 
GetTotalNumberOfItems(const BluetoothRemoteDevice & device)1511 int AvrcpController::GetTotalNumberOfItems(const BluetoothRemoteDevice &device)
1512 {
1513     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1514 
1515     if (!IS_BT_ENABLED()) {
1516         HILOGE("bluetooth is off.");
1517         return RET_BAD_STATUS;
1518     }
1519 
1520     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1521     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1522 
1523     uint8_t scope = AVRC_MEDIA_SCOPE_NOW_PLAYING;
1524     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1525     return proxy->GetTotalNumberOfItems(rawAddr, static_cast<int32_t>(scope));
1526 }
1527 
1528 /******************************************************************
1529  * ABSOLUTE VOLUME                                                *
1530  ******************************************************************/
1531 
SetAbsoluteVolume(const BluetoothRemoteDevice & device,uint8_t volume)1532 int AvrcpController::SetAbsoluteVolume(const BluetoothRemoteDevice &device, uint8_t volume)
1533 {
1534     HILOGI("enter, device: %{public}s, volume: %{public}d", GET_ENCRYPT_ADDR(device), volume);
1535 
1536     if (!IS_BT_ENABLED()) {
1537         HILOGE("bluetooth is off.");
1538         return RET_BAD_STATUS;
1539     }
1540 
1541     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1542     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1543 
1544     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1545     return proxy->SetAbsoluteVolume(rawAddr, static_cast<int32_t>(volume));
1546 }
1547 
1548 /******************************************************************
1549  * NOTIFY                                                         *
1550  ******************************************************************/
1551 
EnableNotification(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & events,uint32_t interval)1552 int AvrcpController::EnableNotification(
1553     const BluetoothRemoteDevice &device, const std::vector<uint8_t> &events, uint32_t interval)
1554 {
1555     HILOGI("enter, device: %{public}s, interval: %{public}d", GET_ENCRYPT_ADDR(device), interval);
1556 
1557     if (!IS_BT_ENABLED()) {
1558         HILOGE("bluetooth is off.");
1559         return RET_BAD_STATUS;
1560     }
1561 
1562     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1563     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1564 
1565     std::vector<int32_t> myEvents;
1566     for (auto event : events) {
1567         myEvents.push_back(static_cast<int32_t>(event));
1568     }
1569 
1570     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1571     return proxy->EnableNotification(rawAddr, myEvents, static_cast<int32_t>(interval));
1572 }
1573 
DisableNotification(const BluetoothRemoteDevice & device,const std::vector<uint8_t> & events)1574 int AvrcpController::DisableNotification(const BluetoothRemoteDevice &device, const std::vector<uint8_t> &events)
1575 {
1576     HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
1577 
1578     if (!IS_BT_ENABLED()) {
1579         HILOGE("bluetooth is off.");
1580         return RET_BAD_STATUS;
1581     }
1582 
1583     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1584     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1585 
1586     std::vector<int32_t> myEvents;
1587     for (auto event : events) {
1588         myEvents.push_back(static_cast<int32_t>(event));
1589     }
1590 
1591     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1592     return proxy->DisableNotification(rawAddr, myEvents);
1593 }
1594 
1595 /******************************************************************
1596  * DO NOT EXPOSE THE INTERFACE                                    *
1597  ******************************************************************/
1598 
SetAddressedPlayer(const BluetoothRemoteDevice & device,uint16_t playerId)1599 int AvrcpController::SetAddressedPlayer(const BluetoothRemoteDevice &device, uint16_t playerId)
1600 {
1601     HILOGI("enter, device: %{public}s, playerId: %{public}d", GET_ENCRYPT_ADDR(device), playerId);
1602     if (!IS_BT_ENABLED()) {
1603         HILOGE("bluetooth is off.");
1604         return RET_BAD_STATUS;
1605     }
1606 
1607     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1608     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1609 
1610     return RET_BAD_STATUS;
1611 }
1612 
SetBrowsedPlayer(const BluetoothRemoteDevice & device,uint16_t playerId)1613 int AvrcpController::SetBrowsedPlayer(const BluetoothRemoteDevice &device, uint16_t playerId)
1614 {
1615     HILOGI("enter, device: %{public}s, playerId: %{public}d", GET_ENCRYPT_ADDR(device), playerId);
1616 
1617     if (!IS_BT_ENABLED()) {
1618         HILOGE("bluetooth is off.");
1619         return RET_BAD_STATUS;
1620     }
1621 
1622     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1623     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1624 
1625     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1626     return proxy->SetBrowsedPlayer(rawAddr, (int32_t)playerId);
1627 }
1628 
ChangePath(const BluetoothRemoteDevice & device,uint16_t uidCounter,uint16_t direction,uint64_t folderUid)1629 int AvrcpController::ChangePath(
1630     const BluetoothRemoteDevice &device, uint16_t uidCounter, uint16_t direction, uint64_t folderUid)
1631 {
1632     HILOGI("enter, device: %{public}s, uidCounter: %{public}d, direction: %{public}d",
1633         GET_ENCRYPT_ADDR(device), uidCounter, direction);
1634 
1635     if (!IS_BT_ENABLED()) {
1636         HILOGE("bluetooth is off.");
1637         return RET_BAD_STATUS;
1638     }
1639 
1640     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1641     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1642 
1643     return RET_BAD_STATUS;
1644 }
1645 
GetItemAttributes(const BluetoothRemoteDevice & device,uint64_t uid,uint16_t uidCounter,const std::vector<uint32_t> & attributes)1646 int AvrcpController::GetItemAttributes(
1647     const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter, const std::vector<uint32_t> &attributes)
1648 {
1649     HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter);
1650 
1651     if (!IS_BT_ENABLED()) {
1652         HILOGE("bluetooth is off.");
1653         return RET_BAD_STATUS;
1654     }
1655 
1656     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1657     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1658 
1659     std::vector<int32_t> attrs;
1660     for (auto attribute : attributes) {
1661         attrs.push_back(static_cast<int32_t>(attribute));
1662     }
1663 
1664     BluetoothRawAddress rawAddr(device.GetDeviceAddr());
1665     return proxy->GetItemAttributes(rawAddr, (int64_t)uid, (int32_t)uidCounter, attrs);
1666 }
1667 
RequestContinuingResponse(const BluetoothRemoteDevice & device,uint8_t pduId)1668 int AvrcpController::RequestContinuingResponse(const BluetoothRemoteDevice &device, uint8_t pduId)
1669 {
1670     HILOGI("enter, device: %{public}s, pduId: %{public}d", GET_ENCRYPT_ADDR(device), pduId);
1671 
1672     if (!IS_BT_ENABLED()) {
1673         HILOGE("bluetooth is off.");
1674         return RET_BAD_STATUS;
1675     }
1676 
1677     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1678     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1679 
1680     return RET_BAD_STATUS;
1681 }
1682 
AbortContinuingResponse(const BluetoothRemoteDevice & device,uint8_t pduId)1683 int AvrcpController::AbortContinuingResponse(const BluetoothRemoteDevice &device, uint8_t pduId)
1684 {
1685     HILOGI("enter, device: %{public}s, pduId: %{public}d", GET_ENCRYPT_ADDR(device), pduId);
1686 
1687     int result = RET_BAD_STATUS;
1688 
1689     return result;
1690 }
1691 
AddToNowPlaying(const BluetoothRemoteDevice & device,uint64_t uid,uint16_t uidCounter)1692 int AvrcpController::AddToNowPlaying(const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter)
1693 {
1694     HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter);
1695 
1696     if (!IS_BT_ENABLED()) {
1697         HILOGE("bluetooth is off.");
1698         return RET_BAD_STATUS;
1699     }
1700 
1701     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1702     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy");
1703 
1704     return RET_BAD_STATUS;
1705 }
1706 
AvrcpController(void)1707 AvrcpController::AvrcpController(void)
1708 {
1709     HILOGI("enter");
1710 
1711     pimpl = std::make_unique<AvrcpController::impl>();
1712 }
1713 
~AvrcpController(void)1714 AvrcpController::~AvrcpController(void)
1715 {
1716     HILOGI("enter");
1717     sptr<IBluetoothAvrcpCt> proxy = GetRemoteProxy<IBluetoothAvrcpCt>(PROFILE_AVRCP_CT);
1718     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
1719     pimpl = nullptr;
1720 }
1721 
1722 }  // namespace Bluetooth
1723 }  // namespace OHOS
1724