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