Skip to content

Commit d2e010d

Browse files
sgbihuSidLeung
authored andcommitted
Jira 836, Enumerate non connectable Peripherals in Central scan
Added feature: - In Central mode, the BLE library should enumerate Peripheral that are non-connectable (eg beacon). Code Mods: 1. BLEDevice.cpp: - Add the call to get Manufacturer Data info from a Peripheral. Non-connectable device may only broadcast this info. 2. BLEDevice.h: - Prototyping. 3. BLEDeviceManager.cpp: - Delete the filter about non-connectable advertisement. - Added APIs, hasManufacturerData(), setManufacturerData(), hasManufacturerData(), to gain access to adv info of non-connectable Peripherals.
1 parent 2b6a0f9 commit d2e010d

File tree

4 files changed

+137
-121
lines changed

4 files changed

+137
-121
lines changed

libraries/CurieBLE/src/BLEDevice.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ void BLEDevice::setManufacturerData(const unsigned char manufacturerData[],
133133
BLEDeviceManager::instance()->setManufacturerData(manufacturerData, manufacturerDataLength);
134134
}
135135

136+
bool BLEDevice::getManufacturerData (unsigned char* manu_data,
137+
unsigned char& manu_data_len) const
138+
{
139+
return BLEDeviceManager::instance()->getManufacturerData(this, manu_data, manu_data_len);
140+
}
141+
142+
bool BLEDevice::hasManufacturerData() const
143+
{
144+
return BLEDeviceManager::instance()->hasManufacturerData(this);
145+
}
146+
136147
void BLEDevice::setLocalName(const char *localName)
137148
{
138149
BLEDeviceManager::instance()->setLocalName(localName);

libraries/CurieBLE/src/BLEDevice.h

+5
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ class BLEDevice
191191
void setManufacturerData(const unsigned char manufacturerData[],
192192
unsigned char manufacturerDataLength);
193193

194+
bool getManufacturerData (unsigned char* manu_data,
195+
unsigned char& manu_data_len) const;
196+
197+
bool hasManufacturerData() const;
198+
194199
/**
195200
* Set the local name that the BLE Peripheral Device advertises
196201
*

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

+109-119
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ BLEDeviceManager::BLEDeviceManager():
4141
_available_for_connect_peripheral_adv_data_len(0),
4242
_available_for_connect_peripheral_scan_rsp_data_len(0),
4343
_available_for_connect_peripheral_adv_rssi(0),
44+
_available_for_connect_peripheral_connectable(false),
4445
_connecting(false),
4546
_has_service_uuid(false),
4647
_has_service_solicit_uuid(false),
@@ -83,9 +84,12 @@ BLEDeviceManager::BLEDeviceManager():
8384
memset(_peer_scan_rsp_data_len, 0, sizeof(_peer_scan_rsp_data_len));
8485
memset(_peer_adv_rssi, 0, sizeof(_peer_adv_rssi));
8586

87+
memset(_peer_adv_connectable, 0, sizeof(_peer_adv_connectable));
88+
8689
memset(_peer_temp_adv_buffer, 0, sizeof(_peer_temp_adv_buffer));
8790
memset(_peer_temp_adv_data, 0, sizeof(_peer_temp_adv_data));
8891
memset(_peer_temp_adv_data_len, 0, sizeof(_peer_temp_adv_data_len));
92+
memset(_peer_temp_adv_connectable, 0, sizeof(_peer_adv_connectable));
8993

9094
memset(&_adv_accept_critical, 0, sizeof(_adv_accept_critical));
9195
memset(&_adv_critical_service_uuid, 0, sizeof(_adv_critical_service_uuid));
@@ -724,36 +728,44 @@ bool BLEDeviceManager::hasLocalName(const BLEDevice* device) const
724728
BT_DATA_NAME_COMPLETE,
725729
local_name,
726730
local_name_len);
727-
/*
728-
getDeviceAdvertiseBuffer(device->bt_le_address(),
729-
adv_data,
730-
adv_data_len);
731-
if (NULL == adv_data)
731+
}
732+
733+
bool BLEDeviceManager::hasManufacturerData(const BLEDevice* device) const
734+
{
735+
if (BLEUtils::isLocalBLE(*device) == true)
732736
{
733-
return false;
737+
return (_manufacturer_data_length != 0);
734738
}
735739

736-
while (adv_data_len > 1)
737-
{
738-
uint8_t len = adv_data[0];
739-
uint8_t type = adv_data[1];
740-
741-
// Check for early termination
742-
if (len == 0 || ((len + 1) > adv_data_len))
743-
{
744-
return false;
745-
}
746-
747-
if (type == BT_DATA_NAME_COMPLETE)
748-
{
749-
return true;
750-
}
740+
const uint8_t* manufactgurer_data = NULL;
741+
uint8_t manufactgurer_data_len = 0;
742+
return getDataFromAdvertiseByType(device,
743+
BT_DATA_MANUFACTURER_DATA,
744+
manufactgurer_data,
745+
manufactgurer_data_len);
746+
}
751747

752-
adv_data_len -= len + 1;
753-
adv_data += len + 1;
748+
bool BLEDeviceManager::getManufacturerData (const BLEDevice* device,
749+
uint8_t* manu_data,
750+
uint8_t&manu_data_len) const
751+
{
752+
if (BLEUtils::isLocalBLE(*device) == true)
753+
{
754+
return (_manufacturer_data_length != 0);
754755
}
755-
return false;
756-
*/
756+
757+
const uint8_t* manufactgurer_data = NULL;
758+
uint8_t manufactgurer_data_len = 0;
759+
bool retval = getDataFromAdvertiseByType(device,
760+
BT_DATA_MANUFACTURER_DATA,
761+
manufactgurer_data,
762+
manufactgurer_data_len);
763+
if (retval)
764+
{
765+
memcpy (manu_data, manufactgurer_data, manufactgurer_data_len);
766+
manu_data_len = manufactgurer_data_len;
767+
}
768+
return retval;
757769
}
758770

