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_gatt.h"
17
18 #include <unistd.h>
19 #include <cstdint>
20 #include <sstream>
21 #include <vector>
22 #include "__config"
23 #include "bluetooth_ble_advertiser.h"
24 #include "bluetooth_ble_central_manager.h"
25 #include "bluetooth_log.h"
26 #include "bluetooth_remote_device.h"
27 #include "bluetooth_utils.h"
28 #include "cstdint"
29 #include "iosfwd"
30 #include "istream"
31 #include "new"
32 #include "ohos_bt_adapter_utils.h"
33 #include "ohos_bt_def.h"
34 #include "ostream"
35 #include "securec.h"
36 #include "streambuf"
37 #include "string"
38 #include "uuid.h"
39 #include "bluetooth_object_map.h"
40
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 using namespace std;
47
48 namespace OHOS {
49 namespace Bluetooth {
50 #define MAX_BLE_ADV_NUM 7
51
52 class BleCentralManagerCallbackWapper;
53 class BleAdvCallback;
54
55 static BtGattCallbacks *g_AppCallback;
56
57 static std::shared_ptr<BleAdvCallback> g_bleAdvCallbacks[MAX_BLE_ADV_NUM];
58 static std::shared_ptr<BleAdvertiser> g_BleAdvertiser = nullptr;
59
60 constexpr int32_t MAX_BLE_SCAN_NUM = 5;
61 static BluetoothObjectMap<std::shared_ptr<BleCentralManager>, (MAX_BLE_SCAN_NUM + 1)> g_bleCentralManagerMap;
62
63 class BleCentralManagerCallbackWapper : public BleCentralManagerCallback {
64 public:
65 /**
66 * @brief Scan result callback.
67 *
68 * @param result Scan result.
69 * @since 6
70 */
OnScanCallback(const BleScanResult & result)71 void OnScanCallback(const BleScanResult &result) override
72 {
73 BtScanResultData scanResult;
74 scanResult.eventType = OHOS_BLE_EVT_LEGACY_CONNECTABLE; // result.IsConnectable();
75 scanResult.dataStatus = OHOS_BLE_DATA_COMPLETE;
76 scanResult.addrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS;
77 scanResult.primaryPhy = OHOS_BLE_SCAN_PHY_1M;
78 scanResult.secondaryPhy = OHOS_BLE_SCAN_PHY_1M;
79 scanResult.advSid = 1;
80 scanResult.txPower = 1;
81 scanResult.rssi = result.GetRssi();
82 scanResult.directAddrType = OHOS_BLE_RANDOM_DEVICE_ADDRESS;
83 GetAddrFromString(result.GetPeripheralDevice().GetDeviceAddr(), scanResult.addr.addr);
84 vector<uint8_t> vec = result.GetPayload();
85 scanResult.advData = vec.data();
86 scanResult.advLen = vec.size();
87
88 std::string strs;
89 std::stringstream strStream;
90 for (int i = 0; i < scanResult.advLen; i++) {
91 char token[3] = {0};
92 (void)sprintf_s(token, 3, "%02X", scanResult.advData[i]);
93 strStream << token;
94 }
95 strStream >> strs;
96 string address = result.GetPeripheralDevice().GetDeviceAddr();
97 HILOGD("device: %{public}s, scan data: %{public}s", GetEncryptAddr(address).c_str(), strs.c_str());
98 if (appCallback != nullptr && appCallback->scanResultCb != nullptr) {
99 appCallback->scanResultCb(&scanResult);
100 } else {
101 HILOGW("call back is null.");
102 }
103 }
104
105 /**
106 * @brief Batch scan results event callback.
107 *
108 * @param results Scan results.
109 * @since 6
110 */
OnBleBatchScanResultsEvent(const std::vector<BleScanResult> & results)111 void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) override {}
112
113 /**
114 * @brief Start or Stop scan event callback.
115 *
116 * @param resultCode Scan result code.
117 * @param isStartScan true->start scan, false->stop scan.
118 * @since 6
119 */
OnStartOrStopScanEvent(int32_t resultCode,bool isStartScan)120 void OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) override
121 {
122 HILOGD("resultCode: %{public}d, isStartScan: %{public}d", resultCode, isStartScan);
123 if (appCallback != nullptr && appCallback->scanStateChangeCb != nullptr) {
124 appCallback->scanStateChangeCb(resultCode, isStartScan);
125 } else {
126 HILOGE("call back is null.");
127 }
128 }
129
130 /**
131 * @brief Notify low power device msg callback.
132 *
133 * @param btUuid uuid.
134 * @param msgType notify msgType.
135 * @param value notify msg value.
136 * @since 6
137 */
OnNotifyMsgReportFromLpDevice(const UUID & btUuid,int msgType,const std::vector<uint8_t> & value)138 void OnNotifyMsgReportFromLpDevice(const UUID &btUuid, int msgType, const std::vector<uint8_t> &value) override
139 {
140 if (appCallback != nullptr && appCallback->lpDeviceInfoCb != nullptr) {
141 BtUuid retUuid;
142 string strUuid = btUuid.ToString();
143 retUuid.uuid = (char *)strUuid.c_str();
144 retUuid.uuidLen = strUuid.size();
145 appCallback->lpDeviceInfoCb(&retUuid, msgType, const_cast<uint8_t*>(value.data()), value.size());
146 }
147 }
148
149 public:
150 BleScanCallbacks *appCallback = nullptr;
151 };
152
153 class BleAdvCallback : public BleAdvertiseCallback {
154 public:
BleAdvCallback(int advId)155 explicit BleAdvCallback(int advId)
156 {
157 advId_ = advId;
158 }
159
OnStartResultEvent(int result)160 void OnStartResultEvent(int result) override
161 {
162 HILOGI("advId: %{public}d, ret: %{public}d", advId_, result);
163 int ret = (result == 0) ? OHOS_BT_STATUS_SUCCESS : OHOS_BT_STATUS_FAIL;
164 if (g_AppCallback != nullptr && g_AppCallback->advEnableCb != nullptr) {
165 g_AppCallback->advEnableCb(advId_, ret);
166 } else {
167 HILOGW("call back is null.");
168 }
169 }
170
OnSetAdvDataEvent(int result)171 void OnSetAdvDataEvent(int result) override
172 {
173 HILOGI("advId: %{public}d, ret: %{public}d", advId_, result);
174 int ret = (result == 0) ? OHOS_BT_STATUS_SUCCESS : OHOS_BT_STATUS_FAIL;
175 if (g_AppCallback != nullptr && g_AppCallback->advDataCb != nullptr) {
176 g_AppCallback->advDataCb(advId_, ret);
177 }
178 }
179
180 protected:
181 BleAdvertiserData *advData = nullptr;
182 BleAdvertiserData *advResponseData = nullptr;
183 BleAdvertiserSettings *advSetting = nullptr;
184
185 private:
186 int advId_;
187 };
188
189 /**
190 * @brief Initializes the Bluetooth protocol stack.
191 *
192 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is initialized;
193 * returns an error code defined in {@link BtStatus} otherwise.
194 * @since 6
195 */
InitBtStack(void)196 int InitBtStack(void)
197 {
198 return OHOS_BT_STATUS_SUCCESS;
199 }
200
201 /**
202 * @brief Enables the Bluetooth protocol stack.
203 *
204 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is enabled;
205 * returns an error code defined in {@link BtStatus} otherwise.
206 * @since 6
207 */
EnableBtStack(void)208 int EnableBtStack(void)
209 {
210 return OHOS_BT_STATUS_SUCCESS;
211 }
212
213 /**
214 * @brief Disables the Bluetooth protocol stack.
215 *
216 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth protocol stack is disabled;
217 * returns an error code defined in {@link BtStatus} otherwise.
218 * @since 6
219 */
DisableBtStack(void)220 int DisableBtStack(void)
221 {
222 return OHOS_BT_STATUS_SUCCESS;
223 }
224
225 /**
226 * @brief Sets the Bluetooth device name.
227 *
228 * @param name Indicates the pointer to the name to set.
229 * @param len Indicates the length of the name to set.
230 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the Bluetooth device name is set;
231 * returns an error code defined in {@link BtStatus} otherwise.
232 * @since 6
233 */
SetDeviceName(const char * name,unsigned int len)234 int SetDeviceName(const char *name, unsigned int len)
235 {
236 return OHOS_BT_STATUS_UNSUPPORTED;
237 }
238
ConvertDataToVec(uint8_t * data,unsigned int len)239 static vector<uint8_t> ConvertDataToVec(uint8_t *data, unsigned int len)
240 {
241 if (data == nullptr || len == 0) {
242 return {};
243 }
244
245 return vector<uint8_t>(data, data + len);
246 }
247
248 /**
249 * @brief Sets advertising data.
250 *
251 * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser.
252 * @param data Indicates the pointer to the advertising data. For details, see {@link StartAdvRawData}.
253 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising data is set;
254 * returns an error code defined in {@link BtStatus} otherwise.
255 * @since 6
256 */
BleSetAdvData(int advId,const StartAdvRawData data)257 int BleSetAdvData(int advId, const StartAdvRawData data)
258 {
259 if (advId < 0 || advId >= MAX_BLE_ADV_NUM) {
260 HILOGE("Invalid advId (%{public}d)", advId);
261 return OHOS_BT_STATUS_PARM_INVALID;
262 }
263 if (g_BleAdvertiser == nullptr || g_bleAdvCallbacks[advId] == nullptr) {
264 HILOGE("Adv is not started, need call 'BleStartAdvEx' first.");
265 return OHOS_BT_STATUS_FAIL;
266 }
267 HILOGI("advId: %{public}d, advLen: %{public}u, scanRspLen: %{public}u", advId, data.advDataLen, data.rspDataLen);
268
269 auto advData = ConvertDataToVec(data.advData, data.advDataLen);
270 auto rspData = ConvertDataToVec(data.rspData, data.rspDataLen);
271 g_BleAdvertiser->SetAdvertisingData(advData, rspData, *g_bleAdvCallbacks[advId]);
272
273 return OHOS_BT_STATUS_SUCCESS;
274 }
275
276 /**
277 * @brief Starts advertising.
278 *
279 * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser.
280 * @param param Indicates the pointer to the advertising parameters. For details, see {@link BleAdvParams}.
281 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising is started;
282 * returns an error code defined in {@link BtStatus} otherwise.
283 * @since 6
284 */
BleStartAdv(int advId,const BleAdvParams * param)285 int BleStartAdv(int advId, const BleAdvParams *param)
286 {
287 return OHOS_BT_STATUS_UNSUPPORTED;
288 }
289
IsAllAdvStopped()290 static bool IsAllAdvStopped()
291 {
292 for (int i = 0; i < MAX_BLE_ADV_NUM; i++) {
293 if (g_bleAdvCallbacks[i] != nullptr) {
294 return false;
295 }
296 }
297 return true;
298 }
299
300 /**
301 * @brief Stops advertising.
302 *
303 * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser.
304 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising is stopped;
305 * returns an error code defined in {@link BtStatus} otherwise.
306 * @since 6
307 */
BleStopAdv(int advId)308 int BleStopAdv(int advId)
309 {
310 HILOGI("BleStopAdv, advId: %{public}d.", advId);
311 if (advId < 0 || advId >= MAX_BLE_ADV_NUM) {
312 HILOGE("BleStopAdv fail, advId is invalid.");
313 return OHOS_BT_STATUS_FAIL;
314 }
315 if (g_BleAdvertiser == nullptr || g_bleAdvCallbacks[advId] == nullptr) {
316 HILOGE("BleStopAdv fail, the current adv is not started.");
317 return OHOS_BT_STATUS_FAIL;
318 }
319
320 g_BleAdvertiser->StopAdvertising(*g_bleAdvCallbacks[advId]);
321
322 usleep(100);
323 if (g_AppCallback != nullptr && g_AppCallback->advDisableCb != nullptr) {
324 HILOGI("adv stopped advId: %{public}d.", advId);
325 g_AppCallback->advDisableCb(advId, 0);
326 }
327 g_bleAdvCallbacks[advId] = nullptr;
328
329 if (IsAllAdvStopped()) {
330 HILOGI("All adv have been stopped.");
331 }
332 return OHOS_BT_STATUS_SUCCESS;
333 }
334
335 /**
336 * @brief Updates advertising parameters.
337 *
338 * @param advId Indicates the advertisement ID, which is allocated by the upper layer of the advertiser.
339 * @param param Indicates the pointer to the advertising parameters. For details, see {@link BleAdvParams}.
340 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if advertising parameters are updated;
341 * returns an error code defined in {@link BtStatus} otherwise.
342 * @since 6
343 */
BleUpdateAdv(int advId,const BleAdvParams * param)344 int BleUpdateAdv(int advId, const BleAdvParams *param)
345 {
346 return OHOS_BT_STATUS_UNSUPPORTED;
347 }
348
349 /**
350 * @brief Sets the secure I/O capability mode.
351 *
352 * @param mode Indicates the capability mode to set. For details, see {@link BleIoCapMode}.
353 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the capability mode is set;
354 * returns an error code defined in {@link BtStatus} otherwise.
355 * @since 6
356 */
BleSetSecurityIoCap(BleIoCapMode mode)357 int BleSetSecurityIoCap(BleIoCapMode mode)
358 {
359 return OHOS_BT_STATUS_UNSUPPORTED;
360 }
361
362 /**
363 * @brief Sets the authentication mode for secure connection requests.
364 *
365 * @param mode Indicates the authentication mode to set. For details, see {@link BleAuthReqMode}.
366 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the authentication mode is set;
367 * returns an error code defined in {@link BtStatus} otherwise.
368 * @since 6
369 */
BleSetSecurityAuthReq(BleAuthReqMode mode)370 int BleSetSecurityAuthReq(BleAuthReqMode mode)
371 {
372 return OHOS_BT_STATUS_UNSUPPORTED;
373 }
374
375 /**
376 * @brief Responds to a secure connection request.
377 *
378 * @param bdAddr Indicates the address of the device that sends the request.
379 * @param accept Specifies whether to accept the request. The value <b>true</b> means to accept the request,
380 * and <b>false</b> means to reject the request.
381 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the request is responded to;
382 * returns an error code defined in {@link BtStatus} otherwise.
383 * @since 6
384 */
BleGattSecurityRsp(BdAddr bdAddr,bool accept)385 int BleGattSecurityRsp(BdAddr bdAddr, bool accept)
386 {
387 return OHOS_BT_STATUS_UNSUPPORTED;
388 }
389
390 /**
391 * @brief Obtains the device MAC address.
392 *
393 * @param mac Indicates the pointer to the device MAC address.
394 * @param len Indicates the length of the device MAC address.
395 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the device MAC address is obtained;
396 * returns an error code defined in {@link BtStatus} otherwise.
397 * @since 6
398 */
ReadBtMacAddr(unsigned char * mac,unsigned int len)399 int ReadBtMacAddr(unsigned char *mac, unsigned int len)
400 {
401 return OHOS_BT_STATUS_UNSUPPORTED;
402 }
403
404 /**
405 * @brief Sets scan parameters.
406 *
407 * @param clientId Indicates the client ID, which is obtained during client registration.
408 * @param param Indicates the pointer to the scan parameters. For details, see {@link BleScanParams}.
409 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan parameters are set;
410 * returns an error code defined in {@link BtStatus} otherwise.
411 * @since 6
412 */
BleSetScanParameters(int clientId,BleScanParams * param)413 int BleSetScanParameters(int clientId, BleScanParams *param)
414 {
415 return OHOS_BT_STATUS_SUCCESS;
416 }
417
418 /**
419 * @brief Starts a scan.
420 *
421 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is started;
422 * returns an error code defined in {@link BtStatus} otherwise.
423 * @since 6
424 */
BleStartScan(void)425 int BleStartScan(void)
426 {
427 HILOGE("This interface is deprecated. BleStartScanEx is recommended.");
428 return OHOS_BT_STATUS_UNSUPPORTED;
429 }
430
431 /**
432 * @brief Stops a scan.
433 *
434 * @param scannerId Indicates the scanner id.
435 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is stopped;
436 * returns an error code defined in {@link BtStatus} otherwise.
437 * @since 6
438 */
BleStopScan(int32_t scannerId)439 int BleStopScan(int32_t scannerId)
440 {
441 HILOGI("BleStopScan enter scannerId: %{public}d", scannerId);
442 std::shared_ptr<BleCentralManager> bleCentralManager = g_bleCentralManagerMap.GetObject(scannerId);
443 if (bleCentralManager == nullptr) {
444 HILOGE("BleStopScan fail, scannerId is invalid.");
445 return OHOS_BT_STATUS_FAIL;
446 }
447
448 bleCentralManager->StopScan();
449 return OHOS_BT_STATUS_SUCCESS;
450 }
451
452 /**
453 * @brief Registers GATT callbacks.
454 *
455 * @param func Indicates the pointer to the callbacks to register. For details, see {@link BtGattCallbacks}.
456 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the GATT callbacks are registered;
457 * returns an error code defined in {@link BtStatus} otherwise.
458 * @since 6
459 */
BleGattRegisterCallbacks(BtGattCallbacks * func)460 int BleGattRegisterCallbacks(BtGattCallbacks *func)
461 {
462 HILOGI("BleGattRegisterCallbacks enter");
463 if (func == nullptr) {
464 HILOGE("func is null.");
465 return OHOS_BT_STATUS_PARM_INVALID;
466 }
467 g_AppCallback = func;
468 return OHOS_BT_STATUS_SUCCESS;
469 }
470
471 /**
472 * @brief Register ble scan callbacks.
473 *
474 * @param func Indicates the pointer to the callbacks to register. For details, see {@link BleScanCallbacks}.
475 * @param scannerId Indicates the pointer to the scannerId, identify one scan.
476 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the BLE callbacks are registered;
477 * returns an error code defined in {@link BtStatus} otherwise.
478 * @since 6
479 */
BleRegisterScanCallbacks(BleScanCallbacks * func,int32_t * scannerId)480 int BleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)
481 {
482 HILOGI("BleRegisterScanCallbacks enter");
483 if (scannerId == nullptr || func == nullptr) {
484 HILOGE("BleRegisterScanCallbacks scannerId or func is null");
485 return OHOS_BT_STATUS_PARM_INVALID;
486 }
487
488 std::shared_ptr<BleCentralManagerCallbackWapper> callback = std::make_shared<BleCentralManagerCallbackWapper>();
489 callback->appCallback = func;
490 std::shared_ptr<BleCentralManager> bleCentralManager = std::make_shared<BleCentralManager>(callback);
491 int id = g_bleCentralManagerMap.AddLimitedObject(bleCentralManager);
492 if (id == -1) {
493 HILOGE("BleRegisterScanCallbacks fail.");
494 return OHOS_BT_STATUS_FAIL;
495 }
496 *scannerId = id;
497 HILOGI("BleRegisterScanCallbacks success scannerId: %{public}d", *scannerId);
498 return OHOS_BT_STATUS_SUCCESS;
499 }
500
501 /**
502 * @brief Deregister ble scan callbacks.
503 *
504 * @param scannerId Indicates the scanner id.
505 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the BLE callbacks are deregistered;
506 * returns an error code defined in {@link BtStatus} otherwise.
507 * @since 6
508 */
BleDeregisterScanCallbacks(int32_t scannerId)509 int BleDeregisterScanCallbacks(int32_t scannerId)
510 {
511 HILOGI("BleDeregisterScanCallbacks enter. scannerId: %{public}d", scannerId);
512 if (g_bleCentralManagerMap.GetObject(scannerId) == nullptr) {
513 HILOGE("BleDeregisterScanCallbacks fail, scannerId is invalid.");
514 return OHOS_BT_STATUS_FAIL;
515 }
516 g_bleCentralManagerMap.RemoveObject(scannerId);
517 return OHOS_BT_STATUS_SUCCESS;
518 }
519
520 /**
521 * @brief Sets advertising data and parameters and starts advertising.
522 *
523 * This function is available for system applications only. \n
524 *
525 * @param advId Indicates the pointer to the advertisement ID.
526 * @param rawData Indicates the advertising data. For details, see {@link StartAdvRawData}.
527 * @param advParam Indicates the advertising parameters. For details, see {@link BleAdvParams}.
528 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the operation is successful;
529 * returns an error code defined in {@link BtStatus} otherwise.
530 * @since 6
531 */
BleStartAdvEx(int * advId,const StartAdvRawData rawData,BleAdvParams advParam)532 int BleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam)
533 {
534 HILOGI("BleStartAdvEx enter");
535 if (g_BleAdvertiser == nullptr) {
536 g_BleAdvertiser = std::make_shared<BleAdvertiser>();
537 }
538 int i = 0;
539 for (i = 0; i < MAX_BLE_ADV_NUM; i++) {
540 if (g_bleAdvCallbacks[i] == nullptr) {
541 g_bleAdvCallbacks[i] = std::make_shared<BleAdvCallback>(i);
542 break;
543 }
544 }
545
546 if (i == MAX_BLE_ADV_NUM) {
547 return OHOS_BT_STATUS_UNHANDLED;
548 }
549
550 *advId = i;
551 HILOGI("ret advId: %{public}d.", *advId);
552
553 BleAdvertiserSettings settings;
554 settings.SetInterval(advParam.minInterval);
555 if (advParam.advType == OHOS_BLE_ADV_SCAN_IND ||
556 advParam.advType == OHOS_BLE_ADV_NONCONN_IND) {
557 settings.SetConnectable(false);
558 }
559
560 auto advData = ConvertDataToVec(rawData.advData, rawData.advDataLen);
561 auto scanResponse = ConvertDataToVec(rawData.rspData, rawData.rspDataLen);
562 g_BleAdvertiser->StartAdvertising(settings, advData, scanResponse, *g_bleAdvCallbacks[i]);
563 return OHOS_BT_STATUS_SUCCESS;
564 }
565
SetServiceUuidParameter(BleScanFilter & scanFilter,BleScanNativeFilter * nativeScanFilter)566 static bool SetServiceUuidParameter(BleScanFilter &scanFilter, BleScanNativeFilter *nativeScanFilter)
567 {
568 HILOGI("SetServiceUuidParameter enter");
569 if (nativeScanFilter->serviceUuidLength != 0 && nativeScanFilter->serviceUuid != nullptr) {
570 if (!regex_match(std::string(reinterpret_cast<char *>(nativeScanFilter->serviceUuid)), uuidRegex)) {
571 HILOGE("match the UUID faild.");
572 return false;
573 }
574 UUID serviceUuid = UUID::FromString((char *)nativeScanFilter->serviceUuid);
575 scanFilter.SetServiceUuid(serviceUuid);
576 if (nativeScanFilter->serviceUuidMask != nullptr) {
577 if (!regex_match(std::string(reinterpret_cast<char *>(nativeScanFilter->serviceUuidMask)), uuidRegex)) {
578 HILOGE("match the UUID faild.");
579 return false;
580 }
581 UUID serviceUuidMask = UUID::FromString((char *)nativeScanFilter->serviceUuidMask);
582 scanFilter.SetServiceUuidMask(serviceUuidMask);
583 }
584 }
585 return true;
586 }
587
588 /**
589 * @brief Sets one scan filter config.
590 *
591 * @param scanFilter Indicates the framework object of scan filter, see {@link BleScanFilter}.
592 * @param nativeScanFilter Indicates the pointer to the scan filter. For details, see {@link BleScanNativeFilter}.
593 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set onr scan filter config success;
594 * returns an error code defined in {@link BtStatus} otherwise.
595 * @since 6
596 */
SetOneScanFilter(BleScanFilter & scanFilter,BleScanNativeFilter * nativeScanFilter)597 static int SetOneScanFilter(BleScanFilter &scanFilter, BleScanNativeFilter *nativeScanFilter)
598 {
599 HILOGI("SetOneScanFilter enter");
600 if (nativeScanFilter->address != nullptr) {
601 scanFilter.SetDeviceId(nativeScanFilter->address);
602 }
603 if (nativeScanFilter->deviceName != nullptr) {
604 scanFilter.SetName(nativeScanFilter->deviceName);
605 }
606 bool isSuccess = SetServiceUuidParameter(scanFilter, nativeScanFilter);
607 if (!isSuccess) {
608 HILOGE("SetServiceUuidParameter faild.");
609 return OHOS_BT_STATUS_PARM_INVALID;
610 }
611
612 if (nativeScanFilter->serviceData != nullptr) {
613 std::vector<uint8_t> serviceData;
614 for (unsigned int i = 0; i < nativeScanFilter->serviceDataLength; i++) {
615 serviceData.push_back(nativeScanFilter->serviceData[i]);
616 }
617 scanFilter.SetServiceData(serviceData);
618
619 if (nativeScanFilter->serviceDataMask != nullptr) {
620 std::vector<uint8_t> serviceDataMask;
621 for (unsigned int i = 0; i < nativeScanFilter->serviceDataLength; i++) {
622 serviceDataMask.push_back(nativeScanFilter->serviceDataMask[i]);
623 }
624 scanFilter.SetServiceDataMask(serviceDataMask);
625 }
626 }
627
628 if (nativeScanFilter->manufactureData != nullptr) {
629 std::vector<uint8_t> manufactureData;
630 for (unsigned int i = 0; i < nativeScanFilter->manufactureDataLength; i++) {
631 manufactureData.push_back(nativeScanFilter->manufactureData[i]);
632 }
633 scanFilter.SetManufactureData(manufactureData);
634
635 if (nativeScanFilter->manufactureDataMask != nullptr) {
636 std::vector<uint8_t> manufactureDataMask;
637 for (unsigned int i = 0; i < nativeScanFilter->manufactureDataLength; i++) {
638 manufactureDataMask.push_back(nativeScanFilter->manufactureDataMask[i]);
639 }
640 scanFilter.SetManufactureDataMask(manufactureDataMask);
641 }
642
643 if (nativeScanFilter->manufactureId != 0) {
644 scanFilter.SetManufacturerId(nativeScanFilter->manufactureId);
645 }
646 }
647 return OHOS_BT_STATUS_SUCCESS;
648 }
649
650 /**
651 * @brief Sets scan filter configs.
652 *
653 * @param scannerId Indicates the scanner id.
654 * @param filter Indicates the pointer to the scan filter. For details, see {@link BleScanNativeFilter}.
655 * @param filterSize Indicates the number of the scan filter.
656 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set scan filter configs success;
657 * returns an error code defined in {@link BtStatus} otherwise.
658 * @since 6
659 */
SetConfigScanFilter(int32_t scannerId,const BleScanNativeFilter * filter,uint32_t filterSize)660 static int SetConfigScanFilter(int32_t scannerId, const BleScanNativeFilter *filter, uint32_t filterSize)
661 {
662 HILOGI("SetConfigScanFilter enter");
663 vector<BleScanFilter> scanFilters;
664 for (uint32_t i = 0; i < filterSize; i++) {
665 BleScanNativeFilter nativeScanFilter = filter[i];
666 BleScanFilter scanFilter;
667 int result = SetOneScanFilter(scanFilter, &nativeScanFilter);
668 if (result != OHOS_BT_STATUS_SUCCESS) {
669 HILOGE("SetOneScanFilter faild, result: %{public}d", result);
670 return OHOS_BT_STATUS_PARM_INVALID;
671 }
672 scanFilters.push_back(scanFilter);
673 }
674 std::shared_ptr<BleCentralManager> bleCentralManager = g_bleCentralManagerMap.GetObject(scannerId);
675 if (bleCentralManager == nullptr) {
676 HILOGE("SetConfigScanFilter fail, ble centra manager is null.");
677 return OHOS_BT_STATUS_FAIL;
678 }
679 bleCentralManager->ConfigScanFilter(scanFilters);
680 return OHOS_BT_STATUS_SUCCESS;
681 }
682
683 /**
684 * @brief Starts a scan with BleScanConfigs.
685 * If don't need ble scan filter, set BleScanNativeFilter to NULL or filterSize to zero.
686 * If one of the ble scan filtering rules is not required, set it to NULL.
687 * For example, set the address to NULL when you don't need it.
688 * Don't support only using manufactureId as filter conditions, need to use it with manufactureData.
689 * The manufactureId need to be set a related number when you need a filtering condition of manufactureData.
690 *
691 * @param scannerId Indicates the scanner id.
692 * @param configs Indicates the pointer to the scan filter. For details, see {@link BleScanConfigs}.
693 * @param filter Indicates the pointer to the scan filter. For details, see {@link BleScanNativeFilter}.
694 * @param filterSize Indicates the number of the scan filter.
695 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if the scan is started;
696 * returns an error code defined in {@link BtStatus} otherwise.
697 * @since 6
698 */
BleStartScanEx(int32_t scannerId,const BleScanConfigs * configs,const BleScanNativeFilter * filter,uint32_t filterSize)699 int BleStartScanEx(int32_t scannerId, const BleScanConfigs *configs, const BleScanNativeFilter *filter,
700 uint32_t filterSize)
701 {
702 HILOGI("BleStartScanEx enter, scannerId: %{public}d, filterSize %{public}u", scannerId, filterSize);
703 if (configs == nullptr) {
704 HILOGE("BleStartScanEx fail, configs is null.");
705 return OHOS_BT_STATUS_FAIL;
706 }
707 std::shared_ptr<BleCentralManager> bleCentralManager = g_bleCentralManagerMap.GetObject(scannerId);
708 if (bleCentralManager == nullptr) {
709 HILOGE("BleStartScanEx fail, scannerId is invalid.");
710 return OHOS_BT_STATUS_FAIL;
711 }
712
713 if (filter != nullptr && filterSize != 0) {
714 int result = SetConfigScanFilter(scannerId, filter, filterSize);
715 if (result != OHOS_BT_STATUS_SUCCESS) {
716 HILOGE("SetConfigScanFilter faild, result: %{public}d", result);
717 return OHOS_BT_STATUS_PARM_INVALID;
718 }
719 }
720
721 HILOGI("scanMode: %{public}d", configs->scanMode);
722 BleScanSettings settings;
723 settings.SetScanMode(configs->scanMode);
724 bleCentralManager->StartScan(settings);
725 return OHOS_BT_STATUS_SUCCESS;
726 }
727
728 /**
729 * @brief Get BleCentralManager object.
730 * get one from existing objects, if not exist, create one.
731 * used to operate by BleCentralManager object, but not related to one scan.
732 *
733 * @param bleCentralManager the pointer of BleCentralManager.
734 * @since 6
735 */
GetBleCentralManagerObject(std::shared_ptr<BleCentralManager> & bleCentralManager)736 void GetBleCentralManagerObject(std::shared_ptr<BleCentralManager> &bleCentralManager)
737 {
738 bleCentralManager = g_bleCentralManagerMap.GetObject();
739 if (bleCentralManager == nullptr) {
740 HILOGI("no exist BleCentralManager");
741 std::shared_ptr<BleCentralManagerCallbackWapper> callback = std::make_shared<BleCentralManagerCallbackWapper>();
742 bleCentralManager = std::make_shared<BleCentralManager>(callback);
743 }
744 }
745
746 /**
747 * @brief set low power device adv param.
748 *
749 * @param duration advertise duration.
750 * @param maxExtAdvEvents maximum number of extended advertising events.
751 * @param window work window.
752 * @param interval work interval.
753 * @param advHandle Indicates the advertise handle.
754 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set success;
755 * returns an error code defined in {@link BtStatus} otherwise.
756 * @since 6
757 */
SetLpDeviceAdvParam(int duration,int maxExtAdvEvents,int window,int interval,int advHandle)758 int SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle)
759 {
760 HILOGI("SetLpDeviceAdvParam enter. duration: %{public}d, maxExtAdvEvents: %{public}d, window: %{public}d \
761 interval: %{public}d, advHandle: %{public}d", duration, maxExtAdvEvents, window, interval, advHandle);
762 if (duration < LPDEVICE_ADVERTISING_DURATION_MIN || duration > LPDEVICE_ADVERTISING_DURATION_MAX) {
763 HILOGE("duration out of range:, duration: %{public}d", duration);
764 return OHOS_BT_STATUS_PARM_INVALID;
765 }
766
767 if (maxExtAdvEvents < LPDEVICE_ADVERTISING_EXTADVEVENT_MIN
768 || maxExtAdvEvents > LPDEVICE_ADVERTISING_EXTADVEVENT_MAX) {
769 HILOGE("maxExtAdvEvents out of range:, maxExtAdvEvents: %{public}d", maxExtAdvEvents);
770 return OHOS_BT_STATUS_PARM_INVALID;
771 }
772
773 if (window < LPDEVICE_ADVERTISING_WINDOW_MIN || window > LPDEVICE_ADVERTISING_WINDOW_MAX) {
774 HILOGE("window out of range:, window: %{public}d", window);
775 return OHOS_BT_STATUS_PARM_INVALID;
776 }
777
778 if (interval < LPDEVICE_ADVERTISING_INTERVAL_MIN || interval > LPDEVICE_ADVERTISING_INTERVAL_MAX) {
779 HILOGE("interval out of range:, interval: %{public}d", interval);
780 return OHOS_BT_STATUS_PARM_INVALID;
781 }
782
783 if (interval < window) {
784 HILOGE("interval must bigger than window, interval: %{public}d, window: %{public}d", interval, window);
785 return OHOS_BT_STATUS_PARM_INVALID;
786 }
787 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
788 GetBleCentralManagerObject(bleCentralManager);
789 if (bleCentralManager == nullptr) {
790 HILOGE("get BleCentralManager fail.");
791 return OHOS_BT_STATUS_FAIL;
792 }
793 if (bleCentralManager->SetLpDeviceAdvParam(duration, maxExtAdvEvents, window, interval, advHandle) !=
794 BT_NO_ERROR) {
795 return OHOS_BT_STATUS_FAIL;
796 }
797 return OHOS_BT_STATUS_SUCCESS;
798 }
799
800 /**
801 * @brief Set scan report channel.
802 *
803 * @param scannerId Indicates the scanner id.
804 * @param enable true:report to low power device; false:not report to low power device.
805 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set report channel success;
806 * returns an error code defined in {@link BtStatus} otherwise.
807 * @since 6
808 */
SetScanReportChannelToLpDevice(int32_t scannerId,bool enable)809 int SetScanReportChannelToLpDevice(int32_t scannerId, bool enable)
810 {
811 HILOGI("SetScanReportChannelToLpDevice enter. scannerId: %{public}d, isToAp: %{public}d", scannerId, enable);
812 std::shared_ptr<BleCentralManager> bleCentralManager = g_bleCentralManagerMap.GetObject(scannerId);
813 if (bleCentralManager == nullptr) {
814 HILOGE("SetScanReportChannelToLpDevice fail, ble centra manager is null.");
815 return OHOS_BT_STATUS_FAIL;
816 }
817 if (bleCentralManager->SetScanReportChannelToLpDevice(enable) != BT_NO_ERROR) {
818 return OHOS_BT_STATUS_FAIL;
819 }
820 return OHOS_BT_STATUS_SUCCESS;
821 }
822
823 /**
824 * @brief Enable synchronizing data to low power device.
825 *
826 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if enable sync success;
827 * returns an error code defined in {@link BtStatus} otherwise.
828 * @since 6
829 */
EnableSyncDataToLpDevice(void)830 int EnableSyncDataToLpDevice(void)
831 {
832 HILOGI("EnableSyncDataToLpDevice enter");
833 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
834 GetBleCentralManagerObject(bleCentralManager);
835 if (bleCentralManager == nullptr) {
836 HILOGE("get BleCentralManager fail.");
837 return OHOS_BT_STATUS_FAIL;
838 }
839 if (bleCentralManager->EnableSyncDataToLpDevice() != BT_NO_ERROR) {
840 return OHOS_BT_STATUS_FAIL;
841 }
842 return OHOS_BT_STATUS_SUCCESS;
843 }
844
845 /**
846 * @brief Disable synchronizing data to the low power device.
847 *
848 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if disable sync success;
849 * returns an error code defined in {@link BtStatus} otherwise.
850 * @since 6
851 */
DisableSyncDataToLpDevice(void)852 int DisableSyncDataToLpDevice(void)
853 {
854 HILOGI("DisableSyncDataToLpDevice enter");
855 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
856 GetBleCentralManagerObject(bleCentralManager);
857 if (bleCentralManager == nullptr) {
858 HILOGE("get BleCentralManager fail.");
859 return OHOS_BT_STATUS_FAIL;
860 }
861 if (bleCentralManager->DisableSyncDataToLpDevice() != BT_NO_ERROR) {
862 return OHOS_BT_STATUS_FAIL;
863 }
864 return OHOS_BT_STATUS_SUCCESS;
865 }
866
867 /**
868 * @brief Get advertiser handle.
869 *
870 * @param advId Indicates the advertisement ID.
871 * @param advHandle Indicates the pointer to the advertiser handle.
872 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if get success;
873 * returns an error code defined in {@link BtStatus} otherwise.
874 * @since 6
875 */
GetAdvHandle(int advId,int * advHandle)876 int GetAdvHandle(int advId, int *advHandle)
877 {
878 HILOGI("GetAdvHandle enter. advId: %{public}d", advId);
879 if (advHandle == nullptr) {
880 HILOGE("GetAdvHandle fail, advHandle is nullptr.");
881 return OHOS_BT_STATUS_PARM_INVALID;
882 }
883 if (advId < 0 || advId >= MAX_BLE_ADV_NUM) {
884 HILOGE("GetAdvHandle fail, advId is invalid.advId: %{public}d.", advId);
885 return OHOS_BT_STATUS_PARM_INVALID;
886 }
887 if (g_BleAdvertiser == nullptr || g_bleAdvCallbacks[advId] == nullptr) {
888 HILOGE("GetAdvHandle fail, the current adv is not started.");
889 return OHOS_BT_STATUS_FAIL;
890 }
891 *advHandle = g_BleAdvertiser->GetAdvHandle(*g_bleAdvCallbacks[advId]);
892 return OHOS_BT_STATUS_SUCCESS;
893 }
894
895 /**
896 * @brief Translate ParamData to low power device.
897 *
898 * @param data Indicates the pointer to the data.
899 * @param dataSize Indicates the data size.
900 * @param type Indicates the data type.
901 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set param to low power device success;
902 * returns an error code defined in {@link BtStatus} otherwise.
903 * @since 6
904 */
SendParamsToLpDevice(const uint8_t * data,uint32_t dataSize,int32_t type)905 int SendParamsToLpDevice(const uint8_t *data, uint32_t dataSize, int32_t type)
906 {
907 HILOGI("SendParamsToLpDevice enter. type: %{public}d, dataSize: %{public}d", type, dataSize);
908 if (data == nullptr || dataSize <= 0) {
909 HILOGE("SendParamsToLpDevice fail, data is nullptr or dataSize is wrong.");
910 return OHOS_BT_STATUS_PARM_INVALID;
911 }
912 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
913 GetBleCentralManagerObject(bleCentralManager);
914 if (bleCentralManager == nullptr) {
915 HILOGE("get BleCentralManager fail.");
916 return OHOS_BT_STATUS_FAIL;
917 }
918 std::vector<uint8_t> dataValue(data, data + dataSize);
919 if (bleCentralManager->SendParamsToLpDevice(std::move(dataValue), type) != BT_NO_ERROR) {
920 return OHOS_BT_STATUS_FAIL;
921 }
922 return OHOS_BT_STATUS_SUCCESS;
923 }
924
925 /**
926 * @brief Get whether low power device available.
927 *
928 * @return true: available; false: not available.
929 * @since 6
930 */
IsLpDeviceAvailable(void)931 bool IsLpDeviceAvailable(void)
932 {
933 HILOGI("IsLpDeviceAvailable enter.");
934 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
935 GetBleCentralManagerObject(bleCentralManager);
936 if (bleCentralManager == nullptr) {
937 HILOGE("get BleCentralManager fail.");
938 return OHOS_BT_STATUS_FAIL;
939 }
940
941 return bleCentralManager->IsLpDeviceAvailable();
942 }
943
BleScanNativeFilterLog(BleScanNativeFilter & filter)944 void BleScanNativeFilterLog(BleScanNativeFilter &filter)
945 {
946 if (filter.address != nullptr) {
947 HILOGI("address: %{public}s", filter.address);
948 }
949 if (filter.deviceName != nullptr) {
950 HILOGI("deviceName: %{public}s", filter.deviceName);
951 }
952 if (filter.serviceUuidLength != 0) {
953 if (filter.serviceUuid != nullptr) {
954 HILOGI("serviceUuid: %{public}s", reinterpret_cast<char *>(filter.serviceUuid));
955 }
956 if (filter.serviceUuidMask != nullptr) {
957 HILOGI("serviceUuidMask: %{public}s", reinterpret_cast<char *>(filter.serviceUuidMask));
958 }
959 }
960 std::string dataStr;
961 if (filter.serviceDataLength != 0) {
962 if (filter.serviceData != nullptr) {
963 dataStr = "";
964 ConvertDataToHex(filter.serviceData, filter.serviceDataLength, dataStr);
965 HILOGI("serviceData: %{public}s", dataStr.c_str());
966 }
967 if (filter.serviceDataMask != nullptr) {
968 dataStr = "";
969 ConvertDataToHex(filter.serviceDataMask, filter.serviceDataLength, dataStr);
970 HILOGI("serviceDataMask: %{public}s", dataStr.c_str());
971 }
972 }
973 if (filter.manufactureDataLength != 0) {
974 if (filter.manufactureData != nullptr) {
975 dataStr = "";
976 ConvertDataToHex(filter.manufactureData, filter.manufactureDataLength, dataStr);
977 HILOGI("manufactureData: %{public}s", dataStr.c_str());
978 }
979 if (filter.manufactureDataMask != nullptr) {
980 dataStr = "";
981 ConvertDataToHex(filter.manufactureDataMask, filter.manufactureDataLength, dataStr);
982 HILOGI("manufactureDataMask: %{public}s", dataStr.c_str());
983 }
984 HILOGI("manufactureId: %{public}d", filter.manufactureId);
985 }
986 }
987
ConvertScanFilterParam(const BtLpDeviceParam * param,std::vector<BleScanFilter> & filter)988 bool ConvertScanFilterParam(const BtLpDeviceParam *param, std::vector<BleScanFilter> &filter)
989 {
990 for (unsigned int i = 0; i < param->filterSize; i++) {
991 BleScanNativeFilter nativeScanFilter = param->filter[i];
992 BleScanNativeFilterLog(nativeScanFilter);
993 BleScanFilter scanFilter;
994 int result = SetOneScanFilter(scanFilter, &nativeScanFilter);
995 if (result != OHOS_BT_STATUS_SUCCESS) {
996 HILOGE("SetOneScanFilter faild, result: %{public}d", result);
997 return false;
998 }
999 filter.push_back(scanFilter);
1000 }
1001 return true;
1002 }
1003
ConvertAdvSettingParam(const BtLpDeviceParam * param,BleAdvertiserSettings & advSettings)1004 void ConvertAdvSettingParam(const BtLpDeviceParam *param, BleAdvertiserSettings &advSettings)
1005 {
1006 HILOGI("BleAdvParams: minInterval: %{public}d, advType: %{public}d",
1007 param->advParam.minInterval, param->advParam.advType);
1008 advSettings.SetInterval(param->advParam.minInterval);
1009 if (param->advParam.advType == OHOS_BLE_ADV_SCAN_IND ||
1010 param->advParam.advType == OHOS_BLE_ADV_NONCONN_IND) {
1011 advSettings.SetConnectable(false);
1012 }
1013 }
1014
ConvertAdvDeviceInfo(const BtLpDeviceParam * param,std::vector<BleActiveDeviceInfo> & activeDeviceInfos)1015 bool ConvertAdvDeviceInfo(const BtLpDeviceParam *param, std::vector<BleActiveDeviceInfo> &activeDeviceInfos)
1016 {
1017 if (param->activeDeviceInfo == nullptr || param->activeDeviceSize == 0) {
1018 return true;
1019 }
1020 for (unsigned int i = 0; i < param->activeDeviceSize; i++) {
1021 HILOGI("ActiveDeviceInfo: status: %{public}d, timeOut: %{public}d",
1022 param->activeDeviceInfo[i].status, param->activeDeviceInfo[i].timeOut);
1023 BleActiveDeviceInfo deviceInfo;
1024 std::vector<int8_t> value(param->activeDeviceInfo[i].deviceId,
1025 param->activeDeviceInfo[i].deviceId + OHOS_ACTIVE_DEVICE_ID_LEN);
1026 deviceInfo.deviceId = value;
1027 deviceInfo.status = param->activeDeviceInfo[i].status;
1028 deviceInfo.timeOut = param->activeDeviceInfo[i].timeOut;
1029 activeDeviceInfos.push_back(deviceInfo);
1030 }
1031 return true;
1032 }
1033
ConvertBtUuid(const BtUuid & inUuid,UUID & outUuid)1034 bool ConvertBtUuid(const BtUuid &inUuid, UUID &outUuid)
1035 {
1036 if (inUuid.uuid == nullptr || inUuid.uuidLen == 0) {
1037 HILOGE("uuid is NULL or len is 0.");
1038 return false;
1039 }
1040 string strUuid(inUuid.uuid);
1041 HILOGD("UUID: %{public}s", strUuid.c_str());
1042 if (!regex_match(strUuid, uuidRegex)) {
1043 HILOGE("match the UUID faild.");
1044 return false;
1045 }
1046 outUuid = UUID::FromString(strUuid);
1047 return true;
1048 }
1049
ConvertLpDeviceParamData(const BtLpDeviceParam * inParam,BleLpDeviceParamSet & outParam)1050 int ConvertLpDeviceParamData(const BtLpDeviceParam *inParam, BleLpDeviceParamSet &outParam)
1051 {
1052 outParam.fieldValidFlagBit = 0;
1053 if (!ConvertBtUuid(inParam->uuid, outParam.uuid)) {
1054 return OHOS_BT_STATUS_PARM_INVALID;
1055 }
1056
1057 if (inParam->scanConfig != nullptr) {
1058 outParam.scanSettings.SetScanMode(inParam->scanConfig->scanMode);
1059 outParam.fieldValidFlagBit |= BLE_LPDEVICE_SCAN_SETTING_VALID_BIT;
1060 }
1061
1062 if (inParam->filter != nullptr && inParam->filterSize > 0) {
1063 if (!ConvertScanFilterParam(inParam, outParam.scanFilters)) {
1064 return OHOS_BT_STATUS_PARM_INVALID;
1065 }
1066 outParam.fieldValidFlagBit |= BLE_LPDEVICE_SCAN_FILTER_VALID_BIT;
1067 }
1068
1069 outParam.fieldValidFlagBit |= BLE_LPDEVICE_ADV_SETTING_VALID_BIT;
1070 ConvertAdvSettingParam(inParam, outParam.advSettings);
1071
1072 if (inParam->rawData.advData != nullptr && inParam->rawData.advDataLen > 0) {
1073 outParam.advData = ConvertDataToVec(inParam->rawData.advData,
1074 inParam->rawData.advDataLen);
1075 outParam.fieldValidFlagBit |= BLE_LPDEVICE_ADVDATA_VALID_BIT;
1076 }
1077
1078 if (inParam->rawData.rspData != nullptr && inParam->rawData.rspDataLen > 0) {
1079 outParam.respData = ConvertDataToVec(inParam->rawData.rspData,
1080 inParam->rawData.rspDataLen);
1081 outParam.fieldValidFlagBit |= BLE_LPDEVICE_RESPDATA_VALID_BIT;
1082 }
1083
1084 if (inParam->activeDeviceInfo != nullptr && inParam->activeDeviceSize > 0) {
1085 if (!ConvertAdvDeviceInfo(inParam, outParam.activeDeviceInfos)) {
1086 return OHOS_BT_STATUS_PARM_INVALID;
1087 }
1088 outParam.fieldValidFlagBit |= BLE_LPDEVICE_ADV_DEVICEINFO_VALID_BIT;
1089 }
1090
1091 outParam.advHandle = inParam->advHandle;
1092 outParam.duration = inParam->duration;
1093 outParam.deliveryMode = inParam->deliveryMode;
1094 return OHOS_BT_STATUS_SUCCESS;
1095 }
1096
1097 /**
1098 * @brief Set low power device Param.
1099 *
1100 * @param lpDeviceParam the param set to low power device.
1101 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if set lpDeviceParam success;
1102 * returns an error code defined in {@link BtStatus} otherwise.
1103 * @since 6
1104 */
SetLpDeviceParam(const BtLpDeviceParam * lpDeviceParam)1105 int SetLpDeviceParam(const BtLpDeviceParam *lpDeviceParam)
1106 {
1107 HILOGI("SetLpDeviceParam enter.");
1108 if (lpDeviceParam == nullptr) {
1109 HILOGE("SetLpDeviceParam fail, lpDeviceParam is null.");
1110 return OHOS_BT_STATUS_PARM_INVALID;
1111 }
1112 BleLpDeviceParamSet paramSet;
1113 int ret = ConvertLpDeviceParamData(lpDeviceParam, paramSet);
1114 if (ret != OHOS_BT_STATUS_SUCCESS) {
1115 return ret;
1116 }
1117 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
1118 GetBleCentralManagerObject(bleCentralManager);
1119 if (bleCentralManager == nullptr) {
1120 HILOGE("get BleCentralManager fail.");
1121 return OHOS_BT_STATUS_FAIL;
1122 }
1123
1124 HILOGI("SetLpDeviceParam fieldValidFlagBit: %{public}u", paramSet.fieldValidFlagBit);
1125 if (bleCentralManager->SetLpDeviceParam(paramSet) != BT_NO_ERROR) {
1126 return OHOS_BT_STATUS_FAIL;
1127 }
1128 return OHOS_BT_STATUS_SUCCESS;
1129 }
1130
1131 /**
1132 * @brief Remove low power device Param.
1133 *
1134 * @param uuid Uuid.
1135 * @return Returns {@link OHOS_BT_STATUS_SUCCESS} if remove success;
1136 * returns an error code defined in {@link BtStatus} otherwise.
1137 * @since 6
1138 */
RemoveLpDeviceParam(BtUuid uuid)1139 int RemoveLpDeviceParam(BtUuid uuid)
1140 {
1141 HILOGI("RemoveLpDeviceParam enter.");
1142 std::shared_ptr<BleCentralManager> bleCentralManager = nullptr;
1143 GetBleCentralManagerObject(bleCentralManager);
1144 if (bleCentralManager == nullptr) {
1145 HILOGE("get BleCentralManager fail.");
1146 return OHOS_BT_STATUS_FAIL;
1147 }
1148 UUID srvUuid;
1149 if (!ConvertBtUuid(uuid, srvUuid)) {
1150 return OHOS_BT_STATUS_PARM_INVALID;
1151 }
1152 if (bleCentralManager->RemoveLpDeviceParam(srvUuid) != BT_NO_ERROR) {
1153 return OHOS_BT_STATUS_FAIL;
1154 }
1155 return OHOS_BT_STATUS_SUCCESS;
1156 }
1157
1158 } // namespace Bluetooth
1159 } // namespace OHOS
1160 #ifdef __cplusplus
1161 }
1162 #endif
1163 /** @} */
1164