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
16 #include "bluetooth_def.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_hitrace.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils_server.h"
21 #include "hisysevent.h"
22 #include "interface_profile_manager.h"
23 #include "interface_profile_a2dp_src.h"
24 #include "remote_observer_list.h"
25 #include "interface_adapter_manager.h"
26 #include "permission_utils.h"
27 #include "bluetooth_a2dp_source_server.h"
28
29 namespace OHOS {
30 namespace Bluetooth {
31 class A2dpSourceObserver : public IA2dpObserver {
32 public:
33 A2dpSourceObserver() = default;
34 ~A2dpSourceObserver() override = default;
35
OnConnectionStateChanged(const RawAddress & device,int state)36 void OnConnectionStateChanged(const RawAddress &device, int state) override
37 {
38 HILOGI("addr: %{public}s, state: %{public}d", GET_ENCRYPT_ADDR(device), state);
39 if (state == static_cast<int>(BTConnectState::CONNECTED) ||
40 state == static_cast<int>(BTConnectState::DISCONNECTED)) {
41 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BT_SERVICE, "A2DP_CONNECTED_STATE",
42 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", state);
43 }
44 observers_->ForEach([device, state](sptr<IBluetoothA2dpSourceObserver> observer) {
45 observer->OnConnectionStateChanged(device, state);
46 });
47 }
48
OnPlayingStatusChaned(const RawAddress & device,int playingState,int error)49 void OnPlayingStatusChaned(const RawAddress &device, int playingState, int error) override
50 {
51 HILOGI("addr: %{public}s, state: %{public}d, error: %{public}d",
52 GET_ENCRYPT_ADDR(device), playingState, error);
53 observers_->ForEach([device, playingState, error](sptr<IBluetoothA2dpSourceObserver> observer) {
54 observer->OnPlayingStatusChanged(device, playingState, error);
55 });
56 }
57
OnConfigurationChanged(const RawAddress & device,const A2dpSrcCodecInfo & info,int error)58 void OnConfigurationChanged(const RawAddress &device, const A2dpSrcCodecInfo &info, int error) override
59 {
60 HILOGI("addr: %{public}s, error: %{public}d", GET_ENCRYPT_ADDR(device), error);
61 observers_->ForEach([device, info, error](sptr<IBluetoothA2dpSourceObserver> observer) {
62 BluetoothA2dpCodecInfo tmpInfo {};
63 tmpInfo.bitsPerSample = info.bitsPerSample;
64 tmpInfo.channelMode = info.channelMode;
65 tmpInfo.codecPriority = info.codecPriority;
66 tmpInfo.codecType = info.codecType;
67 tmpInfo.sampleRate = info.sampleRate;
68 tmpInfo.codecSpecific1 = info.codecSpecific1;
69 tmpInfo.codecSpecific2 = info.codecSpecific2;
70 tmpInfo.codecSpecific3 = info.codecSpecific3;
71 tmpInfo.codecSpecific4 = info.codecSpecific4;
72
73 observer->OnConfigurationChanged(device, tmpInfo, error);
74 });
75 }
76
OnMediaStackChanged(const RawAddress & device,int action)77 void OnMediaStackChanged(const RawAddress &device, int action) override
78 {
79 HILOGI("addr: %{public}s, action: %{public}d", GET_ENCRYPT_ADDR(device), action);
80 }
81
SetObserver(RemoteObserverList<IBluetoothA2dpSourceObserver> * observers)82 void SetObserver(RemoteObserverList<IBluetoothA2dpSourceObserver> *observers)
83 {
84 observers_ = observers;
85 }
86
87 private:
88 RemoteObserverList<IBluetoothA2dpSourceObserver> *observers_;
89 };
90
91 struct BluetoothA2dpSourceServer::impl {
92 impl();
93 ~impl();
94
95 /// sys state observer
96 class SystemStateObserver;
97 std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
98
99 RemoteObserverList<IBluetoothA2dpSourceObserver> observers_;
100 std::unique_ptr<A2dpSourceObserver> observerImp_{std::make_unique<A2dpSourceObserver>()};
101 IProfileA2dpSrc *a2dpSrcService_ = nullptr;
102 };
103
104 class BluetoothA2dpSourceServer::impl::SystemStateObserver : public ISystemStateObserver {
105 public:
SystemStateObserver(BluetoothA2dpSourceServer::impl * pimpl)106 SystemStateObserver(BluetoothA2dpSourceServer::impl *pimpl) : pimpl_(pimpl) {};
107 ~SystemStateObserver() override = default;
108
OnSystemStateChange(const BTSystemState state)109 void OnSystemStateChange(const BTSystemState state) override
110 {
111 IProfileManager *serviceMgr = IProfileManager::GetInstance();
112 if (!pimpl_) {
113 HILOGI("failed: pimpl_ is null");
114 return;
115 }
116
117 switch (state) {
118 case BTSystemState::ON:
119 if (serviceMgr != nullptr) {
120 pimpl_->a2dpSrcService_ =
121 (IProfileA2dpSrc *)serviceMgr->GetProfileService(PROFILE_NAME_A2DP_SRC);
122 if (pimpl_->a2dpSrcService_ != nullptr) {
123 pimpl_->a2dpSrcService_->RegisterObserver(pimpl_->observerImp_.get());
124 }
125 }
126 break;
127 case BTSystemState::OFF:
128 pimpl_->a2dpSrcService_ = nullptr;
129 break;
130 default:
131 break;
132 }
133 }
134
135 private:
136 BluetoothA2dpSourceServer::impl *pimpl_ = nullptr;
137 };
138
impl()139 BluetoothA2dpSourceServer::impl::impl()
140 {
141 HILOGI("starts");
142 }
143
~impl()144 BluetoothA2dpSourceServer::impl::~impl()
145 {
146 HILOGI("starts");
147 }
148
BluetoothA2dpSourceServer()149 BluetoothA2dpSourceServer::BluetoothA2dpSourceServer()
150 {
151 HILOGI("starts");
152 pimpl = std::make_unique<impl>();
153 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
154 pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
155 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
156
157 IProfileManager *serviceMgr = IProfileManager::GetInstance();
158 if (serviceMgr != nullptr) {
159 pimpl->a2dpSrcService_ = (IProfileA2dpSrc *)serviceMgr->GetProfileService(PROFILE_NAME_A2DP_SRC);
160 if (pimpl->a2dpSrcService_ != nullptr) {
161 pimpl->a2dpSrcService_->RegisterObserver(pimpl->observerImp_.get());
162 }
163 }
164 }
165
~BluetoothA2dpSourceServer()166 BluetoothA2dpSourceServer::~BluetoothA2dpSourceServer()
167 {
168 HILOGI("starts");
169 IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
170 if (pimpl->a2dpSrcService_ != nullptr) {
171 pimpl->a2dpSrcService_->DeregisterObserver(pimpl->observerImp_.get());
172 }
173 }
174
RegisterObserver(const sptr<IBluetoothA2dpSourceObserver> & observer)175 void BluetoothA2dpSourceServer::RegisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)
176 {
177 HILOGI("starts");
178 if (observer == nullptr) {
179 HILOGI("observer is null");
180 return;
181 }
182 auto func = std::bind(&BluetoothA2dpSourceServer::DeregisterObserver, this, std::placeholders::_1);
183 pimpl->observers_.Register(observer, func);
184 if (pimpl->a2dpSrcService_ == nullptr) {
185 return;
186 }
187 // During A2DP HDF Registration, check the current status and callback
188 RawAddress device = GetActiveSinkDevice();
189 int state = static_cast<int>(BTConnectState::DISCONNECTED);
190 GetDeviceState(static_cast<const RawAddress &>(device), state);
191 if (state == static_cast<int>(BTConnectState::CONNECTED)) {
192 HILOGI("onConnectionStateChanged");
193 observer->OnConnectionStateChanged(device, state);
194 }
195 }
196
DeregisterObserver(const sptr<IBluetoothA2dpSourceObserver> & observer)197 void BluetoothA2dpSourceServer::DeregisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)
198 {
199 HILOGI("starts");
200 if (observer == nullptr) {
201 HILOGE("observer is null");
202 return;
203 }
204
205 if (pimpl != nullptr) {
206 pimpl->observers_.Deregister(observer);
207 }
208 }
209
Connect(const RawAddress & device)210 int32_t BluetoothA2dpSourceServer::Connect(const RawAddress &device)
211 {
212 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
213 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
214 HILOGE("Connect error, check permission failed");
215 return BT_ERR_SYSTEM_PERMISSION_FAILED;
216 }
217 OHOS::Bluetooth::BluetoothHiTrace::BluetoothStartAsyncTrace("A2DP_SRC_CONNECT", 1);
218 int32_t result = pimpl->a2dpSrcService_->Connect(device);
219 OHOS::Bluetooth::BluetoothHiTrace::BluetoothFinishAsyncTrace("A2DP_SRC_CONNECT", 1);
220 return result;
221 }
222
Disconnect(const RawAddress & device)223 int32_t BluetoothA2dpSourceServer::Disconnect(const RawAddress &device)
224 {
225 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
226 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
227 HILOGE("Disconnect error, check permission failed");
228 return BT_ERR_SYSTEM_PERMISSION_FAILED;
229 }
230 return pimpl->a2dpSrcService_->Disconnect(device);
231 }
232
GetDeviceState(const RawAddress & device,int & state)233 int BluetoothA2dpSourceServer::GetDeviceState(const RawAddress &device, int &state)
234 {
235 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
236 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
237 HILOGE("false, check permission failed");
238 return RET_NO_SUPPORT;
239 }
240 state = pimpl->a2dpSrcService_->GetDeviceState(device);
241 return NO_ERROR;
242 }
243
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<RawAddress> & rawAddrs)244 int BluetoothA2dpSourceServer::GetDevicesByStates(const std::vector<int32_t> &states, std::vector<RawAddress> &rawAddrs)
245 {
246 HILOGI("starts");
247 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
248 HILOGE("false, check permission failed");
249 return RET_NO_SUPPORT;
250 }
251 std::vector<int> tmpStates;
252 for (int32_t state : states) {
253 HILOGI("state = %{public}d", state);
254 tmpStates.push_back((int)state);
255 }
256
257 rawAddrs = pimpl->a2dpSrcService_->GetDevicesByStates(tmpStates);
258 return NO_ERROR;
259 }
260
GetPlayingState(const RawAddress & device,int & state)261 int32_t BluetoothA2dpSourceServer::GetPlayingState(const RawAddress &device, int &state)
262 {
263 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
264 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
265 HILOGE("false, check permission failed");
266 return BT_ERR_SYSTEM_PERMISSION_FAILED;
267 }
268 int ret = pimpl->a2dpSrcService_->GetPlayingState(device, state);
269 if (ret != NO_ERROR) {
270 return BT_ERR_INTERNAL_ERROR;
271 }
272 return NO_ERROR;
273 }
274
SetConnectStrategy(const RawAddress & device,int strategy)275 int BluetoothA2dpSourceServer::SetConnectStrategy(const RawAddress &device, int strategy)
276 {
277 if (!PermissionUtils::CheckSystemHapApp()) {
278 HILOGE("check system api failed.");
279 return BT_ERR_SYSTEM_PERMISSION_FAILED;
280 }
281 HILOGI("addr: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
282 return pimpl->a2dpSrcService_->SetConnectStrategy(device, strategy);
283 }
284
GetConnectStrategy(const RawAddress & device,int & strategy)285 int BluetoothA2dpSourceServer::GetConnectStrategy(const RawAddress &device, int &strategy)
286 {
287 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
288 strategy = pimpl->a2dpSrcService_->GetConnectStrategy(device);
289 return NO_ERROR;
290 }
291
SetActiveSinkDevice(const RawAddress & device)292 int BluetoothA2dpSourceServer::SetActiveSinkDevice(const RawAddress &device)
293 {
294 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
295 return pimpl->a2dpSrcService_->SetActiveSinkDevice(device);
296 }
297
GetActiveSinkDevice()298 RawAddress BluetoothA2dpSourceServer::GetActiveSinkDevice()
299 {
300 HILOGI("starts");
301 return pimpl->a2dpSrcService_->GetActiveSinkDevice();
302 }
303
GetCodecStatus(const RawAddress & device)304 BluetoothA2dpCodecStatus BluetoothA2dpSourceServer::GetCodecStatus(const RawAddress &device)
305 {
306 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
307 bluetooth::RawAddress addr(device.GetAddress());
308 bluetooth::A2dpSrcCodecStatus ret;
309 BluetoothA2dpCodecStatus codeStatus;
310 BluetoothA2dpCodecInfo serviceInfo;
311 ret = pimpl->a2dpSrcService_->GetCodecStatus(addr);
312
313 codeStatus.codecInfo.codecPriority = ret.codecInfo.codecPriority;
314 codeStatus.codecInfo.codecType = ret.codecInfo.codecType;
315 codeStatus.codecInfo.sampleRate = ret.codecInfo.sampleRate;
316 codeStatus.codecInfo.bitsPerSample = ret.codecInfo.bitsPerSample;
317 codeStatus.codecInfo.channelMode = ret.codecInfo.channelMode;
318 codeStatus.codecInfo.codecSpecific1 = ret.codecInfo.codecSpecific1;
319 codeStatus.codecInfo.codecSpecific2 = ret.codecInfo.codecSpecific2;
320 codeStatus.codecInfo.codecSpecific3 = ret.codecInfo.codecSpecific3;
321 codeStatus.codecInfo.codecSpecific4 = ret.codecInfo.codecSpecific4;
322
323 for (auto it = ret.codecInfoConfirmedCap.begin(); it != ret.codecInfoConfirmedCap.end(); it++) {
324 serviceInfo.codecPriority = it->codecPriority;
325 serviceInfo.codecType = it->codecType;
326 serviceInfo.sampleRate = it->sampleRate;
327 serviceInfo.bitsPerSample = it->bitsPerSample;
328 serviceInfo.channelMode = it->channelMode;
329 serviceInfo.codecSpecific1 = it->codecSpecific1;
330 serviceInfo.codecSpecific2 = it->codecSpecific2;
331 serviceInfo.codecSpecific3 = it->codecSpecific3;
332 serviceInfo.codecSpecific4 = it->codecSpecific4;
333 codeStatus.codecInfoConfirmCap.push_back(serviceInfo);
334 }
335
336 for (auto it = ret.codecInfoLocalCap.begin(); it != ret.codecInfoLocalCap.end(); it++) {
337 serviceInfo.codecPriority = it->codecPriority;
338 serviceInfo.codecType = it->codecType;
339 serviceInfo.sampleRate = it->sampleRate;
340 serviceInfo.bitsPerSample = it->bitsPerSample;
341 serviceInfo.channelMode = it->channelMode;
342 serviceInfo.codecSpecific1 = it->codecSpecific1;
343 serviceInfo.codecSpecific2 = it->codecSpecific2;
344 serviceInfo.codecSpecific3 = it->codecSpecific3;
345 serviceInfo.codecSpecific4 = it->codecSpecific4;
346 codeStatus.codecInfoLocalCap.push_back(serviceInfo);
347 }
348
349 return codeStatus;
350 }
351
GetCodecPreference(const RawAddress & device,BluetoothA2dpCodecInfo & info)352 int BluetoothA2dpSourceServer::GetCodecPreference(const RawAddress &device, BluetoothA2dpCodecInfo &info)
353 {
354 return NO_ERROR;
355 }
356
SetCodecPreference(const RawAddress & device,const BluetoothA2dpCodecInfo & info)357 int BluetoothA2dpSourceServer::SetCodecPreference(const RawAddress &device, const BluetoothA2dpCodecInfo &info)
358 {
359 HILOGI("BluetoothA2dpSourceServer::SetCodecPreference starts, codecPriority = %{public}u,"
360 "codecPriority = %{public}u, sampleRate = %{public}u, bitsPerSample = %{public}d, "
361 "channelMode = %{public}d, codecSpecific1 = %{public}llu, codecSpecific2 = %{public}llu, "
362 "codecSpecific3 = %{public}llu, codecSpecific4 = %{public}llu",
363 info.codecPriority, info.codecType, info.sampleRate, info.bitsPerSample, info.channelMode,
364 (unsigned long long)info.codecSpecific1, (unsigned long long)info.codecSpecific2,
365 (unsigned long long)info.codecSpecific3, (unsigned long long)info.codecSpecific4);
366 bluetooth::A2dpSrcCodecInfo setInfo;
367
368 setInfo.bitsPerSample = info.bitsPerSample;
369 setInfo.channelMode = info.channelMode;
370 setInfo.codecPriority = info.codecPriority;
371 setInfo.codecType = info.codecType;
372 setInfo.sampleRate = info.sampleRate;
373 setInfo.codecSpecific1 = info.codecSpecific1;
374 setInfo.codecSpecific2 = info.codecSpecific2;
375 setInfo.codecSpecific3 = info.codecSpecific3;
376 setInfo.codecSpecific4 = info.codecSpecific4;
377
378 return pimpl->a2dpSrcService_->SetCodecPreference(device, setInfo);
379 }
380
SwitchOptionalCodecs(const RawAddress & device,bool isEnable)381 void BluetoothA2dpSourceServer::SwitchOptionalCodecs(const RawAddress &device, bool isEnable)
382 {
383 HILOGI("addr: %{public}s, isEnable = %{public}d", GET_ENCRYPT_ADDR(device), isEnable);
384 pimpl->a2dpSrcService_->SwitchOptionalCodecs(device, isEnable);
385 }
386
GetOptionalCodecsSupportState(const RawAddress & device)387 int BluetoothA2dpSourceServer::GetOptionalCodecsSupportState(const RawAddress &device)
388 {
389 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
390 return pimpl->a2dpSrcService_->GetOptionalCodecsSupportState(device);
391 }
392
StartPlaying(const RawAddress & device)393 int BluetoothA2dpSourceServer::StartPlaying(const RawAddress &device)
394 {
395 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
396 return pimpl->a2dpSrcService_->StartPlaying(device);
397 }
398
SuspendPlaying(const RawAddress & device)399 int BluetoothA2dpSourceServer::SuspendPlaying(const RawAddress &device)
400 {
401 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
402 return pimpl->a2dpSrcService_->SuspendPlaying(device);
403 }
404
StopPlaying(const RawAddress & device)405 int BluetoothA2dpSourceServer::StopPlaying(const RawAddress &device)
406 {
407 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
408 return pimpl->a2dpSrcService_->StopPlaying(device);
409 }
410
WriteFrame(const uint8_t * data,uint32_t size)411 int BluetoothA2dpSourceServer::WriteFrame(const uint8_t *data, uint32_t size)
412 {
413 HILOGI("size = %{public}u", size);
414 return pimpl->a2dpSrcService_->WriteFrame(data, size);
415 }
416
GetRenderPosition(uint16_t & delayValue,uint16_t & sendDataSize,uint32_t & timeStamp)417 void BluetoothA2dpSourceServer::GetRenderPosition(uint16_t &delayValue, uint16_t &sendDataSize, uint32_t &timeStamp)
418 {
419 HILOGI("starts");
420 pimpl->a2dpSrcService_->GetRenderPosition(delayValue, sendDataSize, timeStamp);
421 HILOGI("delayValue = %{public}hu, sendDataSize = %{public}hu, timeStamp = %{public}u", delayValue, sendDataSize,
422 timeStamp);
423 }
424
OffloadStartPlaying(const RawAddress & device,const std::vector<int32_t> & sessionsId)425 int BluetoothA2dpSourceServer::OffloadStartPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
426 {
427 return BT_ERR_API_NOT_SUPPORT;
428 }
429
OffloadStopPlaying(const RawAddress & device,const std::vector<int32_t> & sessionsId)430 int BluetoothA2dpSourceServer::OffloadStopPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
431 {
432 return BT_ERR_API_NOT_SUPPORT;
433 }
A2dpOffloadSessionPathRequest(const RawAddress & device,const std::vector<BluetoothA2dpStreamInfo> & info)434 int BluetoothA2dpSourceServer::A2dpOffloadSessionPathRequest(const RawAddress &device,
435 const std::vector<BluetoothA2dpStreamInfo> &info)
436 {
437 return BT_ERR_API_NOT_SUPPORT;
438 }
439
GetOffloadCodecStatus(const RawAddress & device)440 BluetoothA2dpOffloadCodecStatus BluetoothA2dpSourceServer::GetOffloadCodecStatus(const RawAddress &device)
441 {
442 BluetoothA2dpOffloadCodecStatus ret;
443 HILOGI("enter");
444 return ret;
445 }
446 } // namespace Bluetooth
447 } // namespace OHOS
448