• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_observer_list.h"
25 #include "iservice_registry.h"
26 #include "raw_address.h"
27 #include "system_ability_definition.h"
28 #include "bluetooth_host_proxy.h"
29 #include "bluetooth_log.h"
30 
31 namespace OHOS {
32 namespace Bluetooth {
33 class BluetoothPbapPseObserverImp : public BluetoothPbapPseObserverStub {
34 public:
35     BluetoothPbapPseObserverImp() = default;
36     virtual ~BluetoothPbapPseObserverImp() override = default;
SetObserver(BluetoothObserverList<PbapObserver> * observers)37     void SetObserver(BluetoothObserverList<PbapObserver> *observers)
38     {
39         observers_ = observers;
40     }
41 
OnServiceConnectionStateChanged(const BluetoothRawAddress & device,int state)42     void OnServiceConnectionStateChanged(const BluetoothRawAddress &device, int state) override
43     {
44         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
45         observers_->ForEach([device, state](std::shared_ptr<PbapObserver> observer) {
46             BluetoothRemoteDevice dev(device.GetAddress(), 0);
47             observer->OnServiceConnectionStateChanged(dev, state);
48         });
49     }
50 
OnServicePermission(const BluetoothRawAddress & device)51     void OnServicePermission(const BluetoothRawAddress &device) override
52     {
53         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
54         observers_->ForEach([device](std::shared_ptr<PbapObserver> observer) {
55             BluetoothRemoteDevice dev(device.GetAddress(), 0);
56             observer->OnServicePermission(dev);
57         });
58     }
OnServicePasswordRequired(const BluetoothRawAddress & device,const::std::vector<uint8_t> & description,int8_t charset,bool fullAccess)59     void OnServicePasswordRequired(const BluetoothRawAddress &device,
60         const ::std::vector<uint8_t> &description, int8_t charset, bool fullAccess) override
61     {
62         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
63         observers_->ForEach([device, description, charset, fullAccess](std::shared_ptr<PbapObserver> observer) {
64             BluetoothRemoteDevice dev(device.GetAddress(), 0);
65             observer->OnServicePasswordRequired(dev, description, charset, fullAccess);
66         });
67     }
68 
69 private:
70     BluetoothObserverList<PbapObserver> *observers_;
71 };
72 
73 std::string PbapPseServiceName = "bluetooth-pbap-pse-server";
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         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
92         if (observer) {
93             observers_.Register(observer);
94         }
95     }
96 
DeregisterObserverOHOS::Bluetooth::PbapServer::impl97     void DeregisterObserver(std::shared_ptr<PbapObserver> &observer)
98     {
99         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
100         if (observer) {
101             observers_.Deregister(observer);
102         }
103     }
104 
GetDeviceStateOHOS::Bluetooth::PbapServer::impl105     int GetDeviceState(const BluetoothRemoteDevice &device)
106     {
107         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
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         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
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         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
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         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
147         if (!device.IsValidBluetoothRemoteDevice()) {
148             return false;
149         }
150         if (proxy_ != nullptr && IS_BT_ENABLED()) {
151             int32_t ret = RET_NO_SUPPORT;
152             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         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
161         if (!device.IsValidBluetoothRemoteDevice()) {
162             return false;
163         }
164         if (proxy_ != nullptr && IS_BT_ENABLED()) {
165             int32_t ret = RET_NO_SUPPORT;
166             ret = proxy_->SetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()), strategy);
167             return ret == RET_NO_ERROR;
168         }
169         return false;
170     }
171 
GetConnectionStrategyOHOS::Bluetooth::PbapServer::impl172     int GetConnectionStrategy(const BluetoothRemoteDevice &device) const
173     {
174         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
175         if (!device.IsValidBluetoothRemoteDevice()) {
176             return static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
177         }
178         int32_t ret = static_cast<int>(BTStrategyType::CONNECTION_FORBIDDEN);
179         if (proxy_ != nullptr && IS_BT_ENABLED()) {
180             ret = proxy_->GetConnectionStrategy(bluetooth::RawAddress(device.GetDeviceAddr()));
181         }
182         return (int)ret;
183     }
184 
GrantPermissionOHOS::Bluetooth::PbapServer::impl185     void GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
186     {
187         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
188         if (!device.IsValidBluetoothRemoteDevice()) {
189             return;
190         }
191         if (proxy_ != nullptr && IS_BT_ENABLED()) {
192             proxy_->GrantPermission(bluetooth::RawAddress(device.GetDeviceAddr()), allow, save);
193         }
194     }
SetDevicePasswordOHOS::Bluetooth::PbapServer::impl195     int SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
196     {
197         HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
198         if (!device.IsValidBluetoothRemoteDevice()) {
199             return RET_BAD_PARAM;
200         }
201         int32_t ret = RET_NO_SUPPORT;
202         if (proxy_ != nullptr && IS_BT_ENABLED()) {
203             ret = proxy_->SetDevicePassword(bluetooth::RawAddress(device.GetDeviceAddr()), password, userId);
204         }
205         return (int)ret;
206     }
207 
208 private:
209     std::mutex mutex_;
210     BluetoothObserverList<PbapObserver> observers_;
211     sptr<BluetoothPbapPseObserverImp> serviceObserver_;
212 };
213 
214 class PbapServer::impl::BluetoothPbapPseDeathRecipient final : public IRemoteObject::DeathRecipient {
215 public:
BluetoothPbapPseDeathRecipient(PbapServer::impl & pbapPse)216     BluetoothPbapPseDeathRecipient(PbapServer::impl &pbapPse) : pbapPse_(pbapPse) {};
217     ~BluetoothPbapPseDeathRecipient() final = default;
218     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothPbapPseDeathRecipient);
219 
OnRemoteDied(const wptr<IRemoteObject> & remote)220     void OnRemoteDied(const wptr<IRemoteObject> &remote) final
221     {
222         HILOGI("PbapServer::impl::BluetoothPbapPseDeathRecipient::OnRemoteDied starts");
223         pbapPse_.proxy_->AsObject()->RemoveDeathRecipient(pbapPse_.deathRecipient_);
224         pbapPse_.proxy_ = nullptr;
225     }
226 
227 private:
228     PbapServer::impl &pbapPse_;
229 };
230 
impl()231 PbapServer::impl::impl()
232 {
233     serviceObserver_ = new BluetoothPbapPseObserverImp();
234     serviceObserver_->SetObserver(&observers_);
235     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
236     sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
237 
238     if (!hostRemote) {
239         HILOGI("PbapServer::impl:impl() failed: no hostRemote");
240         return;
241     }
242     sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
243     sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_PBAP_PSE);
244 
245     if (!remote) {
246         HILOGE("PbapServer::impl:impl() failed: no remote");
247         return;
248     }
249     HILOGI("PbapServer::impl:impl() remote obtained");
250 
251     proxy_ = iface_cast<IBluetoothPbapPse>(remote);
252 
253     deathRecipient_ = new BluetoothPbapPseDeathRecipient(*this);
254     proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
255 
256     if (proxy_ != nullptr) {
257         proxy_->RegisterObserver(serviceObserver_);
258     }
259 }
260 
GetProfile()261 PbapServer *PbapServer::GetProfile()
262 {
263     static PbapServer instance;
264     return &instance;
265 }
266 
PbapServer()267 PbapServer::PbapServer()
268 {
269     pimpl = std::make_unique<impl>();
270 }
271 
~PbapServer()272 PbapServer::~PbapServer()
273 {}
274 
RegisterObserver(PbapObserver * observer)275 void PbapServer::RegisterObserver(PbapObserver *observer)
276 {
277     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
278     std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
279     return pimpl->RegisterObserver(pointer);
280 }
281 
DeregisterObserver(PbapObserver * observer)282 void PbapServer::DeregisterObserver(PbapObserver *observer)
283 {
284     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
285     std::shared_ptr<PbapObserver> pointer(observer, [](PbapObserver *) {});
286     return pimpl->DeregisterObserver(pointer);
287 }
288 
GetDeviceState(const BluetoothRemoteDevice & device)289 int PbapServer::GetDeviceState(const BluetoothRemoteDevice &device)
290 {
291     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
292     return pimpl->GetDeviceState(device);
293 }
294 
GetDevicesByStates(const std::vector<int> & states)295 std::vector<BluetoothRemoteDevice> PbapServer::GetDevicesByStates(const std::vector<int> &states)
296 {
297     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
298     return pimpl->GetDevicesByStates(states);
299 }
300 
GetConnectedDevices()301 std::vector<BluetoothRemoteDevice> PbapServer::GetConnectedDevices()
302 {
303     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
304     return pimpl->GetConnectedDevices();
305 }
306 
Disconnect(const BluetoothRemoteDevice & device)307 bool PbapServer::Disconnect(const BluetoothRemoteDevice &device)
308 {
309     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
310     return pimpl->Disconnect(device);
311 }
312 
SetConnectionStrategy(const BluetoothRemoteDevice & device,int strategy)313 bool PbapServer::SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy)
314 {
315     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
316     return pimpl->SetConnectionStrategy(device, strategy);
317 }
318 
GetConnectionStrategy(const BluetoothRemoteDevice & device) const319 int PbapServer::GetConnectionStrategy(const BluetoothRemoteDevice &device) const
320 {
321     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
322     return pimpl->GetConnectionStrategy(device);
323 }
324 
GrantPermission(const BluetoothRemoteDevice & device,bool allow,bool save)325 void PbapServer::GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save)
326 {
327     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
328     pimpl->GrantPermission(device, allow, save);
329 }
330 
SetDevicePassword(const BluetoothRemoteDevice & device,const std::string & password,std::string userId)331 int PbapServer::SetDevicePassword(const BluetoothRemoteDevice &device, const std::string &password, std::string userId)
332 {
333     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
334     return pimpl->SetDevicePassword(device, password, userId);
335 }
336 }  // namespace Bluetooth
337 }  // namespace OHOS