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 "ohos_bt_gap.h"
17
18 #include <string.h>
19 #include "__config"
20 #include "bluetooth_def.h"
21 #include "bluetooth_host.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_remote_device.h"
24 #include "bluetooth_utils.h"
25 #include "iosfwd"
26 #include "ohos_bt_adapter_utils.h"
27 #include "ohos_bt_def.h"
28 #include "string"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 using namespace std;
35
36 namespace OHOS {
37 namespace Bluetooth {
38 static BluetoothHost *g_BluetoothHost;
39 static BtGapCallBacks *g_GapCallback;
40
41 class BluetoothHostObserverWapper : public BluetoothHostObserver {
42 public:
43 /**
44 * @brief Adapter state change function.
45 *
46 * @param transport Transport type when state change.
47 * BTTransport::ADAPTER_BREDR : classic;
48 * BTTransport::ADAPTER_BLE : ble.
49 * @param state Change to the new state.
50 * BTStateID::STATE_TURNING_ON;
51 * BTStateID::STATE_TURN_ON;
52 * BTStateID::STATE_TURNING_OFF;
53 * BTStateID::STATE_TURN_OFF.
54 * @since 6
55 */
OnStateChanged(const int transport,const int status)56 void OnStateChanged(const int transport, const int status) override
57 {
58 int cvtTransport = OHOS_BT_TRANSPORT_LE;
59 if (transport == BTTransport::ADAPTER_BREDR) {
60 cvtTransport = OHOS_BT_TRANSPORT_BR_EDR;
61 }
62 HILOGI("transport: %{public}d, status: %{public}d", cvtTransport, status);
63 if (g_GapCallback != nullptr && g_GapCallback->stateChangeCallback != nullptr) {
64 g_GapCallback->stateChangeCallback(cvtTransport, status);
65 } else {
66 HILOGW("callback func is null!");
67 }
68 }
69
70 /**
71 * @brief Discovery state changed observer.
72 *
73 * @param status Device discovery status.
74 * @since 6
75 */
OnDiscoveryStateChanged(int status)76 void OnDiscoveryStateChanged(int status) override
77 {
78 return;
79 }
80
81 /**
82 * @brief Discovery result observer.
83 *
84 * @param device Remote device.
85 * @since 6
86 */
OnDiscoveryResult(const BluetoothRemoteDevice & device)87 void OnDiscoveryResult(const BluetoothRemoteDevice &device) override
88 {
89 return;
90 }
91
92 /**
93 * @brief Pair request observer.
94 *
95 * @param device Remote device.
96 * @since 6
97 */
OnPairRequested(const BluetoothRemoteDevice & device)98 void OnPairRequested(const BluetoothRemoteDevice &device) override
99 {
100 return;
101 }
102
103 /**
104 * @brief Pair confirmed observer.
105 *
106 * @param device Remote device.
107 * @param reqType Pair type.
108 * @param number Paired passkey.
109 * @since 6
110 */
OnPairConfirmed(const BluetoothRemoteDevice & device,int reqType,int number)111 void OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number) override
112 {
113 return;
114 }
115
116 /**
117 * @brief Scan mode changed observer.
118 *
119 * @param mode Device scan mode.
120 * @since 6
121 */
OnScanModeChanged(int mode)122 void OnScanModeChanged(int mode) override
123 {
124 HILOGI("mode: %{public}d", mode);
125 if (g_GapCallback != nullptr && g_GapCallback->scanModeChangedCallback != nullptr) {
126 g_GapCallback->scanModeChangedCallback(mode);
127 } else {
128 HILOGW("mode: %{public}d, but callback is null!", mode);
129 }
130 };
131
132 /**
133 * @brief Device name changed observer.
134 *
135 * @param deviceName Device name.
136 * @since 6
137 */
OnDeviceNameChanged(const std::string & deviceName)138 void OnDeviceNameChanged(const std::string &deviceName) override
139 {
140 return;
141 }
142
143 /**
144 * @brief Device address changed observer.
145 *
146 * @param address Device address.
147 * @since 6
148 */
OnDeviceAddrChanged(const std::string & address)149 void OnDeviceAddrChanged(const std::string &address) override
150 {
151 return;
152 }
153 };
154
155 class BluetoothRemoteDeviceObserverWapper : public BluetoothRemoteDeviceObserver {
156 public:
157 /**
158 * @brief Acl state changed observer.
159 *
160 * @param device Remote device.
161 * @param state Remote device acl state.
162 * @param reason Remote device reason.
163 * @since 6
164 */
OnAclStateChanged(const BluetoothRemoteDevice & device,int state,unsigned int reason)165 void OnAclStateChanged(const BluetoothRemoteDevice &device, int state, unsigned int reason) override
166 {
167 if (g_GapCallback == nullptr || g_GapCallback->aclStateChangedCallbak == nullptr) {
168 HILOGW("callback func is null!");
169 return;
170 }
171 std::string stateStr;
172 GetAclStateName(device.GetTransportType(), state, stateStr);
173 HILOGD("device: %{public}s, state: %{public}s, reason: %{public}u",
174 GetEncryptAddr(device.GetDeviceAddr()).c_str(), stateStr.c_str(), reason);
175 BdAddr remoteAddr;
176 GetAddrFromString(device.GetDeviceAddr(), remoteAddr.addr);
177 g_GapCallback->aclStateChangedCallbak(&remoteAddr, ConvertAclState(device.GetTransportType(), state), reason);
178 }
179
180 /**
181 * @brief Pair status changed observer.
182 *
183 * @param device Remote device.
184 * @param status Remote device pair status.
185 * @since 6
186 */
OnPairStatusChanged(const BluetoothRemoteDevice & device,int status)187 void OnPairStatusChanged(const BluetoothRemoteDevice &device, int status) override
188 {
189 return;
190 }
191
192 /**
193 * @brief Remote uuid changed observer.
194 *
195 * @param device Remote device.
196 * @param uuids Remote device uuids.
197 * @since 6
198 */
OnRemoteUuidChanged(const BluetoothRemoteDevice & device,const std::vector<ParcelUuid> & uuids)199 void OnRemoteUuidChanged(const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids) override
200 {
201 return;
202 }
203
204 /**
205 * @brief Remote name changed observer.
206 *
207 * @param device Remote device.
208 * @param deviceName Remote device name.
209 * @since 6
210 */
OnRemoteNameChanged(const BluetoothRemoteDevice & device,const std::string & deviceName)211 void OnRemoteNameChanged(const BluetoothRemoteDevice &device, const std::string &deviceName) override
212 {
213 return;
214 }
215
216 /**
217 * @brief Remote alias changed observer.
218 *
219 * @param device Remote device.
220 * @param alias Remote device alias.
221 * @since 6
222 */
OnRemoteAliasChanged(const BluetoothRemoteDevice & device,const std::string & alias)223 void OnRemoteAliasChanged(const BluetoothRemoteDevice &device, const std::string &alias) override
224 {
225 return;
226 }
227
228 /**
229 * @brief Remote cod changed observer.
230 *
231 * @param device Remote device.
232 * @param cod Remote device cod.
233 * @since 6
234 */
OnRemoteCodChanged(const BluetoothRemoteDevice & device,const BluetoothDeviceClass & cod)235 void OnRemoteCodChanged(const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod) override
236 {
237 return;
238 }
239
240 /**
241 * @brief Remote battery level changed observer.
242 *
243 * @param device Remote device.
244 * @param cod Remote device battery Level.
245 * @since 6
246 */
OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice & device,int batteryLevel)247 void OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice &device, int batteryLevel) override
248 {
249 return;
250 }
251
252 /**
253 * @brief Remote rssi event observer.
254 *
255 * @param device Remote device.
256 * @param rssi Remote device rssi.
257 * @param status Read status.
258 * @since 6
259 */
OnReadRemoteRssiEvent(const BluetoothRemoteDevice & device,int rssi,int status)260 void OnReadRemoteRssiEvent(const BluetoothRemoteDevice &device, int rssi, int status) override
261 {
262 return;
263 }
264 };
265
266 static BluetoothHostObserverWapper g_hostObserver;
267 static std::shared_ptr<BluetoothRemoteDeviceObserverWapper> g_remoteDeviceObserver;
268
EnableBle(void)269 bool EnableBle(void)
270 {
271 HILOGI("enter");
272 if (g_BluetoothHost == nullptr) {
273 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
274 }
275
276 if (g_BluetoothHost->IsBleEnabled()) {
277 HILOGI("ble enabled already");
278 return true;
279 }
280 bool isEnabled = false;
281 int32_t ret = g_BluetoothHost->EnableBle();
282 HILOGI("result: %{public}d", ret);
283 if (ret == BT_NO_ERROR) {
284 isEnabled = true;
285 }
286 return isEnabled;
287 }
288
DisableBle(void)289 bool DisableBle(void)
290 {
291 HILOGI("enter");
292 if (g_BluetoothHost == nullptr) {
293 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
294 }
295
296 if (!g_BluetoothHost->IsBleEnabled()) {
297 HILOGI("ble disabled already");
298 return true;
299 }
300 bool isEnabled = false;
301 int ret = g_BluetoothHost->DisableBle();
302 HILOGI("result: %{public}d", ret);
303 if (ret == BT_NO_ERROR) {
304 isEnabled = true;
305 }
306 return isEnabled;
307 }
308
EnableBt(void)309 bool EnableBt(void)
310 {
311 HILOGI("enter");
312 if (g_BluetoothHost == nullptr) {
313 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
314 }
315
316 int state;
317 g_BluetoothHost->GetBtState(state);
318 if (state == BTStateID::STATE_TURNING_ON ||
319 state == BTStateID::STATE_TURN_ON) {
320 HILOGI("br state is %{public}d", state);
321 return true;
322 }
323 bool isEnabled = false;
324 int ret = g_BluetoothHost->EnableBt();
325 HILOGI("result: %{public}d", ret);
326 if (ret == BT_NO_ERROR) {
327 isEnabled = true;
328 }
329 return isEnabled;
330 }
331
DisableBt(void)332 bool DisableBt(void)
333 {
334 HILOGI("enter");
335 if (g_BluetoothHost == nullptr) {
336 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
337 }
338
339 int state;
340 g_BluetoothHost->GetBtState(state);
341 if (state == BTStateID::STATE_TURNING_OFF ||
342 state == BTStateID::STATE_TURN_OFF) {
343 HILOGI("br state is %{public}d", state);
344 return true;
345 }
346 bool isDisabled = false;
347 int ret = g_BluetoothHost->DisableBt();
348 HILOGI("result: %{public}d", ret);
349 if (ret == BT_NO_ERROR) {
350 isDisabled = true;
351 }
352 return isDisabled;
353 }
354
GetBtState()355 int GetBtState()
356 {
357 HILOGI("enter");
358 if (g_BluetoothHost == nullptr) {
359 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
360 }
361
362 int state;
363 g_BluetoothHost->GetBtState(state);
364 HILOGI("br state: %{public}d", state);
365 return state;
366 }
367
IsBleEnabled()368 bool IsBleEnabled()
369 {
370 if (g_BluetoothHost == nullptr) {
371 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
372 }
373
374 bool ret = g_BluetoothHost->IsBleEnabled();
375 HILOGI("ble enable: %{public}d", ret);
376 return ret;
377 }
378
GetLocalAddr(unsigned char * mac,unsigned int len)379 bool GetLocalAddr(unsigned char *mac, unsigned int len)
380 {
381 HILOGI("enter");
382 if (mac == nullptr || len < OHOS_BD_ADDR_LEN) {
383 HILOGE("invalid param, len:%{public}d", len);
384 return false;
385 }
386 if (g_BluetoothHost == nullptr) {
387 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
388 }
389
390 string localAddress = g_BluetoothHost->GetLocalAddress();
391 GetAddrFromString(localAddress, mac);
392 HILOGI("device: %{public}s", GetEncryptAddr(localAddress).c_str());
393 return true;
394 }
395
SetLocalName(unsigned char * localName,unsigned char length)396 bool SetLocalName(unsigned char *localName, unsigned char length)
397 {
398 HILOGI("enter");
399 if (localName == nullptr) {
400 HILOGE("localName is null");
401 return false;
402 }
403
404 if (g_BluetoothHost == nullptr) {
405 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
406 }
407
408 string newName(reinterpret_cast<const char *>(localName));
409 bool isSuccess = false;
410 int ret = g_BluetoothHost->SetLocalName(newName);
411 if (ret == BT_NO_ERROR) {
412 isSuccess = true;
413 }
414 HILOGI("result %{public}d: LocalName : %{public}s", ret, g_BluetoothHost->GetLocalName().c_str());
415 return isSuccess;
416 }
417
SetBtScanMode(int mode,int duration)418 bool SetBtScanMode(int mode, int duration)
419 {
420 HILOGI("mode: %{public}d, duration: %{public}d", mode, duration);
421 if (g_BluetoothHost == nullptr) {
422 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
423 }
424 bool isSuccess = false;
425 int ret = g_BluetoothHost->SetBtScanMode(mode, duration);
426 if (ret == BT_NO_ERROR) {
427 isSuccess = true;
428 }
429 g_BluetoothHost->SetBondableMode(BT_TRANSPORT_BREDR, BONDABLE_MODE_ON);
430 return isSuccess;
431 }
432
PairRequestReply(const BdAddr * bdAddr,int transport,bool accept)433 bool PairRequestReply(const BdAddr *bdAddr, int transport, bool accept)
434 {
435 string strAddress;
436 ConvertAddr(bdAddr->addr, strAddress);
437 HILOGI("device: %{public}s", GetEncryptAddr(strAddress).c_str());
438 BluetoothRemoteDevice remoteDevice;
439 if (transport == OHOS_BT_TRANSPORT_BR_EDR) {
440 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BREDR);
441 } else if (transport == OHOS_BT_TRANSPORT_LE) {
442 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BLE);
443 } else {
444 HILOGE("transport: %{public}d is invalid", transport);
445 return false;
446 }
447 bool ret = remoteDevice.PairRequestReply(accept);
448 HILOGI("transport: %{public}d, accept: %{public}d, ret: %{public}d", transport, accept, ret);
449 return ret;
450 }
451
SetDevicePairingConfirmation(const BdAddr * bdAddr,int transport,bool accept)452 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int transport, bool accept)
453 {
454 string strAddress;
455 ConvertAddr(bdAddr->addr, strAddress);
456 HILOGI("device: %{public}s, accept: %{public}d", GetEncryptAddr(strAddress).c_str(), accept);
457 BluetoothRemoteDevice remoteDevice;
458 if (transport == OHOS_BT_TRANSPORT_BR_EDR) {
459 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BREDR);
460 } else if (transport == OHOS_BT_TRANSPORT_LE) {
461 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BLE);
462 } else {
463 HILOGE("transport: %{public}d is invalid", transport);
464 return false;
465 }
466 bool isSuccess = false;
467 int ret = remoteDevice.SetDevicePairingConfirmation(accept);
468 HILOGI("ret: %{public}d", ret);
469 if (ret == BT_NO_ERROR) {
470 isSuccess = true;
471 }
472 return isSuccess;
473 }
474
GapRegisterCallbacks(BtGapCallBacks * func)475 int GapRegisterCallbacks(BtGapCallBacks *func)
476 {
477 HILOGI("enter");
478 if (func == nullptr) {
479 HILOGE("func is null.");
480 return OHOS_BT_STATUS_PARM_INVALID;
481 }
482 if (g_BluetoothHost == nullptr) {
483 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
484 }
485 g_GapCallback = func;
486 g_remoteDeviceObserver = std::make_shared<BluetoothRemoteDeviceObserverWapper>();
487 g_BluetoothHost->RegisterObserver(g_hostObserver);
488 g_BluetoothHost->RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
489 return OHOS_BT_STATUS_SUCCESS;
490 }
491
SetFastScan(bool isEnable)492 bool SetFastScan(bool isEnable)
493 {
494 HILOGI("isEnable: %{public}d", isEnable);
495 if (g_BluetoothHost == nullptr) {
496 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
497 }
498 bool isSuccess = false;
499 int ret = g_BluetoothHost->SetFastScan(isEnable);
500 if (ret == BT_NO_ERROR) {
501 isSuccess = true;
502 }
503
504 return isSuccess;
505 }
506 } // namespace Bluetooth
507 } // namespace OHOS
508 #ifdef __cplusplus
509 }
510 #endif
511