Skip to content

Commit 1e0f86b

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 aae69c3 commit 1e0f86b

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),
@@ -80,9 +81,12 @@ BLEDeviceManager::BLEDeviceManager():
8081
memset(_peer_scan_rsp_data_len, 0, sizeof(_peer_scan_rsp_data_len));
8182
memset(_peer_adv_rssi, 0, sizeof(_peer_adv_rssi));
8283

84+
memset(_peer_adv_connectable, 0, sizeof(_peer_adv_connectable));
85+
8386
memset(_peer_temp_adv_buffer, 0, sizeof(_peer_temp_adv_buffer));
8487
memset(_peer_temp_adv_data, 0, sizeof(_peer_temp_adv_data));
8588
memset(_peer_temp_adv_data_len, 0, sizeof(_peer_temp_adv_data_len));
89+
memset(_peer_temp_adv_connectable, 0, sizeof(_peer_adv_connectable));
8690

8791
memset(&_adv_accept_critical, 0, sizeof(_adv_accept_critical));
8892
memset(&_adv_critical_service_uuid, 0, sizeof(_adv_critical_service_uuid));
@@ -716,36 +720,44 @@ bool BLEDeviceManager::hasLocalName(const BLEDevice* device) const
716720
BT_DATA_NAME_COMPLETE,
717721
local_name,
718722
local_name_len);
719-
/*
720-
getDeviceAdvertiseBuffer(device->bt_le_address(),
721-
adv_data,
722-
adv_data_len);
723-
if (NULL == adv_data)
723+
}
724+
725+
bool BLEDeviceManager::hasManufacturerData(const BLEDevice* device) const
726+
{
727+
if (BLEUtils::isLocalBLE(*device) == true)
724728
{
725-
return false;
729+
return (_manufacturer_data_length != 0);
726730
}
727731

728-
while (adv_data_len > 1)
729-
{
730-
uint8_t len = adv_data[0];
731-
uint8_t type = adv_data[1];
732-
733-
// Check for early termination
734-
if (len == 0 || ((len + 1) > adv_data_len))
735-
{
736-
return false;
737-
}
738-
739-
if (type == BT_DATA_NAME_COMPLETE)
740-
{
741-
return true;
742-
}
732+
const uint8_t* manufactgurer_data = NULL;
733+
uint8_t manufactgurer_data_len = 0;
734+
return getDataFromAdvertiseByType(device,
735+
BT_DATA_MANUFACTURER_DATA,
736+
manufactgurer_data,
737+
manufactgurer_data_len);
738+
}
743739

744-
adv_data_len -= len + 1;
745-
adv_data += len + 1;
740+
bool BLEDeviceManager::getManufacturerData (const BLEDevice* device,
741+
uint8_t* manu_data,
742+
uint8_t&manu_data_len) const
743+
{
744+
if (BLEUtils::isLocalBLE(*device) == true)
745+
{
746+
return (_manufacturer_data_length != 0);
746747
}
747-
return false;
748-
*/
748+
749+
const uint8_t* manufactgurer_data = NULL;
750+
uint8_t manufactgurer_data_len = 0;
751+
bool retval = getDataFromAdvertiseByType(device,
752+
BT_DATA_MANUFACTURER_DATA,
753+
manufactgurer_data,
754+
manufactgurer_data_len);
755+
if (retval)
756+
{
757+
memcpy (manu_data, manufactgurer_data, manufactgurer_data_len);
758+
manu_data_len = manufactgurer_data_len;
759+
}
760+
return retval;
749761
}
750762

751763
bool BLEDeviceManager::hasAdvertisedServiceUuid(const BLEDevice* device) const
@@ -895,50 +907,26 @@ String BLEDeviceManager::localName(const BLEDevice* device) const
895907
{
896908
return _local_name;
897909
}
898-
const uint8_t* adv_data = NULL;
899-
uint8_t adv_data_len = 0;
900-
char localname_string[BLE_MAX_ADV_SIZE];
901-
memset(localname_string, 0, sizeof(localname_string));
902910

