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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_pan"
17 #endif
18
19 #include "bluetooth_pan.h"
20 #include "bluetooth_host.h"
21 #include "bluetooth_profile_manager.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_observer_list.h"
24 #include "bluetooth_pan_observer_stub.h"
25 #include "i_bluetooth_pan.h"
26 #include "i_bluetooth_host.h"
27 #include "bluetooth_utils.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33 std::mutex g_proxyMutex;
34 class PanInnerObserver : public BluetoothPanObserverStub {
35 public:
PanInnerObserver(BluetoothObserverList<PanObserver> & observers)36 explicit PanInnerObserver(BluetoothObserverList<PanObserver> &observers) : observers_(observers)
37 {
38 HILOGD("enter");
39 }
~PanInnerObserver()40 ~PanInnerObserver() override
41 {
42 HILOGD("enter");
43 }
44
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state,int32_t cause)45 ErrCode OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state, int32_t cause) override
46 {
47 HILOGI("enter, device: %{public}s, state: %{public}d, cause: %{public}d",
48 GET_ENCRYPT_RAW_ADDR(device), state, cause);
49 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 1);
50 observers_.ForEach([remoteDevice, state, cause](std::shared_ptr<PanObserver> observer) {
51 observer->OnConnectionStateChanged(remoteDevice, state, cause);
52 });
53 return NO_ERROR;
54 }
55
56 private:
57 BluetoothObserverList<PanObserver> &observers_;
58 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PanInnerObserver);
59 };
60
61 struct Pan::impl {
62 impl();
63 ~impl();
64
GetDevicesByStatesOHOS::Bluetooth::Pan::impl65 int32_t GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice>& result)
66 {
67 HILOGI("enter");
68 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
69 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
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 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
95 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
96 HILOGE("invalid param.");
97 return BT_ERR_INVALID_PARAM;
98 }
99
100 return proxy->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()), state);
101 }
102
DisconnectOHOS::Bluetooth::Pan::impl103 int32_t Disconnect(const BluetoothRemoteDevice &device)
104 {
105 HILOGI("device: %{public}s", GET_ENCRYPT_ADDR(device));
106 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
107 if (proxy == nullptr || !device.IsValidBluetoothRemoteDevice()) {
108 HILOGE("invalid param.");
109 return BT_ERR_INVALID_PARAM;
110 }
111
112 return proxy->Disconnect(BluetoothRawAddress(device.GetDeviceAddr()));
113 }
114
RegisterObserverOHOS::Bluetooth::Pan::impl115 void RegisterObserver(std::shared_ptr<PanObserver> observer)
116 {
117 HILOGD("enter");
118 observers_.Register(observer);
119 }
120
DeregisterObserverOHOS::Bluetooth::Pan::impl121 void DeregisterObserver(std::shared_ptr<PanObserver> observer)
122 {
123 HILOGI("enter");
124 observers_.Deregister(observer);
125 }
126
SetTetheringOHOS::Bluetooth::Pan::impl127 int32_t SetTethering(bool value)
128 {
129 HILOGI("enter");
130 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
131 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
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 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
142 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_SERVICE_DISCONNECTED, "failed: no proxy");
143
144 return proxy->IsTetheringOn(value);
145 }
146 int32_t profileRegisterId = 0;
147 private:
148
149 BluetoothObserverList<PanObserver> observers_;
150 sptr<PanInnerObserver> innerObserver_;
151 };
152
impl()153 Pan::impl::impl()
154 {
155 innerObserver_ = new PanInnerObserver(observers_);
156 profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_PAN_SERVER,
157 [this](sptr<IRemoteObject> remote) {
158 sptr<IBluetoothPan> proxy = iface_cast<IBluetoothPan>(remote);
159 if (proxy == nullptr) {
160 HILOGD("failed: no proxy");
161 return;
162 }
163 proxy->RegisterObserver(innerObserver_);
164 });
165 }
166
~impl()167 Pan::impl::~impl()
168 {
169 HILOGD("start");
170 BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
171 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_PAN_SERVER);
172 CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
173 proxy->DeregisterObserver(innerObserver_);
174 }
175
Pan()176 Pan::Pan()
177 {
178 pimpl = std::make_unique<impl>();
179 }
180
~Pan()181 Pan::~Pan()
182 {}
183
GetProfile()184 Pan *Pan::GetProfile()
185 {
186 #ifdef DTFUZZ_TEST
187 static BluetoothNoDestructor<Pan> instance;
188 return instance.get();
189 #else
190 static Pan instance;
191 return &instance;
192 #endif
193 }
194
GetDevicesByStates(std::vector<int> states,std::vector<BluetoothRemoteDevice> & result)195 int32_t Pan::GetDevicesByStates(std::vector<int> states, std::vector<BluetoothRemoteDevice> &result)
196 {
197 if (!IS_BT_ENABLED()) {
198 HILOGE("bluetooth is off.");
199 return BT_ERR_INVALID_STATE;
200 }
201 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
202 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
203
204 return pimpl->GetDevicesByStates(states, result);
205 }
206
GetDeviceState(const BluetoothRemoteDevice & device,int32_t & state)207 int32_t Pan::GetDeviceState(const BluetoothRemoteDevice &device, int32_t &state)
208 {
209 if (!IS_BT_ENABLED()) {
210 HILOGE("bluetooth is off.");
211 return BT_ERR_INVALID_STATE;
212 }
213 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
214 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
215
216 return pimpl->GetDeviceState(device, state);
217 }
218
Disconnect(const BluetoothRemoteDevice & device)219 int32_t Pan::Disconnect(const BluetoothRemoteDevice &device)
220 {
221 if (!IS_BT_ENABLED()) {
222 HILOGE("bluetooth is off.");
223 return BT_ERR_INVALID_STATE;
224 }
225
226 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
227 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
228
229 return pimpl->Disconnect(device);
230 }
231
RegisterObserver(std::shared_ptr<PanObserver> observer)232 void Pan::RegisterObserver(std::shared_ptr<PanObserver> observer)
233 {
234 HILOGD("enter");
235 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
236 pimpl->RegisterObserver(observer);
237 }
238
DeregisterObserver(std::shared_ptr<PanObserver> observer)239 void Pan::DeregisterObserver(std::shared_ptr<PanObserver> observer)
240 {
241 HILOGD("enter");
242 CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
243 pimpl->DeregisterObserver(observer);
244 }
245
SetTethering(bool value)246 int32_t Pan::SetTethering(bool value)
247 {
248 if (!IS_BT_ENABLED()) {
249 HILOGE("bluetooth is off.");
250 return BT_ERR_INVALID_STATE;
251 }
252
253 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
254 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
255
256 return pimpl->SetTethering(value);
257 }
258
IsTetheringOn(bool & value)259 int32_t Pan::IsTetheringOn(bool &value)
260 {
261 if (!IS_BT_ENABLED()) {
262 HILOGE("bluetooth is off.");
263 return BT_ERR_INVALID_STATE;
264 }
265
266 sptr<IBluetoothPan> proxy = GetRemoteProxy<IBluetoothPan>(PROFILE_OPP_SERVER);
267 CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "failed: no proxy");
268
269 return pimpl->IsTetheringOn(value);
270 }
271 } // namespace Bluetooth
272 } // namespace OHOS