From d4e3244341e55f80c790045e3fd6c6b74a96f892 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 10 Jul 2017 20:56:19 +0700 Subject: [PATCH] make Gatt support multiple connections --- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 42 +++++++++++++++--------- libraries/Bluefruit52Lib/src/BLEGatt.h | 2 +- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index c8a4c474..7f345847 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -70,6 +70,9 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe void BLEGatt::_eventHandler(ble_evt_t* evt) { + // conn handle has fixed offset regardless of event type + const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle; + // Server Characteristics for(int i=0; i<_server.chr_count; i++) { @@ -79,28 +82,37 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) // Client Characteristics for(int i=0; i<_client.chr_count; i++) { - bool matched = false; - - switch(evt->header.evt_id) + if ( evt_conn_hdl == _client.chr_list[i]->connHandle() ) { - case BLE_GATTC_EVT_HVX: - case BLE_GATTC_EVT_WRITE_RSP: - case BLE_GATTC_EVT_READ_RSP: - // write & read & hvc response's handle has same offset from the struct - matched = (_client.chr_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle); - break; + bool matched = false; - default: break; + switch(evt->header.evt_id) + { + case BLE_GATTC_EVT_HVX: + case BLE_GATTC_EVT_WRITE_RSP: + case BLE_GATTC_EVT_READ_RSP: + // write & read & hvc response's handle has same offset from the struct + matched = (_client.chr_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle); + break; + + default: break; + } + + // invoke charactersistic handler if matched + if ( matched ) _client.chr_list[i]->_eventHandler(evt); } - - // invoke charactersistic handler if matched - if ( matched ) _client.chr_list[i]->_eventHandler(evt); } - // disconnect Client Service + // disconnect Client Service of the disconnected handle if ( evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED ) { - for(int i=0; i<_client.svc_count; i++) _client.svc_list[i]->disconnect(); + for(int i=0; i<_client.svc_count; i++) + { + if ( evt_conn_hdl == _client.svc_list[i]->_conn_hdl) + { + _client.svc_list[i]->disconnect(); + } + } } // GATTC Read Characteristic by UUID procedure diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h index b7396ab2..342aeb5d 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.h +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -48,7 +48,7 @@ #define BLE_GATT_MAX_SERVER_CHARS 40 -#define BLE_GATT_MAX_CLIENT_SERVICE 10 +#define BLE_GATT_MAX_CLIENT_SERVICE 20 #define BLE_GATT_MAX_CLIENT_CHARS 30