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 <list>
17 #include <mutex>
18 #include <string>
19 #include "bluetooth_pbap_pse_observer_stub.h"
20 #include "bluetooth_pbap_pse_proxy.h"
21 #include "bluetooth_pbap_server.h"
22 #include "bluetooth_remote_device.h"
23 #include "bluetooth_host.h"
24 #include "bluetooth_load_system_ability.h"
25 #include "bluetooth_utils.h"
26 #include "bluetooth_observer_list.h"
27 #include "iservice_registry.h"
28 #include "raw_address.h"
29 #include "system_ability_definition.h"
30 #include "bluetooth_host_proxy.h"
31 #include "bluetooth_log.h"
32
33 namespace OHOS {
34 namespace Bluetooth {
35 class BluetoothPbapPseObserverImp : public BluetoothPbapPseObserverStub {
36 public:
37 BluetoothPbapPseObserverImp() = default;
38 ~BluetoothPbapPseObserverImp() override = default;
SetObserver(BluetoothObserverList<PbapObserver> * observers)39 void SetObserver(BluetoothObserverList<PbapObserver> *observers)
40 {
41 observers_ = observers;
42 }
43
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)44 void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
45 {
46 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
47 observers_->ForEach([device, state](std::shared_ptr<PbapObserver> observer) {
48 BluetoothRemoteDevice dev(device.GetAddress(), 0);
49 observer->OnServiceConnectionStateChanged(dev, state);
50 });
51 }
52
OnServicePermission(const BluetoothRawAddress & device)53 void OnServicePermission(const BluetoothRawAddress &device) override
54 {
55 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
56 observers_->ForEach([device](std::shared_ptr<PbapObserver> observer) {
57 BluetoothRemoteDevice dev(device.GetAddress(), 0);
58 observer->OnServicePermission(dev);
59 });
60 }
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,uint8_t charset,bool fullAccess)61 void OnServicePasswordRequired(const BluetoothRawAddress &device,
62 const ::std::vector<uint8_t> &description, uint8_t charset, bool fullAccess) override
63 {
64 HILOGI("enter, device: %{public}s, charset: %{public}d, fullAccess: %{public}d",
65 GetEncryptAddr((device).GetAddress()).c_str(), charset, fullAccess);
66 observers_->ForEach([device, description, charset, fullAccess](std::shared_ptr<PbapObserver> observer) {
67 BluetoothRemoteDevice dev(device.GetAddress(), 0);
68 observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
69 });
70 }
71
72 private:
73 BluetoothObserverList<PbapObserver> *observers_;
74 };
75
76 struct PbapServer::impl {
77 sptr<IBluetoothPbapPse> proxy_;
78 class BluetoothPbapPseDeathRecipient;
79 sptr<BluetoothPbapPseDeathRecipient> deathRecipient_ = nullptr;
80 impl();
81
~implOHOS::Bluetooth::PbapServer::impl82 ~impl()
83 {
84 if (proxy_ != nullptr) {
85 proxy_->DeregisterObserver(serviceObserver_);
86 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
87 }
88 }
89
RegisterObserverOHOS::Bluetooth::PbapServer::impl90 void RegisterObserver(std::shared_ptr<PbapObserver> &observer)
91 {
92 HILOGI("enter");
93 if (observer) {
94 observers_.Register(observer);
95 }
96 }
97
DeregisterObserverOHOS::Bluetooth::PbapServer::impl98 void DeregisterObserver(std::shared_ptr<PbapObserver> &observer)
99 {
100 HILOGI("enter");
101 if (observer) {
102 observers_.Deregister(observer);
103 }
104 }
105
GetDeviceStateOHOS::Bluetooth::PbapServer::impl106 int GetDeviceState(const BluetoothRemoteDevice &device)
107 {
108 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
109 if (!device.IsValidBluetoothRemoteDevice()) {
110 return static_cast<int>(BTConnectState::DISCONNECTED);
111 }
112 int32_t state = static_cast<int>(BTConnectState::DISCONNECTED);
113 if (proxy_ != nullptr && IS_BT_ENABLED()) {
114 state = proxy_->GetDeviceState(bluetooth::RawAddress(device.GetDeviceAddr()));
115 }
116 return static_cast<int>(state);
117 }
118
GetDevicesByStatesOHOS::Bluetooth::PbapServer::impl119 std::vector<BluetoothRemoteDevice> GetDevicesByStates(const std::vector<int> &states)
120 {
121 HILOGI("enter");
122 std::vector<BluetoothRemoteDevice> remoteDevices;
123 if (proxy_ != nullptr && IS_BT_ENABLED()) {
124 std::vector<int32_t> tmpStates;
125 for (int state : states) {
126 tmpStates.push_back((int32_t)state);
127 }
128 std::vector<BluetoothRawAddress> rawDevices;
129 proxy_->GetDevicesByStates(tmpStates, rawDevices);
130 for (BluetoothRawAddress rawDevice : rawDevices) {
131 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
132 remoteDevices.push_back(remoteDevice);
133 }
134 }
135 return remoteDevices;
136 }
137
GetConnectedDevicesOHOS::Bluetooth::PbapServer::impl138 std::vector<BluetoothRemoteDevice> GetConnectedDevices()
139 {
140 HILOGI("enter");
141 std::vector<int> states {static_cast<int>(BTConnectState::CONNECTED)};
142 return GetDevicesByStates(states);
143 }
144
DisconnectOHOS::Bluetooth::PbapServer::impl145 bool Disconnect(const BluetoothRemoteDevice &device)
146 {
147 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
148 if (!device.IsValidBluetoothRemoteDevice()) {
149 return false;
150 }
151 if (proxy_ != nullptr && IS_BT_ENABLED()) {
152 int32_t ret = proxy_->Disconnect(bluetooth::RawAddress(device.GetDeviceAddr()));
153 return ret == RET_NO_ERROR;
154 }
155 return false;
156 }
157
SetConnectionStrategyOHOS::Bluetooth::PbapServer::impl158 bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
159 {
160 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
161 if (!device.IsValidBluetoothRemoteDevice()) {
162 return false;
163 }
164 if (proxy_ != nullptr && IS_BT_ENABLED()) {
165 int32_t ret = proxy_->SetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()), strategy);
166 return ret == RET_NO_ERROR;
167 }
168 return false;
169 }
170
GetConnectionStrategyOHOS::Bluetooth::PbapServer::impl171 int GetConnectionStrategy(const BluetoothRemoteDevice &device)
172 {
173 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
174 if (!device.IsValidBluetoothRemoteDevice()) {
175 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
176 }
177 int32_t ret = static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
178 if (proxy_ != nullptr && IS_BT_ENABLED()) {
179 ret = proxy_->GetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()));
180 }
181 return static_cast<int>(ret);
182 }
183
GrantPermissionOHOS::Bluetooth::PbapServer::impl184 void GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
185 {
186 HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save);
187 if (!device.IsValidBluetoothRemoteDevice()) {
188 return;
189 }
190 if (proxy_ != nullptr && IS_BT_ENABLED()) {
191 proxy_->GrantPermission(bluetooth::RawAddress(device.GetDeviceAddr()), allow, save);
192 }
193 }
SetDevicePasswordOHOS::Bluetooth::PbapServer::impl194 int SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
195 {
196 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
197 if (!device.IsValidBluetoothRemoteDevice()) {
198 return RET_BAD_PARAM;
199 }
200 int32_t ret = RET_NO_SUPPORT;
201 if (proxy_ != nullptr && IS_BT_ENABLED()) {
202 ret = proxy_->SetDevicePassword(bluetooth::RawAddress(device.GetDeviceAddr()), password, userId);
203 }
204 return static_cast<int>(ret);
205 }
206
207 bool InitPbapServerProxy(void);
208
209 private:
210 std::mutex mutex_;
211 BluetoothObserverList<PbapObserver> observers_;
212 sptr<BluetoothPbapPseObserverImp> serviceObserver_;
213 };
214
215 class PbapServer::impl::BluetoothPbapPseDeathRecipient final : public IRemoteObject::DeathRecipient {
216 public:
BluetoothPbapPseDeathRecipient(PbapServer::impl & pbapPse)217 explicit BluetoothPbapPseDeathRecipient(PbapServer::impl &pbapPse) : pbapPse_(pbapPse) {};
218 ~BluetoothPbapPseDeathRecipient() final = default;
219 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPseDeathRecipient);
220
OnRemoteDied(const wptr<IRemoteObject> & remote)221 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
222 {
223 HILOGI("starts");
224 if (!pbapPse_.proxy_) {
225 return;
226 }
227 }
228
229 private:
230 PbapServer::impl &pbapPse_;
231 };
232
impl()233 PbapServer::impl::impl()
234 {
235 if (proxy_) {
236 return;
237 }
238 BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_PBAP_PSE);
239 if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
240 BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
241 return;
242 }
243 InitPbapServerProxy();
244 }
245
InitPbapServerProxy(void)246 bool PbapServer::impl::InitPbapServerProxy(void)
247 {
248 if (proxy_) {
249 return true;
250 }
251
252 proxy_ = GetRemoteProxy<IBluetoothPbapPse>(PROFILE_PBAP_PSE);
253 if (!proxy_) {
254 HILOGE("get PbapServer proxy_ failed");
255 return false;
256 }
257
258 serviceObserver_ = new BluetoothPbapPseObserverImp();
259 serviceObserver_->SetObserver(&observers_);
260 if (serviceObserver_ != nullptr) {
261 proxy_->RegisterObserver(serviceObserver_);
262 }
263
264 deathRecipient_ = new BluetoothPbapPseDeathRecipient(*this);
265 if (deathRecipient_ != nullptr) {
266 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
267 }
268 return true;
269 }
270
GetProfile()271 PbapServer *PbapServer::GetProfile()
272 {
273 static PbapServer instance;
274 return &instance;
275 }
276
PbapServer()277 PbapServer::PbapServer()
278 {
279 pimpl = std::make_unique<impl>();
280 }
281
~PbapServer()282 PbapServer::~PbapServer()
283 {}
284
Init()285 void PbapServer::Init()
286 {
287 if (!pimpl) {
288 HILOGE("fails: no pimpl");
289 return;
290 }
291 if (!pimpl->InitPbapServerProxy()) {
292 HILOGE("PbapServer proxy_ is nullptr");
293 return;
294 }
295 }
296
RegisterObserver(PbapObserver * observer)297 void PbapServer::RegisterObserver(PbapObserver *observer)
298 {
299 HILOGI("enter");
300 std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
301 return pimpl->RegisterObserver(pointer);
302 }
303
DeregisterObserver(PbapObserver * observer)304 void PbapServer::DeregisterObserver(PbapObserver *observer)
305 {
306 HILOGI("enter");
307 std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
308 return pimpl->DeregisterObserver(pointer);
309 }
310
GetDeviceState(const BluetoothRemoteDevice & device)311 int PbapServer::GetDeviceState(const BluetoothRemoteDevice &device)
312 {
313 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
314 if (!IS_BT_ENABLED()) {
315 HILOGE("bluetooth is off.");
316 return static_cast<int>(BTConnectState::DISCONNECTED);
317 }
318
319 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
320 HILOGE("pimpl or pbapServer proxy_ is nullptr");
321 return static_cast<int>(BTConnectState::DISCONNECTED);
322 }
323
324 return pimpl->GetDeviceState(device);
325 }
326
GetDevicesByStates(const std::vector<int> & states)327 std::vector<BluetoothRemoteDevice> PbapServer::GetDevicesByStates(const std::vector<int> &states)
328 {
329 HILOGI("enter");
330 if (!IS_BT_ENABLED()) {
331 HILOGE("bluetooth is off.");
332 return std::vector<BluetoothRemoteDevice>();
333 }
334
335 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
336 HILOGE("pimpl or pbapServer proxy_ is nullptr");
337 return std::vector<BluetoothRemoteDevice>();
338 }
339
340 return pimpl->GetDevicesByStates(states);
341 }
342
GetConnectedDevices()343 std::vector<BluetoothRemoteDevice> PbapServer::GetConnectedDevices()
344 {
345 HILOGI("enter");
346 if (!IS_BT_ENABLED()) {
347 HILOGE("bluetooth is off.");
348 return std::vector<BluetoothRemoteDevice>();
349 }
350
351 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
352 HILOGE("pimpl or pbapServer proxy_ is nullptr");
353 return std::vector<BluetoothRemoteDevice>();
354 }
355
356 return pimpl->GetConnectedDevices();
357 }
358
Disconnect(const BluetoothRemoteDevice & device)359 bool PbapServer::Disconnect(const BluetoothRemoteDevice &device)
360 {
361 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
362 if (!IS_BT_ENABLED()) {
363 HILOGE("bluetooth is off.");
364 return false;
365 }
366
367 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
368 HILOGE("pimpl or pbapServer proxy_ is nullptr");
369 return false;
370 }
371
372 return pimpl->Disconnect(device);
373 }
374
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)375 bool PbapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
376 {
377 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
378 if (!IS_BT_ENABLED()) {
379 HILOGE("bluetooth is off.");
380 return false;
381 }
382
383 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
384 HILOGE("pimpl or pbapServer proxy_ is nullptr");
385 return false;
386 }
387
388 return pimpl->SetConnectionStrategy(device, strategy);
389 }
390
GetConnectionStrategy(const BluetoothRemoteDevice & device)391 int PbapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device)
392 {
393 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
394 if (!IS_BT_ENABLED()) {
395 HILOGE("bluetooth is off.");
396 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
397 }
398
399 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
400 HILOGE("pimpl or pbapServer proxy_ is nullptr");
401 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
402 }
403
404 return pimpl->GetConnectionStrategy(device);
405 }
406
GrantPermission(const BluetoothRemoteDevice & device,bool allow,bool save)407 void PbapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
408 {
409 HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save);
410 if (!IS_BT_ENABLED()) {
411 HILOGE("bluetooth is off.");
412 return;
413 }
414
415 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
416 HILOGE("pimpl or pbapServer proxy_ is nullptr");
417 return;
418 }
419
420 pimpl->GrantPermission(device, allow, save);
421 }
422
SetDevicePassword(const BluetoothRemoteDevice & device,const std::string & password,std::string userId)423 int PbapServer::SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
424 {
425 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
426 if (!IS_BT_ENABLED()) {
427 HILOGE("bluetooth is off.");
428 return RET_BAD_STATUS;
429 }
430
431 if (pimpl == nullptr || !pimpl->InitPbapServerProxy()) {
432 HILOGE("pimpl or pbapServer proxy_ is nullptr");
433 return RET_BAD_STATUS;
434 }
435
436 return pimpl->SetDevicePassword(device, password, userId);
437 }
438 } // namespace Bluetooth
439 } // namespace OHOS