From 90db83ef32b83edf883ca72600ee9a601506edba Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 11 Jul 2017 13:56:50 +0700 Subject: [PATCH] Add conn handle to Bluefruit's connect and disconnect prototype move connect and disconnect into BLEGap --- changelog.md | 11 +++- libraries/Bluefruit52Lib/src/BLECentral.cpp | 24 +++------ libraries/Bluefruit52Lib/src/BLECentral.h | 12 ++--- libraries/Bluefruit52Lib/src/BLEGap.cpp | 53 ++++++++++++++++++- libraries/Bluefruit52Lib/src/BLEGap.h | 25 ++++++--- libraries/Bluefruit52Lib/src/bluefruit.cpp | 18 +++---- libraries/Bluefruit52Lib/src/bluefruit.h | 14 ++--- .../src/utility/AdaCallback.cpp | 16 +++--- .../Bluefruit52Lib/src/utility/AdaCallback.h | 11 ++-- 9 files changed, 115 insertions(+), 69 deletions(-) diff --git a/changelog.md b/changelog.md index a59882bd..21c6fcce 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,13 @@ # Adafruit nRF52 Arduino Core Changelog +## 0.7.0 + +### BLE + +- Change Bluefruit.peerAddr() to Bluefruit.getPeerAddr() +- Change BLEClientUart's rx callback +- Add connection handle to Bluefruit's connect and disconnect callback prototype + ## 0.6.5 ### Core @@ -18,8 +26,7 @@ - Updated for latest Bluefruit LE Connect version with RGBW support - Implement Gatt readCharByUuid() - Implement Gap getPeerName() -- Change Bluefruit.peerAddr() to Bluefruit.getPeerAddr() -- change rx callback of BLEClientUart + ### New Examples diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index fcb9ca37..d89c7b4e 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -51,10 +51,6 @@ BLECentral::BLECentral(void) .window = 0x0050, .timeout = 0, // no timeout }; - - - _connect_cb = NULL; - _disconnect_cb = NULL; } void BLECentral::begin(void) @@ -210,14 +206,14 @@ bool BLECentral::connected(void) return false; } -void BLECentral::setConnectCallback( connect_callback_t fp) +void BLECentral::setConnectCallback( BLEGap::connect_callback_t fp) { - _connect_cb = fp; + Bluefruit.Gap.setConnectCallback(fp, true); } -void BLECentral::setDisconnectCallback( disconnect_callback_t fp) +void BLECentral::setDisconnectCallback( BLEGap::disconnect_callback_t fp) { - _disconnect_cb = fp; + Bluefruit.Gap.setDisconnectCallback(fp, true); } @@ -244,29 +240,21 @@ void BLECentral::_event_handler(ble_evt_t* evt) break; case BLE_GAP_EVT_CONNECTED: - { + { // Note callback is invoked by BLEGap ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected; if (para->role == BLE_GAP_ROLE_CENTRAL) { Bluefruit.stopConnLed(); if (Bluefruit._led_conn) ledOn(LED_BLUE); - - if ( _connect_cb ) - { - ada_callback(NULL, _connect_cb, evt->evt.gap_evt.conn_handle); - } } } break; case BLE_GAP_EVT_DISCONNECTED: + // Note callback is invoked by BLEGap if (Bluefruit._led_conn) ledOff(LED_BLUE); - _conn_hdl = BLE_CONN_HANDLE_INVALID; - - if ( _disconnect_cb ) _disconnect_cb(evt_conn_hdl, evt->evt.gap_evt.params.disconnected.reason); - startScanning(); break; diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index 3c85a76b..51ee621f 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -39,6 +39,9 @@ #include #include "bluefruit_common.h" +#include "BLEGap.h" +#include "BLEGatt.h" + #include "BLEUuid.h" #include "BLECharacteristic.h" #include "BLEClientCharacteristic.h" @@ -53,8 +56,6 @@ class BLECentral { public: typedef void (*scan_callback_t ) (ble_gap_evt_adv_report_t*); - typedef void (*connect_callback_t ) (uint16_t conn_handle); - typedef void (*disconnect_callback_t ) (uint16_t conn_handle, uint8_t reason); BLECentral(void); // Constructor void begin(void); @@ -89,8 +90,8 @@ class BLECentral /*------------------------------------------------------------------*/ /* CALLBACKS *------------------------------------------------------------------*/ - void setConnectCallback ( connect_callback_t fp); - void setDisconnectCallback( disconnect_callback_t fp); + void setConnectCallback ( BLEGap::connect_callback_t fp); + void setDisconnectCallback( BLEGap::disconnect_callback_t fp); private: uint16_t _conn_hdl; @@ -98,9 +99,6 @@ class BLECentral ble_gap_scan_params_t _scan_param; scan_callback_t _scan_cb; - connect_callback_t _connect_cb; - disconnect_callback_t _disconnect_cb; - bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len); void _event_handler(ble_evt_t* evt); diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index be079bcd..a42a4059 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -35,9 +35,12 @@ /**************************************************************************/ #include "bluefruit.h" +#include "utility/AdaCallback.h" BLEGap::BLEGap(void) { + varclr(&_prph_cb); + varclr(&_central_cb); varclr(&_peers); } @@ -46,6 +49,28 @@ bool BLEGap::connected(uint16_t conn_handle) return _peers[conn_handle].connected; } +void BLEGap::setConnectCallback(connect_callback_t fp, bool isCentral) +{ + if (isCentral) + { + _central_cb.connect_cb = fp; + }else + { + _prph_cb.connect_cb = fp; + } +} + +void BLEGap::setDisconnectCallback(disconnect_callback_t fp, bool isCentral) +{ + if (isCentral) + { + _central_cb.disconnect_cb = fp; + }else + { + _prph_cb.disconnect_cb = fp; + } +} + uint8_t BLEGap::getPeerAddr(uint16_t conn_handle, uint8_t addr[6]) { memcpy(addr, _peers[conn_handle].addr.addr, BLE_GAP_ADDR_LEN); @@ -93,20 +118,46 @@ void BLEGap::_eventHandler(ble_evt_t* evt) switch(evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: + { + ble_gap_evt_connected_t const * para = &evt->evt.gap_evt.params.connected; + peer->connected = true; - peer->addr = evt->evt.gap_evt.params.connected.peer_addr; + peer->role = para->role; + peer->addr = para->peer_addr; // Init transmission buffer for notification uint8_t txbuf_max; (void) sd_ble_tx_packet_count_get(conn_handle, &txbuf_max); peer->txpacket_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max); + + // Invoke callback according to the role + if ( peer->role == BLE_GAP_ROLE_PERIPH) + { + if (_prph_cb.connect_cb) ada_callback(NULL, _prph_cb.connect_cb, conn_handle); + }else + { + if (_central_cb.connect_cb) ada_callback(NULL, _central_cb.connect_cb, conn_handle); + } + } break; case BLE_GAP_EVT_DISCONNECTED: + { + ble_gap_evt_disconnected_t const* para = &evt->evt.gap_evt.params.disconnected; + peer->connected = false; vSemaphoreDelete( peer->txpacket_sem ); peer->txpacket_sem = NULL; + + if ( peer->role == BLE_GAP_ROLE_PERIPH) + { + if (_prph_cb.disconnect_cb) ada_callback(NULL, _prph_cb.disconnect_cb, conn_handle, para->reason); + }else + { + if (_central_cb.disconnect_cb) ada_callback(NULL, _central_cb.disconnect_cb, conn_handle, para->reason); + } + } break; case BLE_EVT_TX_COMPLETE: diff --git a/libraries/Bluefruit52Lib/src/BLEGap.h b/libraries/Bluefruit52Lib/src/BLEGap.h index 82f50347..27754382 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.h +++ b/libraries/Bluefruit52Lib/src/BLEGap.h @@ -43,18 +43,24 @@ class BLEGap { public: + typedef void (*connect_callback_t ) (uint16_t conn_handle); + typedef void (*disconnect_callback_t ) (uint16_t conn_handle, uint8_t reason); + BLEGap(void); bool connected(uint16_t conn_handle); - uint8_t getPeerAddr(uint16_t conn_handle, uint8_t addr[6]); + uint8_t getPeerAddr(uint16_t conn_handle, uint8_t addr[6]); ble_gap_addr_t getPeerAddr(uint16_t conn_handle); - uint16_t getPeerName(char* buf, uint16_t bufsize); - uint16_t getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize); + uint16_t getPeerName(char* buf, uint16_t bufsize); + uint16_t getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize); - bool getTxPacket(void); - bool getTxPacket(uint16_t conn_handle); + bool getTxPacket(void); + bool getTxPacket(uint16_t conn_handle); + + void setConnectCallback (connect_callback_t fp, bool isCentral); + void setDisconnectCallback(disconnect_callback_t fp, bool isCentral); /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY @@ -64,10 +70,17 @@ class BLEGap void _eventHandler(ble_evt_t* evt); private: + struct { + connect_callback_t connect_cb; + disconnect_callback_t disconnect_cb; + }_prph_cb, _central_cb; + // Array of TX Packet semaphore, indexed by connection handle // Peer info where conn_handle serves as index typedef struct { - bool connected; + bool connected; + uint8_t role; + ble_gap_addr_t addr; SemaphoreHandle_t txpacket_sem; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index dbe40dc0..bfc52155 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -120,9 +120,6 @@ COMMENT_OUT( .kdist_own = { .enc = 1, .id = 1}, .kdist_peer = { .enc = 1, .id = 1}, }; - - _connect_cb = NULL; - _disconnect_cb = NULL; } err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable) @@ -314,14 +311,14 @@ void AdafruitBluefruit::disconnect(void) } } -void AdafruitBluefruit::setConnectCallback ( connect_callback_t fp ) +void AdafruitBluefruit::setConnectCallback( BLEGap::connect_callback_t fp ) { - _connect_cb = fp; + Gap.setConnectCallback(fp, false); } -void AdafruitBluefruit::setDisconnectCallback( disconnect_callback_t fp ) +void AdafruitBluefruit::setDisconnectCallback( BLEGap::disconnect_callback_t fp ) { - _disconnect_cb = fp; + Gap.setDisconnectCallback(fp, false); } uint16_t AdafruitBluefruit::connHandle(void) @@ -485,7 +482,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) switch ( evt->header.evt_id ) { case BLE_GAP_EVT_CONNECTED: - { + { // Note callback is invoked by BLEGap ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected; if (para->role == BLE_GAP_ROLE_PERIPH) @@ -503,8 +500,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) // Null, value is set by sd_ble_gap_ppcp_set will be used VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), ); } - - if ( _connect_cb ) ada_callback(NULL, _connect_cb); } } break; @@ -519,6 +514,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) break; case BLE_GAP_EVT_DISCONNECTED: + // Note callback is invoked by BLEGap if (_led_conn) ledOff(LED_BLUE); // Save all configured cccd @@ -527,8 +523,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) _conn_hdl = BLE_CONN_HANDLE_INVALID; _bonded = false; - if ( _disconnect_cb ) ada_callback(NULL, _disconnect_cb, evt->evt.gap_evt.params.disconnected.reason); - Advertising.start(); break; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index b30f05de..93f39ba9 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -85,9 +85,6 @@ extern "C" class AdafruitBluefruit { public: - typedef void (*connect_callback_t ) (void); - typedef void (*disconnect_callback_t) (uint8_t reason); - // Constructor AdafruitBluefruit(void); @@ -138,8 +135,8 @@ class AdafruitBluefruit /*------------------------------------------------------------------*/ /* Callbacks *------------------------------------------------------------------*/ - void setConnectCallback ( connect_callback_t fp); - void setDisconnectCallback( disconnect_callback_t fp); + void setConnectCallback ( BLEGap::connect_callback_t fp); + void setDisconnectCallback( BLEGap::disconnect_callback_t fp); COMMENT_OUT ( bool setPIN(const char* pin); ) @@ -186,10 +183,9 @@ COMMENT_OUT( char _pin[BLE_GAP_PASSKEY_LEN]; ) - /*------------- Callbacks -------------*/ - connect_callback_t _connect_cb; - disconnect_callback_t _disconnect_cb; - + /*------------------------------------------------------------------*/ + /* INTERNAL USAGE ONLY + *------------------------------------------------------------------*/ bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len); bool _saveBondKeys(void); diff --git a/libraries/Bluefruit52Lib/src/utility/AdaCallback.cpp b/libraries/Bluefruit52Lib/src/utility/AdaCallback.cpp index abb8c251..02f4fd4e 100644 --- a/libraries/Bluefruit52Lib/src/utility/AdaCallback.cpp +++ b/libraries/Bluefruit52Lib/src/utility/AdaCallback.cpp @@ -56,19 +56,19 @@ void adafruit_callback_task(void* arg) switch(cb_data->callback_type) { - /*------------- Bluefruit -------------*/ - case AdafruitBluefruit_connect_callback_t: - ((AdafruitBluefruit::connect_callback_t) func) (); + + /*------------- BLEGap -------------*/ + case BLEGap_connect_callback_t: + ((BLEGap::connect_callback_t) func) ( (uint16_t) args[0] ); break; - case AdafruitBluefruit_disconnect_callback_t: - ((AdafruitBluefruit::disconnect_callback_t) func) ( (uint8_t) args[0] ); + case BLEGap_disconnect_callback_t: + ((BLEGap::disconnect_callback_t) func) ( (uint16_t) args[0], (uint8_t) args[1] ); break; + /*------------- Bluefruit -------------*/ + /*------------- Central -------------*/ - case BLECentral_connect_callback_t: - ((BLECentral::connect_callback_t) func)( (uint16_t) args[0]); - break; /*------------- Client Service & Chars -------------*/ case BLEClientCharacteristic_notify_cb_t: diff --git a/libraries/Bluefruit52Lib/src/utility/AdaCallback.h b/libraries/Bluefruit52Lib/src/utility/AdaCallback.h index d36e7df2..e31d19aa 100644 --- a/libraries/Bluefruit52Lib/src/utility/AdaCallback.h +++ b/libraries/Bluefruit52Lib/src/utility/AdaCallback.h @@ -61,15 +61,14 @@ static_assert(sizeof(ada_callback_t) == 16, "Incorrect Size"); /* X Macros expansion *------------------------------------------------------------------*/ #define ADA_CB_LOOKUP(XPAND) \ + /* BLEGap */ \ + XPAND(BLEGap , connect_callback_t ) \ + XPAND(BLEGap , disconnect_callback_t ) \ /* Bluefruit */ \ - XPAND(AdafruitBluefruit , connect_callback_t ) \ - XPAND(AdafruitBluefruit , disconnect_callback_t ) \ /* Central */ \ XPAND(BLECentral , scan_callback_t ) \ - XPAND(BLECentral , connect_callback_t ) \ - XPAND(BLECentral , disconnect_callback_t ) \ - /* Client Characteristic */ \ - XPAND(BLEClientCharacteristic , notify_cb_t ) \ + /* Client Characteristic */ \ + XPAND(BLEClientCharacteristic , notify_cb_t ) \ /*XPAND(BLEClientCharacteristic , indicate_cb_t )*/ \ #define ADA_CB_ENUM_XPAND(_class, _cbname) \