Skip to content

Commit 6939b5c

Browse files
committed
Jira 896, BLE scan withDuplicates logic inversion, git 476
Bug fixed: This feature was implemented in PR #457, merged, and released in Deneb.RC1. However, it was discovered that the logic was inverted and was report as Git issue #476. BLE scan() and input parameter withDuplicates is true means reporting all Peripherals detected regardless how many times it was reported. Code mods: 1. libraries/CurieBLE/src/BLEDevice.cpp: - In startScan, calls the correct routine to scan for all Peripherals if withDuplicates is true. Otherwise, just scan for new ones. 2. libraries/CurieBLE/src/BLEDevice.h: - Prototyping. 3. libraries/CurieBLE/src/internal/BLEDeviceManager.h, libraries/CurieBLE/src/internal/BLEDeviceManager.cpp - Modified header comment to reflect the input parameter withDuplictes correct meaning. - Default the input parameter, withDupilcates, as true. - And, consequently, eliminated some redundant methods.
1 parent 6b33e73 commit 6939b5c

File tree

4 files changed

+78
-79
lines changed

4 files changed

+78
-79
lines changed

libraries/CurieBLE/src/BLEDevice.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,38 +247,22 @@ bool BLEDevice::startScan(bool withDuplicates)
247247
}
248248
else
249249
{
250-
return BLEDeviceManager::instance()->startScanning();
250+
return BLEDeviceManager::instance()->startScanningNewPeripherals();
251251
}
252252
}
253253

254-
255-
void BLEDevice::scan()
256-
{
257-
scan(false);
258-
}
259-
260254
void BLEDevice::scan(bool withDuplicates)
261255
{
262256
BLEDeviceManager::instance()->clearAdvertiseCritical();
263257
startScan(withDuplicates);
264258
}
265259

266-
void BLEDevice::scanForName(String name)
267-
{
268-
scanForName(name, false);
269-
}
270-
271260
void BLEDevice::scanForName(String name, bool withDuplicates)
272261
{
273262
BLEDeviceManager::instance()->setAdvertiseCritical(name);
274263
startScan(withDuplicates);
275264
}
276265

