1 /*
2 * Copyright (C) 2021-2022 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 "avrcp_ct_service.h"
17 #include "adapter_config.h"
18 #include "avrcp_ct_internal.h"
19 #include "class_creator.h"
20 #include "profile_service_manager.h"
21
22 namespace OHOS {
23 namespace bluetooth {
AvrcpCtService()24 AvrcpCtService::AvrcpCtService() : utility::Context(PROFILE_NAME_AVRCP_CT, "1.6.2")
25 {
26 HILOGI("enter");
27 /// Need to set the features of the AVRCP CT service.
28 InitFeatures();
29 using namespace std::placeholders;
30 AvrcCtProfile::Observer observer = {
31 std::bind(&AvrcpCtService::OnProfileDisabled, this, RET_NO_ERROR),
32 std::bind(&AvrcpCtService::OnConnectionStateChanged, this, _1, _2),
33 std::bind(&AvrcpCtService::OnButtonPressed, this, _1, _2, _3),
34 std::bind(&AvrcpCtService::OnButtonReleased, this, _1, _2, _3),
35 std::bind(&AvrcpCtService::OnGetCapabilities, this, _1, _2, _3, _4),
36 std::bind(&AvrcpCtService::OnGetPlayerAppSettingAttribtues, this, _1, _2, _3),
37 std::bind(&AvrcpCtService::OnGetPlayerAppSettingValues, this, _1, _2, _3, _4),
38 std::bind(&AvrcpCtService::OnGetPlayerAppSettingCurrentValue, this, _1, _2, _3, _4),
39 std::bind(&AvrcpCtService::OnSetPlayerAppSettingCurrentValue, this, _1, _2),
40 std::bind(&AvrcpCtService::OnGetPlayerAppSettingAttributeText, this, _1, _2, _3, _4),
41 std::bind(&AvrcpCtService::OnGetPlayerAppSettingValueText, this, _1, _2, _3, _4),
42 std::bind(&AvrcpCtService::OnGetElementAttributes, this, _1, _2, _3, _4),
43 std::bind(&AvrcpCtService::OnGetPlayStatus, this, _1, _2, _3, _4, _5),
44 std::bind(&AvrcpCtService::OnSetAddressedPlayer, this, _1, _2, _3),
45 std::bind(&AvrcpCtService::OnSetBrowsedPlayer, this, _1, _2, _3, _4, _5, _6),
46 std::bind(&AvrcpCtService::OnChangePath, this, _1, _2, _3, _4),
47 std::bind(&AvrcpCtService::OnGetFolderItems, this, _1, _2, _3, _4, _5, _6, _7),
48 std::bind(&AvrcpCtService::OnGetItemAttributes, this, _1, _2, _3, _4, _5),
49 std::bind(&AvrcpCtService::OnGetTotalNumberOfItems, this, _1, _2, _3, _4, _5),
50 std::bind(&AvrcpCtService::OnPlayItem, this, _1, _2, _3),
51 std::bind(&AvrcpCtService::OnAddToNowPlaying, this, _1, _2, _3),
52 std::bind(&AvrcpCtService::OnSetAbsoluteVolume, this, _1, _2, _3),
53 std::bind(&AvrcpCtService::OnPlaybackStatusChanged, this, _1, _2, _3),
54 std::bind(&AvrcpCtService::OnTrackChanged, this, _1, _2, _3),
55 std::bind(&AvrcpCtService::OnTrackReachedEnd, this, _1, _2),
56 std::bind(&AvrcpCtService::OnTrackReachedStart, this, _1, _2),
57 std::bind(&AvrcpCtService::OnPlaybackPosChanged, this, _1, _2, _3),
58 std::bind(&AvrcpCtService::OnPlayerApplicationSettingChanged, this, _1, _2, _3, _4),
59 std::bind(&AvrcpCtService::OnNowPlayingContentChanged, this, _1, _2),
60 std::bind(&AvrcpCtService::OnAvailablePlayersChanged, this, _1, _2),
61 std::bind(&AvrcpCtService::OnAddressedPlayerChanged, this, _1, _2, _3, _4),
62 std::bind(&AvrcpCtService::OnUidChanged, this, _1, _2, _3),
63 std::bind(&AvrcpCtService::OnVolumeChanged, this, _1, _2, _3),
64 };
65 pfObserver_ = std::make_unique<AvrcCtProfile::Observer>(observer);
66 }
67
~AvrcpCtService()68 AvrcpCtService::~AvrcpCtService()
69 {
70 HILOGI("enter");
71
72 SetServiceState(AVRC_CT_SERVICE_STATE_DISABLED);
73
74 myObserver_ = nullptr;
75 }
76
GetContext()77 utility::Context *AvrcpCtService::GetContext()
78 {
79 HILOGI("enter");
80
81 return this;
82 }
83
InitFeatures()84 void AvrcpCtService::InitFeatures()
85 {
86 HILOGI("enter");
87
88 features_ |= AVRC_CT_FEATURE_CATEGORY_1;
89 features_ |= AVRC_CT_FEATURE_CATEGORY_2;
90 features_ |= AVRC_CT_FEATURE_BROWSING;
91 features_ |= AVRC_CT_FEATURE_KEY_OPERATION;
92 features_ |= AVRC_CT_FEATURE_ABSOLUTE_VOLUME;
93 features_ |= AVRC_CT_FEATURE_NOTIFY_PLAYBACK_STATUS_CHANGED;
94 features_ |= AVRC_CT_FEATURE_NOTIFY_TRACK_REACHED_END;
95 features_ |= AVRC_CT_FEATURE_NOTIFY_TRACK_REACHED_START;
96 features_ |= AVRC_CT_FEATURE_NOTIFY_PLAYBACK_POSITION_CHANGED;
97 features_ |= AVRC_CT_FEATURE_NOTIFY_PLAYER_SETTING_CHANGED;
98 features_ |= AVRC_CT_FEATURE_NOTIFY_NOW_PLAYING_CONTENT_CHANGED;
99 features_ |= AVRC_CT_FEATURE_NOTIFY_UIDS_CHANGED;
100 features_ |= AVRC_CT_FEATURE_NOTIFY_ABSOLUTE_VOLUME_CHANGED;
101 }
102
103 /******************************************************************
104 * REGISTER / UNREGISTER OBSERVER *
105 ******************************************************************/
106
RegisterObserver(IObserver * observer)107 void AvrcpCtService::RegisterObserver(IObserver *observer)
108 {
109 HILOGI("enter");
110
111 std::lock_guard<std::mutex> lock(mutex_);
112
113 myObserver_ = observer;
114 }
115
UnregisterObserver(void)116 void AvrcpCtService::UnregisterObserver(void)
117 {
118 HILOGI("enter");
119
120 std::lock_guard<std::mutex> lock(mutex_);
121
122 myObserver_ = nullptr;
123 }
124
125 /******************************************************************
126 * ENABLE / DISABLE *
127 ******************************************************************/
128
Enable(void)129 void AvrcpCtService::Enable(void)
130 {
131 HILOGI("enter");
132
133 if (IsDisabled()) {
134 SetServiceState(AVRC_CT_SERVICE_STATE_ENABLING);
135
136 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::EnableNative, this));
137 } else {
138 LOG_ERROR("Is not disabled!");
139 }
140 }
141
Disable(void)142 void AvrcpCtService::Disable(void)
143 {
144 HILOGI("enter");
145
146 if (IsEnabled()) {
147 SetServiceState(AVRC_CT_SERVICE_STATE_DISABLING);
148 if (profile_ != nullptr) {
149 profile_->SetEnableFlag(false);
150 }
151
152 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::DisableNative, this));
153 } else {
154 LOG_ERROR("Is not enable!");
155 }
156 }
157
EnableNative(void)158 void AvrcpCtService::EnableNative(void)
159 {
160 HILOGI("enter");
161
162 int result = RET_BAD_STATUS;
163
164 IAdapterConfig *config = AdapterConfig::GetInstance();
165 config->GetValue(SECTION_AVRCP_CT_SERVICE, PROPERTY_MAX_CONNECTED_DEVICES, maxConnection_);
166
167 do {
168 result = RegisterSecurity();
169 if (result != RET_NO_ERROR) {
170 break;
171 }
172 result = RegisterService();
173 if (result != RET_NO_ERROR) {
174 break;
175 }
176 result = EnableProfile();
177 if (result != RET_NO_ERROR) {
178 break;
179 }
180 } while (false);
181
182 if (result == RET_NO_ERROR) {
183 SetServiceState(AVRC_CT_SERVICE_STATE_ENABLED);
184 } else {
185 SetServiceState(AVRC_CT_SERVICE_STATE_DISABLED);
186 }
187 GetContext()->OnEnable(PROFILE_NAME_AVRCP_CT, IsEnabled());
188 HILOGI("AvrcpCtService:: AVRCP is ENABLED");
189 }
190
DisableNative(void)191 void AvrcpCtService::DisableNative(void)
192 {
193 HILOGI("enter");
194
195 if (DisableProfile() != RET_NO_ERROR) {
196 OnProfileDisabled(RET_BAD_STATUS);
197 }
198 }
199
EnableProfile(void)200 int AvrcpCtService::EnableProfile(void)
201 {
202 HILOGI("enter");
203
204 /// Gets the size of the MTU.
205 int controlMtu = AVRC_CT_DEFAULT_CONTROL_MTU_SIZE;
206 int browseMtu = AVRC_CT_DEFAULT_BROWSE_MTU_SIZE;
207 bool isTgEnabled = false;
208
209 IAdapterConfig *config = AdapterConfig::GetInstance();
210 config->GetValue(SECTION_AVRCP_CT_SERVICE, PROPERTY_CONTROL_MTU, controlMtu);
211 config->GetValue(SECTION_AVRCP_CT_SERVICE, PROPERTY_BROWSE_MTU, browseMtu);
212 config->GetValue(SECTION_CLASSIC_ADAPTER, PROPERTY_AVRCP_TG_SERVICE, isTgEnabled);
213
214 profile_ = std::make_unique<AvrcCtProfile>(features_,
215 AVRC_CT_DEFAULT_BLUETOOTH_SIG_COMPANY_ID,
216 controlMtu,
217 browseMtu,
218 AVRC_CT_DEFAULT_MAX_FRAGMENTS,
219 GetDispatcher(),
220 ChannelEventCallback,
221 ChannelMessageCallback);
222 profile_->RegisterObserver(pfObserver_.get());
223
224 return profile_->Enable(isTgEnabled);
225 }
226
DisableProfile(void) const227 int AvrcpCtService::DisableProfile(void) const
228 {
229 HILOGI("enter");
230
231 return profile_->Disable();
232 }
233
OnProfileDisabled(int result)234 void AvrcpCtService::OnProfileDisabled(int result)
235 {
236 HILOGI("result: %{public}d", result);
237
238 SetServiceState(AVRC_CT_SERVICE_STATE_DISABLED);
239
240 profile_->UnregisterObserver();
241 profile_ = nullptr;
242
243 result |= UnregisterService();
244 result |= UnregisterSecurity();
245
246 GetContext()->OnDisable(PROFILE_NAME_AVRCP_CT, result == RET_NO_ERROR);
247 }
248
RegisterSecurity(void)249 int AvrcpCtService::RegisterSecurity(void)
250 {
251 HILOGI("enter");
252
253 gapManager_ = std::make_unique<AvrcCtGapManager>();
254
255 return gapManager_->RegisterSecurity();
256 }
257
UnregisterSecurity(void)258 int AvrcpCtService::UnregisterSecurity(void)
259 {
260 HILOGI("enter");
261
262 int result = gapManager_->UnregisterSecurity();
263 gapManager_ = nullptr;
264
265 return result;
266 }
267
RegisterService(void)268 int AvrcpCtService::RegisterService(void)
269 {
270 HILOGI("enter");
271
272 sdpManager_ = std::make_unique<AvrcCtSdpManager>(features_ & AVRC_CT_SDP_ALL_SUPPORTED_FEATURES);
273
274 return sdpManager_->RegisterService();
275 }
276
UnregisterService(void)277 int AvrcpCtService::UnregisterService(void)
278 {
279 HILOGI("enter");
280
281 int result = sdpManager_->UnregisterService();
282 sdpManager_ = nullptr;
283
284 return result;
285 }
286
IsEnabled(void)287 bool AvrcpCtService::IsEnabled(void)
288 {
289 HILOGI("enter");
290
291 return (state_ == AVRC_CT_SERVICE_STATE_ENABLED);
292 }
293
IsDisabled(void)294 bool AvrcpCtService::IsDisabled(void)
295 {
296 HILOGI("enter");
297
298 return (state_ == AVRC_CT_SERVICE_STATE_DISABLED);
299 }
300
SetServiceState(uint8_t state)301 void AvrcpCtService::SetServiceState(uint8_t state)
302 {
303 HILOGI("state: %{public}d", state);
304
305 state_ = state;
306 }
307
308 /******************************************************************
309 * CONNECTION *
310 ******************************************************************/
311
GetConnectedDevices(void)312 std::vector<RawAddress> AvrcpCtService::GetConnectedDevices(void)
313 {
314 HILOGI("enter");
315
316 std::vector<RawAddress> result;
317
318 if (IsEnabled()) {
319 result = profile_->GetConnectedDevices();
320 }
321
322 return result;
323 }
324
GetDevicesByStates(const std::vector<int> & states)325 std::vector<bluetooth::RawAddress> AvrcpCtService::GetDevicesByStates(const std::vector<int> &states)
326 {
327 HILOGI("enter");
328
329 std::vector<bluetooth::RawAddress> result;
330
331 if (IsEnabled()) {
332 result = profile_->GetDevicesByStates(states);
333 }
334
335 return result;
336 }
337
GetMaxConnectNum(void)338 int AvrcpCtService::GetMaxConnectNum(void)
339 {
340 HILOGI("enter");
341
342 int result = 0;
343
344 if (IsEnabled()) {
345 result = profile_->GetMaxConnectNum();
346 }
347
348 HILOGI("result: %{public}d", result);
349 return result;
350 }
351
GetDeviceState(const RawAddress & rawAddr)352 int AvrcpCtService::GetDeviceState(const RawAddress &rawAddr)
353 {
354 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
355
356 int result = static_cast<int>(BTConnectState::DISCONNECTED);
357
358 if (IsEnabled()) {
359 result = profile_->GetDeviceState(rawAddr);
360 }
361
362 HILOGI("result: %{public}d", result);
363 return result;
364 }
365
Connect(const RawAddress & rawAddr)366 int AvrcpCtService::Connect(const RawAddress &rawAddr)
367 {
368 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
369
370 int result = RET_BAD_STATUS;
371
372 do {
373 if (!IsEnabled()) {
374 break;
375 }
376
377 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::DISCONNECTED)) {
378 break;
379 }
380
381 if (!CheckConnectionNum()) {
382 break;
383 }
384
385 RawAddress peerAddr(rawAddr.GetAddress());
386 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::ConnectNative, this, peerAddr));
387 result = RET_NO_ERROR;
388 } while (false);
389
390 return result;
391 }
392
ConnectNative(RawAddress rawAddr)393 void AvrcpCtService::ConnectNative(RawAddress rawAddr)
394 {
395 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
396
397 AcceptActiveConnect(rawAddr);
398 }
399
Disconnect(const RawAddress & rawAddr)400 int AvrcpCtService::Disconnect(const RawAddress &rawAddr)
401 {
402 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
403
404 int result = RET_BAD_STATUS;
405
406 do {
407 if (!IsEnabled()) {
408 break;
409 }
410
411 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
412 break;
413 }
414
415 RawAddress peerAddr(rawAddr.GetAddress());
416 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::DisconnectNative, this, peerAddr));
417 result = RET_NO_ERROR;
418 } while (false);
419
420 return result;
421 }
422
DisconnectNative(RawAddress rawAddr)423 void AvrcpCtService::DisconnectNative(RawAddress rawAddr)
424 {
425 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
426
427 do {
428 if (!IsEnabled()) {
429 break;
430 }
431
432 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
433 break;
434 }
435
436 if (profile_->Disconnect(rawAddr) != RET_NO_ERROR) {
437 HILOGI("Disconnect Failed! Addr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
438 }
439 } while (false);
440 }
441
GetConnectState(void)442 int AvrcpCtService::GetConnectState(void)
443 {
444 HILOGI("enter");
445
446 int result = PROFILE_STATE_DISCONNECTED;
447
448 if (IsEnabled()) {
449 result = profile_->GetConnectState();
450 }
451
452 return result;
453 }
454
OnConnectionStateChanged(const RawAddress & rawAddr,int state) const455 void AvrcpCtService::OnConnectionStateChanged(const RawAddress &rawAddr, int state) const
456 {
457 HILOGI("Address: %{public}s, state: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), state);
458
459 if (myObserver_ != nullptr) {
460 myObserver_->OnConnectionStateChanged(rawAddr, state);
461 } else {
462 HILOGI("The observer is not registered!");
463 }
464 }
465
AcceptActiveConnect(const RawAddress & rawAddr)466 void AvrcpCtService::AcceptActiveConnect(const RawAddress &rawAddr)
467 {
468 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
469
470 do {
471 if (!IsEnabled()) {
472 break;
473 }
474
475 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::DISCONNECTED)) {
476 break;
477 }
478
479 if (profile_->Connect(rawAddr) != RET_NO_ERROR) {
480 HILOGI("Connect Failed! Addr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
481 }
482 } while (false);
483 }
484
RejectActiveConnect(const RawAddress & rawAddr) const485 void AvrcpCtService::RejectActiveConnect(const RawAddress &rawAddr) const
486 {
487 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
488 }
489
FindTgService(const RawAddress & rawAddr) const490 int AvrcpCtService::FindTgService(const RawAddress &rawAddr) const
491 {
492 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
493
494 return sdpManager_->FindTgService(rawAddr, FindTgServiceCallback);
495 }
496
FindTgServiceCallback(const BtAddr * btAddr,const uint32_t * handleArray,uint16_t handleCount,void * context)497 void AvrcpCtService::FindTgServiceCallback(
498 const BtAddr *btAddr, const uint32_t *handleArray, uint16_t handleCount, void *context)
499 {
500 HILOGI("handleCount: %{public}d", handleCount);
501
502 auto servManager = IProfileManager::GetInstance();
503 auto service = static_cast<AvrcpCtService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
504 RawAddress rawAddr(RawAddress::ConvertToString(btAddr->addr));
505 if (service != nullptr) {
506 if (handleCount > 0) {
507 service->GetDispatcher()->PostTask(std::bind(&AvrcpCtService::AcceptActiveConnect, service, rawAddr));
508 } else {
509 service->GetDispatcher()->PostTask(std::bind(&AvrcpCtService::RejectActiveConnect, service, rawAddr));
510 }
511 }
512 }
513
514 /******************************************************************
515 * BUTTON OPERATION *
516 ******************************************************************/
517
PressButton(const RawAddress & rawAddr,uint8_t button)518 int AvrcpCtService::PressButton(const RawAddress &rawAddr, uint8_t button)
519 {
520 HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
521
522 int result = RET_BAD_STATUS;
523
524 do {
525 if (!IsEnabled()) {
526 break;
527 }
528
529 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
530 break;
531 }
532
533 if (profile_->IsPassQueueFull(rawAddr)) {
534 break;
535 }
536
537 RawAddress peerAddr(rawAddr.GetAddress());
538 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::PressButtonNative, this, peerAddr, button));
539 result = RET_NO_ERROR;
540 } while (false);
541
542 return result;
543 }
544
PressButtonNative(RawAddress rawAddr,uint8_t button)545 void AvrcpCtService::PressButtonNative(RawAddress rawAddr, uint8_t button)
546 {
547 HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
548
549 do {
550 if (!IsEnabled()) {
551 break;
552 }
553
554 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
555 break;
556 }
557
558 profile_->SendPressButtonCmd(rawAddr, button);
559 } while (false);
560 }
561
OnButtonPressed(const RawAddress & rawAddr,uint8_t button,int result) const562 void AvrcpCtService::OnButtonPressed(const RawAddress &rawAddr, uint8_t button, int result) const
563 {
564 HILOGI("Address: %{public}s, button: %{public}d, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr),
565 button, result);
566
567 if (myObserver_ != nullptr) {
568 myObserver_->OnPressButton(rawAddr, button, result);
569 } else {
570 HILOGI("The observer[onPressButton] is not registered!");
571 }
572 }
573
ReleaseButton(const RawAddress & rawAddr,uint8_t button)574 int AvrcpCtService::ReleaseButton(const RawAddress &rawAddr, uint8_t button)
575 {
576 HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
577
578 int result = RET_BAD_STATUS;
579
580 do {
581 if (!IsEnabled()) {
582 break;
583 }
584
585 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
586 break;
587 }
588
589 if (profile_->IsPassQueueFull(rawAddr)) {
590 break;
591 }
592
593 RawAddress peerAddr(rawAddr.GetAddress());
594 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::ReleaseButtonNative, this, peerAddr, button));
595 result = RET_NO_ERROR;
596 } while (false);
597
598 return result;
599 }
600
ReleaseButtonNative(RawAddress rawAddr,uint8_t button)601 void AvrcpCtService::ReleaseButtonNative(RawAddress rawAddr, uint8_t button)
602 {
603 HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
604
605 do {
606 if (!IsEnabled()) {
607 break;
608 }
609
610 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
611 break;
612 }
613
614 profile_->SendReleaseButtonCmd(rawAddr, button);
615 } while (false);
616 }
617
OnButtonReleased(const RawAddress & rawAddr,uint8_t button,int result) const618 void AvrcpCtService::OnButtonReleased(const RawAddress &rawAddr, uint8_t button, int result) const
619 {
620 HILOGI("Address: %{public}s, button: %{public}d, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr),
621 button, result);
622
623 if (myObserver_ != nullptr) {
624 myObserver_->OnReleaseButton(rawAddr, button, result);
625 } else {
626 HILOGI("The observer[onReleaseButton] is not registered!");
627 }
628 }
629
630 /******************************************************************
631 * UNIT INFO / SUB UNIT INFO *
632 ******************************************************************/
633
GetUnitInfo(const RawAddress & rawAddr)634 int AvrcpCtService::GetUnitInfo(const RawAddress &rawAddr)
635 {
636 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
637
638 int result = RET_BAD_STATUS;
639
640 do {
641 if (!IsEnabled()) {
642 break;
643 }
644
645 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
646 break;
647 }
648
649 if (profile_->IsVendorQueueFull(rawAddr)) {
650 break;
651 }
652
653 RawAddress peerAddr(rawAddr.GetAddress());
654 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::UnitInfoNative, this, peerAddr));
655 result = RET_NO_ERROR;
656 } while (false);
657
658 return result;
659 }
660
UnitInfoNative(RawAddress rawAddr)661 void AvrcpCtService::UnitInfoNative(RawAddress rawAddr)
662 {
663 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
664
665 do {
666 if (!IsEnabled()) {
667 break;
668 }
669
670 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
671 break;
672 }
673
674 profile_->SendUnitCmd(rawAddr);
675 } while (false);
676 }
677
GetSubUnitInfo(const RawAddress & rawAddr)678 int AvrcpCtService::GetSubUnitInfo(const RawAddress &rawAddr)
679 {
680 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
681
682 int result = RET_BAD_STATUS;
683
684 do {
685 if (!IsEnabled()) {
686 break;
687 }
688
689 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
690 break;
691 }
692
693 if (profile_->IsVendorQueueFull(rawAddr)) {
694 break;
695 }
696
697 RawAddress peerAddr(rawAddr.GetAddress());
698 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::SubUnitInfoNative, this, peerAddr));
699 result = RET_NO_ERROR;
700 } while (false);
701
702 return result;
703 }
704
SubUnitInfoNative(RawAddress rawAddr)705 void AvrcpCtService::SubUnitInfoNative(RawAddress rawAddr)
706 {
707 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
708
709 do {
710 if (!IsEnabled()) {
711 break;
712 }
713
714 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
715 break;
716 }
717
718 profile_->SendSubUnitCmd(rawAddr);
719 } while (false);
720 }
721
722 /******************************************************************
723 * Media Player Selection *
724 ******************************************************************/
725
SetAddressedPlayer(const RawAddress & rawAddr,uint16_t playerId)726 int AvrcpCtService::SetAddressedPlayer(const RawAddress &rawAddr, uint16_t playerId)
727 {
728 HILOGI("address: %{public}s, playerId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
729
730 int result = RET_BAD_STATUS;
731
732 do {
733 if (!IsEnabled()) {
734 break;
735 }
736
737 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
738 break;
739 }
740
741 if (profile_->IsVendorQueueFull(rawAddr)) {
742 break;
743 }
744
745 RawAddress peerAddr(rawAddr.GetAddress());
746 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::SetAddressedPlayerNative, this, peerAddr, playerId));
747 result = RET_NO_ERROR;
748 } while (false);
749
750 return result;
751 }
752
SetAddressedPlayerNative(RawAddress rawAddr,uint16_t playerId)753 void AvrcpCtService::SetAddressedPlayerNative(RawAddress rawAddr, uint16_t playerId)
754 {
755 HILOGI("address: %{public}s, playerId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
756
757 do {
758 if (!IsEnabled()) {
759 break;
760 }
761
762 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
763 break;
764 }
765
766 profile_->SendSetAddressedPlayerCmd(rawAddr, playerId);
767 } while (false);
768 }
769
OnSetAddressedPlayer(const RawAddress & rawAddr,int result,int detail) const770 void AvrcpCtService::OnSetAddressedPlayer(const RawAddress &rawAddr, int result, int detail) const
771 {
772 HILOGI("address: %{public}s, result: %{public}d, detail: %{public}d",
773 GET_ENCRYPT_AVRCP_ADDR(rawAddr), result, detail);
774
775 if (myObserver_ != nullptr) {
776 myObserver_->OnSetAddressedPlayer(rawAddr, result, detail);
777 HILOGI("enter");
778 } else {
779 HILOGI("The observer is not registered!");
780 }
781 }
782
SetBrowsedPlayer(const RawAddress & rawAddr,uint16_t playerId)783 int AvrcpCtService::SetBrowsedPlayer(const RawAddress &rawAddr, uint16_t playerId)
784 {
785 HILOGI("address: %{public}s, playerId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
786
787 int result = RET_BAD_STATUS;
788
789 do {
790 if (!IsEnabled()) {
791 break;
792 }
793
794 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
795 break;
796 }
797
798 if (profile_->IsVendorQueueFull(rawAddr)) {
799 break;
800 }
801
802 if (!profile_->IsBrowsingConnected(rawAddr)) {
803 break;
804 }
805
806 RawAddress peerAddr(rawAddr.GetAddress());
807 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::SetBrowsedPlayerNative, this, peerAddr, playerId));
808 result = RET_NO_ERROR;
809 } while (false);
810
811 return result;
812 }
813
SetBrowsedPlayerNative(RawAddress rawAddr,uint16_t playerId)814 void AvrcpCtService::SetBrowsedPlayerNative(RawAddress rawAddr, uint16_t playerId)
815 {
816 HILOGI("address: %{public}s, playerId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
817
818 do {
819 if (!IsEnabled()) {
820 break;
821 }
822
823 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
824 break;
825 }
826
827 profile_->SendSetBrowsedPlayerCmd(rawAddr, playerId);
828 } while (false);
829 }
830
OnSetBrowsedPlayer(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,const std::vector<std::string> & folderNames,int result,int detail) const831 void AvrcpCtService::OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems,
832 const std::vector<std::string> &folderNames, int result, int detail) const
833 {
834 HILOGI("addr: %{public}s, uidCounter: %{public}hu, numOfItems: %{public}d, folderNames.size: %{public}zu, "
835 "result: %{public}d, detail: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems,
836 folderNames.size(), result, detail);
837
838 if (myObserver_ != nullptr) {
839 myObserver_->OnSetBrowsedPlayer(rawAddr, uidCounter, numOfItems, folderNames, result, detail);
840 HILOGI("enter");
841 } else {
842 HILOGI("The observer is not registered!");
843 }
844 }
845
846 /******************************************************************
847 * Capabilities *
848 ******************************************************************/
849
GetSupportedCompanies(const RawAddress & rawAddr)850 int AvrcpCtService::GetSupportedCompanies(const RawAddress &rawAddr)
851 {
852 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
853
854 int result = RET_BAD_STATUS;
855
856 do {
857 if (!IsEnabled()) {
858 break;
859 }
860
861 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
862 break;
863 }
864
865 if (profile_->IsVendorQueueFull(rawAddr)) {
866 break;
867 }
868
869 RawAddress peerAddr(rawAddr.GetAddress());
870 GetDispatcher()->PostTask(
871 std::bind(&AvrcpCtService::GetCapabilitiesNative, this, peerAddr, AVRC_CAPABILITY_COMPANYID));
872 result = RET_NO_ERROR;
873 } while (false);
874
875 return result;
876 }
877
GetSupportedEvents(const RawAddress & rawAddr)878 int AvrcpCtService::GetSupportedEvents(const RawAddress &rawAddr)
879 {
880 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
881
882 int result = RET_BAD_STATUS;
883
884 do {
885 if (!IsEnabled()) {
886 break;
887 }
888
889 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
890 break;
891 }
892
893 if (profile_->IsVendorQueueFull(rawAddr)) {
894 break;
895 }
896
897 RawAddress peerAddr(rawAddr.GetAddress());
898 GetDispatcher()->PostTask(
899 std::bind(&AvrcpCtService::GetCapabilitiesNative, this, peerAddr, AVRC_CAPABILITY_EVENTID));
900 result = RET_NO_ERROR;
901 } while (false);
902
903 return result;
904 }
905
GetCapabilitiesNative(RawAddress rawAddr,uint8_t capabilityId)906 void AvrcpCtService::GetCapabilitiesNative(RawAddress rawAddr, uint8_t capabilityId)
907 {
908 do {
909 if (!IsEnabled()) {
910 break;
911 }
912
913 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
914 break;
915 }
916
917 profile_->SendGetCapabilitiesCmd(rawAddr, capabilityId);
918 } while (false);
919 }
920
OnGetCapabilities(const RawAddress & rawAddr,const std::vector<uint32_t> & companies,const std::vector<uint8_t> & events,int result) const921 void AvrcpCtService::OnGetCapabilities(const RawAddress &rawAddr, const std::vector<uint32_t> &companies,
922 const std::vector<uint8_t> &events, int result) const
923 {
924 HILOGI("address: %{public}s, companies.size: %{public}zu, events.size: %{public}zu, result: %{public}d",
925 GET_ENCRYPT_AVRCP_ADDR(rawAddr), companies.size(), events.size(), result);
926
927 if (myObserver_ != nullptr) {
928 myObserver_->OnGetCapabilities(rawAddr, companies, events, result);
929 HILOGI("enter");
930 } else {
931 HILOGI("The observer is not registered!");
932 }
933 }
934 /******************************************************************
935 * PLAYER APPLICATION SETTINGS *
936 ******************************************************************/
937
GetPlayerAppSettingAttributes(const RawAddress & rawAddr)938 int AvrcpCtService::GetPlayerAppSettingAttributes(const RawAddress &rawAddr)
939 {
940 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
941
942 int result = RET_BAD_STATUS;
943
944 do {
945 if (!IsEnabled()) {
946 break;
947 }
948
949 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
950 break;
951 }
952
953 if (profile_->IsVendorQueueFull(rawAddr)) {
954 break;
955 }
956
957 RawAddress peerAddr(rawAddr.GetAddress());
958 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::GetPlayerAppSettingAttributesNative, this, peerAddr));
959 result = RET_NO_ERROR;
960 } while (false);
961
962 return result;
963 }
964
GetPlayerAppSettingAttributesNative(RawAddress rawAddr)965 void AvrcpCtService::GetPlayerAppSettingAttributesNative(RawAddress rawAddr)
966 {
967 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
968
969 do {
970 if (!IsEnabled()) {
971 break;
972 }
973
974 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
975 break;
976 }
977
978 profile_->SendListPlayerApplicationSettingAttributesCmd(rawAddr);
979 } while (false);
980 }
981
OnGetPlayerAppSettingAttribtues(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,int result) const982 void AvrcpCtService::OnGetPlayerAppSettingAttribtues(
983 const RawAddress &rawAddr, const std::vector<uint8_t> &attributes, int result) const
984 {
985 HILOGI("addr: %{public}s, attribute.size: %{public}zu, result: %{public}d",
986 GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size(), result);
987
988 if (myObserver_ != nullptr) {
989 myObserver_->OnGetPlayerAppSettingAttributes(rawAddr, attributes, result);
990 HILOGI("enter");
991 } else {
992 HILOGI("The observer is not registered!");
993 }
994 }
995
GetPlayerAppSettingValues(const RawAddress & rawAddr,uint8_t attribute)996 int AvrcpCtService::GetPlayerAppSettingValues(const RawAddress &rawAddr, uint8_t attribute)
997 {
998 HILOGI("address: %{public}s, attribute: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attribute);
999
1000 int result = RET_BAD_STATUS;
1001
1002 do {
1003 if (!IsEnabled()) {
1004 break;
1005 }
1006
1007 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1008 break;
1009 }
1010
1011 if (profile_->IsVendorQueueFull(rawAddr)) {
1012 break;
1013 }
1014
1015 RawAddress peerAddr(rawAddr.GetAddress());
1016 GetDispatcher()->PostTask(
1017 std::bind(&AvrcpCtService::GetPlayerAppSettingValuesNative, this, peerAddr, attribute));
1018 result = RET_NO_ERROR;
1019 } while (false);
1020
1021 return result;
1022 }
1023
GetPlayerAppSettingValuesNative(RawAddress rawAddr,uint8_t attribute)1024 void AvrcpCtService::GetPlayerAppSettingValuesNative(RawAddress rawAddr, uint8_t attribute)
1025 {
1026 HILOGI("address: %{public}s, attribute: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attribute);
1027
1028 do {
1029 if (!IsEnabled()) {
1030 break;
1031 }
1032
1033 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1034 break;
1035 }
1036
1037 profile_->SendListPlayerApplicationSettingValuesCmd(rawAddr, attribute);
1038 } while (false);
1039 }
1040
OnGetPlayerAppSettingValues(const RawAddress & rawAddr,uint8_t attribute,const std::vector<uint8_t> & values,int result) const1041 void AvrcpCtService::OnGetPlayerAppSettingValues(
1042 const RawAddress &rawAddr, uint8_t attribute, const std::vector<uint8_t> &values, int result) const
1043 {
1044 HILOGI("addr: %{public}s, attribute: %{public}d, values.size: %{public}zu, result: %{public}d",
1045 GET_ENCRYPT_AVRCP_ADDR(rawAddr), attribute, values.size(), result);
1046
1047 if (myObserver_ != nullptr) {
1048 myObserver_->OnGetPlayerAppSettingValues(rawAddr, attribute, values, result);
1049 HILOGI("enter");
1050 } else {
1051 HILOGI("The observer is not registered!");
1052 }
1053 }
1054
GetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes)1055 int AvrcpCtService::GetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes)
1056 {
1057 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1058
1059 int result = RET_BAD_STATUS;
1060
1061 do {
1062 if (!IsEnabled()) {
1063 break;
1064 }
1065
1066 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1067 break;
1068 }
1069
1070 if (profile_->IsVendorQueueFull(rawAddr)) {
1071 break;
1072 }
1073
1074 RawAddress peerAddr(rawAddr.GetAddress());
1075 GetDispatcher()->PostTask(
1076 std::bind(&AvrcpCtService::GetPlayerAppSettingCurrentValueNative, this, peerAddr, attributes));
1077 result = RET_NO_ERROR;
1078 } while (false);
1079
1080 return result;
1081 }
1082
GetPlayerAppSettingCurrentValueNative(RawAddress rawAddr,std::vector<uint8_t> attributes)1083 void AvrcpCtService::GetPlayerAppSettingCurrentValueNative(RawAddress rawAddr, std::vector<uint8_t> attributes)
1084 {
1085 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1086
1087 do {
1088 if (!IsEnabled()) {
1089 break;
1090 }
1091
1092 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1093 break;
1094 }
1095
1096 profile_->SendGetCurrentPlayerApplicationSettingValueCmd(rawAddr, attributes);
1097 } while (false);
1098 }
1099
OnGetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values,int result) const1100 void AvrcpCtService::OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr,
1101 const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values, int result) const
1102 {
1103 HILOGI("addr: %{public}s, attributes.size: %{public}zu, values.size: %{public}zu, result: %{public}d",
1104 GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size(), values.size(), result);
1105
1106 if (myObserver_ != nullptr) {
1107 myObserver_->OnGetPlayerAppSettingCurrentValue(rawAddr, attributes, values, result);
1108 HILOGI("enter");
1109 } else {
1110 HILOGI("The observer is not registered!");
1111 }
1112 }
1113
SetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values)1114 int AvrcpCtService::SetPlayerAppSettingCurrentValue(
1115 const RawAddress &rawAddr, const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values)
1116 {
1117 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1118
1119 int result = RET_BAD_STATUS;
1120
1121 do {
1122 if (!IsEnabled()) {
1123 break;
1124 }
1125
1126 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1127 break;
1128 }
1129
1130 if (profile_->IsVendorQueueFull(rawAddr)) {
1131 break;
1132 }
1133
1134 RawAddress peerAddr(rawAddr.GetAddress());
1135 GetDispatcher()->PostTask(
1136 std::bind(&AvrcpCtService::SetPlayerAppSettingCurrentValueNative, this, peerAddr, attributes, values));
1137 result = RET_NO_ERROR;
1138 } while (false);
1139
1140 return result;
1141 }
1142
SetPlayerAppSettingCurrentValueNative(RawAddress rawAddr,std::vector<uint8_t> attributes,std::vector<uint8_t> values)1143 void AvrcpCtService::SetPlayerAppSettingCurrentValueNative(
1144 RawAddress rawAddr, std::vector<uint8_t> attributes, std::vector<uint8_t> values)
1145 {
1146 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1147
1148 do {
1149 if (!IsEnabled()) {
1150 break;
1151 }
1152
1153 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1154 break;
1155 }
1156
1157 profile_->SendSetPlayerApplicationSettingValueCmd(rawAddr, attributes, values);
1158 } while (false);
1159 }
1160
OnSetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,int result) const1161 void AvrcpCtService::OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, int result) const
1162 {
1163 HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1164
1165 if (myObserver_ != nullptr) {
1166 myObserver_->OnSetPlayerAppSettingCurrentValue(rawAddr, result);
1167 HILOGI("enter");
1168 } else {
1169 HILOGI("The observer is not registered!");
1170 }
1171 }
1172
GetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes)1173 int AvrcpCtService::GetPlayerAppSettingAttributeText(const RawAddress &rawAddr, const std::vector<uint8_t> &attributes)
1174 {
1175 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1176
1177 int result = RET_BAD_STATUS;
1178
1179 do {
1180 if (!IsEnabled()) {
1181 break;
1182 }
1183
1184 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1185 break;
1186 }
1187
1188 if (profile_->IsVendorQueueFull(rawAddr)) {
1189 break;
1190 }
1191
1192 RawAddress peerAddr(rawAddr.GetAddress());
1193 GetDispatcher()->PostTask(
1194 std::bind(&AvrcpCtService::GetPlayerAppSettingAttributeTextNative, this, peerAddr, attributes));
1195 result = RET_NO_ERROR;
1196 } while (false);
1197
1198 return result;
1199 }
1200
GetPlayerAppSettingAttributeTextNative(RawAddress rawAddr,std::vector<uint8_t> attributes)1201 void AvrcpCtService::GetPlayerAppSettingAttributeTextNative(RawAddress rawAddr, std::vector<uint8_t> attributes)
1202 {
1203 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1204
1205 do {
1206 if (!IsEnabled()) {
1207 break;
1208 }
1209
1210 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1211 break;
1212 }
1213
1214 profile_->SendGetPlayerApplicationSettingAttributeTextCmd(rawAddr, attributes);
1215 } while (false);
1216 }
1217
OnGetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<std::string> & attrStr,int result) const1218 void AvrcpCtService::OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr,
1219 const std::vector<uint8_t> &attributes, const std::vector<std::string> &attrStr, int result) const
1220 {
1221 HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1222
1223 if (myObserver_ != nullptr) {
1224 myObserver_->OnGetPlayerAppSettingAttributeText(rawAddr, attributes, attrStr, result);
1225 } else {
1226 HILOGI("The observer is not registered!");
1227 }
1228 }
1229
GetPlayerAppSettingValueText(const RawAddress & rawAddr,uint8_t attributeId,const std::vector<uint8_t> & values)1230 int AvrcpCtService::GetPlayerAppSettingValueText(
1231 const RawAddress &rawAddr, uint8_t attributeId, const std::vector<uint8_t> &values)
1232 {
1233 HILOGI("address: %{public}s, attributeId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributeId);
1234
1235 int result = RET_BAD_STATUS;
1236
1237 do {
1238 if (!IsEnabled()) {
1239 break;
1240 }
1241
1242 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1243 break;
1244 }
1245
1246 if (profile_->IsVendorQueueFull(rawAddr)) {
1247 break;
1248 }
1249
1250 RawAddress peerAddr(rawAddr.GetAddress());
1251 GetDispatcher()->PostTask(
1252 std::bind(&AvrcpCtService::GetPlayerAppSettingValueTextNative, this, peerAddr, attributeId, values));
1253 result = RET_NO_ERROR;
1254 } while (false);
1255
1256 return result;
1257 }
1258
GetPlayerAppSettingValueTextNative(RawAddress rawAddr,uint8_t attributeId,std::vector<uint8_t> values)1259 void AvrcpCtService::GetPlayerAppSettingValueTextNative(
1260 RawAddress rawAddr, uint8_t attributeId, std::vector<uint8_t> values)
1261 {
1262 HILOGI("address: %{public}s, attributeId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributeId);
1263
1264 do {
1265 if (!IsEnabled()) {
1266 break;
1267 }
1268
1269 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1270 break;
1271 }
1272
1273 profile_->SendGetPlayerApplicationSettingValueTextCmd(rawAddr, attributeId, values);
1274 } while (false);
1275 }
1276
OnGetPlayerAppSettingValueText(const RawAddress & rawAddr,const std::vector<uint8_t> & values,const std::vector<std::string> & valueStr,int result) const1277 void AvrcpCtService::OnGetPlayerAppSettingValueText(const RawAddress &rawAddr, const std::vector<uint8_t> &values,
1278 const std::vector<std::string> &valueStr, int result) const
1279 {
1280 HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1281
1282 if (myObserver_ != nullptr) {
1283 myObserver_->OnGetPlayerAppSettingValueText(rawAddr, values, valueStr, result);
1284 } else {
1285 HILOGI("The observer is not registered!");
1286 }
1287 }
1288
1289 /******************************************************************
1290 * MEDIA INFORMATION *
1291 ******************************************************************/
1292
GetElementAttributes(const RawAddress & rawAddr,uint64_t identifier,const std::vector<uint32_t> & attributes)1293 int AvrcpCtService::GetElementAttributes(
1294 const RawAddress &rawAddr, uint64_t identifier, const std::vector<uint32_t> &attributes)
1295 {
1296 HILOGI("address: %{public}s, identifier: %{public}llu",
1297 GET_ENCRYPT_AVRCP_ADDR(rawAddr), static_cast<unsigned long long>(identifier));
1298
1299 int result = RET_BAD_STATUS;
1300 do {
1301 if (!IsEnabled()) {
1302 break;
1303 }
1304
1305 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1306 break;
1307 }
1308
1309 if (profile_->IsVendorQueueFull(rawAddr)) {
1310 break;
1311 }
1312
1313 RawAddress peerAddr(rawAddr.GetAddress());
1314 GetDispatcher()->PostTask(
1315 std::bind(&AvrcpCtService::GetElementAttributesNative, this, peerAddr, identifier, attributes));
1316 result = RET_NO_ERROR;
1317 } while (false);
1318
1319 return result;
1320 }
1321
GetElementAttributesNative(RawAddress rawAddr,uint64_t identifier,std::vector<uint32_t> attributes)1322 void AvrcpCtService::GetElementAttributesNative(
1323 RawAddress rawAddr, uint64_t identifier, std::vector<uint32_t> attributes)
1324 {
1325 HILOGI("address: %{public}s, identifier: %{public}llu",
1326 GET_ENCRYPT_AVRCP_ADDR(rawAddr), static_cast<unsigned long long>(identifier));
1327
1328 do {
1329 if (!IsEnabled()) {
1330 break;
1331 }
1332
1333 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1334 break;
1335 }
1336
1337 profile_->SendGetElementAttributesCmd(rawAddr, identifier, attributes);
1338 } while (false);
1339 }
1340
OnGetElementAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attributes,const std::vector<std::string> & values,int result) const1341 void AvrcpCtService::OnGetElementAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
1342 const std::vector<std::string> &values, int result) const
1343 {
1344 HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1345
1346 if (myObserver_ != nullptr) {
1347 myObserver_->OnGetElementAttributes(rawAddr, attributes, values, result);
1348 } else {
1349 HILOGI("The observer is not registered!");
1350 }
1351 }
1352
1353 /******************************************************************
1354 * PLAY *
1355 ******************************************************************/
1356
GetPlayStatus(const RawAddress & rawAddr)1357 int AvrcpCtService::GetPlayStatus(const RawAddress &rawAddr)
1358 {
1359 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1360
1361 int result = RET_BAD_STATUS;
1362 do {
1363 if (!IsEnabled()) {
1364 break;
1365 }
1366
1367 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1368 break;
1369 }
1370
1371 if (profile_->IsVendorQueueFull(rawAddr)) {
1372 break;
1373 }
1374
1375 RawAddress peerAddr(rawAddr.GetAddress());
1376 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::GetPlayStatusNative, this, peerAddr));
1377 result = RET_NO_ERROR;
1378 } while (false);
1379
1380 return result;
1381 }
1382
GetPlayStatusNative(RawAddress rawAddr)1383 void AvrcpCtService::GetPlayStatusNative(RawAddress rawAddr)
1384 {
1385 HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1386
1387 do {
1388 if (!IsEnabled()) {
1389 break;
1390 }
1391
1392 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1393 break;
1394 }
1395
1396 profile_->SendGetPlayStatusCmd(rawAddr);
1397 } while (false);
1398 }
1399
OnGetPlayStatus(const RawAddress & rawAddr,uint32_t songLength,uint32_t songPosition,uint8_t playStatus,int result) const1400 void AvrcpCtService::OnGetPlayStatus(
1401 const RawAddress &rawAddr, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result) const
1402 {
1403 HILOGI("address: %{public}s, songLength: %{public}d, songPosition: %{public}d, playStatus: %{public}d, "
1404 "result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), songLength, songPosition, playStatus, result);
1405
1406 if (myObserver_ != nullptr) {
1407 myObserver_->OnGetPlayStatus(rawAddr, songLength, songPosition, playStatus, result);
1408 } else {
1409 HILOGI("The observer is not registered!");
1410 }
1411 }
1412
PlayItem(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1413 int AvrcpCtService::PlayItem(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1414 {
1415 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1416 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1417
1418 int result = RET_BAD_STATUS;
1419
1420 do {
1421 if (!IsEnabled()) {
1422 break;
1423 }
1424
1425 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1426 break;
1427 }
1428
1429 if (profile_->IsVendorQueueFull(rawAddr)) {
1430 break;
1431 }
1432
1433 RawAddress peerAddr(rawAddr.GetAddress());
1434 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::PlayItemNative, this, peerAddr, scope, uid, uidCounter));
1435 result = RET_NO_ERROR;
1436 } while (false);
1437
1438 return result;
1439 }
1440
PlayItemNative(RawAddress rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1441 void AvrcpCtService::PlayItemNative(RawAddress rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1442 {
1443 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1444 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1445
1446 do {
1447 if (!IsEnabled()) {
1448 break;
1449 }
1450
1451 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1452 break;
1453 }
1454
1455 profile_->SendPlayItemCmd(rawAddr, scope, uid, uidCounter);
1456 } while (false);
1457 };
1458
OnPlayItem(const RawAddress & rawAddr,int result,int detail) const1459 void AvrcpCtService::OnPlayItem(const RawAddress &rawAddr, int result, int detail) const
1460 {
1461 HILOGI("address: %{public}s, result: %{public}d, detail: %{public}d",
1462 GET_ENCRYPT_AVRCP_ADDR(rawAddr), result, detail);
1463 if (myObserver_ != nullptr) {
1464 myObserver_->OnPlayItem(rawAddr, result, detail);
1465 } else {
1466 HILOGI("The observer is not registered!");
1467 }
1468 };
1469
AddToNowPlaying(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1470 int AvrcpCtService::AddToNowPlaying(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1471 {
1472 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1473 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1474 int result = RET_BAD_STATUS;
1475
1476 do {
1477 if (!IsEnabled()) {
1478 break;
1479 }
1480
1481 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1482 break;
1483 }
1484
1485 if (profile_->IsVendorQueueFull(rawAddr)) {
1486 break;
1487 }
1488
1489 RawAddress peerAddr(rawAddr.GetAddress());
1490 GetDispatcher()->PostTask(
1491 std::bind(&AvrcpCtService::AddToNowPlayingNative, this, peerAddr, scope, uid, uidCounter));
1492 result = RET_NO_ERROR;
1493 } while (false);
1494
1495 return result;
1496 }
1497
AddToNowPlayingNative(RawAddress rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1498 void AvrcpCtService::AddToNowPlayingNative(RawAddress rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1499 {
1500 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1501 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1502 do {
1503 if (!IsEnabled()) {
1504 break;
1505 }
1506
1507 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1508 break;
1509 }
1510
1511 profile_->SendAddToNowPlayingCmd(rawAddr, scope, uid, uidCounter);
1512 } while (false);
1513 }
1514
OnAddToNowPlaying(const RawAddress & rawAddr,int result,int detail) const1515 void AvrcpCtService::OnAddToNowPlaying(const RawAddress &rawAddr, int result, int detail) const
1516 {
1517 HILOGI("address: %{public}s, result: %{public}d, detail: %{public}d",
1518 GET_ENCRYPT_AVRCP_ADDR(rawAddr), result, detail);
1519
1520 if (myObserver_ != nullptr) {
1521 myObserver_->OnAddToNowPlaying(rawAddr, result, detail);
1522 } else {
1523 HILOGI("The observer is not registered!");
1524 }
1525 }
1526
1527 /******************************************************************
1528 * CONTINUING RESPONSE / ABORT CONTINUING RESPONSE *
1529 ******************************************************************/
1530
RequestContinuingResponse(const RawAddress & rawAddr,uint8_t pduId)1531 int AvrcpCtService::RequestContinuingResponse(const RawAddress &rawAddr, uint8_t pduId)
1532 {
1533 HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1534 int result = RET_BAD_STATUS;
1535
1536 do {
1537 if (!IsEnabled()) {
1538 break;
1539 }
1540
1541 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1542 break;
1543 }
1544
1545 if (profile_->IsVendorQueueFull(rawAddr)) {
1546 break;
1547 }
1548
1549 RawAddress peerAddr(rawAddr.GetAddress());
1550 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::RequestContinuingResponseNative, this, peerAddr, pduId));
1551 result = RET_NO_ERROR;
1552 } while (false);
1553
1554 return result;
1555 }
1556
RequestContinuingResponseNative(RawAddress rawAddr,uint8_t pduId)1557 void AvrcpCtService::RequestContinuingResponseNative(RawAddress rawAddr, uint8_t pduId)
1558 {
1559 HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1560
1561 do {
1562 if (!IsEnabled()) {
1563 break;
1564 }
1565
1566 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1567 break;
1568 }
1569
1570 profile_->SendRequestContinuingResponseCmd(rawAddr, pduId);
1571 } while (false);
1572 }
1573
AbortContinuingResponse(const RawAddress & rawAddr,uint8_t pduId)1574 int AvrcpCtService::AbortContinuingResponse(const RawAddress &rawAddr, uint8_t pduId)
1575 {
1576 HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1577
1578 int result = RET_BAD_STATUS;
1579
1580 do {
1581 if (!IsEnabled()) {
1582 break;
1583 }
1584
1585 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1586 break;
1587 }
1588
1589 if (profile_->IsVendorQueueFull(rawAddr)) {
1590 break;
1591 }
1592
1593 RawAddress peerAddr(rawAddr.GetAddress());
1594 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::AbortContinuingResponseNative, this, peerAddr, pduId));
1595 result = RET_NO_ERROR;
1596 } while (false);
1597
1598 return result;
1599 }
1600
AbortContinuingResponseNative(RawAddress rawAddr,uint8_t pduId)1601 void AvrcpCtService::AbortContinuingResponseNative(RawAddress rawAddr, uint8_t pduId)
1602 {
1603 HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1604
1605 do {
1606 if (!IsEnabled()) {
1607 break;
1608 }
1609
1610 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1611 break;
1612 }
1613
1614 profile_->SendAbortContinuingResponseCmd(rawAddr, pduId);
1615 } while (false);
1616 }
1617
1618 /******************************************************************
1619 * OPERATE THE VIRTUAL FILE SYSTEM *
1620 ******************************************************************/
1621
ChangePath(const RawAddress & rawAddr,uint16_t uidCounter,uint8_t direction,uint64_t folderUid)1622 int AvrcpCtService::ChangePath(const RawAddress &rawAddr, uint16_t uidCounter, uint8_t direction, uint64_t folderUid)
1623 {
1624 HILOGI("address: %{public}s, uidCounter: %{public}d, direction: %{public}d, folderUid: %{public}llu",
1625 GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, direction, static_cast<unsigned long long>(folderUid));
1626 int result = RET_BAD_STATUS;
1627
1628 do {
1629 if (!IsEnabled()) {
1630 break;
1631 }
1632
1633 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1634 break;
1635 }
1636
1637 if (profile_->IsBrowseQueueFull(rawAddr)) {
1638 break;
1639 }
1640
1641 if (!profile_->IsBrowsingConnected(rawAddr)) {
1642 break;
1643 }
1644 RawAddress peerAddr(rawAddr.GetAddress());
1645 GetDispatcher()->PostTask(
1646 std::bind(&AvrcpCtService::ChangePathNative, this, peerAddr, uidCounter, direction, folderUid));
1647 result = RET_NO_ERROR;
1648 } while (false);
1649
1650 return result;
1651 }
1652
ChangePathNative(RawAddress rawAddr,uint16_t uidCounter,uint8_t direction,uint64_t folderUid)1653 void AvrcpCtService::ChangePathNative(RawAddress rawAddr, uint16_t uidCounter, uint8_t direction, uint64_t folderUid)
1654 {
1655 HILOGI("address: %{public}s, uidCounter: %{public}d, direction: %{public}d, folderUid: %{public}llu",
1656 GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, direction, static_cast<unsigned long long>(folderUid));
1657 do {
1658 if (!IsEnabled()) {
1659 break;
1660 }
1661
1662 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1663 break;
1664 }
1665
1666 profile_->SendChangePathCmd(rawAddr, uidCounter, direction, folderUid);
1667 } while (false);
1668 }
1669
OnChangePath(const RawAddress & rawAddr,uint32_t numOfItems,int result,int detail) const1670 void AvrcpCtService::OnChangePath(const RawAddress &rawAddr, uint32_t numOfItems, int result, int detail) const
1671 {
1672 HILOGI("address: %{public}s, numOfItems: %{public}u, result: %{public}d, detail: %{public}d",
1673 GET_ENCRYPT_AVRCP_ADDR(rawAddr), numOfItems, result, detail);
1674
1675 if (myObserver_ != nullptr) {
1676 myObserver_->OnChangePath(rawAddr, numOfItems, result, detail);
1677 } else {
1678 HILOGI("The observer is not registered!");
1679 }
1680 }
1681
GetFolderItems(const RawAddress & rawAddr,uint8_t scope,uint32_t startItem,uint32_t endItem,const std::vector<uint32_t> & attributes)1682 int AvrcpCtService::GetFolderItems(const RawAddress &rawAddr, uint8_t scope, uint32_t startItem, uint32_t endItem,
1683 const std::vector<uint32_t> &attributes)
1684 {
1685 HILOGI("address: %{public}s, scope: %{public}d, startItem: %{public}u, endItem: %{public}u",
1686 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, startItem, endItem);
1687 int result = RET_BAD_STATUS;
1688
1689 do {
1690 if (!IsEnabled()) {
1691 break;
1692 }
1693
1694 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1695 break;
1696 }
1697
1698 if (profile_->IsBrowseQueueFull(rawAddr)) {
1699 break;
1700 }
1701
1702 if (!profile_->IsBrowsingConnected(rawAddr)) {
1703 break;
1704 }
1705
1706 RawAddress peerAddr(rawAddr.GetAddress());
1707 GetDispatcher()->PostTask(
1708 std::bind(&AvrcpCtService::GetFolderItemsNative, this, peerAddr, scope, startItem, endItem, attributes));
1709 result = RET_NO_ERROR;
1710 } while (false);
1711
1712 return result;
1713 }
1714
GetFolderItemsNative(RawAddress rawAddr,uint8_t scope,uint32_t startItem,uint32_t endItem,std::vector<uint32_t> attributes)1715 void AvrcpCtService::GetFolderItemsNative(
1716 RawAddress rawAddr, uint8_t scope, uint32_t startItem, uint32_t endItem, std::vector<uint32_t> attributes)
1717 {
1718 HILOGI("address: %{public}s, scope: %{public}d, startItem: %{public}u, endItem: %{public}u",
1719 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, startItem, endItem);
1720 do {
1721 if (!IsEnabled()) {
1722 break;
1723 }
1724
1725 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1726 break;
1727 }
1728
1729 profile_->SendGetFolderItemsCmd(rawAddr, scope, startItem, endItem, attributes);
1730 } while (false);
1731 }
1732
OnGetFolderItems(const RawAddress & rawAddr,uint8_t scope,uint16_t uidCounter,const std::vector<AvrcMpItem> & mpItems,const std::vector<AvrcMeItem> & meItems,int result,int detail) const1733 void AvrcpCtService::OnGetFolderItems(const RawAddress &rawAddr, uint8_t scope, uint16_t uidCounter,
1734 const std::vector<AvrcMpItem> &mpItems, const std::vector<AvrcMeItem> &meItems, int result, int detail) const
1735 {
1736 HILOGI("address: %{public}s, scope: %{public}d, uidCounter: %{public}d, result: %{public}d, detail: %{public}d",
1737 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, uidCounter, result, detail);
1738
1739 if (myObserver_ != nullptr) {
1740 if (scope == AVRC_MEDIA_SCOPE_PLAYER_LIST) {
1741 myObserver_->OnGetMediaPlayers(rawAddr, uidCounter, mpItems, result, detail);
1742 } else {
1743 myObserver_->OnGetFolderItems(rawAddr, uidCounter, meItems, result, detail);
1744 }
1745 } else {
1746 HILOGI("The observer is not registered!");
1747 }
1748 }
1749
GetItemAttributes(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter,const std::vector<uint32_t> & attributes)1750 int AvrcpCtService::GetItemAttributes(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter,
1751 const std::vector<uint32_t> &attributes)
1752 {
1753 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1754 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1755 int result = RET_BAD_STATUS;
1756
1757 do {
1758 if (!IsEnabled()) {
1759 break;
1760 }
1761
1762 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1763 break;
1764 }
1765
1766 if (profile_->IsBrowseQueueFull(rawAddr)) {
1767 break;
1768 }
1769
1770 if (!profile_->IsBrowsingConnected(rawAddr)) {
1771 break;
1772 }
1773
1774 RawAddress peerAddr(rawAddr.GetAddress());
1775 GetDispatcher()->PostTask(
1776 std::bind(&AvrcpCtService::GetItemAttributesNative, this, peerAddr, scope, uid, uidCounter, attributes));
1777 result = RET_NO_ERROR;
1778 } while (false);
1779
1780 return result;
1781 }
1782
GetItemAttributesNative(RawAddress rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter,std::vector<uint32_t> attributes)1783 void AvrcpCtService::GetItemAttributesNative(
1784 RawAddress rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter, std::vector<uint32_t> attributes)
1785 {
1786 HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}llu, uidCounter: %{public}d",
1787 GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter);
1788 do {
1789 if (!IsEnabled()) {
1790 break;
1791 }
1792
1793 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1794 break;
1795 }
1796
1797 profile_->SendGetItemAttributesCmd(rawAddr, scope, uid, uidCounter, attributes);
1798 } while (false);
1799 }
1800
OnGetItemAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attributes,const std::vector<std::string> & values,int result,int detail) const1801 void AvrcpCtService::OnGetItemAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
1802 const std::vector<std::string> &values, int result, int detail) const
1803 {
1804 HILOGI("addr:%{public}s, result:%{public}d, detail:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result, detail);
1805
1806 if (myObserver_ != nullptr) {
1807 myObserver_->OnGetItemAttributes(rawAddr, attributes, values, result, detail);
1808 } else {
1809 HILOGI("The observer is not registered!");
1810 }
1811 }
1812
GetTotalNumberOfItems(const RawAddress & rawAddr,uint8_t scope)1813 int AvrcpCtService::GetTotalNumberOfItems(const RawAddress &rawAddr, uint8_t scope)
1814 {
1815 HILOGI("addr:%{public}s, scope:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope);
1816
1817 int result = RET_BAD_STATUS;
1818
1819 do {
1820 if (!IsEnabled()) {
1821 break;
1822 }
1823
1824 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1825 break;
1826 }
1827
1828 if (profile_->IsBrowseQueueFull(rawAddr)) {
1829 break;
1830 }
1831
1832 if (!profile_->IsBrowsingConnected(rawAddr)) {
1833 break;
1834 }
1835
1836 RawAddress peerAddr(rawAddr.GetAddress());
1837 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::GetTotalNumberOfItemsNative, this, peerAddr, scope));
1838 result = RET_NO_ERROR;
1839 } while (false);
1840
1841 return result;
1842 }
1843
GetTotalNumberOfItemsNative(RawAddress rawAddr,uint8_t scope)1844 void AvrcpCtService::GetTotalNumberOfItemsNative(RawAddress rawAddr, uint8_t scope)
1845 {
1846 HILOGI("addr:%{public}s, scope:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope);
1847
1848 do {
1849 if (!IsEnabled()) {
1850 break;
1851 }
1852
1853 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1854 break;
1855 }
1856
1857 profile_->SendGetTotalNumberOfItemsCmd(rawAddr, scope);
1858 } while (false);
1859 }
1860
OnGetTotalNumberOfItems(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,int result,int detail) const1861 void AvrcpCtService::OnGetTotalNumberOfItems(
1862 const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems, int result, int detail) const
1863 {
1864 HILOGI("addr:%{public}s, uidCounter:%{public}d, numOfItems:%{public}u, result:%{public}d, detail:%{public}d",
1865 GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems, result, detail);
1866
1867 if (myObserver_ != nullptr) {
1868 myObserver_->OnGetTotalNumberOfItems(rawAddr, uidCounter, numOfItems, result, detail);
1869 } else {
1870 HILOGI("The observer is not registered!");
1871 }
1872 }
1873
1874 /******************************************************************
1875 * ABSOLUTE VOLUME *
1876 ******************************************************************/
1877
SetAbsoluteVolume(const RawAddress & rawAddr,uint8_t volume)1878 int AvrcpCtService::SetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume)
1879 {
1880 HILOGI("addr: %{public}s, volume: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume);
1881
1882 int result = RET_BAD_STATUS;
1883
1884 do {
1885 if (!IsEnabled()) {
1886 break;
1887 }
1888
1889 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1890 break;
1891 }
1892
1893 if (profile_->IsVendorQueueFull(rawAddr)) {
1894 break;
1895 }
1896
1897 if (profile_->IsDisableAbsoluteVolume(rawAddr)) {
1898 result = RET_NO_SUPPORT;
1899 break;
1900 }
1901
1902 RawAddress peerAddr(rawAddr.GetAddress());
1903 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::SetAbsoluteVolumeNative, this, peerAddr, volume));
1904 result = RET_NO_ERROR;
1905 } while (false);
1906
1907 return result;
1908 }
1909
SetAbsoluteVolumeNative(RawAddress rawAddr,uint8_t volume)1910 void AvrcpCtService::SetAbsoluteVolumeNative(RawAddress rawAddr, uint8_t volume)
1911 {
1912 HILOGI("addr: %{public}s, volume: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume);
1913
1914 do {
1915 if (!IsEnabled()) {
1916 break;
1917 }
1918
1919 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1920 break;
1921 }
1922
1923 profile_->SendSetAbsoluteVolumeCmd(rawAddr, volume);
1924 } while (false);
1925 }
1926
OnSetAbsoluteVolume(const RawAddress & rawAddr,uint8_t volume,int result) const1927 void AvrcpCtService::OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, int result) const
1928 {
1929 HILOGI("addr:%{public}s, volume:%{public}d, result:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, result);
1930
1931 if (myObserver_ != nullptr) {
1932 myObserver_->OnSetAbsoluteVolume(rawAddr, volume, result);
1933 } else {
1934 HILOGI("The observer is not registered!");
1935 }
1936 }
1937
1938 /******************************************************************
1939 * NOTIFICATION *
1940 ******************************************************************/
1941
EnableNotification(const RawAddress & rawAddr,const std::vector<uint8_t> & events,uint8_t interval)1942 int AvrcpCtService::EnableNotification(const RawAddress &rawAddr, const std::vector<uint8_t> &events, uint8_t interval)
1943 {
1944 HILOGI("addr: %{public}s, interval: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), interval);
1945
1946 int result = RET_BAD_STATUS;
1947
1948 do {
1949 if (!IsEnabled()) {
1950 break;
1951 }
1952
1953 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1954 break;
1955 }
1956
1957 if (profile_->IsVendorQueueFull(rawAddr)) {
1958 break;
1959 }
1960
1961 RawAddress peerAddr(rawAddr.GetAddress());
1962 GetDispatcher()->PostTask(
1963 std::bind(&AvrcpCtService::EnableNotificationNative, this, peerAddr, events, interval));
1964 result = RET_NO_ERROR;
1965 } while (false);
1966
1967 return result;
1968 }
1969
DisableNotification(const RawAddress & rawAddr,const std::vector<uint8_t> & events)1970 int AvrcpCtService::DisableNotification(const RawAddress &rawAddr, const std::vector<uint8_t> &events)
1971 {
1972 HILOGI("addr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1973
1974 int result = RET_BAD_STATUS;
1975
1976 do {
1977 if (!IsEnabled()) {
1978 break;
1979 }
1980
1981 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1982 break;
1983 }
1984
1985 if (profile_->IsVendorQueueFull(rawAddr)) {
1986 break;
1987 }
1988
1989 RawAddress peerAddr(rawAddr.GetAddress());
1990 GetDispatcher()->PostTask(std::bind(&AvrcpCtService::DisableNotificationNative, this, peerAddr, events));
1991 result = RET_NO_ERROR;
1992 } while (false);
1993
1994 return result;
1995 }
1996
EnableNotificationNative(RawAddress rawAddr,std::vector<uint8_t> events,uint8_t interval)1997 void AvrcpCtService::EnableNotificationNative(RawAddress rawAddr, std::vector<uint8_t> events, uint8_t interval)
1998 {
1999 HILOGI("addr: %{public}s, interval: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), interval);
2000
2001 do {
2002 if (!IsEnabled()) {
2003 break;
2004 }
2005
2006 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2007 break;
2008 }
2009
2010 profile_->EnableNotification(rawAddr, events, interval);
2011 } while (false);
2012 }
2013
DisableNotificationNative(RawAddress rawAddr,std::vector<uint8_t> events)2014 void AvrcpCtService::DisableNotificationNative(RawAddress rawAddr, std::vector<uint8_t> events)
2015 {
2016 HILOGI("addr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2017
2018 do {
2019 if (!IsEnabled()) {
2020 break;
2021 }
2022
2023 if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2024 break;
2025 }
2026
2027 profile_->DisableNotification(rawAddr, events);
2028 } while (false);
2029 }
2030
OnPlaybackStatusChanged(const RawAddress & rawAddr,uint8_t playStatus,int result) const2031 void AvrcpCtService::OnPlaybackStatusChanged(const RawAddress &rawAddr, uint8_t playStatus, int result) const
2032 {
2033 HILOGI("addr: %{public}s, playStatus: %{public}d, result: %{public}d",
2034 GET_ENCRYPT_AVRCP_ADDR(rawAddr), playStatus, result);
2035
2036 if (myObserver_ != nullptr) {
2037 myObserver_->OnPlaybackStatusChanged(rawAddr, playStatus, result);
2038 } else {
2039 HILOGI("The observer is not registered!");
2040 }
2041 }
2042
OnTrackChanged(const RawAddress & rawAddr,uint64_t uid,int result) const2043 void AvrcpCtService::OnTrackChanged(const RawAddress &rawAddr, uint64_t uid, int result) const
2044 {
2045 HILOGI("addr: %{public}s, uid: %{public}jx, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), uid, result);
2046
2047 if (myObserver_ != nullptr) {
2048 myObserver_->OnTrackChanged(rawAddr, uid, result);
2049 } else {
2050 HILOGI("The observer is not registered!");
2051 }
2052 }
2053
OnTrackReachedEnd(const RawAddress & rawAddr,int result) const2054 void AvrcpCtService::OnTrackReachedEnd(const RawAddress &rawAddr, int result) const
2055 {
2056 HILOGI("addr: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
2057
2058 if (myObserver_ != nullptr) {
2059 myObserver_->OnTrackReachedEnd(rawAddr, result);
2060 } else {
2061 HILOGI("The observer is not registered!");
2062 }
2063 }
2064
OnTrackReachedStart(const RawAddress & rawAddr,int result) const2065 void AvrcpCtService::OnTrackReachedStart(const RawAddress &rawAddr, int result) const
2066 {
2067 HILOGI("addr: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
2068
2069 if (myObserver_ != nullptr) {
2070 myObserver_->OnTrackReachedStart(rawAddr, result);
2071 } else {
2072 HILOGI("The observer is not registered!");
2073 }
2074 }
2075
OnPlaybackPosChanged(const RawAddress & rawAddr,uint32_t playbackPos,int result) const2076 void AvrcpCtService::OnPlaybackPosChanged(const RawAddress &rawAddr, uint32_t playbackPos, int result) const
2077 {
2078 HILOGI("addr: %{public}s, playbackPos: %{public}d, result: %{public}d",
2079 GET_ENCRYPT_AVRCP_ADDR(rawAddr), playbackPos, result);
2080
2081 if (myObserver_ != nullptr) {
2082 myObserver_->OnPlaybackPosChanged(rawAddr, playbackPos, result);
2083 } else {
2084 HILOGI("The observer is not registered!");
2085 }
2086 }
2087
OnPlayerApplicationSettingChanged(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values,int result) const2088 void AvrcpCtService::OnPlayerApplicationSettingChanged(const RawAddress &rawAddr,
2089 const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values, int result) const
2090 {
2091 HILOGI("addr: %{public}s, attributes.size: %{public}zu, values.size: %{public}zu, result: %{public}d",
2092 GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size(), values.size(), result);
2093
2094 if (myObserver_ != nullptr) {
2095 myObserver_->OnPlayerAppSettingChanged(rawAddr, attributes, values, result);
2096 } else {
2097 HILOGI("The observer is not registered!");
2098 }
2099 }
2100
OnNowPlayingContentChanged(const RawAddress & rawAddr,int result) const2101 void AvrcpCtService::OnNowPlayingContentChanged(const RawAddress &rawAddr, int result) const
2102 {
2103 HILOGI("addr: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
2104
2105 if (myObserver_ != nullptr) {
2106 myObserver_->OnNowPlayingContentChanged(rawAddr, result);
2107 } else {
2108 HILOGI("The observer is not registered!");
2109 }
2110 }
2111
OnAvailablePlayersChanged(const RawAddress & rawAddr,int result) const2112 void AvrcpCtService::OnAvailablePlayersChanged(const RawAddress &rawAddr, int result) const
2113 {
2114 HILOGI("addr: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
2115
2116 if (myObserver_ != nullptr) {
2117 myObserver_->OnAvailablePlayersChanged(rawAddr, result);
2118 } else {
2119 HILOGI("The observer is not registered!");
2120 }
2121 }
2122
OnAddressedPlayerChanged(const RawAddress & rawAddr,uint16_t playerId,uint16_t uidCounter,int result) const2123 void AvrcpCtService::OnAddressedPlayerChanged(
2124 const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, int result) const
2125 {
2126 HILOGI("addr: %{public}s, playerId: %{public}d, uidCounter: %{public}d, result: %{public}d",
2127 GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId, uidCounter, result);
2128
2129 if (myObserver_ != nullptr) {
2130 myObserver_->OnAddressedPlayerChanged(rawAddr, playerId, uidCounter, result);
2131 } else {
2132 HILOGI("The observer is not registered!");
2133 }
2134 }
2135
OnUidChanged(const RawAddress & rawAddr,uint16_t uidCounter,int result) const2136 void AvrcpCtService::OnUidChanged(const RawAddress &rawAddr, uint16_t uidCounter, int result) const
2137 {
2138 HILOGI("addr: %{public}s, uidCounter: %{public}d, result: %{public}d",
2139 GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, result);
2140
2141 if (myObserver_ != nullptr) {
2142 myObserver_->OnUidChanged(rawAddr, uidCounter, result);
2143 } else {
2144 HILOGI("The observer is not registered!");
2145 }
2146 }
2147
OnVolumeChanged(const RawAddress & rawAddr,uint8_t volume,int result) const2148 void AvrcpCtService::OnVolumeChanged(const RawAddress &rawAddr, uint8_t volume, int result) const
2149 {
2150 HILOGI("addr: %{public}s, volume: %{public}d, result: %{public}d",
2151 GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, result);
2152
2153 if (myObserver_ != nullptr) {
2154 myObserver_->OnVolumeChanged(rawAddr, volume, result);
2155 } else {
2156 HILOGI("The observer is not registered!");
2157 }
2158 }
2159
ProcessChannelEvent(RawAddress rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context)2160 void AvrcpCtService::ProcessChannelEvent(
2161 RawAddress rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context)
2162 {
2163 HILOGI("addr: %{public}s, connectId: %{public}d, event: %{public}d, result: %{public}d",
2164 GET_ENCRYPT_AVRCP_ADDR(rawAddr), connectId, event, result);
2165 if (!IsDisabled()) {
2166 profile_->ProcessChannelEvent(rawAddr, connectId, event, result, context);
2167 }
2168 }
2169
ProcessChannelMessage(uint8_t connectId,uint8_t label,uint8_t crType,uint8_t chType,Packet * pkt,void * context)2170 void AvrcpCtService::ProcessChannelMessage(
2171 uint8_t connectId, uint8_t label, uint8_t crType, uint8_t chType, Packet *pkt, void *context)
2172 {
2173 HILOGI("connectId: %{public}d, label: %{public}d, crType: %{public}d, chType: %{public}d",
2174 connectId, label, crType, chType);
2175
2176 if (!IsDisabled()) {
2177 profile_->ProcessChannelMessage(connectId, label, crType, chType, pkt, context);
2178 }
2179 }
2180
ChannelEventCallback(uint8_t connectId,uint8_t event,uint16_t result,const BtAddr * btAddr,void * context)2181 void AvrcpCtService::ChannelEventCallback(
2182 uint8_t connectId, uint8_t event, uint16_t result, const BtAddr *btAddr, void *context)
2183 {
2184 HILOGI("connectId: %{public}d, event: %{public}d, result: %{public}d", connectId, event, result);
2185 auto servManager = IProfileManager::GetInstance();
2186 auto service = static_cast<AvrcpCtService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
2187 RawAddress rawAddr(RawAddress::ConvertToString(btAddr->addr));
2188
2189 if (service != nullptr) {
2190 switch (event) {
2191 case AVCT_CONNECT_IND_EVT:
2192 case AVCT_CONNECT_CFM_EVT:
2193 if (result != RET_NO_ERROR) {
2194 service->DecConnectionNum();
2195 }
2196 break;
2197 case AVCT_DISCONNECT_IND_EVT:
2198 case AVCT_DISCONNECT_CFM_EVT:
2199 service->DecConnectionNum();
2200 break;
2201 default:
2202 break;
2203 }
2204 service->GetDispatcher()->PostTask(
2205 std::bind(&AvrcpCtService::ProcessChannelEvent, service, rawAddr, connectId, event, result, context));
2206 }
2207 }
2208
ChannelMessageCallback(uint8_t connectId,uint8_t label,uint8_t crType,uint8_t chType,Packet * pkt,void * context)2209 void AvrcpCtService::ChannelMessageCallback(
2210 uint8_t connectId, uint8_t label, uint8_t crType, uint8_t chType, Packet *pkt, void *context)
2211 {
2212 HILOGI("connectId: %{public}d, label: %{public}d, crType: %{public}d, chType: %{public}d",
2213 connectId, label, crType, chType);
2214
2215 auto servManager = IProfileManager::GetInstance();
2216 auto service = static_cast<AvrcpCtService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_CT));
2217 auto myPkt = PacketRefMalloc(pkt);
2218
2219 if (service != nullptr) {
2220 service->GetDispatcher()->PostTask(std::bind(
2221 &AvrcpCtService::ProcessChannelMessage, service, connectId, label, crType, chType, myPkt, context));
2222 }
2223 }
2224
CheckConnectionNum()2225 bool AvrcpCtService::CheckConnectionNum()
2226 {
2227 if (++currentConn_ > maxConnection_) {
2228 currentConn_ = maxConnection_;
2229 return false;
2230 } else {
2231 return true;
2232 }
2233 }
2234
2235 REGISTER_CLASS_CREATOR(AvrcpCtService);
2236 } // namespace bluetooth
2237 } // namespace OHOS
2238