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