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 
16 #include "avrcp_ct_connection.h"
17 #include "avrcp_ct_packet.h"
18 #include "avrcp_ct_pass_through.h"
19 
20 namespace OHOS {
21 namespace bluetooth {
22 AvrcCtConnectManager *AvrcCtConnectManager::g_instance = nullptr;
23 
AvrcCtConnectInfo(const std::string & btAddr,uint8_t connectId,uint8_t role,uint16_t controlMtu,uint16_t browseMtu,uint32_t companyId,uint16_t uidCounter,bool absVolume,AvctChannelEventCallback eventCallback,AvctMsgCallback msgCallback)24 AvrcCtConnectInfo::AvrcCtConnectInfo(const std::string &btAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu,
25     uint16_t browseMtu, uint32_t companyId, uint16_t uidCounter, bool absVolume, AvctChannelEventCallback eventCallback,
26     AvctMsgCallback msgCallback)
27     : eventCallback_(eventCallback),
28       msgCallback_(msgCallback),
29       connectId_(connectId),
30       role_(role),
31       controlMtu_(controlMtu),
32       browseMtu_(browseMtu),
33       companyId_(companyId),
34       uidCounter_(uidCounter),
35       absVolume_(absVolume),
36       btAddr_(btAddr)
37 {
38     HILOGI("enter");
39 
40     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYBACK_STATUS_CHANGED, true));
41     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_CHANGED, true));
42     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_REACHED_END, true));
43     notes_.insert(std::make_pair(AVRC_EVENT_ID_TRACK_REACHED_START, true));
44     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYBACK_POS_CHANGED, true));
45     notes_.insert(std::make_pair(AVRC_EVENT_ID_BATT_STATUS_CHANGED, false));
46     notes_.insert(std::make_pair(AVRC_EVENT_ID_SYSTEM_STATUS_CHANGED, false));
47     notes_.insert(std::make_pair(AVRC_EVENT_ID_PLAYER_APPLICATION_SETTING_CHANGED, true));
48     notes_.insert(std::make_pair(AVRC_EVENT_ID_NOW_PLAYING_CONTENT_CHANGED, true));
49     notes_.insert(std::make_pair(AVRC_EVENT_ID_AVAILABLE_PLAYERS_CHANGED, true));
50     notes_.insert(std::make_pair(AVRC_EVENT_ID_ADDRESSED_PLAYER_CHANGED, true));
51     notes_.insert(std::make_pair(AVRC_EVENT_ID_UIDS_CHANGED, true));
52     notes_.insert(std::make_pair(AVRC_EVENT_ID_VOLUME_CHANGED, true));
53 
54     ptInfo_ = {nullptr, nullptr};
55     vdInfo_ = {nullptr, nullptr};
56     brInfo_ = {nullptr, nullptr};
57 
58     brConnected_ = false;
59 }
60 
~AvrcCtConnectInfo()61 AvrcCtConnectInfo::~AvrcCtConnectInfo()
62 {
63     HILOGI("enter");
64 
65     if (ptInfo_.timer_ != nullptr) {
66         ptInfo_.timer_->Stop();
67     }
68 
69     while (!ptInfo_.cmdQue_.empty()) {
70         ptInfo_.cmdQue_.pop();
71     }
72 
73     if (unInfo_.timer_ != nullptr) {
74         unInfo_.timer_->Stop();
75     }
76 
77     while (!unInfo_.cmdQue_.empty()) {
78         unInfo_.cmdQue_.pop();
79     }
80 
81     if (vdInfo_.timer_ != nullptr) {
82         vdInfo_.timer_->Stop();
83     }
84 
85     while (!vdInfo_.cmdQue_.empty()) {
86         vdInfo_.cmdQue_.pop();
87     }
88 
89     if (brInfo_.timer_ != nullptr) {
90         brInfo_.timer_->Stop();
91     }
92 
93     while (!brInfo_.cmdQue_.empty()) {
94         brInfo_.cmdQue_.pop();
95     }
96 }
97 
GetInstance(void)98 AvrcCtConnectManager *AvrcCtConnectManager::GetInstance(void)
99 {
100     HILOGI("enter");
101 
102     if (g_instance == nullptr) {
103         g_instance = new (std::nothrow) AvrcCtConnectManager();
104     }
105 
106     return g_instance;
107 }
108 
Add(const RawAddress & rawAddr,uint8_t connectId,uint8_t role,uint16_t controlMtu,uint16_t browseMtu,uint32_t companyId,uint16_t uidCounter,bool absVolume,AvctChannelEventCallback eventCallback,AvctMsgCallback msgCallback)109 int AvrcCtConnectManager::Add(const RawAddress &rawAddr, uint8_t connectId, uint8_t role, uint16_t controlMtu,
110     uint16_t browseMtu, uint32_t companyId, uint16_t uidCounter, bool absVolume, AvctChannelEventCallback eventCallback,
111     AvctMsgCallback msgCallback)
112 {
113     HILOGI("address: %{public}s, connectId: %{public}d, role: %{public}d, controlMtu: %{public}hu, browseMtu: "
114         "%{public}hu, companyId: %{public}u, uidCounter: %{public}hu, absVolume: %{public}d",
115         GET_ENCRYPT_AVRCP_ADDR(rawAddr),
116         connectId,
117         role,
118         controlMtu,
119         browseMtu,
120         companyId,
121         uidCounter,
122         absVolume);
123 
124     std::lock_guard<std::recursive_mutex> lock(mutex_);
125 
126     int result = BT_SUCCESS;
127 
128     if (GetConnectInfo(rawAddr.GetAddress()) == nullptr) {
129         infos_.insert(std::make_pair(rawAddr.GetAddress(),
130             AvrcCtConnectInfo(rawAddr.GetAddress(),
131                 connectId,
132                 role,
133                 controlMtu,
134                 browseMtu,
135                 companyId,
136                 uidCounter,
137                 absVolume,
138                 eventCallback,
139                 msgCallback)));
140     } else {
141         result = RET_BAD_PARAM;
142         HILOGI("The connection information exists!");
143     }
144 
145     return result;
146 }
147 
Delete(const RawAddress & rawAddr)148 void AvrcCtConnectManager::Delete(const RawAddress &rawAddr)
149 {
150     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
151 
152     std::lock_guard<std::recursive_mutex> lock(mutex_);
153 
154     infos_.erase(rawAddr.GetAddress());
155 }
156 
GetConnectInfo(const RawAddress & rawAddr) const157 const AvrcCtConnectInfo *AvrcCtConnectManager::GetConnectInfo(const RawAddress &rawAddr) const
158 {
159     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
160 
161     const AvrcCtConnectInfo *info = nullptr;
162 
163     auto iter = infos_.find(rawAddr.GetAddress());
164     if (iter != infos_.end()) {
165         info = &iter->second;
166     }
167 
168     return info;
169 }
170 
GetRawAddress(uint8_t connectId)171 RawAddress AvrcCtConnectManager::GetRawAddress(uint8_t connectId)
172 {
173     HILOGI("connectId: %{public}d", connectId);
174     std::lock_guard<std::recursive_mutex> lock(mutex_);
175     std::string addr(AVRC_CT_DEFAULT_BLUETOOTH_ADDRESS);
176 
177     for (auto &info : infos_) {
178         if (info.second.connectId_ == connectId) {
179             addr = info.second.btAddr_;
180             break;
181         }
182     }
183 
184     return RawAddress(addr);
185 }
186 
GetDeviceAddresses(void)187 std::list<std::string> AvrcCtConnectManager::GetDeviceAddresses(void)
188 {
189     HILOGI("enter");
190 
191     std::lock_guard<std::recursive_mutex> lock(mutex_);
192 
193     std::list<std::string> addrs;
194 
195     std::transform(infos_.begin(), infos_.end(), std::inserter(addrs,addrs.end()),
196         [](const std::pair<std::string, AvrcCtConnectInfo> &info){return info.second.btAddr_;});
197 
198     return addrs;
199 }
200 
AddDisconnectedDevice(const std::string & addr)201 void AvrcCtConnectManager::AddDisconnectedDevice(const std::string &addr)
202 {
203     HILOGI("addr: %{public}s", GetEncryptAddr(addr).c_str());
204 
205     std::lock_guard<std::recursive_mutex> lock(mutex_);
206 
207     disconnectedDevices_.push_back(addr);
208 }
209 
DeleteDisconnectedDevice(const std::string & addr)210 void AvrcCtConnectManager::DeleteDisconnectedDevice(const std::string &addr)
211 {
212     HILOGI("addr: %{public}s", GetEncryptAddr(addr).c_str());
213 
214     std::lock_guard<std::recursive_mutex> lock(mutex_);
215 
216     for (std::list<std::string>::iterator it = disconnectedDevices_.begin(); it != disconnectedDevices_.end(); ++it) {
217         if (it->compare(addr) == 0x00) {
218             it = disconnectedDevices_.erase(it);
219         }
220     }
221 }
222 
GetAllDisconnectedDevices(void)223 const std::list<std::string> &AvrcCtConnectManager::GetAllDisconnectedDevices(void)
224 {
225     HILOGI("enter");
226 
227     std::lock_guard<std::recursive_mutex> lock(mutex_);
228 
229     return disconnectedDevices_;
230 }
231 
IsConnectInfoEmpty(void)232 bool AvrcCtConnectManager::IsConnectInfoEmpty(void)
233 {
234     HILOGI("enter");
235     std::lock_guard<std::recursive_mutex> lock(mutex_);
236     return infos_.empty();
237 }
238 
GetConnectId(const RawAddress & rawAddr)239 uint8_t AvrcCtConnectManager::GetConnectId(const RawAddress &rawAddr)
240 {
241     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
242     std::lock_guard<std::recursive_mutex> lock(mutex_);
243     uint8_t connectId = 0x00;
244 
245     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
246     if (info != nullptr) {
247         connectId = info->connectId_;
248     } else {
249         HILOGI("The connection information does not exist!");
250     }
251 
252     return connectId;
253 }
254 
SetConnectId(const RawAddress & rawAddr,uint8_t connectId)255 void AvrcCtConnectManager::SetConnectId(const RawAddress &rawAddr, uint8_t connectId)
256 {
257     HILOGI("address: %{public}s, connectId: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), connectId);
258     std::lock_guard<std::recursive_mutex> lock(mutex_);
259     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
260     if (info != nullptr) {
261         info->connectId_ = connectId;
262     } else {
263         HILOGI("The connection information does not exist!");
264     }
265 }
266 
EnableNotifyState(const RawAddress & rawAddr,uint8_t notification)267 void AvrcCtConnectManager::EnableNotifyState(const RawAddress &rawAddr, uint8_t notification)
268 {
269     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
270 
271     std::lock_guard<std::recursive_mutex> lock(mutex_);
272 
273     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
274     if (info != nullptr) {
275         auto iter = info->notes_.find(notification);
276         if (iter != info->notes_.end()) {
277             iter->second = true;
278         }
279     } else {
280         HILOGI("The connection information does not exist!");
281     }
282 }
283 
DisableNotifyState(const RawAddress & rawAddr,uint8_t notification)284 void AvrcCtConnectManager::DisableNotifyState(const RawAddress &rawAddr, uint8_t notification)
285 {
286     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
287 
288     std::lock_guard<std::recursive_mutex> lock(mutex_);
289 
290     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
291     if (info != nullptr) {
292         auto iter = info->notes_.find(notification);
293         if (iter != info->notes_.end()) {
294             iter->second = false;
295         }
296     } else {
297         HILOGI("The connection information does not exist!");
298     }
299 }
300 
IsNotifyStateEnabled(const RawAddress & rawAddr,uint8_t notification)301 bool AvrcCtConnectManager::IsNotifyStateEnabled(const RawAddress &rawAddr, uint8_t notification)
302 {
303     HILOGI("address: %{public}s, notification: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), notification);
304 
305     std::lock_guard<std::recursive_mutex> lock(mutex_);
306 
307     bool result = false;
308 
309     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
310     if (info != nullptr) {
311         auto iter = info->notes_.find(notification);
312         if (iter != info->notes_.end()) {
313             result = iter->second;
314         }
315     } else {
316         HILOGI("The connection information does not exist!");
317     }
318 
319     HILOGI("result[%{public}d]", result);
320 
321     return result;
322 }
323 
DisableExcludeEvents(const RawAddress & rawAddr,std::vector<uint8_t> events)324 void AvrcCtConnectManager::DisableExcludeEvents(const RawAddress &rawAddr, std::vector<uint8_t> events)
325 {
326     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
327     std::lock_guard<std::recursive_mutex> lock(mutex_);
328     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
329     if (info == nullptr) {
330         HILOGI("The connection information does not exist!");
331         return;
332     }
333     for (auto pos = info->notes_.begin(); pos != info->notes_.end(); ) {
334         for (auto event : events) {
335             if (pos->first != event) {
336                 pos->second = false;
337                 break;
338             }
339         }
340         pos++;
341     }
342 }
343 
GetCompanyId(const RawAddress & rawAddr)344 uint32_t AvrcCtConnectManager::GetCompanyId(const RawAddress &rawAddr)
345 {
346     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
347     std::lock_guard<std::recursive_mutex> lock(mutex_);
348     uint32_t companyId = AVRC_CT_DEFAULT_BLUETOOTH_SIG_COMPANY_ID;
349 
350     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
351     if (info != nullptr) {
352         companyId = info->companyId_;
353     } else {
354         HILOGI("The connection information does not exist!");
355     }
356 
357     return companyId;
358 }
359 
GetUidCounter(const RawAddress & rawAddr)360 uint16_t AvrcCtConnectManager::GetUidCounter(const RawAddress &rawAddr)
361 {
362     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
363     std::lock_guard<std::recursive_mutex> lock(mutex_);
364     uint16_t uidCounter = 0x00;
365 
366     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
367     if (info != nullptr) {
368         uidCounter = info->uidCounter_;
369     } else {
370         HILOGI("The connection information does not exist!");
371     }
372 
373     return uidCounter;
374 }
375 
SetUidCounter(const RawAddress & rawAddr,uint16_t uidCounter)376 void AvrcCtConnectManager::SetUidCounter(const RawAddress &rawAddr, uint16_t uidCounter)
377 {
378     HILOGI("address: %{public}s, uidCounter: %{public}hu", GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter);
379 
380     std::lock_guard<std::recursive_mutex> lock(mutex_);
381 
382     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
383     if (info != nullptr) {
384         info->uidCounter_ = uidCounter;
385     } else {
386         HILOGI("The connection information does not exist!");
387     }
388 }
389 
390 /******************************************************************
391  * PASS THROUGH COMMAND                                           *
392  ******************************************************************/
393 
GetPassPacket(const RawAddress & rawAddr)394 std::shared_ptr<AvrcCtPassPacket> AvrcCtConnectManager::GetPassPacket(const RawAddress &rawAddr)
395 {
396     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
397     std::lock_guard<std::recursive_mutex> lock(mutex_);
398     std::shared_ptr<AvrcCtPassPacket> pkt = nullptr;
399 
400     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
401     if (info != nullptr) {
402         pkt = info->ptInfo_.pkt_;
403     } else {
404         HILOGI("The connection information does not exist!");
405     }
406 
407     return pkt;
408 }
409 
SetPassPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtPassPacket> & pkt)410 void AvrcCtConnectManager::SetPassPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtPassPacket> &pkt)
411 {
412     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
413     std::lock_guard<std::recursive_mutex> lock(mutex_);
414     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
415     if (info != nullptr) {
416         info->ptInfo_.pkt_ = pkt;
417     } else {
418         HILOGI("The connection information does not exist!");
419     }
420 }
421 
IsPassPacketEmpty(const RawAddress & rawAddr)422 bool AvrcCtConnectManager::IsPassPacketEmpty(const RawAddress &rawAddr)
423 {
424     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
425     std::lock_guard<std::recursive_mutex> lock(mutex_);
426     bool result = true;
427 
428     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
429     if (info != nullptr && info->ptInfo_.pkt_ != nullptr) {
430         result = false;
431     } else {
432         HILOGI("The PASS THROUGH packet is empty!");
433     }
434 
435     return result;
436 }
437 
SetPassTimer(const RawAddress & rawAddr,const std::function<void ()> callback,int ms,bool isPeriodic)438 void AvrcCtConnectManager::SetPassTimer(
439     const RawAddress &rawAddr, const std::function<void()> callback, int ms, bool isPeriodic)
440 {
441     HILOGI("addr:%{public}s, ms:%{public}d, isPeriodic:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), ms, isPeriodic);
442     std::lock_guard<std::recursive_mutex> lock(mutex_);
443 
444     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
445     if (info != nullptr) {
446         info->ptInfo_.timer_ = std::make_shared<utility::Timer>(callback);
447         info->ptInfo_.timer_->Start(ms, isPeriodic);
448     } else {
449         HILOGI("The connection information does not exist!");
450     }
451 }
452 
ClearPassPacket(const RawAddress & rawAddr)453 void AvrcCtConnectManager::ClearPassPacket(const RawAddress &rawAddr)
454 {
455     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
456     std::lock_guard<std::recursive_mutex> lock(mutex_);
457 
458     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
459     if (info != nullptr) {
460         info->ptInfo_.pkt_ = nullptr;
461     } else {
462         HILOGI("The connection information does not exist!");
463     }
464 }
465 
ClearPassTimer(const RawAddress & rawAddr)466 void AvrcCtConnectManager::ClearPassTimer(const RawAddress &rawAddr)
467 {
468     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
469     std::lock_guard<std::recursive_mutex> lock(mutex_);
470 
471     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
472     if (info != nullptr) {
473         if (info->ptInfo_.timer_ != nullptr) {
474             info->ptInfo_.timer_->Stop();
475             info->ptInfo_.timer_ = nullptr;
476         }
477     } else {
478         HILOGI("The connection information does not exist!");
479     }
480 }
481 
ClearPassInfo(const RawAddress & rawAddr)482 void AvrcCtConnectManager::ClearPassInfo(const RawAddress &rawAddr)
483 {
484     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
485 
486     ClearPassTimer(rawAddr);
487     ClearPassPacket(rawAddr);
488 }
489 
PushPassQueue(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtPassPacket> & pkt)490 void AvrcCtConnectManager::PushPassQueue(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtPassPacket> &pkt)
491 {
492     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
493 
494     std::lock_guard<std::recursive_mutex> lock(mutex_);
495 
496     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
497     if (info != nullptr) {
498         info->ptInfo_.cmdQue_.push(pkt);
499     } else {
500         HILOGI("The connection information does not exist!");
501     }
502 }
503 
PopPassQueue(const RawAddress & rawAddr)504 std::shared_ptr<AvrcCtPassPacket> AvrcCtConnectManager::PopPassQueue(const RawAddress &rawAddr)
505 {
506     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
507 
508     std::lock_guard<std::recursive_mutex> lock(mutex_);
509 
510     std::shared_ptr<AvrcCtPassPacket> pkt = nullptr;
511 
512     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
513     if (info != nullptr) {
514         pkt = info->ptInfo_.cmdQue_.front();
515         info->ptInfo_.cmdQue_.pop();
516     } else {
517         HILOGI("The connection information does not exist!");
518     }
519 
520     return pkt;
521 }
522 
GetSizeOfPassQueue(const RawAddress & rawAddr)523 uint8_t AvrcCtConnectManager::GetSizeOfPassQueue(const RawAddress &rawAddr)
524 {
525     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
526     uint8_t result = 0x00;
527 
528     std::lock_guard<std::recursive_mutex> lock(mutex_);
529 
530     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
531     if (info != nullptr) {
532         result = info->ptInfo_.cmdQue_.size();
533     } else {
534         HILOGI("The connection information does not exist!");
535     }
536 
537     return result;
538 }
539 
540 /******************************************************************
541  * UNIT INFO / SUB UNIT INFO COMMAND                              *
542  ******************************************************************/
543 
GetUnitPacket(const RawAddress & rawAddr)544 std::shared_ptr<AvrcCtUnitPacket> AvrcCtConnectManager::GetUnitPacket(const RawAddress &rawAddr)
545 {
546     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
547     std::lock_guard<std::recursive_mutex> lock(mutex_);
548     std::shared_ptr<AvrcCtUnitPacket> pkt = nullptr;
549 
550     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
551     if (info != nullptr) {
552         pkt = info->unInfo_.pkt_;
553     } else {
554         HILOGI("The connection information does not exist!");
555     }
556 
557     return pkt;
558 }
559 
SetUnitPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtUnitPacket> & pkt)560 void AvrcCtConnectManager::SetUnitPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtUnitPacket> &pkt)
561 {
562     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
563     std::lock_guard<std::recursive_mutex> lock(mutex_);
564 
565     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
566     if (info != nullptr) {
567         info->unInfo_.pkt_ = pkt;
568     } else {
569         HILOGI("The connection information does not exist!");
570     }
571 }
572 
SetUnitTimer(const RawAddress & rawAddr,std::function<void ()> callback,int ms,bool isPeriodic)573 void AvrcCtConnectManager::SetUnitTimer(
574     const RawAddress &rawAddr, std::function<void()> callback, int ms, bool isPeriodic)
575 {
576     HILOGI("addr:%{public}s, ms:%{public}d, isPeriodic:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), ms, isPeriodic);
577     std::lock_guard<std::recursive_mutex> lock(mutex_);
578 
579     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
580     if (info != nullptr) {
581         info->unInfo_.timer_ = std::make_shared<utility::Timer>(callback);
582         info->unInfo_.timer_->Start(ms, isPeriodic);
583     } else {
584         HILOGI("The connection information does not exist!");
585     }
586 }
587 
ClearUnitPacket(const RawAddress & rawAddr)588 void AvrcCtConnectManager::ClearUnitPacket(const RawAddress &rawAddr)
589 {
590     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
591     std::lock_guard<std::recursive_mutex> lock(mutex_);
592 
593     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
594     if (info != nullptr) {
595         info->unInfo_.pkt_ = nullptr;
596     } else {
597         HILOGI("The connection information does not exist!");
598     }
599 }
600 
ClearUnitTimer(const RawAddress & rawAddr)601 void AvrcCtConnectManager::ClearUnitTimer(const RawAddress &rawAddr)
602 {
603     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
604     std::lock_guard<std::recursive_mutex> lock(mutex_);
605 
606     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
607     if (info != nullptr) {
608         if (info->unInfo_.timer_ != nullptr) {
609             info->unInfo_.timer_->Stop();
610             info->unInfo_.timer_ = nullptr;
611         }
612     } else {
613         HILOGI("The connection information does not exist!");
614     }
615 }
616 
ClearUnitInfo(const RawAddress & rawAddr)617 void AvrcCtConnectManager::ClearUnitInfo(const RawAddress &rawAddr)
618 {
619     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
620 
621     ClearUnitTimer(rawAddr);
622     ClearUnitPacket(rawAddr);
623 }
624 
PushUnitQueue(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtUnitPacket> & pkt)625 void AvrcCtConnectManager::PushUnitQueue(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtUnitPacket> &pkt)
626 {
627     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
628     std::lock_guard<std::recursive_mutex> lock(mutex_);
629 
630     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
631     if (info != nullptr) {
632         info->unInfo_.cmdQue_.push(pkt);
633     } else {
634         HILOGI("The connection information does not exist!");
635     }
636 }
637 
PopUnitQueue(const RawAddress & rawAddr)638 std::shared_ptr<AvrcCtUnitPacket> AvrcCtConnectManager::PopUnitQueue(const RawAddress &rawAddr)
639 {
640     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
641     std::lock_guard<std::recursive_mutex> lock(mutex_);
642     std::shared_ptr<AvrcCtUnitPacket> pkt = nullptr;
643 
644     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
645     if (info != nullptr) {
646         pkt = info->unInfo_.cmdQue_.front();
647         info->unInfo_.cmdQue_.pop();
648     } else {
649         HILOGI("The connection information does not exist!");
650     }
651 
652     return pkt;
653 }
654 
GetSizeOfUnitQueue(const RawAddress & rawAddr)655 uint8_t AvrcCtConnectManager::GetSizeOfUnitQueue(const RawAddress &rawAddr)
656 {
657     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
658     std::lock_guard<std::recursive_mutex> lock(mutex_);
659     uint8_t result = 0x00;
660 
661     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
662     if (info != nullptr) {
663         result = info->unInfo_.cmdQue_.size();
664     } else {
665         HILOGI("The connection information does not exist!");
666     }
667 
668     return result;
669 }
670 
671 /******************************************************************
672  * VENDOR DEPENDENT COMMAND                                       *
673  ******************************************************************/
674 
GetVendorPacket(const RawAddress & rawAddr)675 std::shared_ptr<AvrcCtVendorPacket> AvrcCtConnectManager::GetVendorPacket(const RawAddress &rawAddr)
676 {
677     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
678     std::lock_guard<std::recursive_mutex> lock(mutex_);
679     std::shared_ptr<AvrcCtVendorPacket> pkt = nullptr;
680 
681     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
682     if (info != nullptr) {
683         pkt = info->vdInfo_.pkt_;
684     } else {
685         HILOGI("The connection information does not exist!");
686     }
687 
688     return pkt;
689 }
690 
GetVendorContinuePacket(const RawAddress & rawAddr)691 std::shared_ptr<AvrcCtVendorPacket> AvrcCtConnectManager::GetVendorContinuePacket(const RawAddress &rawAddr)
692 {
693     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
694     std::lock_guard<std::recursive_mutex> lock(mutex_);
695     std::shared_ptr<AvrcCtVendorPacket> pkt = nullptr;
696 
697     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
698     if (info != nullptr) {
699         pkt = info->vdInfo_.continuePkt_;
700     } else {
701         HILOGI("The connection information does not exist!");
702     }
703 
704     return pkt;
705 }
706 
SetVendorPacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & pkt)707 void AvrcCtConnectManager::SetVendorPacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &pkt)
708 {
709     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
710     std::lock_guard<std::recursive_mutex> lock(mutex_);
711 
712     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
713     if (info != nullptr) {
714         info->vdInfo_.pkt_ = pkt;
715     } else {
716         HILOGI("The connection information does not exist!");
717     }
718 }
719 
SetVendorContinuePacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & pkt)720 void AvrcCtConnectManager::SetVendorContinuePacket(const RawAddress &rawAddr,
721     const std::shared_ptr<AvrcCtVendorPacket> &pkt)
722 {
723     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
724     std::lock_guard<std::recursive_mutex> lock(mutex_);
725 
726     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
727     if (info != nullptr) {
728         info->vdInfo_.continuePkt_ = pkt;
729     } else {
730         HILOGI("The connection information does not exist!");
731     }
732 }
733 
SetVendorTimer(const RawAddress & rawAddr,std::function<void ()> callback,int ms,bool isPeriodic)734 void AvrcCtConnectManager::SetVendorTimer(
735     const RawAddress &rawAddr, std::function<void()> callback, int ms, bool isPeriodic)
736 {
737     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
738     std::lock_guard<std::recursive_mutex> lock(mutex_);
739 
740     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
741     if (info != nullptr) {
742         info->vdInfo_.timer_ = std::make_shared<utility::Timer>(callback);
743         info->vdInfo_.timer_->Start(ms, isPeriodic);
744     } else {
745         HILOGI("The connection information does not exist!");
746     }
747 }
748 
ClearVendorPacket(const RawAddress & rawAddr)749 void AvrcCtConnectManager::ClearVendorPacket(const RawAddress &rawAddr)
750 {
751     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
752     std::lock_guard<std::recursive_mutex> lock(mutex_);
753 
754     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
755     if (info != nullptr) {
756         info->vdInfo_.pkt_ = nullptr;
757     } else {
758         HILOGI("The connection information does not exist!");
759     }
760 }
761 
ClearVendorContinuePacket(const RawAddress & rawAddr)762 void AvrcCtConnectManager::ClearVendorContinuePacket(const RawAddress &rawAddr)
763 {
764     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
765     std::lock_guard<std::recursive_mutex> lock(mutex_);
766 
767     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
768     if (info != nullptr) {
769         info->vdInfo_.continuePkt_ = nullptr;
770     } else {
771         HILOGI("The connection information does not exist!");
772     }
773 }
774 
ClearVendorTimer(const RawAddress & rawAddr)775 void AvrcCtConnectManager::ClearVendorTimer(const RawAddress &rawAddr)
776 {
777     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
778     std::lock_guard<std::recursive_mutex> lock(mutex_);
779 
780     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
781     if (info != nullptr) {
782         if (info->vdInfo_.timer_ != nullptr) {
783             info->vdInfo_.timer_->Stop();
784             info->vdInfo_.timer_ = nullptr;
785         }
786     } else {
787         HILOGI("The connection information does not exist!");
788     }
789 }
790 
ClearVendorInfo(const RawAddress & rawAddr)791 void AvrcCtConnectManager::ClearVendorInfo(const RawAddress &rawAddr)
792 {
793     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
794 
795     ClearVendorTimer(rawAddr);
796     ClearVendorPacket(rawAddr);
797     ClearVendorContinuePacket(rawAddr);
798 }
799 
PushVendorQueue(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtVendorPacket> & pkt)800 void AvrcCtConnectManager::PushVendorQueue(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtVendorPacket> &pkt)
801 {
802     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
803     std::lock_guard<std::recursive_mutex> lock(mutex_);
804 
805     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
806     if (info != nullptr) {
807         info->vdInfo_.cmdQue_.push(pkt);
808     } else {
809         HILOGI("The connection information does not exist!");
810     }
811 }
812 
PopVendorQueue(const RawAddress & rawAddr)813 std::shared_ptr<AvrcCtVendorPacket> AvrcCtConnectManager::PopVendorQueue(const RawAddress &rawAddr)
814 {
815     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
816     std::lock_guard<std::recursive_mutex> lock(mutex_);
817     std::shared_ptr<AvrcCtVendorPacket> pkt = nullptr;
818 
819     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
820     if (info != nullptr) {
821         pkt = info->vdInfo_.cmdQue_.front();
822         info->vdInfo_.cmdQue_.pop();
823     } else {
824         HILOGI("The connection information does not exist!");
825     }
826 
827     return pkt;
828 }
829 
GetSizeOfVendorQueue(const RawAddress & rawAddr)830 uint8_t AvrcCtConnectManager::GetSizeOfVendorQueue(const RawAddress &rawAddr)
831 {
832     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
833     std::lock_guard<std::recursive_mutex> lock(mutex_);
834     uint8_t result = 0x00;
835 
836     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
837     if (info != nullptr) {
838         result = info->vdInfo_.cmdQue_.size();
839     } else {
840         HILOGI("The connection information does not exist!");
841     }
842 
843     return result;
844 }
845 
846 /******************************************************************
847  * BROWSING COMMAND                                               *
848  ******************************************************************/
849 
GetBrowsePacket(const RawAddress & rawAddr)850 std::shared_ptr<AvrcCtBrowsePacket> AvrcCtConnectManager::GetBrowsePacket(const RawAddress &rawAddr)
851 {
852     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
853     std::lock_guard<std::recursive_mutex> lock(mutex_);
854     std::shared_ptr<AvrcCtBrowsePacket> pkt = nullptr;
855 
856     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
857     if (info != nullptr) {
858         pkt = info->brInfo_.pkt_;
859     } else {
860         HILOGI("The connection information does not exist!");
861     }
862 
863     return pkt;
864 }
865 
SetBrowsePacket(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtBrowsePacket> & pkt)866 void AvrcCtConnectManager::SetBrowsePacket(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtBrowsePacket> &pkt)
867 {
868     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
869     std::lock_guard<std::recursive_mutex> lock(mutex_);
870 
871     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
872     if (info != nullptr) {
873         info->brInfo_.pkt_ = pkt;
874     } else {
875         HILOGI("The connection information does not exist!");
876     }
877 }
878 
SetBrowseTimer(const RawAddress & rawAddr,std::function<void ()> callback,int ms,bool isPeriodic)879 void AvrcCtConnectManager::SetBrowseTimer(
880     const RawAddress &rawAddr, std::function<void()> callback, int ms, bool isPeriodic)
881 {
882     HILOGI("addr:%{public}s, ms:%{public}d, isPeriodic:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), ms, isPeriodic);
883     std::lock_guard<std::recursive_mutex> lock(mutex_);
884 
885     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
886     if (info != nullptr) {
887         info->brInfo_.timer_ = std::make_shared<utility::Timer>(callback);
888         info->brInfo_.timer_->Start(ms, isPeriodic);
889     } else {
890         HILOGI("The connection information does not exist!");
891     }
892 }
893 
ClearBrowsePacket(const RawAddress & rawAddr)894 void AvrcCtConnectManager::ClearBrowsePacket(const RawAddress &rawAddr)
895 {
896     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
897     std::lock_guard<std::recursive_mutex> lock(mutex_);
898 
899     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
900     if (info != nullptr) {
901         info->brInfo_.pkt_ = nullptr;
902     } else {
903         HILOGI("The connection information does not exist!");
904     }
905 }
906 
ClearBrowseTimer(const RawAddress & rawAddr)907 void AvrcCtConnectManager::ClearBrowseTimer(const RawAddress &rawAddr)
908 {
909     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
910     std::lock_guard<std::recursive_mutex> lock(mutex_);
911 
912     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
913     if (info != nullptr) {
914         if (info->brInfo_.timer_ != nullptr) {
915             info->brInfo_.timer_->Stop();
916             info->brInfo_.timer_ = nullptr;
917         }
918     } else {
919         HILOGI("The connection information does not exist!");
920     }
921 }
922 
ClearBrowseInfo(const RawAddress & rawAddr)923 void AvrcCtConnectManager::ClearBrowseInfo(const RawAddress &rawAddr)
924 {
925     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
926 
927     ClearBrowseTimer(rawAddr);
928     ClearBrowsePacket(rawAddr);
929 }
930 
PushBrowseQueue(const RawAddress & rawAddr,const std::shared_ptr<AvrcCtBrowsePacket> & pkt)931 void AvrcCtConnectManager::PushBrowseQueue(const RawAddress &rawAddr, const std::shared_ptr<AvrcCtBrowsePacket> &pkt)
932 {
933     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
934     std::lock_guard<std::recursive_mutex> lock(mutex_);
935 
936     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
937     if (info != nullptr) {
938         info->brInfo_.cmdQue_.push(pkt);
939     } else {
940         HILOGI("The connection information does not exist!");
941     }
942 }
943 
PopBrowseQueue(const RawAddress & rawAddr)944 std::shared_ptr<AvrcCtBrowsePacket> AvrcCtConnectManager::PopBrowseQueue(const RawAddress &rawAddr)
945 {
946     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
947     std::lock_guard<std::recursive_mutex> lock(mutex_);
948     std::shared_ptr<AvrcCtBrowsePacket> pkt = nullptr;
949 
950     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
951     if (info != nullptr) {
952         pkt = info->brInfo_.cmdQue_.front();
953         info->brInfo_.cmdQue_.pop();
954     } else {
955         HILOGI("The connection information does not exist!");
956     }
957 
958     return pkt;
959 }
960 
GetSizeOfBrowseQueue(const RawAddress & rawAddr)961 uint8_t AvrcCtConnectManager::GetSizeOfBrowseQueue(const RawAddress &rawAddr)
962 {
963     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
964     std::lock_guard<std::recursive_mutex> lock(mutex_);
965     uint8_t result = 0x00;
966 
967     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
968     if (info != nullptr) {
969         result = info->brInfo_.cmdQue_.size();
970     } else {
971         HILOGI("The connection information does not exist!");
972     }
973 
974     return result;
975 }
976 
IsDisableAbsoluteVolume(const RawAddress & rawAddr)977 bool AvrcCtConnectManager::IsDisableAbsoluteVolume(const RawAddress &rawAddr)
978 {
979     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
980     std::lock_guard<std::recursive_mutex> lock(mutex_);
981     bool result = false;
982 
983     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
984     if (info != nullptr) {
985         result = info->absVolume_;
986     } else {
987         HILOGI("The connection information does not exist!");
988     }
989 
990     return result;
991 }
992 
IsBrowsingConnected(const RawAddress & rawAddr)993 bool AvrcCtConnectManager::IsBrowsingConnected(const RawAddress &rawAddr)
994 {
995     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
996     std::lock_guard<std::recursive_mutex> lock(mutex_);
997     bool result = false;
998 
999     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
1000     if (info != nullptr) {
1001         result = info->brConnected_;
1002     } else {
1003         HILOGI("The connection information does not exist!");
1004     }
1005 
1006     return result;
1007 }
1008 
SetBrowsingState(const RawAddress & rawAddr,bool state)1009 void AvrcCtConnectManager::SetBrowsingState(const RawAddress &rawAddr, bool state)
1010 {
1011     HILOGI("address: %{public}s, state: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), state);
1012     std::lock_guard<std::recursive_mutex> lock(mutex_);
1013 
1014     AvrcCtConnectInfo *info = GetConnectInfo(rawAddr.GetAddress());
1015     if (info != nullptr) {
1016         info->brConnected_ = state;
1017     } else {
1018         HILOGI("The connection information does not exist!");
1019     }
1020 }
1021 
~AvrcCtConnectManager()1022 AvrcCtConnectManager::~AvrcCtConnectManager()
1023 {
1024     HILOGI("enter");
1025 
1026     infos_.clear();
1027 
1028     g_instance = nullptr;
1029 }
1030 
GetConnectInfo(const std::string & btAddr)1031 AvrcCtConnectInfo *AvrcCtConnectManager::GetConnectInfo(const std::string &btAddr)
1032 {
1033     HILOGI("addr: %{public}s", GetEncryptAddr(btAddr).c_str());
1034     std::lock_guard<std::recursive_mutex> lock(mutex_);
1035     AvrcCtConnectInfo *info = nullptr;
1036 
1037     auto iter = infos_.find(btAddr);
1038     if (iter != infos_.end()) {
1039         info = &iter->second;
1040     }
1041 
1042     return info;
1043 }
1044 }  // namespace bluetooth
1045 }  // namespace OHOS