759771
bool BLEDeviceManager::hasAdvertisedServiceUuid(const BLEDevice* device) const
@@ -903,50 +915,26 @@ String BLEDeviceManager::localName(const BLEDevice* device) const
903915
{
904916
return _local_name;
905917
}
906-
const uint8_t* adv_data = NULL;
907-
uint8_t adv_data_len = 0;
908-
char localname_string[BLE_MAX_ADV_SIZE];
909-
memset(localname_string, 0, sizeof(localname_string));
910918

911-
getDeviceAdvertiseBuffer(device->bt_le_address(),
912-
adv_data,
913-
adv_data_len);
914-
915-
if (NULL == adv_data) {
916-
String temp(localname_string);
917-
return temp;
918-
}
919-
920-
while (adv_data_len > 1)
921-
{
922-
uint8_t len = adv_data[0];
923-
uint8_t type = adv_data[1];
924-
925-
/* Check for early termination */
926-
if ((len == 0) || ((len + 1) > adv_data_len)) {
927-
break;
928-
}
929-
930-
/* Sid, 2/15/2017. Support both forms of data name.
931-
*/
932-
if (type == BT_DATA_NAME_COMPLETE ||
933-
type == BT_DATA_NAME_SHORTENED)
919+
const uint8_t* local_name = NULL;
920+
uint8_t local_name_len = 0;
921+
String temp("");
922+
char local_name_buff[BLE_MAX_ADV_SIZE];
923+
bool retval = getDataFromAdvertiseByType(device,
924+
BT_DATA_NAME_COMPLETE,
925+
local_name,
926+
local_name_len);
927+
if (true == retval)
928+
{
929+
if (local_name_len >= BLE_MAX_ADV_SIZE)
934930
{
935-
if (len >= BLE_MAX_ADV_SIZE)
936-
{
937-
len = BLE_MAX_ADV_SIZE-1;
938-
}
939-
uint8_t copy_len = len - 1;
940-
memcpy(localname_string, &adv_data[2], copy_len);
941-
localname_string[copy_len] = '\0';
942-
break;
931+
local_name_len = BLE_MAX_ADV_SIZE - 1;
943932
}
944-
945-
adv_data_len -= len + 1;
946-
adv_data += len + 1;
933+
memcpy(local_name_buff, local_name, local_name_len);
934+
local_name_buff[local_name_len] = '\0';
935+
temp = local_name_buff;
947936
}
948-
949-
String temp(localname_string);
937+
950938
return temp;
951939
}
952940

@@ -1368,8 +1356,8 @@ BLEDevice BLEDeviceManager::available()
13681356
_available_for_connect_peripheral_scan_rsp_data_len = _peer_scan_rsp_data_len[index];
13691357
_available_for_connect_peripheral_adv_data_len = _peer_adv_data_len[index];
13701358
_available_for_connect_peripheral_adv_rssi = _peer_adv_rssi[index];
1371-
1372-
pr_debug(LOG_MODULE_BLE, "%s-%d:Con addr-%s", __FUNCTION__, __LINE__, BLEUtils::macAddressBT2String(*temp).c_str());
1359+
_available_for_connect_peripheral_connectable = _peer_adv_connectable[index];
1360+
//pr_debug(LOG_MODULE_BLE, "%s-%d:Con addr-%s", __FUNCTION__, __LINE__, BLEUtils::macAddressBT2String(*temp).c_str());
13731361
_peer_adv_mill[index] -= 2000; // Set it as expired
13741362
if (_adv_duplicate_filter_enabled)
13751363
{
@@ -1383,7 +1371,8 @@ BLEDevice BLEDeviceManager::available()
13831371
bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
13841372
const uint8_t *ad,
13851373
uint8_t data_len,
1386-
int8_t rssi)
1374+
int8_t rssi,
1375+
bool connectable)
13871376
{
13881377
bt_addr_le_t* temp = NULL;
13891378
uint64_t timestamp = millis();
@@ -1429,6 +1418,7 @@ bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
14291418
_peer_adv_rssi[index] = rssi;
14301419
// Update the timestamp
14311420
_peer_adv_mill[index] = timestamp;
1421+
_peer_adv_connectable[index] = connectable;
14321422
retval = true;
14331423
}
14341424

@@ -1498,7 +1488,8 @@ uint8_t BLEDeviceManager::getTempAdvertiseIndexFromBuffer(const bt_addr_le_t* bt
14981488
void BLEDeviceManager::setTempAdvertiseBuffer(const bt_addr_le_t* bt_addr,
14991489
int8_t rssi,
15001490
const uint8_t *ad,
1501-
uint8_t data_len)
1491+
uint8_t data_len,
1492+
bool connectable)
15021493
{
15031494
bt_addr_le_t* temp = NULL;
15041495
uint8_t i = getTempAdvertiseIndexFromBuffer(bt_addr);
@@ -1517,6 +1508,7 @@ void BLEDeviceManager::setTempAdvertiseBuffer(const bt_addr_le_t* bt_addr,
15171508

15181509
memcpy(_peer_temp_adv_data[i], ad, data_len);
15191510
_peer_temp_adv_data_len[i] = data_len;
1511+
_peer_temp_adv_connectable[i] = connectable;
15201512

15211513
return;
15221514
}
@@ -1541,12 +1533,14 @@ void BLEDeviceManager::advertiseAcceptHandler(const bt_addr_le_t *addr,
15411533
{
15421534
const uint8_t *adv_data = ad;
15431535
uint8_t adv_data_len = data_len;
1536+
bool connectable = (BT_LE_ADV_NONCONN_IND != type);
15441537
bool update_advertise_data = true;
15451538
// The critical is accepted
15461539
// Find the oldest and expired buffer
15471540
if (BT_LE_ADV_SCAN_RSP == type)
15481541
{
1549-
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1542+
update_advertise_data = false;
1543+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
15501544
if (false == setScanRespBuffer(addr, ad, data_len, rssi))
15511545
{
15521546
// Find the device in the ADV temp buffer
@@ -1555,22 +1549,20 @@ void BLEDeviceManager::advertiseAcceptHandler(const bt_addr_le_t *addr,
15551549
{
15561550
adv_data = _peer_temp_adv_data[tempIndex];
15571551
adv_data_len = _peer_temp_adv_data_len[tempIndex];
1558-
}
1559-
else
1560-
{
1561-
update_advertise_data = false;
1552+
connectable = _peer_temp_adv_connectable[tempIndex];
1553+
update_advertise_data = true;
15621554
}
15631555
}
1564-
else
1565-
{
1566-
update_advertise_data = false;
1567-
}
15681556
}
15691557
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
15701558

15711559
if (true == update_advertise_data)
15721560
{
1573-
if (false == setAdvertiseBuffer(addr, adv_data, adv_data_len, rssi))
1561+
if (false == setAdvertiseBuffer(addr,
1562+
adv_data,
1563+
adv_data_len,
1564+
rssi,
1565+
connectable))
15741566
{
15751567
pr_info(LOG_MODULE_BLE, "No buffer to store the ADV\n");
15761568
}
@@ -1592,54 +1584,52 @@ void BLEDeviceManager::handleDeviceFound(const bt_addr_le_t *addr,
15921584
uint8_t real_adv_len = data_len;
15931585

15941586
/* We're only interested in connectable events */
1595-
if (type == BT_LE_ADV_IND || type == BT_LE_ADV_DIRECT_IND ||
1596-
type == BT_LE_ADV_SCAN_RSP)
1587+
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1588+
// Filter address
1589+
if (BLEUtils::macAddressValid(_adv_accept_device) == true &&
1590+
(memcmp(addr->val, _adv_accept_device.val, sizeof (addr->val)) != 0))
15971591
{
1598-
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1599-
// Filter address
1600-
if (BLEUtils::macAddressValid(_adv_accept_device) == true &&
1601-
(memcmp(addr->val, _adv_accept_device.val, sizeof (addr->val)) != 0))
1592+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1593+
return;
1594+
}
1595+
1596+
while (data_len > 1)
1597+
{
1598+
uint8_t len = data[0];
1599+
1600+
/* Check for early termination */
1601+
if (len == 0)
16021602
{
16031603
return;
16041604
}
1605-
1606-
while (data_len > 1)
1607-
{
1608-
uint8_t len = data[0];
16091605

1610-
/* Check for early termination */
1611-
if (len == 0)
1612-
{
1613-
return;
1614-
}
1615-
1616-
if ((len + 1) > data_len) { // Sid. KW, cannot be (data_len < 2)
1617-
pr_info(LOG_MODULE_BLE, "AD malformed\n");
1618-
return;
1619-
}
1620-
1621-
if (true == advertiseDataProc(data[1], &data[2], len - 1))
1622-
{
1623-
advertiseAcceptHandler(addr, rssi, type, ad, real_adv_len);
1624-
pr_debug(LOG_MODULE_BLE, "%s-%d: Done", __FUNCTION__, __LINE__);
1625-
return;
1626-
}
1627-
1628-
data_len -= len + 1;
1629-
data += len + 1;
1630-
}
1631-
//pr_debug(LOG_MODULE_BLE, "%s: done", __FUNCTION__);
1632-
// Doesn't accept the ADV/scan data
1633-
// Check it in the buffer
1634-
if (BT_LE_ADV_SCAN_RSP == type)
1635-
{
1636-
setScanRespBuffer(addr, ad, real_adv_len, rssi);
1606+
if ((len + 1) > data_len) { // Sid. KW, cannot be (data_len < 2)
1607+
pr_info(LOG_MODULE_BLE, "AD malformed\n");
1608+
return;
16371609
}
1638-
else
1610+
1611+
if (true == advertiseDataProc(data[1], &data[2], len - 1))
16391612
{
1640-
// Add advertise into buffer
1641-
setTempAdvertiseBuffer(addr, rssi, ad, real_adv_len);
1613+
advertiseAcceptHandler(addr, rssi, type, ad, real_adv_len);
1614+
//pr_debug(LOG_MODULE_BLE, "%s-%d: Done", __FUNCTION__, __LINE__);
1615+
return;
16421616
}
1617+
1618+
data_len -= len + 1;
1619+
data += len + 1;
1620+
}
1621+
//pr_debug(LOG_MODULE_BLE, "%s: done", __FUNCTION__);
1622+
// Doesn't accept the ADV/scan data
1623+
// Check it in the buffer
1624+
if (BT_LE_ADV_SCAN_RSP == type)
1625+
{
1626+
// Find the ADV and set response
1627+
setScanRespBuffer(addr, ad, real_adv_len, rssi);
1628+
}
1629+
else
1630+
{
1631+
// Add advertise into buffer
1632+
setTempAdvertiseBuffer(addr, rssi, ad, real_adv_len, BT_LE_ADV_NONCONN_IND != type);
16431633
}
16441634

16451635
}

0 commit comments

Comments
 (0)