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