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