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_errorcode.h"
17 #include "bluetooth_pan.h"
18 #include "bluetooth_host.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_observer_list.h"
21 #include "bluetooth_pan_observer_stub.h"
22 #include "i_bluetooth_pan.h"
23 #include "i_bluetooth_host.h"
24 #include "bluetooth_utils.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27
28 namespace OHOS {
29 namespace Bluetooth {
30 class PanInnerObserver : public BluetoothPanObserverStub {
31 public:
PanInnerObserver(BluetoothObserverList<PanObserver> & observers)32 explicit PanInnerObserver(BluetoothObserverList<PanObserver> &observers) : observers_(observers)
33 {
34 HILOGI("enter");
35 }
~PanInnerObserver()36 ~PanInnerObserver() override
37 {
38 HILOGI("enter");
39 }
40
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)41 ErrCode OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
42 {
43 HILOGI("enter, device: %{public}s, state: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), state);
44 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 1);
45 observers_.ForEach([remoteDevice, state](std::shared_ptr<PanObserver> observer) {
46 observer->OnConnectionStateChanged(remoteDevice, state);
47 });
48 return NO_ERROR;
49 }
50
51 private:
52 BluetoothObserverList<PanObserver> &observers_;
53 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PanInnerObserver);
54 };
55
56 struct Pan::impl {
57 impl();
58 ~impl();
59
GetDevicesByStatesOHOS::Bluetooth::Pan::impl60 int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice>& result)
61 {
62 HILOGI("enter");
63 if (!proxy_) {
64 HILOGE("proxy_ is nullptr.");
65 return BT_ERR_SERVICE_DISCONNECTED;
66 }
67 if (!IS_BT_ENABLED()) {
68 HILOGE("bluetooth is off.");
69 return BT_ERR_INVALID_STATE;
70 }
71
72 std::vector<BluetoothRawAddress> rawDevices;
73 std::vector<int32_t> tmpStates;
74 for (int32_t state : states) {
75 tmpStates.push_back((int32_t)state);
76 }
77
78 int32_t ret = proxy_->GetDevicesByStates(tmpStates, rawDevices);
79 if (ret != BT_SUCCESS) {
80 HILOGE("inner error.");
81 return ret;
82 }
83
84 for (BluetoothRawAddress rawDevice : rawDevices) {
85 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 1);
86 result.push_back(remoteDevice);
87 }
88
89 return BT_SUCCESS;
90 }
91
GetDeviceStateOHOS::Bluetooth::Pan::impl92 int32_t GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
93 {
94 HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
95 if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
96 HILOGE("invalid param.");
97 return BT_ERR_INVALID_PARAM;
98 }
99 if (!IS_BT_ENABLED()) {
100 HILOGE("bluetooth is off.");
101 return BT_ERR_INVALID_STATE;
102 }
103 return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
104 }
105
DisconnectOHOS::Bluetooth::Pan::impl106 int32_t Disconnect(const BluetoothRemoteDevice &device)
107 {
108 HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
109 if (proxy_ == nullptr || !device.IsValidBluetoothRemoteDevice()) {
110 HILOGE("invalid param.");
111 return BT_ERR_INVALID_PARAM;
112 }
113 if (!IS_BT_ENABLED()) {
114 HILOGE("bluetooth is off.");
115 return BT_ERR_INVALID_STATE;
116 }
117 return proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
118 }
119
RegisterObserverOHOS::Bluetooth::Pan::impl120 void RegisterObserver(std::shared_ptr<PanObserver> observer)
121 {
122 HILOGI("enter");
123 observers_.Register(observer);
124 }
125
DeregisterObserverOHOS::Bluetooth::Pan::impl126 void DeregisterObserver(std::shared_ptr<PanObserver> observer)
127 {
128 HILOGI("enter");
129 observers_.Deregister(observer);
130 }
131
SetTetheringOHOS::Bluetooth::Pan::impl132 int32_t SetTethering(bool value)
133 {
134 HILOGI("enter");
135 if (!proxy_) {
136 HILOGE("proxy_ is nullptr.");
137 return BT_ERR_SERVICE_DISCONNECTED;
138 }
139 if (!IS_BT_ENABLED()) {
140 HILOGE("bluetooth is off.");
141 return BT_ERR_INVALID_STATE;
142 }
143 int32_t ret = proxy_->SetTethering(value);
144 HILOGI("fwk ret:%{public}d", ret);
145 return ret;
146 }
147
IsTetheringOnOHOS::Bluetooth::Pan::impl148 int32_t IsTetheringOn(bool &value)
149 {
150 HILOGI("enter");
151 if (!proxy_) {
152 HILOGE("proxy_ is nullptr.");
153 return BT_ERR_SERVICE_DISCONNECTED;
154 }
155 if (!IS_BT_ENABLED()) {
156 HILOGE("bluetooth is off.");
157 return BT_ERR_INVALID_STATE;
158 }
159 return proxy_->IsTetheringOn(value);
160 }
161
162 private:
163 BluetoothObserverList<PanObserver> observers_;
164 sptr<PanInnerObserver> innerObserver_;
165 sptr<IBluetoothPan> proxy_;
166 class PanDeathRecipient;
167 sptr<PanDeathRecipient> deathRecipient_;
168 };
169
170 class Pan::impl::PanDeathRecipient final : public IRemoteObject::DeathRecipient {
171 public:
PanDeathRecipient(Pan::impl & impl)172 PanDeathRecipient(Pan::impl &impl) : impl_(impl)
173 {};
174 ~PanDeathRecipient() final = default;
175 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PanDeathRecipient);
176
OnRemoteDied(const wptr<IRemoteObject> & remote)177 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
178 {
179 HILOGI("starts");
180 impl_.proxy_->AsObject()->RemoveDeathRecipient(impl_.deathRecipient_);
181 impl_.proxy_ = nullptr;
182 }
183
184 private:
185 Pan::impl &impl_;
186 };
187
impl()188 Pan::impl::impl()
189 {
190 HILOGI("enter");
191 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
192 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
193
194 if (!hostRemote) {
195 HILOGE("failed: no hostRemote");
196 return;
197 }
198 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
199 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_PAN_SERVER);
200
201 if (!remote) {
202 HILOGE("failed: no remote");
203 return;
204 }
205 HILOGI("remote obtained");
206
207 innerObserver_ = new PanInnerObserver(observers_);
208
209 proxy_ = iface_cast<IBluetoothPan>(remote);
210 proxy_->RegisterObserver(innerObserver_);
211 deathRecipient_ = new PanDeathRecipient(*this);
212 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
213 }
214
~impl()215 Pan::impl::~impl()
216 {
217 HILOGI("enter");
218 if (proxy_ != nullptr) {
219 proxy_->DeregisterObserver(innerObserver_);
220 }
221 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
222 }
223
Pan()224 Pan::Pan()
225 {
226 pimpl = std::make_unique<impl>();
227 }
228
~Pan()229 Pan::~Pan()
230 {}
231
GetProfile()232 Pan *Pan::GetProfile()
233 {
234 static Pan instance;
235 return &instance;
236 }
237
GetDevicesByStates(std::vector<int> states,std::vector<BluetoothRemoteDevice> & result)238 int32_t Pan::GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result)
239 {
240 return pimpl->GetDevicesByStates(states, result);
241 }
242
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)243 int32_t Pan::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
244 {
245 return pimpl->GetDeviceState(device, state);
246 }
247
Disconnect(const BluetoothRemoteDevice & device)248 int32_t Pan::Disconnect(const BluetoothRemoteDevice &device)
249 {
250 return pimpl->Disconnect(device);
251 }
252
RegisterObserver(PanObserver * observer)253 void Pan::RegisterObserver(PanObserver *observer)
254 {
255 std::shared_ptr<PanObserver> observerPtr(observer, [](PanObserver *) {});
256 return pimpl->RegisterObserver(observerPtr);
257 }
258
DeregisterObserver(PanObserver * observer)259 void Pan::DeregisterObserver(PanObserver *observer)
260 {
261 std::shared_ptr<PanObserver> observerPtr(observer, [](PanObserver *) {});
262 return pimpl->DeregisterObserver(observerPtr);
263 }
264
SetTethering(bool value)265 int32_t Pan::SetTethering(bool value)
266 {
267 return pimpl->SetTethering(value);
268 }
269
IsTetheringOn(bool & value)270 int32_t Pan::IsTetheringOn(bool &value)
271 {
272 return pimpl->IsTetheringOn(value);
273 }
274 } // namespace Bluetooth
275 } // namespace OHOS