277-
void BLEDevice::scanForUuid(String uuid)
278-
{
279-
scanForUuid(uuid, false);
280-
}
281-
282266
void BLEDevice::scanForUuid(String uuid, bool withDuplicates)
283267
{
284268
BLEService service_temp(uuid.c_str());

libraries/CurieBLE/src/BLEDevice.h

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -361,83 +361,70 @@ class BLEDevice
361361
//void scanForAddress(String address); // Not include in baseline. Add here as feature for feature release.
362362

363363
/**
364-
* @brief Start scanning for peripherals without filter
364+
* @brief Start scanning for peripherals with the option of accepting all detectable
365+
* Peripherals or just the newly detected.
365366
*
366-
* @param none
367-
*
368-
* @return none
369-
*
370-
* @note none
371-
*/
372-
void scan();
373-
374-
/**
375-
* @brief Start scanning for peripherals with filter
376-
*
377-
* @param[in] withDuplicates true - with duplicate filter
378-
* false- without duplicate filter
379-
*
380-
* @return none
381-
*
382-
* @note option to filter out duplicate addresses for Arduino.
383-
* The current only support fileter duplicate mode.
384-
*/
385-
void scan(bool withDuplicates);
386-
387-
/**
388-
* @brief Start scanning for peripherals and filter by device name in ADV
389-
*
390-
* @param name The device's local name.
367+
* @param[in] withDuplicates true - return all detectable Peripherals.
368+
* false- return only new Peripherals.
391369
*
392370
* @return none
393371
*
394-
* @note option to filter out duplicate addresses for Arduino.
395-
* The current only support fileter duplicate mode.
372+
* @note When, withDuplicates = true (default), accept all detectable Peripherals.
373+
* No Peripheral filtering process applied to the scan result.
396374
*/
397-
void scanForName(String name);
375+
void scan(bool withDuplicates = true);
398376

399377
/**
400-
* @brief Start scanning for peripherals and filter by device name in ADV
378+
* @brief Start scanning for peripherals and filter by device name in ADV and
379+
* the option of accepting all detectable Peripherals or just the
380+
* newly detected.
401381
*
402382
* @param[in] name The device's local name.
403383
*
404-
* @param[in] withDuplicates true - with duplicate filter
405-
* false- without duplicate filter
384+
* @param[in] withDuplicates true - return all detectable Peripherals.
385+
* false- return only new Peripherals.
406386
*
407387
* @return none
408388
*
409-
* @note option to filter out duplicate addresses for Arduino.
410-
* The current only support fileter duplicate mode.
389+
* @note When, withDuplicates = true (default), accept all detectable Peripherals.
390+
* No Peripheral filtering process applied to the scan result.
411391
*/
412-
void scanForName(String name, bool withDuplicates);
392+
void scanForName(String name, bool withDuplicates = true);
413393

414394
/**
415-
* @brief Start scanning for peripherals and filter by service in ADV
395+
* @brief Start scanning for peripherals and filter by service in ADV and
396+
* the option of accepting all detectable Peripherals or just the
397+
* newly detected.
416398
*
417-
* @param service The service
399+
* @param[in] service The service
400+
*
401+
* @param[in] withDuplicates true - return all detectable Peripherals.
402+
* false- return only new Peripherals.
418403
*
419404
* @return none
420405
*
421-
* @note none
406+
* @note When, withDuplicates = true (default), accept all detectable Peripherals.
407+
* No Peripheral filtering process applied to the scan result.
422408
*/
423-
void scanForUuid(String uuid);
409+
void scanForUuid(String uuid, bool withDuplicates = true);
424410

425411
/**
426-
* @brief Start scanning for peripherals and filter by service in ADV
412+
* @brief Start scanning for peripherals and filter by MAC address and
413+
* the option of accepting all detectable Peripherals or just the
414+
* newly detected.
427415
*
428-
* @param[in] service The service
416+
* @param[in] macaddr The Peripheral MAC address
429417
*
430-
* @param[in] withDuplicates true - with duplicate filter
431-
* false- without duplicate filter
418+
* @param[in] withDuplicates true - return all detectable Peripherals.
419+
* false- return only new Peripherals.
432420
*
433421
* @return none
434422
*
435-
* @note option to filter out duplicate addresses for Arduino.
436-
* The current only support fileter duplicate mode.
423+
* @note When, withDuplicates = true (default), accept all detectable Peripherals.
424+
* No Peripheral filtering process applied to the scan result.
437425
*/
438-
void scanForUuid(String uuid, bool withDuplicates);
439-
440426
void scanForAddress(String macaddr, bool withDuplicates = true);
427+
441428
/**
442429
* @brief Stop scanning for peripherals
443430
*
@@ -679,15 +666,15 @@ class BLEDevice
679666
void preCheckProfile();
680667

681668
/**
682-
* @brief Start scanning for peripherals with/without duplicate filter
669+
* @brief Start scanning for peripherals with the option of accepting all
670+
* detectable Peripherals or just the newly detected.
683671
*
684672
* @param[in] withDuplicates true - with duplicate filter
685673
* false- without duplicate filter
686674
*
687675
* @return none
688676
*
689-
* @note option to filter out duplicate addresses for Arduino.
690-
* The current only support fileter duplicate mode.
677+
* @note When, withDuplicates = true (default), accept all detectable Peripherals.
691678
*/
692679
bool startScan(bool withDuplicates);
693680

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ BLEDeviceManager::BLEDeviceManager():
101101

102102
memset(&_available_for_connect_peripheral_adv_data, 0, sizeof(_available_for_connect_peripheral_adv_data));
103103
memset(&_available_for_connect_peripheral_scan_rsp_data, 0, sizeof(_available_for_connect_peripheral_scan_rsp_data));
104-
105-
memset(&_wait_for_connect_peripheral, 0, sizeof(_wait_for_connect_peripheral));
106104

107105
memset(&_service_uuid, 0, sizeof(_service_uuid));
108106
memset(&_service_solicit_uuid, 0, sizeof(_service_solicit_uuid));
@@ -576,10 +574,32 @@ BLEDevice BLEDeviceManager::peripheral()
576574
return temp;
577575
}
578576

