• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_ble_central_manager"
17 #endif
18 
19 #include "bluetooth_ble_central_manager.h"
20 #include "bluetooth_ble_central_manager_callback_stub.h"
21 #include "bluetooth_def.h"
22 #include "bluetooth_host.h"
23 #include "bluetooth_log.h"
24 #include "bluetooth_observer_list.h"
25 #include "bluetooth_utils.h"
26 #include "i_bluetooth_ble_central_manager.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "bluetooth_profile_manager.h"
30 
31 namespace OHOS {
32 namespace Bluetooth {
33 struct BleCentralManager::impl {
34     impl();
35     ~impl();
36 
37     void ConvertBleScanSetting(const BleScanSettings &inSettings, BluetoothBleScanSettings &outSetting);
38     void ConvertBleScanFilter(const std::vector<BleScanFilter> &filters,
39         std::vector<BluetoothBleScanFilter> &bluetoothBleScanFilters);
40     void ConvertAdvertiserSetting(const BleAdvertiserSettings &inSettings, BluetoothBleAdvertiserSettings &outSettings);
41     void ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> &inDeviceInfos,
42         std::vector<BluetoothActiveDeviceInfo> &outDeviceInfos);
43     bool InitScannerId(void);
44     int32_t CheckScanParams(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters,
45         bool checkFilter);
46 
47     class BluetoothBleCentralManagerCallbackImp : public BluetoothBleCentralManagerCallBackStub {
48     public:
BluetoothBleCentralManagerCallbackImp()49         explicit BluetoothBleCentralManagerCallbackImp() {};
50         ~BluetoothBleCentralManagerCallbackImp() override = default;
51         __attribute__((no_sanitize("cfi")))
OnScanCallback(const BluetoothBleScanResult & result,uint8_t callbackType)52         void OnScanCallback(const BluetoothBleScanResult &result, uint8_t callbackType) override
53         {
54             callbacks_.ForEach(
55                 [callbackType, &result](std::shared_ptr<BleCentralManagerCallback> observer) {
56                 BluetoothBleScanResult tempResult(result);
57                 BleScanResult scanResult;
58                 for (auto &manufacturerData : tempResult.GetManufacturerData()) {
59                     scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
60                 }
61 
62                 for (auto &serviceUuidData : tempResult.GetServiceUuids()) {
63                     UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
64                     scanResult.AddServiceUuid(uuid);
65                 }
66 
67                 for (auto &serviceData : tempResult.GetServiceData()) {
68                     UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
69                     scanResult.AddServiceData(uuid, serviceData.second);
70                 }
71 
72                 scanResult.SetAdvertiseFlag(tempResult.GetAdvertiseFlag());
73                 scanResult.SetRssi(tempResult.GetRssi());
74                 scanResult.SetConnectable(tempResult.IsConnectable());
75                 BluetoothRemoteDevice device(tempResult.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
76                 scanResult.SetPeripheralDevice(device);
77                 scanResult.SetPayload(tempResult.GetPayload());
78                 scanResult.SetName(tempResult.GetName());
79                 scanResult.SetEventType(tempResult.GetEventType());
80                 std::string address = result.GetPeripheralDevice().GetAddress();
81                 HILOGI("device: %{public}s, len: %{public}d",
82                     GetEncryptAddr(address).c_str(), static_cast<int>(scanResult.GetPayload().size()));
83                 if (callbackType == BLE_SCAN_CALLBACK_TYPE_ALL_MATCH) {
84                     observer->OnScanCallback(scanResult);
85                 } else {
86                     observer->OnFoundOrLostCallback(scanResult, callbackType);
87                 }
88             });
89         }
90 
OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> & results)91         void OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> &results) override
92         {
93             HILOGI("enter");
94             callbacks_.ForEach([&results](std::shared_ptr<BleCentralManagerCallback> observer) {
95                 std::vector<BleScanResult> scanResults;
96                 for (auto &result : results) {
97                     BleScanResult scanResult;
98                     for (auto &manufacturerData : result.GetManufacturerData()) {
99                         scanResult.AddManufacturerData(manufacturerData.first, manufacturerData.second);
100                     }
101 
102                     for (auto &serviceData : result.GetServiceData()) {
103                         UUID uuid = UUID::ConvertFrom128Bits(serviceData.first.ConvertTo128Bits());
104                         scanResult.AddServiceData(uuid, serviceData.second);
105                     }
106 
107                     for (auto &serviceUuidData : result.GetServiceUuids()) {
108                         UUID uuid = UUID::ConvertFrom128Bits(serviceUuidData.ConvertTo128Bits());
109                         scanResult.AddServiceUuid(uuid);
110                     }
111 
112                     scanResult.SetAdvertiseFlag(result.GetAdvertiseFlag());
113                     scanResult.SetConnectable(result.IsConnectable());
114                     scanResult.SetRssi(result.GetRssi());
115                     BluetoothRemoteDevice device(result.GetPeripheralDevice().GetAddress(), BT_TRANSPORT_BLE);
116                     scanResult.SetPeripheralDevice(device);
117                     scanResult.SetPayload(result.GetPayload());
118 
119                     scanResults.push_back(scanResult);
120                 }
121 
122                 observer->OnBleBatchScanResultsEvent(scanResults);
123             });
124         }
125 
126         __attribute__((no_sanitize("cfi")))
OnStartOrStopScanEvent(int resultCode,bool isStartScan)127         void OnStartOrStopScanEvent(int resultCode, bool isStartScan) override
128         {
129             HILOGD("resultCode: %{public}d, isStartScan: %{public}d", resultCode, isStartScan);
130             callbacks_.ForEach(
131                 [resultCode, isStartScan](std::shared_ptr<BleCentralManagerCallback> observer) {
132                     observer->OnStartOrStopScanEvent(resultCode, isStartScan);
133             });
134         }
135 
OnNotifyMsgReportFromLpDevice(const bluetooth::Uuid & uuid,int msgType,const std::vector<uint8_t> & value)136         void OnNotifyMsgReportFromLpDevice(const bluetooth::Uuid &uuid, int msgType,
137             const std::vector<uint8_t> &value) override
138         {
139             callbacks_.ForEach(
140                 [uuid, msgType, value](std::shared_ptr<BleCentralManagerCallback> observer) {
141                     UUID btUuid = UUID::ConvertFrom128Bits(uuid.ConvertTo128Bits());
142                     observer->OnNotifyMsgReportFromLpDevice(btUuid, msgType, value);
143             });
144         }
145     public:
146         BluetoothObserverList<BleCentralManagerCallback> callbacks_;
147     private:
148         BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothBleCentralManagerCallbackImp);
149     };
150     sptr<BluetoothBleCentralManagerCallbackImp> callbackImp_ = nullptr;
151 
152     int32_t scannerId_ = BLE_SCAN_INVALID_ID;  // lock by scannerIdMutex_
153     std::mutex scannerIdMutex_ {};
154     bool enableRandomAddrMode_ = true;
155     int32_t profileRegisterId = 0;
156 };
157 
impl()158 BleCentralManager::impl::impl()
159 {
160     callbackImp_ = new BluetoothBleCentralManagerCallbackImp();
161     auto bleTurnOnFunc = [this](sptr<IRemoteObject> remote) {
162         sptr<IBluetoothBleCentralManager> proxy = iface_cast<IBluetoothBleCentralManager>(remote);
163         CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
164         std::lock_guard<std::mutex> lock(scannerIdMutex_);
165         if (scannerId_ == BLE_SCAN_INVALID_ID) {
166             proxy->RegisterBleCentralManagerCallback(scannerId_, enableRandomAddrMode_, callbackImp_);
167         }
168     };
169     auto bluetoothTurnOffFunc = [this]() {
170         scannerId_ = BLE_SCAN_INVALID_ID;
171     };
172     ProfileFunctions profileFunctions = {
173         .bluetoothLoadedfunc = nullptr,
174         .bleTurnOnFunc = bleTurnOnFunc,
175         .bluetoothTurnOffFunc = bluetoothTurnOffFunc,
176     };
177     profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(
178         BLE_CENTRAL_MANAGER_SERVER, profileFunctions);
179 }
180 
InitScannerId(void)181 bool BleCentralManager::impl::InitScannerId(void)
182 {
183     sptr<IBluetoothBleCentralManager> proxy =
184         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
185     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
186 
187     std::lock_guard<std::mutex> lock(scannerIdMutex_);
188     if (scannerId_ != BLE_SCAN_INVALID_ID) {
189         HILOGI("scannerId(%{public}d) is already registered", scannerId_);
190         return true;
191     }
192 
193     proxy->RegisterBleCentralManagerCallback(scannerId_, enableRandomAddrMode_, callbackImp_);
194     return scannerId_ != BLE_SCAN_INVALID_ID;
195 }
196 
ConvertBleScanSetting(const BleScanSettings & inSettings,BluetoothBleScanSettings & outSetting)197 void BleCentralManager::impl::ConvertBleScanSetting(const BleScanSettings &inSettings,
198     BluetoothBleScanSettings &outSetting)
199 {
200     outSetting.SetReportDelay(0);
201     outSetting.SetScanMode(inSettings.GetScanMode());
202     outSetting.SetLegacy(inSettings.GetLegacy());
203     outSetting.SetPhy(inSettings.GetPhy());
204     outSetting.SetCallbackType(inSettings.GetCallbackType());
205     outSetting.SetSensitivityMode(inSettings.GetSensitivityMode());
206     outSetting.SetMatchTrackAdvType(inSettings.GetMatchTrackAdvType());
207 }
208 
ConvertBleScanFilter(const std::vector<BleScanFilter> & filters,std::vector<BluetoothBleScanFilter> & bluetoothBleScanFilters)209 void BleCentralManager::impl::ConvertBleScanFilter(const std::vector<BleScanFilter> &filters,
210     std::vector<BluetoothBleScanFilter> &bluetoothBleScanFilters)
211 {
212     for (auto filter : filters) {
213         BluetoothBleScanFilter scanFilter;
214         scanFilter.SetDeviceId(filter.GetDeviceId());
215         scanFilter.SetName(filter.GetName());
216         if (filter.HasServiceUuid()) {
217             scanFilter.SetServiceUuid(bluetooth::Uuid::ConvertFromString(
218                 filter.GetServiceUuid().ToString()));
219         }
220         if (filter.HasServiceUuidMask()) {
221             scanFilter.SetServiceUuidMask(bluetooth::Uuid::ConvertFromString(
222                 filter.GetServiceUuidMask().ToString()));
223         }
224         if (filter.HasSolicitationUuid()) {
225             scanFilter.SetServiceSolicitationUuid(bluetooth::Uuid::ConvertFromString(
226                 filter.GetServiceSolicitationUuid().ToString()));
227         }
228         if (filter.HasSolicitationUuidMask()) {
229             scanFilter.SetServiceSolicitationUuidMask(bluetooth::Uuid::ConvertFromString(
230                 filter.GetServiceSolicitationUuidMask().ToString()));
231         }
232         scanFilter.SetServiceData(filter.GetServiceData());
233         scanFilter.SetServiceDataMask(filter.GetServiceDataMask());
234         scanFilter.SetManufacturerId(filter.GetManufacturerId());
235         scanFilter.SetManufactureData(filter.GetManufactureData());
236         scanFilter.SetManufactureDataMask(filter.GetManufactureDataMask());
237         scanFilter.SetAdvIndReportFlag(filter.GetAdvIndReportFlag());
238         scanFilter.SetFilterIndex(filter.GetFilterIndex());
239         bluetoothBleScanFilters.push_back(scanFilter);
240     }
241 }
242 
ConvertAdvertiserSetting(const BleAdvertiserSettings & inSettings,BluetoothBleAdvertiserSettings & outSettings)243 void BleCentralManager::impl::ConvertAdvertiserSetting(const BleAdvertiserSettings &inSettings,
244     BluetoothBleAdvertiserSettings &outSettings)
245 {
246     outSettings.SetConnectable(inSettings.IsConnectable());
247     outSettings.SetInterval(inSettings.GetInterval());
248     outSettings.SetLegacyMode(inSettings.IsLegacyMode());
249     outSettings.SetTxPower(inSettings.GetTxPower());
250 }
251 
ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> & inDeviceInfos,std::vector<BluetoothActiveDeviceInfo> & outDeviceInfos)252 void BleCentralManager::impl::ConvertActiveDeviceInfo(const std::vector<BleActiveDeviceInfo> &inDeviceInfos,
253     std::vector<BluetoothActiveDeviceInfo> &outDeviceInfos)
254 {
255     for (auto info : inDeviceInfos) {
256         BluetoothActiveDeviceInfo deviceInfo;
257         deviceInfo.deviceId = info.deviceId;
258         deviceInfo.status = info.status;
259         deviceInfo.timeOut = info.timeOut;
260         outDeviceInfos.push_back(deviceInfo);
261     }
262 }
263 
CheckScanParams(const BleScanSettings & settings,const std::vector<BleScanFilter> & filters,bool checkFilter)264 int32_t BleCentralManager::impl::CheckScanParams(const BleScanSettings &settings,
265     const std::vector<BleScanFilter> &filters, bool checkFilter)
266 {
267     uint8_t callbackType = settings.GetCallbackType();
268     if (callbackType != BLE_SCAN_CALLBACK_TYPE_ALL_MATCH &&
269         callbackType != BLE_SCAN_CALLBACK_TYPE_FIRST_MATCH &&
270         callbackType != BLE_SCAN_CALLBACK_TYPE_LOST_MATCH &&
271         callbackType != BLE_SCAN_CALLBACK_TYPE_FIRST_AND_LOST_MATCH) {
272         HILOGE("Illegal callbackType argument %{public}d", callbackType);
273         return BT_ERR_INVALID_PARAM;
274     }
275 
276     if ((callbackType & BLE_SCAN_CALLBACK_TYPE_FIRST_AND_LOST_MATCH) != 0 && checkFilter) {
277         if (filters.size() == 0) {
278             HILOGE("onFound/onLost need non-empty filters callbackType: %{public}d", callbackType);
279             return BT_ERR_INVALID_PARAM;
280         }
281         for (auto filter : filters) {
282             BleScanFilter emptyFilter;
283             if (filter == emptyFilter) {
284                 HILOGE("onFound/onLost need non-empty filter callbackType: %{public}d", callbackType);
285                 return BT_ERR_INVALID_PARAM;
286             }
287         }
288     }
289 
290     uint8_t matchTrackAdvType = settings.GetMatchTrackAdvType();
291     if (matchTrackAdvType < ONE_MATCH_TRACK_ADV || matchTrackAdvType > MAX_MATCH_TRACK_ADV) {
292         HILOGE("Illegal matchTrackAdvType argument %{public}d", matchTrackAdvType);
293         return BT_ERR_INVALID_PARAM;
294     }
295 
296     return BT_NO_ERROR;
297 }
298 
~impl()299 BleCentralManager::impl::~impl()
300 {
301     HILOGD("start");
302     BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
303     sptr<IBluetoothBleCentralManager> proxy =
304         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
305     CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
306     proxy->DeregisterBleCentralManagerCallback(scannerId_, callbackImp_);
307 }
308 
BleCentralManager(BleCentralManagerCallback & callback)309 BleCentralManager::BleCentralManager(BleCentralManagerCallback &callback) : pimpl(nullptr)
310 {
311     if (pimpl == nullptr) {
312         pimpl = std::make_unique<impl>();
313         if (pimpl == nullptr) {
314             HILOGE("failed, no pimpl");
315             return;
316         }
317     }
318 
319     HILOGI("successful");
320     std::shared_ptr<BleCentralManagerCallback> pointer(&callback, [](BleCentralManagerCallback *) {});
321     bool ret = pimpl->callbackImp_->callbacks_.Register(pointer);
322     if (ret)
323         return;
324 }
325 
BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback,bool enableRandomAddrMode)326 BleCentralManager::BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode)
327     : pimpl(nullptr)
328 {
329     if (pimpl == nullptr) {
330         pimpl = std::make_unique<impl>();
331         if (pimpl == nullptr) {
332             HILOGE("failed, no pimpl");
333             return;
334         }
335     }
336     HILOGI("successful");
337     pimpl->enableRandomAddrMode_ = enableRandomAddrMode;
338     pimpl->callbackImp_->callbacks_.Register(callback);
339 }
340 
~BleCentralManager()341 BleCentralManager::~BleCentralManager()
342 {
343     HILOGD("~BleCentralManager");
344 }
345 
StartScan(const BleScanSettings & settings,const std::vector<BleScanFilter> & filters)346 int BleCentralManager::StartScan(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters)
347 {
348     if (!IS_BLE_ENABLED()) {
349         HILOGD("bluetooth is off.");
350         return BT_ERR_INVALID_STATE;
351     }
352 
353     int ret = pimpl->CheckScanParams(settings, filters, true);
354     if (ret != BT_NO_ERROR) {
355         return ret;
356     }
357 
358     if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
359         HILOGE("init scannerId failed");
360         return BT_ERR_INTERNAL_ERROR;
361     }
362 
363     HILOGD("StartScan with params, scannerId: %{public}d, callbackType: %{public}d, sensitivityMode: %{public}d",
364         pimpl->scannerId_, settings.GetCallbackType(), settings.GetSensitivityMode());
365     BluetoothBleScanSettings parcelSettings;
366     pimpl->ConvertBleScanSetting(settings, parcelSettings);
367 
368     std::vector<BluetoothBleScanFilter> parcelFilters;
369     pimpl->ConvertBleScanFilter(filters, parcelFilters);
370 
371     sptr<IBluetoothBleCentralManager> proxy =
372         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
373     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
374     return proxy->StartScan(pimpl->scannerId_, parcelSettings, parcelFilters, isNewApi_);
375 }
376 
StopScan()377 int BleCentralManager::StopScan()
378 {
379     if (!IS_BLE_ENABLED()) {
380         HILOGD("bluetooth is off.");
381         return BT_ERR_INVALID_STATE;
382     }
383 
384     if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
385         HILOGE("init scannerId failed");
386         return BT_ERR_INTERNAL_ERROR;
387     }
388 
389     sptr<IBluetoothBleCentralManager> proxy =
390         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
391     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
392     HILOGD("scannerId_: %{public}d", pimpl->scannerId_);
393 
394     int ret = proxy->StopScan(pimpl->scannerId_);
395     proxy->RemoveScanFilter(pimpl->scannerId_);
396     return ret;
397 }
398 
SetLpDeviceAdvParam(int duration,int maxExtAdvEvents,int window,int interval,int advHandle)399 int BleCentralManager::SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle)
400 {
401     if (!IS_BLE_ENABLED()) {
402         HILOGE("bluetooth is off.");
403         return BT_ERR_INTERNAL_ERROR;
404     }
405 
406     sptr<IBluetoothBleCentralManager> proxy =
407         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
408     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
409     return proxy->SetLpDeviceAdvParam(duration, maxExtAdvEvents, window, interval, advHandle);
410 }
411 
SetScanReportChannelToLpDevice(bool enable)412 int BleCentralManager::SetScanReportChannelToLpDevice(bool enable)
413 {
414     if (!IS_BLE_ENABLED()) {
415         HILOGE("bluetooth is off.");
416         return BT_ERR_INTERNAL_ERROR;
417     }
418 
419     // need start scan first
420     if (pimpl->scannerId_ == BLE_SCAN_INVALID_ID && !pimpl->InitScannerId()) {
421         HILOGE("init scannerId failed");
422         return BT_ERR_INTERNAL_ERROR;
423     }
424     sptr<IBluetoothBleCentralManager> proxy =
425         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
426     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
427     return proxy->SetScanReportChannelToLpDevice(pimpl->scannerId_, enable);
428 }
429 
EnableSyncDataToLpDevice()430 int BleCentralManager::EnableSyncDataToLpDevice()
431 {
432     if (!IS_BLE_ENABLED()) {
433         HILOGE("bluetooth is off.");
434         return BT_ERR_INTERNAL_ERROR;
435     }
436 
437     sptr<IBluetoothBleCentralManager> proxy =
438         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
439     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
440     return proxy->EnableSyncDataToLpDevice();
441 }
442 
DisableSyncDataToLpDevice()443 int BleCentralManager::DisableSyncDataToLpDevice()
444 {
445     if (!IS_BLE_ENABLED()) {
446         HILOGE("bluetooth is off.");
447         return BT_ERR_INTERNAL_ERROR;
448     }
449 
450     sptr<IBluetoothBleCentralManager> proxy =
451         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
452     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
453     return proxy->DisableSyncDataToLpDevice();
454 }
455 
SendParamsToLpDevice(const std::vector<uint8_t> & dataValue,int32_t type)456 int BleCentralManager::SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type)
457 {
458     if (!IS_BLE_ENABLED()) {
459         HILOGE("bluetooth is off.");
460         return BT_ERR_INTERNAL_ERROR;
461     }
462 
463     sptr<IBluetoothBleCentralManager> proxy =
464         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
465     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
466     return proxy->SendParamsToLpDevice(dataValue, type);
467 }
468 
IsLpDeviceAvailable()469 bool BleCentralManager::IsLpDeviceAvailable()
470 {
471     if (!IS_BLE_ENABLED()) {
472         HILOGE("bluetooth is off.");
473         return false;
474     }
475 
476     sptr<IBluetoothBleCentralManager> proxy =
477         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
478     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy");
479     return proxy->IsLpDeviceAvailable();
480 }
481 
SetLpDeviceParam(const BleLpDeviceParamSet & lpDeviceParamSet)482 int BleCentralManager::SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet)
483 {
484     if (!IS_BLE_ENABLED()) {
485         HILOGE("bluetooth is off.");
486         return BT_ERR_INTERNAL_ERROR;
487     }
488 
489     BluetoothLpDeviceParamSet paramSet;
490     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_SCAN_SETTING_VALID_BIT) != 0) {
491         pimpl->ConvertBleScanSetting(lpDeviceParamSet.scanSettings, paramSet.btScanSettings);
492     }
493 
494     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_SCAN_FILTER_VALID_BIT) != 0) {
495         pimpl->ConvertBleScanFilter(lpDeviceParamSet.scanFilters, paramSet.btScanFilters);
496     }
497 
498     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADV_SETTING_VALID_BIT) != 0) {
499         pimpl->ConvertAdvertiserSetting(lpDeviceParamSet.advSettings, paramSet.btAdvSettings);
500     }
501 
502     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADVDATA_VALID_BIT) != 0) {
503         paramSet.btAdvData.SetPayload(std::string(lpDeviceParamSet.advData.begin(),
504             lpDeviceParamSet.advData.end()));
505     }
506 
507     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_RESPDATA_VALID_BIT) != 0) {
508         paramSet.btRespData.SetPayload(std::string(lpDeviceParamSet.respData.begin(),
509             lpDeviceParamSet.respData.end()));
510     }
511 
512     if ((lpDeviceParamSet.fieldValidFlagBit & BLE_LPDEVICE_ADV_DEVICEINFO_VALID_BIT) != 0) {
513         pimpl->ConvertActiveDeviceInfo(lpDeviceParamSet.activeDeviceInfos, paramSet.activeDeviceInfos);
514     }
515     paramSet.fieldValidFlagBit = lpDeviceParamSet.fieldValidFlagBit;
516     paramSet.uuid = bluetooth::Uuid::ConvertFromString(lpDeviceParamSet.uuid.ToString());
517     paramSet.advHandle = lpDeviceParamSet.advHandle;
518     paramSet.deliveryMode = lpDeviceParamSet.deliveryMode;
519     paramSet.duration = lpDeviceParamSet.duration;
520     sptr<IBluetoothBleCentralManager> proxy =
521         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
522     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
523     return proxy->SetLpDeviceParam(paramSet);
524 }
525 
RemoveLpDeviceParam(const UUID & uuid)526 int BleCentralManager::RemoveLpDeviceParam(const UUID &uuid)
527 {
528     if (!IS_BLE_ENABLED()) {
529         HILOGE("bluetooth is off.");
530         return BT_ERR_INTERNAL_ERROR;
531     }
532 
533     sptr<IBluetoothBleCentralManager> proxy =
534         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
535     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
536     bluetooth::Uuid btUuid = bluetooth::Uuid::ConvertFromString(uuid.ToString());
537     return proxy->RemoveLpDeviceParam(btUuid);
538 }
539 
ChangeScanParams(const BleScanSettings & settings,const std::vector<BleScanFilter> & filters,uint32_t filterAction)540 int BleCentralManager::ChangeScanParams(const BleScanSettings &settings, const std::vector<BleScanFilter> &filters,
541     uint32_t filterAction)
542 {
543     if (!IS_BLE_ENABLED()) {
544         HILOGE("bluetooth is off.");
545         return BT_ERR_INVALID_STATE;
546     }
547     CHECK_AND_RETURN_LOG_RET((pimpl->scannerId_ != BLE_SCAN_INVALID_ID), BT_ERR_INVALID_PARAM, "scannerId invalid");
548     int ret = pimpl->CheckScanParams(settings, filters, filters.size() != 0);
549     CHECK_AND_RETURN_LOG_RET((ret == BT_NO_ERROR), ret, "param invalid");
550 
551     HILOGI("scannerId:%{public}d, callbackType:%{public}d", pimpl->scannerId_, settings.GetCallbackType());
552     BluetoothBleScanSettings parcelSetting;
553     pimpl->ConvertBleScanSetting(settings, parcelSetting);
554 
555     std::vector<BluetoothBleScanFilter> parcelFilters;
556     if (filters.size() != 0) {
557         pimpl->ConvertBleScanFilter(filters, parcelFilters);
558     }
559     sptr<IBluetoothBleCentralManager> proxy =
560         GetRemoteProxy<IBluetoothBleCentralManager>(BLE_CENTRAL_MANAGER_SERVER);
561     CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_INTERNAL_ERROR, "failed: no proxy");
562     return proxy->ChangeScanParams(pimpl->scannerId_, parcelSetting, parcelFilters, filterAction);
563 }
564 
SetNewApiFlag()565 void BleCentralManager::SetNewApiFlag()
566 {
567     isNewApi_ = true;
568 }
569 
BleScanResult()570 BleScanResult::BleScanResult()
571 {}
572 
~BleScanResult()573 BleScanResult::~BleScanResult()
574 {}
575 
GetServiceUuids() const576 std::vector<UUID> BleScanResult::GetServiceUuids() const
577 {
578     return serviceUuids_;
579 }
580 
GetManufacturerData() const581 std::map<uint16_t, std::string> BleScanResult::GetManufacturerData() const
582 {
583     return manufacturerSpecificData_;
584 }
585 
GetServiceData() const586 std::map<UUID, std::string> BleScanResult::GetServiceData() const
587 {
588     return serviceData_;
589 }
590 
GetPeripheralDevice() const591 BluetoothRemoteDevice BleScanResult::GetPeripheralDevice() const
592 {
593     return peripheralDevice_;
594 }
595 
GetRssi() const596 int8_t BleScanResult::GetRssi() const
597 {
598     return rssi_;
599 }
600 
IsConnectable() const601 bool BleScanResult::IsConnectable() const
602 {
603     return connectable_;
604 }
605 
GetAdvertiseFlag() const606 uint8_t BleScanResult::GetAdvertiseFlag() const
607 {
608     return advertiseFlag_;
609 }
610 
GetPayload() const611 std::vector<uint8_t> BleScanResult::GetPayload() const
612 {
613     return payload_;
614 }
615 
AddManufacturerData(uint16_t manufacturerId,const std::string & data)616 void BleScanResult::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
617 {
618     manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
619 }
620 
AddServiceData(const UUID & uuid,const std::string & data)621 void BleScanResult::AddServiceData(const UUID &uuid, const std::string &data)
622 {
623     serviceData_.insert(std::make_pair(uuid, data));
624 }
625 
AddServiceUuid(const UUID & serviceUuid)626 void BleScanResult::AddServiceUuid(const UUID &serviceUuid)
627 {
628     serviceUuids_.push_back(serviceUuid);
629 }
630 
SetPayload(std::string payload)631 void BleScanResult::SetPayload(std::string payload)
632 {
633     payload_.assign(payload.begin(), payload.end());
634 }
635 
SetPeripheralDevice(const BluetoothRemoteDevice & device)636 void BleScanResult::SetPeripheralDevice(const BluetoothRemoteDevice &device)
637 {
638     peripheralDevice_ = device;
639 }
640 
SetRssi(int8_t rssi)641 void BleScanResult::SetRssi(int8_t rssi)
642 {
643     rssi_ = rssi;
644 }
645 
SetConnectable(bool connectable)646 void BleScanResult::SetConnectable(bool connectable)
647 {
648     connectable_ = connectable;
649 }
650 
SetAdvertiseFlag(uint8_t flag)651 void BleScanResult::SetAdvertiseFlag(uint8_t flag)
652 {
653     advertiseFlag_ = flag;
654 }
655 
SetName(const std::string & name)656 void BleScanResult::SetName(const std::string &name)
657 {
658     name_ = name;
659 }
GetName(void)660 std::string BleScanResult::GetName(void)
661 {
662     return name_;
663 }
664 
SetEventType(uint16_t eventType)665 void BleScanResult::SetEventType(uint16_t eventType)
666 {
667     eventType_ = eventType;
668 }
669 
GetEventType(void) const670 uint16_t BleScanResult::GetEventType(void) const
671 {
672     return eventType_;
673 }
674 
BleScanSettings()675 BleScanSettings::BleScanSettings()
676 {}
677 
~BleScanSettings()678 BleScanSettings::~BleScanSettings()
679 {}
680 
SetReportDelay(long reportDelayMillis)681 void BleScanSettings::SetReportDelay(long reportDelayMillis)
682 {
683     reportDelayMillis_ = reportDelayMillis;
684 }
685 
GetReportDelayMillisValue() const686 long BleScanSettings::GetReportDelayMillisValue() const
687 {
688     return reportDelayMillis_;
689 }
690 
SetScanMode(int scanMode)691 int BleScanSettings::SetScanMode(int scanMode)
692 {
693     if (scanMode < SCAN_MODE_LOW_POWER || scanMode >= SCAN_MODE_INVALID) {
694         return RET_BAD_PARAM;
695     }
696 
697     scanMode_ = scanMode;
698     return RET_NO_ERROR;
699 }
700 
GetScanMode() const701 int BleScanSettings::GetScanMode() const
702 {
703     return scanMode_;
704 }
705 
SetLegacy(bool legacy)706 void BleScanSettings::SetLegacy(bool legacy)
707 {
708     legacy_ = legacy;
709 }
710 
GetLegacy() const711 bool BleScanSettings::GetLegacy() const
712 {
713     return legacy_;
714 }
715 
SetPhy(int phy)716 void BleScanSettings::SetPhy(int phy)
717 {
718     phy_ = phy;
719 }
720 
GetPhy() const721 int BleScanSettings::GetPhy() const
722 {
723     return phy_;
724 }
725 
SetCallbackType(uint8_t callbackType)726 void BleScanSettings::SetCallbackType(uint8_t callbackType)
727 {
728     callbackType_ = callbackType;
729 }
730 
GetCallbackType() const731 uint8_t BleScanSettings::GetCallbackType() const
732 {
733     return callbackType_;
734 }
735 
SetSensitivityMode(uint8_t sensitivityMode)736 void BleScanSettings::SetSensitivityMode(uint8_t sensitivityMode)
737 {
738     sensitivityMode_ = sensitivityMode;
739 }
740 
GetSensitivityMode() const741 uint8_t BleScanSettings::GetSensitivityMode() const
742 {
743     return sensitivityMode_;
744 }
745 
SetMatchTrackAdvType(uint8_t matchTrackAdvType)746 void BleScanSettings::SetMatchTrackAdvType(uint8_t matchTrackAdvType)
747 {
748     matchTrackAdvType_ = matchTrackAdvType;
749 }
750 
GetMatchTrackAdvType() const751 uint8_t BleScanSettings::GetMatchTrackAdvType() const
752 {
753     return matchTrackAdvType_;
754 }
755 
BleScanFilter()756 BleScanFilter::BleScanFilter()
757 {}
758 
~BleScanFilter()759 BleScanFilter::~BleScanFilter()
760 {}
761 
SetDeviceId(std::string deviceId)762 void BleScanFilter::SetDeviceId(std::string deviceId)
763 {
764     deviceId_ = deviceId;
765 }
766 
GetDeviceId() const767 std::string BleScanFilter::GetDeviceId() const
768 {
769     return deviceId_;
770 }
771 
SetName(std::string name)772 void BleScanFilter::SetName(std::string name)
773 {
774     name_ = name;
775 }
776 
GetName() const777 std::string BleScanFilter::GetName() const
778 {
779     return name_;
780 }
781 
SetServiceUuid(const UUID & uuid)782 void BleScanFilter::SetServiceUuid(const UUID &uuid)
783 {
784     serviceUuid_ = uuid;
785     hasServiceUuid_ = true;
786 }
787 
HasServiceUuid()788 bool BleScanFilter::HasServiceUuid()
789 {
790     return hasServiceUuid_;
791 }
792 
GetServiceUuid() const793 UUID BleScanFilter::GetServiceUuid() const
794 {
795     return serviceUuid_;
796 }
797 
SetServiceUuidMask(const UUID & serviceUuidMask)798 void BleScanFilter::SetServiceUuidMask(const UUID &serviceUuidMask)
799 {
800     serviceUuidMask_ = serviceUuidMask;
801     hasServiceUuidMask_ = true;
802 }
803 
HasServiceUuidMask()804 bool BleScanFilter::HasServiceUuidMask()
805 {
806     return hasServiceUuidMask_;
807 }
808 
GetServiceUuidMask() const809 UUID BleScanFilter::GetServiceUuidMask() const
810 {
811     return serviceUuidMask_;
812 }
813 
SetServiceSolicitationUuid(const UUID & serviceSolicitationUuid)814 void BleScanFilter::SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid)
815 {
816     serviceSolicitationUuid_ = serviceSolicitationUuid;
817     hasSolicitationUuid_ = true;
818 }
819 
HasSolicitationUuid()820 bool BleScanFilter::HasSolicitationUuid()
821 {
822     return hasSolicitationUuid_;
823 }
824 
GetServiceSolicitationUuid() const825 UUID BleScanFilter::GetServiceSolicitationUuid() const
826 {
827     return serviceSolicitationUuid_;
828 }
829 
SetServiceSolicitationUuidMask(const UUID & serviceSolicitationUuidMask)830 void BleScanFilter::SetServiceSolicitationUuidMask(const UUID &serviceSolicitationUuidMask)
831 {
832     serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
833     hasSolicitationUuidMask_ = true;
834 }
835 
HasSolicitationUuidMask()836 bool BleScanFilter::HasSolicitationUuidMask()
837 {
838     return hasSolicitationUuidMask_;
839 }
840 
GetServiceSolicitationUuidMask() const841 UUID BleScanFilter::GetServiceSolicitationUuidMask() const
842 {
843     return serviceSolicitationUuidMask_;
844 }
845 
SetServiceData(std::vector<uint8_t> serviceData)846 void BleScanFilter::SetServiceData(std::vector<uint8_t> serviceData)
847 
848 {
849     serviceData_ = serviceData;
850 }
851 
GetServiceData() const852 std::vector<uint8_t> BleScanFilter::GetServiceData() const
853 {
854     return serviceData_;
855 }
856 
SetServiceDataMask(std::vector<uint8_t> serviceDataMask)857 void BleScanFilter::SetServiceDataMask(std::vector<uint8_t> serviceDataMask)
858 {
859     serviceDataMask_ = serviceDataMask;
860 }
861 
GetServiceDataMask() const862 std::vector<uint8_t> BleScanFilter::GetServiceDataMask() const
863 {
864     return serviceDataMask_;
865 }
866 
SetManufacturerId(uint16_t manufacturerId)867 void BleScanFilter::SetManufacturerId(uint16_t manufacturerId)
868 {
869     manufacturerId_ = manufacturerId;
870 }
871 
GetManufacturerId() const872 uint16_t BleScanFilter::GetManufacturerId() const
873 {
874     return manufacturerId_;
875 }
876 
SetManufactureData(std::vector<uint8_t> manufactureData)877 void BleScanFilter::SetManufactureData(std::vector<uint8_t> manufactureData)
878 {
879     manufactureData_ = manufactureData;
880 }
881 
GetManufactureData() const882 std::vector<uint8_t> BleScanFilter::GetManufactureData() const
883 {
884     return manufactureData_;
885 }
886 
SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)887 void BleScanFilter::SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask)
888 {
889     manufactureDataMask_ = manufactureDataMask;
890 }
891 
GetManufactureDataMask() const892 std::vector<uint8_t> BleScanFilter::GetManufactureDataMask() const
893 {
894     return manufactureDataMask_;
895 }
896 
SetAdvIndReportFlag(bool advIndReprot)897 void BleScanFilter::SetAdvIndReportFlag(bool advIndReprot)
898 {
899     advIndReprot_ = advIndReprot;
900 }
901 
GetAdvIndReportFlag() const902 bool BleScanFilter::GetAdvIndReportFlag() const
903 {
904     return advIndReprot_;
905 }
906 
SetFilterIndex(uint8_t index)907 void BleScanFilter::SetFilterIndex(uint8_t index)
908 {
909     filterIndex_ = index;
910 }
911 
GetFilterIndex() const912 uint8_t BleScanFilter::GetFilterIndex() const
913 {
914     return filterIndex_;
915 }
916 }  // namespace Bluetooth
917 }  // namespace OHOS
918