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