579-
bool BLEDeviceManager::startScanning()
577+
void BLEDeviceManager::_clearAdvertiseBuffer()
578+
{
579+
580+
// Clear the previous found ADV
581+
memset(_peer_temp_adv_buffer, 0, sizeof(_peer_temp_adv_buffer));
582+
memset(_peer_temp_adv_data, 0, sizeof(_peer_temp_adv_data));
583+
memset(_peer_temp_adv_data_len, 0, sizeof(_peer_temp_adv_data_len));
584+
memset(_peer_temp_adv_connectable, 0, sizeof(_peer_adv_connectable));
585+
586+
memset(_peer_adv_buffer, 0, sizeof(_peer_adv_buffer));
587+
memset(_peer_adv_mill, 0, sizeof(_peer_adv_mill));
588+
memset(_peer_adv_data, 0, sizeof(_peer_adv_data));
589+
memset(_peer_adv_data_len, 0, sizeof(_peer_adv_data_len));
590+
memset(_peer_scan_rsp_data, 0, sizeof(_peer_scan_rsp_data));
591+
memset(_peer_scan_rsp_data_len, 0, sizeof(_peer_scan_rsp_data_len));
592+
memset(_peer_adv_rssi, 0, sizeof(_peer_adv_rssi));
593+
594+
}
595+
596+
bool BLEDeviceManager::startScanningWithDuplicates()
580597
{
581598
_adv_duplicate_filter_enabled = false;
582599
_scan_param.filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_ENABLE;
600+
601+
_clearAdvertiseBuffer();
602+
583603
int err = bt_le_scan_start(&_scan_param, ble_central_device_found);
584604
if (err)
585605
{
@@ -589,11 +609,13 @@ bool BLEDeviceManager::startScanning()
589609
return true;
590610
}
591611

592-
bool BLEDeviceManager::startScanningWithDuplicates()
612+
bool BLEDeviceManager::startScanningNewPeripherals()
593613
{
594614
_adv_duplicate_filter_enabled = true;
595615
memset(_peer_duplicate_address_buffer, 0, sizeof(_peer_duplicate_address_buffer));
596616
_duplicate_filter_header = _duplicate_filter_tail = 0;
617+
618+
_clearAdvertiseBuffer();
597619

598620
_scan_param.filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_ENABLE;
599621
int err = bt_le_scan_start(&_scan_param, ble_central_device_found);
@@ -896,11 +918,6 @@ int BLEDeviceManager::advertisedServiceUuidCount(const BLEDevice* device) const
896918
return service_cnt;
897919
}
898920

899-
if ((len + 1) > adv_data_len) { // Sid. KW, can't be (adv_data_len < 2)
900-
pr_info(LOG_MODULE_BLE, "AD malformed\n");
901-
return service_cnt;
902-
}
903-
904921
/* Sid, 2/15/2017. Sandeep reported that Apple devices may use
905922
BT_DATA_UUID16_SOME and BT_DATA_UUID128_SOME in addition to ALL.
906923
Practically, these types are same as ALL. */
@@ -1067,15 +1084,20 @@ bool BLEDeviceManager::connect(BLEDevice &device)
10671084
uint64_t timestamp = millis();
10681085
uint64_t timestampcur = timestamp;
10691086
bool ret = true;
1087+
if (_available_for_connect_peripheral_connectable == false)
1088+
{
1089+
return false;
1090+
}
1091+
10701092
bt_addr_le_copy(&_wait_for_connect_peripheral, device.bt_le_address());
10711093
// Buffer the ADV data
10721094
memcpy(_wait_for_connect_peripheral_adv_data, _available_for_connect_peripheral_adv_data, BLE_MAX_ADV_SIZE);
10731095
memcpy(_wait_for_connect_peripheral_scan_rsp_data, _available_for_connect_peripheral_scan_rsp_data, BLE_MAX_ADV_SIZE);
10741096
_wait_for_connect_peripheral_adv_data_len = _available_for_connect_peripheral_adv_data_len;
10751097
_wait_for_connect_peripheral_scan_rsp_data_len = _available_for_connect_peripheral_scan_rsp_data_len;
10761098
_wait_for_connect_peripheral_adv_rssi = _available_for_connect_peripheral_adv_rssi;
1077-
1078-
startScanning();
1099+
1100+
startScanningWithDuplicates();
10791101

10801102
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
10811103
// Wait for the connection
@@ -1247,6 +1269,8 @@ void BLEDeviceManager::handleDisconnectEvent(bt_conn_t *conn, uint8_t reason)
12471269
memset(_peer_peripheral_adv_data[i], 0, BLE_MAX_ADV_SIZE);
12481270
_peer_peripheral_adv_data_len[i] = 0;
12491271
_peer_peripheral_adv_rssi[i] = 0;
1272+
memset(_peer_peripheral_scan_rsp_data[i], 0, BLE_MAX_ADV_SIZE);
1273+
_peer_peripheral_scan_rsp_data_len[i] = 0;
12501274
break;
12511275
}
12521276
}
@@ -1406,7 +1430,10 @@ bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
14061430
{
14071431
max_delta = timestamp_delta;
14081432
if (max_delta > 2000) // expired
1433+
{
14091434
index = i;
1435+
_peer_scan_rsp_data_len[index] = 0; // Invalid the scan response
1436+
}
14101437
}
14111438

14121439
if (bt_addr_le_cmp(temp, bt_addr) == 0)

libraries/CurieBLE/src/internal/BLEDeviceManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class BLEDeviceManager
310310
void setAdvertiseCritical(String name);
311311
void setAdvertiseCritical(BLEService& service);
312312
void setAdvertiseCritical(const char* macaddress);
313-
bool startScanning(); // start scanning for peripherals
313+
bool startScanningNewPeripherals(); // start scanning for new peripherals, don't report the detected ones
314314
bool startScanningWithDuplicates(); // start scanning for peripherals, and report all duplicates
315315
bool stopScanning(); // stop scanning for peripherals
316316

@@ -358,6 +358,7 @@ class BLEDeviceManager
358358
const uint8_t* data,
359359
uint8_t length);
360360
BLE_STATUS_T _advDataInit(void);
361+
void _clearAdvertiseBuffer();
361362
bool advertiseDataProc(uint8_t type,
362363
const uint8_t *dataPtr,
363364
uint8_t data_len);

0 commit comments

Comments
 (0)