move scan report parser to Scanner
This commit is contained in:
1 parent
c2991de3cd
commit
c84236b2f9
7 files changed
+90
-87
No files matched your search
@@ -58,7 +58,7 @@ void setup()
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Check if advertising contain BleUart service
|
||||
if ( Bluefruit.Central.checkUuidInScan(report, BLEUART_UUID_SERVICE) )
|
||||
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
|
||||
{
|
||||
Serial.println("BLE UART service detected");
|
||||
Serial.println("Attempt to connect ... ");
|
||||
|
||||
@@ -20,7 +20,7 @@ void setup()
|
||||
|
||||
Serial.println("Bluefruit52 Central Scan Example");
|
||||
|
||||
// up to 1 peripheral conn and 1 central conn
|
||||
// Enable both peripheral and central
|
||||
Bluefruit.begin(true, true);
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
|
||||
@@ -48,7 +48,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
Serial.println();
|
||||
|
||||
// Check if advertising contain BleUart service
|
||||
if ( Bluefruit.Central.checkUuidInScan(report, BLEUART_UUID_SERVICE) )
|
||||
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
|
||||
{
|
||||
Serial.println(" BLE UART service detected");
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ void setup()
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Check if advertising contain BleUart service
|
||||
if ( Bluefruit.Central.checkUuidInScan(report, BLEUART_UUID_SERVICE) )
|
||||
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
|
||||
{
|
||||
Serial.println("BLE UART service detected");
|
||||
Serial.println("Attempt to connect ... ");
|
||||
|
||||
@@ -50,82 +50,6 @@ void BLECentral::begin(void)
|
||||
Bluefruit.Discovery.begin();
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Scan and Parser
|
||||
*------------------------------------------------------------------*/
|
||||
uint8_t* BLECentral::extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len)
|
||||
{
|
||||
*result_len = 0;
|
||||
|
||||
// len (1+data), type, data
|
||||
while ( scanlen )
|
||||
{
|
||||
if ( scandata[1] == type )
|
||||
{
|
||||
*result_len = scandata[0]-1;
|
||||
return (uint8_t*) (scandata + 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
scanlen -= (scandata[0] + 1);
|
||||
scandata += (scandata[0] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t* BLECentral::extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len)
|
||||
{
|
||||
return extractScanData(report->data, report->dlen, type, result_len);
|
||||
}
|
||||
|
||||
bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid)
|
||||
{
|
||||
const uint8_t* uuid;
|
||||
uint8_t uuid_len = ble_uuid.size();
|
||||
|
||||
uint8_t type_arr[2];
|
||||
|
||||
// Check both UUID16 more available and complete list
|
||||
if ( uuid_len == 16)
|
||||
{
|
||||
type_arr[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
|
||||
type_arr[1] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
|
||||
|
||||
uuid = (uint8_t*) &ble_uuid._uuid.uuid;
|
||||
}else
|
||||
{
|
||||
type_arr[0] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE;
|
||||
type_arr[1] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
|
||||
|
||||
uuid = ble_uuid._uuid128;
|
||||
}
|
||||
|
||||
uuid_len /= 8; // convert uuid_len to number of bytes
|
||||
|
||||
for (int i=0; i<2; i++)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
uint8_t const* data = extractScanData(report, type_arr[i] , &len);
|
||||
|
||||
while( len )
|
||||
{
|
||||
// found matched
|
||||
if ( !memcmp(data, uuid, uuid_len) )
|
||||
{
|
||||
return true;
|
||||
}else
|
||||
{
|
||||
data += uuid_len;
|
||||
len -= uuid_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
@@ -58,13 +58,6 @@ class BLECentral
|
||||
BLECentral(void); // Constructor
|
||||
void begin(void);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Scan & Parser
|
||||
*------------------------------------------------------------------*/
|
||||
uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len);
|
||||
uint8_t* extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len);
|
||||
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
@@ -113,6 +113,86 @@ bool BLEScanner::stop(void)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Paser helper
|
||||
*------------------------------------------------------------------*/
|
||||
uint8_t* BLEScanner::parseReportByType(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len)
|
||||
{
|
||||
*result_len = 0;
|
||||
|
||||
// len (1+data), type, data
|
||||
while ( scanlen )
|
||||
{
|
||||
if ( scandata[1] == type )
|
||||
{
|
||||
*result_len = scandata[0]-1;
|
||||
return (uint8_t*) (scandata + 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
scanlen -= (scandata[0] + 1);
|
||||
scandata += (scandata[0] + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t* BLEScanner::parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len)
|
||||
{
|
||||
return parseReportByType(report->data, report->dlen, type, result_len);
|
||||
}
|
||||
|
||||
bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid)
|
||||
{
|
||||
const uint8_t* uuid;
|
||||
uint8_t uuid_len = ble_uuid.size();
|
||||
|
||||
uint8_t type_arr[2];
|
||||
|
||||
// Check both UUID16 more available and complete list
|
||||
if ( uuid_len == 16)
|
||||
{
|
||||
type_arr[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
|
||||
type_arr[1] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
|
||||
|
||||
uuid = (uint8_t*) &ble_uuid._uuid.uuid;
|
||||
}else
|
||||
{
|
||||
type_arr[0] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE;
|
||||
type_arr[1] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
|
||||
|
||||
uuid = ble_uuid._uuid128;
|
||||
}
|
||||
|
||||
uuid_len /= 8; // convert uuid_len to number of bytes
|
||||
|
||||
for (int i=0; i<2; i++)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
uint8_t const* data = parseReportByType(report, type_arr[i] , &len);
|
||||
|
||||
while( len )
|
||||
{
|
||||
// found matched
|
||||
if ( !memcmp(data, uuid, uuid_len) )
|
||||
{
|
||||
return true;
|
||||
}else
|
||||
{
|
||||
data += uuid_len;
|
||||
len -= uuid_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event Handler
|
||||
* @param evt
|
||||
*/
|
||||
void BLEScanner::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
switch ( evt->header.evt_id )
|
||||
|
||||
@@ -65,6 +65,12 @@ public:
|
||||
void setRxCallback(rx_callback_t fp);
|
||||
void setStopCallback(stop_callback_t fp);
|
||||
|
||||
/*------------- Data Parser -------------*/
|
||||
uint8_t* parseReportByType(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len);
|
||||
uint8_t* parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len);
|
||||
|
||||
bool checkReportForUuid(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
|
||||
Reference in new issue
Block a user