1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <mutex>
16 #include "bluetooth_def.h"
17 #include "bluetooth_avrcp_tg_server.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils.h"
20 #include "interface_adapter_manager.h"
21 #include "interface_profile.h"
22 #include "interface_profile_avrcp_tg.h"
23 #include "interface_profile_manager.h"
24 #include "remote_observer_list.h"
25 #include "permission_utils.h"
26 
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 using namespace OHOS::bluetooth;
31 
32 struct BluetoothAvrcpTgServer::impl {
33 public:
34     class SysStsObserverImpl : public ISystemStateObserver {
35     public:
SysStsObserverImpl(BluetoothAvrcpTgServer::impl * impl)36         explicit SysStsObserverImpl(BluetoothAvrcpTgServer::impl *impl) : impl_(impl) {}
~SysStsObserverImpl()37         virtual ~SysStsObserverImpl() {}
38 
OnSystemStateChange(const BTSystemState state)39         void OnSystemStateChange(const BTSystemState state) override
40         {
41             impl_->OnSystemStateChange(state);
42         }
43 
44     private:
45         BluetoothAvrcpTgServer::impl *impl_;
46     };
47     class ObserverImpl : public IProfileAvrcpTg::IObserver {
48     public:
ObserverImpl(BluetoothAvrcpTgServer::impl * impl)49         explicit ObserverImpl(BluetoothAvrcpTgServer::impl *impl) : impl_(impl) {}
~ObserverImpl()50         virtual ~ObserverImpl() {}
51 
OnConnectionStateChanged(const RawAddress & rawAddr,int state)52         void OnConnectionStateChanged(const RawAddress &rawAddr, int state) override
53         {
54             impl_->OnConnectionStateChanged(rawAddr, state);
55         }
56 
57     private:
58         BluetoothAvrcpTgServer::impl *impl_;
59     };
60 
implOHOS::Bluetooth::BluetoothAvrcpTgServer::impl61     impl()
62     {
63         HILOGI("start.");
64 
65         auto svManager = IProfileManager::GetInstance();
66         service_ = static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
67         if (service_ != nullptr) {
68             observer_ = std::make_unique<ObserverImpl>(this);
69             service_->RegisterObserver(observer_.get());
70         }
71 
72         sysObserver_ = std::make_unique<SysStsObserverImpl>(this);
73         IAdapterManager::GetInstance()->RegisterSystemStateObserver(*sysObserver_);
74     }
75 
~implOHOS::Bluetooth::BluetoothAvrcpTgServer::impl76     ~impl()
77     {
78         HILOGI("start.");
79 
80         auto svManager = IProfileManager::GetInstance();
81         service_ = static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
82         if (service_ != nullptr) {
83             service_->UnregisterObserver();
84             observer_ = nullptr;
85             service_ = nullptr;
86         }
87 
88         IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*sysObserver_);
89         sysObserver_ = nullptr;
90     }
91 
IsEnabledOHOS::Bluetooth::BluetoothAvrcpTgServer::impl92     bool IsEnabled()
93     {
94         HILOGI("start.");
95 
96         auto servManager = IProfileManager::GetInstance();
97         service_ = static_cast<IProfileAvrcpTg *>(servManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
98 
99         return (service_ != nullptr && service_->IsEnabled());
100     }
101 
OnSystemStateChangeOHOS::Bluetooth::BluetoothAvrcpTgServer::impl102     void OnSystemStateChange(const BTSystemState state)
103     {
104         HILOGI("start.");
105 
106         std::lock_guard<std::mutex> lock(serviceMutex_);
107 
108         switch (state) {
109             case BTSystemState::ON: {
110                 auto svManager = IProfileManager::GetInstance();
111                 service_ = static_cast<IProfileAvrcpTg *>(svManager->GetProfileService(PROFILE_NAME_AVRCP_TG));
112                 if (service_ != nullptr) {
113                     observer_ = std::make_unique<ObserverImpl>(this);
114                     service_->RegisterObserver(observer_.get());
115                 }
116                 break;
117             }
118             case BTSystemState::OFF:
119                 /// FALL THROUGH
120             default:
121                 if (service_ != nullptr) {
122                     service_->UnregisterObserver();
123                     observer_ = nullptr;
124                     service_ = nullptr;
125                 }
126                 break;
127         }
128     }
129 
OnConnectionStateChangedOHOS::Bluetooth::BluetoothAvrcpTgServer::impl130     void OnConnectionStateChanged(const RawAddress &rawAddr, int state)
131     {
132         HILOGI("res: %{public}s, state: %{public}d.", GET_ENCRYPT_AVRCP_ADDR(rawAddr), state);
133         std::lock_guard<std::mutex> lock(observerMutex_);
134 
135         observers_.ForEach([rawAddr, state](IBluetoothAvrcpTgObserver *observer) {
136             observer->OnConnectionStateChanged(rawAddr, static_cast<int32_t>(state),
137                 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
138         });
139     }
140 
141     std::mutex serviceMutex_;
142     IProfileAvrcpTg *service_;
143 
144     std::mutex observerMutex_;
145     RemoteObserverList<IBluetoothAvrcpTgObserver> observers_;
146 
147     std::unique_ptr<ObserverImpl> observer_;
148     std::unique_ptr<SysStsObserverImpl> sysObserver_;
149 };
150 
BluetoothAvrcpTgServer()151 BluetoothAvrcpTgServer::BluetoothAvrcpTgServer()
152 {
153     HILOGI("start.");
154 
155     pimpl = std::make_unique<impl>();
156 }
157 
~BluetoothAvrcpTgServer()158 BluetoothAvrcpTgServer::~BluetoothAvrcpTgServer()
159 {
160     pimpl = nullptr;
161 }
162 
RegisterObserver(const sptr<IBluetoothAvrcpTgObserver> & observer)163 void BluetoothAvrcpTgServer::RegisterObserver(const sptr<IBluetoothAvrcpTgObserver> &observer)
164 {
165     HILOGI("start.");
166 
167     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
168 
169     if (observer == nullptr) {
170         HILOGI("observer is NULL.");
171         return ;
172     }
173     auto func = std::bind(&BluetoothAvrcpTgServer::UnregisterObserver, this, std::placeholders::_1);
174     pimpl->observers_.Register(observer, func);
175     HILOGI("end.");
176 
177     return ;
178 }
179 
UnregisterObserver(const sptr<IBluetoothAvrcpTgObserver> & observer)180 void BluetoothAvrcpTgServer::UnregisterObserver(const sptr<IBluetoothAvrcpTgObserver> &observer)
181 {
182     HILOGI("start.");
183 
184     std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
185 
186     if (observer == nullptr) {
187         HILOGI("observer is NULL.");
188         return;
189     }
190     pimpl->observers_.Deregister(observer);
191     HILOGI("end.");
192 
193     return;
194 }
195 
SetActiveDevice(const BluetoothRawAddress & addr)196 void BluetoothAvrcpTgServer::SetActiveDevice(const BluetoothRawAddress &addr)
197 {
198     HILOGI("address: %{public}s", GetEncryptAddr(addr.GetAddress()).c_str());
199 
200     if (pimpl->IsEnabled()) {
201         pimpl->service_->SetActiveDevice(addr);
202     } else {
203         HILOGE("service is null or disable ");
204     }
205     HILOGI("end.");
206 }
207 
Connect(const BluetoothRawAddress & addr)208 int32_t BluetoothAvrcpTgServer::Connect(const BluetoothRawAddress &addr)
209 {
210     HILOGI("address: %{public}s", GetEncryptAddr(addr.GetAddress()).c_str());
211     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
212         HILOGE("Connect error, check permission failed");
213         return BT_FAILURE;
214     }
215     int32_t result = 0;
216 
217     if (pimpl->IsEnabled()) {
218         result = pimpl->service_->Connect(addr);
219     } else {
220         HILOGE("service is null or disable ");
221     }
222     HILOGI("end.");
223 
224     return result;
225 }
226 
Disconnect(const BluetoothRawAddress & addr)227 int32_t BluetoothAvrcpTgServer::Disconnect(const BluetoothRawAddress &addr)
228 {
229     HILOGI("address: %{public}s", GetEncryptAddr(addr.GetAddress()).c_str());
230 
231     int32_t result = 0;
232 
233     if (pimpl->IsEnabled()) {
234         result = pimpl->service_->Disconnect(addr);
235     } else {
236         HILOGE("service is null or disable ");
237     }
238     HILOGI("end.");
239 
240     return result;
241 }
242 
GetConnectedDevices()243 std::vector<BluetoothRawAddress> BluetoothAvrcpTgServer::GetConnectedDevices()
244 {
245     HILOGI("start.");
246     std::vector<BluetoothRawAddress> results;
247     if (!pimpl->IsEnabled()) {
248         HILOGE("service is null or disable ");
249         return results;
250     }
251 
252     std::vector<RawAddress> devices;
253     devices = pimpl->service_->GetConnectedDevices();
254     for (auto device : devices) {
255         BluetoothRawAddress rawAddr = BluetoothRawAddress(device);
256         results.emplace_back(rawAddr);
257     }
258     HILOGI("end.");
259     return results;
260 }
261 
GetDevicesByStates(const std::vector<int32_t> & states)262 std::vector<BluetoothRawAddress> BluetoothAvrcpTgServer::GetDevicesByStates(const std::vector<int32_t> &states)
263 {
264     HILOGI("start.");
265 
266     std::vector<BluetoothRawAddress> results;
267 
268     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
269         HILOGE("false, check permission failed");
270         return results;
271     }
272     if (!pimpl->IsEnabled()) {
273         HILOGE("service is null or disable ");
274         return results;
275     }
276 
277     std::vector<RawAddress> devices;
278     std::vector<int> convertStates;
279     for (auto state : states) {
280         HILOGI("state = %{public}d", state);
281         convertStates.push_back(static_cast<int>(state));
282     }
283 
284     devices = pimpl->service_->GetDevicesByStates(convertStates);
285     for (auto device : devices) {
286         BluetoothRawAddress rawAddr = BluetoothRawAddress(device);
287         results.emplace_back(rawAddr);
288     }
289     HILOGI("end.");
290 
291     return results;
292 }
293 
GetDeviceState(const BluetoothRawAddress & addr)294 int32_t BluetoothAvrcpTgServer::GetDeviceState(const BluetoothRawAddress &addr)
295 {
296     HILOGI("address: %{public}s", GetEncryptAddr(addr.GetAddress()).c_str());
297     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
298         HILOGE("false, check permission failed");
299         return BT_FAILURE;
300     }
301     int32_t result = 0;
302 
303     if (pimpl->IsEnabled()) {
304         result = pimpl->service_->GetDeviceState(addr);
305     } else {
306         HILOGE("service is null or disable ");
307     }
308     HILOGI("end.");
309 
310     return result;
311 }
312 
NotifyPlaybackStatusChanged(int32_t playStatus,int32_t playbackPos)313 void BluetoothAvrcpTgServer::NotifyPlaybackStatusChanged(int32_t playStatus, int32_t playbackPos)
314 {
315     HILOGI("playStatus: %{public}d, playbackPos: %{public}d", playStatus, playbackPos);
316 
317     if (!pimpl->IsEnabled()) {
318         HILOGE("service is null or disable ");
319         return;
320     }
321 
322     pimpl->service_->NotifyPlaybackStatusChanged(static_cast<uint8_t>(playStatus),
323                                                  static_cast<uint32_t>(playbackPos));
324     HILOGI("end.");
325 }
326 
NotifyTrackChanged(int64_t uid,int32_t playbackPos)327 void BluetoothAvrcpTgServer::NotifyTrackChanged(int64_t uid, int32_t playbackPos)
328 {
329     HILOGI("uid: %{public}jd, playbackPos: %{public}d", uid, playbackPos);
330 
331     if (!pimpl->IsEnabled()) {
332         HILOGE("service is null or disable ");
333         return;
334     }
335 
336     pimpl->service_->NotifyTrackChanged(static_cast<uint64_t>(uid), static_cast<uint32_t>(playbackPos));
337     HILOGI("end.");
338 }
339 
NotifyTrackReachedEnd(int32_t playbackPos)340 void BluetoothAvrcpTgServer::NotifyTrackReachedEnd(int32_t playbackPos)
341 {
342     HILOGI("playbackPos: %{public}d", playbackPos);
343 
344     if (!pimpl->IsEnabled()) {
345         HILOGE("service is null or disable ");
346         return;
347     }
348 
349     pimpl->service_->NotifyTrackReachedEnd(static_cast<uint32_t>(playbackPos));
350     HILOGI("end.");
351 }
352 
NotifyTrackReachedStart(int32_t playbackPos)353 void BluetoothAvrcpTgServer::NotifyTrackReachedStart(int32_t playbackPos)
354 {
355     HILOGI("playbackPos: %{public}d", playbackPos);
356 
357     if (!pimpl->IsEnabled()) {
358         HILOGE("service is null or disable ");
359         return;
360     }
361 
362     pimpl->service_->NotifyTrackReachedStart(static_cast<uint32_t>(playbackPos));
363     HILOGI("end.");
364 }
365 
NotifyPlaybackPosChanged(int32_t playbackPos)366 void BluetoothAvrcpTgServer::NotifyPlaybackPosChanged(int32_t playbackPos)
367 {
368     HILOGI("playbackPos: %{public}d", playbackPos);
369 
370     if (!pimpl->IsEnabled()) {
371         HILOGE("service is null or disable ");
372         return;
373     }
374 
375     pimpl->service_->NotifyPlaybackPosChanged(static_cast<uint32_t>(playbackPos));
376     HILOGI("end.");
377 }
378 
NotifyPlayerAppSettingChanged(const std::vector<int32_t> & attributes,const std::vector<int32_t> & values)379 void BluetoothAvrcpTgServer::NotifyPlayerAppSettingChanged(const std::vector<int32_t> &attributes,
380     const std::vector<int32_t> &values)
381 {
382     HILOGI("start.");
383 
384     if (!pimpl->IsEnabled()) {
385         HILOGE("service is null or disable ");
386         return;
387     }
388 
389     std::deque<uint8_t> attrs;
390     std::deque<uint8_t> vals;
391 
392     for (auto attribute : attributes) {
393         HILOGI("attributes = %{public}d", attribute);
394         attrs.push_back(attribute);
395     }
396     for (auto value : values) {
397         HILOGI("values = %{public}d", value);
398         vals.push_back(value);
399     }
400 
401     pimpl->service_->NotifyPlayerAppSettingChanged(attrs, vals);
402     HILOGI("end.");
403 }
404 
NotifyNowPlayingContentChanged()405 void BluetoothAvrcpTgServer::NotifyNowPlayingContentChanged()
406 {
407     HILOGI("start.");
408 
409     if (!pimpl->IsEnabled()) {
410         HILOGE("service is null or disable ");
411         return;
412     }
413 
414     pimpl->service_->NotifyNowPlayingContentChanged();
415     HILOGI("end.");
416 }
417 
NotifyAvailablePlayersChanged()418 void BluetoothAvrcpTgServer::NotifyAvailablePlayersChanged()
419 {
420     HILOGI("start.");
421 
422     if (!pimpl->IsEnabled()) {
423         HILOGE("service is null or disable ");
424         return;
425     }
426 
427     pimpl->service_->NotifyAvailablePlayersChanged();
428     HILOGI("end.");
429 }
430 
NotifyAddressedPlayerChanged(int32_t playerId,int32_t uidCounter)431 void BluetoothAvrcpTgServer::NotifyAddressedPlayerChanged(int32_t playerId, int32_t uidCounter)
432 {
433     HILOGI("playerId: %{public}d, uidCounter: %{public}d", playerId, uidCounter);
434 
435     if (!pimpl->IsEnabled()) {
436         HILOGE("service is null or disable ");
437         return;
438     }
439 
440     pimpl->service_->NotifyAddressedPlayerChanged(static_cast<uint32_t>(playerId), static_cast<uint32_t>(uidCounter));
441     HILOGI("end.");
442 }
443 
NotifyUidChanged(int32_t uidCounter)444 void BluetoothAvrcpTgServer::NotifyUidChanged(int32_t uidCounter)
445 {
446     HILOGI("uidCounter: %{public}d", uidCounter);
447 
448     if (!pimpl->IsEnabled()) {
449         HILOGE("service is null or disable ");
450         return;
451     }
452 
453     pimpl->service_->NotifyUidChanged(static_cast<uint32_t>(uidCounter));
454     HILOGI("end.");
455 }
456 
NotifyVolumeChanged(int32_t volume)457 void BluetoothAvrcpTgServer::NotifyVolumeChanged(int32_t volume)
458 {
459     HILOGI("volume: %{public}d", volume);
460 
461     if (!pimpl->IsEnabled()) {
462         HILOGE("service is null or disable ");
463         return;
464     }
465 
466     pimpl->service_->NotifyVolumeChanged(static_cast<uint8_t>(volume));
467     HILOGI("end.");
468 }
SetDeviceAbsoluteVolume(const BluetoothRawAddress & addr,int32_t volumeLevel)469 int32_t SetDeviceAbsoluteVolume(const BluetoothRawAddress &addr, int32_t volumeLevel)
470 {
471     return BT_NO_ERROR;
472 }
473 
SetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t ability)474 int32_t SetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t ability)
475 {
476     return BT_NO_ERROR;
477 }
478 
GetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t & ability)479 int32_t GetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t &ability)
480 {
481     ability = DeviceAbsVolumeAbility::DEVICE_ABSVOL_UNSUPPORT;
482     return BT_NO_ERROR;
483 }
484 }  // namespace Bluetooth
485 }  // namespace OHOS
486