diff --git a/changelog.md b/changelog.md index ff9ab2e3..ec3d59b0 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,8 @@ - Fixed Servo detach issue - Fixed pulseIn() compile issue: implement countPulseASM() using C instead of ASM +- Introduce BLEPeriph + - setConnInterval(), setConnIntervalMS(), setConnSupervisionTimeout(), setConnSupervisionTimeoutMS() - Enhance Gap, add BLEConnection class to mange peer connection - Remove Gap.connected(), Gap.getRole() in prefer to BLEConnection API - Added setRssiCallback(), monitorRssi(), getRssi(), stopRssi() for tracking rssi of a connection diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino b/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino index 5cfa9433..e86f953d 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino @@ -797,7 +797,7 @@ void setup() // try to go as fast as possible, could be rejected by some central, increase it if needed // iOS won't negotitate and will mostly use 30ms - Bluefruit.setConnInterval(9, 24); // min = 9*1.25=11.25 ms, max = 23*1.25=30ms + Bluefruit.Periph.setConnInterval(9, 24); // min = 9*1.25=11.25 ms, max = 23*1.25=30ms // Configure and Start BLE Uart Service // Firmata use several small write(1) --> buffering TXD is required to run smoothly diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino index 1b056b4d..4f48d74f 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_camerashutter/hid_camerashutter.ino @@ -66,7 +66,7 @@ void setup() * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms */ - /* Bluefruit.setConnInterval(9, 12); */ + /* Bluefruit.Periph.setConnInterval(9, 12); */ // Set up and start advertising startAdv(); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino index 9b446934..75a4db7b 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyboard/hid_keyboard.ino @@ -60,7 +60,7 @@ void setup() * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms */ - /* Bluefruit.setConnInterval(9, 12); */ + /* Bluefruit.Periph.setConnInterval(9, 12); */ // Set up and start advertising startAdv(); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino index 719b876e..3f152d76 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_keyscan/hid_keyscan.ino @@ -81,7 +81,7 @@ void setup() * Note: It is already set by BLEHidAdafruit::begin() to 11.25ms - 15ms * min = 9*1.25=11.25 ms, max = 12*1.25= 15 ms */ - /* Bluefruit.setConnInterval(9, 12); */ + /* Bluefruit.Periph.setConnInterval(9, 12); */ // Set up and start advertising startAdv(); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino b/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino index 68dbf483..b0c1272b 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/hid_mouse/hid_mouse.ino @@ -36,7 +36,7 @@ void setup() Bluefruit.begin(); // HID Device can have a min connection interval of 9*1.25 = 11.25 ms - Bluefruit.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms + Bluefruit.Periph.setConnInterval(9, 16); // min = 9*1.25=11.25 ms, max = 16*1.25=20ms // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 Bluefruit.setTxPower(4); Bluefruit.setName("Bluefruit52"); diff --git a/libraries/Bluefruit52Lib/src/BLEPeriph.cpp b/libraries/Bluefruit52Lib/src/BLEPeriph.cpp new file mode 100644 index 00000000..1e8dbb57 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEPeriph.cpp @@ -0,0 +1,138 @@ +/**************************************************************************/ +/*! + @file BLEPeriph.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2019, 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" + +// Constructor +BLEPeriph::BLEPeriph(void) +{ + + _ppcp = ((ble_gap_conn_params_t) { + .min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, + .max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT, + .slave_latency = 0, + .conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit + }); + +} + +bool BLEPeriph::begin(void) +{ + // Peripheral Preferred Connection Parameters + VERIFY_STATUS(sd_ble_gap_ppcp_set(&_ppcp), false); + + VERIFY_STATUS( sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN), false ); + + return true; +} + +void BLEPeriph::printInfo(void) +{ + char const * title_fmt = "%-16s: "; + + Serial.printf(title_fmt, "Conn Intervals"); + Serial.printf("min = %.2f ms, ", _ppcp.min_conn_interval*1.25f); + Serial.printf("max = %.2f ms", _ppcp.max_conn_interval*1.25f); + Serial.println(); + + Serial.printf(title_fmt, "Conn Timeout"); + Serial.printf("%.2f ms", _ppcp.conn_sup_timeout*10.0f); + Serial.println(); +} + +bool BLEPeriph::setConnInterval (uint16_t min, uint16_t max) +{ + _ppcp.min_conn_interval = min; + _ppcp.max_conn_interval = max; + + VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false); + + return true; +} + +bool BLEPeriph::setConnIntervalMS (uint16_t min_ms, uint16_t max_ms) +{ + return setConnInterval( MS100TO125(min_ms), MS100TO125(max_ms) ); +} + + +bool BLEPeriph::setConnSupervisionTimeout(uint16_t timeout) +{ + _ppcp.conn_sup_timeout = timeout; + + VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false); + + return true; +} + +bool BLEPeriph::setConnSupervisionTimeoutMS(uint16_t timeout_ms) +{ + return setConnSupervisionTimeout(timeout_ms / 10); // 10ms unit +} + +void BLEPeriph::_eventHandler(ble_evt_t* evt) +{ + uint16_t const conn_hdl = evt->evt.common_evt.conn_handle; + + switch ( evt->header.evt_id ) + { + case BLE_GAP_EVT_CONNECTED: + { + ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected; + + if (para->role == BLE_GAP_ROLE_PERIPH) + { + // Connection interval set by Central is out of preferred range + // Try to negotiate with Central using our preferred values + if ( !is_within(_ppcp.min_conn_interval, para->conn_params.min_conn_interval, _ppcp.max_conn_interval) ) + { + VERIFY_STATUS( sd_ble_gap_conn_param_update(conn_hdl, &_ppcp), ); + } + } + } + break; + + case BLE_GAP_EVT_DISCONNECTED: + + break; + + case BLE_GATTS_EVT_SYS_ATTR_MISSING: + sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0); + break; + + default: break; + } +} diff --git a/libraries/Bluefruit52Lib/src/BLEPeriph.h b/libraries/Bluefruit52Lib/src/BLEPeriph.h new file mode 100644 index 00000000..7f9c8de7 --- /dev/null +++ b/libraries/Bluefruit52Lib/src/BLEPeriph.h @@ -0,0 +1,68 @@ +/**************************************************************************/ +/*! + @file BLEPeriph.h + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2019, 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 BLEPERIPH_H_ +#define BLEPERIPH_H_ + +#include +#include "bluefruit_common.h" + +class BLEPeriph +{ + public: + BLEPeriph(void); + + bool begin(void); + + bool setConnInterval (uint16_t min, uint16_t max); + bool setConnIntervalMS (uint16_t min_ms, uint16_t max_ms); + bool setConnSupervisionTimeout(uint16_t timeout); + bool setConnSupervisionTimeoutMS(uint16_t timeout_ms); + + + void printInfo(void); + + /*------------------------------------------------------------------*/ + /* INTERNAL USAGE ONLY + * Although declare as public, it is meant to be invoked by internal code. + *------------------------------------------------------------------*/ + void _eventHandler(ble_evt_t* evt); + + private: + ble_gap_conn_params_t _ppcp; +}; + +#endif /* BLEPERIPH_H_ */ diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index de955582..e1076af7 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -130,11 +130,6 @@ AdafruitBluefruit::AdafruitBluefruit(void) _conn_hdl = BLE_CONN_HANDLE_INVALID; - _ppcp.min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT; - _ppcp.max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT; - _ppcp.slave_latency = 0; - _ppcp.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit - _conn_interval = 0; _event_cb = NULL; @@ -412,15 +407,12 @@ bool AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count) /*------------- Configure GAP -------------*/ - // Peripheral Preferred Connection Parameters - VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false ); + VERIFY( Periph.begin() ); // Default device name ble_gap_conn_sec_mode_t sec_mode = BLE_SECMODE_OPEN; VERIFY_STATUS(sd_ble_gap_device_name_set(&sec_mode, (uint8_t const *) CFG_DEFAULT_NAME, strlen(CFG_DEFAULT_NAME)), false); - VERIFY_STATUS( sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN), false ); - //------------- USB -------------// #if NRF52840_XXAA sd_power_usbdetected_enable(true); @@ -565,35 +557,6 @@ bool AdafruitBluefruit::disconnect(void) return true; // not connected still return true } -bool AdafruitBluefruit::setConnInterval(uint16_t min, uint16_t max) -{ - _ppcp.min_conn_interval = min; - _ppcp.max_conn_interval = max; - - VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false); - - return true; -} - -bool AdafruitBluefruit::setConnIntervalMS(uint16_t min_ms, uint16_t max_ms) -{ - return setConnInterval( MS100TO125(min_ms), MS100TO125(max_ms) ); -} - -bool AdafruitBluefruit::setConnSupervisionTimeout(uint16_t timeout) -{ - _ppcp.conn_sup_timeout = timeout; - - VERIFY_STATUS( sd_ble_gap_ppcp_set(&_ppcp), false); - - return true; -} - -bool AdafruitBluefruit::setConnSupervisionTimeoutMS(uint16_t timeout_ms) -{ - return setConnSupervisionTimeout(timeout_ms / 10); // 10ms unit -} - void AdafruitBluefruit::setConnectCallback( BLEGap::connect_callback_t fp ) { Gap._prph_setConnectCallback(fp); @@ -763,15 +726,17 @@ void adafruit_ble_task(void* arg) void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) { // conn handle has fixed offset for all events - uint16_t const evt_conn_hdl = evt->evt.common_evt.conn_handle; + uint16_t const conn_hdl = evt->evt.common_evt.conn_handle; - LOG_LV1("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), evt_conn_hdl); + LOG_LV1("BLE", "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), conn_hdl); // GAP handler Gap._eventHandler(evt); Advertising._eventHandler(evt); Scanner._eventHandler(evt); + Periph._eventHandler(evt); + /*------------- BLE Peripheral Events -------------*/ /* Only handle Peripheral events with matched connection handle * or a few special one @@ -784,7 +749,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) * Reconnect to a paired device * - Connect -> SEC_INFO_REQUEST -> CONN_SEC_UPDATE */ - if ( evt_conn_hdl == _conn_hdl || + if ( conn_hdl == _conn_hdl || evt->header.evt_id == BLE_GAP_EVT_CONNECTED || evt->header.evt_id == BLE_GAP_EVT_TIMEOUT ) { @@ -798,14 +763,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) { _conn_hdl = evt->evt.gap_evt.conn_handle; _conn_interval = para->conn_params.min_conn_interval; - - // Connection interval set by Central is out of preferred range - // Try to negotiate with Central using our preferred values - if ( !is_within(_ppcp.min_conn_interval, para->conn_params.min_conn_interval, _ppcp.max_conn_interval) ) - { - // Null, value is set by sd_ble_gap_ppcp_set will be used - VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), ); - } } } break; @@ -835,8 +792,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) if (_central_count) { // Skip if not central connection - if (evt_conn_hdl != _conn_hdl || - evt_conn_hdl == BLE_CONN_HANDLE_INVALID) + if (conn_hdl != _conn_hdl || + conn_hdl == BLE_CONN_HANDLE_INVALID) { Central._eventHandler(evt); } @@ -998,15 +955,7 @@ void AdafruitBluefruit::printInfo(void) Serial.printf("%d dBm", _tx_power); Serial.println(); - // Connection Intervals - Serial.printf(title_fmt, "Conn Intervals"); - Serial.printf("min = %.2f ms, ", _ppcp.min_conn_interval*1.25f); - Serial.printf("max = %.2f ms", _ppcp.max_conn_interval*1.25f); - Serial.println(); - - Serial.printf(title_fmt, "Conn Timeout"); - Serial.printf("%.2f ms", _ppcp.conn_sup_timeout*10.0f); - Serial.println(); + Periph.printInfo(); /*------------- List the paried device -------------*/ if ( _prph_count ) diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index 54551f36..798af651 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -52,8 +52,8 @@ #include "BLEAdvertising.h" #include "BLECharacteristic.h" #include "BLEService.h" - #include "BLEScanner.h" +#include "BLEPeriph.h" #include "BLECentral.h" #include "BLEClientCharacteristic.h" #include "BLEClientService.h" @@ -111,6 +111,7 @@ class AdafruitBluefruit BLEAdvertising Advertising; BLEAdvertisingData ScanResponse; BLEScanner Scanner; + BLEPeriph Periph; BLECentral Central; BLEDiscovery Discovery; @@ -152,11 +153,6 @@ class AdafruitBluefruit bool connected (uint16_t conn_hdl); // connected to a specific central bool disconnect (void); - bool setConnInterval (uint16_t min, uint16_t max); - bool setConnIntervalMS (uint16_t min_ms, uint16_t max_ms); - bool setConnSupervisionTimeout(uint16_t timeout); - bool setConnSupervisionTimeoutMS(uint16_t timeout_ms); - uint16_t connHandle (void); bool connPaired (void); uint16_t connInterval (void); @@ -199,9 +195,6 @@ class AdafruitBluefruit uint8_t _prph_count; uint8_t _central_count; - // Peripheral Preferred Connection Parameters (PPCP) - ble_gap_conn_params_t _ppcp; - // Actual connection interval in use uint16_t _conn_interval; diff --git a/libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp b/libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp index 45a04048..290f5bd2 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEHidAdafruit.cpp @@ -213,7 +213,7 @@ err_t BLEHidAdafruit::begin(void) VERIFY_STATUS( BLEHidGeneric::begin() ); // Attempt to change the connection interval to 11.25-15 ms when starting HID - Bluefruit.setConnInterval(9, 12); + Bluefruit.Periph.setConnInterval(9, 12); return ERROR_NONE; } diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index 586f5eeb..29a876ce 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -153,7 +153,7 @@ err_t BLEMidi::begin(void) VERIFY_STATUS( _io.begin() ); // Attempt to change the connection interval to 11.25-15 ms when starting HID - Bluefruit.setConnInterval(9, 12); + Bluefruit.Periph.setConnInterval(9, 12); return ERROR_NONE; }