diff --git a/cores/nRF5/nrf52_flash.c b/cores/nRF5/nrf52_flash.c index e695d189..596c38d9 100644 --- a/cores/nRF5/nrf52_flash.c +++ b/cores/nRF5/nrf52_flash.c @@ -154,7 +154,7 @@ int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) memcpy(tempbuf+num_bytes, (uint8_t*) (address+num_bytes), 4-leftover); } - int err = write_and_wait(address, tempbuf, (n4 / 4) + (leftover ? 1 : 0) ); + int err = write_and_wait(address, (uint32_t*)tempbuf, (n4 / 4) + (leftover ? 1 : 0) ); rtos_free(tempbuf); diff --git a/libraries/Bluefruit52Lib/examples/custom_hrm/custom_hrm.ino b/libraries/Bluefruit52Lib/examples/custom_hrm/custom_hrm.ino index 30386485..1a8f22dc 100644 --- a/libraries/Bluefruit52Lib/examples/custom_hrm/custom_hrm.ino +++ b/libraries/Bluefruit52Lib/examples/custom_hrm/custom_hrm.ino @@ -84,20 +84,20 @@ void setup() // Start Advertising Serial.println("Ready Player One!!!"); Serial.println("\nAdvertising"); - Bluefruit.startAdvertising(); + Bluefruit.Advertising.start(); } void setupAdv(void) { - Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - Bluefruit.addAdvTxPower(); + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); // Include HRM Service UUID - Bluefruit.addAdvService(hrms); + Bluefruit.Advertising.addService(hrms); // There isn't enough room in the advertising packet for the // name so we'll place it on the secondary Scan Response packet - Bluefruit.addScanRespName(); + Bluefruit.ScanResponse.addName(); } void setupHRM(void) diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp new file mode 100644 index 00000000..b69b6017 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.cpp @@ -0,0 +1,195 @@ +/**************************************************************************/ +/*! + @file BLEAdvertising.cpp + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "bluefruit.h" + +#define GAP_ADV_INTERVAL_MS 20 +#define GAP_ADV_TIMEOUT_S 30 + +BLEAdvertising::BLEAdvertising(void) +{ + _count = 0; + varclr(_data); +} + +bool BLEAdvertising::start(uint8_t mode) +{ + (void) mode; // not used yet + + // Only allow to call start with Bluefruit.Advertising.start() + VERIFY( this == &Bluefruit.Advertising ); + + // Configure Data + VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse._data, Bluefruit.ScanResponse._count), false ); + + // ADV Params + ble_gap_adv_params_t adv_para = + { + .type = BLE_GAP_ADV_TYPE_ADV_IND, + .p_peer_addr = NULL , // Undirected advertisement + .fp = BLE_GAP_ADV_FP_ANY , + .p_whitelist = NULL , + .interval = MS1000TO625(GAP_ADV_INTERVAL_MS), // advertising interval (in units of 0.625 ms) + .timeout = GAP_ADV_TIMEOUT_S + }; + + VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para), false ); + + Bluefruit.startConnLed(); // start blinking + + return ERROR_NONE; +} + +bool BLEAdvertising::stop(void) +{ + // Only allow to call start with Bluefruit.Advertising.start() + VERIFY( this == &Bluefruit.Advertising ); + VERIFY_STATUS( sd_ble_gap_adv_stop(), false); + + Bluefruit.stopConnLed(); // stop blinking + + return true; +} + + +bool BLEAdvertising::addData(uint8_t type, const void* data, uint8_t len) +{ + VERIFY( _count + len + 2 <= BLE_GAP_ADV_MAX_SIZE ); + + uint8_t* adv_data = &_data[_count]; + + // len (1+data), type, data + *adv_data++ = (len+1); + *adv_data++ = type; + memcpy(adv_data, data, len); + + _count = _count + len + 2; + + return true; +} + +bool BLEAdvertising::addUuid(uint16_t uuid16) +{ + return addData(BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, &uuid16, 2); +} + +bool BLEAdvertising::addUuid(uint8_t const uuid128[]) +{ + return addData(BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, uuid128, 16); +} + +bool BLEAdvertising::addService(BLEService& service) +{ + // UUID128 + if ( service.uuid._uuid128 ) + { + return addUuid(service.uuid._uuid128); + }else + { + return addUuid(service.uuid._uuid.uuid); + } +} + +// Add Name to Adv packet, use setName() to set +bool BLEAdvertising::addName(void) +{ + const char* name = Bluefruit.getName(); + + uint8_t type = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME; + uint8_t len = strlen(name); + + // not enough for full name, chop it + if (_count + len + 2 > BLE_GAP_ADV_MAX_SIZE) + { + type = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME; + len = BLE_GAP_ADV_MAX_SIZE - (_count+2); + } + + return addData(type, name, len); +} + +// tx power is set by setTxPower +bool BLEAdvertising::addTxPower(void) +{ + int8_t tx_power = Bluefruit.getTxPower(); + return addData(BLE_GAP_AD_TYPE_TX_POWER_LEVEL, &tx_power, 1); +} + +bool BLEAdvertising::addFlags(uint8_t flags) +{ + return addData(BLE_GAP_AD_TYPE_FLAGS, &flags, 1); +} + +bool BLEAdvertising::addApperance(uint16_t appearance) +{ + return addData(BLE_GAP_AD_TYPE_APPEARANCE, &appearance, 2); +} + +bool BLEAdvertising::setBeacon(BLEBeacon& beacon) +{ + return beacon.start(*this); +} + +/*------------------------------------------------------------------*/ +/* CUSTOM API + *------------------------------------------------------------------*/ +uint8_t BLEAdvertising::count(void) +{ + return _count; +} + +uint8_t BLEAdvertising::getData(uint8_t* buffer) +{ + memcpy(buffer, _data, _count); + return _count; +} + +bool BLEAdvertising::setData(uint8_t const * data, uint8_t count) +{ + VERIFY( data && (count <= BLE_GAP_ADV_MAX_SIZE) ); + + memcpy(_data, data, count); + _count = count; + + return true; +} + +void BLEAdvertising::clearData(void) +{ + _count = 0; + varclr(_data); +} + diff --git a/libraries/Bluefruit52Lib/src/BLEAdvertising.h b/libraries/Bluefruit52Lib/src/BLEAdvertising.h new file mode 100644 index 00000000..0c5349a0 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEAdvertising.h @@ -0,0 +1,85 @@ +/**************************************************************************/ +/*! + @file BLEAdvertising.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef BLEADVERTISING_H_ +#define BLEADVERTISING_H_ + +#include +#include "bluefruit_common.h" + +#include "BLEUuid.h" +#include "BLECharacteristic.h" +#include "BLEService.h" + +#include "services/BLEBeacon.h" + +//enum +//{ +// BLE_ADV_MODE +//}; + +class BLEAdvertising +{ +private: + uint8_t _data[BLE_GAP_ADV_MAX_SIZE]; + uint8_t _count; + +public: + BLEAdvertising(void); + + bool start(uint8_t mode = 0); + bool stop (void); + + bool addData(uint8_t type, const void* data, uint8_t len); + bool addFlags(uint8_t flags); + bool addTxPower(void); + bool addName(void); + bool addApperance(uint16_t appearance); + + bool addUuid(uint16_t uuid16); + bool addUuid(uint8_t const uuid128[]); + bool addService(BLEService& service); + + bool setBeacon(BLEBeacon& beacon); + + // Custom API + uint8_t count(void); + uint8_t getData(uint8_t* buffer); + bool setData(const uint8_t* data, uint8_t count); + void clearData(void); + +}; + +#endif /* BLEADVERTISING_H_ */ diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index d2f62577..4ea81d43 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -69,13 +69,13 @@ err_t BLECentral::startScanning(uint16_t timeout) { _scan_param.timeout = timeout; VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param) ); - Bluefruit._startConnLed(); // start blinking + Bluefruit.startConnLed(); // start blinking return ERROR_NONE; } err_t BLECentral::stopScanning(void) { - Bluefruit._stopConnLed(); // stop blinking + Bluefruit.stopConnLed(); // stop blinking return sd_ble_gap_scan_stop(); } @@ -210,7 +210,7 @@ void BLECentral::_event_handler(ble_evt_t* evt) if (para->role == BLE_GAP_ROLE_CENTRAL) { - Bluefruit._stopConnLed(); + Bluefruit.stopConnLed(); if (Bluefruit._led_conn) ledOn(LED_CONN); _conn_hdl = evt->evt.gap_evt.conn_handle; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 13e2d97f..42fe1e76 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -47,15 +47,12 @@ #define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS) -#define GAP_ADV_INTERVAL_MS 20 -#define GAP_ADV_TIMEOUT_S 30 - #define CFG_BLE_TX_POWER_LEVEL 0 #define CFG_DEFAULT_NAME "Bluefruit52" #define CFG_ADV_BLINKY_INTERVAL 500 #define CFG_BLE_TASK_STACKSIZE (512*3) -#define CFG_SOC_TASK_STACKSIZE ((configMINIMAL_STACK_SIZE*3) / 2 ) +#define CFG_SOC_TASK_STACKSIZE ((configMINIMAL_STACK_SIZE*10) / 2 ) #define CFG_BOND_NFFS_DIR "/adafruit/bond" @@ -94,9 +91,6 @@ AdafruitBluefruit::AdafruitBluefruit(void) _led_blink_th = NULL; _led_conn = true; - varclr(&_adv); - varclr(&_scan_resp); - _tx_power = 0; strcpy(_name, CFG_DEFAULT_NAME); @@ -202,10 +196,10 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) // Create Timer for led advertising blinky _led_blink_th = xTimerCreate(NULL, ms2tick(CFG_ADV_BLINKY_INTERVAL), true, NULL, bluefruit_blinky_cb); -#if 0 // Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time) Nffs.begin(); +#if 0 Nffs.mkdir_p(CFG_BOND_NFFS_DIR); PRINT_INT(Nffs.errnum); @@ -244,6 +238,35 @@ void AdafruitBluefruit::setName(const char* str) strncpy(_name, str, 32); } +char* AdafruitBluefruit::getName(void) +{ + return _name; +} + +bool AdafruitBluefruit::setTxPower(int8_t power) +{ + // Check if TX Power is valid value + const int8_t accepted[] = { -40, -30, -20, -16, -12, -8, -4, 0, 4 }; + + uint32_t i; + for (i=0; i BLE_GAP_ADV_MAX_SIZE) - { - type = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME; - len = BLE_GAP_ADV_MAX_SIZE - (_adv.count+2); - } - - return addAdvData(type, _name, len); -} - -// tx power is set by setTxPower -bool AdafruitBluefruit::addAdvTxPower(void) -{ - return addAdvData(BLE_GAP_AD_TYPE_TX_POWER_LEVEL, &_tx_power, 1); -} - -bool AdafruitBluefruit::addAdvFlags(uint8_t flags) -{ - return addAdvData(BLE_GAP_AD_TYPE_FLAGS, &flags, 1); -} - -bool AdafruitBluefruit::addAdvApperance(uint16_t appearance) -{ - return addAdvData(BLE_GAP_AD_TYPE_APPEARANCE, &appearance, 2); -} - -bool AdafruitBluefruit::setAdvBeacon(BLEBeacon& beacon) -{ - return beacon.start(); -} - -uint8_t AdafruitBluefruit::getAdvLen(void) -{ - return _adv.count; -} - -uint8_t AdafruitBluefruit::getAdvData(uint8_t* buffer) -{ - memcpy(buffer, _adv.data, _adv.count); - return _adv.count; -} - -bool AdafruitBluefruit::setAdvData(uint8_t const * data, uint8_t count) -{ - VERIFY( data && (count <= BLE_GAP_ADV_MAX_SIZE) ); - - memcpy(_adv.data, data, count); - _adv.count = count; - - return true; -} - -void AdafruitBluefruit::clearAdvData(void) -{ - varclr(&_adv); -} - -/*------------------------------------------------------------------*/ -/* Scan Response - *------------------------------------------------------------------*/ -bool AdafruitBluefruit::addScanRespData(uint8_t type, const void* data, uint8_t len) -{ - return _addToAdv(true, type, data, len); -} - -bool AdafruitBluefruit::addScanRespUuid(uint16_t uuid16) -{ - return addScanRespData(BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE, &uuid16, 2); -} - -bool AdafruitBluefruit::addScanRespUuid(uint8_t const uuid128[]) -{ - return addScanRespData(BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE, uuid128, 16); -} - -bool AdafruitBluefruit::addScanRespName(void) -{ - uint8_t type = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME; - uint8_t len = strlen(_name); - - // not enough for full name, chop it - if (_scan_resp.count + len + 2 > BLE_GAP_ADV_MAX_SIZE) - { - type = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME; - len = BLE_GAP_ADV_MAX_SIZE - (_scan_resp.count+2); - } - - return addScanRespData(type, _name, len); -} - -uint8_t AdafruitBluefruit::getScanRespLen(void) -{ - return _scan_resp.count; -} - -uint8_t AdafruitBluefruit::getScanRespData(uint8_t* buffer) -{ - memcpy(buffer, _scan_resp.data, _scan_resp.count); - return _scan_resp.count; -} - -bool AdafruitBluefruit::setScanRespData(uint8_t const* data, uint8_t count) -{ - VERIFY (data && count <= BLE_GAP_ADV_MAX_SIZE); - - memcpy(_scan_resp.data, data, count); - _scan_resp.count = count; - - return true; -} - -void AdafruitBluefruit::clearScanData(void) -{ - varclr(&_scan_resp); -} - -err_t AdafruitBluefruit::startAdvertising(void) -{ - VERIFY_STATUS( sd_ble_gap_adv_data_set(_adv.data, _adv.count, _scan_resp.data, _scan_resp.count) ); - - // ADV Params - ble_gap_adv_params_t adv_para = - { - .type = BLE_GAP_ADV_TYPE_ADV_IND, - .p_peer_addr = NULL , // Undirected advertisement - .fp = BLE_GAP_ADV_FP_ANY , - .p_whitelist = NULL , - .interval = MS1000TO625(GAP_ADV_INTERVAL_MS), // advertising interval (in units of 0.625 ms) - .timeout = GAP_ADV_TIMEOUT_S - }; - - VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para) ); - - _startConnLed(); // start blinking - - return ERROR_NONE; -} - -void AdafruitBluefruit::stopAdvertising(void) -{ - sd_ble_gap_adv_stop(); - - _stopConnLed(); // stop blinking -} - err_t AdafruitBluefruit::_registerCharacteristic(BLECharacteristic* chars) { if ( _chars_count == BLE_MAX_CHARS ) return NRF_ERROR_NO_MEM; @@ -551,12 +376,12 @@ err_t AdafruitBluefruit::_saveBondedCCCD(void) /*------------------------------------------------------------------*/ /* Private Methods *------------------------------------------------------------------*/ -void AdafruitBluefruit::_startConnLed(void) +void AdafruitBluefruit::startConnLed(void) { if (_led_conn) xTimerStart(_led_blink_th, 0); } -void AdafruitBluefruit::_stopConnLed(void) +void AdafruitBluefruit::stopConnLed(void) { xTimerStop(_led_blink_th, 0); } @@ -567,8 +392,8 @@ void AdafruitBluefruit::_stopConnLed(void) void SD_EVT_IRQHandler(void) { // Notify both BLE & SOC Task - xSemaphoreGiveFromISR(Bluefruit._ble_event_sem, NULL); xSemaphoreGiveFromISR(Bluefruit._soc_event_sem, NULL); + xSemaphoreGiveFromISR(Bluefruit._ble_event_sem, NULL); } /** @@ -596,6 +421,7 @@ void adafruit_soc_task(void* arg) { case NRF_EVT_FLASH_OPERATION_SUCCESS: case NRF_EVT_FLASH_OPERATION_ERROR: + PRINT_INT(soc_evt); if (hal_flash_event_cb) hal_flash_event_cb(soc_evt); break; @@ -654,7 +480,7 @@ void AdafruitBluefruit::_poll(void) if (para->role == BLE_GAP_ROLE_PERIPH) { - _stopConnLed(); + stopConnLed(); if (_led_conn) ledOn(LED_CONN); _conn_hdl = evt->evt.gap_evt.conn_handle; @@ -708,7 +534,7 @@ void AdafruitBluefruit::_poll(void) if ( _discconnect_cb ) _discconnect_cb(evt->evt.gap_evt.params.disconnected.reason); - startAdvertising(); + Advertising.start(); } break; @@ -716,7 +542,7 @@ void AdafruitBluefruit::_poll(void) if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING) { // Restart Advertising - startAdvertising(); + Advertising.start(); } break; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index db7d4f5b..e8eb67cd 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -38,6 +38,7 @@ #include "bluefruit_common.h" #include "BLEUuid.h" +#include "BLEAdvertising.h" #include "BLECharacteristic.h" #include "BLEService.h" #include "BLECentral.h" @@ -67,45 +68,22 @@ class AdafruitBluefruit err_t begin(bool prph_enable = true, bool central_enable = false); - void autoConnLed(bool enabled); - void setConnLedInterval(uint32_t ms); - void setName(const char* str); -// bool setTxPower(int8_t power); + void autoConnLed(bool enabled); + void setConnLedInterval(uint32_t ms); + void startConnLed(void); + void stopConnLed(void); + + void setName(const char* str); + char* getName(void); + + bool setTxPower(int8_t power); + int8_t getTxPower(void); /*------------------------------------------------------------------*/ /* Advertising & Scan Response (active scan) *------------------------------------------------------------------*/ - uint8_t getAdvLen(void); - uint8_t getAdvData(uint8_t* buffer); - bool setAdvData(const uint8_t* data, uint8_t count); - void clearAdvData(void); - - bool addAdvData(uint8_t type, const void* data, uint8_t len); - - bool addAdvFlags(uint8_t flags); - bool addAdvTxPower(void); - bool addAdvName(void); - bool addAdvApperance(uint16_t appearance); - - bool addAdvUuid(uint16_t uuid16); - bool addAdvUuid(uint8_t const uuid128[]); - bool addAdvService(BLEService& service); - - bool setAdvBeacon(BLEBeacon& beacon); - - // Scan Response Data (less helper than Adv packet) - uint8_t getScanRespLen(void); - uint8_t getScanRespData(uint8_t* buffer); - bool setScanRespData(const uint8_t* data, uint8_t count); - void clearScanData(void); - - bool addScanRespData(uint8_t type, const void* data, uint8_t len); - bool addScanRespName(void); - bool addScanRespUuid(uint16_t uuid16); - bool addScanRespUuid(uint8_t const uuid128[]); - - err_t startAdvertising(void); - void stopAdvertising(void); + BLEAdvertising Advertising; + BLEAdvertising ScanResponse; /*------------------------------------------------------------------*/ /* @@ -120,6 +98,7 @@ class AdafruitBluefruit bool connBonded(void); uint16_t connInterval(void); + ble_gap_addr_t peerAddr(void); bool txbuf_get(uint32_t ms); @@ -144,9 +123,20 @@ class AdafruitBluefruit err_t _registerCharacteristic(BLECharacteristic* chars); private: + /*------------- BLE para -------------*/ bool _prph_enabled; bool _central_enabled; + // Peripheral Preferred Connection Parameters (PPCP) + uint16_t _ppcp_min_conn; + uint16_t _ppcp_max_conn; + + // Actual connection interval in use + uint16_t _conn_interval; + + int8_t _tx_power; + char _name[32+1]; + SemaphoreHandle_t _ble_event_sem; SemaphoreHandle_t _soc_event_sem; @@ -155,29 +145,12 @@ class AdafruitBluefruit BLEDfu _dfu_svc; - // ADV Data - struct { - uint8_t data[BLE_GAP_ADV_MAX_SIZE]; - uint8_t count; - } _adv, _scan_resp; - - int8_t _tx_power; - - char _name[32+1]; - BLECharacteristic* _chars_list[BLE_MAX_CHARS]; uint8_t _chars_count; uint16_t _conn_hdl; bool _bonded; - // Peripheral Preferred Connection Parameters (PPCP) - uint16_t _ppcp_min_conn; - uint16_t _ppcp_max_conn; - - // Actual connection interval in use - uint16_t _conn_interval; - COMMENT_OUT( uint8_t _auth_type; char _pin[BLE_GAP_PASSKEY_LEN]; @@ -209,9 +182,6 @@ private: bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len); - void _startConnLed(void); - void _stopConnLed(void); - err_t _saveBondedCCCD(void); void _poll(void); diff --git a/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp b/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp index 883cf148..adf19f67 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEBeacon.cpp @@ -88,7 +88,12 @@ void BLEBeacon::setRssiAt1m(int8_t rssi) bool BLEBeacon::start(void) { - Bluefruit.clearAdvData(); + return start(Bluefruit.Advertising); +} + +bool BLEBeacon::start(BLEAdvertising& adv) +{ + adv.clearData(); struct ATTR_PACKED { @@ -116,6 +121,6 @@ bool BLEBeacon::start(void) memcpy(beacon_data.uuid128, _uuid128, 16); - Bluefruit.addAdvFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); - return Bluefruit.addAdvData(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, &beacon_data, sizeof(beacon_data)); + adv.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + return adv.addData(BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA, &beacon_data, sizeof(beacon_data)); } diff --git a/libraries/Bluefruit52Lib/src/services/BLEBeacon.h b/libraries/Bluefruit52Lib/src/services/BLEBeacon.h index 4d1eb2a2..136f14f3 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEBeacon.h +++ b/libraries/Bluefruit52Lib/src/services/BLEBeacon.h @@ -41,6 +41,8 @@ #include "BLECharacteristic.h" #include "BLEService.h" +class BLEAdvertising; // foward declare + class BLEBeacon { private: @@ -63,6 +65,7 @@ class BLEBeacon void setRssiAt1m(int8_t rssi); bool start(void); + bool start(BLEAdvertising& adv); }; diff --git a/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp b/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp index e00ac8be..a0d93b01 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEDfu.cpp @@ -143,7 +143,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt // Initiate DFU Sequence and reboot into DFU OTA mode Bluefruit.disconnect(); - Bluefruit.stopAdvertising(); + Bluefruit.Advertising.stop(); // Set GPReset to DFU OTA enum { DFU_OTA_MAGIC = 0xB1 };