1 /*
2 * Copyright (C) 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_pan.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_host.h"
19 #include "bluetooth_load_system_ability.h"
20 #include "bluetooth_log.h"
21 #include "bluetooth_observer_list.h"
22 #include "bluetooth_pan_observer_stub.h"
23 #include "i_bluetooth_pan.h"
24 #include "i_bluetooth_host.h"
25 #include "bluetooth_utils.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace Bluetooth {
31 std::mutex g_proxyMutex;
32 class PanInnerObserver : public BluetoothPanObserverStub {
33 public:
PanInnerObserver(BluetoothObserverList<PanObserver> & observers)34 explicit PanInnerObserver(BluetoothObserverList<PanObserver> &observers) : observers_(observers)
35 {
36 HILOGI("enter");
37 }
~PanInnerObserver()38 ~PanInnerObserver() override
39 {
40 HILOGI("enter");
41 }
42
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)43 ErrCode OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
44 {
45 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
46 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 1);
47 observers_.ForEach([remoteDevice, state](std::shared_ptr<PanObserver> observer) {
48 observer->OnConnectionStateChanged(remoteDevice, state);
49 });
50 return NO_ERROR;
51 }
52
53 private:
54 BluetoothObserverList<PanObserver> &observers_;
55 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PanInnerObserver);
56 };
57
58 struct Pan::impl {
59 impl();
60 ~impl();
61 bool InitPanProxy(void);
62
GetDevicesByStatesOHOS::Bluetooth::Pan::impl63 int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice>& result)
64 {
65 HILOGI("enter");
66 if (!proxy_) {
67 HILOGE("proxy_ is nullptr.");
68 return BT_ERR_SERVICE_DISCONNECTED;
69 }
70
71 std::vector<BluetoothRawAddress> rawDevices;
72 std::vector<int32_t> tmpStates;
73 for (int32_t state : states) {
74 tmpStates.push_back((int32_t)state);
75 }
76
77 int32_t ret = proxy_->GetDevicesByStates(tmpStates, rawDevices);
78 if (ret != BT_NO_ERROR) {
79 HILOGE("inner error.");
80 return ret;
81 }
82
83 for (BluetoothRawAddress rawDevice : rawDevices) {
84 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 1);
85 result.push_back(remoteDevice);
86 }
87
88 return BT_NO_ERROR;
89 }
90
GetDeviceStateOHOS::Bluetooth::Pan::impl91 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
92 {
93 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
94 if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
95 HILOGE("invalid param.");
96 return BT_ERR_INVALID_PARAM;
97 }
98
99 return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
100 }
101
DisconnectOHOS::Bluetooth::Pan::impl102 int32_t Disconnect(const BluetoothRemoteDevice &device)
103 {
104 HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
105 if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
106 HILOGE("invalid param.");
107 return BT_ERR_INVALID_PARAM;
108 }
109
110 return proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
111 }
112
RegisterObserverOHOS::Bluetooth::Pan::impl113 void RegisterObserver(std::shared_ptr<PanObserver> observer)
114 {
115 HILOGI("enter");
116 observers_.Register(observer);
117 }
118
DeregisterObserverOHOS::Bluetooth::Pan::impl119 void DeregisterObserver(std::shared_ptr<PanObserver> observer)
120 {
121 HILOGI("enter");
122 observers_.Deregister(observer);
123 }
124
SetTetheringOHOS::Bluetooth::Pan::impl125 int32_t SetTethering(bool value)
126 {
127 HILOGI("enter");
128 if (!proxy_) {
129 HILOGE("proxy_ is nullptr.");
130 return BT_ERR_SERVICE_DISCONNECTED;
131 }
132
133 int32_t ret = proxy_->SetTethering(value);
134 HILOGI("fwk ret:%{public}d", ret);
135 return ret;
136 }
137
IsTetheringOnOHOS::Bluetooth::Pan::impl138 int32_t IsTetheringOn(bool &value)
139 {
140 HILOGI("enter");
141 if (!proxy_) {
142 HILOGE("proxy_ is nullptr.");
143 return BT_ERR_SERVICE_DISCONNECTED;
144 }
145
146 return proxy_->IsTetheringOn(value);
147 }
148 sptr<IBluetoothPan> proxy_;
149 private:
150
151 BluetoothObserverList<PanObserver> observers_;
152 sptr<PanInnerObserver> innerObserver_;
153 class PanDeathRecipient;
154 sptr<PanDeathRecipient> deathRecipient_;
155 };
156
157 class Pan::impl::PanDeathRecipient final : public IRemoteObject::DeathRecipient {
158 public:
PanDeathRecipient(Pan::impl & impl)159 explicit PanDeathRecipient(Pan::impl &impl) : impl_(impl)
160 {};
161 ~PanDeathRecipient() final = default;
162 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PanDeathRecipient);
163
OnRemoteDied(const wptr<IRemoteObject> & remote)164 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
165 {
166 HILOGI("starts");
167 std::lock_guard<std::mutex> lock(g_proxyMutex);
168 if (!impl_.proxy_) {
169 return;
170 }
171 impl_.proxy_ = nullptr;
172 }
173
174 private:
175 Pan::impl &impl_;
176 };
177
impl()178 Pan::impl::impl()
179 {
180 if (proxy_) {
181 return;
182 }
183 BluetootLoadSystemAbility::GetInstance().RegisterNotifyMsg(PROFILE_ID_PAN_SERVER);
184 if (!BluetootLoadSystemAbility::GetInstance().HasSubscribedBluetoothSystemAbility()) {
185 BluetootLoadSystemAbility::GetInstance().SubScribeBluetoothSystemAbility();
186 return;
187 }
188 InitPanProxy();
189 }
190
~impl()191 Pan::impl::~impl()
192 {
193 HILOGI("enter");
194 if (proxy_ != nullptr) {
195 proxy_->DeregisterObserver(innerObserver_);
196 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
197 }
198 }
199
InitPanProxy(void)200 bool Pan::impl::InitPanProxy(void)
201 {
202 std::lock_guard<std::mutex> lock(g_proxyMutex);
203 if (proxy_) {
204 return true;
205 }
206 HILOGI("enter");
207 proxy_ = GetRemoteProxy<IBluetoothPan>(PROFILE_PAN_SERVER);
208 if (!proxy_) {
209 HILOGE("get Pan proxy_ failed");
210 return false;
211 }
212
213 innerObserver_ = new PanInnerObserver(observers_);
214 if (innerObserver_ != nullptr) {
215 proxy_->RegisterObserver(innerObserver_);
216 }
217
218 deathRecipient_ = new PanDeathRecipient(*this);
219 if (deathRecipient_ != nullptr) {
220 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
221 }
222 return true;
223 }
224
Pan()225 Pan::Pan()
226 {
227 pimpl = std::make_unique<impl>();
228 }
229
~Pan()230 Pan::~Pan()
231 {}
232
Init()233 void Pan::Init()
234 {
235 if (!pimpl) {
236 HILOGE("fails: no pimpl");
237 return;
238 }
239 if (!pimpl->InitPanProxy()) {
240 HILOGE("Pan proxy_ is nullptr");
241 return;
242 }
243 }
244
GetProfile()245 Pan *Pan::GetProfile()
246 {
247 static Pan instance;
248 return &instance;
249 }
250
GetDevicesByStates(std::vector<int> states,std::vector<BluetoothRemoteDevice> & result)251 int32_t Pan::GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result)
252 {
253 if (!IS_BT_ENABLED()) {
254 HILOGE("bluetooth is off.");
255 return BT_ERR_INVALID_STATE;
256 }
257
258 if (pimpl == nullptr || !pimpl->proxy_) {
259 HILOGE("pimpl or pan proxy_ is nullptr");
260 return BT_ERR_UNAVAILABLE_PROXY;
261 }
262
263 return pimpl->GetDevicesByStates(states, result);
264 }
265
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)266 int32_t Pan::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
267 {
268 if (!IS_BT_ENABLED()) {
269 HILOGE("bluetooth is off.");
270 return BT_ERR_INVALID_STATE;
271 }
272
273 if (pimpl == nullptr || !pimpl->proxy_) {
274 HILOGE("pimpl or pan proxy_ is nullptr");
275 return BT_ERR_UNAVAILABLE_PROXY;
276 }
277
278 return pimpl->GetDeviceState(device, state);
279 }
280
Disconnect(const BluetoothRemoteDevice & device)281 int32_t Pan::Disconnect(const BluetoothRemoteDevice &device)
282 {
283 if (!IS_BT_ENABLED()) {
284 HILOGE("bluetooth is off.");
285 return BT_ERR_INVALID_STATE;
286 }
287
288 if (pimpl == nullptr || !pimpl->proxy_) {
289 HILOGE("pimpl or pan proxy_ is nullptr");
290 return BT_ERR_UNAVAILABLE_PROXY;
291 }
292
293 return pimpl->Disconnect(device);
294 }
295
RegisterObserver(PanObserver * observer)296 void Pan::RegisterObserver(PanObserver *observer)
297 {
298 std::shared_ptr<PanObserver> observerPtr(observer, [](PanObserver *) {});
299 return pimpl->RegisterObserver(observerPtr);
300 }
301
DeregisterObserver(PanObserver * observer)302 void Pan::DeregisterObserver(PanObserver *observer)
303 {
304 std::shared_ptr<PanObserver> observerPtr(observer, [](PanObserver *) {});
305 return pimpl->DeregisterObserver(observerPtr);
306 }
307
SetTethering(bool value)308 int32_t Pan::SetTethering(bool value)
309 {
310 if (!IS_BT_ENABLED()) {
311 HILOGE("bluetooth is off.");
312 return BT_ERR_INVALID_STATE;
313 }
314
315 if (pimpl == nullptr || !pimpl->proxy_) {
316 HILOGE("pimpl or pan proxy_ is nullptr");
317 return BT_ERR_UNAVAILABLE_PROXY;
318 }
319
320 return pimpl->SetTethering(value);
321 }
322
IsTetheringOn(bool & value)323 int32_t Pan::IsTetheringOn(bool &value)
324 {
325 if (!IS_BT_ENABLED()) {
326 HILOGE("bluetooth is off.");
327 return BT_ERR_INVALID_STATE;
328 }
329
330 if (pimpl == nullptr || !pimpl->proxy_) {
331 HILOGE("pimpl or pan proxy_ is nullptr");
332 return BT_ERR_UNAVAILABLE_PROXY;
333 }
334
335 return pimpl->IsTetheringOn(value);
336 }
337 } // namespace Bluetooth
338 } // namespace OHOS