From 9a74a6f3004030dd92f4de9ef2bce67976be5b3d Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Jul 2017 23:39:18 +0700 Subject: [PATCH] more central support - rename BLE_GAP_MAX_CONN to BLE_MAX_CONN - change rx callback of BLEClientUart - better support multiple connection --- changelog.md | 2 + .../central_bleuart/central_bleuart.ino | 13 ++- libraries/Bluefruit52Lib/src/BLECentral.cpp | 107 ++++++++---------- libraries/Bluefruit52Lib/src/BLEGap.cpp | 2 +- libraries/Bluefruit52Lib/src/BLEGap.h | 4 +- libraries/Bluefruit52Lib/src/BLEGatt.h | 2 +- libraries/Bluefruit52Lib/src/bluefruit.h | 2 + .../src/clients/BLEClientUart.cpp | 6 +- .../src/clients/BLEClientUart.h | 2 +- 9 files changed, 67 insertions(+), 73 deletions(-) diff --git a/changelog.md b/changelog.md index 02d3acbb..a59882bd 100644 --- a/changelog.md +++ b/changelog.md @@ -18,6 +18,8 @@ - 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/examples/Central/central_bleuart/central_bleuart.ino b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino index dc6eadb9..f092b032 100644 --- a/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino +++ b/libraries/Bluefruit52Lib/examples/Central/central_bleuart/central_bleuart.ino @@ -132,19 +132,24 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) Serial.println("Bluefruit will auto start scanning (default)"); } -void uart_rx_callback(void) +/** + * Callback invoked when uart received data + * @param uart_svc Reference object to the service where the data + * arrived. In this example it is clientUart + */ +void uart_rx_callback(BLEClientUart& uart_svc) { Serial.print("[RX]: "); - while ( clientUart.available() ) + while ( uart_svc.available() ) { - Serial.print( (char) clientUart.read() ); + Serial.print( (char) uart_svc.read() ); } Serial.println(); } -void loop() +void loop() { if ( Bluefruit.Central.connected() ) { diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 4bfab045..f6b8960d 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -42,8 +42,6 @@ */ BLECentral::BLECentral(void) { - _conn_hdl = BLE_CONN_HANDLE_INVALID; - _scan_cb = NULL; _scan_param = (ble_gap_scan_params_t) { .active = 1, @@ -200,7 +198,7 @@ bool BLECentral::connected(uint16_t conn_handle) */ bool BLECentral::connected(void) { - for (uint8_t conn=0; connevt.common_evt.conn_handle; - /* Only handle Central events with matched connection handle - * or a few special one - * - Connected event - * - Advertising Report - * - Advertising timeout (could be connected and advertising at the same time) + /* PrPh handle connection is already filtered. Only handle Central events or + * connection handle is BLE_CONN_HANDLE_INVALID (e.g BLE_GAP_EVT_ADV_REPORT) */ - if ( evt_conn_hdl == _conn_hdl || - evt->header.evt_id == BLE_GAP_EVT_CONNECTED || - evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT || - evt->header.evt_id == BLE_GAP_EVT_TIMEOUT ) + switch ( evt->header.evt_id ) { - switch ( evt->header.evt_id ) + case BLE_GAP_EVT_ADV_REPORT: { - case BLE_GAP_EVT_ADV_REPORT: - { - ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report; - if (_scan_cb) _scan_cb(adv_report); - } - break; - - case BLE_GAP_EVT_CONNECTED: - { - 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); - - // TODO multiple connections - _conn_hdl = evt->evt.gap_evt.conn_handle; - - if ( _connect_cb ) - { - ada_callback(NULL, _connect_cb, evt->evt.gap_evt.conn_handle); - } - } - } - break; - - case BLE_GAP_EVT_DISCONNECTED: - 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; - - case BLE_GAP_EVT_TIMEOUT: - if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN) - { - // TODO Advance Scanning - // Restart Scanning - startScanning(); - } - break; - - default: break; + // evt_conn_hdl is equal to BLE_CONN_HANDLE_INVALID + ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report; + if (_scan_cb) _scan_cb(adv_report); } + break; + + case BLE_GAP_EVT_CONNECTED: + { + 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: + 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; + + case BLE_GAP_EVT_TIMEOUT: + if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN) + { + // TODO Advance Scanning + // Restart Scanning + startScanning(); + } + break; + + default: break; } } diff --git a/libraries/Bluefruit52Lib/src/BLEGap.cpp b/libraries/Bluefruit52Lib/src/BLEGap.cpp index 16a3887f..be079bcd 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGap.cpp @@ -64,7 +64,7 @@ bool BLEGap::getTxPacket(void) bool BLEGap::getTxPacket(uint16_t conn_handle) { - VERIFY( (conn_handle < BLE_GAP_MAX_CONN) && (_peers[conn_handle].txpacket_sem != NULL) ); + VERIFY( (conn_handle < BLE_MAX_CONN) && (_peers[conn_handle].txpacket_sem != NULL) ); return xSemaphoreTake(_peers[conn_handle].txpacket_sem, ms2tick(BLE_GENERIC_TIMEOUT)); } diff --git a/libraries/Bluefruit52Lib/src/BLEGap.h b/libraries/Bluefruit52Lib/src/BLEGap.h index d76bb471..82f50347 100644 --- a/libraries/Bluefruit52Lib/src/BLEGap.h +++ b/libraries/Bluefruit52Lib/src/BLEGap.h @@ -40,8 +40,6 @@ #include "bluefruit_common.h" #include "BLEUuid.h" -#define BLE_GAP_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN) - class BLEGap { public: @@ -75,7 +73,7 @@ class BLEGap SemaphoreHandle_t txpacket_sem; } gap_peer_t; - gap_peer_t _peers[BLE_GAP_MAX_CONN]; + gap_peer_t _peers[BLE_MAX_CONN]; }; #endif /* BLEGAP_H_ */ diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h index 342aeb5d..14edb595 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.h +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -49,7 +49,7 @@ #define BLE_GATT_MAX_SERVER_CHARS 40 #define BLE_GATT_MAX_CLIENT_SERVICE 20 -#define BLE_GATT_MAX_CLIENT_CHARS 30 +#define BLE_GATT_MAX_CLIENT_CHARS 40 class BLEGatt diff --git a/libraries/Bluefruit52Lib/src/bluefruit.h b/libraries/Bluefruit52Lib/src/bluefruit.h index b6029931..a62388cc 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.h +++ b/libraries/Bluefruit52Lib/src/bluefruit.h @@ -47,6 +47,8 @@ #define BLE_CENTRAL_MAX_CONN 4 #define BLE_CENTRAL_MAX_SECURE_CONN 1 // should be enough +#define BLE_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN) + #include "BLEUuid.h" #include "BLEAdvertising.h" #include "BLECharacteristic.h" diff --git a/libraries/Bluefruit52Lib/src/clients/BLEClientUart.cpp b/libraries/Bluefruit52Lib/src/clients/BLEClientUart.cpp index 4b2a11bd..4569309c 100644 --- a/libraries/Bluefruit52Lib/src/clients/BLEClientUart.cpp +++ b/libraries/Bluefruit52Lib/src/clients/BLEClientUart.cpp @@ -50,8 +50,8 @@ bool BLEClientUart::begin(void) // Invoke base class begin() BLEClientService::begin(); - _rxd.begin(); - _txd.begin(); + _rxd.begin(this); + _txd.begin(this); // set up notify callback _txd.setNotifyCallback(bleuart_central_notify_cb); @@ -100,7 +100,7 @@ void bleuart_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint uart_svc._fifo.write(data, len); // invoke callback - if ( uart_svc._rx_cb ) uart_svc._rx_cb(); + if ( uart_svc._rx_cb ) uart_svc._rx_cb(uart_svc); } /*------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/clients/BLEClientUart.h b/libraries/Bluefruit52Lib/src/clients/BLEClientUart.h index 23a6ec4d..90c12278 100644 --- a/libraries/Bluefruit52Lib/src/clients/BLEClientUart.h +++ b/libraries/Bluefruit52Lib/src/clients/BLEClientUart.h @@ -47,7 +47,7 @@ class BLEClientUart : public BLEClientService, public Stream { public: - typedef void (*rx_callback_t) (void); + typedef void (*rx_callback_t) (BLEClientUart& svc); BLEClientUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH);