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_profile.h"
17 #include "avrcp_ct_browse.h"
18 #include "avrcp_ct_connection.h"
19 #include "avrcp_ct_notification.h"
20 #include "avrcp_ct_pass_through.h"
21 #include "avrcp_ct_sub_unit_info.h"
22 #include "avrcp_ct_unit_info.h"
23 #include "avrcp_ct_vendor.h"
24 #include "avrcp_ct_vendor_continuation.h"
25 #include "avrcp_ct_vendor_player_application_settings.h"
26 #include "compat.h"
27 #include "power_manager.h"
28 
29 namespace OHOS {
30 namespace bluetooth {
31 bool AvrcCtProfile::g_isEnabled = false;
32 
AvrcCtProfile(uint16_t features,uint32_t companyId,uint16_t controlMtu,uint16_t browseMtu,uint8_t maxFragments,utility::Dispatcher * dispatcher,AvctChannelEventCallback eventCallback,AvctMsgCallback msgCallback)33 AvrcCtProfile::AvrcCtProfile(uint16_t features, uint32_t companyId, uint16_t controlMtu, uint16_t browseMtu,
34     uint8_t maxFragments, utility::Dispatcher *dispatcher, AvctChannelEventCallback eventCallback,
35     AvctMsgCallback msgCallback)
36     : features_(features),
37       companyId_(companyId),
38       controlMtu_(controlMtu),
39       browseMtu_(browseMtu),
40       maxFragments_(maxFragments),
41       dispatcher_(dispatcher),
42       connectId_(0xFF),
43       eventCallback_(eventCallback),
44       msgCallback_(msgCallback)
45 {
46     HILOGI("features: %{public}d, companyId: %{public}u, controlMtu: %{public}d, browseMtu: %{public}d, "
47         "maxFragments: %{public}d", features, companyId, controlMtu, browseMtu, maxFragments);
48 
49     SetEnableFlag(false);
50 }
51 
IsEnabled(void)52 bool AvrcCtProfile::IsEnabled(void)
53 {
54     HILOGI("enter");
55 
56     return g_isEnabled;
57 }
58 
~AvrcCtProfile()59 AvrcCtProfile::~AvrcCtProfile()
60 {
61     HILOGI("enter");
62 
63     SetEnableFlag(false);
64 }
65 
66 /******************************************************************
67  * REGISTER / UNREGISTER OBSERVER                                 *
68  ******************************************************************/
69 
RegisterObserver(AvrcCtProfile::Observer * observer)70 void AvrcCtProfile::RegisterObserver(AvrcCtProfile::Observer *observer)
71 {
72     HILOGI("enter");
73 
74     myObserver_ = observer;
75 }
76 
UnregisterObserver(void)77 void AvrcCtProfile::UnregisterObserver(void)
78 {
79     HILOGI("enter");
80 
81     myObserver_ = nullptr;
82 }
83 
84 /******************************************************************
85  * ENABLE / DISABLE                                               *
86  ******************************************************************/
87 
Enable(bool isTgEnabled)88 int AvrcCtProfile::Enable(bool isTgEnabled)
89 {
90     HILOGI("isTgEnabled: %{public}d", isTgEnabled);
91 
92     int result = BT_SUCCESS;
93     SetEnableFlag(true);
94 
95     if (!isTgEnabled) {
96         /// If the following statement in the debt_config.xml. That means that TG allows to enable. CT do not
97         /// allow the passive connection. Because if CT and TG are registered passive connections, it will case that the
98         /// inability to distinguish between the end device
99         /// "<T1 property=" AvrcpTgService ">true</T1>"
100         AVCT_Register(controlMtu_, browseMtu_, AVCT_CT);
101 
102         AvctConnectParam param = {eventCallback_, msgCallback_, AVRC_CT_AV_REMOTE_CONTROL, AVCT_ACPT, nullptr};
103         BtAddr btAddr = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x00};
104         if (AVCT_ConnectReq(&connectId_, &param, &btAddr) != AVCT_SUCCESS) {
105             result = RET_BAD_STATUS;
106         }
107     } else {
108         /// CT is only allowed the active connection.
109     }
110 
111     return result;
112 }
113 
Disable(void)114 int AvrcCtProfile::Disable(void)
115 {
116     HILOGI("enter");
117 
118     do {
119         std::lock_guard<std::recursive_mutex> lock(mutex_);
120         SetEnableFlag(false);
121     } while (false);
122 
123     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
124     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
125 
126     if (!cnManager->IsConnectInfoEmpty()) {
127         utility::Message msg(AVRC_CT_SM_EVENT_TO_DISABLE_STATE);
128         smManager->SendMessageToAllBrowseStateMachine(msg);
129         smManager->SendMessageToAllControlStateMachine(msg);
130     } else {
131         AvrcpCtSafeDelete(cnManager);
132         smManager->ShutDown();
133         myObserver_->onDisabled();
134     }
135 
136     return BT_SUCCESS;
137 }
138 
SetEnableFlag(bool isEnabled)139 void AvrcCtProfile::SetEnableFlag(bool isEnabled)
140 {
141     HILOGI("isEnabled: %{public}d", isEnabled);
142 
143     g_isEnabled = isEnabled;
144 }
145 
146 /******************************************************************
147  * CONNECTION                                                     *
148  ******************************************************************/
149 
GetConnectedDevices(void)150 std::vector<bluetooth::RawAddress> AvrcCtProfile::GetConnectedDevices(void)
151 {
152     HILOGI("enter");
153     std::lock_guard<std::recursive_mutex> lock(mutex_);
154 
155     std::vector<bluetooth::RawAddress> result;
156 
157     std::list<std::string> addrs = AvrcCtConnectManager::GetInstance()->GetDeviceAddresses();
158     for (auto &addr : addrs) {
159         RawAddress rawAddr(addr);
160         if (GetDeviceState(rawAddr) == static_cast<int>(BTConnectState::CONNECTED)) {
161             result.push_back(rawAddr);
162         }
163     }
164 
165     return result;
166 }
167 
GetDevicesByStates(const std::vector<int> & states)168 std::vector<RawAddress> AvrcCtProfile::GetDevicesByStates(const std::vector<int> &states)
169 {
170     HILOGI("enter");
171     std::lock_guard<std::recursive_mutex> lock(mutex_);
172 
173     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
174 
175     std::vector<RawAddress> rawAddrs;
176     std::list<std::string> addrs;
177 
178     for (auto &state : states) {
179         if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
180             addrs = cnManager->GetAllDisconnectedDevices();
181         } else {
182             addrs = cnManager->GetDeviceAddresses();
183         }
184         for (auto &addr : addrs) {
185             RawAddress rawAddr(addr);
186             if (GetDeviceState(rawAddr) == state) {
187                 rawAddrs.push_back(rawAddr);
188             }
189         }
190     }
191 
192     return rawAddrs;
193 }
194 
GetDeviceState(const RawAddress & rawAddr)195 int AvrcCtProfile::GetDeviceState(const RawAddress &rawAddr)
196 {
197     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
198     std::lock_guard<std::recursive_mutex> lock(mutex_);
199 
200     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
201     int result;
202 
203     if (smManager->IsControlConnectingState(rawAddr)) {
204         result = static_cast<int>(BTConnectState::CONNECTING);
205     } else if (smManager->IsControlConnectedState(rawAddr) || smManager->IsControlPendingState(rawAddr) ||
206                smManager->IsControlContinuationState(rawAddr)) {
207         result = static_cast<int>(BTConnectState::CONNECTED);
208     } else if (smManager->IsControlDisconnectingState(rawAddr)) {
209         result = static_cast<int>(BTConnectState::DISCONNECTING);
210     } else if (smManager->IsControlDisableState(rawAddr)) {
211         result = static_cast<int>(BTConnectState::DISCONNECTING);
212     } else {
213         result = static_cast<int>(BTConnectState::DISCONNECTED);
214     }
215 
216     return result;
217 }
218 
GetMaxConnectNum(void)219 int AvrcCtProfile::GetMaxConnectNum(void)
220 {
221     HILOGI("enter");
222     std::lock_guard<std::recursive_mutex> lock(mutex_);
223 
224     int result = 0x00;
225 
226     std::list<std::string> addrs = AvrcCtConnectManager::GetInstance()->GetDeviceAddresses();
227 
228     for (auto &addr : addrs) {
229         RawAddress rawAddr(addr);
230         if (GetDeviceState(rawAddr) == static_cast<int>(BTConnectState::CONNECTED)) {
231             ++result;
232         }
233     }
234 
235     return result;
236 }
237 
Connect(const RawAddress & rawAddr) const238 int AvrcCtProfile::Connect(const RawAddress &rawAddr) const
239 {
240     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
241 
242     int result = BT_SUCCESS;
243 
244     bool absVolume = Compat::CompatCheck(bluetooth::CompatType::COMPAT_REJECT_ABSOLUTE_VOLUME, rawAddr.GetAddress());
245     result |= AvrcCtConnectManager::GetInstance()->Add(
246         rawAddr, 0x00, AVCT_INIT, controlMtu_, browseMtu_, companyId_, 0x0000, absVolume, eventCallback_, msgCallback_);
247     result |= AvrcCtStateMachineManager::GetInstance()->AddControlStateMachine(rawAddr);
248     if (result == BT_SUCCESS) {
249         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::CONNECTING));
250     }
251 
252     return result;
253 }
254 
Disconnect(const RawAddress & rawAddr) const255 int AvrcCtProfile::Disconnect(const RawAddress &rawAddr) const
256 {
257     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
258 
259     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
260     utility::Message msg(AVRC_CT_SM_EVENT_TO_DISCONNECTING_STATE);
261     bool result = true;
262 
263     result &= smManager->SendMessageToBrowseStateMachine(rawAddr, msg);
264     result &= smManager->SendMessageToControlStateMachine(rawAddr, msg);
265     if (result) {
266         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTING));
267     }
268 
269     return BT_SUCCESS;
270 }
271 
ConnectBr(const RawAddress & rawAddr)272 int AvrcCtProfile::ConnectBr(const RawAddress &rawAddr)
273 {
274     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
275 
276     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
277 
278     return smManager->AddBrowseStateMachine(rawAddr);
279 }
280 
DisconnectBr(const RawAddress & rawAddr)281 int AvrcCtProfile::DisconnectBr(const RawAddress &rawAddr)
282 {
283     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
284 
285     utility::Message msg(AVRC_CT_SM_EVENT_TO_DISCONNECTING_STATE);
286     AvrcCtStateMachineManager::GetInstance()->SendMessageToBrowseStateMachine(rawAddr, msg);
287 
288     return BT_SUCCESS;
289 }
290 
GetConnectState(void)291 int AvrcCtProfile::GetConnectState(void)
292 {
293     HILOGI("enter");
294     std::lock_guard<std::recursive_mutex> lock(mutex_);
295 
296     int result = PROFILE_STATE_DISCONNECTED;
297 
298     std::list<std::string> addrs = AvrcCtConnectManager::GetInstance()->GetDeviceAddresses();
299     for (auto &addr : addrs) {
300         RawAddress rawAddr(addr);
301         switch (static_cast<BTConnectState>(GetDeviceState(rawAddr))) {
302             case BTConnectState::CONNECTING:
303                 result |= PROFILE_STATE_CONNECTING;
304                 break;
305             case BTConnectState::CONNECTED:
306                 result |= PROFILE_STATE_CONNECTED;
307                 break;
308             case BTConnectState::DISCONNECTING:
309                 result |= PROFILE_STATE_DISCONNECTING;
310                 break;
311             case BTConnectState::DISCONNECTED:
312                 /// FALL THROUGH
313             default:
314                 result |= PROFILE_STATE_DISCONNECTED;
315                 break;
316         }
317     }
318 
319     return result;
320 }
321 
322 /******************************************************************
323  * PASS THROUGH COMMAND                                           *
324  ******************************************************************/
325 
SendPressButtonCmd(const RawAddress & rawAddr,uint8_t button)326 void AvrcCtProfile::SendPressButtonCmd(const RawAddress &rawAddr, uint8_t button)
327 {
328     HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
329 
330     std::shared_ptr<AvrcCtPassPacket> packet = std::make_shared<AvrcCtPassPacket>(button, AVRC_KEY_STATE_PRESS);
331     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
332     SendPassCmd(rawAddr, packet);
333     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
334 }
335 
SendReleaseButtonCmd(const RawAddress & rawAddr,uint8_t button)336 void AvrcCtProfile::SendReleaseButtonCmd(const RawAddress &rawAddr, uint8_t button)
337 {
338     HILOGI("address: %{public}s, button: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button);
339 
340     std::shared_ptr<AvrcCtPassPacket> packet = std::make_shared<AvrcCtPassPacket>(button, AVRC_KEY_STATE_RELEASE);
341     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
342     SendPassCmd(rawAddr, packet);
343     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
344 }
345 
IsPassQueueFull(const RawAddress & rawAddr)346 bool AvrcCtProfile::IsPassQueueFull(const RawAddress &rawAddr)
347 {
348     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
349     std::lock_guard<std::recursive_mutex> lock(mutex_);
350 
351     return (AvrcCtConnectManager::GetInstance()->GetSizeOfPassQueue(rawAddr) == AVRC_CT_DEFAULT_SIZE_OF_QUEUE);
352 }
353 
SendPassCmd(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtPassPacket> & pkt)354 void AvrcCtProfile::SendPassCmd(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtPassPacket> &pkt)
355 {
356     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
357 
358     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
359 
360     if (cnManager->IsPassPacketEmpty(rawAddr)) {
361         /// Unprocessed pass through command.
362 
363         /// Sets the information which is used in the "CONNECTED" state of the control state machine.
364         cnManager->SetPassPacket(rawAddr, pkt);
365 
366         utility::Message msg(AVRC_CT_SM_EVENT_PASS_THROUGH);
367         AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
368 
369         /// Sets the information which is used in the timeout.
370         auto func =
371             std::bind(&AvrcCtProfile::PassTimeoutCallback, this, rawAddr, pkt->GetKeyOperation(), pkt->GetKeyState());
372         cnManager->SetPassTimer(rawAddr, func, AVRC_CT_TIMER_PASS_THROUGH);
373     } else {
374         /// There is a command in processing,
375         if (cnManager->GetSizeOfPassQueue(rawAddr) >= AVRC_CT_DEFAULT_SIZE_OF_QUEUE) {
376             InformPassRsp(rawAddr, pkt->GetKeyOperation(), pkt->GetKeyState(), RET_BAD_STATUS);
377             HILOGI("The pass through command is full! - Address: %{public}s - sizeOf: %{public}d",
378                 GET_ENCRYPT_AVRCP_ADDR(rawAddr), AVRC_CT_DEFAULT_SIZE_OF_QUEUE);
379         } else {
380             cnManager->PushPassQueue(rawAddr, pkt);
381             HILOGI("Waiting for the response! - Address[%{public}s]", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
382         }
383     }
384 }
385 
SendNextPassCmd(const RawAddress & rawAddr)386 void AvrcCtProfile::SendNextPassCmd(const RawAddress &rawAddr)
387 {
388     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
389 
390     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
391     cnManager->ClearPassInfo(rawAddr);
392 
393     if (cnManager->GetSizeOfPassQueue(rawAddr) != 0x00) {
394         std::shared_ptr<AvrcCtPassPacket> packet = cnManager->PopPassQueue(rawAddr);
395         SendPassCmd(rawAddr, packet);
396     }
397 }
398 
ReceivePassRsp(const RawAddress & rawAddr,Packet * pkt)399 void AvrcCtProfile::ReceivePassRsp(const RawAddress &rawAddr, Packet *pkt)
400 {
401     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
402 
403     SendNextPassCmd(rawAddr);
404 
405     AvrcCtPassPacket packet(pkt);
406     HILOGI("Address: %{public}s - key: %{public}x - state: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr),
407         packet.GetKeyOperation(), packet.GetKeyState());
408 
409     InformPassRsp(
410         rawAddr, packet.GetKeyOperation(), packet.GetKeyState(), ExpainPassCrCodeToResult(packet.GetCrCode()));
411 }
412 
InformPassRsp(const RawAddress & rawAddr,uint8_t button,uint8_t state,int result) const413 void AvrcCtProfile::InformPassRsp(const RawAddress &rawAddr, uint8_t button, uint8_t state, int result) const
414 {
415     HILOGI("address: %{public}s, button: %{public}d, state: %{public}d, result: %{public}d",
416         GET_ENCRYPT_AVRCP_ADDR(rawAddr), button, state, result);
417 
418     switch (state) {
419         case AVRC_KEY_STATE_PRESS:
420             myObserver_->onButtonPressed(rawAddr, button, result);
421             break;
422         case AVRC_KEY_STATE_RELEASE:
423             myObserver_->onButtonReleased(rawAddr, button, result);
424             break;
425         default:
426             HILOGI("The button state is incorrect!");
427             break;
428     }
429 }
430 
ProcessPassTimeout(RawAddress rawAddr,uint8_t key,uint8_t state)431 void AvrcCtProfile::ProcessPassTimeout(RawAddress rawAddr, uint8_t key, uint8_t state)
432 {
433     HILOGI("address: %{public}s, key: %{public}x, state: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), key, state);
434 
435     if (IsEnabled()) {
436         SendNextPassCmd(rawAddr);
437         InformPassRsp(rawAddr, key, state, RET_BAD_STATUS);
438     }
439 }
440 
PassTimeoutCallback(const RawAddress & rawAddr,uint8_t key,uint8_t state)441 void AvrcCtProfile::PassTimeoutCallback(const RawAddress &rawAddr, uint8_t key, uint8_t state)
442 {
443     HILOGI("address: %{public}s, key: %{public}x - state: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), key, state);
444 
445     if (IsEnabled()) {
446         dispatcher_->PostTask(std::bind(&AvrcCtProfile::ProcessPassTimeout, this, rawAddr, key, state));
447     }
448 }
449 
450 /******************************************************************
451  * UNIT INFO / SUB UNIT INFO COMMAND                              *
452  ******************************************************************/
453 
SendUnitCmd(const RawAddress & rawAddr)454 void AvrcCtProfile::SendUnitCmd(const RawAddress &rawAddr)
455 {
456     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
457 
458     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
459     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
460 
461     std::shared_ptr<AvrcCtUnitPacket> pkt = std::make_shared<AvrcCtUnitPacket>();
462     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
463     if (!smManager->IsControlPendingState(rawAddr) && !smManager->IsControlContinuationState(rawAddr)) {
464         /// Unprocessed vendor dependent command.
465 
466         /// Sets the information which is used in the "CONNECTED" state of the control state machine.
467         cnManager->SetUnitPacket(rawAddr, pkt);
468 
469         utility::Message msg(AVRC_CT_SM_EVENT_UNIT_INFO);
470         AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
471 
472         /// Sets the information which is used in the timeout.
473         auto func = std::bind(&AvrcCtProfile::UnitTimeoutCallback, this, rawAddr);
474         cnManager->SetUnitTimer(rawAddr, func, AVRC_CT_TIMER_T_MTP);
475     } else {
476         /// There is a command in processing,
477 
478         if (cnManager->GetSizeOfUnitQueue(rawAddr) > AVRC_CT_DEFAULT_SIZE_OF_QUEUE) {
479             HILOGI("The vendor queue is oversize: %{public}d", cnManager->GetSizeOfVendorQueue(rawAddr));
480         } else {
481             cnManager->PushUnitQueue(rawAddr, pkt);
482             HILOGI("Waiting for the response!");
483         }
484     }
485     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
486 }
487 
SendSubUnitCmd(const RawAddress & rawAddr)488 void AvrcCtProfile::SendSubUnitCmd(const RawAddress &rawAddr)
489 {
490     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
491 
492     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
493     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
494 
495     std::shared_ptr<AvrcCtUnitPacket> pkt = std::make_shared<AvrcCtSubUnitPacket>();
496     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
497     if (!smManager->IsControlPendingState(rawAddr) && !smManager->IsControlContinuationState(rawAddr)) {
498         /// Unprocessed vendor dependent command.
499 
500         /// Sets the information which is used in the "CONNECTED" state of the control state machine.
501         cnManager->SetUnitPacket(rawAddr, pkt);
502 
503         utility::Message msg(AVRC_CT_SM_EVENT_SUB_UNIT_INFO);
504         AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
505 
506         /// Sets the information which is used in the timeout.
507         auto func = std::bind(&AvrcCtProfile::UnitTimeoutCallback, this, rawAddr);
508         cnManager->SetUnitTimer(rawAddr, func, AVRC_CT_TIMER_T_MTP);
509     } else {
510         /// There is a command in processing,
511 
512         if (cnManager->GetSizeOfUnitQueue(rawAddr) > AVRC_CT_DEFAULT_SIZE_OF_QUEUE) {
513             HILOGI("The vendor queue is oversize: %{public}d", cnManager->GetSizeOfVendorQueue(rawAddr));
514         } else {
515             cnManager->PushUnitQueue(rawAddr, pkt);
516             HILOGI("Waiting for the response!");
517         }
518     }
519     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
520 }
521 
IsUnitQueueFull(const RawAddress & rawAddr)522 bool AvrcCtProfile::IsUnitQueueFull(const RawAddress &rawAddr)
523 {
524     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
525 
526     return (AvrcCtConnectManager::GetInstance()->GetSizeOfUnitQueue(rawAddr) == AVRC_CT_DEFAULT_SIZE_OF_QUEUE);
527 }
528 
SendNextUnitCmd(const RawAddress & rawAddr)529 void AvrcCtProfile::SendNextUnitCmd(const RawAddress &rawAddr)
530 {
531     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
532 
533     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
534     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
535     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
536 
537     cnManager->ClearUnitInfo(rawAddr);
538     msg.what_ = AVRC_CT_SM_EVENT_TO_CONNECTED_STATE;
539     smManager->SendMessageToControlStateMachine(rawAddr, msg);
540 
541     if (cnManager->GetSizeOfUnitQueue(rawAddr) != 0x00) {
542         std::shared_ptr<AvrcCtUnitPacket> packet = cnManager->PopUnitQueue(rawAddr);
543 
544         switch (packet->GetOpCode()) {
545             case AVRC_CT_OP_CODE_UNIT_INFO:
546                 msg.what_ = AVRC_CT_SM_EVENT_UNIT_INFO;
547                 break;
548             case AVRC_CT_OP_CODE_SUB_UNIT_INFO:
549                 msg.what_ = AVRC_CT_SM_EVENT_SUB_UNIT_INFO;
550                 break;
551             default:
552                 HILOGI("Opcode is wrong! - opCode: %{public}x", packet->GetOpCode());
553                 break;
554         }
555 
556         cnManager->SetUnitPacket(rawAddr, packet);
557         smManager->SendMessageToControlStateMachine(rawAddr, msg);
558     } else if (cnManager->GetSizeOfVendorQueue(rawAddr) != 0x00) {
559         SendNextVendorCmd(rawAddr);
560     }
561 }
562 
ReceiveUnitRsp(const RawAddress & rawAddr,Packet * pkt)563 void AvrcCtProfile::ReceiveUnitRsp(const RawAddress &rawAddr, Packet *pkt)
564 {
565     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
566 
567     SendNextUnitCmd(rawAddr);
568 
569     AvrcCtUnitPacket packet(pkt);
570     HILOGI("response: %{public}x", packet.GetCrCode());
571 }
572 
ReceiveSubUnitRsp(const RawAddress & rawAddr,Packet * pkt)573 void AvrcCtProfile::ReceiveSubUnitRsp(const RawAddress &rawAddr, Packet *pkt)
574 {
575     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
576 
577     SendNextUnitCmd(rawAddr);
578 
579     AvrcCtSubUnitPacket packet(pkt);
580     HILOGI("response: %{public}x", packet.GetCrCode());
581 }
582 
ProcessUnitTimeout(RawAddress rawAddr)583 void AvrcCtProfile::ProcessUnitTimeout(RawAddress rawAddr)
584 {
585     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
586 
587     if (IsEnabled()) {
588         std::shared_ptr<AvrcCtUnitPacket> packet = AvrcCtConnectManager::GetInstance()->GetUnitPacket(rawAddr);
589         if (packet != nullptr) {
590             switch (packet->GetOpCode()) {
591                 case AVRC_CT_OP_CODE_UNIT_INFO:
592                     HILOGI("opCode[UNIT_INFO]");
593                     break;
594                 case AVRC_CT_OP_CODE_SUB_UNIT_INFO:
595                     HILOGI("opCode[SUB_UNIT_INFO]");
596                     break;
597                 default:
598                     HILOGI("Opcode is wrong! - opCode: %{public}x", packet->GetOpCode());
599                     break;
600             }
601         } else {
602             HILOGI("The saved packet is nullptr!");
603         }
604         IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
605         SendNextUnitCmd(rawAddr);
606         IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
607     }
608 }
609 
UnitTimeoutCallback(const RawAddress & rawAddr)610 void AvrcCtProfile::UnitTimeoutCallback(const RawAddress &rawAddr)
611 {
612     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
613 
614     if (IsEnabled()) {
615         dispatcher_->PostTask(std::bind(&AvrcCtProfile::ProcessUnitTimeout, this, rawAddr));
616     }
617 }
618 
619 /******************************************************************
620  * VENDOR COMMAND                                                 *
621  ******************************************************************/
622 
SendVendorCmd(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & pkt,AvrcCtSmEvent event)623 void AvrcCtProfile::SendVendorCmd(
624     const RawAddress &rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &pkt, AvrcCtSmEvent event)
625 {
626     HILOGI("address: %{public}s, event: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), event);
627 
628     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
629     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
630 
631     if (!smManager->IsControlPendingState(rawAddr) && !smManager->IsControlContinuationState(rawAddr)) {
632         /// Unprocessed vendor dependent command.
633         /// Sets the information which is used in the "CONNECTED" state of the control state machine.
634         cnManager->ClearVendorInfo(rawAddr);
635         cnManager->SetVendorPacket(rawAddr, pkt);
636 
637         utility::Message msg(event);
638         AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
639 
640         /// Sets the information which is used in the timeout.
641         auto func = std::bind(&AvrcCtProfile::VendorTimeoutCallback, this, rawAddr);
642         cnManager->SetVendorTimer(rawAddr, func, AVRC_CT_TIMER_T_MTP);
643     } else {
644         /// There is a command in processing,
645 
646         if (cnManager->GetSizeOfVendorQueue(rawAddr) > AVRC_CT_DEFAULT_SIZE_OF_QUEUE) {
647             HILOGI("The vendor queue is oversize: %{public}d", cnManager->GetSizeOfVendorQueue(rawAddr));
648         } else {
649             cnManager->PushVendorQueue(rawAddr, pkt);
650             HILOGI("Waiting for the response!");
651         }
652     }
653 }
654 
SendNextVendorCmd(const RawAddress & rawAddr)655 void AvrcCtProfile::SendNextVendorCmd(const RawAddress &rawAddr)
656 {
657     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
658 
659     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
660     cnManager->ClearVendorInfo(rawAddr);
661 
662     utility::Message msg(AVRC_CT_SM_EVENT_TO_CONNECTED_STATE);
663     AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
664 
665     if (cnManager->GetSizeOfVendorQueue(rawAddr) != 0x00) {
666         std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->PopVendorQueue(rawAddr);
667         SendVendorCmd(rawAddr, packet, static_cast<AvrcCtSmEvent>(packet->GetPduId()));
668     } else if (cnManager->GetSizeOfUnitQueue(rawAddr) != 0x00) {
669         SendNextUnitCmd(rawAddr);
670     }
671 }
672 
SendVendorContinueCmd(const RawAddress & rawAddr,uint8_t pduId)673 void AvrcCtProfile::SendVendorContinueCmd(const RawAddress &rawAddr, uint8_t pduId)
674 {
675     HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
676 
677     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
678     cnManager->ClearVendorPacket(rawAddr);
679     cnManager->ClearVendorTimer(rawAddr);
680 
681     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtRcrPacket>(pduId);
682     cnManager->SetVendorPacket(rawAddr, packet);
683 
684     utility::Message msg(AVRC_CT_SM_EVENT_REQUEST_CONTINUING_RESPONSE);
685     AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
686 
687     /// Sets the information which is used in the timeout.
688     auto func = std::bind(&AvrcCtProfile::VendorTimeoutCallback, this, rawAddr);
689     cnManager->SetVendorTimer(rawAddr, func, AVRC_CT_TIMER_T_MTP);
690 }
691 
SendVendorAbortCmd(const RawAddress & rawAddr,uint8_t pduId) const692 void AvrcCtProfile::SendVendorAbortCmd(const RawAddress &rawAddr, uint8_t pduId) const
693 {
694     HILOGI("address: %{public}s, pduId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
695 
696     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
697     cnManager->ClearVendorPacket(rawAddr);
698     cnManager->ClearVendorTimer(rawAddr);
699 
700     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtAcrPacket>(pduId);
701     cnManager->SetVendorPacket(rawAddr, packet);
702 
703     utility::Message msg(AVRC_CT_SM_EVENT_ABORT_CONTINUING_RESPONSE);
704     AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
705 }
706 
IsVendorQueueFull(const RawAddress & rawAddr)707 bool AvrcCtProfile::IsVendorQueueFull(const RawAddress &rawAddr)
708 {
709     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
710     std::lock_guard<std::recursive_mutex> lock(mutex_);
711 
712     return (AvrcCtConnectManager::GetInstance()->GetSizeOfVendorQueue(rawAddr) == AVRC_CT_DEFAULT_SIZE_OF_QUEUE);
713 }
714 
SendGetCapabilitiesCmd(const RawAddress & rawAddr,uint8_t capabilityId)715 void AvrcCtProfile::SendGetCapabilitiesCmd(const RawAddress &rawAddr, uint8_t capabilityId)
716 {
717     HILOGI("address: %{public}s, capabilityId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), capabilityId);
718 
719     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGcPacket>(capabilityId);
720     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
721     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_CAPABILITIES);
722     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
723 }
724 
ReceiveGetCapabilitiesRsp(const RawAddress & rawAddr,Packet * pkt)725 void AvrcCtProfile::ReceiveGetCapabilitiesRsp(const RawAddress &rawAddr, Packet *pkt)
726 {
727     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
728 
729     SendNextVendorCmd(rawAddr);
730 
731     AvrcCtGcPacket packet(pkt);
732     if (packet.GetEvents().size() != 0x00) {
733         AvrcCtConnectManager::GetInstance()->DisableExcludeEvents(rawAddr, packet.GetEvents());
734     }
735     myObserver_->onGetCapabilities(
736         rawAddr, packet.GetCompanies(), packet.GetEvents(), ExplainCrCodeToResult(packet.GetCrCode()));
737 }
738 
SendListPlayerApplicationSettingAttributesCmd(const RawAddress & rawAddr)739 void AvrcCtProfile::SendListPlayerApplicationSettingAttributesCmd(const RawAddress &rawAddr)
740 {
741     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
742 
743     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtLpasaPacket>();
744     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
745     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES);
746     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
747 }
748 
ReceiveListPlayerApplicationSettingAttributesRsp(const RawAddress & rawAddr,Packet * pkt)749 void AvrcCtProfile::ReceiveListPlayerApplicationSettingAttributesRsp(const RawAddress &rawAddr, Packet *pkt)
750 {
751     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
752 
753     SendNextVendorCmd(rawAddr);
754 
755     AvrcCtLpasaPacket packet(pkt);
756     myObserver_->onListPlayerApplicationSettingAttributes(
757         rawAddr, packet.GetAttributes(), ExplainCrCodeToResult(packet.GetCrCode()));
758 }
759 
SendListPlayerApplicationSettingValuesCmd(const RawAddress & rawAddr,uint8_t attribute)760 void AvrcCtProfile::SendListPlayerApplicationSettingValuesCmd(const RawAddress &rawAddr, uint8_t attribute)
761 {
762     HILOGI("address: %{public}s, attribute: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attribute);
763 
764     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtLpasvPacket>(attribute);
765     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
766     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_LIST_PLAYER_APPLICATION_SETTING_VALUES);
767     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
768 }
769 
ReceiveListPlayerApplicationSettingValuesRsp(const RawAddress & rawAddr,Packet * pkt)770 void AvrcCtProfile::ReceiveListPlayerApplicationSettingValuesRsp(const RawAddress &rawAddr, Packet *pkt)
771 {
772     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
773 
774     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
775 
776     std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
777     AvrcCtLpasvPacket *lpasvPkt = nullptr;
778 
779     if (packet == nullptr) {
780         packet = cnManager->GetVendorPacket(rawAddr);
781         lpasvPkt = static_cast<AvrcCtLpasvPacket *>(cnManager->GetVendorPacket(rawAddr).get());
782         lpasvPkt->DisassemblePacket(pkt);
783     } else {
784         lpasvPkt = static_cast<AvrcCtLpasvPacket *>(packet.get());
785         lpasvPkt->DisassemblePacket(pkt);
786     }
787 
788     HILOGI("attribute: %{public}x - values.size: %{public}zu - result: %{public}d", lpasvPkt->GetAttribute(),
789         lpasvPkt->GetValues().size(), ExplainCrCodeToResult(lpasvPkt->GetCrCode()));
790 
791     switch (lpasvPkt->GetPacketType()) {
792         case AVRC_CT_VENDOR_PACKET_TYPE:
793         /// FALL THROUGH
794         case AVRC_CT_VENDOR_PACKET_TYPE_END: {
795             SendNextVendorCmd(rawAddr);
796 
797             myObserver_->onListPlayerApplicationSettingValues(
798                 rawAddr, lpasvPkt->GetAttribute(), lpasvPkt->GetValues(), ExplainCrCodeToResult(lpasvPkt->GetCrCode()));
799             break;
800         }
801         case AVRC_CT_VENDOR_PACKET_TYPE_START:
802         /// FALL THROUGH
803         case AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE: {
804             if (packet->GetReceivedFragments() >= maxFragments_) {
805                 SendVendorAbortCmd(rawAddr, lpasvPkt->GetPduId());
806                 myObserver_->onListPlayerApplicationSettingValues(rawAddr,
807                     lpasvPkt->GetAttribute(),
808                     lpasvPkt->GetValues(),
809                     ExplainCrCodeToResult(lpasvPkt->GetCrCode()));
810             } else {
811                 cnManager->SetVendorContinuePacket(rawAddr, packet);
812                 SendVendorContinueCmd(rawAddr, lpasvPkt->GetPduId());
813             }
814             break;
815         }
816         default:
817             HILOGI("Packet Type is wrong! - packetType: %{public}d", lpasvPkt->GetPacketType());
818             break;
819     }
820 }
821 
SendGetCurrentPlayerApplicationSettingValueCmd(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes)822 void AvrcCtProfile::SendGetCurrentPlayerApplicationSettingValueCmd(
823     const RawAddress &rawAddr, const std::vector<uint8_t> &attributes)
824 {
825     HILOGI("address: %{public}s, attributes.size: %{public}zu", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size());
826 
827     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGcpasvPacket>(attributes);
828     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
829     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE);
830     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
831 }
832 
ReceiveGetCurrentPlayerApplicationSettingValueRsp(const RawAddress & rawAddr,Packet * pkt)833 void AvrcCtProfile::ReceiveGetCurrentPlayerApplicationSettingValueRsp(const RawAddress &rawAddr, Packet *pkt)
834 {
835     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
836 
837     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
838 
839     std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
840     AvrcCtGcpasvPacket *gcpasvPkt = nullptr;
841 
842     if (packet == nullptr) {
843         packet = cnManager->GetVendorPacket(rawAddr);
844         gcpasvPkt = static_cast<AvrcCtGcpasvPacket *>(packet.get());
845         gcpasvPkt->ClearAttributes();
846         gcpasvPkt->DisassemblePacket(pkt);
847     } else {
848         gcpasvPkt = static_cast<AvrcCtGcpasvPacket *>(packet.get());
849         gcpasvPkt->DisassemblePacket(pkt);
850     }
851 
852     HILOGI("attributes.size: %{public}zu - values.size: %{public}zu - result: %{public}d",
853         gcpasvPkt->GetAttributes().size(), gcpasvPkt->GetValues().size(),
854         ExplainCrCodeToResult(gcpasvPkt->GetCrCode()));
855 
856     switch (gcpasvPkt->GetPacketType()) {
857         case AVRC_CT_VENDOR_PACKET_TYPE:
858         /// FALL THROUGH
859         case AVRC_CT_VENDOR_PACKET_TYPE_END: {
860             SendNextVendorCmd(rawAddr);
861 
862             myObserver_->onGetCurrentPlayerApplicationSettingValue(rawAddr,
863                 gcpasvPkt->GetAttributes(),
864                 gcpasvPkt->GetValues(),
865                 ExplainCrCodeToResult(gcpasvPkt->GetCrCode()));
866             break;
867         }
868         case AVRC_CT_VENDOR_PACKET_TYPE_START:
869         /// FALL THROUGH
870         case AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE: {
871             if (packet->GetReceivedFragments() >= maxFragments_) {
872                 SendVendorAbortCmd(rawAddr, gcpasvPkt->GetPduId());
873                 myObserver_->onGetCurrentPlayerApplicationSettingValue(rawAddr,
874                     gcpasvPkt->GetAttributes(),
875                     gcpasvPkt->GetValues(),
876                     ExplainCrCodeToResult(gcpasvPkt->GetCrCode()));
877             } else {
878                 cnManager->SetVendorContinuePacket(rawAddr, packet);
879                 SendVendorContinueCmd(rawAddr, gcpasvPkt->GetPduId());
880             }
881             break;
882         }
883         default:
884             HILOGI("Packet Type is wrong! - packetType: %{public}d", gcpasvPkt->GetPacketType());
885             break;
886     }
887 }
888 
SendSetPlayerApplicationSettingValueCmd(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<uint8_t> & values)889 void AvrcCtProfile::SendSetPlayerApplicationSettingValueCmd(
890     const RawAddress &rawAddr, const std::vector<uint8_t> &attributes, const std::vector<uint8_t> &values)
891 {
892     HILOGI("address: %{public}s, attributes.size: %{public}zu, values.size: %{public}zu",
893         GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size(), values.size());
894 
895     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtSpasvPacket>(attributes, values);
896     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
897     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_SET_PLAYER_APPLICATION_SETTING_VALUE);
898     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
899 }
900 
ReceiveSetPlayerApplicationSettingValueRsp(const RawAddress & rawAddr,Packet * pkt)901 void AvrcCtProfile::ReceiveSetPlayerApplicationSettingValueRsp(const RawAddress &rawAddr, Packet *pkt)
902 {
903     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
904 
905     SendNextVendorCmd(rawAddr);
906 
907     AvrcCtSpasvPacket packet(pkt);
908     myObserver_->onSetPlayerApplicationSettingValue(rawAddr, ExplainCrCodeToResult(packet.GetCrCode()));
909 }
910 
SendGetPlayerApplicationSettingAttributeTextCmd(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes)911 void AvrcCtProfile::SendGetPlayerApplicationSettingAttributeTextCmd(
912     const RawAddress &rawAddr, const std::vector<uint8_t> &attributes)
913 {
914     HILOGI("address: %{public}s, attributes.size: %{public}zu", GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributes.size());
915 
916     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGpasatPacket>(attributes);
917     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
918     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT);
919     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
920 }
921 
ReceiveGetPlayerApplicationSettingAttributeTextRsp(const RawAddress & rawAddr,Packet * pkt)922 void AvrcCtProfile::ReceiveGetPlayerApplicationSettingAttributeTextRsp(const RawAddress &rawAddr, Packet *pkt)
923 {
924     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
925 
926     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
927 
928     std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
929     AvrcCtGpasatPacket *gpasatPkt = nullptr;
930 
931     if (packet == nullptr) {
932         packet = cnManager->GetVendorPacket(rawAddr);
933         gpasatPkt = static_cast<AvrcCtGpasatPacket *>(packet.get());
934         gpasatPkt->ClearAttributes();
935         gpasatPkt->ClearValueName();
936         gpasatPkt->DisassemblePacket(pkt);
937     } else {
938         gpasatPkt = static_cast<AvrcCtGpasatPacket *>(packet.get());
939         gpasatPkt->DisassemblePacket(pkt);
940     }
941     HILOGI("attributes.size: %{public}zu, attributeStringLength.size: %{public}zu, result: %{public}d",
942         gpasatPkt->GetAttributes().size(), gpasatPkt->GetAttributeName().size(),
943         ExplainCrCodeToResult(gpasatPkt->GetCrCode()));
944 
945     switch (gpasatPkt->GetPacketType()) {
946         case AVRC_CT_VENDOR_PACKET_TYPE:
947         /// FALL THROUGH
948         case AVRC_CT_VENDOR_PACKET_TYPE_END: {
949             SendNextVendorCmd(rawAddr);
950 
951             myObserver_->onGetPlayerApplicationSettingAttributeText(rawAddr,
952                 gpasatPkt->GetAttributes(),
953                 gpasatPkt->GetAttributeName(),
954                 ExplainCrCodeToResult(gpasatPkt->GetCrCode()));
955             break;
956         }
957         case AVRC_CT_VENDOR_PACKET_TYPE_START:
958         /// FALL THROUGH
959         case AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE: {
960             if (packet->GetReceivedFragments() >= maxFragments_) {
961                 SendVendorAbortCmd(rawAddr, gpasatPkt->GetPduId());
962                 myObserver_->onGetPlayerApplicationSettingAttributeText(rawAddr,
963                     gpasatPkt->GetAttributes(),
964                     gpasatPkt->GetAttributeName(),
965                     ExplainCrCodeToResult(gpasatPkt->GetCrCode()));
966             } else {
967                 cnManager->SetVendorContinuePacket(rawAddr, packet);
968                 SendVendorContinueCmd(rawAddr, gpasatPkt->GetPduId());
969             }
970             break;
971         }
972         default:
973             HILOGI("Packet Type is wrong! - packetType: %{public}d", gpasatPkt->GetPacketType());
974             break;
975     }
976 }
977 
SendGetPlayerApplicationSettingValueTextCmd(const RawAddress & rawAddr,uint8_t attributeId,const std::vector<uint8_t> & values)978 void AvrcCtProfile::SendGetPlayerApplicationSettingValueTextCmd(
979     const RawAddress &rawAddr, uint8_t attributeId, const std::vector<uint8_t> &values)
980 {
981     HILOGI("address: %{public}s, attributeId: %{public}d, values.size[%{public}zu]",
982         GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributeId, values.size());
983 
984     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGpasvtPacket>(attributeId, values);
985     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
986     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT);
987     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
988 }
989 
ReceiveGetPlayerApplicationSettingValueTextRsp(const RawAddress & rawAddr,Packet * pkt)990 void AvrcCtProfile::ReceiveGetPlayerApplicationSettingValueTextRsp(const RawAddress &rawAddr, Packet *pkt)
991 {
992     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
993 
994     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
995 
996     std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
997     AvrcCtGpasvtPacket *gpasvtPkt = nullptr;
998 
999     if (packet == nullptr) {
1000         packet = cnManager->GetVendorPacket(rawAddr);
1001         gpasvtPkt = static_cast<AvrcCtGpasvtPacket *>(packet.get());
1002         gpasvtPkt->ClearValues();
1003         gpasvtPkt->ClearValueName();
1004         gpasvtPkt->DisassemblePacket(pkt);
1005     } else {
1006         gpasvtPkt = static_cast<AvrcCtGpasvtPacket *>(packet.get());
1007         gpasvtPkt->DisassemblePacket(pkt);
1008     }
1009     HILOGI("values.size: %{public}zu, valuestr.size: %{public}zu, result: %{public}d",
1010         gpasvtPkt->GetValues().size(), gpasvtPkt->GetValueName().size(), ExplainCrCodeToResult(gpasvtPkt->GetCrCode()));
1011 
1012     switch (gpasvtPkt->GetPacketType()) {
1013         case AVRC_CT_VENDOR_PACKET_TYPE:
1014         /// FALL THROUGH
1015         case AVRC_CT_VENDOR_PACKET_TYPE_END: {
1016             SendNextVendorCmd(rawAddr);
1017 
1018             myObserver_->onGetPlayerApplicationSettingValueText(rawAddr,
1019                 gpasvtPkt->GetValues(),
1020                 gpasvtPkt->GetValueName(),
1021                 ExplainCrCodeToResult(gpasvtPkt->GetCrCode()));
1022             break;
1023         }
1024         case AVRC_CT_VENDOR_PACKET_TYPE_START:
1025         /// FALL THROUGH
1026         case AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE: {
1027             if (packet->GetReceivedFragments() >= maxFragments_) {
1028                 SendVendorAbortCmd(rawAddr, gpasvtPkt->GetPduId());
1029                 myObserver_->onGetPlayerApplicationSettingValueText(rawAddr,
1030                     gpasvtPkt->GetValues(),
1031                     gpasvtPkt->GetValueName(),
1032                     ExplainCrCodeToResult(gpasvtPkt->GetCrCode()));
1033             } else {
1034                 cnManager->SetVendorContinuePacket(rawAddr, packet);
1035                 SendVendorContinueCmd(rawAddr, gpasvtPkt->GetPduId());
1036             }
1037             break;
1038         }
1039         default:
1040             HILOGI("Packet Type is wrong! packetType: %{public}d", gpasvtPkt->GetPacketType());
1041             break;
1042     }
1043 }
1044 
SendGetElementAttributesCmd(const RawAddress & rawAddr,uint64_t identifier,const std::vector<uint32_t> & attributes)1045 void AvrcCtProfile::SendGetElementAttributesCmd(
1046     const RawAddress &rawAddr, uint64_t identifier, const std::vector<uint32_t> &attributes)
1047 {
1048     HILOGI("address: %{public}s, identifier: %{public}ju, attributes.size: %{public}zu",
1049         GET_ENCRYPT_AVRCP_ADDR(rawAddr), identifier, attributes.size());
1050 
1051     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGeaPacket>(identifier, attributes);
1052     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1053     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_ELEMENT_ATTRIBUTES);
1054     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1055 }
1056 
ReceiveGetElementAttributesRsp(const RawAddress & rawAddr,Packet * pkt)1057 void AvrcCtProfile::ReceiveGetElementAttributesRsp(const RawAddress &rawAddr, Packet *pkt)
1058 {
1059     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1060 
1061     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1062 
1063     std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
1064     AvrcCtGeaPacket *geaPkt = nullptr;
1065     if (packet == nullptr) {
1066         packet = cnManager->GetVendorPacket(rawAddr);
1067         geaPkt = static_cast<AvrcCtGeaPacket *>(packet.get());
1068         geaPkt->ClearAttributes();
1069         geaPkt->ClearValues();
1070         geaPkt->DisassemblePacket(pkt);
1071     } else {
1072         geaPkt = static_cast<AvrcCtGeaPacket *>(packet.get());
1073         geaPkt->DisassemblePacket(pkt);
1074     }
1075 
1076     HILOGI("attributes.size: %{public}zu, values.size: %{public}zu, result: %{public}d",
1077         geaPkt->GetAttributes().size(), geaPkt->GetValues().size(), ExplainCrCodeToResult(geaPkt->GetCrCode()));
1078 
1079     switch (geaPkt->GetPacketType()) {
1080         case AVRC_CT_VENDOR_PACKET_TYPE:
1081         /// FALL THROUGH
1082         case AVRC_CT_VENDOR_PACKET_TYPE_END: {
1083             SendNextVendorCmd(rawAddr);
1084 
1085             myObserver_->onGetElementAttributes(
1086                 rawAddr, geaPkt->GetAttributes(), geaPkt->GetValues(), ExplainCrCodeToResult(geaPkt->GetCrCode()));
1087             break;
1088         }
1089         case AVRC_CT_VENDOR_PACKET_TYPE_START:
1090         /// FALL THROUGH
1091         case AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE: {
1092             if (packet->GetReceivedFragments() >= maxFragments_) {
1093                 SendVendorAbortCmd(rawAddr, geaPkt->GetPduId());
1094                 myObserver_->onGetElementAttributes(
1095                     rawAddr, geaPkt->GetAttributes(), geaPkt->GetValues(), ExplainCrCodeToResult(geaPkt->GetCrCode()));
1096             } else {
1097                 cnManager->SetVendorContinuePacket(rawAddr, packet);
1098                 SendVendorContinueCmd(rawAddr, geaPkt->GetPduId());
1099             }
1100             break;
1101         }
1102         default:
1103             HILOGI("Packet Type is wrong! packetType: %{public}d", geaPkt->GetPacketType());
1104             break;
1105     }
1106 }
1107 
SendGetPlayStatusCmd(const RawAddress & rawAddr)1108 void AvrcCtProfile::SendGetPlayStatusCmd(const RawAddress &rawAddr)
1109 {
1110     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1111 
1112     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtGpsPacket>();
1113     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1114     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_PLAY_STATUS);
1115     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1116 }
1117 
ReceiveGetPlayStatusRsp(const RawAddress & rawAddr,Packet * pkt)1118 void AvrcCtProfile::ReceiveGetPlayStatusRsp(const RawAddress &rawAddr, Packet *pkt)
1119 {
1120     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1121 
1122     SendNextVendorCmd(rawAddr);
1123 
1124     AvrcCtGpsPacket packet(pkt);
1125     myObserver_->onGetPlayStatus(rawAddr,
1126         packet.GetSongLength(),
1127         packet.GetSongPosition(),
1128         packet.GetPlayStatus(),
1129         ExplainCrCodeToResult(packet.GetCrCode()));
1130 }
1131 
SendRequestContinuingResponseCmd(const RawAddress & rawAddr,uint8_t pduId)1132 void AvrcCtProfile::SendRequestContinuingResponseCmd(const RawAddress &rawAddr, uint8_t pduId)
1133 {
1134     HILOGI("address: %{public}s, pduId: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1135 
1136     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtRcrPacket>(pduId);
1137     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1138     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_REQUEST_CONTINUING_RESPONSE);
1139     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1140 }
1141 
SendAbortContinuingResponseCmd(const RawAddress & rawAddr,uint8_t pduId)1142 void AvrcCtProfile::SendAbortContinuingResponseCmd(const RawAddress &rawAddr, uint8_t pduId)
1143 {
1144     HILOGI("address: %{public}s, pduId: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), pduId);
1145 
1146     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtAcrPacket>(pduId);
1147     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1148     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_ABORT_CONTINUING_RESPONSE);
1149     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1150 }
1151 
ReceiveAbortContinuingResponseRsp(const RawAddress & rawAddr,Packet * pkt)1152 void AvrcCtProfile::ReceiveAbortContinuingResponseRsp(const RawAddress &rawAddr, Packet *pkt)
1153 {
1154     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1155 
1156     AvrcCtAcrPacket acrPkt(pkt);
1157 
1158     std::shared_ptr<AvrcCtVendorPacket> packet = AvrcCtConnectManager::GetInstance()->GetVendorContinuePacket(rawAddr);
1159 
1160     if (packet != nullptr) {
1161         if (acrPkt.GetPduId() != packet->GetPduId()) {
1162             HILOGI("PDU ID is wrong! Address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1163         }
1164     } else {
1165         HILOGI("The saved continue packet is nullptr!");
1166     }
1167 
1168     SendNextVendorCmd(rawAddr);
1169 }
1170 
SendSetAddressedPlayerCmd(const RawAddress & rawAddr,uint16_t playerId)1171 void AvrcCtProfile::SendSetAddressedPlayerCmd(const RawAddress &rawAddr, uint16_t playerId)
1172 {
1173     HILOGI("address: %{public}s, playerId: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
1174 
1175     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtSapPacket>(playerId);
1176     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1177     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_SET_ADDRESSED_PLAYER);
1178     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1179 }
1180 
ReceiveSetAddressedPlayerRsp(const RawAddress & rawAddr,Packet * pkt)1181 void AvrcCtProfile::ReceiveSetAddressedPlayerRsp(const RawAddress &rawAddr, Packet *pkt)
1182 {
1183     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1184 
1185     SendNextVendorCmd(rawAddr);
1186 
1187     AvrcCtSapPacket packet(pkt);
1188 
1189     int result = RET_BAD_STATUS;
1190     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1191         result = BT_SUCCESS;
1192     }
1193 
1194     myObserver_->onSetAddressedPlayer(rawAddr, result, packet.GetStatus());
1195 }
1196 
SendPlayItemCmd(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1197 void AvrcCtProfile::SendPlayItemCmd(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1198 {
1199     HILOGI("address: %{public}s, scope: %{public}x, uid: %{public}jx, uidCounter: %{public}hd",
1200         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, uid, uidCounter);
1201 
1202     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtPiPacket>(scope, uid, uidCounter);
1203     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1204     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_PLAY_ITEM);
1205     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1206 }
1207 
ReceivePlayItemRsp(const RawAddress & rawAddr,Packet * pkt)1208 void AvrcCtProfile::ReceivePlayItemRsp(const RawAddress &rawAddr, Packet *pkt)
1209 {
1210     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1211 
1212     SendNextVendorCmd(rawAddr);
1213 
1214     AvrcCtPiPacket packet(pkt);
1215     int result;
1216     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1217         result = BT_SUCCESS;
1218     } else {
1219         result = RET_BAD_STATUS;
1220     }
1221     myObserver_->onPlayItem(rawAddr, result, packet.GetStatus());
1222 }
1223 
SendAddToNowPlayingCmd(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter)1224 void AvrcCtProfile::SendAddToNowPlayingCmd(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter)
1225 {
1226     HILOGI("address: %{public}s, scope: %{public}d, uid: %{public}ju, uidCounter: %{public}hu",
1227         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, uid, uidCounter);
1228 
1229     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtAtnpPacket>(scope, uid, uidCounter);
1230     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1231     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_ADD_TO_NOW_PLAYING);
1232     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1233 }
1234 
ReceiveAddToNowPlayingRsp(const RawAddress & rawAddr,Packet * pkt)1235 void AvrcCtProfile::ReceiveAddToNowPlayingRsp(const RawAddress &rawAddr, Packet *pkt)
1236 {
1237     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1238 
1239     SendNextVendorCmd(rawAddr);
1240 
1241     AvrcCtAtnpPacket packet(pkt);
1242 
1243     int result = RET_BAD_STATUS;
1244     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1245         result = BT_SUCCESS;
1246     }
1247 
1248     myObserver_->onAddToNowPlaying(rawAddr, result, packet.GetStatus());
1249 }
1250 
SendSetAbsoluteVolumeCmd(const RawAddress & rawAddr,uint8_t volume)1251 void AvrcCtProfile::SendSetAbsoluteVolumeCmd(const RawAddress &rawAddr, uint8_t volume)
1252 {
1253     HILOGI("address: %{public}s, volume: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume);
1254 
1255     std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtSavPacket>(volume);
1256     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1257     SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_SET_ABSOLUTE_VOLUME);
1258     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1259 }
1260 
ReceiveSetAbsoluteVolumeRsp(const RawAddress & rawAddr,Packet * pkt)1261 void AvrcCtProfile::ReceiveSetAbsoluteVolumeRsp(const RawAddress &rawAddr, Packet *pkt)
1262 {
1263     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1264 
1265     SendNextVendorCmd(rawAddr);
1266 
1267     AvrcCtSavPacket packet(pkt);
1268     myObserver_->onSetAbsoluteVolume(rawAddr, packet.GetAbsoluteVolume(), ExplainCrCodeToResult(packet.GetCrCode()));
1269 }
1270 
EnableNotification(const RawAddress & rawAddr,const std::vector<uint8_t> & events,uint32_t interval)1271 void AvrcCtProfile::EnableNotification(const RawAddress &rawAddr, const std::vector<uint8_t> &events, uint32_t interval)
1272 {
1273     HILOGI("address: %{public}s, events.size: %{public}zu", GET_ENCRYPT_AVRCP_ADDR(rawAddr), events.size());
1274 
1275     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1276     for (auto event : events) {
1277         cnManager->EnableNotifyState(rawAddr, event);
1278 
1279         std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtNotifyPacket>(event);
1280         SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_REGISTER_NOTIFICATION);
1281     }
1282 }
1283 
DisableNotification(const RawAddress & rawAddr,const std::vector<uint8_t> & events)1284 void AvrcCtProfile::DisableNotification(const RawAddress &rawAddr, const std::vector<uint8_t> &events)
1285 {
1286     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1287 
1288     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1289     for (auto event : events) {
1290         cnManager->DisableNotifyState(rawAddr, event);
1291     }
1292 }
1293 
ReceiveRegisterNotificationRsp(const RawAddress & rawAddr,Packet * pkt)1294 void AvrcCtProfile::ReceiveRegisterNotificationRsp(const RawAddress &rawAddr, Packet *pkt)
1295 {
1296     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1297 
1298     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1299     std::shared_ptr<AvrcCtNotifyPacket> notifyPkt = std::make_shared<AvrcCtNotifyPacket>(pkt);
1300     uint8_t crCode = notifyPkt->GetCrCode();
1301     bool supported = false;
1302     if (cnManager->IsNotifyStateEnabled(rawAddr, notifyPkt->GetEventId()) &&
1303         crCode != AVRC_CT_RSP_CODE_NOT_IMPLEMENTED) {
1304         supported = true;
1305     }
1306 
1307     int result = AVRC_ES_CODE_NO_ERROR;
1308     if (crCode == AVRC_CT_RSP_CODE_CHANGED) {
1309         result = AVRC_ES_CODE_NOTIFICATION_CHANGED;
1310     }
1311     if (crCode == AVRC_CT_RSP_CODE_REJECTED) {
1312         result = AVRC_ES_CODE_INTERNAL_ERROR;
1313     }
1314 
1315     if ((notifyPkt->GetPacketType() == AVRC_CT_VENDOR_PACKET_TYPE ||
1316         notifyPkt->GetPacketType() == AVRC_CT_VENDOR_PACKET_TYPE_END)) {
1317         SendNextVendorCmd(rawAddr);
1318     }
1319     if (supported) {
1320         InformNotificationChanged(rawAddr, notifyPkt, result);
1321     }
1322 
1323     if (supported && crCode == AVRC_CT_RSP_CODE_CHANGED) {
1324         std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtNotifyPacket>(notifyPkt->GetEventId());
1325         SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_REGISTER_NOTIFICATION);
1326     }
1327 }
1328 
InformNotificationChanged(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtNotifyPacket> & notifyPkt,int result)1329 void AvrcCtProfile::InformNotificationChanged(
1330     const RawAddress &rawAddr, const std::shared_ptr<AvrcCtNotifyPacket> &notifyPkt, int result)
1331 {
1332     HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1333 
1334     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1335 
1336     switch (notifyPkt->GetEventId()) {
1337         case AVRC_CT_EVENT_ID_PLAYBACK_STATUS_CHANGED:
1338             myObserver_->onPlaybackStatusChanged(rawAddr, notifyPkt->GetPlayStatus(), result);
1339             break;
1340         case AVRC_CT_EVENT_ID_TRACK_CHANGED:
1341             myObserver_->onTrackChanged(rawAddr, notifyPkt->GetUid(), result);
1342             break;
1343         case AVRC_CT_EVENT_ID_TRACK_REACHED_END:
1344             myObserver_->onTrackReachedEnd(rawAddr, result);
1345             break;
1346         case AVRC_CT_EVENT_ID_TRACK_REACHED_START:
1347             myObserver_->onTrackReachedStart(rawAddr, result);
1348             break;
1349         case AVRC_CT_EVENT_ID_PLAYBACK_POS_CHANGED:
1350             myObserver_->onPlaybackPosChanged(rawAddr, notifyPkt->GetPlaybackPosition(), result);
1351             break;
1352         case AVRC_CT_EVENT_ID_NOW_PLAYING_CONTENT_CHANGED:
1353             myObserver_->onNowPlayingContentChanged(rawAddr, result);
1354             break;
1355         case AVRC_CT_EVENT_ID_AVAILABLE_PLAYERS_CHANGED:
1356             myObserver_->onAvailablePlayersChanged(rawAddr, result);
1357             break;
1358         case AVRC_CT_EVENT_ID_ADDRESSED_PLAYER_CHANGED:
1359             cnManager->SetUidCounter(rawAddr, notifyPkt->GetUidCounter());
1360             myObserver_->onAddressedPlayerChanged(
1361                 rawAddr, notifyPkt->GetPlayerId(), notifyPkt->GetUidCounter(), result);
1362             break;
1363         case AVRC_CT_EVENT_ID_UIDS_CHANGED:
1364             cnManager->SetUidCounter(rawAddr, notifyPkt->GetUidCounter());
1365             myObserver_->onUidChanged(rawAddr, notifyPkt->GetUidCounter(), result);
1366             break;
1367         case AVRC_CT_EVENT_ID_VOLUME_CHANGED:
1368             myObserver_->onVolumeChanged(rawAddr, notifyPkt->GetAbsoluteVolume(), result);
1369             break;
1370         case AVRC_CT_EVENT_ID_PLAYER_APPLICATION_SETTING_CHANGED:
1371             InformPlayerApplicationSettingChanged(rawAddr, notifyPkt, result);
1372             break;
1373         default:
1374             HILOGI("The event id is wrong! event: %{public}d", notifyPkt->GetEventId());
1375             break;
1376     }
1377 }
1378 
InformPlayerApplicationSettingChanged(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtNotifyPacket> & notifyPkt,int result)1379 void AvrcCtProfile::InformPlayerApplicationSettingChanged(
1380     const RawAddress &rawAddr, const std::shared_ptr<AvrcCtNotifyPacket> &notifyPkt, int result)
1381 {
1382     HILOGI("address: %{public}s, result: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), result);
1383 
1384     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1385 
1386     if (notifyPkt->GetPacketType() == AVRC_CT_VENDOR_PACKET_TYPE_START ||
1387         notifyPkt->GetPacketType() == AVRC_CT_VENDOR_PACKET_TYPE_CONTINUE) {
1388         std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorContinuePacket(rawAddr);
1389         if (packet == nullptr) {
1390             packet = notifyPkt;
1391         }
1392 
1393         if (packet->GetReceivedFragments() >= maxFragments_) {
1394             SendVendorAbortCmd(rawAddr, notifyPkt->GetPduId());
1395             myObserver_->onVolumeChanged(rawAddr, notifyPkt->GetAbsoluteVolume(), result);
1396         } else {
1397             cnManager->SetVendorContinuePacket(rawAddr, packet);
1398             SendVendorContinueCmd(rawAddr, notifyPkt->GetPduId());
1399         }
1400     } else {
1401         myObserver_->onPlayerApplicationSettingChanged(
1402             rawAddr, notifyPkt->GetAttributes(), notifyPkt->GetValues(), result);
1403     }
1404 }
1405 
ReceiveVendorRspAvcStatus(const RawAddress & rawAddr,Packet * pkt)1406 void AvrcCtProfile::ReceiveVendorRspAvcStatus(const RawAddress &rawAddr, Packet *pkt)
1407 {
1408     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1409 
1410     switch (AvrcCtPacket::GetVendorPdu(pkt)) {
1411         case AVRC_CT_PDU_ID_GET_CAPABILITIES:
1412             ReceiveGetCapabilitiesRsp(rawAddr, pkt);
1413             break;
1414         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES:
1415             ReceiveListPlayerApplicationSettingAttributesRsp(rawAddr, pkt);
1416             break;
1417         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES:
1418             ReceiveListPlayerApplicationSettingValuesRsp(rawAddr, pkt);
1419             break;
1420         case AVRC_CT_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:
1421             ReceiveGetCurrentPlayerApplicationSettingValueRsp(rawAddr, pkt);
1422             break;
1423         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT:
1424             ReceiveGetPlayerApplicationSettingAttributeTextRsp(rawAddr, pkt);
1425             break;
1426         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT:
1427             ReceiveGetPlayerApplicationSettingValueTextRsp(rawAddr, pkt);
1428             break;
1429         case AVRC_CT_PDU_ID_GET_ELEMENT_ATTRIBUTES:
1430             ReceiveGetElementAttributesRsp(rawAddr, pkt);
1431             break;
1432         case AVRC_CT_PDU_ID_GET_PLAY_STATUS:
1433             ReceiveGetPlayStatusRsp(rawAddr, pkt);
1434             break;
1435         default:
1436             break;
1437     }
1438 }
1439 
ReceiveVendorRspAvcControl(const RawAddress & rawAddr,Packet * pkt)1440 void AvrcCtProfile::ReceiveVendorRspAvcControl(const RawAddress &rawAddr, Packet *pkt)
1441 {
1442     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1443 
1444     switch (AvrcCtPacket::GetVendorPdu(pkt)) {
1445         case AVRC_CT_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:
1446             ReceiveSetPlayerApplicationSettingValueRsp(rawAddr, pkt);
1447             break;
1448         case AVRC_CT_PDU_ID_ABORT_CONTINUING_RESPONSE:
1449             ReceiveAbortContinuingResponseRsp(rawAddr, pkt);
1450             break;
1451         case AVRC_CT_PDU_ID_SET_ADDRESSED_PLAYER:
1452             ReceiveSetAddressedPlayerRsp(rawAddr, pkt);
1453             break;
1454         case AVRC_CT_PDU_ID_PLAY_ITEM:
1455             ReceivePlayItemRsp(rawAddr, pkt);
1456             break;
1457         case AVRC_CT_PDU_ID_ADD_TO_NOW_PLAYING:
1458             ReceiveAddToNowPlayingRsp(rawAddr, pkt);
1459             break;
1460         case AVRC_CT_PDU_ID_SET_ABSOLUTE_VOLUME:
1461             ReceiveSetAbsoluteVolumeRsp(rawAddr, pkt);
1462             break;
1463         default:
1464             break;
1465     }
1466 }
1467 
ReceiveVendorRsp(const RawAddress & rawAddr,Packet * pkt)1468 void AvrcCtProfile::ReceiveVendorRsp(const RawAddress &rawAddr, Packet *pkt)
1469 {
1470     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1471 
1472     switch (AvrcCtPacket::GetVendorPdu(pkt)) {
1473         case AVRC_CT_PDU_ID_GET_CAPABILITIES:
1474         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES:
1475         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES:
1476         case AVRC_CT_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:
1477         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT:
1478         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT:
1479         case AVRC_CT_PDU_ID_GET_ELEMENT_ATTRIBUTES:
1480         case AVRC_CT_PDU_ID_GET_PLAY_STATUS:
1481             ReceiveVendorRspAvcStatus(rawAddr, pkt);
1482             break;
1483         case AVRC_CT_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:
1484         case AVRC_CT_PDU_ID_ABORT_CONTINUING_RESPONSE:
1485         case AVRC_CT_PDU_ID_SET_ADDRESSED_PLAYER:
1486         case AVRC_CT_PDU_ID_PLAY_ITEM:
1487         case AVRC_CT_PDU_ID_ADD_TO_NOW_PLAYING:
1488         case AVRC_CT_PDU_ID_SET_ABSOLUTE_VOLUME:
1489             ReceiveVendorRspAvcControl(rawAddr, pkt);
1490             break;
1491         case AVRC_CT_PDU_ID_REGISTER_NOTIFICATION:
1492             ReceiveRegisterNotificationRsp(rawAddr, pkt);
1493             break;
1494         default:
1495             HILOGI("The PDU ID is wrong! Address: %{public}s, pduId: %{public}x",
1496                 GET_ENCRYPT_AVRCP_ADDR(rawAddr), AvrcCtPacket::GetVendorPdu(pkt));
1497             break;
1498     }
1499 }
1500 
ProcessVendorNotificationTimeout(RawAddress rawAddr)1501 void AvrcCtProfile::ProcessVendorNotificationTimeout(RawAddress rawAddr)
1502 {
1503     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1504 
1505     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1506     AvrcCtNotifyPacket *pkt = static_cast<AvrcCtNotifyPacket *>(cnManager->GetVendorPacket(rawAddr).get());
1507     uint8_t event = pkt->GetEventId();
1508     if (cnManager->IsNotifyStateEnabled(rawAddr, event)) {
1509         std::shared_ptr<AvrcCtVendorPacket> packet = std::make_shared<AvrcCtNotifyPacket>(event);
1510         SendVendorCmd(rawAddr, packet, AVRC_CT_SM_EVENT_REGISTER_NOTIFICATION);
1511     }
1512 }
1513 
ProcessVendorAvcControlTimeout(RawAddress rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & packet)1514 void AvrcCtProfile::ProcessVendorAvcControlTimeout(RawAddress rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &packet)
1515 {
1516     switch (packet->GetPduId()) {
1517         case AVRC_CT_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE: {
1518             myObserver_->onSetPlayerApplicationSettingValue(rawAddr, RET_BAD_STATUS);
1519             break;
1520         }
1521         case AVRC_CT_PDU_ID_REQUEST_CONTINUING_RESPONSE:
1522         /// FALL THROUGH
1523         case AVRC_CT_PDU_ID_ABORT_CONTINUING_RESPONSE: {
1524             AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1525             std::shared_ptr<AvrcCtVendorPacket> pkt = cnManager->GetVendorContinuePacket(rawAddr);
1526             cnManager->SetVendorPacket(rawAddr, pkt);
1527             ProcessVendorTimeout(rawAddr);
1528             break;
1529         }
1530         case AVRC_CT_PDU_ID_SET_ADDRESSED_PLAYER: {
1531             myObserver_->onSetAddressedPlayer(rawAddr, RET_BAD_STATUS, AVRC_ES_CODE_INTERNAL_ERROR);
1532             break;
1533         }
1534         case AVRC_CT_PDU_ID_PLAY_ITEM: {
1535             AvrcCtPiPacket *pkt = static_cast<AvrcCtPiPacket *>(packet.get());
1536             myObserver_->onPlayItem(rawAddr, pkt->GetStatus(), RET_BAD_STATUS);
1537             break;
1538         }
1539         case AVRC_CT_PDU_ID_SET_ABSOLUTE_VOLUME: {
1540             AvrcCtSavPacket *pkt = static_cast<AvrcCtSavPacket *>(packet.get());
1541             myObserver_->onSetAbsoluteVolume(rawAddr, pkt->GetAbsoluteVolume(), RET_BAD_STATUS);
1542             break;
1543         }
1544         case AVRC_CT_PDU_ID_ADD_TO_NOW_PLAYING: {
1545             myObserver_->onAddToNowPlaying(rawAddr, RET_BAD_STATUS, AVRC_ES_CODE_INTERNAL_ERROR);
1546             break;
1547         }
1548         default:
1549             break;
1550     }
1551 }
1552 
ProcessVendorAvcStatus1Timeout(RawAddress rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & packet)1553 void AvrcCtProfile::ProcessVendorAvcStatus1Timeout(RawAddress rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &packet)
1554 {
1555     switch (packet->GetPduId()) {
1556         case AVRC_CT_PDU_ID_GET_CAPABILITIES: {
1557             AvrcCtGcPacket *gcPkt = static_cast<AvrcCtGcPacket *>(packet.get());
1558             myObserver_->onGetCapabilities(rawAddr, gcPkt->GetCompanies(), gcPkt->GetEvents(), RET_BAD_STATUS);
1559             break;
1560         }
1561         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES: {
1562             AvrcCtLpasaPacket *pkt = static_cast<AvrcCtLpasaPacket *>(packet.get());
1563             myObserver_->onListPlayerApplicationSettingAttributes(rawAddr, pkt->GetAttributes(), RET_BAD_STATUS);
1564             break;
1565         }
1566         case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES: {
1567             AvrcCtLpasvPacket *pkt = static_cast<AvrcCtLpasvPacket *>(packet.get());
1568             myObserver_->onListPlayerApplicationSettingValues(
1569                 rawAddr, pkt->GetAttribute(), pkt->GetValues(), RET_BAD_STATUS);
1570             break;
1571         }
1572         case AVRC_CT_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE: {
1573             AvrcCtGcpasvPacket *pkt = static_cast<AvrcCtGcpasvPacket *>(packet.get());
1574             myObserver_->onGetCurrentPlayerApplicationSettingValue(
1575                 rawAddr, pkt->GetAttributes(), pkt->GetValues(), RET_BAD_STATUS);
1576             break;
1577         }
1578 
1579         default:
1580             break;
1581     }
1582 }
1583 
ProcessVendorAvcStatus2Timeout(RawAddress rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & packet)1584 void AvrcCtProfile::ProcessVendorAvcStatus2Timeout(RawAddress rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &packet)
1585 {
1586     switch (packet->GetPduId()) {
1587         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT: {
1588             AvrcCtGpasatPacket *gpasatPkt = static_cast<AvrcCtGpasatPacket *>(packet.get());
1589             myObserver_->onGetPlayerApplicationSettingAttributeText(
1590                 rawAddr, gpasatPkt->GetAttributes(), gpasatPkt->GetAttributeName(), RET_BAD_STATUS);
1591             break;
1592         }
1593         case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT: {
1594             AvrcCtGpasvtPacket *gpasvtPkt = static_cast<AvrcCtGpasvtPacket *>(packet.get());
1595             myObserver_->onGetPlayerApplicationSettingAttributeText(
1596                 rawAddr, gpasvtPkt->GetValues(), gpasvtPkt->GetValueName(), RET_BAD_STATUS);
1597             break;
1598         }
1599         case AVRC_CT_PDU_ID_GET_ELEMENT_ATTRIBUTES: {
1600             AvrcCtGeaPacket *geaPkt = static_cast<AvrcCtGeaPacket *>(packet.get());
1601             myObserver_->onGetElementAttributes(rawAddr, geaPkt->GetAttributes(), geaPkt->GetValues(), RET_BAD_STATUS);
1602             break;
1603         }
1604         case AVRC_CT_PDU_ID_GET_PLAY_STATUS: {
1605             AvrcCtGpsPacket *pkt = static_cast<AvrcCtGpsPacket *>(packet.get());
1606             myObserver_->onGetPlayStatus(
1607                 rawAddr, pkt->GetSongLength(), pkt->GetSongPosition(), pkt->GetPlayStatus(), RET_BAD_STATUS);
1608             break;
1609         }
1610         default:
1611             break;
1612     }
1613 }
1614 
ProcessVendorTimeout(RawAddress rawAddr)1615 void AvrcCtProfile::ProcessVendorTimeout(RawAddress rawAddr)
1616 {
1617     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1618 
1619     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1620 
1621     if (IsEnabled()) {
1622         std::shared_ptr<AvrcCtVendorPacket> packet = cnManager->GetVendorPacket(rawAddr);
1623         if (packet != nullptr) {
1624             switch (packet->GetPduId()) {
1625                 case AVRC_CT_PDU_ID_GET_CAPABILITIES:
1626                 case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES:
1627                 case AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES:
1628                 case AVRC_CT_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:
1629                     ProcessVendorAvcStatus1Timeout(rawAddr, packet);
1630                     break;
1631                 case AVRC_CT_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:
1632                 case AVRC_CT_PDU_ID_REQUEST_CONTINUING_RESPONSE:
1633                 /// FALL THROUGH
1634                 case AVRC_CT_PDU_ID_ABORT_CONTINUING_RESPONSE:
1635                 case AVRC_CT_PDU_ID_SET_ADDRESSED_PLAYER:
1636                 case AVRC_CT_PDU_ID_PLAY_ITEM:
1637                 case AVRC_CT_PDU_ID_SET_ABSOLUTE_VOLUME:
1638                 case AVRC_CT_PDU_ID_ADD_TO_NOW_PLAYING:
1639                     ProcessVendorAvcControlTimeout(rawAddr, packet);
1640                     break;
1641                 case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT:
1642                 case AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT:
1643                 case AVRC_CT_PDU_ID_GET_ELEMENT_ATTRIBUTES:
1644                 case AVRC_CT_PDU_ID_GET_PLAY_STATUS:
1645                     ProcessVendorAvcStatus2Timeout(rawAddr, packet);
1646                     break;
1647                 case AVRC_CT_PDU_ID_REGISTER_NOTIFICATION: {
1648                     ProcessVendorNotificationTimeout(rawAddr);
1649                     break;
1650                 }
1651                 default:
1652                     HILOGI("The PDU ID is wrong! Address: %{public}s, pduId: %{public}x",
1653                         GET_ENCRYPT_AVRCP_ADDR(rawAddr), packet->GetPduId());
1654                     break;
1655             }
1656         } else {
1657             HILOGI("The saved packet is nullptr! Address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1658         }
1659 
1660         SendNextVendorCmd(rawAddr);
1661     }
1662 }
1663 
VendorTimeoutCallback(const RawAddress & rawAddr)1664 void AvrcCtProfile::VendorTimeoutCallback(const RawAddress &rawAddr)
1665 {
1666     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1667 
1668     if (IsEnabled()) {
1669         dispatcher_->PostTask(std::bind(&AvrcCtProfile::ProcessVendorTimeout, this, rawAddr));
1670     }
1671 }
1672 
1673 /******************************************************************
1674  * BROWSING COMMAND                                               *
1675  ******************************************************************/
1676 
SendBrowseCmd(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtBrowsePacket> & pkt,AvrcCtSmEvent event)1677 void AvrcCtProfile::SendBrowseCmd(
1678     const RawAddress &rawAddr, const std::shared_ptr<AvrcCtBrowsePacket> &pkt, AvrcCtSmEvent event)
1679 {
1680     HILOGI("address: %{public}s, event%{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), event);
1681 
1682     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1683     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
1684 
1685     if (!smManager->IsBrowsePendingState(rawAddr)) {
1686         cnManager->ClearBrowseInfo(rawAddr);
1687         cnManager->SetBrowsePacket(rawAddr, pkt);
1688 
1689         utility::Message msg(event);
1690         AvrcCtStateMachineManager::GetInstance()->SendMessageToBrowseStateMachine(rawAddr, msg);
1691 
1692         auto func = std::bind(&AvrcCtProfile::BrowseTimeoutCallback, this, rawAddr);
1693         cnManager->SetBrowseTimer(rawAddr, func, AVRC_CT_TIMER_T_MTP);
1694     } else {
1695         if (cnManager->GetSizeOfBrowseQueue(rawAddr) > AVRC_CT_DEFAULT_SIZE_OF_QUEUE) {
1696             HILOGI("The queue is oversize: %{public}d", cnManager->GetSizeOfBrowseQueue(rawAddr));
1697         } else {
1698             cnManager->PushBrowseQueue(rawAddr, pkt);
1699             HILOGI("Waiting for the response! Address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1700         }
1701     }
1702 }
1703 
SendNextBrowseCmd(const RawAddress & rawAddr)1704 void AvrcCtProfile::SendNextBrowseCmd(const RawAddress &rawAddr)
1705 {
1706     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1707 
1708     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1709     cnManager->ClearBrowseInfo(rawAddr);
1710 
1711     utility::Message msg(AVRC_CT_SM_EVENT_TO_CONNECTED_STATE);
1712     AvrcCtStateMachineManager::GetInstance()->SendMessageToBrowseStateMachine(rawAddr, msg);
1713 
1714     if (cnManager->GetSizeOfBrowseQueue(rawAddr) != 0x00) {
1715         std::shared_ptr<AvrcCtBrowsePacket> packet = cnManager->PopBrowseQueue(rawAddr);
1716         AvrcCtSmEvent event = static_cast<AvrcCtSmEvent>(packet->GetPduId());
1717 
1718         SendBrowseCmd(rawAddr, packet, event);
1719     }
1720 }
1721 
IsBrowseQueueFull(const RawAddress & rawAddr)1722 bool AvrcCtProfile::IsBrowseQueueFull(const RawAddress &rawAddr)
1723 {
1724     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1725     std::lock_guard<std::recursive_mutex> lock(mutex_);
1726 
1727     return (AvrcCtConnectManager::GetInstance()->GetSizeOfBrowseQueue(rawAddr) == AVRC_CT_DEFAULT_SIZE_OF_QUEUE);
1728 }
1729 
IsDisableAbsoluteVolume(const RawAddress & rawAddr)1730 bool AvrcCtProfile::IsDisableAbsoluteVolume(const RawAddress &rawAddr)
1731 {
1732     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1733     std::lock_guard<std::recursive_mutex> lock(mutex_);
1734 
1735     return AvrcCtConnectManager::GetInstance()->IsDisableAbsoluteVolume(rawAddr);
1736 }
1737 
IsBrowsingConnected(const RawAddress & rawAddr)1738 bool AvrcCtProfile::IsBrowsingConnected(const RawAddress &rawAddr)
1739 {
1740     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1741     std::lock_guard<std::recursive_mutex> lock(mutex_);
1742 
1743     return AvrcCtConnectManager::GetInstance()->IsBrowsingConnected(rawAddr);
1744 }
1745 
SendSetBrowsedPlayerCmd(const RawAddress & rawAddr,uint16_t playerId)1746 void AvrcCtProfile::SendSetBrowsedPlayerCmd(const RawAddress &rawAddr, uint16_t playerId)
1747 {
1748     HILOGI("address: %{public}s, playerId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId);
1749 
1750     std::shared_ptr<AvrcCtBrowsePacket> packet = std::make_shared<AvrcCtSbpPacket>(playerId);
1751     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1752     SendBrowseCmd(rawAddr, packet, AVRC_CT_SM_EVENT_SET_BROWSED_PLAYER);
1753     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1754 }
1755 
ReceiveSetBrowsedPlayerRsp(const RawAddress & rawAddr,Packet * pkt) const1756 void AvrcCtProfile::ReceiveSetBrowsedPlayerRsp(const RawAddress &rawAddr, Packet *pkt) const
1757 {
1758     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1759 
1760     AvrcCtSbpPacket packet(pkt);
1761 
1762     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1763     cnManager->SetUidCounter(rawAddr, packet.GetUidCounter());
1764 
1765     int result = RET_BAD_STATUS;
1766     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1767         result = BT_SUCCESS;
1768     }
1769 
1770     myObserver_->onSetBrowsedPlayer(
1771         rawAddr, packet.GetUidCounter(), packet.GetNumOfItems(), packet.GetFolderNames(), result, packet.GetStatus());
1772 }
1773 
SendChangePathCmd(const RawAddress & rawAddr,uint16_t uidCounter,uint8_t direction,uint64_t folderUid)1774 void AvrcCtProfile::SendChangePathCmd(
1775     const RawAddress &rawAddr, uint16_t uidCounter, uint8_t direction, uint64_t folderUid)
1776 {
1777     HILOGI("address: %{public}s, uidCounter: %{public}d, direction: %{public}x, folderUid: %{public}jx",
1778         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, direction, folderUid);
1779 
1780     std::shared_ptr<AvrcCtBrowsePacket> packet = std::make_shared<AvrcCtCpPacket>(uidCounter, direction, folderUid);
1781     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1782     SendBrowseCmd(rawAddr, packet, AVRC_CT_SM_EVENT_CHANGE_PATH);
1783     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1784 }
1785 
ReceiveChangePathRsp(const RawAddress & rawAddr,Packet * pkt) const1786 void AvrcCtProfile::ReceiveChangePathRsp(const RawAddress &rawAddr, Packet *pkt) const
1787 {
1788     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1789 
1790     AvrcCtCpPacket packet(pkt);
1791 
1792     int result = RET_BAD_STATUS;
1793     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1794         result = BT_SUCCESS;
1795     }
1796 
1797     myObserver_->onChangePath(rawAddr, packet.GetNumOfItems(), result, packet.GetStatus());
1798 }
1799 
SendGetFolderItemsCmd(const RawAddress & rawAddr,uint8_t scope,uint32_t startItem,uint32_t endItem,const std::vector<uint32_t> & attributes)1800 void AvrcCtProfile::SendGetFolderItemsCmd(const RawAddress &rawAddr, uint8_t scope, uint32_t startItem,
1801     uint32_t endItem, const std::vector<uint32_t> &attributes)
1802 {
1803     HILOGI("addr:%{public}s, scope:%{public}d, startItem:%{public}d, endItem:%{public}d, attributes.size:%{public}zu",
1804         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, startItem, endItem, attributes.size());
1805 
1806     std::shared_ptr<AvrcCtBrowsePacket> packet =
1807         std::make_shared<AvrcCtGfiPacket>(scope, startItem, endItem, attributes);
1808     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1809     SendBrowseCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_ITEM_ATTRIBUTES);
1810     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1811 }
1812 
ReceiveGetFolderItemsRsp(const RawAddress & rawAddr,Packet * pkt) const1813 void AvrcCtProfile::ReceiveGetFolderItemsRsp(const RawAddress &rawAddr, Packet *pkt) const
1814 {
1815     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1816 
1817     AvrcCtGfiPacket packet(pkt);
1818 
1819     int result = RET_BAD_STATUS;
1820     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1821         result = BT_SUCCESS;
1822     }
1823 
1824     myObserver_->onGetFolderItems(rawAddr,
1825         packet.GetScope(),
1826         packet.GetUidCounter(),
1827         packet.GetMediaPlayers(),
1828         packet.GetMediaItems(),
1829         result,
1830         packet.GetStatus());
1831 }
1832 
SendGetItemAttributesCmd(const RawAddress & rawAddr,uint8_t scope,uint64_t uid,uint16_t uidCounter,const std::vector<uint32_t> & attributes)1833 void AvrcCtProfile::SendGetItemAttributesCmd(const RawAddress &rawAddr, uint8_t scope, uint64_t uid,
1834     uint16_t uidCounter, const std::vector<uint32_t> &attributes)
1835 {
1836     HILOGI("addr: %{public}s, scope: %{public}x, uid: %{public}jx, uidCounter: %{public}hu",
1837         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, uid, uidCounter);
1838 
1839     std::shared_ptr<AvrcCtBrowsePacket> packet = std::make_shared<AvrcCtGiaPacket>(scope, uid, uidCounter, attributes);
1840     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1841     SendBrowseCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_ITEM_ATTRIBUTES);
1842     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1843 }
1844 
ReceiveGetItemAttributesRsp(const RawAddress & rawAddr,Packet * pkt) const1845 void AvrcCtProfile::ReceiveGetItemAttributesRsp(const RawAddress &rawAddr, Packet *pkt) const
1846 {
1847     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1848 
1849     AvrcCtGiaPacket packet(pkt);
1850 
1851     int result = RET_BAD_STATUS;
1852     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1853         result = BT_SUCCESS;
1854     }
1855 
1856     myObserver_->onGetItemAttributes(rawAddr, packet.GetAttributes(), packet.GetValues(), result, packet.GetStatus());
1857 }
1858 
SendGetTotalNumberOfItemsCmd(const RawAddress & rawAddr,uint8_t scope)1859 void AvrcCtProfile::SendGetTotalNumberOfItemsCmd(const RawAddress &rawAddr, uint8_t scope)
1860 {
1861     HILOGI("address: %{public}s, scope: %{public}x", GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope);
1862 
1863     std::shared_ptr<AvrcCtBrowsePacket> packet = std::make_shared<AvrcCtGtnoiPacket>(scope);
1864     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
1865     SendBrowseCmd(rawAddr, packet, AVRC_CT_SM_EVENT_GET_TOTAL_NUMBER_OF_ITEMS);
1866     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
1867 }
1868 
ReceiveGetTotalNumberOfItemsRsp(const RawAddress & rawAddr,Packet * pkt) const1869 void AvrcCtProfile::ReceiveGetTotalNumberOfItemsRsp(const RawAddress &rawAddr, Packet *pkt) const
1870 {
1871     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1872 
1873     AvrcCtGtnoiPacket packet(pkt);
1874 
1875     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
1876     cnManager->SetUidCounter(rawAddr, packet.GetUidCounter());
1877 
1878     int result = RET_BAD_STATUS;
1879     if (packet.GetStatus() == AVRC_ES_CODE_NO_ERROR) {
1880         result = BT_SUCCESS;
1881     }
1882 
1883     myObserver_->onGetTotalNumberOfItems(
1884         rawAddr, packet.GetUidCounter(), packet.GetNumOfItems(), result, packet.GetStatus());
1885 }
1886 
ReceiveBrowseRsp(const RawAddress & rawAddr,Packet * pkt)1887 void AvrcCtProfile::ReceiveBrowseRsp(const RawAddress &rawAddr, Packet *pkt)
1888 {
1889     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1890 
1891     SendNextBrowseCmd(rawAddr);
1892 
1893     switch (AvrcCtPacket::GetBrowsePdu(pkt)) {
1894         case AVRC_CT_PDU_ID_SET_BROWSED_PLAYER:
1895             ReceiveSetBrowsedPlayerRsp(rawAddr, pkt);
1896             break;
1897         case AVRC_CT_PDU_ID_CHANGE_PATH:
1898             ReceiveChangePathRsp(rawAddr, pkt);
1899             break;
1900         case AVRC_CT_PDU_ID_GET_FOLDER_ITEMS:
1901             ReceiveGetFolderItemsRsp(rawAddr, pkt);
1902             break;
1903         case AVRC_CT_PDU_ID_GET_ITEM_ATTRIBUTES:
1904             ReceiveGetItemAttributesRsp(rawAddr, pkt);
1905             break;
1906         case AVRC_CT_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:
1907             ReceiveGetTotalNumberOfItemsRsp(rawAddr, pkt);
1908             break;
1909         default:
1910             HILOGI("The PDU ID is wrong! Address: %{public}s, pduId: %{public}x",
1911                 GET_ENCRYPT_AVRCP_ADDR(rawAddr), AvrcCtPacket::GetBrowsePdu(pkt));
1912             break;
1913     }
1914 }
1915 
ProcessBrowseTimeout(RawAddress rawAddr)1916 void AvrcCtProfile::ProcessBrowseTimeout(RawAddress rawAddr)
1917 {
1918     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1919 
1920     if (IsEnabled()) {
1921         std::shared_ptr<AvrcCtBrowsePacket> packet = AvrcCtConnectManager::GetInstance()->GetBrowsePacket(rawAddr);
1922         if (packet != nullptr) {
1923             switch (packet->GetPduId()) {
1924                 case AVRC_CT_PDU_ID_SET_BROWSED_PLAYER: {
1925                     AvrcCtSbpPacket *pkt = static_cast<AvrcCtSbpPacket *>(packet.get());
1926                     myObserver_->onSetBrowsedPlayer(rawAddr,
1927                         pkt->GetUidCounter(),
1928                         0x00,
1929                         pkt->GetFolderNames(),
1930                         RET_BAD_STATUS,
1931                         AVRC_ES_CODE_INTERNAL_ERROR);
1932                     break;
1933                 }
1934                 case AVRC_CT_PDU_ID_CHANGE_PATH:
1935                     myObserver_->onChangePath(rawAddr, 0x00, RET_BAD_STATUS, AVRC_ES_CODE_INTERNAL_ERROR);
1936                     break;
1937                 case AVRC_CT_PDU_ID_GET_FOLDER_ITEMS: {
1938                     AvrcCtGfiPacket *pkt = static_cast<AvrcCtGfiPacket *>(packet.get());
1939                     myObserver_->onGetFolderItems(rawAddr,
1940                         pkt->GetScope(),
1941                         pkt->GetUidCounter(),
1942                         pkt->GetMediaPlayers(),
1943                         pkt->GetMediaItems(),
1944                         RET_BAD_STATUS,
1945                         AVRC_ES_CODE_INTERNAL_ERROR);
1946                     break;
1947                 }
1948                 case AVRC_CT_PDU_ID_GET_ITEM_ATTRIBUTES: {
1949                     AvrcCtGiaPacket *pkt = static_cast<AvrcCtGiaPacket *>(packet.get());
1950                     myObserver_->onGetItemAttributes(
1951                         rawAddr, pkt->GetAttributes(), pkt->GetValues(), RET_BAD_STATUS, AVRC_ES_CODE_INTERNAL_ERROR);
1952                     break;
1953                 }
1954                 case AVRC_CT_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:
1955                     myObserver_->onGetTotalNumberOfItems(
1956                         rawAddr, 0x00, 0x00, RET_BAD_STATUS, AVRC_ES_CODE_INTERNAL_ERROR);
1957                     break;
1958                 default:
1959                     HILOGI("The PDU ID is wrong! Address: %{public}s, pduId: %{public}x",
1960                         GET_ENCRYPT_AVRCP_ADDR(rawAddr), packet->GetPduId());
1961                     break;
1962             }
1963         }
1964         SendNextBrowseCmd(rawAddr);
1965     }
1966 }
1967 
BrowseTimeoutCallback(const RawAddress & rawAddr)1968 void AvrcCtProfile::BrowseTimeoutCallback(const RawAddress &rawAddr)
1969 {
1970     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1971 
1972     if (IsEnabled()) {
1973         dispatcher_->PostTask(std::bind(&AvrcCtProfile::ProcessBrowseTimeout, this, rawAddr));
1974     }
1975 }
1976 
ProcessChannelEvent(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const1977 void AvrcCtProfile::ProcessChannelEvent(
1978     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
1979 {
1980     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
1981         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
1982     switch (event) {
1983         case AVCT_CONNECT_IND_EVT:
1984             ProcessChannelEventConnectIndEvt(rawAddr, connectId, event, result, context);
1985             break;
1986         case AVCT_CONNECT_CFM_EVT:
1987             ProcessChannelEventConnectCfmEvt(rawAddr, connectId, event, result, context);
1988             break;
1989         case AVCT_DISCONNECT_IND_EVT:
1990             ProcessChannelEventDisconnectIndEvt(rawAddr, connectId, event, result, context);
1991             break;
1992         case AVCT_DISCONNECT_CFM_EVT:
1993             ProcessChannelEventDisconnectCfmEvt(rawAddr, connectId, event, result, context);
1994             break;
1995         case AVCT_BR_CONNECT_IND_EVT:
1996             ProcessChannelEventBrConnectIndEvt(rawAddr, connectId, event, result, context);
1997             break;
1998         case AVCT_BR_CONNECT_CFM_EVT:
1999             ProcessChannelEventBrConnectCfmEvt(rawAddr, connectId, event, result, context);
2000             break;
2001         case AVCT_BR_DISCONNECT_IND_EVT:
2002             DeleteBrowseStateMachine(rawAddr);
2003             break;
2004         case AVCT_BR_DISCONNECT_CFM_EVT:
2005             DeleteBrowseStateMachine(rawAddr);
2006             break;
2007         default:
2008             break;
2009     };
2010 }
2011 
ProcessChannelEventConnectIndEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2012 void AvrcCtProfile::ProcessChannelEventConnectIndEvt(
2013     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2014 {
2015     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2016         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2017 
2018     result = ExpainAvctResult(result);
2019 
2020     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2021     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
2022     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
2023 
2024     if (result == BT_SUCCESS) {
2025         bool absVolume =
2026             Compat::CompatCheck(bluetooth::CompatType::COMPAT_REJECT_ABSOLUTE_VOLUME, rawAddr.GetAddress());
2027         result |= cnManager->Add(rawAddr,
2028             connectId,
2029             AVCT_ACPT,
2030             controlMtu_,
2031             browseMtu_,
2032             companyId_,
2033             0x0000,
2034             absVolume,
2035             eventCallback_,
2036             msgCallback_);
2037         result |= smManager->AddControlStateMachine(rawAddr);
2038         if (result == BT_SUCCESS) {
2039             msg.what_ = AVRC_CT_SM_EVENT_TO_CONNECTED_STATE;
2040             AvrcCtStateMachineManager::GetInstance()->SendMessageToControlStateMachine(rawAddr, msg);
2041 
2042             myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::CONNECTED));
2043             IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_ON, PROFILE_NAME_AVRCP_CT, rawAddr);
2044         }
2045     }
2046 }
2047 
ProcessChannelEventConnectCfmEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2048 void AvrcCtProfile::ProcessChannelEventConnectCfmEvt(
2049     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2050 {
2051     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2052         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2053 
2054     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2055     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
2056     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
2057 
2058     if (result == BT_SUCCESS) {
2059         cnManager->SetConnectId(rawAddr, connectId);
2060 
2061         msg.what_ = AVRC_CT_SM_EVENT_TO_CONNECTED_STATE;
2062         smManager->SendMessageToControlStateMachine(rawAddr, msg);
2063 
2064         if (IsSupportedBrowsing()) {
2065             ConnectBr(rawAddr);
2066         }
2067         cnManager->DeleteDisconnectedDevice(rawAddr.GetAddress());
2068         if (!IsSupportedBrowsing()) {
2069             myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::CONNECTED));
2070             IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_ON, PROFILE_NAME_AVRCP_CT, rawAddr);
2071         }
2072     } else {
2073         DeleteResource(rawAddr);
2074         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTED));
2075         IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_OFF, PROFILE_NAME_AVRCP_CT, rawAddr);
2076     }
2077 }
2078 
ProcessChannelEventDisconnectIndEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2079 void AvrcCtProfile::ProcessChannelEventDisconnectIndEvt(
2080     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2081 {
2082     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2083         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2084 
2085     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2086 
2087     myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTING));
2088     DeleteResource(rawAddr);
2089     cnManager->AddDisconnectedDevice(rawAddr.GetAddress());
2090     myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTED));
2091     IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_OFF, PROFILE_NAME_AVRCP_CT, rawAddr);
2092 }
2093 
ProcessChannelEventDisconnectCfmEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2094 void AvrcCtProfile::ProcessChannelEventDisconnectCfmEvt(
2095     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2096 {
2097     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2098         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2099 
2100     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2101     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
2102     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
2103 
2104     if (smManager->IsControlDisableState(rawAddr)) {
2105         DeleteResource(rawAddr);
2106         if (cnManager->IsConnectInfoEmpty()) {
2107             AvrcpCtSafeDelete(cnManager);
2108             smManager->ShutDown();
2109             myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTED));
2110             IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_OFF, PROFILE_NAME_AVRCP_CT, rawAddr);
2111             myObserver_->onDisabled();
2112         }
2113     } else {
2114         DeleteResource(rawAddr);
2115         cnManager->AddDisconnectedDevice(rawAddr.GetAddress());
2116         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::DISCONNECTED));
2117         IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_OFF, PROFILE_NAME_AVRCP_CT, rawAddr);
2118     }
2119 }
2120 
ProcessChannelEventBrConnectIndEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2121 void AvrcCtProfile::ProcessChannelEventBrConnectIndEvt(
2122     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2123 {
2124     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2125         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2126     result = ExpainAvctResult(result);
2127 
2128     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
2129     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2130     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
2131 
2132     if (result == BT_SUCCESS) {
2133         result |= smManager->AddBrowseStateMachine(rawAddr);
2134         if (result == BT_SUCCESS) {
2135             msg.what_ = AVRC_CT_SM_EVENT_TO_CONNECTED_STATE;
2136             smManager->SendMessageToBrowseStateMachine(rawAddr, msg);
2137             cnManager->SetBrowsingState(rawAddr, true);
2138         }
2139     }
2140     if (IsSupportedBrowsing()) {
2141         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::CONNECTED));
2142     }
2143 }
2144 
ProcessChannelEventBrConnectCfmEvt(const RawAddress & rawAddr,uint8_t connectId,uint8_t event,uint16_t result,void * context) const2145 void AvrcCtProfile::ProcessChannelEventBrConnectCfmEvt(
2146     const RawAddress &rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context) const
2147 {
2148     HILOGI("Receive: %{public}s, connectId: %{public}d, Result: %{public}x, Address: %{public}s",
2149         GetEventName(event).c_str(), connectId, result, GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2150     AvrcCtStateMachineManager *smManager = AvrcCtStateMachineManager::GetInstance();
2151     AvrcCtConnectManager *cnManager = AvrcCtConnectManager::GetInstance();
2152     utility::Message msg(AVRC_CT_SM_EVENT_INVALID);
2153 
2154     if (result == BT_SUCCESS) {
2155         msg.what_ = AVRC_CT_SM_EVENT_TO_CONNECTED_STATE;
2156         smManager->SendMessageToBrowseStateMachine(rawAddr, msg);
2157         cnManager->SetBrowsingState(rawAddr, true);
2158         IPowerManager::GetInstance().StatusUpdate(RequestStatus::CONNECT_ON, PROFILE_NAME_AVRCP_CT, rawAddr);
2159     } else {
2160         DeleteBrowseStateMachine(rawAddr);
2161     }
2162     if (IsSupportedBrowsing()) {
2163         myObserver_->onConnectionStateChanged(rawAddr, static_cast<int>(BTConnectState::CONNECTED));
2164     }
2165 }
2166 
ProcessChannelMessage(uint8_t connectId,uint8_t label,uint8_t crType,uint8_t chType,Packet * pkt,void * context)2167 void AvrcCtProfile::ProcessChannelMessage(
2168     uint8_t connectId, uint8_t label, uint8_t crType, uint8_t chType, Packet *pkt, void *context)
2169 {
2170     HILOGI("connectId: %{public}d, label: %{public}d, crType: %{public}d, chType: %{public}d",
2171         connectId, label, crType, chType);
2172 
2173     RawAddress rawAddr = AvrcCtConnectManager::GetInstance()->GetRawAddress(connectId);
2174     IPowerManager::GetInstance().StatusUpdate(RequestStatus::BUSY, PROFILE_NAME_AVRCP_CT, rawAddr);
2175 
2176     if (chType == AVCT_DATA_CTRL) {
2177         uint8_t opCode = AvrcCtPacket::GetOpCode(pkt);
2178         HILOGI("opCode: %{public}x", opCode);
2179         switch (opCode) {
2180             case AVRC_CT_OP_CODE_PASS_THROUGH:
2181                 ReceivePassRsp(rawAddr, pkt);
2182                 break;
2183             case AVRC_CT_OP_CODE_UNIT_INFO:
2184                 ReceiveUnitRsp(rawAddr, pkt);
2185                 break;
2186             case AVRC_CT_OP_CODE_SUB_UNIT_INFO:
2187                 ReceiveSubUnitRsp(rawAddr, pkt);
2188                 break;
2189             case AVRC_CT_OP_CODE_VENDOR:
2190                 ReceiveVendorRsp(rawAddr, pkt);
2191                 break;
2192             default:
2193                 HILOGI("opCode: %{public}x is wrong! ConnectId: %{public}x", opCode, connectId);
2194                 break;
2195         }
2196     } else if (chType == AVCT_DATA_BR) {
2197         ReceiveBrowseRsp(rawAddr, pkt);
2198     } else {
2199         HILOGI("chType: %{public}x is wrong! ConnectId: %{public}x", chType, connectId);
2200     }
2201 
2202     PacketFree(pkt);
2203     IPowerManager::GetInstance().StatusUpdate(RequestStatus::IDLE, PROFILE_NAME_AVRCP_CT, rawAddr);
2204 }
2205 
DeleteResource(const RawAddress & rawAddr)2206 void AvrcCtProfile::DeleteResource(const RawAddress &rawAddr)
2207 {
2208     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2209 
2210     AvrcCtConnectManager::GetInstance()->Delete(rawAddr);
2211     AvrcCtStateMachineManager::GetInstance()->DeletePairOfStateMachine(rawAddr);
2212 }
2213 
DeleteBrowseStateMachine(const RawAddress & rawAddr)2214 void AvrcCtProfile::DeleteBrowseStateMachine(const RawAddress &rawAddr)
2215 {
2216     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
2217 
2218     AvrcCtStateMachineManager::GetInstance()->DeleteBrowseStateMachine(rawAddr);
2219 }
2220 
ExpainAvctResult(uint16_t avctRet)2221 int AvrcCtProfile::ExpainAvctResult(uint16_t avctRet)
2222 {
2223     HILOGI("avctRet: %{public}d", avctRet);
2224     int result = RET_BAD_STATUS;
2225 
2226     switch (avctRet) {
2227         case AVCT_SUCCESS:
2228             result = BT_SUCCESS;
2229             break;
2230         case AVCT_FAILED:
2231             /// FALL THROUGH
2232         default:
2233             /// Do nothing!
2234             break;
2235     }
2236 
2237     return result;
2238 }
2239 
ExpainPassCrCodeToResult(uint8_t code)2240 int AvrcCtProfile::ExpainPassCrCodeToResult(uint8_t code)
2241 {
2242     HILOGI("code: %{public}d", code);
2243     int result = RET_BAD_STATUS;
2244 
2245     switch (code) {
2246         case AVRC_CT_RSP_CODE_ACCEPTED:
2247         /// FALL THROUGH
2248         case AVRC_CT_RSP_CODE_INTERIM:
2249             result = BT_SUCCESS;
2250             break;
2251         case AVRC_CT_RSP_CODE_NOT_IMPLEMENTED:
2252             result = RET_NO_SUPPORT;
2253             break;
2254         case AVRC_CT_RSP_CODE_REJECTED:
2255         /// FALL THROUGH
2256         default:
2257             /// Do nothing!
2258             break;
2259     }
2260 
2261     return result;
2262 }
2263 
ExplainCrCodeToResult(uint8_t crCode)2264 int AvrcCtProfile::ExplainCrCodeToResult(uint8_t crCode)
2265 {
2266     HILOGI("crCode: %{public}d", crCode);
2267     int result = RET_BAD_STATUS;
2268 
2269     switch (crCode) {
2270         case AVRC_CT_RSP_CODE_ACCEPTED:
2271         case AVRC_CT_RSP_CODE_STABLE:
2272         case AVRC_CT_RSP_CODE_CHANGED:
2273             /// FALL THROUGH
2274         case AVRC_CT_RSP_CODE_INTERIM:
2275             result = BT_SUCCESS;
2276             break;
2277         case AVRC_CT_RSP_CODE_NOT_IMPLEMENTED:
2278             result = RET_NO_SUPPORT;
2279             break;
2280         case AVRC_CT_RSP_CODE_REJECTED:
2281             /// FALL THROUGH
2282         default:
2283             /// Do nothing!
2284             break;
2285     }
2286 
2287     return result;
2288 }
2289 
GetEventName(uint8_t event)2290 std::string AvrcCtProfile::GetEventName(uint8_t event)
2291 {
2292     switch (event) {
2293         case AVCT_CONNECT_IND_EVT:
2294             return "AVCT_CONNECT_IND_EVT";
2295         case AVCT_CONNECT_CFM_EVT:
2296             return "AVCT_CONNECT_CFM_EVT";
2297         case AVCT_DISCONNECT_IND_EVT:
2298             return "AVCT_DISCONNECT_IND_EVT";
2299         case AVCT_DISCONNECT_CFM_EVT:
2300             return "AVCT_DISCONNECT_CFM_EVT";
2301         case AVCT_BR_CONNECT_IND_EVT:
2302             return "AVCT_BR_CONNECT_IND_EVT";
2303         case AVCT_BR_CONNECT_CFM_EVT:
2304             return "AVCT_BR_CONNECT_CFM_EVT";
2305         case AVCT_BR_DISCONNECT_IND_EVT:
2306             return "AVCT_BR_DISCONNECT_IND_EVT";
2307         case AVCT_BR_DISCONNECT_CFM_EVT:
2308             return "AVCT_BR_DISCONNECT_CFM_EVT";
2309         default:
2310             return "Unknown";
2311     }
2312 }
2313 }  // namespace bluetooth
2314 }  // namespace OHOS
2315