903-
getDeviceAdvertiseBuffer(device->bt_le_address(),
904-
adv_data,
905-
adv_data_len);
906-
907-
if (NULL == adv_data) {
908-
String temp(localname_string);
909-
return temp;
910-
}
911-
912-
while (adv_data_len > 1)
913-
{
914-
uint8_t len = adv_data[0];
915-
uint8_t type = adv_data[1];
916-
917-
/* Check for early termination */
918-
if ((len == 0) || ((len + 1) > adv_data_len)) {
919-
break;
920-
}
921-
922-
/* Sid, 2/15/2017. Support both forms of data name.
923-
*/
924-
if (type == BT_DATA_NAME_COMPLETE ||
925-
type == BT_DATA_NAME_SHORTENED)
911+
const uint8_t* local_name = NULL;
912+
uint8_t local_name_len = 0;
913+
String temp("");
914+
char local_name_buff[BLE_MAX_ADV_SIZE];
915+
bool retval = getDataFromAdvertiseByType(device,
916+
BT_DATA_NAME_COMPLETE,
917+
local_name,
918+
local_name_len);
919+
if (true == retval)
920+
{
921+
if (local_name_len >= BLE_MAX_ADV_SIZE)
926922
{
927-
if (len >= BLE_MAX_ADV_SIZE)
928-
{
929-
len = BLE_MAX_ADV_SIZE-1;
930-
}
931-
uint8_t copy_len = len - 1;
932-
memcpy(localname_string, &adv_data[2], copy_len);
933-
localname_string[copy_len] = '\0';
934-
break;
923+
local_name_len = BLE_MAX_ADV_SIZE - 1;
935924
}
936-
937-
adv_data_len -= len + 1;
938-
adv_data += len + 1;
925+
memcpy(local_name_buff, local_name, local_name_len);
926+
local_name_buff[local_name_len] = '\0';
927+
temp = local_name_buff;
939928
}
940-
941-
String temp(localname_string);
929+
942930
return temp;
943931
}
944932

