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