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