@@ -1321,8 +1309,8 @@ BLEDevice BLEDeviceManager::available()
13211309
_available_for_connect_peripheral_scan_rsp_data_len = _peer_scan_rsp_data_len[index];
13221310
_available_for_connect_peripheral_adv_data_len = _peer_adv_data_len[index];
13231311
_available_for_connect_peripheral_adv_rssi = _peer_adv_rssi[index];
1324-
1325-
pr_debug(LOG_MODULE_BLE, "%s-%d:Con addr-%s", __FUNCTION__, __LINE__, BLEUtils::macAddressBT2String(*temp).c_str());
1312+
_available_for_connect_peripheral_connectable = _peer_adv_connectable[index];
1313+
//pr_debug(LOG_MODULE_BLE, "%s-%d:Con addr-%s", __FUNCTION__, __LINE__, BLEUtils::macAddressBT2String(*temp).c_str());
13261314
_peer_adv_mill[index] -= 2000; // Set it as expired
13271315
}
13281316
}
@@ -1332,7 +1320,8 @@ BLEDevice BLEDeviceManager::available()
13321320
bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
13331321
const uint8_t *ad,
13341322
uint8_t data_len,
1335-
int8_t rssi)
1323+
int8_t rssi,
1324+
bool connectable)
13361325
{
13371326
bt_addr_le_t* temp = NULL;
13381327
uint64_t timestamp = millis();
@@ -1378,6 +1367,7 @@ bool BLEDeviceManager::setAdvertiseBuffer(const bt_addr_le_t* bt_addr,
13781367
_peer_adv_rssi[index] = rssi;
13791368
// Update the timestamp
13801369
_peer_adv_mill[index] = timestamp;
1370+
_peer_adv_connectable[index] = connectable;
13811371
retval = true;
13821372
}
13831373

@@ -1447,7 +1437,8 @@ uint8_t BLEDeviceManager::getTempAdvertiseIndexFromBuffer(const bt_addr_le_t* bt
14471437
void BLEDeviceManager::setTempAdvertiseBuffer(const bt_addr_le_t* bt_addr,
14481438
int8_t rssi,
14491439
const uint8_t *ad,
1450-
uint8_t data_len)
1440+
uint8_t data_len,
1441+
bool connectable)
14511442
{
14521443
bt_addr_le_t* temp = NULL;
14531444
uint8_t i = getTempAdvertiseIndexFromBuffer(bt_addr);
@@ -1466,6 +1457,7 @@ void BLEDeviceManager::setTempAdvertiseBuffer(const bt_addr_le_t* bt_addr,
14661457

14671458
memcpy(_peer_temp_adv_data[i], ad, data_len);
14681459
_peer_temp_adv_data_len[i] = data_len;
1460+
_peer_temp_adv_connectable[i] = connectable;
14691461

14701462
return;
14711463
}
@@ -1490,12 +1482,14 @@ void BLEDeviceManager::advertiseAcceptHandler(const bt_addr_le_t *addr,
14901482
{
14911483
const uint8_t *adv_data = ad;
14921484
uint8_t adv_data_len = data_len;
1485+
bool connectable = (BT_LE_ADV_NONCONN_IND != type);
14931486
bool update_advertise_data = true;
14941487
// The critical is accepted
14951488
// Find the oldest and expired buffer
14961489
if (BT_LE_ADV_SCAN_RSP == type)
14971490
{
1498-
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1491+
update_advertise_data = false;
1492+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
14991493
if (false == setScanRespBuffer(addr, ad, data_len, rssi))
15001494
{
15011495
// Find the device in the ADV temp buffer
@@ -1504,22 +1498,20 @@ void BLEDeviceManager::advertiseAcceptHandler(const bt_addr_le_t *addr,
15041498
{
15051499
adv_data = _peer_temp_adv_data[tempIndex];
15061500
adv_data_len = _peer_temp_adv_data_len[tempIndex];
1507-
}
1508-
else
1509-
{
1510-
update_advertise_data = false;
1501+
connectable = _peer_temp_adv_connectable[tempIndex];
1502+
update_advertise_data = true;
15111503
}
15121504
}
1513-
else
1514-
{
1515-
update_advertise_data = false;
1516-
}
15171505
}
15181506
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
15191507

15201508
if (true == update_advertise_data)
15211509
{
1522-
if (false == setAdvertiseBuffer(addr, adv_data, adv_data_len, rssi))
1510+
if (false == setAdvertiseBuffer(addr,
1511+
adv_data,
1512+
adv_data_len,
1513+
rssi,
1514+
connectable))
15231515
{
15241516
pr_info(LOG_MODULE_BLE, "No buffer to store the ADV\n");
15251517
}
@@ -1541,54 +1533,52 @@ void BLEDeviceManager::handleDeviceFound(const bt_addr_le_t *addr,
15411533
uint8_t real_adv_len = data_len;
15421534

15431535
/* We're only interested in connectable events */
1544-
if (type == BT_LE_ADV_IND || type == BT_LE_ADV_DIRECT_IND ||
1545-
type == BT_LE_ADV_SCAN_RSP)
1536+
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1537+
// Filter address
1538+
if (BLEUtils::macAddressValid(_adv_accept_device) == true &&
1539+
(memcmp(addr->val, _adv_accept_device.val, sizeof (addr->val)) != 0))
15461540
{
1547-
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1548-
// Filter address
1549-
if (BLEUtils::macAddressValid(_adv_accept_device) == true &&
1550-
(memcmp(addr->val, _adv_accept_device.val, sizeof (addr->val)) != 0))
1541+
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1542+
return;
1543+
}
1544+
1545+
while (data_len > 1)
1546+
{
1547+
uint8_t len = data[0];
1548+
1549+
/* Check for early termination */
1550+
if (len == 0)
15511551
{
15521552
return;
15531553
}
1554-
1555-
while (data_len > 1)
1556-
{
1557-
uint8_t len = data[0];
15581554

1559-
/* Check for early termination */
1560-
if (len == 0)
1561-
{
1562-
return;
1563-
}
1564-
1565-
if ((len + 1) > data_len) { // Sid. KW, cannot be (data_len < 2)
1566-
pr_info(LOG_MODULE_BLE, "AD malformed\n");
1567-
return;
1568-
}
1569-
1570-
if (true == advertiseDataProc(data[1], &data[2], len - 1))
1571-
{
1572-
advertiseAcceptHandler(addr, rssi, type, ad, real_adv_len);
1573-
pr_debug(LOG_MODULE_BLE, "%s-%d: Done", __FUNCTION__, __LINE__);
1574-
return;
1575-
}
1576-
1577-
data_len -= len + 1;
1578-
data += len + 1;
1579-
}
1580-
//pr_debug(LOG_MODULE_BLE, "%s: done", __FUNCTION__);
1581-
// Doesn't accept the ADV/scan data
1582-
// Check it in the buffer
1583-
if (BT_LE_ADV_SCAN_RSP == type)
1584-
{
1585-
setScanRespBuffer(addr, ad, real_adv_len, rssi);
1555+
if ((len + 1) > data_len) { // Sid. KW, cannot be (data_len < 2)
1556+
pr_info(LOG_MODULE_BLE, "AD malformed\n");
1557+
return;
15861558
}
1587-
else
1559+
1560+
if (true == advertiseDataProc(data[1], &data[2], len - 1))
15881561
{
1589-
// Add advertise into buffer
1590-
setTempAdvertiseBuffer(addr, rssi, ad, real_adv_len);
1562+
advertiseAcceptHandler(addr, rssi, type, ad, real_adv_len);
1563+
//pr_debug(LOG_MODULE_BLE, "%s-%d: Done", __FUNCTION__, __LINE__);
1564+
return;
15911565
}
1566+
1567+
data_len -= len + 1;
1568+
data += len + 1;
1569+
}
1570+
//pr_debug(LOG_MODULE_BLE, "%s: done", __FUNCTION__);
1571+
// Doesn't accept the ADV/scan data
1572+
// Check it in the buffer
1573+
if (BT_LE_ADV_SCAN_RSP == type)
1574+
{
1575+
// Find the ADV and set response
1576+
setScanRespBuffer(addr, ad, real_adv_len, rssi);
1577+
}
1578+
else
1579+
{
1580+
// Add advertise into buffer
1581+
setTempAdvertiseBuffer(addr, rssi, ad, real_adv_len, BT_LE_ADV_NONCONN_IND != type);
15921582
}
15931583

15941584
}

0 commit comments

Comments
 (0)