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