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_utils.h"
25 #include "bluetooth_observer_list.h"
26 #include "iservice_registry.h"
27 #include "raw_address.h"
28 #include "system_ability_definition.h"
29 #include "bluetooth_host_proxy.h"
30 #include "bluetooth_log.h"
31
32 namespace OHOS {
33 namespace Bluetooth {
34 class BluetoothPbapPseObserverImp : public BluetoothPbapPseObserverStub {
35 public:
36 BluetoothPbapPseObserverImp() = default;
37 ~BluetoothPbapPseObserverImp() override = default;
SetObserver(BluetoothObserverList<PbapObserver> * observers)38 void SetObserver(BluetoothObserverList<PbapObserver> *observers)
39 {
40 observers_ = observers;
41 }
42
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)43 void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
44 {
45 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
46 observers_->ForEach([device, state](std::shared_ptr<PbapObserver> observer) {
47 BluetoothRemoteDevice dev(device.GetAddress(), 0);
48 observer->OnServiceConnectionStateChanged(dev, state);
49 });
50 }
51
OnServicePermission(const BluetoothRawAddress & device)52 void OnServicePermission(const BluetoothRawAddress &device) override
53 {
54 HILOGI("enter, device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
55 observers_->ForEach([device](std::shared_ptr<PbapObserver> observer) {
56 BluetoothRemoteDevice dev(device.GetAddress(), 0);
57 observer->OnServicePermission(dev);
58 });
59 }
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,int8_t charset,bool fullAccess)60 void OnServicePasswordRequired(const BluetoothRawAddress &device,
61 const ::std::vector<uint8_t> &description, int8_t charset, bool fullAccess) override
62 {
63 HILOGI("enter, device: %{public}s, charset: %{public}d, fullAccess: %{public}d",
64 GetEncryptAddr((device).GetAddress()).c_str(), charset, fullAccess);
65 observers_->ForEach([device, description, charset, fullAccess](std::shared_ptr<PbapObserver> observer) {
66 BluetoothRemoteDevice dev(device.GetAddress(), 0);
67 observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
68 });
69 }
70
71 private:
72 BluetoothObserverList<PbapObserver> *observers_;
73 };
74
75 struct PbapServer::impl {
76 sptr<IBluetoothPbapPse> proxy_;
77 class BluetoothPbapPseDeathRecipient;
78 sptr<BluetoothPbapPseDeathRecipient> deathRecipient_ = nullptr;
79 impl();
80
~implOHOS::Bluetooth::PbapServer::impl81 ~impl()
82 {
83 if (proxy_ != nullptr) {
84 proxy_->DeregisterObserver(serviceObserver_);
85 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
86 }
87 }
88
RegisterObserverOHOS::Bluetooth::PbapServer::impl89 void RegisterObserver(std::shared_ptr<PbapObserver> &observer)
90 {
91 HILOGI("enter");
92 if (observer) {
93 observers_.Register(observer);
94 }
95 }
96
DeregisterObserverOHOS::Bluetooth::PbapServer::impl97 void DeregisterObserver(std::shared_ptr<PbapObserver> &observer)
98 {
99 HILOGI("enter");
100 if (observer) {
101 observers_.Deregister(observer);
102 }
103 }
104
GetDeviceStateOHOS::Bluetooth::PbapServer::impl105 int GetDeviceState(const BluetoothRemoteDevice &device)
106 {
107 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
108 if (!device.IsValidBluetoothRemoteDevice()) {
109 return static_cast<int>(BTConnectState::DISCONNECTED);
110 }
111 int32_t state = static_cast<int>(BTConnectState::DISCONNECTED);
112 if (proxy_ != nullptr && IS_BT_ENABLED()) {
113 state = proxy_->GetDeviceState(bluetooth::RawAddress(device.GetDeviceAddr()));
114 }
115 return (int)state;
116 }
117
GetDevicesByStatesOHOS::Bluetooth::PbapServer::impl118 std::vector<BluetoothRemoteDevice> GetDevicesByStates(const std::vector<int> &states)
119 {
120 HILOGI("enter");
121 std::vector<BluetoothRemoteDevice> remoteDevices;
122 if (proxy_ != nullptr && IS_BT_ENABLED()) {
123 std::vector<int32_t> tmpStates;
124 for (int state : states) {
125 tmpStates.push_back((int32_t)state);
126 }
127 std::vector<BluetoothRawAddress> rawDevices;
128 proxy_->GetDevicesByStates(tmpStates, rawDevices);
129 for (BluetoothRawAddress rawDevice : rawDevices) {
130 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
131 remoteDevices.push_back(remoteDevice);
132 }
133 }
134 return remoteDevices;
135 }
136
GetConnectedDevicesOHOS::Bluetooth::PbapServer::impl137 std::vector<BluetoothRemoteDevice> GetConnectedDevices()
138 {
139 HILOGI("enter");
140 std::vector<int> states {static_cast<int>(BTConnectState::CONNECTED)};
141 return GetDevicesByStates(states);
142 }
143
DisconnectOHOS::Bluetooth::PbapServer::impl144 bool Disconnect(const BluetoothRemoteDevice &device)
145 {
146 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
147 if (!device.IsValidBluetoothRemoteDevice()) {
148 return false;
149 }
150 if (proxy_ != nullptr && IS_BT_ENABLED()) {
151 int32_t ret = proxy_->Disconnect(bluetooth::RawAddress(device.GetDeviceAddr()));
152 return ret == RET_NO_ERROR;
153 }
154 return false;
155 }
156
SetConnectionStrategyOHOS::Bluetooth::PbapServer::impl157 bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
158 {
159 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
160 if (!device.IsValidBluetoothRemoteDevice()) {
161 return false;
162 }
163 if (proxy_ != nullptr && IS_BT_ENABLED()) {
164 int32_t ret = proxy_->SetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()), strategy);
165 return ret == RET_NO_ERROR;
166 }
167 return false;
168 }
169
GetConnectionStrategyOHOS::Bluetooth::PbapServer::impl170 int GetConnectionStrategy(const BluetoothRemoteDevice &device) const
171 {
172 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
173 if (!device.IsValidBluetoothRemoteDevice()) {
174 return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
175 }
176 int32_t ret = static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
177 if (proxy_ != nullptr && IS_BT_ENABLED()) {
178 ret = proxy_->GetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()));
179 }
180 return (int)ret;
181 }
182
GrantPermissionOHOS::Bluetooth::PbapServer::impl183 void GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
184 {
185 HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save);
186 if (!device.IsValidBluetoothRemoteDevice()) {
187 return;
188 }
189 if (proxy_ != nullptr && IS_BT_ENABLED()) {
190 proxy_->GrantPermission(bluetooth::RawAddress(device.GetDeviceAddr()), allow, save);
191 }
192 }
SetDevicePasswordOHOS::Bluetooth::PbapServer::impl193 int SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
194 {
195 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
196 if (!device.IsValidBluetoothRemoteDevice()) {
197 return RET_BAD_PARAM;
198 }
199 int32_t ret = RET_NO_SUPPORT;
200 if (proxy_ != nullptr && IS_BT_ENABLED()) {
201 ret = proxy_->SetDevicePassword(bluetooth::RawAddress(device.GetDeviceAddr()), password, userId);
202 }
203 return (int)ret;
204 }
205
206 private:
207 std::mutex mutex_;
208 BluetoothObserverList<PbapObserver> observers_;
209 sptr<BluetoothPbapPseObserverImp> serviceObserver_;
210 };
211
212 class PbapServer::impl::BluetoothPbapPseDeathRecipient final : public IRemoteObject::DeathRecipient {
213 public:
BluetoothPbapPseDeathRecipient(PbapServer::impl & pbapPse)214 BluetoothPbapPseDeathRecipient(PbapServer::impl &pbapPse) : pbapPse_(pbapPse) {};
215 ~BluetoothPbapPseDeathRecipient() final = default;
216 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPseDeathRecipient);
217
OnRemoteDied(const wptr<IRemoteObject> & remote)218 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
219 {
220 HILOGI("starts");
221 pbapPse_.proxy_->AsObject()->RemoveDeathRecipient(pbapPse_.deathRecipient_);
222 pbapPse_.proxy_ = nullptr;
223 }
224
225 private:
226 PbapServer::impl &pbapPse_;
227 };
228
impl()229 PbapServer::impl::impl()
230 {
231 serviceObserver_ = new BluetoothPbapPseObserverImp();
232 serviceObserver_->SetObserver(&observers_);
233 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
234 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
235
236 if (!hostRemote) {
237 HILOGI("failed: no hostRemote");
238 return;
239 }
240 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
241 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_PBAP_PSE);
242
243 if (!remote) {
244 HILOGE("failed: no remote");
245 return;
246 }
247 HILOGI("remote obtained");
248
249 proxy_ = iface_cast<IBluetoothPbapPse>(remote);
250 if (proxy_ == nullptr) {
251 return;
252 }
253 deathRecipient_ = new BluetoothPbapPseDeathRecipient(*this);
254 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
255 proxy_->RegisterObserver(serviceObserver_);
256 }
257
GetProfile()258 PbapServer *PbapServer::GetProfile()
259 {
260 static PbapServer instance;
261 return &instance;
262 }
263
PbapServer()264 PbapServer::PbapServer()
265 {
266 pimpl = std::make_unique<impl>();
267 }
268
~PbapServer()269 PbapServer::~PbapServer()
270 {}
271
RegisterObserver(PbapObserver * observer)272 void PbapServer::RegisterObserver(PbapObserver *observer)
273 {
274 HILOGI("enter");
275 std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
276 return pimpl->RegisterObserver(pointer);
277 }
278
DeregisterObserver(PbapObserver * observer)279 void PbapServer::DeregisterObserver(PbapObserver *observer)
280 {
281 HILOGI("enter");
282 std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
283 return pimpl->DeregisterObserver(pointer);
284 }
285
GetDeviceState(const BluetoothRemoteDevice & device)286 int PbapServer::GetDeviceState(const BluetoothRemoteDevice &device)
287 {
288 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
289 return pimpl->GetDeviceState(device);
290 }
291
GetDevicesByStates(const std::vector<int> & states)292 std::vector<BluetoothRemoteDevice> PbapServer::GetDevicesByStates(const std::vector<int> &states)
293 {
294 HILOGI("enter");
295 return pimpl->GetDevicesByStates(states);
296 }
297
GetConnectedDevices()298 std::vector<BluetoothRemoteDevice> PbapServer::GetConnectedDevices()
299 {
300 HILOGI("enter");
301 return pimpl->GetConnectedDevices();
302 }
303
Disconnect(const BluetoothRemoteDevice & device)304 bool PbapServer::Disconnect(const BluetoothRemoteDevice &device)
305 {
306 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
307 return pimpl->Disconnect(device);
308 }
309
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)310 bool PbapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
311 {
312 HILOGI("enter, device: %{public}s, strategy: %{public}d", GET_ENCRYPT_ADDR(device), strategy);
313 return pimpl->SetConnectionStrategy(device, strategy);
314 }
315
GetConnectionStrategy(const BluetoothRemoteDevice & device) const316 int PbapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device) const
317 {
318 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
319 return pimpl->GetConnectionStrategy(device);
320 }
321
GrantPermission(const BluetoothRemoteDevice & device,bool allow,bool save)322 void PbapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
323 {
324 HILOGI("enter, device: %{public}s, allow: %{public}d, save: %{public}d", GET_ENCRYPT_ADDR(device), allow, save);
325 pimpl->GrantPermission(device, allow, save);
326 }
327
SetDevicePassword(const BluetoothRemoteDevice & device,const std::string & password,std::string userId)328 int PbapServer::SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
329 {
330 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
331 return pimpl->SetDevicePassword(device, password, userId);
332 }
333 } // namespace Bluetooth
334 } // namespace OHOS