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 /**
17  * @file avrcp_tg_service.cpp
18  *
19  * @brief Defines the class of the AVRCP TG service, including attributes and methods.
20  */
21 
22 #include "avrcp_tg_service.h"
23 
24 #include <functional>
25 #include "adapter_config.h"
26 #include "class_creator.h"
27 #include "packet.h"
28 #include "profile_service_manager.h"
29 #include "stub/media_service.h"
30 #include "timer.h"
31 #include "log_util.h"
32 
33 namespace OHOS {
34 namespace bluetooth {
OnConnectionStateChanged(const std::string & addr,int state)35 void AvrcpTgService::ObserverImpl::OnConnectionStateChanged(const std::string &addr, int state)
36 {
37     HILOGI("addr: %{public}s, state: %{public}d", GetEncryptAddr(addr).c_str(), state);
38 }
39 
OnPressButton(const std::string & addr,uint8_t button)40 void AvrcpTgService::ObserverImpl::OnPressButton(const std::string &addr, uint8_t button)
41 {
42     HILOGI("addr: %{public}s, button: %{public}x", GetEncryptAddr(addr).c_str(), button);
43 }
44 
OnReleaseButton(const std::string & addr,uint8_t button)45 void AvrcpTgService::ObserverImpl::OnReleaseButton(const std::string &addr, uint8_t button)
46 {
47     HILOGI("addr: %{public}s, button: %{public}x", GetEncryptAddr(addr).c_str(), button);
48 }
49 
OnSetAddressedPlayer(const std::string & addr,uint8_t label,int status)50 void AvrcpTgService::ObserverImpl::OnSetAddressedPlayer(const std::string &addr, uint8_t label, int status)
51 {
52     HILOGI("addr: %{public}s, label: %{public}d, status: %{public}d", GetEncryptAddr(addr).c_str(), label, status);
53 
54     IProfileAvrcpTg *service = GetService();
55     if (service != nullptr) {
56         RawAddress rawAddr(addr);
57         service->OnSetAddressedPlayer(rawAddr, label, status);
58     }
59 }
60 
OnSetBrowsedPlayer(const std::string & addr,uint16_t uidCounter,uint32_t numOfItems,const std::vector<std::string> & folderNames,uint8_t label,int status)61 void AvrcpTgService::ObserverImpl::OnSetBrowsedPlayer(const std::string &addr, uint16_t uidCounter, uint32_t numOfItems,
62     const std::vector<std::string> &folderNames, uint8_t label, int status)
63 {
64     HILOGI("addr: %{public}s, uidCounter: %{public}d, numOfItems: %{public}u, label: %{public}d, status: %{public}d",
65         GetEncryptAddr(addr).c_str(), uidCounter, numOfItems, label, status);
66 
67     IProfileAvrcpTg *service = GetService();
68     if (service != nullptr) {
69         RawAddress rawAddr(addr);
70         service->OnSetBrowsedPlayer(rawAddr, uidCounter, numOfItems, folderNames, label, status);
71     }
72 }
73 
OnGetPlayerAppSettingAttributes(const std::string & addr,const std::deque<uint8_t> & attributes,uint8_t label)74 void AvrcpTgService::ObserverImpl::OnGetPlayerAppSettingAttributes(
75     const std::string &addr, const std::deque<uint8_t> &attributes, uint8_t label)
76 {
77     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
78 
79     IProfileAvrcpTg *service = GetService();
80     if (service != nullptr) {
81         RawAddress rawAddr(addr);
82         service->OnGetPlayerAppSettingAttributes(rawAddr, attributes, label);
83     }
84 }
85 
OnGetPlayerAppSettingValues(const std::string & addr,const std::deque<uint8_t> & values,uint8_t label)86 void AvrcpTgService::ObserverImpl::OnGetPlayerAppSettingValues(
87     const std::string &addr, const std::deque<uint8_t> &values, uint8_t label)
88 {
89     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
90 
91     IProfileAvrcpTg *service = GetService();
92     if (service != nullptr) {
93         RawAddress rawAddr(addr);
94         service->OnGetPlayerAppSettingValues(rawAddr, values, label);
95     }
96 }
97 
OnGetPlayerAppSettingCurrentValue(const std::string & addr,const std::deque<uint8_t> & attributes,const std::deque<uint8_t> & values,uint8_t label,uint8_t context)98 void AvrcpTgService::ObserverImpl::OnGetPlayerAppSettingCurrentValue(const std::string &addr,
99     const std::deque<uint8_t> &attributes, const std::deque<uint8_t> &values, uint8_t label, uint8_t context)
100 {
101     HILOGI("addr: %{public}s, label: %{public}d, context: %{public}d", GetEncryptAddr(addr).c_str(), label, context);
102 
103     IProfileAvrcpTg *service = GetService();
104     if (service != nullptr) {
105         RawAddress rawAddr(addr);
106         service->OnGetPlayerAppSettingCurrentValue(rawAddr, attributes, values, label, context);
107     }
108 }
109 
OnSetPlayerAppSettingCurrentValue(const std::string & addr,uint8_t label)110 void AvrcpTgService::ObserverImpl::OnSetPlayerAppSettingCurrentValue(const std::string &addr, uint8_t label)
111 {
112     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
113 
114     IProfileAvrcpTg *service = GetService();
115     if (service != nullptr) {
116         RawAddress rawAddr(addr);
117         service->OnSetPlayerAppSettingCurrentValue(rawAddr, label);
118     }
119 }
120 
OnGetPlayerAppSettingAttributeText(const std::string & addr,const std::vector<uint8_t> & attributes,const std::vector<std::string> & attrStr,uint8_t label)121 void AvrcpTgService::ObserverImpl::OnGetPlayerAppSettingAttributeText(const std::string &addr,
122     const std::vector<uint8_t> &attributes, const std::vector<std::string> &attrStr, uint8_t label)
123 {
124     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
125 
126     IProfileAvrcpTg *service = GetService();
127     if (service != nullptr) {
128         RawAddress rawAddr(addr);
129         service->OnGetPlayerAppSettingAttributeText(rawAddr, attributes, attrStr, label);
130     }
131 }
132 
OnGetPlayerAppSettingValueText(const std::string & addr,const std::vector<uint8_t> & values,const std::vector<std::string> & valueStr,uint8_t label)133 void AvrcpTgService::ObserverImpl::OnGetPlayerAppSettingValueText(const std::string &addr,
134     const std::vector<uint8_t> &values, const std::vector<std::string> &valueStr, uint8_t label)
135 {
136     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
137 
138     IProfileAvrcpTg *service = GetService();
139     if (service != nullptr) {
140         RawAddress rawAddr(addr);
141         service->OnGetPlayerAppSettingValueText(rawAddr, values, valueStr, label);
142     }
143 }
144 
OnGetElementAttributes(const std::string & addr,const std::vector<uint32_t> & attribtues,const std::vector<std::string> & values,uint8_t label)145 void AvrcpTgService::ObserverImpl::OnGetElementAttributes(const std::string &addr,
146     const std::vector<uint32_t> &attribtues, const std::vector<std::string> &values, uint8_t label)
147 {
148     HILOGI("addr: %{public}s, label: %{public}d", GetEncryptAddr(addr).c_str(), label);
149 
150     IProfileAvrcpTg *service = GetService();
151     if (service != nullptr) {
152         RawAddress rawAddr(addr);
153         service->OnGetElementAttributes(rawAddr, attribtues, values, label);
154     }
155 }
156 
OnGetPlayStatus(const std::string & addr,uint32_t songLength,uint32_t songPosition,uint8_t playStatus,uint8_t label,uint8_t context)157 void AvrcpTgService::ObserverImpl::OnGetPlayStatus(const std::string &addr, uint32_t songLength, uint32_t songPosition,
158     uint8_t playStatus, uint8_t label, uint8_t context)
159 {
160     HILOGI("addr:%{public}s, songLength:%{public}u, songPosition:%{public}u, playStatus:%{public}d, label:%{public}d,"
161         " context:%{public}d", GetEncryptAddr(addr).c_str(), songLength, songPosition, playStatus, label, context);
162     IProfileAvrcpTg *service = GetService();
163     if (service != nullptr) {
164         RawAddress rawAddr(addr);
165         service->OnGetPlayStatus(rawAddr, songLength, songPosition, playStatus, label, context);
166     }
167 }
168 
OnPlayItem(const std::string & addr,uint8_t label,int status)169 void AvrcpTgService::ObserverImpl::OnPlayItem(const std::string &addr, uint8_t label, int status)
170 {
171     HILOGI("addr: %{public}s, label: %{public}d, status: %{public}d", GetEncryptAddr(addr).c_str(), label, status);
172 
173     IProfileAvrcpTg *service = GetService();
174     if (service != nullptr) {
175         RawAddress rawAddr(addr);
176         service->OnPlayItem(rawAddr, label, status);
177     }
178 }
OnAddToNowPlaying(const std::string & addr,uint8_t label,int status)179 void AvrcpTgService::ObserverImpl::OnAddToNowPlaying(const std::string &addr, uint8_t label, int status)
180 {
181     HILOGI("addr: %{public}s, label: %{public}d, status: %{public}d", GetEncryptAddr(addr).c_str(), label, status);
182 
183     IProfileAvrcpTg *service = GetService();
184     if (service != nullptr) {
185         RawAddress rawAddr(addr);
186         service->OnAddToNowPlaying(rawAddr, label, status);
187     }
188 }
189 
OnChangePath(const std::string & addr,uint32_t numOfItems,uint8_t label,int status)190 void AvrcpTgService::ObserverImpl::OnChangePath(const std::string &addr, uint32_t numOfItems, uint8_t label, int status)
191 {
192     HILOGI("addr: %{public}s, numOfItems: %{public}u, label: %{public}d, status: %{public}d",
193         GetEncryptAddr(addr).c_str(), numOfItems, label, status);
194 
195     IProfileAvrcpTg *service = GetService();
196     if (service != nullptr) {
197         RawAddress rawAddr(addr);
198         service->OnChangePath(rawAddr, numOfItems, label, status);
199     }
200 }
201 
OnGetMediaPlayers(const std::string & addr,uint16_t uidCounter,const std::vector<stub::MediaService::MediaPlayer> & items,uint8_t label,int status)202 void AvrcpTgService::ObserverImpl::OnGetMediaPlayers(const std::string &addr, uint16_t uidCounter,
203     const std::vector<stub::MediaService::MediaPlayer> &items, uint8_t label, int status)
204 {
205     HILOGI("addr: %{public}s, uidCounter: %{public}d, label: %{public}d, status: %{public}d",
206         GetEncryptAddr(addr).c_str(), uidCounter, label, status);
207     IProfileAvrcpTg *service = GetService();
208     if (service != nullptr) {
209         RawAddress rawAddr(addr);
210         std::vector<AvrcMpItem> mpItems;
211         for (stub::MediaService::MediaPlayer item : items) {
212             AvrcMpItem mpItem(item.itemType_,
213                 item.playerId_,
214                 item.majorType_,
215                 item.subType_,
216                 item.playStatus_,
217                 item.features_,
218                 item.name_);
219             mpItems.push_back(mpItem);
220         }
221 
222         service->OnGetMediaPlayers(rawAddr, uidCounter, mpItems, label, status);
223     }
224 }
225 
OnGetFolderItems(const std::string & addr,uint16_t uidCounter,const std::vector<stub::MediaService::MediaItem> & items,uint8_t label,int status)226 void AvrcpTgService::ObserverImpl::OnGetFolderItems(const std::string &addr, uint16_t uidCounter,
227     const std::vector<stub::MediaService::MediaItem> &items, uint8_t label, int status)
228 {
229     HILOGI("addr: %{public}s, uidCounter: %{public}d, label: %{public}d, status: %{public}d",
230         GetEncryptAddr(addr).c_str(), uidCounter, label, status);
231     IProfileAvrcpTg *service = GetService();
232     if (service != nullptr) {
233         RawAddress rawAddr(addr);
234         std::vector<AvrcMeItem> meItems;
235         for (stub::MediaService::MediaItem item : items) {
236             AvrcMeItem meItem(
237                 item.itemType_, item.uid_, item.mediaType_, item.playable_, item.name_, item.attributes_, item.values_);
238             meItems.push_back(meItem);
239         }
240 
241         service->OnGetFolderItems(rawAddr, uidCounter, meItems, label, status);
242     }
243 }
244 
OnGetItemAttributes(const std::string & addr,const std::vector<uint32_t> & attributes,const std::vector<std::string> & values,uint8_t label,int status)245 void AvrcpTgService::ObserverImpl::OnGetItemAttributes(const std::string &addr, const std::vector<uint32_t> &attributes,
246     const std::vector<std::string> &values, uint8_t label, int status)
247 {
248     HILOGI("addr: %{public}s, label: %{public}d, status: %{public}d", GetEncryptAddr(addr).c_str(), label, status);
249     IProfileAvrcpTg *service = GetService();
250     if (service != nullptr) {
251         RawAddress rawAddr(addr);
252         service->OnGetItemAttributes(rawAddr, attributes, values, label, status);
253     }
254 }
255 
OnGetTotalNumberOfItems(const std::string & addr,uint16_t uidCounter,uint32_t numOfItems,uint8_t label,int status)256 void AvrcpTgService::ObserverImpl::OnGetTotalNumberOfItems(
257     const std::string &addr, uint16_t uidCounter, uint32_t numOfItems, uint8_t label, int status)
258 {
259     HILOGI("addr: %{public}s, uidCounter: %{public}d, numOfItems: %{public}u, label: %{public}d, status: %{public}d",
260         GetEncryptAddr(addr).c_str(), uidCounter, numOfItems, label, status);
261 
262     IProfileAvrcpTg *service = GetService();
263     if (service != nullptr) {
264         RawAddress rawAddr(addr);
265         service->OnGetTotalNumberOfItems(rawAddr, uidCounter, numOfItems, label, status);
266     }
267 }
268 
OnSetAbsoluteVolume(const std::string & addr,uint8_t volume,uint8_t label)269 void AvrcpTgService::ObserverImpl::OnSetAbsoluteVolume(const std::string &addr, uint8_t volume, uint8_t label)
270 {
271     HILOGI("addr: %{public}s, volume: %{public}d, label: %{public}d", GetEncryptAddr(addr).c_str(), volume, label);
272 
273     IProfileAvrcpTg *service = GetService();
274     if (service != nullptr) {
275         RawAddress rawAddr(addr);
276         service->OnSetAbsoluteVolume(rawAddr, volume, label);
277     }
278 }
279 
OnGetSelectedTrack(const std::string & addr,uint64_t uid,uint8_t label)280 void AvrcpTgService::ObserverImpl::OnGetSelectedTrack(const std::string &addr, uint64_t uid, uint8_t label)
281 {
282     HILOGI("addr: %{public}s, uid: %{public}llu, label: %{public}d",
283         GetEncryptAddr(addr).c_str(), static_cast<unsigned long long>(uid), label);
284 
285     IProfileAvrcpTg *service = GetService();
286     if (service != nullptr) {
287         RawAddress rawAddr(addr);
288         service->OnGetSelectedTrack(rawAddr, uid, label);
289     }
290 }
291 
OnGetAddressedPlayer(const std::string & addr,uint16_t playerId,uint16_t uidCounter,uint8_t label)292 void AvrcpTgService::ObserverImpl::OnGetAddressedPlayer(
293     const std::string &addr, uint16_t playerId, uint16_t uidCounter, uint8_t label)
294 {
295     HILOGI("addr: %{public}s, playerId: %{public}d, uidCounter: %{public}d, label: %{public}d",
296         GetEncryptAddr(addr).c_str(), playerId, uidCounter, label);
297 
298     IProfileAvrcpTg *service = GetService();
299     if (service != nullptr) {
300         RawAddress rawAddr(addr);
301         service->OnGetAddressedPlayer(rawAddr, playerId, uidCounter, label);
302     }
303 }
304 
OnGetUidCounter(const std::string & addr,uint16_t uidCounter,uint8_t label)305 void AvrcpTgService::ObserverImpl::OnGetUidCounter(const std::string &addr, uint16_t uidCounter, uint8_t label)
306 {
307     HILOGI("addr: %{public}s, uidCounter: %{public}d, label: %{public}d",
308         GetEncryptAddr(addr).c_str(), uidCounter, label);
309     IProfileAvrcpTg *service = GetService();
310     if (service != nullptr) {
311         RawAddress rawAddr(addr);
312         service->OnGetUidCounter(rawAddr, uidCounter, label);
313     }
314 }
315 
OnGetCurrentAbsoluteVolume(const std::string & addr,uint8_t volume,uint8_t label)316 void AvrcpTgService::ObserverImpl::OnGetCurrentAbsoluteVolume(const std::string &addr, uint8_t volume, uint8_t label)
317 {
318     HILOGI("addr: %{public}s, volume: %{public}d, label: %{public}d", GetEncryptAddr(addr).c_str(), volume, label);
319     IProfileAvrcpTg *service = GetService();
320     if (service != nullptr) {
321         RawAddress rawAddr(addr);
322         service->OnGetCurrentAbsoluteVolume(rawAddr, volume, label);
323     }
324 }
325 
GetService(void)326 IProfileAvrcpTg *AvrcpTgService::ObserverImpl::GetService(void)
327 {
328     HILOGI("enter");
329 
330     auto svManager = IProfileManager::GetInstance();
331 
332     return static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
333 }
334 #ifdef AVRCP_AVSESSION
OnSessionCreate(const OHOS::AVSession::AVSessionDescriptor & descriptor)335 void AvrcpTgService::AVSessionObserverImpl::OnSessionCreate(const OHOS::AVSession::AVSessionDescriptor& descriptor)
336 {
337     HILOGI("enter");
338 
339     IProfileAvrcpTg *service = GetService();
340     if (service != nullptr) {
341         service->OnSessionCreate(descriptor.sessionId_);
342     }
343 }
344 
OnSessionRelease(const OHOS::AVSession::AVSessionDescriptor & descriptor)345 void AvrcpTgService::AVSessionObserverImpl::OnSessionRelease(const OHOS::AVSession::AVSessionDescriptor& descriptor)
346 {
347     HILOGI("enter");
348 
349     IProfileAvrcpTg *service = GetService();
350     if (service != nullptr) {
351         service->OnSessionRelease(descriptor.sessionId_);
352     }
353 }
354 
OnTopSessionChange(const OHOS::AVSession::AVSessionDescriptor & descriptor)355 void AvrcpTgService::AVSessionObserverImpl::OnTopSessionChange(const OHOS::AVSession::AVSessionDescriptor& descriptor)
356 {
357     HILOGI("sessionId:%{public}s", descriptor.sessionId_.c_str());
358 
359     IProfileAvrcpTg *service = GetService();
360     if (service != nullptr) {
361         service->OnTopSessionChange(descriptor.sessionId_);
362     }
363 }
364 
GetService(void)365 IProfileAvrcpTg *AvrcpTgService::AVSessionObserverImpl::GetService(void)
366 {
367     HILOGI("enter");
368 
369     auto svManager = IProfileManager::GetInstance();
370 
371     return static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
372 }
373 
OnSessionDestroy()374 void AvrcpTgService::AVControllerObserverImpl::OnSessionDestroy()
375 {
376     HILOGI("enter");
377 }
378 
OnPlaybackStateChange(const OHOS::AVSession::AVPlaybackState & state)379 void AvrcpTgService::AVControllerObserverImpl::OnPlaybackStateChange(const OHOS::AVSession::AVPlaybackState &state)
380 {
381     HILOGI("state:%{public}d", state.GetState());
382 
383     IProfileAvrcpTg *service = GetService();
384     if (service != nullptr) {
385         service->OnPlaybackStateChange(state.GetState());
386     }
387 }
388 
OnMetaDataChange(const OHOS::AVSession::AVMetaData & data)389 void AvrcpTgService::AVControllerObserverImpl::OnMetaDataChange(const OHOS::AVSession::AVMetaData &data)
390 {
391     HILOGI("enter");
392 }
393 
OnActiveStateChange(bool isActive)394 void AvrcpTgService::AVControllerObserverImpl::OnActiveStateChange(bool isActive)
395 {
396     HILOGI("isActive:%{public}d", isActive);
397 }
398 
OnValidCommandChange(const std::vector<int32_t> & cmds)399 void AvrcpTgService::AVControllerObserverImpl::OnValidCommandChange(const std::vector<int32_t> &cmds)
400 {
401     HILOGI("enter");
402 }
403 
GetService(void)404 IProfileAvrcpTg *AvrcpTgService::AVControllerObserverImpl::GetService(void)
405 {
406     HILOGI("enter");
407 
408     auto svManager = IProfileManager::GetInstance();
409 
410     return static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
411 }
412 #endif
AvrcpTgService()413 AvrcpTgService::AvrcpTgService() : utility::Context(PROFILE_NAME_AVRCP_TG, "1.6.2")
414 {
415     HILOGI("enter");
416 
417     mdObserver_ = std::make_unique<ObserverImpl>();
418 #ifdef AVRCP_AVSESSION
419     avSessionObserver_ = std::make_shared<AVSessionObserverImpl>();
420     avControllerObserver_ = std::make_shared<AVControllerObserverImpl>();
421 #endif
422 
423     InitFeatures();
424 
425     using namespace std::placeholders;
426     AvrcTgProfile::Observer observer = {
427         std::bind(&AvrcpTgService::OnProfileDisabled, this, BT_SUCCESS),
428         std::bind(&AvrcpTgService::OnConnectionStateChanged, this, _1, _2),
429         std::bind(&AvrcpTgService::FindCtService, this, _1),
430         std::bind(&AvrcpTgService::OnButtonPressed, this, _1, _2, _3),
431         std::bind(&AvrcpTgService::OnButtonReleased, this, _1, _2, _3),
432         std::bind(&AvrcpTgService::HoldButton, this, _1, _2, _3),
433         std::bind(&AvrcpTgService::GetCapabilities, this, _1, _2),
434         std::bind(&AvrcpTgService::GetPlayerAppSettingAttributes, this, _1, _2),
435         std::bind(&AvrcpTgService::GetPlayerAppSettingValues, this, _1, _2, _3),
436         std::bind(&AvrcpTgService::GetPlayerAppSettingCurrentValue, this, _1, _2, _3, _4),
437         std::bind(&AvrcpTgService::SetPlayerAppSettingCurrentValue, this, _1, _2, _3, _4),
438         std::bind(&AvrcpTgService::GetPlayerAppSettingAttributeText, this, _1, _2, _3),
439         std::bind(&AvrcpTgService::GetPlayerAppSettingValueText, this, _1, _2, _3, _4),
440         std::bind(&AvrcpTgService::GetElementAttributes, this, _1, _2, _3, _4),
441         std::bind(&AvrcpTgService::GetPlayStatus, this, _1, _2, _3),
442         std::bind(&AvrcpTgService::SetAddressedPlayer, this, _1, _2, _3, _4),
443         std::bind(&AvrcpTgService::SetBrowsedPlayer, this, _1, _2, _3, _4),
444         std::bind(&AvrcpTgService::ChangePath, this, _1, _2, _3, _4, _5),
445         std::bind(&AvrcpTgService::GetFolderItems, this, _1, _2, _3, _4, _5, _6),
446         std::bind(&AvrcpTgService::GetItemAttributes, this, _1, _2, _3, _4, _5, _6),
447         std::bind(&AvrcpTgService::GetTotalNumberOfItems, this, _1, _2, _3),
448         std::bind(&AvrcpTgService::PlayItem, this, _1, _2, _3, _4, _5),
449         std::bind(&AvrcpTgService::AddToNowPlaying, this, _1, _2, _3, _4, _5),
450         std::bind(&AvrcpTgService::SetAbsoluteVolume, this, _1, _2, _3),
451         std::bind(&AvrcpTgService::GetSelectedTrack, this, _1, _2),
452         std::bind(&AvrcpTgService::GetAddressedPlayer, this, _1, _2),
453         std::bind(&AvrcpTgService::GetUidCounter, this, _1, _2),
454         std::bind(&AvrcpTgService::GetCurrentAbsoluteVolume, this, _1, _2),
455         std::bind(&AvrcpTgService::SetPlaybackInterval, this, _1, _2),
456         std::bind(&AvrcpTgService::SetActiveDevice, this, _1),
457     };
458     pfObserver_ = std::make_unique<AvrcTgProfile::Observer>(observer);
459 }
460 
~AvrcpTgService()461 AvrcpTgService::~AvrcpTgService()
462 {
463     HILOGI("enter");
464 
465     myObserver_ = nullptr;
466 }
467 
GetContext()468 utility::Context *AvrcpTgService::GetContext()
469 {
470     HILOGI("enter");
471 
472     return this;
473 }
474 
InitFeatures()475 void AvrcpTgService::InitFeatures()
476 {
477     HILOGI("enter");
478 
479     features_ |= AVRC_TG_FEATURE_CATEGORY_1;
480     features_ |= AVRC_TG_FEATURE_CATEGORY_2;
481     features_ |= AVRC_TG_FEATURE_PLAYER_APPLICATION_SETTINGS;
482     features_ |= AVRC_TG_FEATURE_BROWSING;
483     features_ |= AVRC_TG_FEATURE_MULTIPLE_MEDIA_PLAYER_APPLICATIONS;
484     features_ |= AVRC_TG_FEATURE_KEY_OPERATION;
485     features_ |= AVRC_TG_FEATURE_ABSOLUTE_VOLUME;
486     features_ |= AVRC_TG_FEATURE_NOTIFY_PLAYBACK_STATUS_CHANGED;
487     features_ |= AVRC_TG_FEATURE_NOTIFY_TRACK_REACHED_END;
488     features_ |= AVRC_TG_FEATURE_NOTIFY_TRACK_REACHED_START;
489     features_ |= AVRC_TG_FEATURE_NOTIFY_PLAYBACK_POSITION_CHANGED;
490     features_ |= AVRC_TG_FEATURE_NOTIFY_PLAYER_SETTING_CHANGED;
491     features_ |= AVRC_TG_FEATURE_NOTIFY_NOW_PLAYING_CONTENT_CHANGED;
492     features_ |= AVRC_TG_FEATURE_NOTIFY_UIDS_CHANGED;
493     features_ |= AVRC_TG_FEATURE_NOTIFY_ABSOLUTE_VOLUME_CHANGED;
494 }
495 
496 /******************************************************************
497  * REGISTER / UNREGISTER OBSERVER                                 *
498  ******************************************************************/
499 
RegisterObserver(IObserver * observer)500 void AvrcpTgService::RegisterObserver(IObserver *observer)
501 {
502     HILOGI("enter");
503 
504     std::lock_guard<std::recursive_mutex> lock(mutex_);
505 
506     myObserver_ = observer;
507 }
508 
UnregisterObserver(void)509 void AvrcpTgService::UnregisterObserver(void)
510 {
511     HILOGI("enter");
512 
513     std::lock_guard<std::recursive_mutex> lock(mutex_);
514 
515     myObserver_ = nullptr;
516 }
517 
518 /******************************************************************
519  * ENABLE / DISABLE                                               *
520  ******************************************************************/
521 
Enable(void)522 void AvrcpTgService::Enable(void)
523 {
524     HILOGI("enter");
525 
526     if (IsDisabled()) {
527         SetServiceState(AVRC_TG_SERVICE_STATE_ENABLING);
528 
529         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::EnableNative, this));
530     } else {
531         HILOGE("Is not disabled!");
532     }
533 }
534 
Disable(void)535 void AvrcpTgService::Disable(void)
536 {
537     HILOGI("enter");
538 
539     if (IsEnabled()) {
540         SetServiceState(AVRC_TG_SERVICE_STATE_DISABLING);
541         if (profile_ != nullptr) {
542             profile_->SetEnableFlag(false);
543         }
544         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::DisableNative, this));
545     } else {
546         HILOGE("Is not enable!");
547     }
548 }
549 
EnableNative(void)550 void AvrcpTgService::EnableNative(void)
551 {
552     HILOGI("enter");
553 
554     int result = BT_SUCCESS;
555 
556     stub::MediaService::GetInstance()->RegisterObserver(mdObserver_.get());
557 #ifdef AVRCP_AVSESSION
558     RegisterAvSessionControl();
559 #endif
560     IAdapterConfig *config = AdapterConfig::GetInstance();
561     config->GetValue(SECTION_AVRCP_TG_SERVICE, PROPERTY_MAX_CONNECTED_DEVICES, maxConnection_);
562 
563     do {
564         result = RegisterSecurity();
565         if (result != BT_SUCCESS) {
566             break;
567         }
568 
569         result = RegisterService();
570         if (result != BT_SUCCESS) {
571             break;
572         }
573 
574         result = EnableProfile();
575         if (result != BT_SUCCESS) {
576             break;
577         }
578     } while (false);
579 
580     if (result == BT_SUCCESS) {
581         SetServiceState(AVRC_TG_SERVICE_STATE_ENABLED);
582     } else {
583         SetServiceState(AVRC_TG_SERVICE_STATE_DISABLED);
584     }
585 
586     GetContext()->OnEnable(PROFILE_NAME_AVRCP_TG, IsEnabled());
587 }
588 
DisableNative(void)589 void AvrcpTgService::DisableNative(void)
590 {
591     HILOGI("enter");
592 
593     if (DisableProfile() != BT_SUCCESS) {
594         OnProfileDisabled(RET_BAD_STATUS);
595     }
596 }
597 
598 #ifdef AVRCP_AVSESSION
RegisterAvSessionControl(void)599 void AvrcpTgService::RegisterAvSessionControl(void)
600 {
601     // register avSession observer
602     auto res = OHOS::AVSession::AVSessionManager::GetInstance().RegisterSessionListener(avSessionObserver_);
603     if (res != OHOS::AVSession::AVSESSION_SUCCESS) {
604         HILOGE("RegisterSessionListener fail");
605         return;
606     }
607 
608     res = OHOS::AVSession::AVSessionManager::GetInstance().GetAllSessionDescriptors(avSessionDescriptor_);
609     if (res != OHOS::AVSession::AVSESSION_SUCCESS) {
610         HILOGE("GetAllSessionDescriptors fail");
611         return;
612     }
613 
614     // parse top descriptor
615     std::string sessionId;
616     for (auto descriptor : avSessionDescriptor_) {
617         HILOGI("isTopSession:%{public}d", descriptor.isTopSession_);
618         if (descriptor.isTopSession_) {
619             sessionId = descriptor.sessionId_;
620         }
621     }
622 
623     if (sessionId.empty()) {
624         HILOGI("no valid avsession");
625     } else {
626         res = OHOS::AVSession::AVSessionManager::GetInstance().CreateController(sessionId, avSessionController_);
627         if (res == OHOS::AVSession::AVSESSION_SUCCESS && avSessionController_ != NULL) {
628             avSessionController_->RegisterCallback(avControllerObserver_);
629         } else {
630             HILOGE("avSessionController_ is NULL");
631         }
632     }
633 }
634 #endif
635 
RegisterSecurity(void)636 int AvrcpTgService::RegisterSecurity(void)
637 {
638     HILOGI("enter");
639 
640     gapManager_ = std::make_unique<AvrcTgGapManager>();
641 
642     return gapManager_->RegisterSecurity();
643 }
644 
UnregisterSecurity(void)645 int AvrcpTgService::UnregisterSecurity(void)
646 {
647     HILOGI("enter");
648 
649     int result = gapManager_->UnregisterSecurity();
650     gapManager_ = nullptr;
651 
652     return result;
653 }
654 
RegisterService(void)655 int AvrcpTgService::RegisterService(void)
656 {
657     HILOGI("enter");
658 
659     sdpManager_ = std::make_unique<AvrcTgSdpManager>(features_ & AVRC_TG_SDP_ALL_SUPPORTED_FEATURES);
660 
661     return sdpManager_->RegisterService();
662 }
663 
UnregisterService(void)664 int AvrcpTgService::UnregisterService(void)
665 {
666     HILOGI("enter");
667 
668     int result = sdpManager_->UnregisterService();
669     sdpManager_ = nullptr;
670 
671     return result;
672 }
673 
EnableProfile(void)674 int AvrcpTgService::EnableProfile(void)
675 {
676     HILOGI("enter");
677 
678     /// Gets the size of the MTU.
679     int controlMtu = AVRC_TG_DEFAULT_CONTROL_MTU_SIZE;
680     int browseMtu = AVRC_TG_DEFAULT_BROWSE_MTU_SIZE;
681 
682     IAdapterConfig *config = AdapterConfig::GetInstance();
683     config->GetValue(SECTION_AVRCP_TG_SERVICE, PROPERTY_CONTROL_MTU, controlMtu);
684     config->GetValue(SECTION_AVRCP_TG_SERVICE, PROPERTY_BROWSE_MTU, browseMtu);
685 
686     profile_ = std::make_unique<AvrcTgProfile>(features_,
687         AVRC_TG_DEFAULT_BLUETOOTH_SIG_COMPANY_ID,
688         controlMtu,
689         browseMtu,
690         GetDispatcher(),
691         ChannelEventCallback,
692         ChannelMessageCallback);
693     profile_->RegisterObserver(pfObserver_.get());
694 
695     return profile_->Enable();
696 }
697 
DisableProfile(void) const698 int AvrcpTgService::DisableProfile(void) const
699 {
700     HILOGI("enter");
701 
702     return profile_->Disable();
703 }
704 
OnProfileDisabled(int result)705 void AvrcpTgService::OnProfileDisabled(int result)
706 {
707     HILOGI("result:%{public}d", result);
708 
709     SetServiceState(AVRC_TG_SERVICE_STATE_DISABLED);
710 
711     profile_->UnregisterObserver();
712     profile_ = nullptr;
713 
714     result |= UnregisterService();
715     result |= UnregisterSecurity();
716     stub::MediaService::GetInstance()->UnregisterObserver(mdObserver_.get());
717 
718     GetContext()->OnDisable(PROFILE_NAME_AVRCP_TG, result == BT_SUCCESS);
719 }
720 
IsEnabled(void)721 bool AvrcpTgService::IsEnabled(void)
722 {
723     bool enable = (state_ == AVRC_TG_SERVICE_STATE_ENABLED);
724     HILOGI("enable:%{public}d", enable);
725 
726     return enable;
727 }
728 
IsDisabled(void)729 bool AvrcpTgService::IsDisabled(void)
730 {
731     bool disable = (state_ == AVRC_TG_SERVICE_STATE_DISABLED);
732     HILOGI("disable:%{public}d", disable);
733 
734     return disable;
735 }
736 
SetServiceState(uint8_t state)737 void AvrcpTgService::SetServiceState(uint8_t state)
738 {
739     HILOGI("state:%{public}d", state);
740 
741     state_ = state;
742 }
743 
744 /******************************************************************
745  * CONNECTION                                                     *
746  ******************************************************************/
747 
SetActiveDevice(const RawAddress & rawAddr)748 void AvrcpTgService::SetActiveDevice(const RawAddress &rawAddr)
749 {
750     HILOGI("address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
751 
752     if (IsEnabled()) {
753         profile_->SetActiveDevice(rawAddr);
754         stub::MediaService::GetInstance()->SetActiveDevice(rawAddr.GetAddress());
755     }
756 }
757 
GetConnectedDevices(void)758 std::vector<RawAddress> AvrcpTgService::GetConnectedDevices(void)
759 {
760     HILOGI("enter");
761 
762     std::vector<RawAddress> result;
763 
764     if (IsEnabled()) {
765         result = profile_->GetConnectedDevices();
766     }
767 
768     return result;
769 }
770 
GetDevicesByStates(const std::vector<int> & states)771 std::vector<bluetooth::RawAddress> AvrcpTgService::GetDevicesByStates(const std::vector<int> &states)
772 {
773     HILOGI("enter");
774 
775     std::vector<bluetooth::RawAddress> result;
776 
777     if (IsEnabled()) {
778         result = profile_->GetDevicesByStates(states);
779     }
780 
781     return result;
782 }
783 
GetDeviceState(const RawAddress & rawAddr)784 int AvrcpTgService::GetDeviceState(const RawAddress &rawAddr)
785 {
786     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
787 
788     int result = static_cast<int>(BTConnectState::DISCONNECTED);
789 
790     if (IsEnabled()) {
791         result = profile_->GetDeviceState(rawAddr);
792     }
793 
794     HILOGI("result: %{public}d", result);
795 
796     return result;
797 }
798 
GetMaxConnectNum(void)799 int AvrcpTgService::GetMaxConnectNum(void)
800 {
801     HILOGI("enter");
802 
803     int result = 0;
804 
805     if (IsEnabled()) {
806         result = profile_->GetMaxConnectNum();
807     }
808 
809     return result;
810 }
811 
Connect(const RawAddress & rawAddr)812 int AvrcpTgService::Connect(const RawAddress &rawAddr)
813 {
814     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
815 
816     int result = RET_BAD_STATUS;
817 
818     do {
819         if (!IsEnabled()) {
820             break;
821         }
822 
823         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::DISCONNECTED)) {
824             break;
825         }
826 
827         if (!CheckConnectionNum()) {
828             break;
829         }
830 
831         RawAddress peerAddr(rawAddr.GetAddress());
832         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::ConnectNative, this, peerAddr));
833         result = BT_SUCCESS;
834     } while (false);
835 
836     return result;
837 }
838 
ConnectNative(RawAddress rawAddr)839 void AvrcpTgService::ConnectNative(RawAddress rawAddr)
840 {
841     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
842 
843     AcceptActiveConnect(rawAddr);
844 }
845 
Disconnect(const RawAddress & rawAddr)846 int AvrcpTgService::Disconnect(const RawAddress &rawAddr)
847 {
848     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
849 
850     int result = RET_BAD_STATUS;
851 
852     do {
853         if (!IsEnabled()) {
854             break;
855         }
856 
857         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
858             break;
859         }
860 
861         RawAddress peerAddr(rawAddr.GetAddress());
862         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::DisconnectNative, this, peerAddr));
863         result = BT_SUCCESS;
864     } while (false);
865 
866     return result;
867 }
868 
DisconnectNative(RawAddress rawAddr)869 void AvrcpTgService::DisconnectNative(RawAddress rawAddr)
870 {
871     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
872 
873     do {
874         if (!IsEnabled()) {
875             break;
876         }
877 
878         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
879             break;
880         }
881 
882         if (profile_->Disconnect(rawAddr) != BT_SUCCESS) {
883             HILOGI("Call AvrcTgProfile::Disconnect Failed Address%{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
884         }
885     } while (false);
886 }
887 
GetConnectState(void)888 int AvrcpTgService::GetConnectState(void)
889 {
890     HILOGI("enter");
891 
892     int result = PROFILE_STATE_DISCONNECTED;
893 
894     if (IsEnabled()) {
895         result = profile_->GetConnectState();
896     }
897 
898     return result;
899 }
900 
OnConnectionStateChanged(const RawAddress & rawAddr,int state)901 void AvrcpTgService::OnConnectionStateChanged(const RawAddress &rawAddr, int state)
902 {
903     HILOGI("Address: %{public}s, state: %{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), state);
904     std::lock_guard<std::recursive_mutex> lock(mutex_);
905     if (myObserver_ != nullptr) {
906         myObserver_->OnConnectionStateChanged(rawAddr, state);
907     }
908 }
909 
AcceptActiveConnect(const RawAddress & rawAddr)910 void AvrcpTgService::AcceptActiveConnect(const RawAddress &rawAddr)
911 {
912     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
913 
914     do {
915         if (!IsEnabled()) {
916             break;
917         }
918 
919         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::DISCONNECTED)) {
920             break;
921         }
922 
923         if (profile_->Connect(rawAddr) != BT_SUCCESS) {
924             HILOGI("Call Connect Failed! Address: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
925         }
926     } while (false);
927 }
928 
AcceptPassiveConnect(const RawAddress & rawAddr)929 void AvrcpTgService::AcceptPassiveConnect(const RawAddress &rawAddr)
930 {
931     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
932 
933     do {
934         if (!IsEnabled()) {
935             break;
936         }
937 
938         profile_->AcceptPassiveConnect(rawAddr);
939     } while (false);
940 }
941 
RejectPassiveConnect(const RawAddress & rawAddr)942 void AvrcpTgService::RejectPassiveConnect(const RawAddress &rawAddr)
943 {
944     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
945 
946     do {
947         if (!IsEnabled()) {
948             break;
949         }
950 
951         profile_->RejectPassiveConnect(rawAddr);
952     } while (false);
953 }
954 
FindCtService(const RawAddress & rawAddr)955 void AvrcpTgService::FindCtService(const RawAddress &rawAddr)
956 {
957     HILOGI("rawAddr: %{public}s", GET_ENCRYPT_AVRCP_ADDR(rawAddr));
958 
959     if (sdpManager_->FindCtService(rawAddr, FindCtServiceCallback) != BT_SUCCESS) {
960         RejectPassiveConnect(rawAddr);
961     }
962 }
963 
FindCtServiceCallback(const BtAddr * btAddr,const uint32_t * handleArray,uint16_t handleCount,void * context)964 void AvrcpTgService::FindCtServiceCallback(
965     const BtAddr *btAddr, const uint32_t *handleArray, uint16_t handleCount, void *context)
966 {
967     HILOGI("handleCount: %{public}d", handleCount);
968 
969     auto servManager = IProfileManager::GetInstance();
970     auto service = static_cast<AvrcpTgService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
971     RawAddress rawAddr(RawAddress::ConvertToString(btAddr->addr));
972     if (service != nullptr) {
973         if (handleCount > 0) {
974             service->GetDispatcher()->PostTask(std::bind(&AvrcpTgService::AcceptPassiveConnect, service, rawAddr));
975         } else {
976             service->GetDispatcher()->PostTask(std::bind(&AvrcpTgService::RejectPassiveConnect, service, rawAddr));
977         }
978     }
979 }
980 
981 #ifdef AVRCP_AVSESSION
982 /******************************************************************
983  * PASS THROUGH COMMAND                                           *
984  ******************************************************************/
SetAvControlCommand(uint8_t button,OHOS::AVSession::AVControlCommand & command,bool buttonStatus)985 static void SetAvControlCommand(uint8_t button, OHOS::AVSession::AVControlCommand &command, bool buttonStatus)
986 {
987     switch (button) {
988         case AVRC_KEY_OPERATION_VOLUME_UP:
989             break;
990         case AVRC_KEY_OPERATION_VOLUME_DOWN:
991             break;
992         case AVRC_KEY_OPERATION_MUTE:
993             break;
994         case AVRC_KEY_OPERATION_PLAY:
995             command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PLAY);
996             break;
997         case AVRC_KEY_OPERATION_STOP:
998             command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_STOP);
999             break;
1000         case AVRC_KEY_OPERATION_PAUSE:
1001             command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PAUSE);
1002             break;
1003         case AVRC_KEY_OPERATION_REWIND:
1004             if (!buttonStatus) {
1005                 command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_REWIND);
1006             } else {
1007                 command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PLAY);
1008             }
1009             break;
1010         case AVRC_KEY_OPERATION_FAST_FORWARD:
1011             if (!buttonStatus) {
1012                 command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_FAST_FORWARD);
1013             } else {
1014                 command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PLAY);
1015             }
1016             break;
1017         case AVRC_KEY_OPERATION_FORWARD:
1018             command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PLAY_NEXT);
1019             break;
1020         case AVRC_KEY_OPERATION_BACKWARD:
1021             command.SetCommand(OHOS::AVSession::AVControlCommand::SESSION_CMD_PLAY_PREVIOUS);
1022             break;
1023         default:
1024             break;
1025     }
1026 }
1027 #endif
OnButtonPressed(const RawAddress & rawAddr,uint8_t button,uint8_t label) const1028 void AvrcpTgService::OnButtonPressed(const RawAddress &rawAddr, uint8_t button, uint8_t label) const
1029 {
1030     HILOGI("rawAddr:%{public}s, button:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button, label);
1031 #ifndef AVRCP_AVSESSION
1032     int result = stub::MediaService::GetInstance()->PressButton(rawAddr.GetAddress(), button, label);
1033     profile_->SendPressButtonRsp(rawAddr, button, label, result);
1034 #else
1035     OHOS::AVSession::AVControlCommand command;
1036     SetAvControlCommand(button, command, false);
1037     profile_->SendPressButtonRsp(rawAddr, button, label, BT_SUCCESS);
1038 #endif
1039 }
1040 
OnButtonReleased(const RawAddress & rawAddr,uint8_t button,uint8_t label) const1041 void AvrcpTgService::OnButtonReleased(const RawAddress &rawAddr, uint8_t button, uint8_t label) const
1042 {
1043     HILOGI("rawAddr:%{public}s, button:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button, label);
1044 #ifndef AVRCP_AVSESSION
1045     int result = stub::MediaService::GetInstance()->ReleaseButton(rawAddr.GetAddress(), button, label);
1046 #else
1047     OHOS::AVSession::AVControlCommand command;
1048     SetAvControlCommand(button, command, true);
1049     int result = OHOS::AVSession::AVSessionManager::GetInstance().SendSystemControlCommand(command);
1050 #endif
1051     profile_->SendReleaseButtonRsp(rawAddr, button, label, result);
1052 }
1053 
HoldButton(const RawAddress & rawAddr,uint8_t button,uint8_t label) const1054 void AvrcpTgService::HoldButton(const RawAddress &rawAddr, uint8_t button, uint8_t label) const
1055 {
1056     HILOGI("rawAddr:%{public}s, button:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), button, label);
1057 
1058     int result = stub::MediaService::GetInstance()->PressButton(rawAddr.GetAddress(), button, label);
1059     profile_->SendPressButtonRsp(rawAddr, button, label, result);
1060 }
1061 
1062 /******************************************************************
1063  * Media Player Selection                                         *
1064  ******************************************************************/
1065 
SetAddressedPlayer(const RawAddress & rawAddr,uint16_t playerId,uint16_t uidCounter,uint8_t label) const1066 void AvrcpTgService::SetAddressedPlayer(
1067     const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, uint8_t label) const
1068 {
1069     HILOGI("rawAddr:%{public}s, playerId:%{public}d, uidCounter:%{public}d, label:%{public}d",
1070         GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId, uidCounter, label);
1071 
1072     int result =
1073         stub::MediaService::GetInstance()->SetAddressedPlayer(rawAddr.GetAddress(), playerId, uidCounter, label);
1074     if (result != BT_SUCCESS) {
1075         profile_->SendSetAddressedPlayerRsp(rawAddr, AVRC_ES_CODE_INTERNAL_ERROR, label, result);
1076     }
1077 }
1078 
OnSetAddressedPlayer(const RawAddress & rawAddr,uint8_t label,int status)1079 void AvrcpTgService::OnSetAddressedPlayer(const RawAddress &rawAddr, uint8_t label, int status)
1080 {
1081     HILOGI("rawAddr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1082     do {
1083         if (!IsEnabled()) {
1084             break;
1085         }
1086 
1087         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1088             break;
1089         }
1090 
1091         RawAddress peerAddr(rawAddr.GetAddress());
1092         GetDispatcher()->PostTask(
1093             std::bind(&AvrcpTgService::OnSetAddressedPlayerNative, this, peerAddr, label, status));
1094     } while (false);
1095 }
1096 
OnSetAddressedPlayerNative(RawAddress rawAddr,uint8_t label,int status)1097 void AvrcpTgService::OnSetAddressedPlayerNative(RawAddress rawAddr, uint8_t label, int status)
1098 {
1099     HILOGI("rawAddr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1100 
1101     do {
1102         if (!IsEnabled()) {
1103             break;
1104         }
1105 
1106         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1107             break;
1108         }
1109 
1110         profile_->SendSetAddressedPlayerRsp(rawAddr, status, label, BT_SUCCESS);
1111     } while (false);
1112 }
1113 
SetBrowsedPlayer(const RawAddress & rawAddr,uint16_t playerId,uint16_t uidCounter,uint8_t label) const1114 void AvrcpTgService::SetBrowsedPlayer(const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter,
1115     uint8_t label) const
1116 {
1117     HILOGI("rawAddr:%{public}s, playerId:%{public}d, uidCounter:%{public}d, label:%{public}d",
1118         GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId, uidCounter, label);
1119 
1120     int result = stub::MediaService::GetInstance()->SetBrowsedPlayer(rawAddr.GetAddress(), playerId, uidCounter, label);
1121     if (result != BT_SUCCESS) {
1122         std::vector<std::string> folderNames;
1123         profile_->SendSetBrowsedPlayerRsp(rawAddr, 0x00, 0x00, folderNames, label, AVRC_ES_CODE_INTERNAL_ERROR);
1124     }
1125 }
1126 
OnSetBrowsedPlayer(const RawAddress & rawAddr,uint16_t uidCounter,uint32_t numOfItems,const std::vector<std::string> & folderNames,uint8_t label,int status)1127 void AvrcpTgService::OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems,
1128     const std::vector<std::string> &folderNames, uint8_t label, int status)
1129 {
1130     HILOGI("rawAddr:%{public}s, uidCounter:%{public}d, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1131         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems, label, status);
1132     do {
1133         if (!IsEnabled()) {
1134             break;
1135         }
1136 
1137         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1138             break;
1139         }
1140 
1141         RawAddress peerAddr(rawAddr.GetAddress());
1142         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnSetBrowsedPlayerNative,
1143             this,
1144             peerAddr,
1145             uidCounter,
1146             numOfItems,
1147             folderNames,
1148             label,
1149             status));
1150     } while (false);
1151 }
1152 
OnSetBrowsedPlayerNative(RawAddress rawAddr,uint16_t uidCounter,uint32_t numOfItems,std::vector<std::string> folderNames,uint8_t label,int status)1153 void AvrcpTgService::OnSetBrowsedPlayerNative(RawAddress rawAddr, uint16_t uidCounter, uint32_t numOfItems,
1154     std::vector<std::string> folderNames, uint8_t label, int status)
1155 {
1156     HILOGI("rawAddr:%{public}s, uidCounter:%{public}d, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1157         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems, label, status);
1158     do {
1159         if (!IsEnabled()) {
1160             break;
1161         }
1162 
1163         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1164             break;
1165         }
1166 
1167         profile_->SendSetBrowsedPlayerRsp(rawAddr, uidCounter, numOfItems, folderNames, label, status);
1168     } while (false);
1169 }
1170 
1171 /******************************************************************
1172  * Capabilities                                                   *
1173  ******************************************************************/
GetCapabilities(const RawAddress & rawAddr,uint8_t label)1174 void AvrcpTgService::GetCapabilities(const RawAddress &rawAddr, uint8_t label)
1175 {
1176     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1177 #ifndef AVRCP_AVSESSION
1178     std::vector<uint8_t> events = stub::MediaService::GetInstance()->GetCapabilities(rawAddr.GetAddress(), label);
1179 #else
1180     std::vector<uint8_t> events;
1181     events.push_back(AVRC_EVENT_ID_PLAYBACK_STATUS_CHANGED);
1182     events.push_back(AVRC_EVENT_ID_TRACK_CHANGED);
1183 #endif
1184     do {
1185         if (!IsEnabled()) {
1186             break;
1187         }
1188 
1189         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1190             break;
1191         }
1192 
1193         profile_->SendGetCapabilitiesRsp(rawAddr, events, label, BT_SUCCESS);
1194     } while (false);
1195 }
1196 
1197 /******************************************************************
1198  * PLAYER APPLICATION SETTINGS                                    *
1199  ******************************************************************/
1200 
GetPlayerAppSettingAttributes(const RawAddress & rawAddr,uint8_t label) const1201 void AvrcpTgService::GetPlayerAppSettingAttributes(const RawAddress &rawAddr, uint8_t label) const
1202 {
1203     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1204 
1205     int result = stub::MediaService::GetInstance()->GetPlayerAppSettingAttributes(rawAddr.GetAddress(), label);
1206     if (result != BT_SUCCESS) {
1207         std::deque<uint8_t> attribtues;
1208         profile_->SendListPlayerApplicationSettingAttributesRsp(rawAddr, attribtues, label, result);
1209     }
1210 }
1211 
OnGetPlayerAppSettingAttributes(const RawAddress & rawAddr,const std::deque<uint8_t> & attributes,uint8_t label)1212 void AvrcpTgService::OnGetPlayerAppSettingAttributes(
1213     const RawAddress &rawAddr, const std::deque<uint8_t> &attributes, uint8_t label)
1214 {
1215     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1216 
1217     do {
1218         if (!IsEnabled()) {
1219             break;
1220         }
1221 
1222         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1223             break;
1224         }
1225 
1226         RawAddress peerAddr(rawAddr.GetAddress());
1227         GetDispatcher()->PostTask(
1228             std::bind(&AvrcpTgService::OnGetPlayerAppSettingAttributesNative, this, peerAddr, attributes, label));
1229     } while (false);
1230 }
1231 
OnGetPlayerAppSettingAttributesNative(RawAddress rawAddr,std::deque<uint8_t> attributes,uint8_t label)1232 void AvrcpTgService::OnGetPlayerAppSettingAttributesNative(
1233     RawAddress rawAddr, std::deque<uint8_t> attributes, uint8_t label)
1234 {
1235     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1236 
1237     do {
1238         if (!IsEnabled()) {
1239             break;
1240         }
1241 
1242         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1243             break;
1244         }
1245 
1246         profile_->SendListPlayerApplicationSettingAttributesRsp(rawAddr, attributes, label, BT_SUCCESS);
1247     } while (false);
1248 }
1249 
GetPlayerAppSettingValues(const RawAddress & rawAddr,uint8_t attribute,uint8_t label) const1250 void AvrcpTgService::GetPlayerAppSettingValues(const RawAddress &rawAddr, uint8_t attribute, uint8_t label) const
1251 {
1252     HILOGI("rawAddr:%{public}s, attribute:%{public}d, label:%{public}d",
1253         GET_ENCRYPT_AVRCP_ADDR(rawAddr), attribute, label);
1254 
1255     int result = stub::MediaService::GetInstance()->GetPlayerAppSettingValues(rawAddr.GetAddress(), attribute, label);
1256     if (result != BT_SUCCESS) {
1257         std::deque<uint8_t> values;
1258         profile_->SendListPlayerApplicationSettingValuesRsp(rawAddr, values, label, result);
1259     }
1260 }
1261 
OnGetPlayerAppSettingValues(const RawAddress & rawAddr,const std::deque<uint8_t> & values,uint8_t label)1262 void AvrcpTgService::OnGetPlayerAppSettingValues(
1263     const RawAddress &rawAddr, const std::deque<uint8_t> &values, uint8_t label)
1264 {
1265     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1266 
1267     do {
1268         if (!IsEnabled()) {
1269             break;
1270         }
1271 
1272         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1273             break;
1274         }
1275 
1276         RawAddress peerAddr(rawAddr.GetAddress());
1277         GetDispatcher()->PostTask(
1278             std::bind(&AvrcpTgService::OnGetPlayerAppSettingValuesNative, this, peerAddr, values, label));
1279     } while (false);
1280 }
1281 
OnGetPlayerAppSettingValuesNative(RawAddress rawAddr,std::deque<uint8_t> values,uint8_t label)1282 void AvrcpTgService::OnGetPlayerAppSettingValuesNative(RawAddress rawAddr, std::deque<uint8_t> values, uint8_t label)
1283 {
1284     HILOGI("rawAddr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1285 
1286     do {
1287         if (!IsEnabled()) {
1288             break;
1289         }
1290 
1291         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1292             break;
1293         }
1294 
1295         profile_->SendListPlayerApplicationSettingValuesRsp(rawAddr, values, label, BT_SUCCESS);
1296     } while (false);
1297 }
1298 
GetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::deque<uint8_t> & attributes,uint8_t label,uint8_t context) const1299 void AvrcpTgService::GetPlayerAppSettingCurrentValue(
1300     const RawAddress &rawAddr, const std::deque<uint8_t> &attributes, uint8_t label, uint8_t context) const
1301 {
1302     HILOGI("addr:%{public}s, label:%{public}d, context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, context);
1303 
1304     int result = stub::MediaService::GetInstance()->GetPlayerAppSettingCurrentValue(
1305         rawAddr.GetAddress(), attributes, label, context);
1306     if (result != BT_SUCCESS) {
1307         std::deque<uint8_t> attrs;
1308         std::deque<uint8_t> vals;
1309         if (context == AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_CURRENT_VALUE) {
1310             profile_->SendGetCurrentPlayerApplicationSettingValueRsp(rawAddr, attrs, vals, label, result);
1311         } else {
1312             profile_->SendPlayerApplicationSettingChangedRsp(true, attrs, vals, label, result);
1313         }
1314     }
1315 }
1316 
OnGetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::deque<uint8_t> & attributes,const std::deque<uint8_t> & values,uint8_t label,uint8_t context)1317 void AvrcpTgService::OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::deque<uint8_t> &attributes,
1318     const std::deque<uint8_t> &values, uint8_t label, uint8_t context)
1319 {
1320     HILOGI("addr:%{public}s, label:%{public}d, context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, context);
1321 
1322     do {
1323         if (!IsEnabled()) {
1324             break;
1325         }
1326 
1327         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1328             break;
1329         }
1330 
1331         RawAddress peerAddr(rawAddr.GetAddress());
1332         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnGetCurrentPlayerAppSettingValueNative,
1333             this,
1334             peerAddr,
1335             attributes,
1336             values,
1337             label,
1338             context));
1339     } while (false);
1340 }
1341 
OnGetCurrentPlayerAppSettingValueNative(RawAddress rawAddr,std::deque<uint8_t> attributes,std::deque<uint8_t> values,uint8_t label,uint8_t context)1342 void AvrcpTgService::OnGetCurrentPlayerAppSettingValueNative(
1343     RawAddress rawAddr, std::deque<uint8_t> attributes, std::deque<uint8_t> values, uint8_t label, uint8_t context)
1344 {
1345     HILOGI("addr:%{public}s, label:%{public}d, context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, context);
1346 
1347     do {
1348         if (!IsEnabled()) {
1349             break;
1350         }
1351 
1352         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1353             break;
1354         }
1355 
1356         if (context == AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_CURRENT_VALUE) {
1357             profile_->SendGetCurrentPlayerApplicationSettingValueRsp(rawAddr, attributes, values, label, BT_SUCCESS);
1358         } else {
1359             profile_->SendPlayerApplicationSettingChangedRsp(true, attributes, values, label, BT_SUCCESS);
1360         }
1361     } while (false);
1362 }
1363 
SetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,const std::deque<uint8_t> & attributes,const std::deque<uint8_t> & values,uint8_t label) const1364 void AvrcpTgService::SetPlayerAppSettingCurrentValue(
1365     const RawAddress &rawAddr, const std::deque<uint8_t> &attributes,
1366     const std::deque<uint8_t> &values, uint8_t label) const
1367 {
1368     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1369 
1370     int result = stub::MediaService::GetInstance()->SetPlayerAppSettingCurrentValue(
1371         rawAddr.GetAddress(), attributes, values, label);
1372     if (result != BT_SUCCESS) {
1373         profile_->SendSetPlayerApplicationSettingValueRsp(rawAddr, label, result);
1374     }
1375 }
1376 
OnSetPlayerAppSettingCurrentValue(const RawAddress & rawAddr,uint8_t label)1377 void AvrcpTgService::OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, uint8_t label)
1378 {
1379     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1380 
1381     do {
1382         if (!IsEnabled()) {
1383             break;
1384         }
1385 
1386         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1387             break;
1388         }
1389 
1390         RawAddress peerAddr(rawAddr.GetAddress());
1391         GetDispatcher()->PostTask(
1392             std::bind(&AvrcpTgService::OnSetPlayerAppSettingCurrentValueNative, this, peerAddr, label));
1393     } while (false);
1394 }
1395 
OnSetPlayerAppSettingCurrentValueNative(RawAddress rawAddr,uint8_t label)1396 void AvrcpTgService::OnSetPlayerAppSettingCurrentValueNative(RawAddress rawAddr, uint8_t label)
1397 {
1398     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1399 
1400     do {
1401         if (!IsEnabled()) {
1402             break;
1403         }
1404 
1405         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1406             break;
1407         }
1408 
1409         profile_->SendSetPlayerApplicationSettingValueRsp(rawAddr, label, BT_SUCCESS);
1410     } while (false);
1411 }
1412 
GetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,uint8_t label) const1413 void AvrcpTgService::GetPlayerAppSettingAttributeText(
1414     const RawAddress &rawAddr, const std::vector<uint8_t> &attributes, uint8_t label) const
1415 {
1416     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1417 
1418     int result =
1419         stub::MediaService::GetInstance()->GetPlayerAppSettingAttributeText(rawAddr.GetAddress(), attributes, label);
1420     if (result != BT_SUCCESS) {
1421         profile_->SendSetPlayerApplicationSettingValueRsp(rawAddr, label, result);
1422     }
1423 }
1424 
OnGetPlayerAppSettingAttributeText(const RawAddress & rawAddr,const std::vector<uint8_t> & attributes,const std::vector<std::string> & attrStr,uint8_t label)1425 void AvrcpTgService::OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr,
1426     const std::vector<uint8_t> &attributes, const std::vector<std::string> &attrStr, uint8_t label)
1427 {
1428     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1429 
1430     do {
1431         if (!IsEnabled()) {
1432             break;
1433         }
1434 
1435         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1436             break;
1437         }
1438 
1439         RawAddress peerAddr(rawAddr.GetAddress());
1440         GetDispatcher()->PostTask(std::bind(
1441             &AvrcpTgService::OnGetPlayerAppSettingAttributeTextNative, this, peerAddr, attributes, attrStr, label));
1442     } while (false);
1443 }
1444 
OnGetPlayerAppSettingAttributeTextNative(RawAddress rawAddr,std::vector<uint8_t> attributes,std::vector<std::string> attrStr,uint8_t label)1445 void AvrcpTgService::OnGetPlayerAppSettingAttributeTextNative(
1446     RawAddress rawAddr, std::vector<uint8_t> attributes, std::vector<std::string> attrStr, uint8_t label)
1447 {
1448     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1449 
1450     do {
1451         if (!IsEnabled()) {
1452             break;
1453         }
1454 
1455         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1456             break;
1457         }
1458 
1459         profile_->SendGetPlayerApplicationSettingAttributeTextRsp(rawAddr, attributes, attrStr, label, BT_SUCCESS);
1460     } while (false);
1461 }
1462 
GetPlayerAppSettingValueText(const RawAddress & rawAddr,uint8_t attributeId,const std::vector<uint8_t> & values,uint8_t label) const1463 void AvrcpTgService::GetPlayerAppSettingValueText(
1464     const RawAddress &rawAddr, uint8_t attributeId, const std::vector<uint8_t> &values, uint8_t label) const
1465 {
1466     HILOGI("addr:%{public}s, attributeId:%{public}d, label:%{public}d",
1467         GET_ENCRYPT_AVRCP_ADDR(rawAddr), attributeId, label);
1468 
1469     int result = stub::MediaService::GetInstance()->GetPlayerAppSettingValueText(
1470         rawAddr.GetAddress(), attributeId, values, label);
1471     if (result != BT_SUCCESS) {
1472         profile_->SendSetPlayerApplicationSettingValueRsp(rawAddr, label, result);
1473     }
1474 }
1475 
OnGetPlayerAppSettingValueText(const RawAddress & rawAddr,const std::vector<uint8_t> & values,const std::vector<std::string> & valueStr,uint8_t label)1476 void AvrcpTgService::OnGetPlayerAppSettingValueText(const RawAddress &rawAddr, const std::vector<uint8_t> &values,
1477     const std::vector<std::string> &valueStr, uint8_t label)
1478 {
1479     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1480 
1481     do {
1482         if (!IsEnabled()) {
1483             break;
1484         }
1485 
1486         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1487             break;
1488         }
1489 
1490         RawAddress peerAddr(rawAddr.GetAddress());
1491         GetDispatcher()->PostTask(
1492             std::bind(&AvrcpTgService::OnGetPlayerAppSettingValueTextNative, this, peerAddr, values, valueStr, label));
1493     } while (false);
1494 }
1495 
OnGetPlayerAppSettingValueTextNative(RawAddress rawAddr,std::vector<uint8_t> values,std::vector<std::string> valueStr,uint8_t label)1496 void AvrcpTgService::OnGetPlayerAppSettingValueTextNative(
1497     RawAddress rawAddr, std::vector<uint8_t> values, std::vector<std::string> valueStr, uint8_t label)
1498 {
1499     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1500 
1501     do {
1502         if (!IsEnabled()) {
1503             break;
1504         }
1505 
1506         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1507             break;
1508         }
1509 
1510         profile_->SendGetPlayerApplicationSettingValueTextRsp(rawAddr, values, valueStr, label, BT_SUCCESS);
1511     } while (false);
1512 }
1513 
1514 /******************************************************************
1515  * MEDIA INFORMATION PDUS                                         *
1516  ******************************************************************/
1517 
GetElementAttributes(const RawAddress & rawAddr,uint64_t identifier,const std::vector<uint32_t> & attributes,uint8_t label) const1518 void AvrcpTgService::GetElementAttributes(
1519     const RawAddress &rawAddr, uint64_t identifier, const std::vector<uint32_t> &attributes, uint8_t label) const
1520 {
1521     HILOGI("addr:%{public}s, identifier:%{public}llu, label:%{public}d",
1522         GET_ENCRYPT_AVRCP_ADDR(rawAddr), static_cast<unsigned long long>(identifier), label);
1523     int result =
1524         stub::MediaService::GetInstance()->GetElementAttributes(rawAddr.GetAddress(), identifier, attributes, label);
1525     if (result != BT_SUCCESS) {
1526         std::vector<std::string> values;
1527         profile_->SendGetElementAttributesRsp(rawAddr, attributes, values, label, result);
1528     }
1529 }
1530 
OnGetElementAttributes(const RawAddress & rawAddr,const std::vector<uint32_t> & attribtues,const std::vector<std::string> & values,uint8_t label)1531 void AvrcpTgService::OnGetElementAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attribtues,
1532     const std::vector<std::string> &values, uint8_t label)
1533 {
1534     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1535 
1536     do {
1537         if (!IsEnabled()) {
1538             break;
1539         }
1540         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1541             break;
1542         }
1543         RawAddress peerAddr(rawAddr.GetAddress());
1544         GetDispatcher()->PostTask(
1545             std::bind(&AvrcpTgService::OnGetElementAttributesNative, this, peerAddr, attribtues, values, label));
1546     } while (false);
1547 }
1548 
OnGetElementAttributesNative(RawAddress rawAddr,std::vector<uint32_t> attributes,std::vector<std::string> values,uint8_t label)1549 void AvrcpTgService::OnGetElementAttributesNative(
1550     RawAddress rawAddr, std::vector<uint32_t> attributes, std::vector<std::string> values, uint8_t label)
1551 {
1552     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
1553 
1554     do {
1555         if (!IsEnabled()) {
1556             break;
1557         }
1558 
1559         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1560             break;
1561         }
1562 
1563         profile_->SendGetElementAttributesRsp(rawAddr, attributes, values, label, BT_SUCCESS);
1564     } while (false);
1565 }
1566 
1567 /******************************************************************
1568  * PLAY                                                           *
1569  ******************************************************************/
1570 
GetPlayStatus(const RawAddress & rawAddr,uint8_t label,uint8_t context) const1571 void AvrcpTgService::GetPlayStatus(const RawAddress &rawAddr, uint8_t label, uint8_t context) const
1572 {
1573     HILOGI("addr:%{public}s, label:%{public}d, context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, context);
1574 #ifndef AVRCP_AVSESSION
1575     int result = stub::MediaService::GetInstance()->GetPlayStatus(rawAddr.GetAddress(), label, context);
1576     if (result != BT_SUCCESS) {
1577         if (context == AVRC_ACTION_TYPE_GET_PLAY_STATUS) {
1578             profile_->SendGetPlayStatusRsp(rawAddr,
1579                 AVRC_PLAY_STATUS_INVALID_SONG_LENGTH,
1580                 AVRC_PLAY_STATUS_INVALID_SONG_POSITION,
1581                 AVRC_PLAY_STATUS_ERROR,
1582                 label,
1583                 result);
1584         } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_STATUS_CHANGED) {
1585             profile_->SendPlaybackStatusChangedRsp(true, AVRC_PLAY_STATUS_ERROR, label, result);
1586         } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_POS_CHANGED) {
1587             profile_->SendPlaybackPosChangedRsp(true, AVRC_PLAY_STATUS_INVALID_SONG_POSITION, label, result);
1588         }
1589 #else
1590     OHOS::AVSession::AVPlaybackState state;
1591     if (avSessionController_ == NULL) {
1592         HILOGE("avSessionController_ is NULL");
1593         return;
1594     }
1595     int32_t result = avSessionController_->GetAVPlaybackState(state);
1596     uint8_t playstate = AvrcpTgService::ConvertPlayState(state.GetState());
1597 
1598     if (context == AVRC_ACTION_TYPE_GET_PLAY_STATUS) {
1599         profile_->SendGetPlayStatusRsp(rawAddr,
1600             AVRC_PLAY_STATUS_INVALID_SONG_LENGTH,
1601             AVRC_PLAY_STATUS_INVALID_SONG_POSITION,
1602             AVRC_PLAY_STATUS_ERROR,
1603             label,
1604             result);
1605     } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_STATUS_CHANGED) {
1606         profile_->SendPlaybackStatusChangedRsp(true, playstate, label, result);
1607     } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_POS_CHANGED) {
1608         profile_->SendPlaybackPosChangedRsp(true, AVRC_PLAY_STATUS_INVALID_SONG_POSITION, label, result);
1609 #endif
1610     }
1611 }
1612 
1613 void AvrcpTgService::OnGetPlayStatus(const RawAddress &rawAddr, uint32_t songLength, uint32_t songPosition,
1614     uint8_t playStatus, uint8_t label, uint8_t context)
1615 {
1616     HILOGI("addr:%{public}s, songLength:%{public}u, songPosition:%{public}u, playStatus:%{public}d, label:%{public}d,"
1617         " context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), songLength, songPosition, playStatus, label, context);
1618 
1619     do {
1620         if (!IsEnabled()) {
1621             break;
1622         }
1623 
1624         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1625             break;
1626         }
1627 
1628         RawAddress peerAddr(rawAddr.GetAddress());
1629         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnGetPlayStatusNative,
1630             this,
1631             peerAddr,
1632             songLength,
1633             songPosition,
1634             playStatus,
1635             label,
1636             context));
1637     } while (false);
1638 }
1639 
1640 void AvrcpTgService::OnGetPlayStatusNative(
1641     RawAddress rawAddr, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, uint8_t label, uint8_t context)
1642 {
1643     HILOGI("addr:%{public}s, songLength:%{public}u, songPosition:%{public}u, playStatus:%{public}d, label:%{public}d,"
1644         " context:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), songLength, songPosition, playStatus, label, context);
1645     do {
1646         if (!IsEnabled()) {
1647             break;
1648         }
1649 
1650         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1651             break;
1652         }
1653 
1654         if (context == AVRC_ACTION_TYPE_GET_PLAY_STATUS) {
1655             profile_->SendGetPlayStatusRsp(rawAddr, songLength, songPosition, playStatus, label, BT_SUCCESS);
1656         } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_STATUS_CHANGED) {
1657             profile_->SendPlaybackStatusChangedRsp(true, AVRC_PLAY_STATUS_ERROR, label, BT_SUCCESS);
1658         } else if (context == AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_POS_CHANGED) {
1659             profile_->SendPlaybackPosChangedRsp(true, AVRC_PLAY_STATUS_INVALID_SONG_POSITION, label, BT_SUCCESS);
1660         }
1661     } while (false);
1662 }
1663 
1664 void AvrcpTgService::PlayItem(
1665     const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter, uint8_t label) const
1666 {
1667     HILOGI("addr:%{public}s, scope:%{public}d, uid:%{public}llu, uidCounter:%{public}d, label:%{public}d",
1668         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter, label);
1669     int result = stub::MediaService::GetInstance()->PlayItem(rawAddr.GetAddress(), scope, uid, uidCounter, label);
1670     if (result != BT_SUCCESS) {
1671         profile_->SendPlayItemRsp(rawAddr, AVRC_ES_CODE_INTERNAL_ERROR, label, result);
1672     }
1673 }
1674 
1675 void AvrcpTgService::OnPlayItem(const RawAddress &rawAddr, uint8_t label, int status)
1676 {
1677     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1678 
1679     do {
1680         if (!IsEnabled()) {
1681             break;
1682         }
1683         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1684             break;
1685         }
1686         RawAddress peerAddr(rawAddr.GetAddress());
1687         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnPlayItemNative, this, peerAddr, label, status));
1688     } while (false);
1689 }
1690 
1691 void AvrcpTgService::OnPlayItemNative(RawAddress rawAddr, uint8_t label, int status)
1692 {
1693     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1694 
1695     do {
1696         if (!IsEnabled()) {
1697             break;
1698         }
1699 
1700         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1701             break;
1702         }
1703 
1704         profile_->SendPlayItemRsp(rawAddr, status, label, BT_SUCCESS);
1705     } while (false);
1706 }
1707 
1708 void AvrcpTgService::AddToNowPlaying(
1709     const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter, uint8_t label) const
1710 {
1711     HILOGI("addr:%{public}s, scope:%{public}d, uid:%{public}llu, uidCounter:%{public}d, label:%{public}d",
1712         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter, label);
1713     int result =
1714         stub::MediaService::GetInstance()->AddToNowPlaying(rawAddr.GetAddress(), scope, uid, uidCounter, label);
1715     if (result != BT_SUCCESS) {
1716         profile_->SendAddToNowPlayingRsp(rawAddr, AVRC_ES_CODE_INTERNAL_ERROR, label, result);
1717     }
1718 }
1719 
1720 void AvrcpTgService::OnAddToNowPlaying(const RawAddress &rawAddr, uint8_t label, int status)
1721 {
1722     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1723 
1724     do {
1725         if (!IsEnabled()) {
1726             break;
1727         }
1728 
1729         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1730             break;
1731         }
1732 
1733         RawAddress peerAddr(rawAddr.GetAddress());
1734         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnAddToNowPlayingNative, this, peerAddr, label, status));
1735     } while (false);
1736 }
1737 
1738 void AvrcpTgService::OnAddToNowPlayingNative(RawAddress rawAddr, uint8_t label, int status)
1739 {
1740     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1741 
1742     do {
1743         if (!IsEnabled()) {
1744             break;
1745         }
1746 
1747         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1748             break;
1749         }
1750 
1751         profile_->SendAddToNowPlayingRsp(rawAddr, status, label, BT_SUCCESS);
1752     } while (false);
1753 }
1754 
1755 /******************************************************************
1756  * OPERATE THE VIRTUAL FILE SYSTEM                                *
1757  ******************************************************************/
1758 
1759 void AvrcpTgService::ChangePath(
1760     const RawAddress &rawAddr, uint16_t uidCounter, uint8_t direction, uint64_t folderUid, uint8_t label) const
1761 {
1762     HILOGI("addr:%{public}s, uidCounter:%{public}d, direction:%{public}d, folderUid:%{public}llu, label:%{public}d",
1763         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, direction, static_cast<unsigned long long>(folderUid), label);
1764     int result =
1765         stub::MediaService::GetInstance()->ChangePath(rawAddr.GetAddress(), uidCounter, direction, folderUid, label);
1766     if (result != BT_SUCCESS) {
1767         profile_->SendChangePathRsp(rawAddr, AVRC_TG_CP_NUMBER_OF_ITEMS, label, AVRC_ES_CODE_INTERNAL_ERROR);
1768     }
1769 }
1770 
1771 void AvrcpTgService::OnChangePath(const RawAddress &rawAddr, uint32_t numOfItems, uint8_t label, int status)
1772 {
1773     HILOGI("addr:%{public}s, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1774         GET_ENCRYPT_AVRCP_ADDR(rawAddr), numOfItems, label, status);
1775 
1776     do {
1777         if (!IsEnabled()) {
1778             break;
1779         }
1780 
1781         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1782             break;
1783         }
1784 
1785         RawAddress peerAddr(rawAddr.GetAddress());
1786         GetDispatcher()->PostTask(
1787             std::bind(&AvrcpTgService::OnChangePathNative, this, peerAddr, numOfItems, label, status));
1788     } while (false);
1789 }
1790 
1791 void AvrcpTgService::OnChangePathNative(RawAddress rawAddr, uint32_t numOfItems, uint8_t label, int status)
1792 {
1793     HILOGI("addr:%{public}s, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1794         GET_ENCRYPT_AVRCP_ADDR(rawAddr), numOfItems, label, status);
1795     do {
1796         if (!IsEnabled()) {
1797             break;
1798         }
1799 
1800         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1801             break;
1802         }
1803 
1804         profile_->SendChangePathRsp(rawAddr, numOfItems, label, status);
1805     } while (false);
1806 }
1807 
1808 void AvrcpTgService::GetFolderItems(const RawAddress &rawAddr, uint8_t scope, uint32_t startItem, uint32_t endItem,
1809     const std::vector<uint32_t> &attributes, uint8_t label) const
1810 {
1811     HILOGI("addr:%{public}s, scope:%{public}d, startItem:%{public}u, endItem:%{public}u, label:%{public}d",
1812         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, startItem, endItem, label);
1813     int result = stub::MediaService::GetInstance()->GetFolderItems(
1814         rawAddr.GetAddress(), scope, startItem, endItem, attributes, label);
1815     if (result != BT_SUCCESS) {
1816         if (scope == AVRC_MEDIA_SCOPE_PLAYER_LIST) {
1817             std::vector<AvrcMpItem> items;
1818             profile_->SendGetFolderItemsRsp(rawAddr, 0x00, items, label, AVRC_ES_CODE_INTERNAL_ERROR);
1819         } else {
1820             std::vector<AvrcMeItem> items;
1821             profile_->SendGetFolderItemsRsp(rawAddr, 0x00, items, label, AVRC_ES_CODE_INTERNAL_ERROR);
1822         }
1823     }
1824 }
1825 
1826 void AvrcpTgService::OnGetMediaPlayers(
1827     const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMpItem> &items, uint8_t label, int status)
1828 {
1829     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d, status:%{public}d",
1830         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label, status);
1831     do {
1832         if (!IsEnabled()) {
1833             break;
1834         }
1835 
1836         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1837             break;
1838         }
1839 
1840         RawAddress peerAddr(rawAddr.GetAddress());
1841         GetDispatcher()->PostTask(
1842             std::bind(&AvrcpTgService::OnGetMediaPlayersNative, this, peerAddr, uidCounter, items, label, status));
1843     } while (false);
1844 }
1845 
1846 void AvrcpTgService::OnGetMediaPlayersNative(
1847     RawAddress rawAddr, uint16_t uidCounter, std::vector<AvrcMpItem> items, uint8_t label, int status)
1848 {
1849     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d, status:%{public}d",
1850         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label, status);
1851     do {
1852         if (!IsEnabled()) {
1853             break;
1854         }
1855 
1856         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1857             break;
1858         }
1859 
1860         profile_->SendGetFolderItemsRsp(rawAddr, uidCounter, items, label, status);
1861     } while (false);
1862 }
1863 
1864 void AvrcpTgService::OnGetFolderItems(
1865     const RawAddress &rawAddr, uint16_t uidCounter, const std::vector<AvrcMeItem> &items, uint8_t label, int status)
1866 {
1867     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d, status:%{public}d",
1868         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label, status);
1869     do {
1870         if (!IsEnabled()) {
1871             break;
1872         }
1873 
1874         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1875             break;
1876         }
1877 
1878         RawAddress peerAddr(rawAddr.GetAddress());
1879         GetDispatcher()->PostTask(
1880             std::bind(&AvrcpTgService::OnGetFolderItemsNative, this, peerAddr, uidCounter, items, label, status));
1881     } while (false);
1882 }
1883 
1884 void AvrcpTgService::OnGetFolderItemsNative(
1885     RawAddress rawAddr, uint16_t uidCounter, std::vector<AvrcMeItem> items, uint8_t label, int status)
1886 {
1887     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d, status:%{public}d",
1888         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label, status);
1889     do {
1890         if (!IsEnabled()) {
1891             break;
1892         }
1893 
1894         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1895             break;
1896         }
1897 
1898         profile_->SendGetFolderItemsRsp(rawAddr, uidCounter, items, label, status);
1899     } while (false);
1900 }
1901 
1902 void AvrcpTgService::GetItemAttributes(const RawAddress &rawAddr, uint8_t scope, uint64_t uid, uint16_t uidCounter,
1903     std::vector<uint32_t> attributes, uint8_t label) const
1904 {
1905     HILOGI("addr:%{public}s, scope:%{public}d, uid:%{public}llu, uidCounter:%{public}d, label:%{public}d",
1906         GET_ENCRYPT_AVRCP_ADDR(rawAddr), scope, static_cast<unsigned long long>(uid), uidCounter, label);
1907     int result = stub::MediaService::GetInstance()->GetItemAttributes(
1908         rawAddr.GetAddress(), scope, uid, uidCounter, attributes, label);
1909     if (result != BT_SUCCESS) {
1910         std::vector<uint32_t> attrs;
1911         std::vector<std::string> vals;
1912         profile_->SendGetItemAttributesRsp(rawAddr, attrs, vals, label, AVRC_ES_CODE_INTERNAL_ERROR);
1913     }
1914 }
1915 
1916 void AvrcpTgService::OnGetItemAttributes(const RawAddress &rawAddr, const std::vector<uint32_t> &attributes,
1917     const std::vector<std::string> &values, uint8_t label, int status)
1918 {
1919     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1920     do {
1921         if (!IsEnabled()) {
1922             break;
1923         }
1924 
1925         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1926             break;
1927         }
1928 
1929         RawAddress peerAddr(rawAddr.GetAddress());
1930         GetDispatcher()->PostTask(
1931             std::bind(&AvrcpTgService::OnGetItemAttributesNative, this, peerAddr, attributes, values, label, status));
1932     } while (false);
1933 }
1934 
1935 void AvrcpTgService::OnGetItemAttributesNative(
1936     RawAddress rawAddr, std::vector<uint32_t> attributes, std::vector<std::string> values, uint8_t label, int status)
1937 {
1938     HILOGI("addr:%{public}s, label:%{public}d, status:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label, status);
1939 
1940     do {
1941         if (!IsEnabled()) {
1942             break;
1943         }
1944 
1945         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1946             break;
1947         }
1948 
1949         profile_->SendGetItemAttributesRsp(rawAddr, attributes, values, label, status);
1950     } while (false);
1951 }
1952 
1953 void AvrcpTgService::GetTotalNumberOfItems(const RawAddress &rawAddr, uint8_t scope, uint8_t label) const
1954 {
1955     int result = stub::MediaService::GetInstance()->GetTotalNumberOfItems(rawAddr.GetAddress(), scope, label);
1956     if (result != BT_SUCCESS) {
1957         profile_->SendGetTotalNumberOfItemsRsp(rawAddr, 0x00, 0x00, label, AVRC_ES_CODE_INTERNAL_ERROR);
1958     }
1959 }
1960 
1961 void AvrcpTgService::OnGetTotalNumberOfItems(
1962     const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems, uint8_t label, int status)
1963 {
1964     HILOGI("addr:%{public}s, uidCounter:%{public}d, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1965         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems, label, status);
1966 
1967     do {
1968         if (!IsEnabled()) {
1969             break;
1970         }
1971 
1972         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1973             break;
1974         }
1975 
1976         RawAddress peerAddr(rawAddr.GetAddress());
1977         GetDispatcher()->PostTask(std::bind(
1978             &AvrcpTgService::OnGetTotalNumberOfItemsNative, this, peerAddr, uidCounter, numOfItems, label, status));
1979     } while (false);
1980 }
1981 
1982 void AvrcpTgService::OnGetTotalNumberOfItemsNative(
1983     RawAddress rawAddr, uint16_t uidCounter, uint32_t numOfItems, uint8_t label, int status)
1984 {
1985     HILOGI("addr:%{public}s, uidCounter:%{public}d, numOfItems:%{public}u, label:%{public}d, status:%{public}d",
1986         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, numOfItems, label, status);
1987     do {
1988         if (!IsEnabled()) {
1989             break;
1990         }
1991 
1992         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
1993             break;
1994         }
1995 
1996         profile_->SendGetTotalNumberOfItemsRsp(rawAddr, uidCounter, numOfItems, label, status);
1997     } while (false);
1998 }
1999 
2000 /******************************************************************
2001  * ABSOLUTE VOLUME                                                *
2002  ******************************************************************/
2003 
2004 void AvrcpTgService::SetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, uint8_t label) const
2005 {
2006     HILOGI("addr:%{public}s, volume:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, label);
2007     int result = stub::MediaService::GetInstance()->SetAbsoluteVolume(rawAddr.GetAddress(), volume, label);
2008     if (result != BT_SUCCESS) {
2009         profile_->SendSetAbsoluteVolumeRsp(rawAddr, AVRC_ABSOLUTE_VOLUME_PERCENTAGE_0, label, result);
2010     }
2011 }
2012 
2013 void AvrcpTgService::OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, uint8_t label)
2014 {
2015     HILOGI("addr:%{public}s, volume:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, label);
2016 
2017     do {
2018         if (!IsEnabled()) {
2019             break;
2020         }
2021 
2022         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2023             break;
2024         }
2025 
2026         RawAddress peerAddr(rawAddr.GetAddress());
2027         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnSetAbsoluteVolumeNative, this, peerAddr, volume, label));
2028     } while (false);
2029 }
2030 
2031 void AvrcpTgService::OnSetAbsoluteVolumeNative(RawAddress rawAddr, uint8_t volume, uint8_t label)
2032 {
2033     HILOGI("addr:%{public}s, volume:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, label);
2034 
2035     do {
2036         if (!IsEnabled()) {
2037             break;
2038         }
2039 
2040         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2041             break;
2042         }
2043 
2044         profile_->SendSetAbsoluteVolumeRsp(rawAddr, volume, label, BT_SUCCESS);
2045     } while (false);
2046 }
2047 
2048 /******************************************************************
2049  * NOTIFICATION                                                   *
2050  ******************************************************************/
2051 
2052 void AvrcpTgService::GetSelectedTrack(const RawAddress &rawAddr, uint8_t label) const
2053 {
2054     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
2055 
2056     int result = stub::MediaService::GetInstance()->GetSelectedTrack(rawAddr.GetAddress(), label);
2057     if (result != BT_SUCCESS) {
2058         profile_->SendTrackChangedRsp(true, 0xFFFFFFFFFFFFFFFF, label, result);
2059     }
2060 }
2061 
2062 void AvrcpTgService::OnGetSelectedTrack(const RawAddress &rawAddr, uint64_t uid, uint8_t label)
2063 {
2064     HILOGI("addr:%{public}s, uid:%{public}llu, label:%{public}d",
2065         GET_ENCRYPT_AVRCP_ADDR(rawAddr), static_cast<unsigned long long>(uid), label);
2066 
2067     do {
2068         if (!IsEnabled()) {
2069             break;
2070         }
2071 
2072         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2073             break;
2074         }
2075 
2076         RawAddress peerAddr(rawAddr.GetAddress());
2077         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnGetSelectedTrackNative, this, peerAddr, uid, label));
2078     } while (false);
2079 }
2080 
2081 void AvrcpTgService::OnGetSelectedTrackNative(RawAddress rawAddr, uint64_t uid, uint8_t label)
2082 {
2083     HILOGI("addr:%{public}s, uid:%{public}llu, label:%{public}d",
2084         GET_ENCRYPT_AVRCP_ADDR(rawAddr), static_cast<unsigned long long>(uid), label);
2085     do {
2086         if (!IsEnabled()) {
2087             break;
2088         }
2089 
2090         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2091             break;
2092         }
2093 
2094         profile_->SendTrackChangedRsp(true, uid, label, BT_SUCCESS);
2095     } while (false);
2096 }
2097 
2098 void AvrcpTgService::GetAddressedPlayer(const RawAddress &rawAddr, uint8_t label) const
2099 {
2100     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
2101 
2102     int result = stub::MediaService::GetInstance()->GetAddressedPlayer(rawAddr.GetAddress(), label);
2103     if (result != BT_SUCCESS) {
2104         profile_->SendAddressedPlayerChangedRsp(true, 0xFFFF, 0xFFFF, label, result);
2105     }
2106 }
2107 
2108 void AvrcpTgService::OnGetAddressedPlayer(
2109     const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, uint8_t label)
2110 {
2111     HILOGI("addr:%{public}s, playerId:%{public}d, uidCounter:%{public}d, label:%{public}d",
2112         GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId, uidCounter, label);
2113 
2114     do {
2115         if (!IsEnabled()) {
2116             break;
2117         }
2118 
2119         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2120             break;
2121         }
2122 
2123         RawAddress peerAddr(rawAddr.GetAddress());
2124         GetDispatcher()->PostTask(
2125             std::bind(&AvrcpTgService::OnGetAddressedPlayerNative, this, peerAddr, playerId, uidCounter, label));
2126     } while (false);
2127 }
2128 
2129 void AvrcpTgService::OnGetAddressedPlayerNative(
2130     RawAddress rawAddr, uint16_t playerId, uint16_t uidCounter, uint8_t label)
2131 {
2132     HILOGI("addr:%{public}s, playerId:%{public}d, uidCounter:%{public}d, label:%{public}d",
2133         GET_ENCRYPT_AVRCP_ADDR(rawAddr), playerId, uidCounter, label);
2134     do {
2135         if (!IsEnabled()) {
2136             break;
2137         }
2138 
2139         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2140             break;
2141         }
2142 
2143         profile_->SendAddressedPlayerChangedRsp(true, playerId, uidCounter, label, BT_SUCCESS);
2144     } while (false);
2145 }
2146 
2147 void AvrcpTgService::GetUidCounter(const RawAddress &rawAddr, uint8_t label) const
2148 {
2149     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
2150 
2151     int result = stub::MediaService::GetInstance()->GetUidCounter(rawAddr.GetAddress(), label);
2152     if (result != BT_SUCCESS) {
2153         profile_->SendUidsChangedRsp(true, 0xFFFF, label, result);
2154     }
2155 }
2156 
2157 void AvrcpTgService::OnGetUidCounter(const RawAddress &rawAddr, uint16_t uidCounter, uint8_t label)
2158 {
2159     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d",
2160         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label);
2161 
2162     do {
2163         if (!IsEnabled()) {
2164             break;
2165         }
2166 
2167         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2168             break;
2169         }
2170 
2171         RawAddress peerAddr(rawAddr.GetAddress());
2172         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::OnGetUidCounterNative, this, peerAddr, uidCounter, label));
2173     } while (false);
2174 }
2175 
2176 void AvrcpTgService::OnGetUidCounterNative(const RawAddress &rawAddr, uint16_t uidCounter, uint8_t label)
2177 {
2178     HILOGI("addr:%{public}s, uidCounter:%{public}d, label:%{public}d",
2179         GET_ENCRYPT_AVRCP_ADDR(rawAddr), uidCounter, label);
2180     do {
2181         if (!IsEnabled()) {
2182             break;
2183         }
2184 
2185         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2186             break;
2187         }
2188 
2189         profile_->SendUidsChangedRsp(true, uidCounter, label, BT_SUCCESS);
2190     } while (false);
2191 }
2192 
2193 void AvrcpTgService::GetCurrentAbsoluteVolume(const RawAddress &rawAddr, uint8_t label) const
2194 {
2195     HILOGI("addr:%{public}s, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), label);
2196 
2197     int result = stub::MediaService::GetInstance()->GetCurrentAbsoluteVolume(rawAddr.GetAddress(), label);
2198     if (result != BT_SUCCESS) {
2199         profile_->SendVolumeChangedRsp(true, AVRC_ABSOLUTE_VOLUME_INVALID, label, result);
2200     }
2201 }
2202 
2203 void AvrcpTgService::OnGetCurrentAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, uint8_t label)
2204 {
2205     HILOGI("addr:%{public}s, volume:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, label);
2206     do {
2207         if (!IsEnabled()) {
2208             break;
2209         }
2210 
2211         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2212             break;
2213         }
2214 
2215         RawAddress peerAddr(rawAddr.GetAddress());
2216         GetDispatcher()->PostTask(
2217             std::bind(&AvrcpTgService::OnGetCurrentAbsoluteVolumeNative, this, peerAddr, volume, label));
2218     } while (false);
2219 }
2220 
2221 void AvrcpTgService::OnGetCurrentAbsoluteVolumeNative(const RawAddress &rawAddr, uint8_t volume, uint8_t label)
2222 {
2223     HILOGI("addr:%{public}s, volume:%{public}d, label:%{public}d", GET_ENCRYPT_AVRCP_ADDR(rawAddr), volume, label);
2224 
2225     do {
2226         if (!IsEnabled()) {
2227             break;
2228         }
2229 
2230         if (GetDeviceState(rawAddr) != static_cast<int>(BTConnectState::CONNECTED)) {
2231             break;
2232         }
2233 
2234         profile_->SendVolumeChangedRsp(true, volume, label, BT_SUCCESS);
2235     } while (false);
2236 }
2237 
2238 void AvrcpTgService::SetPlaybackInterval(const RawAddress &rawAddr, uint32_t interval) const
2239 {
2240     HILOGI("addr:%{public}s, interval:%{public}u", GET_ENCRYPT_AVRCP_ADDR(rawAddr), interval);
2241 
2242     stub::MediaService::GetInstance()->SetPlaybackInterval(rawAddr.GetAddress(), interval);
2243 }
2244 
2245 void AvrcpTgService::NotifyPlaybackStatusChanged(uint8_t playStatus, uint32_t playbackPos, uint8_t label)
2246 {
2247     HILOGI("playStatus:%{public}d, playbackPos:%{public}u, label:%{public}d", playStatus, playbackPos, label);
2248 
2249     do {
2250         if (!IsEnabled()) {
2251             break;
2252         }
2253 
2254         GetDispatcher()->PostTask(
2255             std::bind(&AvrcpTgService::NotifyPlaybackStatusChangedNative, this, playStatus, playbackPos, label));
2256     } while (false);
2257 }
2258 void AvrcpTgService::NotifyPlaybackStatusChangedNative(uint8_t playStatus, uint32_t playbackPos, uint8_t label)
2259 {
2260     HILOGI("playStatus:%{public}d, playbackPos:%{public}u, label:%{public}d", playStatus, playbackPos, label);
2261 
2262     do {
2263         if (!IsEnabled()) {
2264             break;
2265         }
2266 
2267         profile_->SendPlaybackStatusChangedRsp(false, playStatus, label, BT_SUCCESS);
2268         profile_->SendPlaybackPosChangedRsp(false, playbackPos, label, BT_SUCCESS);
2269     } while (false);
2270 }
2271 
2272 void AvrcpTgService::NotifyTrackChanged(uint64_t uid, uint32_t playbackPos, uint8_t label)
2273 {
2274     HILOGI("uid:%{public}llu, playbackPos:%{public}u, label:%{public}d",
2275         static_cast<unsigned long long>(uid), playbackPos, label);
2276 
2277     do {
2278         if (!IsEnabled()) {
2279             break;
2280         }
2281 
2282         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyTrackChangedNative, this, uid, playbackPos, label));
2283     } while (false);
2284 }
2285 
2286 void AvrcpTgService::NotifyTrackChangedNative(uint64_t uid, uint32_t playbackPos, uint8_t label)
2287 {
2288     HILOGI("uid:%{public}llu, playbackPos:%{public}u, label:%{public}d",
2289         static_cast<unsigned long long>(uid), playbackPos, label);
2290     do {
2291         if (!IsEnabled()) {
2292             break;
2293         }
2294 
2295         profile_->SendTrackChangedRsp(false, uid, label, BT_SUCCESS);
2296         profile_->SendPlaybackPosChangedRsp(false, playbackPos, label, BT_SUCCESS);
2297     } while (false);
2298 }
2299 
2300 void AvrcpTgService::NotifyTrackReachedEnd(uint32_t playbackPos, uint8_t label)
2301 {
2302     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2303 
2304     do {
2305         if (!IsEnabled()) {
2306             break;
2307         }
2308 
2309         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyTrackReachedEndNative, this, playbackPos, label));
2310     } while (false);
2311 }
2312 
2313 void AvrcpTgService::NotifyTrackReachedEndNative(uint32_t playbackPos, uint8_t label)
2314 {
2315     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2316 
2317     do {
2318         if (!IsEnabled()) {
2319             break;
2320         }
2321 
2322         profile_->SendTrackReachedEndRsp(false, label, BT_SUCCESS);
2323         profile_->SendPlaybackPosChangedRsp(false, playbackPos, label, BT_SUCCESS);
2324     } while (false);
2325 }
2326 
2327 void AvrcpTgService::NotifyTrackReachedStart(uint32_t playbackPos, uint8_t label)
2328 {
2329     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2330 
2331     do {
2332         if (!IsEnabled()) {
2333             break;
2334         }
2335 
2336         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyTrackReachedStartNative, this, playbackPos, label));
2337     } while (false);
2338 }
2339 
2340 void AvrcpTgService::NotifyTrackReachedStartNative(uint32_t playbackPos, uint8_t label)
2341 {
2342     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2343 
2344     do {
2345         if (!IsEnabled()) {
2346             break;
2347         }
2348 
2349         profile_->SendTrackReachedStartRsp(false, label, BT_SUCCESS);
2350         profile_->SendPlaybackPosChangedRsp(false, playbackPos, label, BT_SUCCESS);
2351     } while (false);
2352 }
2353 
2354 void AvrcpTgService::NotifyPlaybackPosChanged(uint32_t playbackPos, uint8_t label)
2355 {
2356     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2357 
2358     do {
2359         if (!IsEnabled()) {
2360             break;
2361         }
2362 
2363         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyPlaybackPosChangedNative, this, playbackPos, label));
2364     } while (false);
2365 }
2366 
2367 void AvrcpTgService::NotifyPlaybackPosChangedNative(uint32_t playbackPos, uint8_t label)
2368 {
2369     HILOGI("playbackPos:%{public}u, label:%{public}d", playbackPos, label);
2370 
2371     do {
2372         if (!IsEnabled()) {
2373             break;
2374         }
2375 
2376         profile_->SendPlaybackPosChangedRsp(false, playbackPos, label, BT_SUCCESS);
2377     } while (false);
2378 }
2379 
2380 void AvrcpTgService::NotifyPlayerAppSettingChanged(
2381     const std::deque<uint8_t> &attributes, const std::deque<uint8_t> &values, uint8_t label)
2382 {
2383     HILOGI("label:%{public}d", label);
2384 
2385     do {
2386         if (!IsEnabled()) {
2387             break;
2388         }
2389 
2390         GetDispatcher()->PostTask(
2391             std::bind(&AvrcpTgService::NotifyPlayerAppSettingChangedNative, this, attributes, values, label));
2392     } while (false);
2393 }
2394 
2395 void AvrcpTgService::NotifyPlayerAppSettingChangedNative(
2396     std::deque<uint8_t> attributes, std::deque<uint8_t> values, uint8_t label)
2397 {
2398     HILOGI("label:%{public}d", label);
2399 
2400     do {
2401         if (!IsEnabled()) {
2402             break;
2403         }
2404 
2405         profile_->SendPlayerApplicationSettingChangedRsp(false, attributes, values, label, BT_SUCCESS);
2406     } while (false);
2407 }
2408 
2409 void AvrcpTgService::NotifyNowPlayingContentChanged(uint8_t label)
2410 {
2411     HILOGI("label:%{public}d", label);
2412 
2413     do {
2414         if (!IsEnabled()) {
2415             break;
2416         }
2417 
2418         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyNowPlayingContentChangedNative, this, label));
2419     } while (false);
2420 }
2421 
2422 void AvrcpTgService::NotifyNowPlayingContentChangedNative(uint8_t label)
2423 {
2424     HILOGI("label:%{public}d", label);
2425 
2426     do {
2427         if (!IsEnabled()) {
2428             break;
2429         }
2430         profile_->SendNowPlayingContentChangedRsp(false, label, BT_SUCCESS);
2431     } while (false);
2432 }
2433 
2434 void AvrcpTgService::NotifyAvailablePlayersChanged(uint8_t label)
2435 {
2436     HILOGI("label:%{public}d", label);
2437 
2438     do {
2439         if (!IsEnabled()) {
2440             break;
2441         }
2442 
2443         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyAvailablePlayersChangedNative, this, label));
2444     } while (false);
2445 }
2446 
2447 void AvrcpTgService::NotifyAvailablePlayersChangedNative(uint8_t label)
2448 {
2449     HILOGI("label:%{public}d", label);
2450 
2451     do {
2452         if (!IsEnabled()) {
2453             break;
2454         }
2455         profile_->SendAvailablePlayersChangedRsp(false, label, BT_SUCCESS);
2456     } while (false);
2457 }
2458 
2459 void AvrcpTgService::NotifyAddressedPlayerChanged(uint16_t playerId, uint16_t uidCounter, uint8_t label)
2460 {
2461     HILOGI("playerId:%{public}d, uidCounter:%{public}d, label:%{public}d", playerId, uidCounter, label);
2462 
2463     do {
2464         if (!IsEnabled()) {
2465             break;
2466         }
2467 
2468         GetDispatcher()->PostTask(
2469             std::bind(&AvrcpTgService::NotifyAddressedPlayerChangedNative, this, playerId, uidCounter, label));
2470     } while (false);
2471 }
2472 
2473 void AvrcpTgService::NotifyAddressedPlayerChangedNative(uint16_t playerId, uint16_t uidCounter, uint8_t label)
2474 {
2475     HILOGI("playerId:%{public}d, uidCounter:%{public}d, label:%{public}d", playerId, uidCounter, label);
2476 
2477     do {
2478         if (!IsEnabled()) {
2479             break;
2480         }
2481         profile_->SendAddressedPlayerChangedRsp(false, playerId, uidCounter, label, BT_SUCCESS);
2482         profile_->SendPlaybackStatusChangedRsp(false, AVRC_PLAY_STATUS_ERROR, label, RET_BAD_STATUS);
2483         profile_->SendTrackChangedRsp(true, 0xFFFFFFFFFFFFFFFF, label, RET_BAD_STATUS);
2484         profile_->SendTrackReachedEndRsp(false, label, RET_BAD_STATUS);
2485         profile_->SendTrackReachedStartRsp(false, label, RET_BAD_STATUS);
2486         profile_->SendPlaybackPosChangedRsp(false, AVRC_PLAY_STATUS_INVALID_SONG_POSITION, label, RET_BAD_STATUS);
2487         std::deque<uint8_t> attrs;
2488         std::deque<uint8_t> vals;
2489         profile_->SendPlayerApplicationSettingChangedRsp(true, attrs, vals, label, RET_BAD_STATUS);
2490         profile_->SendNowPlayingContentChangedRsp(true, label, RET_BAD_STATUS);
2491     } while (false);
2492 }
2493 
2494 void AvrcpTgService::NotifyUidChanged(uint16_t uidCounter, uint8_t label)
2495 {
2496     HILOGI("uidCounter:%{public}d, label:%{public}d", uidCounter, label);
2497 
2498     do {
2499         if (!IsEnabled()) {
2500             break;
2501         }
2502 
2503         GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyUidChangedNative, this, uidCounter, label));
2504     } while (false);
2505 }
2506 
2507 void AvrcpTgService::NotifyUidChangedNative(uint16_t uidCounter, uint8_t label)
2508 {
2509     HILOGI("uidCounter:%{public}d, label:%{public}d", uidCounter, label);
2510 
2511     do {
2512         if (!IsEnabled()) {
2513             break;
2514         }
2515 
2516         profile_->SendUidsChangedRsp(false, uidCounter, label, BT_SUCCESS);
2517     } while (false);
2518 }
2519 
2520 void AvrcpTgService::NotifyVolumeChanged(uint8_t volume, uint8_t label)
2521 {
2522     {
2523         HILOGI("volume:%{public}d, label:%{public}d", volume, label);
2524 
2525         do {
2526             if (!IsEnabled()) {
2527                 break;
2528             }
2529 
2530             GetDispatcher()->PostTask(std::bind(&AvrcpTgService::NotifyVolumeChangedNative, this, volume, label));
2531         } while (false);
2532     }
2533 }
2534 
2535 void AvrcpTgService::NotifyVolumeChangedNative(uint8_t volume, uint8_t label)
2536 {
2537     HILOGI("volume:%{public}d, label:%{public}d", volume, label);
2538 
2539     do {
2540         if (!IsEnabled()) {
2541             break;
2542         }
2543         profile_->SendVolumeChangedRsp(false, volume, label, BT_SUCCESS);
2544     } while (false);
2545 }
2546 
2547 void AvrcpTgService::OnSessionCreate(std::string sessionId)
2548 {
2549     HILOGI("sessionId:%{public}s", sessionId.c_str());
2550 }
2551 
2552 void AvrcpTgService::OnSessionRelease(std::string sessionId)
2553 {
2554     HILOGI("sessionId:%{public}s", sessionId.c_str());
2555 #ifdef AVRCP_AVSESSION
2556     if (avSessionController_ != NULL && avSessionController_->GetSessionId().compare(sessionId) == 0) {
2557         avSessionController_->Destroy();
2558         avSessionController_ = NULL;
2559     }
2560 #endif
2561 }
2562 
2563 void AvrcpTgService::OnTopSessionChange(std::string sessionId)
2564 {
2565     HILOGI("sessionId:%{public}s", sessionId.c_str());
2566 #ifdef AVRCP_AVSESSION
2567     if (avSessionController_ != NULL) {
2568         if (avSessionController_->GetSessionId().compare(sessionId) == 0) {
2569             return;
2570         }
2571         avSessionController_->Destroy();
2572     }
2573     auto res = OHOS::AVSession::AVSessionManager::GetInstance().CreateController(sessionId, avSessionController_);
2574     if (res == OHOS::AVSession::AVSESSION_SUCCESS && avSessionController_ != NULL) {
2575         avSessionController_->RegisterCallback(avControllerObserver_);
2576         OHOS::AVSession::AVPlaybackState::PlaybackStateMaskType filter;
2577         filter.set(OHOS::AVSession::AVPlaybackState::PLAYBACK_KEY_STATE);
2578         avSessionController_->SetPlaybackFilter(filter);
2579     } else {
2580         HILOGE("avSessionController_ is NULL");
2581     }
2582 #endif
2583 }
2584 
2585 void AvrcpTgService::OnPlaybackStateChange(const int32_t state)
2586 {
2587     HILOGI("state:%{public}d", state);
2588 
2589     do {
2590         if (!IsEnabled()) {
2591             break;
2592         }
2593         GetDispatcher()->PostTask(
2594             std::bind(&AvrcpTgService::OnPlaybackStateChangeNative, this, AvrcpTgService::ConvertPlayState(state)));
2595     } while (false);
2596 }
2597 
2598 void AvrcpTgService::OnPlaybackStateChangeNative(int32_t state)
2599 {
2600     HILOGI("state:%{public}d", state);
2601     std::pair<bool, uint8_t> playback = profile_->GetNotificationLabel(AVRC_EVENT_ID_PLAYBACK_STATUS_CHANGED);
2602     if (!playback.first) {
2603         HILOGE("device is not register for playStatus updates");
2604         return;
2605     }
2606 
2607     NotifyPlaybackStatusChanged(state, 0, playback.second);
2608 }
2609 
2610 void AvrcpTgService::ProcessChannelEvent(
2611     RawAddress rawAddr, uint8_t connectId, uint8_t event, uint16_t result, void *context)
2612 {
2613     HILOGI("addr:%{public}s, connectId:%{public}d, event:%{public}d, result:%{public}d",
2614         GET_ENCRYPT_AVRCP_ADDR(rawAddr), connectId, event, result);
2615 
2616     if (!IsDisabled()) {
2617         profile_->ProcessChannelEvent(rawAddr, connectId, event, result, context);
2618     }
2619 }
2620 
2621 void AvrcpTgService::ProcessChannelMessage(
2622     uint8_t connectId, uint8_t label, uint8_t crType, uint8_t chType, Packet *pkt, void *context)
2623 {
2624     HILOGI("connectId:%{public}d, label:%{public}d, crType:%{public}d, chType:%{public}d",
2625         connectId, label, crType, chType);
2626     if (!IsDisabled()) {
2627         profile_->ProcessChannelMessage(connectId, label, crType, chType, pkt, context);
2628     }
2629 }
2630 
2631 void AvrcpTgService::ChannelEventCallback(
2632     uint8_t connectId, uint8_t event, uint16_t result, const BtAddr *btAddr, void *context)
2633 {
2634     HILOGI("connectId:%{public}d, event:%{public}d, result:%{public}d", connectId, event, result);
2635     auto servManager = IProfileManager::GetInstance();
2636     auto service = static_cast<AvrcpTgService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
2637     RawAddress rawAddr(RawAddress::ConvertToString(btAddr->addr));
2638 
2639     if (service != nullptr) {
2640         switch (event) {
2641             case AVCT_CONNECT_IND_EVT:
2642             case AVCT_CONNECT_CFM_EVT:
2643                 if (result != BT_SUCCESS) {
2644                     service->DecConnectionNum();
2645                 }
2646                 break;
2647             case AVCT_DISCONNECT_IND_EVT:
2648             case AVCT_DISCONNECT_CFM_EVT:
2649                 service->DecConnectionNum();
2650                 break;
2651             default:
2652                 break;
2653         }
2654         service->GetDispatcher()->PostTask(
2655             std::bind(&AvrcpTgService::ProcessChannelEvent, service, rawAddr, connectId, event, result, context));
2656     }
2657 }
2658 
2659 void AvrcpTgService::ChannelMessageCallback(
2660     uint8_t connectId, uint8_t label, uint8_t crType, uint8_t chType, Packet *pkt, void *context)
2661 {
2662     HILOGI("connectId:%{public}d, label:%{public}d, crType:%{public}d, chType:%{public}d",
2663         connectId, label, crType, chType);
2664     auto servManager = IProfileManager::GetInstance();
2665     auto service = static_cast<AvrcpTgService *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
2666     auto myPkt = PacketRefMalloc(pkt);
2667 
2668     if (service != nullptr) {
2669         service->GetDispatcher()->PostTask(std::bind(
2670             &AvrcpTgService::ProcessChannelMessage, service, connectId, label, crType, chType, myPkt, context));
2671     }
2672 }
2673 bool AvrcpTgService::CheckConnectionNum()
2674 {
2675     if (++currentConn_ > maxConnection_) {
2676         currentConn_ = maxConnection_;
2677         return false;
2678     } else {
2679         return true;
2680     }
2681 }
2682 
2683 uint8_t AvrcpTgService::ConvertPlayState(const int32_t state) const
2684 {
2685     HILOGI("state:%{public}d", state);
2686     uint8_t ret = AVRC_PLAY_STATUS_ERROR;
2687 #ifdef AVRCP_AVSESSION
2688     switch (state) {
2689         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_INITIAL:
2690         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_PREPARING:
2691         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_MAX:
2692             break;
2693         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_PLAYING:
2694             ret = AVRC_PLAY_STATUS_PLAYING;
2695             break;
2696         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_PAUSED:
2697             ret = AVRC_PLAY_STATUS_PAUSED;
2698             break;
2699         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_FAST_FORWARD:
2700             ret = AVRC_PLAY_STATUS_FWD_SEEK;
2701             break;
2702         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_REWIND:
2703             ret = AVRC_PLAY_STATUS_REV_SEEK;
2704             break;
2705         case OHOS::AVSession::AVPlaybackState::PLAYBACK_STATE_STOP:
2706             ret = AVRC_PLAY_STATUS_STOPPED;
2707             break;
2708         default:
2709             break;
2710     }
2711 #endif
2712     return ret;
2713 }
2714 REGISTER_CLASS_CREATOR(AvrcpTgService);
2715 }  // namespace bluetooth
2716 }  // namespace OHOS
2717