From d7e50b2a255b59d35d4e362b658c428d3756d61c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Jul 2017 17:27:27 +0700 Subject: [PATCH] change parseReportByType signature and behavior from return pointer to copy data --- changelog.md | 4 ++ libraries/Bluefruit52Lib/src/BLEScanner.cpp | 49 +++++++++++++++------ libraries/Bluefruit52Lib/src/BLEScanner.h | 6 +-- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/changelog.md b/changelog.md index d2ab036a..6380239b 100644 --- a/changelog.md +++ b/changelog.md @@ -20,6 +20,10 @@ - Added `getRole()` to distinguish between peripheral/central +### BLECentral + +- Move scanner & report parser into new BLEScanner class + ## 0.6.5 ### Core diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.cpp b/libraries/Bluefruit52Lib/src/BLEScanner.cpp index 0b969739..893bae15 100644 --- a/libraries/Bluefruit52Lib/src/BLEScanner.cpp +++ b/libraries/Bluefruit52Lib/src/BLEScanner.cpp @@ -116,17 +116,28 @@ bool BLEScanner::stop(void) /*------------------------------------------------------------------*/ /* Paser helper *------------------------------------------------------------------*/ -uint8_t* BLEScanner::parseReportByType(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len) + + /** + * @param scandata + * @param scanlen + * @param type + * @param buf + * @param bufsize If bufsize is skipped (zero), len check will be skipped + * @return + */ +uint8_t BLEScanner::parseReportByType(const uint8_t* scandata, uint8_t scanlen, uint8_t type, uint8_t* buf, uint8_t bufsize) { - *result_len = 0; + uint8_t len = 0; + uint8_t const* ptr = NULL; // len (1+data), type, data while ( scanlen ) { if ( scandata[1] == type ) { - *result_len = scandata[0]-1; - return (uint8_t*) (scandata + 2); + len = (scandata[0]-1); + ptr = (scandata + 2); + break; } else { @@ -135,12 +146,19 @@ uint8_t* BLEScanner::parseReportByType(uint8_t const* scandata, uint8_t scanlen, } } - return NULL; + // not found return 0 + if (ptr == NULL) return 0; + + // found but not enough data, skip copying + if ( (bufsize > 0) && (bufsize < len) ) return len; + + memcpy(buf, ptr, len); + return len; } -uint8_t* BLEScanner::parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len) +uint8_t BLEScanner::parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* buf, uint8_t bufsize) { - return parseReportByType(report->data, report->dlen, type, result_len); + return parseReportByType(report->data, report->dlen, type, buf, bufsize); } bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid) @@ -150,7 +168,7 @@ bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEU uint8_t type_arr[2]; - // Check both UUID16 more available and complete list + // Check both more available and complete list if ( uuid_len == 16) { type_arr[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE; @@ -165,23 +183,26 @@ bool BLEScanner::checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEU uuid = ble_uuid._uuid128; } - uuid_len /= 8; // convert uuid_len to number of bytes + uuid_len /= 8; // convert 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); + uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 }; + uint8_t len = parseReportByType(report, type_arr[i], buffer); + uint8_t* ptr = buffer; + + // look for uuid in the uuid list while( len ) { // found matched - if ( !memcmp(data, uuid, uuid_len) ) + if ( !memcmp(ptr, uuid, uuid_len) ) { return true; }else { - data += uuid_len; - len -= uuid_len; + ptr += uuid_len; + len -= uuid_len; } } } diff --git a/libraries/Bluefruit52Lib/src/BLEScanner.h b/libraries/Bluefruit52Lib/src/BLEScanner.h index 32fb8cd8..18dc07d8 100644 --- a/libraries/Bluefruit52Lib/src/BLEScanner.h +++ b/libraries/Bluefruit52Lib/src/BLEScanner.h @@ -66,10 +66,10 @@ public: 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); + uint8_t parseReportByType(const uint8_t* scandata, uint8_t scanlen, uint8_t type, uint8_t* buf, uint8_t bufsize = 0); + uint8_t parseReportByType(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* buf, uint8_t bufsize = 0); - bool checkReportForUuid(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid); + bool checkReportForUuid(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid); /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY