From 852e10000762790bbda812447e9d474316067728 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 30 Mar 2017 19:55:57 +0700 Subject: [PATCH] make gatt support both client & server --- libraries/Bluefruit52Lib/src/BLECentral.cpp | 49 --------------- libraries/Bluefruit52Lib/src/BLECentral.h | 17 ------ .../src/BLECentralCharacteristic.cpp | 2 +- .../src/BLECentralCharacteristic.h | 2 +- .../Bluefruit52Lib/src/BLECentralService.cpp | 2 +- .../Bluefruit52Lib/src/BLECentralService.h | 2 +- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 60 ++++++++++++++++--- libraries/Bluefruit52Lib/src/BLEGatt.h | 30 +++++++++- 8 files changed, 83 insertions(+), 81 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 65605cd6..7fed4ad5 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -47,12 +47,6 @@ BLECentral::BLECentral(void) _txpacket_sem = NULL; - _svc_count = 0; - for(uint8_t i=0; idisconnect(); - if ( _disconnect_cb ) _disconnect_cb(evt->evt.gap_evt.params.disconnected.reason); startScanning(); @@ -323,28 +298,4 @@ void BLECentral::_event_handler(ble_evt_t* evt) default: break; } } - - // GATT characteristics event handler only if Connection Handle matches - if ( evt_conn_hdl == _conn_hdl) - { - for(int i=0; i<_chars_count; i++) - { - bool matched = false; - - switch(evt->header.evt_id) - { - case BLE_GATTC_EVT_HVX: - case BLE_GATTC_EVT_WRITE_RSP: - case BLE_GATTC_EVT_READ_RSP: - // write & read response's handle has same offset. - matched = (_chars_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle); - break; - - default: break; - } - - // invoke charactersistic handler if matched - if ( matched ) _chars_list[i]->_eventHandler(evt); - } - } } diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index 09674fa4..b14180eb 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -46,9 +46,6 @@ #include "BLECentralCharacteristic.h" #include "BLECentralService.h" -#define BLE_CENTRAL_MAX_SERVICE 10 -#define BLE_CENTRAL_MAX_CHARS 30 - class AdafruitBluefruit; @@ -97,26 +94,12 @@ class BLECentral void setConnectCallback ( connect_callback_t fp); void setDisconnectCallback( disconnect_callback_t fp); - /*------------------------------------------------------------------*/ - /* INTERNAL USAGE ONLY - * Although declare as public, it is meant to be invoked by internal - * code. User should not call these directly - *------------------------------------------------------------------*/ - bool _registerService(BLECentralService* svc); - bool _registerCharacteristic(BLECentralCharacteristic* chr); - private: uint16_t _conn_hdl; // Transmission Buffer Count for HVX notification, max is seen at 7 SemaphoreHandle_t _txpacket_sem; - BLECentralService* _svc_list[BLE_CENTRAL_MAX_SERVICE]; - uint8_t _svc_count; - - BLECentralCharacteristic* _chars_list[BLE_CENTRAL_MAX_CHARS]; - uint8_t _chars_count; - ble_gap_scan_params_t _scan_param; scan_callback_t _scan_cb; diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp index ee362b30..0a63480a 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -106,7 +106,7 @@ void BLECentralCharacteristic::begin(void) _service = BLECentralService::lastService; // Register to Bluefruit (required for callback and write response) - (void) Bluefruit.Central._registerCharacteristic(this); + (void) Bluefruit.Gatt._addCharacteristic(this); } /*------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h index 0fc323ce..9cac4236 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -88,7 +88,7 @@ class BLECentralCharacteristic void _init (void); void _eventHandler (ble_evt_t* event); - friend class BLECentral; + friend class BLEGatt; }; #endif /* BLECENTRALCHARACTERISTIC_H_ */ diff --git a/libraries/Bluefruit52Lib/src/BLECentralService.cpp b/libraries/Bluefruit52Lib/src/BLECentralService.cpp index 89068129..55097044 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralService.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralService.cpp @@ -63,7 +63,7 @@ bool BLECentralService::begin(void) uuid.begin(); lastService = this; - (void) Bluefruit.Central._registerService(this); + (void) Bluefruit.Gatt._addService(this); return true; } diff --git a/libraries/Bluefruit52Lib/src/BLECentralService.h b/libraries/Bluefruit52Lib/src/BLECentralService.h index 4839f5ec..8c82dbc7 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralService.h +++ b/libraries/Bluefruit52Lib/src/BLECentralService.h @@ -60,7 +60,7 @@ class BLECentralService virtual bool discover (uint16_t conn_handle); bool discovered(void); - friend class BLECentral; + friend class BLEGatt; }; diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 94726589..b9264123 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -39,24 +39,68 @@ BLEGatt::BLEGatt(void) { - _chars_count = 0; - for(uint8_t i=0; i_eventHandler(evt); + for(int i=0; i_eventHandler(evt); + } + // Client Characteristics + for(int i=0; iheader.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._chars_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle); + break; + + default: break; + } + + // invoke charactersistic handler if matched + if ( matched ) client._chars_list[i]->_eventHandler(evt); + } + + // disconnect Client Service + if ( evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED ) + { + for(int i=0; idisconnect(); + } } -bool BLEGatt::_addCharacteristic(BLECharacteristic* chars) +bool BLEGatt::_addCharacteristic(BLECharacteristic* chr) { - if ( _chars_count == BLE_GATT_MAX_SERVER_CHARS ) return false; - _chars_list[ _chars_count++ ] = chars; + if ( server._chars_count == BLE_GATT_MAX_SERVER_CHARS ) return false; + server._chars_list[ server._chars_count++ ] = chr; + + return true; +} + + + +bool BLEGatt::_addCharacteristic(BLECentralCharacteristic* chr) +{ + VERIFY( client._chars_count < BLE_CENTRAL_MAX_CHARS ); + client._chars_list[ client._chars_count++ ] = chr; + + return true; +} + +bool BLEGatt::_addService(BLECentralService* svc) +{ + VERIFY( client._svc_count < BLE_CENTRAL_MAX_SERVICE ); + client._svc_list[ client._svc_count++ ] = svc; return true; } diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h index 3701b1b6..c48467f3 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.h +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -47,18 +47,42 @@ #include "BLECentralService.h" #define BLE_GATT_MAX_SERVER_CHARS 40 +#define BLE_CENTRAL_MAX_SERVICE 10 +#define BLE_CENTRAL_MAX_CHARS 30 + class BLEGatt { public: BLEGatt(void); - bool _addCharacteristic(BLECharacteristic* chars); + /*------------------------------------------------------------------*/ + /* INTERNAL USAGE ONLY + * Although declare as public, it is meant to be invoked by internal + * code. User should not call these directly + *------------------------------------------------------------------*/ + // Server + bool _addCharacteristic(BLECharacteristic* chr); + + // Client + bool _addCharacteristic(BLECentralCharacteristic* chr); + bool _addService(BLECentralService* svc); + void _eventHandler(ble_evt_t* evt); private: - BLECharacteristic* _chars_list[BLE_GATT_MAX_SERVER_CHARS]; - uint8_t _chars_count; + struct { + uint8_t _chars_count; + BLECharacteristic* _chars_list[BLE_GATT_MAX_SERVER_CHARS]; + } server; + + struct { + uint8_t _svc_count; + BLECentralService* _svc_list[BLE_CENTRAL_MAX_SERVICE]; + + uint8_t _chars_count; + BLECentralCharacteristic* _chars_list[BLE_CENTRAL_MAX_CHARS]; + }client; }; #endif /* BLEGATT_H_ */