1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <algorithm>
16 #include "avrcp_tg_connection.h"
17 #include "avrcp_tg_packet.h"
18 #include "avrcp_tg_pass_through.h"
19 #include "log_util.h"
20 
21 namespace OHOS {
22 namespace bluetooth {
23 AvrcTgConnectManager *AvrcTgConnectManager::g_instance = nullptr;
24 
AvrcTgConnectInfo(const std::string & btAddr,uint8_t connectId,uint8_t role,uint16_t controlMtu,uint16_t browseMtu,uint32_t companyId,uint16_t uidCounter,AvctChannelEventCallback eventCallback,AvctMsgCallback msgCallback)25 AvrcTgConnectInfo::AvrcTgConnectInfo(const std::string &btAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu,
26     uint16_t browseMtu, uint32_t companyId, uint16_t uidCounter, AvctChannelEventCallback eventCallback,
27     AvctMsgCallback msgCallback)
28     : eventCallback_(eventCallback),
29       msgCallback_(msgCallback),
30       connectId_(connectId),
31       role_(role),
32       controlMtu_(controlMtu),
33       browseMtu_(browseMtu),
34       companyId_(companyId),
35       uidCounter_(uidCounter),
36       btAddr_(btAddr)
37 {
38     HILOGI("connectId:%{public}d, role:%{public}d, controlMtu:%{public}d, browseMtu:%{public}d, companyId:%{public}u, "
39         "uidCounter:%{public}d", connectId, role, controlMtu, browseMtu, companyId, uidCounter);
40 
41     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYBACK_STATUS_CHANGED, true));
42     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_CHANGED, true));
43     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_REACHED_END, false));
44     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_REACHED_START, false));
45     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYBACK_POS_CHANGED, false));
46     notes_.insert(std::make_pair(AVRC_EVENT_ID_BATT_STATUS_CHANGED, false));
47     notes_.insert(std::make_pair(AVRC_EVENT_ID_SYSTEM_STATUS_CHANGED, false));
48     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYER_APPLICATION_SETTING_CHANGED, false));
49     notes_.insert(std::make_pair(AVRC_EVENT_ID_NOW_PLAYING_CONTENT_CHANGED, false));
50     notes_.insert(std::make_pair(AVRC_EVENT_ID_AVAILABLE_PLAYERS_CHANGED, false));
51     notes_.insert(std::make_pair(AVRC_EVENT_ID_ADDRESSED_PLAYER_CHANGED, false));
52     notes_.insert(std::make_pair(AVRC_EVENT_ID_UIDS_CHANGED, false));
53     notes_.insert(std::make_pair(AVRC_EVENT_ID_VOLUME_CHANGED, false));
54 
55     ptInfo_ = {nullptr, nullptr};
56     vdInfo_ = {nullptr, nullptr};
57     brInfo_ = {nullptr, nullptr};
58 }
59 
~AvrcTgConnectInfo()60 AvrcTgConnectInfo::~AvrcTgConnectInfo()
61 {
62     HILOGI("enter");
63 
64     if (ptInfo_.timer_ != nullptr) {
65         ptInfo_.timer_->Stop();
66     }
67 
68     if (unInfo_.timer_ != nullptr) {
69         unInfo_.timer_->Stop();
70     }
71 
72     if (vdInfo_.timer_ != nullptr) {
73         vdInfo_.timer_->Stop();
74     }
75 
76     if (brInfo_.timer_ != nullptr) {
77         brInfo_.timer_->Stop();
78     }
79 }
80 
GetInstance(void)81 AvrcTgConnectManager *AvrcTgConnectManager::GetInstance(void)
82 {
83     HILOGI("enter");
84 
85     if (g_instance == nullptr) {
86         g_instance = new (std::nothrow) AvrcTgConnectManager();
87     }
88 
89     return g_instance;
90 }
91 
Add(const RawAddress & rawAddr,uint8_t connectId,uint8_t role,uint16_t controlMtu,uint16_t browseMtu,uint32_t companyId,uint16_t uidCounter,AvctChannelEventCallback eventCallback,AvctMsgCallback msgCallback)92 int AvrcTgConnectManager::Add(const RawAddress &rawAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu,
93     uint16_t browseMtu, uint32_t companyId, uint16_t uidCounter, AvctChannelEventCallback eventCallback,
94     AvctMsgCallback msgCallback)
95 {
96     HILOGI("addr:%{public}s, connectId:%{public}d, role:%{public}d, controlMtu:%{public}d, browseMtu:%{public}d, "
97         "companyId:%{public}u, uidCounter:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), connectId, role, controlMtu,
98         browseMtu, companyId, uidCounter);
99 
100     std::lock_guard<std::mutex> lock(mutex_);
101 
102     int result = BT_SUCCESS;
103 
104     if (GetConnectInfo(rawAddr.GetAddress()) == nullptr) {
105         infos_.insert(std::make_pair(rawAddr.GetAddress(),
106             AvrcTgConnectInfo(rawAddr.GetAddress(),
107                 connectId,
108                 role,
109                 controlMtu,
110                 browseMtu,
111                 companyId,
112                 uidCounter,
113                 eventCallback,
114                 msgCallback)));
115     } else {
116         result = RET_BAD_PARAM;
117         HILOGI("The connection information exists!");
118     }
119 
120     return result;
121 }
122 
Delete(const RawAddress & rawAddr)123 void AvrcTgConnectManager::Delete(const RawAddress &rawAddr)
124 {
125     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
126 
127     std::lock_guard<std::mutex> lock(mutex_);
128 
129     infos_.erase(rawAddr.GetAddress());
130 }
131 
GetConnectInfo(const RawAddress & rawAddr)132 const AvrcTgConnectInfo *AvrcTgConnectManager::GetConnectInfo(const RawAddress &rawAddr)
133 {
134     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
135 
136     const AvrcTgConnectInfo *info = nullptr;
137 
138     auto iter = infos_.find(rawAddr.GetAddress());
139     if (iter != infos_.end()) {
140         info = &iter->second;
141     }
142 
143     return info;
144 }
145 
GetRawAddress(uint8_t connectId) const146 RawAddress AvrcTgConnectManager::GetRawAddress(uint8_t connectId) const
147 {
148     HILOGI("connectId: %{public}d", connectId);
149 
150     std::string addr(AVRC_TG_DEFAULT_BLUETOOTH_ADDRESS);
151 
152     for (auto &info : infos_) {
153         if (info.second.connectId_ == connectId) {
154             addr = info.second.btAddr_;
155             break;
156         }
157     }
158 
159     return RawAddress(addr);
160 }
161 
GetDeviceAddresses(void)162 std::list<std::string> AvrcTgConnectManager::GetDeviceAddresses(void)
163 {
164     HILOGI("enter");
165 
166     std::lock_guard<std::mutex> lock(mutex_);
167 
168     std::list<std::string> addrs;
169     std::transform(infos_.begin(), infos_.end(), std::inserter(addrs, addrs.end()),
170         [](const std::pair<std::string, AvrcTgConnectInfo>& info){return info.second.btAddr_;});
171     return addrs;
172 }
173 
SetActiveDevice(const std::string addr)174 void AvrcTgConnectManager::SetActiveDevice(const std::string addr)
175 {
176     HILOGI("rawAddr: %{public}s", GetEncryptAddr(addr).c_str());
177 
178     std::lock_guard<std::mutex> lock(mutex_);
179 
180     activeAddr_ = addr;
181 }
182 
GetActiveDevice(void)183 const std::string &AvrcTgConnectManager::GetActiveDevice(void)
184 {
185     HILOGI("enter");
186 
187     std::lock_guard<std::mutex> lock(mutex_);
188 
189     HILOGI("rawAddr: %{public}s", GetEncryptAddr(activeAddr_).c_str());
190 
191     return activeAddr_;
192 }
193 
AddDisconnectedDevice(const std::string addr)194 void AvrcTgConnectManager::AddDisconnectedDevice(const std::string addr)
195 {
196     HILOGI("rawAddr: %{public}s", GetEncryptAddr(addr).c_str());
197 
198     std::lock_guard<std::mutex> lock(mutex_);
199 
200     disconnectedDevices_.push_back(addr);
201 }
202 
DeleteDisconnectedDevice(const std::string addr)203 void AvrcTgConnectManager::DeleteDisconnectedDevice(const std::string addr)
204 {
205     HILOGI("rawAddr: %{public}s", GetEncryptAddr(addr).c_str());
206 
207     std::lock_guard<std::mutex> lock(mutex_);
208 
209     for (std::list<std::string>::iterator it = disconnectedDevices_.begin(); it != disconnectedDevices_.end(); it++) {
210         if (it->compare(addr) == 0x00) {
211             it = disconnectedDevices_.erase(it);
212         }
213     }
214 }
215 
GetAllDisconnectedDevices(void)216 const std::list<std::string> &AvrcTgConnectManager::GetAllDisconnectedDevices(void)
217 {
218     HILOGI("enter");
219 
220     std::lock_guard<std::mutex> lock(mutex_);
221 
222     return disconnectedDevices_;
223 }
224 
IsConnectInfoEmpty(void) const225 bool AvrcTgConnectManager::IsConnectInfoEmpty(void) const
226 {
227     HILOGI("enter");
228 
229     return infos_.empty();
230 }
231 
GetConnectId(const RawAddress & rawAddr)232 uint8_t AvrcTgConnectManager::GetConnectId(const RawAddress &rawAddr)
233 {
234     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
235 
236     uint8_t connectId = 0x00;
237 
238     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
239     if (info != nullptr) {
240         connectId = info->connectId_;
241     } else {
242         HILOGI("The connection information does not exist!");
243     }
244 
245     return connectId;
246 }
247 
SetConnectId(const RawAddress & rawAddr,uint8_t connectId)248 void AvrcTgConnectManager::SetConnectId(const RawAddress &rawAddr, uint8_t connectId)
249 {
250     HILOGI("address: %{public}s, connectId: %{public}u", GET_ENCRYPT_AVRCP_ADDR(rawAddr), connectId);
251 
252     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
253     if (info != nullptr) {
254         info->connectId_ = connectId;
255     } else {
256         HILOGI("The connection information does not exist!");
257     }
258 }
259 
GetControlMtu(const RawAddress & rawAddr)260 uint16_t AvrcTgConnectManager::GetControlMtu(const RawAddress &rawAddr)
261 {
262     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
263 
264     uint16_t mtu = AVRC_TG_DEFAULT_CONTROL_MTU_SIZE;
265 
266     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
267     if (info != nullptr) {
268         mtu = info->controlMtu_;
269     } else {
270         HILOGI("The connection information does not exist!");
271     }
272 
273     return mtu;
274 }
275 
SetControlMtu(const RawAddress & rawAddr,uint16_t mtu)276 void AvrcTgConnectManager::SetControlMtu(const RawAddress &rawAddr, uint16_t mtu)
277 {
278     HILOGI("address: %{public}s, mtu: %{public}u", GET_ENCRYPT_AVRCP_ADDR(rawAddr), mtu);
279 
280     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
281     if (info != nullptr) {
282         info->controlMtu_ = mtu;
283     } else {
284         HILOGI("The connection information does not exist!");
285     }
286 }
287 
GetBrowseMtu(const RawAddress & rawAddr)288 uint16_t AvrcTgConnectManager::GetBrowseMtu(const RawAddress &rawAddr)
289 {
290     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
291 
292     uint16_t mtu = AVRC_TG_DEFAULT_BROWSE_MTU_SIZE;
293 
294     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
295     if (info != nullptr) {
296         mtu = info->browseMtu_;
297     } else {
298         HILOGI("The connection information does not exist!");
299     }
300 
301     return mtu;
302 }
303 
SetBrowseMtu(const RawAddress & rawAddr,uint16_t mtu)304 void AvrcTgConnectManager::SetBrowseMtu(const RawAddress &rawAddr, uint16_t mtu)
305 {
306     HILOGI("address: %{public}s, mtu: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), mtu);
307 
308     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
309     if (info != nullptr) {
310         info->browseMtu_ = mtu;
311     } else {
312         HILOGI("The connection information does not exist!");
313     }
314 }
315 
EnableNotifyState(const RawAddress & rawAddr,uint8_t notification)316 void AvrcTgConnectManager::EnableNotifyState(const RawAddress &rawAddr, uint8_t notification)
317 {
318     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
319 
320     std::lock_guard<std::mutex> lock(mutex_);
321 
322     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
323     if (info != nullptr) {
324         auto iter = info->notes_.find(notification);
325         if (iter != info->notes_.end()) {
326             iter->second = true;
327         }
328     } else {
329         HILOGI("The connection information does not exist!");
330     }
331 }
332 
DisableNotifyState(const RawAddress & rawAddr,uint8_t notification)333 void AvrcTgConnectManager::DisableNotifyState(const RawAddress &rawAddr, uint8_t notification)
334 {
335     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
336 
337     std::lock_guard<std::mutex> lock(mutex_);
338 
339     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
340     if (info != nullptr) {
341         auto iter = info->notes_.find(notification);
342         if (iter != info->notes_.end()) {
343             iter->second = false;
344         }
345     } else {
346         HILOGI("The connection information does not exist!");
347     }
348 }
349 
IsNotifyStateEnabled(const RawAddress & rawAddr,uint8_t notification)350 bool AvrcTgConnectManager::IsNotifyStateEnabled(const RawAddress &rawAddr, uint8_t notification)
351 {
352     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
353 
354     std::lock_guard<std::mutex> lock(mutex_);
355 
356     bool result = false;
357 
358     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
359     if (info != nullptr) {
360         auto iter = info->notes_.find(notification);
361         if (iter != info->notes_.end()) {
362             result = iter->second;
363         }
364     } else {
365         HILOGI("enter The connection information does not exist!");
366     }
367 
368     HILOGI("enter result: %{public}d", result);
369 
370     return result;
371 }
372 
GetCompanyId(const RawAddress & rawAddr)373 uint32_t AvrcTgConnectManager::GetCompanyId(const RawAddress &rawAddr)
374 {
375     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
376 
377     uint32_t companyId = AVRC_TG_DEFAULT_BLUETOOTH_SIG_COMPANY_ID;
378 
379     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
380     if (info != nullptr) {
381         companyId = info->companyId_;
382     } else {
383         HILOGI("The connection information does not exist!");
384     }
385 
386     return companyId;
387 }
388 
GetUidCounter(const RawAddress & rawAddr)389 uint16_t AvrcTgConnectManager::GetUidCounter(const RawAddress &rawAddr)
390 {
391     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
392 
393     uint16_t uidCounter = 0x00;
394 
395     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
396     if (info != nullptr) {
397         uidCounter = info->uidCounter_;
398     } else {
399         HILOGI("The connection information does not exist!");
400     }
401 
402     return uidCounter;
403 }
404 
SetUidCounter(const RawAddress & rawAddr,uint16_t uidCounter)405 void AvrcTgConnectManager::SetUidCounter(const RawAddress &rawAddr, uint16_t uidCounter)
406 {
407     HILOGI("address: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter);
408 
409     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
410     if (info != nullptr) {
411         info->uidCounter_ = uidCounter;
412     } else {
413         HILOGI("The connection information does not exist!");
414     }
415 }
416 
417 /******************************************************************
418  * PASS THROUGH COMMAND                                           *
419  ******************************************************************/
420 
GetPassPacket(const RawAddress & rawAddr)421 std::shared_ptr<AvrcTgPassPacket> AvrcTgConnectManager::GetPassPacket(const RawAddress &rawAddr)
422 {
423     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
424 
425     std::shared_ptr<AvrcTgPassPacket> pkt = nullptr;
426 
427     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
428     if (info != nullptr) {
429         pkt = info->ptInfo_.pkt_;
430     } else {
431         HILOGI("The connection information does not exist!");
432     }
433 
434     return pkt;
435 }
436 
SetPassPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcTgPassPacket> & pkt)437 void AvrcTgConnectManager::SetPassPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgPassPacket> &pkt)
438 {
439     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
440 
441     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
442     if (info != nullptr) {
443         info->ptInfo_.pkt_ = pkt;
444     } else {
445         HILOGI("The connection information does not exist!");
446     }
447 }
448 
IsPassTimerEmpty(const RawAddress & rawAddr)449 bool AvrcTgConnectManager::IsPassTimerEmpty(const RawAddress &rawAddr)
450 {
451     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
452 
453     bool result = true;
454 
455     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
456     if (info != nullptr && info->ptInfo_.timer_ != nullptr) {
457         result = false;
458     } else {
459         HILOGI("The PASS THROUGH timer is empty!");
460     }
461 
462     return result;
463 }
464 
SetPassTimer(const RawAddress & rawAddr,std::function<void ()> callback,int ms,bool isPeriodic)465 void AvrcTgConnectManager::SetPassTimer(
466     const RawAddress &rawAddr, std::function<void()> callback, int ms, bool isPeriodic)
467 {
468     HILOGI("addr:%{public}s, ms:%{public}d, isPeriodic:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), ms, isPeriodic);
469 
470     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
471     if (info != nullptr) {
472         info->ptInfo_.timer_ = std::make_shared<utility::Timer>(callback);
473         info->ptInfo_.timer_->Start(ms, isPeriodic);
474     } else {
475         HILOGI("The connection information does not exist!");
476     }
477 }
478 
ClearPassPacket(const RawAddress & rawAddr)479 void AvrcTgConnectManager::ClearPassPacket(const RawAddress &rawAddr)
480 {
481     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
482 
483     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
484     if (info != nullptr) {
485         info->ptInfo_.pkt_ = nullptr;
486     } else {
487         HILOGI("The connection information does not exist!");
488     }
489 }
490 
ClearPassTimer(const RawAddress & rawAddr)491 void AvrcTgConnectManager::ClearPassTimer(const RawAddress &rawAddr)
492 {
493     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
494 
495     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
496     if (info != nullptr) {
497         if (info->ptInfo_.timer_ != nullptr) {
498             info->ptInfo_.timer_->Stop();
499             info->ptInfo_.timer_ = nullptr;
500         }
501     } else {
502         HILOGI("The connection information does not exist!");
503     }
504 }
505 
ClearPassInfo(const RawAddress & rawAddr)506 void AvrcTgConnectManager::ClearPassInfo(const RawAddress &rawAddr)
507 {
508     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
509 
510     ClearPassTimer(rawAddr);
511     ClearPassPacket(rawAddr);
512 }
513 
514 /******************************************************************
515  * UNIT INFO / SUB UNIT INFO COMMAND                              *
516  ******************************************************************/
517 
GetUnitPacket(const RawAddress & rawAddr)518 std::shared_ptr<AvrcTgUnitPacket> AvrcTgConnectManager::GetUnitPacket(const RawAddress &rawAddr)
519 {
520     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
521 
522     std::shared_ptr<AvrcTgUnitPacket> pkt = nullptr;
523 
524     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
525     if (info != nullptr) {
526         pkt = info->unInfo_.pkt_;
527     } else {
528         HILOGI("The connection information does not exist!");
529     }
530 
531     return pkt;
532 }
533 
SetUnitPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcTgUnitPacket> & pkt)534 void AvrcTgConnectManager::SetUnitPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgUnitPacket> &pkt)
535 {
536     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
537 
538     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
539     if (info != nullptr) {
540         info->unInfo_.pkt_ = pkt;
541     } else {
542         HILOGI("The connection information does not exist!");
543     }
544 }
545 
ClearUnitPacket(const RawAddress & rawAddr)546 void AvrcTgConnectManager::ClearUnitPacket(const RawAddress &rawAddr)
547 {
548     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
549 
550     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
551     if (info != nullptr) {
552         info->unInfo_.pkt_ = nullptr;
553     } else {
554         HILOGI("The connection information does not exist!");
555     }
556 }
557 
ClearUnitInfo(const RawAddress & rawAddr)558 void AvrcTgConnectManager::ClearUnitInfo(const RawAddress &rawAddr)
559 {
560     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
561 
562     ClearUnitPacket(rawAddr);
563 }
564 
565 /******************************************************************
566  * VENDOR DEPENDENT COMMAND                                       *
567  ******************************************************************/
568 
GetVendorPacket(const RawAddress & rawAddr)569 std::shared_ptr<AvrcTgVendorPacket> AvrcTgConnectManager::GetVendorPacket(const RawAddress &rawAddr)
570 {
571     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
572 
573     std::shared_ptr<AvrcTgVendorPacket> pkt = nullptr;
574 
575     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
576     if (info != nullptr) {
577         pkt = info->vdInfo_.pkt_;
578     } else {
579         HILOGI("The connection information does not exist!");
580     }
581 
582     return pkt;
583 }
584 
SetVendorPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcTgVendorPacket> & pkt)585 void AvrcTgConnectManager::SetVendorPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgVendorPacket> &pkt)
586 {
587     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
588 
589     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
590     if (info != nullptr) {
591         info->vdInfo_.pkt_ = pkt;
592     } else {
593         HILOGI("The connection information does not exist!");
594     }
595 }
596 
ClearVendorPacket(const RawAddress & rawAddr)597 void AvrcTgConnectManager::ClearVendorPacket(const RawAddress &rawAddr)
598 {
599     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
600 
601     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
602     if (info != nullptr) {
603         info->vdInfo_.pkt_ = nullptr;
604     } else {
605         HILOGI("The connection information does not exist!");
606     }
607 }
608 
ClearVendorInfo(const RawAddress & rawAddr)609 void AvrcTgConnectManager::ClearVendorInfo(const RawAddress &rawAddr)
610 {
611     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
612 
613     ClearVendorPacket(rawAddr);
614 }
615 
616 /******************************************************************
617  * BROWSING COMMAND                                               *
618  ******************************************************************/
619 
GetBrowsePacket(const RawAddress & rawAddr)620 std::shared_ptr<AvrcTgBrowsePacket> AvrcTgConnectManager::GetBrowsePacket(const RawAddress &rawAddr)
621 {
622     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
623 
624     std::shared_ptr<AvrcTgBrowsePacket> pkt = nullptr;
625 
626     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
627     if (info != nullptr) {
628         pkt = info->brInfo_.pkt_;
629     } else {
630         HILOGI("The connection information does not exist!");
631     }
632 
633     return pkt;
634 }
635 
SetBrowsePacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcTgBrowsePacket> & pkt)636 void AvrcTgConnectManager::SetBrowsePacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcTgBrowsePacket> &pkt)
637 {
638     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
639 
640     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
641     if (info != nullptr) {
642         info->brInfo_.pkt_ = pkt;
643     } else {
644         HILOGI("The connection information does not exist!");
645     }
646 }
647 
ClearBrowsePacket(const RawAddress & rawAddr)648 void AvrcTgConnectManager::ClearBrowsePacket(const RawAddress &rawAddr)
649 {
650     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
651 
652     AvrcTgConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
653     if (info != nullptr) {
654         info->brInfo_.pkt_ = nullptr;
655     } else {
656         HILOGI("The connection information does not exist!");
657     }
658 }
659 
ClearBrowseInfo(const RawAddress & rawAddr)660 void AvrcTgConnectManager::ClearBrowseInfo(const RawAddress &rawAddr)
661 {
662     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
663 
664     ClearBrowsePacket(rawAddr);
665 }
666 
~AvrcTgConnectManager()667 AvrcTgConnectManager::~AvrcTgConnectManager()
668 {
669     HILOGI("enter");
670 
671     infos_.clear();
672 
673     g_instance = nullptr;
674 }
675 
GetConnectInfo(const std::string & btAddr)676 AvrcTgConnectInfo *AvrcTgConnectManager::GetConnectInfo(const std::string &btAddr)
677 {
678     HILOGI("enter");
679 
680     AvrcTgConnectInfo *info = nullptr;
681 
682     auto iter = infos_.find(btAddr);
683     if (iter != infos_.end()) {
684         info = &iter->second;
685     }
686 
687     return info;
688 }
689 }  // namespace bluetooth
690 }  // namespace OHOS
691