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 #include <mutex>
16 #include "bluetooth_def.h"
17 #include "bluetooth_avrcp_ct_server.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils.h"
20 #include "interface_adapter_manager.h"
21 #include "interface_profile.h"
22 #include "interface_profile_avrcp_ct.h"
23 #include "interface_profile_manager.h"
24 #include "remote_observer_list.h"
25 #include "permission_utils.h"
26 
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 using namespace OHOS::bluetooth;
31 
32 struct BluetoothAvrcpCtServer::impl {
33 public:
34     class SysStsObserverImpl : public ISystemStateObserver {
35     public:
SysStsObserverImpl(BluetoothAvrcpCtServer::impl * impl)36         explicit SysStsObserverImpl(BluetoothAvrcpCtServer::impl *impl) : impl_(impl)
37         {}
~SysStsObserverImpl()38         virtual ~SysStsObserverImpl()
39         {}
40 
OnSystemStateChange(const BTSystemState state)41         void OnSystemStateChange(const BTSystemState state) override
42         {
43             impl_->OnSystemStateChange(state);
44         }
45 
46     private:
47         BluetoothAvrcpCtServer::impl *impl_;
48     };
49     class ObserverImpl : public IProfileAvrcpCt::IObserver {
50     public:
ObserverImpl(BluetoothAvrcpCtServer::impl * impl)51         explicit ObserverImpl(BluetoothAvrcpCtServer::impl *impl) : impl_(impl)
52         {}
~ObserverImpl()53         virtual ~ObserverImpl()
54         {}
55 
OnConnectionStateChanged(const RawAddress & rawAddr,int state)56         void OnConnectionStateChanged(const RawAddress &rawAddr, int state) override
57         {
58             impl_->OnConnectionStateChanged(rawAddr, state);
59         }
60 
OnPressButton(const RawAddress & rawAddr,uint8_t button,int result)61         void OnPressButton(const RawAddress &rawAddr, uint8_t button, int result) override
62         {
63             impl_->OnPressButton(rawAddr, button, result);
64         }
65 
OnReleaseButton(const RawAddress & rawAddr,uint8_t button,int result)66         void OnReleaseButton(const RawAddress &rawAddr, uint8_t button, int result) override
67         {
68             impl_->OnReleaseButton(rawAddr, button, result);
69         }
70 
OnSetAddressedPlayer(const RawAddress & rawAddr,int result,int detail)71         void OnSetAddressedPlayer(const RawAddress &rawAddr, int result, int detail) override
72         {
73             // The interface is not exposed
74         }
75 
OnSetBrowsedPlayer(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,const std::vector<std::string> & folderNames,int result,int detail)76         void OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems,
77             const std::vector<std::string> &folderNames, int result, int detail) override
78         {
79             impl_->OnSetBrowsedPlayer(rawAddr, uidCounter, numOfItems, folderNames, result, detail);
80         }
81 
OnGetCapabilities(const RawAddress & rawAddr,const std::vector<uint32_t> & companies,const std::vector<uint8_t> & events,int result)82         void OnGetCapabilities(const RawAddress &rawAddr, const std::vector<uint32_t> &companies,
83             const std::vector<uint8_t> &events, int result) override
84         {
85             impl_->OnGetCapabilities(rawAddr, companies, events, result);
86         }
87 
OnGetPlayerAppSettingAttributes(const RawAddress & rawAddr,std::vector<uint8_t> attributes,int result)88         void OnGetPlayerAppSettingAttributes(
89             const RawAddress &rawAddr, std::vector<uint8_t> attributes, int result) override
90         {
91             impl_->OnGetPlayerAppSettingAttributes(rawAddr, attributes, result);
92         }
93 
OnGetPlayerAppSettingValues(const RawAddress & rawAddr,uint8_t attribute,const std::vector<uint8_t> & values,int result)94         void OnGetPlayerAppSettingValues(
95             const RawAddress &rawAddr, uint8_t attribute, const std::vector<uint8_t> &values, int result) override
96         {
97             impl_->OnGetPlayerAppSettingValues(rawAddr, attribute, values, result);
98         }
99 
OnGetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values,int result)100         void OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
101             const std::vector<uint8_t> &values, int result) override
102         {
103             impl_->OnGetPlayerAppSettingCurrentValue(rawAddr, attributes, values, result);
104         }
105 
OnSetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,int result)106         void OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, int result) override
107         {
108             impl_->OnSetPlayerAppSettingCurrentValue(rawAddr, result);
109         }
110 
OnGetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<std::string> & attributeName,int result)111         void OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
112             const std::vector<std::string> &attributeName, int result) override
113         {
114             impl_->OnGetPlayerAppSettingAttributeText(rawAddr, attributes, attributeName, result);
115         }
116 
OnGetPlayerAppSettingValueText(const RawAddress & rawAddr,const std::vector<uint8_t> & values,const std::vector<std::string> & valueName,int result)117         void OnGetPlayerAppSettingValueText(const RawAddress &rawAddr, const std::vector<uint8_t> &values,
118             const std::vector<std::string> &valueName, int result) override
119         {
120             impl_->OnGetPlayerAppSettingValueText(rawAddr, values, valueName, result);
121         }
122 
OnGetElementAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attributes,const std::vector<std::string> & valueName,int result)123         void OnGetElementAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
124             const std::vector<std::string> &valueName, int result) override
125         {
126             impl_->OnGetElementAttributes(rawAddr, attributes, valueName, result);
127         }
128 
OnGetPlayStatus(const RawAddress & rawAddr,uint32_t songLength,uint32_t songPosition,uint8_t playStatus,int result)129         void OnGetPlayStatus(const RawAddress &rawAddr, uint32_t songLength, uint32_t songPosition, uint8_t playStatus,
130             int result) override
131         {
132             impl_->OnGetPlayStatus(rawAddr, songLength, songPosition, playStatus, result);
133         }
134 
OnPlayItem(const RawAddress & rawAddr,int status,int result)135         void OnPlayItem(const RawAddress &rawAddr, int status, int result) override
136         {
137             impl_->OnPlayItem(rawAddr, status, result);
138         }
139 
OnAddToNowPlaying(const RawAddress & rawAddr,int status,int detail)140         void OnAddToNowPlaying(const RawAddress &rawAddr, int status, int detail) override
141         {
142             // The interface is not exposed
143         }
144 
OnChangePath(const RawAddress & rawAddr,uint32_t numOfItems,int result,int detail)145         void OnChangePath(const RawAddress &rawAddr, uint32_t numOfItems, int result, int detail) override
146         {
147             // The interface is not exposed
148         }
149 
OnGetMediaPlayers(const RawAddress & rawAddr,uint16_t uidCounter,const std::vector<AvrcMpItem> & items,int result,int detail)150         void OnGetMediaPlayers(const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMpItem> &items,
151             int result, int detail) override
152         {
153             impl_->OnGetMediaPlayers(rawAddr, uidCounter, items, result, detail);
154         }
155 
OnGetFolderItems(const RawAddress & rawAddr,uint16_t uidCounter,const std::vector<AvrcMeItem> & items,int result,int detail)156         void OnGetFolderItems(const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMeItem> &items,
157             int result, int detail) override
158         {
159             impl_->OnGetFolderItems(rawAddr, uidCounter, items, result, detail);
160         }
161 
OnGetItemAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attributes,const std::vector<std::string> & values,int result,int detail)162         void OnGetItemAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
163             const std::vector<std::string> &values, int result, int detail) override
164         {
165             impl_->OnGetItemAttributes(rawAddr, attributes, values, result, detail);
166         }
167 
OnGetTotalNumberOfItems(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,int result,int detail)168         void OnGetTotalNumberOfItems(
169             const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems, int result, int detail) override
170         {
171             impl_->OnGetTotalNumberOfItems(rawAddr, uidCounter, numOfItems, result, detail);
172         }
173 
OnSetAbsoluteVolume(const RawAddress & rawAddr,uint8_t volume,int result)174         void OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, int result) override
175         {
176             impl_->OnSetAbsoluteVolume(rawAddr, volume, result);
177         }
178 
OnPlaybackStatusChanged(const RawAddress & rawAddr,uint8_t playStatus,int result)179         void OnPlaybackStatusChanged(const RawAddress &rawAddr, uint8_t playStatus, int result) override
180         {
181             impl_->OnPlaybackStatusChanged(rawAddr, playStatus, result);
182         }
183 
OnTrackChanged(const RawAddress & rawAddr,uint64_t uid,int result)184         void OnTrackChanged(const RawAddress &rawAddr, uint64_t uid, int result) override
185         {
186             impl_->OnTrackChanged(rawAddr, uid, result);
187         }
188 
OnTrackReachedEnd(const RawAddress & rawAddr,int result)189         void OnTrackReachedEnd(const RawAddress &rawAddr, int result) override
190         {
191             impl_->OnTrackReachedEnd(rawAddr, result);
192         }
193 
OnTrackReachedStart(const RawAddress & rawAddr,int result)194         void OnTrackReachedStart(const RawAddress &rawAddr, int result) override
195         {
196             impl_->OnTrackReachedStart(rawAddr, result);
197         }
198 
OnPlaybackPosChanged(const RawAddress & rawAddr,uint32_t playbackPos,int result)199         void OnPlaybackPosChanged(const RawAddress &rawAddr, uint32_t playbackPos, int result) override
200         {
201             impl_->OnPlaybackPosChanged(rawAddr, playbackPos, result);
202         }
203 
OnPlayerAppSettingChanged(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values,int result)204         void OnPlayerAppSettingChanged(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
205             const std::vector<uint8_t> &values, int result) override
206         {
207             impl_->OnPlayerAppSettingChanged(rawAddr, attributes, values, result);
208         }
209 
OnNowPlayingContentChanged(const RawAddress & rawAddr,int result)210         void OnNowPlayingContentChanged(const RawAddress &rawAddr, int result) override
211         {
212             impl_->OnNowPlayingContentChanged(rawAddr, result);
213         }
214 
OnAvailablePlayersChanged(const RawAddress & rawAddr,int result)215         void OnAvailablePlayersChanged(const RawAddress &rawAddr, int result) override
216         {
217             impl_->OnAvailablePlayersChanged(rawAddr, result);
218         }
219 
OnAddressedPlayerChanged(const RawAddress & rawAddr,uint16_t playerId,uint16_t uidCounter,int result)220         void OnAddressedPlayerChanged(
221             const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, int result) override
222         {
223             impl_->OnAddressedPlayerChanged(rawAddr, playerId, uidCounter, result);
224         }
225 
OnUidChanged(const RawAddress & rawAddr,uint16_t uidCounter,int result)226         void OnUidChanged(const RawAddress &rawAddr, uint16_t uidCounter, int result) override
227         {
228             impl_->OnUidChanged(rawAddr, uidCounter, result);
229         }
230 
OnVolumeChanged(const RawAddress & rawAddr,uint8_t volume,int result)231         void OnVolumeChanged(const RawAddress &rawAddr, uint8_t volume, int result) override
232         {
233             impl_->OnVolumeChanged(rawAddr, volume, result);
234         }
235 
236     private:
237         BluetoothAvrcpCtServer::impl *impl_;
238     };
239 
implOHOS::Bluetooth::BluetoothAvrcpCtServer::impl240     impl()
241     {
242         HILOGI("start.");
243 
244         auto svManager = IProfileManager::GetInstance();
245         service_ = static_cast<IProfileAvrcpCt *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
246         if (service_ != nullptr) {
247             observer_ = std::make_unique<ObserverImpl>(this);
248             service_->RegisterObserver(observer_.get());
249         }
250 
251         sysObserver_ = std::make_unique<SysStsObserverImpl>(this);
252         IAdapterManager::GetInstance()->RegisterSystemStateObserver(*sysObserver_);
253     }
254 
~implOHOS::Bluetooth::BluetoothAvrcpCtServer::impl255     ~impl()
256     {
257         HILOGI("start.");
258 
259         auto svManager = IProfileManager::GetInstance();
260         service_ = static_cast<IProfileAvrcpCt *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
261         if (service_ != nullptr) {
262             service_->UnregisterObserver();
263             observer_ = nullptr;
264             service_ = nullptr;
265         }
266 
267         IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*sysObserver_);
268         sysObserver_ = nullptr;
269     }
270 
IsEnabledOHOS::Bluetooth::BluetoothAvrcpCtServer::impl271     bool IsEnabled()
272     {
273         HILOGI("start.");
274 
275         auto servManager = IProfileManager::GetInstance();
276         service_ = static_cast<IProfileAvrcpCt *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
277 
278         return (service_ != nullptr && service_->IsEnabled());
279     }
280 
OnSystemStateChangeOHOS::Bluetooth::BluetoothAvrcpCtServer::impl281     void OnSystemStateChange(const BTSystemState state)
282     {
283         HILOGI("state: %{public}d", state);
284 
285         std::lock_guard<std::mutex> lock(serviceMutex_);
286 
287         switch (state) {
288             case BTSystemState::ON: {
289                 auto svManager = IProfileManager::GetInstance();
290                 service_ = static_cast<IProfileAvrcpCt *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
291                 if (service_ != nullptr) {
292                     observer_ = std::make_unique<ObserverImpl>(this);
293                     service_->RegisterObserver(observer_.get());
294                 }
295                 break;
296             }
297             case BTSystemState::OFF:
298                 /// FALL THROUGH
299             default:
300                 if (service_ != nullptr) {
301                     service_->UnregisterObserver();
302                     observer_ = nullptr;
303                     service_ = nullptr;
304                 }
305                 break;
306         }
307     }
308 
OnConnectionStateChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl309     void OnConnectionStateChanged(const RawAddress &rawAddr, int state)
310     {
311         HILOGI("address: %{public}s, state: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), state);
312         std::lock_guard<std::mutex> lock(observerMutex_);
313 
314         observers_.ForEach([rawAddr, state](IBluetoothAvrcpCtObserver *observer) {
315             observer->OnConnectionStateChanged(rawAddr, static_cast<int32_t>(state),
316                 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
317         });
318     }
319 
OnPressButtonOHOS::Bluetooth::BluetoothAvrcpCtServer::impl320     void OnPressButton(const RawAddress &rawAddr, uint8_t button, int result)
321     {
322         HILOGI("address: %{public}s, button: %{public}d, result: %{public}d",
323             GET_ENCRYPT_RAW_ADDR(rawAddr), button, result);
324 
325         std::lock_guard<std::mutex> lock(observerMutex_);
326 
327         observers_.ForEach([rawAddr, button, result](IBluetoothAvrcpCtObserver *observer) {
328             observer->OnPressButton(rawAddr,
329                 static_cast<int32_t>(button),
330                 static_cast<int32_t>(result));
331         });
332     }
333 
OnReleaseButtonOHOS::Bluetooth::BluetoothAvrcpCtServer::impl334     void OnReleaseButton(const RawAddress &rawAddr, uint8_t button, int result)
335     {
336         HILOGI("address: %{public}s, button: %{public}d, result: %{public}d",
337             GET_ENCRYPT_RAW_ADDR(rawAddr), button, result);
338 
339         std::lock_guard<std::mutex> lock(observerMutex_);
340 
341         observers_.ForEach([rawAddr, button, result](IBluetoothAvrcpCtObserver *observer) {
342             observer->OnReleaseButton(rawAddr,
343                 static_cast<int32_t>(button),
344                 static_cast<int32_t>(result));
345         });
346     }
347 
OnSetBrowsedPlayerOHOS::Bluetooth::BluetoothAvrcpCtServer::impl348     void OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems,
349         const std::vector<std::string> &folderNames, int result, int detail)
350     {
351         HILOGI("addr: %{public}s, res: %{public}d, detail: %{public}d",
352             GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
353 
354         std::lock_guard<std::mutex> lock(observerMutex_);
355 
356         observers_.ForEach(
357             [rawAddr, uidCounter, numOfItems, folderNames, result, detail](IBluetoothAvrcpCtObserver *observer) {
358                 observer->OnSetBrowsedPlayer(rawAddr,
359                     static_cast<int32_t>(uidCounter),
360                     static_cast<int32_t>(numOfItems),
361                     folderNames,
362                     static_cast<int32_t>(result),
363                     static_cast<int32_t>(detail));
364             });
365     }
366 
OnGetCapabilitiesOHOS::Bluetooth::BluetoothAvrcpCtServer::impl367     void OnGetCapabilities(const RawAddress &rawAddr, const std::vector<uint32_t> &companies,
368         const std::vector<uint8_t> &events, int result)
369     {
370         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
371 
372         std::lock_guard<std::mutex> lock(observerMutex_);
373 
374         observers_.ForEach([rawAddr, companies, events, result](IBluetoothAvrcpCtObserver *observer) {
375             observer->OnGetCapabilities(rawAddr,
376                 companies,
377                 events,
378                 static_cast<int32_t>(result));
379         });
380     }
381 
OnGetPlayerAppSettingAttributesOHOS::Bluetooth::BluetoothAvrcpCtServer::impl382     void OnGetPlayerAppSettingAttributes(const RawAddress &rawAddr, std::vector<uint8_t> attributes, int result)
383     {
384         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
385 
386         std::lock_guard<std::mutex> lock(observerMutex_);
387 
388         observers_.ForEach([rawAddr, attributes, result](IBluetoothAvrcpCtObserver *observer) {
389             observer->OnGetPlayerAppSettingAttributes(
390                 rawAddr, attributes, static_cast<int32_t>(result));
391         });
392     }
393 
OnGetPlayerAppSettingValuesOHOS::Bluetooth::BluetoothAvrcpCtServer::impl394     void OnGetPlayerAppSettingValues(
395         const RawAddress &rawAddr, uint8_t attribute, const std::vector<uint8_t> values, int result)
396     {
397         HILOGI("addr: %{public}s, attribute: %{public}d, res: %{public}d",
398             GET_ENCRYPT_RAW_ADDR(rawAddr), attribute, result);
399 
400         std::lock_guard<std::mutex> lock(observerMutex_);
401 
402         observers_.ForEach([rawAddr, attribute, values, result](IBluetoothAvrcpCtObserver *observer) {
403             observer->OnGetPlayerAppSettingValues(
404                 rawAddr, static_cast<int32_t>(attribute), values, static_cast<int32_t>(result));
405         });
406     }
407 
OnGetPlayerAppSettingCurrentValueOHOS::Bluetooth::BluetoothAvrcpCtServer::impl408     void OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
409         const std::vector<uint8_t> &values, int result)
410     {
411         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
412 
413         std::lock_guard<std::mutex> lock(observerMutex_);
414 
415         observers_.ForEach([rawAddr, attributes, values, result](IBluetoothAvrcpCtObserver *observer) {
416             observer->OnGetPlayerAppSettingCurrentValue(rawAddr,
417                 attributes,
418                 values,
419                 static_cast<int32_t>(result));
420         });
421     }
422 
OnSetPlayerAppSettingCurrentValueOHOS::Bluetooth::BluetoothAvrcpCtServer::impl423     void OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, int result)
424     {
425         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
426 
427         std::lock_guard<std::mutex> lock(observerMutex_);
428 
429         observers_.ForEach([rawAddr, result](IBluetoothAvrcpCtObserver *observer) {
430             observer->OnSetPlayerAppSettingCurrentValue(
431                 rawAddr, static_cast<int32_t>(result));
432         });
433     }
434 
OnGetPlayerAppSettingAttributeTextOHOS::Bluetooth::BluetoothAvrcpCtServer::impl435     void OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
436         const std::vector<std::string> &attributeName, int result)
437     {
438         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
439 
440         std::lock_guard<std::mutex> lock(observerMutex_);
441 
442         observers_.ForEach([rawAddr, attributes, attributeName, result](IBluetoothAvrcpCtObserver *observer) {
443             observer->OnGetPlayerAppSettingAttributeText(rawAddr,
444                 attributes,
445                 attributeName,
446                 static_cast<int32_t>(result));
447         });
448     }
449 
OnGetPlayerAppSettingValueTextOHOS::Bluetooth::BluetoothAvrcpCtServer::impl450     void OnGetPlayerAppSettingValueText(const RawAddress &rawAddr, const std::vector<uint8_t> &values,
451         const std::vector<std::string> &valueName, int result)
452     {
453         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
454 
455         std::lock_guard<std::mutex> lock(observerMutex_);
456 
457         observers_.ForEach([rawAddr, values, valueName, result](IBluetoothAvrcpCtObserver *observer) {
458             observer->OnGetPlayerAppSettingValueText(
459                 rawAddr, values, valueName, static_cast<int32_t>(result));
460         });
461     }
462 
OnGetElementAttributesOHOS::Bluetooth::BluetoothAvrcpCtServer::impl463     void OnGetElementAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
464         const std::vector<std::string> &valueName, int result)
465     {
466         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
467 
468         std::lock_guard<std::mutex> lock(observerMutex_);
469 
470         observers_.ForEach([rawAddr, attributes, valueName, result](IBluetoothAvrcpCtObserver *observer) {
471             observer->OnGetElementAttributes(rawAddr,
472                 attributes,
473                 valueName,
474                 static_cast<int32_t>(result));
475         });
476     }
477 
OnGetPlayStatusOHOS::Bluetooth::BluetoothAvrcpCtServer::impl478     void OnGetPlayStatus(
479         const RawAddress &rawAddr, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result)
480     {
481         HILOGI("addr: %{public}s, songLength: %{public}u, songPosition: %{public}u, playStatus: %{public}d, "
482             "res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), songLength, songPosition, playStatus, result);
483 
484         std::lock_guard<std::mutex> lock(observerMutex_);
485 
486         observers_.ForEach(
487             [rawAddr, songLength, songPosition, playStatus, result](IBluetoothAvrcpCtObserver *observer) {
488                 observer->OnGetPlayStatus(rawAddr,
489                     songLength,
490                     songPosition,
491                     static_cast<int32_t>(playStatus),
492                     static_cast<int32_t>(result));
493             });
494     }
495 
OnPlayItemOHOS::Bluetooth::BluetoothAvrcpCtServer::impl496     void OnPlayItem(const RawAddress &rawAddr, int result, int detail)
497     {
498         HILOGI("addr: %{public}s, res: %{public}d, detail: %{public}d",
499             GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
500 
501         std::lock_guard<std::mutex> lock(observerMutex_);
502 
503         observers_.ForEach([rawAddr, result, detail](IBluetoothAvrcpCtObserver *observer) {
504             observer->OnPlayItem(rawAddr,
505                 static_cast<int32_t>(result),
506                 static_cast<int32_t>(detail));
507         });
508     }
509 
OnGetMediaPlayersOHOS::Bluetooth::BluetoothAvrcpCtServer::impl510     void OnGetMediaPlayers(
511         const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMpItem> &items, int result, int detail)
512     {
513         HILOGI("addr: %{public}s, res: %{public}d, detail: %{public}d",
514             GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
515 
516         std::lock_guard<std::mutex> lock(observerMutex_);
517 
518         observers_.ForEach([rawAddr, uidCounter, items, result, detail](IBluetoothAvrcpCtObserver *observer) {
519             std::vector<BluetoothAvrcpMpItem> myItems;
520             for (size_t i = 0; i < items.size(); i++) {
521                 myItems.push_back(BluetoothAvrcpMpItem(items.at(i)));
522             }
523             observer->OnGetMediaPlayers(rawAddr,
524                 static_cast<int32_t>(uidCounter),
525                 myItems,
526                 static_cast<int32_t>(result),
527                 static_cast<int32_t>(detail));
528         });
529     }
530 
OnGetFolderItemsOHOS::Bluetooth::BluetoothAvrcpCtServer::impl531     void OnGetFolderItems(
532         const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMeItem> &items, int result, int detail)
533     {
534         HILOGI("addr: %{public}s, uidCounter: %{public}hu, res: %{public}d, detail: %{public}d",
535             GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result, detail);
536 
537         std::lock_guard<std::mutex> lock(observerMutex_);
538 
539         observers_.ForEach([rawAddr, uidCounter, items, result, detail](IBluetoothAvrcpCtObserver *observer) {
540             std::vector<BluetoothAvrcpMeItem> myItems;
541             for (size_t i = 0; i < items.size(); i++) {
542                 myItems.push_back(BluetoothAvrcpMeItem(items.at(i)));
543             }
544             observer->OnGetFolderItems(rawAddr,
545                 static_cast<int32_t>(uidCounter),
546                 myItems,
547                 static_cast<int32_t>(result),
548                 static_cast<int32_t>(detail));
549         });
550     }
551 
OnGetItemAttributesOHOS::Bluetooth::BluetoothAvrcpCtServer::impl552     void OnGetItemAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
553         const std::vector<std::string> &values, int result, int detail)
554     {
555         HILOGI("addr: %{public}s, res: %{public}d, detail: %{public}d",
556             GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail);
557 
558         std::lock_guard<std::mutex> lock(observerMutex_);
559 
560         observers_.ForEach([rawAddr, attributes, values, result, detail](IBluetoothAvrcpCtObserver *observer) {
561             observer->OnGetItemAttributes(rawAddr,
562                 attributes,
563                 values,
564                 static_cast<int32_t>(result),
565                 static_cast<int32_t>(detail));
566         });
567     }
568 
OnGetTotalNumberOfItemsOHOS::Bluetooth::BluetoothAvrcpCtServer::impl569     void OnGetTotalNumberOfItems(
570         const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems, int result, int detail)
571     {
572         HILOGI("addr: %{public}s, uidCounter: %{public}hu, numOfItems: %{public}u, res: %{public}d, detail: %{public}d",
573             GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, numOfItems, result, detail);
574 
575         std::lock_guard<std::mutex> lock(observerMutex_);
576 
577         observers_.ForEach([rawAddr, uidCounter, numOfItems, result, detail](IBluetoothAvrcpCtObserver *observer) {
578             observer->OnGetTotalNumberOfItems(rawAddr,
579                 static_cast<int32_t>(uidCounter),
580                 numOfItems,
581                 static_cast<int32_t>(result),
582                 static_cast<int32_t>(detail));
583         });
584     }
585 
OnSetAbsoluteVolumeOHOS::Bluetooth::BluetoothAvrcpCtServer::impl586     void OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, int result)
587     {
588         HILOGI("addr: %{public}s, volume: %{public}d, res: %{public}d",
589             GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result);
590 
591         std::lock_guard<std::mutex> lock(observerMutex_);
592 
593         observers_.ForEach([rawAddr, volume, result](IBluetoothAvrcpCtObserver *observer) {
594             observer->OnSetAbsoluteVolume(rawAddr,
595                 static_cast<int32_t>(volume),
596                 static_cast<int32_t>(result));
597         });
598     }
599 
OnPlaybackStatusChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl600     void OnPlaybackStatusChanged(const RawAddress &rawAddr, uint8_t playStatus, int result)
601     {
602         HILOGI("addr: %{public}s, playStatus: %{public}d, res: %{public}d",
603             GET_ENCRYPT_RAW_ADDR(rawAddr), playStatus, result);
604 
605         std::lock_guard<std::mutex> lock(observerMutex_);
606 
607         observers_.ForEach([rawAddr, playStatus, result](IBluetoothAvrcpCtObserver *observer) {
608             observer->OnPlaybackStatusChanged(rawAddr,
609                 static_cast<int32_t>(playStatus),
610                 static_cast<int32_t>(result));
611         });
612     }
613 
OnTrackChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl614     void OnTrackChanged(const RawAddress &rawAddr, uint64_t uid, int result)
615     {
616         HILOGI("addr: %{public}s, uid: %{public}llu, res: %{public}d",
617             GET_ENCRYPT_RAW_ADDR(rawAddr), (unsigned long long)uid, result);
618 
619         std::lock_guard<std::mutex> lock(observerMutex_);
620 
621         observers_.ForEach([rawAddr, uid, result](IBluetoothAvrcpCtObserver *observer) {
622             observer->OnTrackChanged(rawAddr,
623                 uid,
624                 static_cast<int32_t>(result));
625         });
626     }
627 
OnTrackReachedEndOHOS::Bluetooth::BluetoothAvrcpCtServer::impl628     void OnTrackReachedEnd(const RawAddress &rawAddr, int result)
629     {
630         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
631 
632         std::lock_guard<std::mutex> lock(observerMutex_);
633 
634         observers_.ForEach([rawAddr, result](IBluetoothAvrcpCtObserver *observer) {
635             observer->OnTrackReachedEnd(
636                 rawAddr, static_cast<int32_t>(result));
637         });
638     }
639 
OnTrackReachedStartOHOS::Bluetooth::BluetoothAvrcpCtServer::impl640     void OnTrackReachedStart(const RawAddress &rawAddr, int result)
641     {
642         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
643 
644         std::lock_guard<std::mutex> lock(observerMutex_);
645 
646         observers_.ForEach([rawAddr, result](IBluetoothAvrcpCtObserver *observer) {
647             observer->OnTrackReachedStart(
648                 rawAddr, static_cast<int32_t>(result));
649         });
650     }
651 
OnPlaybackPosChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl652     void OnPlaybackPosChanged(const RawAddress &rawAddr, uint32_t playbackPos, int result)
653     {
654         HILOGI("addr: %{public}s, playbackPos: %{public}u, res: %{public}d",
655             GET_ENCRYPT_RAW_ADDR(rawAddr), playbackPos, result);
656 
657         std::lock_guard<std::mutex> lock(observerMutex_);
658 
659         observers_.ForEach([rawAddr, playbackPos, result](IBluetoothAvrcpCtObserver *observer) {
660             observer->OnPlaybackPosChanged(rawAddr,
661                 playbackPos,
662                 static_cast<int32_t>(result));
663         });
664     }
665 
OnPlayerAppSettingChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl666     void OnPlayerAppSettingChanged(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes,
667         const std::vector<uint8_t> &values, int result)
668     {
669         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
670 
671         std::lock_guard<std::mutex> lock(observerMutex_);
672 
673         observers_.ForEach([rawAddr, attributes, values, result](IBluetoothAvrcpCtObserver *observer) {
674             observer->OnPlayerAppSettingChanged(rawAddr,
675                 attributes,
676                 values,
677                 static_cast<int32_t>(result));
678         });
679     }
680 
OnNowPlayingContentChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl681     void OnNowPlayingContentChanged(const RawAddress &rawAddr, int result)
682     {
683         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
684 
685         std::lock_guard<std::mutex> lock(observerMutex_);
686 
687         observers_.ForEach([rawAddr, result](IBluetoothAvrcpCtObserver *observer) {
688             observer->OnNowPlayingContentChanged(
689                 rawAddr, static_cast<int32_t>(result));
690         });
691     }
692 
OnAvailablePlayersChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl693     void OnAvailablePlayersChanged(const RawAddress &rawAddr, int result)
694     {
695         HILOGI("addr: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result);
696 
697         std::lock_guard<std::mutex> lock(observerMutex_);
698 
699         observers_.ForEach([rawAddr, result](IBluetoothAvrcpCtObserver *observer) {
700             observer->OnAvailablePlayersChanged(
701                 rawAddr, static_cast<int32_t>(result));
702         });
703     }
704 
OnAddressedPlayerChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl705     void OnAddressedPlayerChanged(const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, int result)
706     {
707         HILOGI("addr: %{public}s, playerId: %{public}hu, uidCounter: %{public}hu, res: %{public}d",
708             GET_ENCRYPT_RAW_ADDR(rawAddr), playerId, uidCounter, result);
709 
710         std::lock_guard<std::mutex> lock(observerMutex_);
711 
712         observers_.ForEach([rawAddr, playerId, uidCounter, result](IBluetoothAvrcpCtObserver *observer) {
713             observer->OnAddressedPlayerChanged(rawAddr,
714                 static_cast<int32_t>(playerId),
715                 static_cast<int32_t>(uidCounter),
716                 static_cast<int32_t>(result));
717         });
718     }
719 
OnUidChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl720     void OnUidChanged(const RawAddress &rawAddr, uint16_t uidCounter, int result)
721     {
722         HILOGI("addr: %{public}s, uidCounter: %{public}hu, res: %{public}d",
723             GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result);
724 
725         std::lock_guard<std::mutex> lock(observerMutex_);
726 
727         observers_.ForEach([rawAddr, uidCounter, result](IBluetoothAvrcpCtObserver *observer) {
728             observer->OnUidChanged(rawAddr,
729                 static_cast<int32_t>(uidCounter),
730                 static_cast<int32_t>(result));
731         });
732     }
733 
OnVolumeChangedOHOS::Bluetooth::BluetoothAvrcpCtServer::impl734     void OnVolumeChanged(const RawAddress &rawAddr, uint8_t volume, int result)
735     {
736         HILOGI("addr: %{public}s, volume: %{public}d, res: %{public}d",
737             GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result);
738 
739         std::lock_guard<std::mutex> lock(observerMutex_);
740 
741         observers_.ForEach([rawAddr, volume, result](IBluetoothAvrcpCtObserver *observer) {
742             observer->OnVolumeChanged(rawAddr,
743                 static_cast<int32_t>(volume),
744                 static_cast<int32_t>(result));
745         });
746     }
747 
748     std::mutex serviceMutex_;
749     IProfileAvrcpCt *service_;
750 
751     std::mutex observerMutex_;
752     RemoteObserverList<IBluetoothAvrcpCtObserver> observers_;
753 
754     std::unique_ptr<ObserverImpl> observer_;
755     std::unique_ptr<SysStsObserverImpl> sysObserver_;
756 };
757 
BluetoothAvrcpCtServer()758 BluetoothAvrcpCtServer::BluetoothAvrcpCtServer()
759 {
760     HILOGI("start.");
761 
762     pimpl = std::make_unique<impl>();
763 }
764 
~BluetoothAvrcpCtServer()765 BluetoothAvrcpCtServer::~BluetoothAvrcpCtServer()
766 {
767     pimpl = nullptr;
768 }
769 
RegisterObserver(const sptr<IBluetoothAvrcpCtObserver> & observer)770 void BluetoothAvrcpCtServer::RegisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)
771 {
772     HILOGI("start.");
773 
774     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
775 
776     if (observer == nullptr) {
777         HILOGI("observer is NULL.");
778         return ;
779     }
780     auto func = std::bind(&BluetoothAvrcpCtServer::UnregisterObserver, this, std::placeholders::_1);
781     pimpl->observers_.Register(observer, func);
782     HILOGI("end.");
783 
784     return ;
785 }
786 
UnregisterObserver(const sptr<IBluetoothAvrcpCtObserver> & observer)787 void BluetoothAvrcpCtServer::UnregisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)
788 {
789     HILOGI("start.");
790 
791     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
792 
793     if (observer == nullptr) {
794         HILOGI("observer is NULL.");
795         return;
796     }
797     pimpl->observers_.Deregister(observer);
798     HILOGI("end.");
799 
800     return;
801 }
802 
GetConnectedDevices()803 std::vector<RawAddress> BluetoothAvrcpCtServer::GetConnectedDevices()
804 {
805     HILOGI("start.");
806     std::vector<RawAddress> devices {};
807     if (pimpl->IsEnabled()) {
808         devices = pimpl->service_->GetConnectedDevices();
809     } else {
810         HILOGE("service is null or disable");
811     }
812     HILOGI("end.");
813 
814     return devices;
815 }
816 
GetDevicesByStates(const std::vector<int32_t> & states)817 std::vector<RawAddress> BluetoothAvrcpCtServer::GetDevicesByStates(const std::vector<int32_t> &states)
818 {
819     HILOGI("start.");
820     std::vector<RawAddress> devices {};
821     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
822         HILOGE("false, check permission failed");
823         return devices;
824     }
825     if (pimpl->IsEnabled()) {
826         std::vector<int> convertStates;
827         for (auto state : states) {
828             convertStates.push_back(static_cast<int>(state));
829         }
830 
831         devices = pimpl->service_->GetDevicesByStates(convertStates);
832     } else {
833         HILOGE("service is null or disable");
834     }
835     HILOGI("end.");
836     return devices;
837 }
838 
GetDeviceState(const RawAddress & device)839 int32_t BluetoothAvrcpCtServer::GetDeviceState(const RawAddress &device)
840 {
841     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
842     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
843         HILOGE("false, check permission failed");
844         return BT_FAILURE;
845     }
846     int32_t result = 0;
847 
848     if (pimpl->IsEnabled()) {
849         result = pimpl->service_->GetDeviceState(BluetoothRawAddress(device));
850     } else {
851         HILOGE("service is null or disable");
852     }
853     HILOGI("end.");
854 
855     return result;
856 }
857 
Connect(const RawAddress & device)858 int32_t BluetoothAvrcpCtServer::Connect(const RawAddress &device)
859 {
860     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
861     int32_t result = 0;
862 
863     if (pimpl->IsEnabled()) {
864         result = pimpl->service_->Connect(BluetoothRawAddress(device));
865     } else {
866         HILOGE("service is null or disable");
867     }
868     HILOGI("end.");
869 
870     return result;
871 }
872 
Disconnect(const RawAddress & device)873 int32_t BluetoothAvrcpCtServer::Disconnect(const RawAddress &device)
874 {
875     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
876     int32_t result = 0;
877 
878     if (pimpl->IsEnabled()) {
879         result = pimpl->service_->Disconnect(BluetoothRawAddress(device));
880     } else {
881         HILOGE("service is null or disable");
882     }
883 
884     HILOGI("end.");
885     return result;
886 }
887 
PressButton(const RawAddress & device,int32_t button)888 int32_t BluetoothAvrcpCtServer::PressButton(const RawAddress &device, int32_t button)
889 {
890     HILOGI("addr: %{public}s, button: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), button);
891     int32_t result = 0;
892 
893     if (pimpl->IsEnabled()) {
894         result = pimpl->service_->PressButton(BluetoothRawAddress(device), static_cast<uint8_t>(button));
895     } else {
896         HILOGE("service is null or disable");
897     }
898     HILOGI("end.");
899 
900     return result;
901 }
902 
ReleaseButton(const RawAddress & device,int32_t button)903 int32_t BluetoothAvrcpCtServer::ReleaseButton(const RawAddress &device, int32_t button)
904 {
905     HILOGI("addr: %{public}s, button: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), button);
906     int32_t result = 0;
907 
908     if (pimpl->IsEnabled()) {
909         result = pimpl->service_->ReleaseButton(BluetoothRawAddress(device), static_cast<uint8_t>(button));
910     } else {
911         HILOGE("service is null or disable");
912     }
913     HILOGI("end.");
914 
915     return result;
916 }
917 
GetUnitInfo(const RawAddress & device)918 int32_t BluetoothAvrcpCtServer::GetUnitInfo(const RawAddress &device)
919 {
920     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
921     int32_t result = 0;
922 
923     if (pimpl->IsEnabled()) {
924         result = pimpl->service_->GetUnitInfo(BluetoothRawAddress(device));
925     } else {
926         HILOGE("service is null or disable");
927     }
928     HILOGI("end.");
929 
930     return result;
931 }
932 
GetSubUnitInfo(const RawAddress & device)933 int32_t BluetoothAvrcpCtServer::GetSubUnitInfo(const RawAddress &device)
934 {
935     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
936     int32_t result = 0;
937 
938     if (pimpl->IsEnabled()) {
939         result = pimpl->service_->GetSubUnitInfo(BluetoothRawAddress(device));
940     } else {
941         HILOGE("service is null or disable");
942     }
943     HILOGI("end.");
944 
945     return result;
946 }
947 
GetSupportedCompanies(const RawAddress & device)948 int32_t BluetoothAvrcpCtServer::GetSupportedCompanies(const RawAddress &device)
949 {
950     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
951     int32_t result = 0;
952 
953     if (pimpl->IsEnabled()) {
954         result = pimpl->service_->GetSupportedCompanies(BluetoothRawAddress(device));
955     } else {
956         HILOGE("service is null or disable");
957     }
958     HILOGI("end.");
959 
960     return result;
961 }
962 
GetSupportedEvents(const RawAddress & device)963 int32_t BluetoothAvrcpCtServer::GetSupportedEvents(const RawAddress &device)
964 {
965     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
966     int32_t result = 0;
967 
968     if (pimpl->IsEnabled()) {
969         result = pimpl->service_->GetSupportedEvents(device);
970     } else {
971         HILOGE("service is null or disable");
972     }
973     HILOGI("end.");
974 
975     return result;
976 }
977 
GetPlayerAppSettingAttributes(const RawAddress & device)978 int32_t BluetoothAvrcpCtServer::GetPlayerAppSettingAttributes(const RawAddress &device)
979 {
980     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
981     int32_t result = 0;
982 
983     if (pimpl->IsEnabled()) {
984         result = pimpl->service_->GetPlayerAppSettingAttributes(BluetoothRawAddress(device));
985     } else {
986         HILOGE("service is null or disable");
987     }
988     HILOGI("end.");
989 
990     return result;
991 }
992 
GetPlayerAppSettingValues(const RawAddress & device,int32_t attribute)993 int32_t BluetoothAvrcpCtServer::GetPlayerAppSettingValues(const RawAddress &device, int32_t attribute)
994 {
995     HILOGI("addr: %{public}s, attribute: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), attribute);
996     int32_t result = 0;
997 
998     if (pimpl->IsEnabled()) {
999         result = pimpl->service_->GetPlayerAppSettingValues(BluetoothRawAddress(device),
1000             static_cast<uint8_t>(attribute));
1001     } else {
1002         HILOGE("service is null or disable");
1003     }
1004     HILOGI("end.");
1005 
1006     return result;
1007 }
1008 
GetPlayerAppSettingCurrentValue(const RawAddress & device,const std::vector<int32_t> & attributes)1009 int32_t BluetoothAvrcpCtServer::GetPlayerAppSettingCurrentValue(const RawAddress &device,
1010     const std::vector<int32_t> &attributes)
1011 {
1012     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1013     int32_t result = 0;
1014     std::vector<uint8_t> myAttributes;
1015 
1016     for (auto attribute : attributes) {
1017         HILOGI("attribute: %{public}d", attribute);
1018         myAttributes.push_back(static_cast<uint8_t>(attribute));
1019     }
1020 
1021     if (pimpl->IsEnabled()) {
1022         result = pimpl->service_->GetPlayerAppSettingCurrentValue(BluetoothRawAddress(device), myAttributes);
1023     } else {
1024         HILOGE("service is null or disable");
1025     }
1026     HILOGI("end.");
1027 
1028     return result;
1029 }
1030 
SetPlayerAppSettingCurrentValue(const RawAddress & device,const std::vector<int32_t> & attributes,const std::vector<int32_t> & values)1031 int32_t BluetoothAvrcpCtServer::SetPlayerAppSettingCurrentValue(const RawAddress &device,
1032     const std::vector<int32_t> &attributes, const std::vector<int32_t> &values)
1033 {
1034     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1035     int32_t result = 0;
1036     std::vector<uint8_t> myAttributes;
1037     std::vector<uint8_t> myValues;
1038     for (auto attribute : attributes) {
1039         HILOGI("attribute: %{public}d", attribute);
1040         myAttributes.push_back(static_cast<uint8_t>(attribute));
1041     }
1042     for (auto value : values) {
1043         HILOGI("value: %{public}d", value);
1044         myValues.push_back(static_cast<uint8_t>(value));
1045     }
1046 
1047     if (pimpl->IsEnabled()) {
1048         result = pimpl->service_->SetPlayerAppSettingCurrentValue(BluetoothRawAddress(device), myAttributes, myValues);
1049     } else {
1050         HILOGE("service is null or disable");
1051     }
1052     HILOGI("end.");
1053 
1054     return result;
1055 }
1056 
GetPlayerAppSettingAttributeText(const RawAddress & device,const std::vector<int32_t> & attributes)1057 int32_t BluetoothAvrcpCtServer::GetPlayerAppSettingAttributeText(const RawAddress &device,
1058     const std::vector<int32_t> &attributes)
1059 {
1060     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1061     int32_t result = 0;
1062     if (pimpl->IsEnabled()) {
1063         std::vector<uint8_t> attrs;
1064         for (auto attribute : attributes) {
1065             HILOGI("attribute: %{public}d", attribute);
1066             attrs.push_back(static_cast<uint8_t>(attribute));
1067         }
1068 
1069         result = pimpl->service_->GetPlayerAppSettingAttributeText(BluetoothRawAddress(device), attrs);
1070     } else {
1071         HILOGE("service is null or disable");
1072     }
1073     HILOGI("end.");
1074 
1075     return result;
1076 }
1077 
GetPlayerAppSettingValueText(const RawAddress & device,int32_t attributes,const std::vector<int32_t> & values)1078 int32_t BluetoothAvrcpCtServer::GetPlayerAppSettingValueText(const RawAddress &device, int32_t attributes,
1079     const std::vector<int32_t> &values)
1080 {
1081     HILOGI("addr: %{public}s, attributes: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), attributes);
1082     int32_t result = 0;
1083 
1084     if (pimpl->IsEnabled()) {
1085         std::vector<uint8_t> myValues;
1086         for (auto value : values) {
1087             HILOGI("value: %{public}d", value);
1088             myValues.push_back(static_cast<uint8_t>(value));
1089         }
1090 
1091         result =
1092             pimpl->service_->GetPlayerAppSettingValueText(BluetoothRawAddress(device),
1093                 static_cast<uint8_t>(attributes), myValues);
1094     } else {
1095         HILOGE("service is null or disable");
1096     }
1097     HILOGI("end.");
1098 
1099     return result;
1100 }
1101 
GetElementAttributes(const RawAddress & device,const std::vector<int32_t> & attributes)1102 int32_t BluetoothAvrcpCtServer::GetElementAttributes(const RawAddress &device,
1103     const std::vector<int32_t> &attributes)
1104 {
1105     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1106     int32_t result = 0;
1107 
1108     if (pimpl->IsEnabled()) {
1109         std::vector<uint32_t> attrs;
1110         for (auto attribute : attributes) {
1111             HILOGI("attribute: %{public}d", attribute);
1112             attrs.push_back(static_cast<uint32_t>(attribute));
1113         }
1114 
1115         result = pimpl->service_->GetElementAttributes(
1116             BluetoothRawAddress(device), AVRC_ELEMENT_ATTRIBUTES_IDENTIFIER_PLAYING, attrs);
1117     } else {
1118         HILOGE("service is null or disable");
1119     }
1120     HILOGI("end.");
1121 
1122     return result;
1123 }
1124 
GetPlayStatus(const RawAddress & device)1125 int32_t BluetoothAvrcpCtServer::GetPlayStatus(const RawAddress &device)
1126 {
1127     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1128     int32_t result = 0;
1129 
1130     if (pimpl->IsEnabled()) {
1131         result = pimpl->service_->GetPlayStatus(BluetoothRawAddress(device));
1132     } else {
1133         HILOGE("service is null or disable");
1134     }
1135     HILOGI("end.");
1136 
1137     return result;
1138 }
1139 
PlayItem(const RawAddress & device,int32_t scope,int64_t uid,int32_t uidCounter)1140 int32_t BluetoothAvrcpCtServer::PlayItem(const RawAddress &device, int32_t scope, int64_t uid, int32_t uidCounter)
1141 {
1142     HILOGI("res: %{public}s, scope: %{public}d, uid: %{public}jd, uidCounter: %{public}d",
1143         GetEncryptAddr(device.GetAddress()).c_str(), scope, uid, uidCounter);
1144     int32_t result = 0;
1145 
1146     if (pimpl->IsEnabled()) {
1147         result = pimpl->service_->PlayItem(
1148             BluetoothRawAddress(device), static_cast<uint8_t>(scope),
1149             static_cast<uint64_t>(uid), static_cast<uint16_t>(uidCounter));
1150     } else {
1151         HILOGE("service is null or disable");
1152     }
1153     HILOGI("end.");
1154 
1155     return result;
1156 }
1157 
GetFolderItems(const RawAddress & device,int32_t startItem,int32_t endItem,const std::vector<int32_t> & attributes)1158 int32_t BluetoothAvrcpCtServer::GetFolderItems(const RawAddress &device, int32_t startItem, int32_t endItem,
1159     const std::vector<int32_t> &attributes)
1160 {
1161     HILOGI("res: %{public}s, startItem: %{public}d, endItem: %{public}d",
1162         GetEncryptAddr(device.GetAddress()).c_str(), startItem, endItem);
1163     int32_t result = 0;
1164     if (pimpl->IsEnabled()) {
1165         std::vector<uint32_t> attrs;
1166         for (auto attribute : attributes) {
1167             HILOGI("attribute: %{public}d,", attribute);
1168             attrs.push_back(static_cast<uint32_t>(attribute));
1169         }
1170         result = pimpl->service_->GetFolderItems(BluetoothRawAddress(device),
1171             AVRC_MEDIA_SCOPE_NOW_PLAYING,
1172             static_cast<uint32_t>(startItem),
1173             static_cast<uint32_t>(endItem),
1174             attrs);
1175     } else {
1176         HILOGE("service is null or disable");
1177     }
1178     HILOGI("end.");
1179 
1180     return result;
1181 }
1182 
GetTotalNumberOfItems(const RawAddress & device,int32_t scope)1183 int32_t BluetoothAvrcpCtServer::GetTotalNumberOfItems(const RawAddress &device, int32_t scope)
1184 {
1185     HILOGI("addr: %{public}s, scope: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), scope);
1186     int32_t result = 0;
1187 
1188     if (pimpl->IsEnabled()) {
1189         result = pimpl->service_->GetTotalNumberOfItems(BluetoothRawAddress(device), static_cast<uint8_t>(scope));
1190     } else {
1191         HILOGE("service is null or disable");
1192     }
1193     HILOGI("end.");
1194 
1195     return result;
1196 }
1197 
SetAbsoluteVolume(const RawAddress & device,int32_t volume)1198 int32_t BluetoothAvrcpCtServer::SetAbsoluteVolume(const RawAddress &device, int32_t volume)
1199 {
1200     HILOGI("addr: %{public}s, volume: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), volume);
1201     int32_t result = 0;
1202 
1203     if (pimpl->IsEnabled()) {
1204         result = pimpl->service_->SetAbsoluteVolume(BluetoothRawAddress(device), static_cast<uint8_t>(volume));
1205     } else {
1206         HILOGE("service is null or disable");
1207     }
1208     HILOGI("end.");
1209 
1210     return result;
1211 }
1212 
EnableNotification(const RawAddress & device,const std::vector<int32_t> & events,int32_t interval)1213 int32_t BluetoothAvrcpCtServer::EnableNotification(const RawAddress &device,
1214     const std::vector<int32_t> &events, int32_t interval)
1215 {
1216     HILOGI("addr: %{public}s, interval: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), interval);
1217     int32_t result = 0;
1218 
1219     std::vector<uint8_t> myEvents;
1220     for (auto event : events) {
1221         HILOGI("event: %{public}d", event);
1222         myEvents.push_back(static_cast<uint8_t>(event));
1223     }
1224 
1225     if (pimpl->IsEnabled()) {
1226         result = pimpl->service_->EnableNotification(BluetoothRawAddress(device),
1227             myEvents, static_cast<uint32_t>(interval));
1228     } else {
1229         HILOGE("service is null or disable");
1230     }
1231     HILOGI("end.");
1232 
1233     return result;
1234 }
1235 
DisableNotification(const RawAddress & device,const std::vector<int32_t> & events)1236 int32_t BluetoothAvrcpCtServer::DisableNotification(const RawAddress &device, const std::vector<int32_t> &events)
1237 {
1238     HILOGI("addr: %{public}s", GetEncryptAddr(device.GetAddress()).c_str());
1239     int32_t result = 0;
1240 
1241     std::vector<uint8_t> myEvents;
1242     for (auto event : events) {
1243         HILOGI("event: %{public}d,", event);
1244         myEvents.push_back(static_cast<uint8_t>(event));
1245     }
1246 
1247     if (pimpl->IsEnabled()) {
1248         result = pimpl->service_->DisableNotification(BluetoothRawAddress(device), myEvents);
1249     } else {
1250         HILOGE("service is null or disable");
1251     }
1252     HILOGI("end.");
1253 
1254     return result;
1255 }
1256 
GetItemAttributes(const RawAddress & device,int64_t uid,int32_t uidCounter,const std::vector<int32_t> & attributes)1257 int32_t BluetoothAvrcpCtServer::GetItemAttributes(const RawAddress &device, int64_t uid, int32_t uidCounter,
1258     const std::vector<int32_t> &attributes)
1259 {
1260     HILOGI("res: %{public}s, uid: %{public}jd, uidCounter: %{public}d",
1261         GetEncryptAddr(device.GetAddress()).c_str(), uid, uidCounter);
1262     int32_t result = 0;
1263 
1264     std::vector<uint32_t> myAttribtues;
1265     for (auto attribue : attributes) {
1266         HILOGI("attribue = %{public}d", attribue);
1267         myAttribtues.push_back(static_cast<uint32_t>(attribue));
1268     }
1269 
1270     if (pimpl->IsEnabled()) {
1271         result = pimpl->service_->GetItemAttributes(BluetoothRawAddress(device),
1272             AVRC_MEDIA_SCOPE_NOW_PLAYING, (uint64_t)uid, (uint16_t)uidCounter, myAttribtues);
1273     } else {
1274         HILOGE("service is null or disable");
1275     }
1276     HILOGI("end.");
1277 
1278     return result;
1279 }
1280 
SetBrowsedPlayer(const RawAddress & device,int32_t playerId)1281 int32_t BluetoothAvrcpCtServer::SetBrowsedPlayer(const RawAddress &device, int32_t playerId)
1282 {
1283     HILOGI("addr: %{public}s, playerId: %{public}d", GetEncryptAddr(device.GetAddress()).c_str(), playerId);
1284     int32_t result = 0;
1285 
1286     if (pimpl->IsEnabled()) {
1287         result = pimpl->service_->SetBrowsedPlayer(BluetoothRawAddress(device), (uint16_t)playerId);
1288     } else {
1289         HILOGE("service is null or disable");
1290     }
1291     HILOGI("end.");
1292 
1293     return result;
1294 }
1295 
GetMeidaPlayerList(const RawAddress & device,int32_t startItem,int32_t endItem)1296 int32_t BluetoothAvrcpCtServer::GetMeidaPlayerList(const RawAddress &device, int32_t startItem, int32_t endItem)
1297 {
1298     HILOGI("res: %{public}s: startItem: %{public}d, endItem: %{public}d",
1299         GetEncryptAddr(device.GetAddress()).c_str(), startItem, endItem);
1300     int32_t result = 0;
1301     std::vector<uint32_t> attrs;
1302 
1303     if (pimpl->IsEnabled()) {
1304         result = pimpl->service_->GetFolderItems(BluetoothRawAddress(device),
1305             AVRC_MEDIA_SCOPE_PLAYER_LIST,
1306             static_cast<uint32_t>(startItem),
1307             static_cast<uint32_t>(endItem),
1308             attrs);
1309     } else {
1310         HILOGE("service is null or disable");
1311     }
1312     HILOGI("end.");
1313     return result;
1314 }
1315 }  // namespace Bluetooth
1316 }  // namespace OHOS
1317