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 std::shared_ptr<BluetoothHostObserverWapper> g_hostObserver = nullptr;
267 static std::shared_ptr<BluetoothRemoteDeviceObserverWapper> g_remoteDeviceObserver = nullptr;
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 HILOGD("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 std::string localAddress = INVALID_MAC_ADDRESS;
391 int32_t err = g_BluetoothHost->GetLocalAddress(localAddress);
392 if (err != BT_NO_ERROR) {
393 HILOGE("error %{public}d", err);
394 return false;
395 }
396 GetAddrFromString(localAddress, mac);
397 HILOGI("device: %{public}s", GetEncryptAddr(localAddress).c_str());
398 return true;
399 }
400
SetLocalName(unsigned char * localName,unsigned char length)401 bool SetLocalName(unsigned char *localName, unsigned char length)
402 {
403 HILOGI("enter");
404 if (localName == nullptr) {
405 HILOGE("localName is null");
406 return false;
407 }
408
409 if (g_BluetoothHost == nullptr) {
410 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
411 }
412
413 string newName(reinterpret_cast<const char *>(localName));
414 bool isSuccess = false;
415 int ret = g_BluetoothHost->SetLocalName(newName);
416 if (ret == BT_NO_ERROR) {
417 isSuccess = true;
418 }
419 HILOGI("result %{public}d: LocalName : %{public}s", ret, g_BluetoothHost->GetLocalName().c_str());
420 return isSuccess;
421 }
422
SetBtScanMode(int mode,int duration)423 bool SetBtScanMode(int mode, int duration)
424 {
425 HILOGI("mode: %{public}d, duration: %{public}d", mode, duration);
426 if (g_BluetoothHost == nullptr) {
427 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
428 }
429 bool isSuccess = false;
430 int ret = g_BluetoothHost->SetBtScanMode(mode, duration);
431 if (ret == BT_NO_ERROR) {
432 isSuccess = true;
433 }
434 g_BluetoothHost->SetBondableMode(BT_TRANSPORT_BREDR, BONDABLE_MODE_ON);
435 return isSuccess;
436 }
437
PairRequestReply(const BdAddr * bdAddr,int transport,bool accept)438 bool PairRequestReply(const BdAddr *bdAddr, int transport, bool accept)
439 {
440 string strAddress;
441 ConvertAddr(bdAddr->addr, strAddress);
442 HILOGI("device: %{public}s", GetEncryptAddr(strAddress).c_str());
443 BluetoothRemoteDevice remoteDevice;
444 if (transport == OHOS_BT_TRANSPORT_BR_EDR) {
445 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BREDR);
446 } else if (transport == OHOS_BT_TRANSPORT_LE) {
447 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BLE);
448 } else {
449 HILOGE("transport: %{public}d is invalid", transport);
450 return false;
451 }
452 bool ret = remoteDevice.PairRequestReply(accept);
453 HILOGI("transport: %{public}d, accept: %{public}d, ret: %{public}d", transport, accept, ret);
454 return ret;
455 }
456
SetDevicePairingConfirmation(const BdAddr * bdAddr,int transport,bool accept)457 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int transport, bool accept)
458 {
459 string strAddress;
460 ConvertAddr(bdAddr->addr, strAddress);
461 HILOGI("device: %{public}s, accept: %{public}d", GetEncryptAddr(strAddress).c_str(), accept);
462 BluetoothRemoteDevice remoteDevice;
463 if (transport == OHOS_BT_TRANSPORT_BR_EDR) {
464 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BREDR);
465 } else if (transport == OHOS_BT_TRANSPORT_LE) {
466 remoteDevice = g_BluetoothHost->GetRemoteDevice(strAddress, BT_TRANSPORT_BLE);
467 } else {
468 HILOGE("transport: %{public}d is invalid", transport);
469 return false;
470 }
471 bool isSuccess = false;
472 int ret = remoteDevice.SetDevicePairingConfirmation(accept);
473 HILOGI("ret: %{public}d", ret);
474 if (ret == BT_NO_ERROR) {
475 isSuccess = true;
476 }
477 return isSuccess;
478 }
479
GapRegisterCallbacks(BtGapCallBacks * func)480 int GapRegisterCallbacks(BtGapCallBacks *func)
481 {
482 HILOGI("enter");
483 if (func == nullptr) {
484 HILOGE("func is null.");
485 return OHOS_BT_STATUS_PARM_INVALID;
486 }
487 if (g_BluetoothHost == nullptr) {
488 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
489 }
490 g_GapCallback = func;
491 g_hostObserver = std::make_shared<BluetoothHostObserverWapper>();
492 g_remoteDeviceObserver = std::make_shared<BluetoothRemoteDeviceObserverWapper>();
493 g_BluetoothHost->RegisterObserver(g_hostObserver);
494 g_BluetoothHost->RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
495 return OHOS_BT_STATUS_SUCCESS;
496 }
497
SetFastScan(bool isEnable)498 bool SetFastScan(bool isEnable)
499 {
500 HILOGI("isEnable: %{public}d", isEnable);
501 if (g_BluetoothHost == nullptr) {
502 g_BluetoothHost = &BluetoothHost::GetDefaultHost();
503 }
504 bool isSuccess = false;
505 int ret = g_BluetoothHost->SetFastScan(isEnable);
506 if (ret == BT_NO_ERROR) {
507 isSuccess = true;
508 }
509
510 return isSuccess;
511 }
512 } // namespace Bluetooth
513 } // namespace OHOS
514 #ifdef __cplusplus
515 }
516 #endif
517