From 8add8a37b909cf52b95c931f7caae6a65762e233 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 26 Jul 2017 11:30:19 +0700 Subject: [PATCH] invoke BLEServer _disconnect_cb and _connect_cb --- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 1 - libraries/Bluefruit52Lib/src/BLEGatt.cpp | 33 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index e5032167..b8de5080 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -291,7 +291,6 @@ err_t BLECharacteristic::begin(void) } /** - * Call by Bluefruit event poll() * @param event */ void BLECharacteristic::_eventHandler(ble_evt_t* event) diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 204f0166..d6c4f07a 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -72,9 +72,28 @@ 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; + const uint16_t evt_id = evt->header.evt_id; + + // Server Service TODO multiple peripherals + if ( evt_conn_hdl == Bluefruit.connHandle() ) + { + if ( evt_id == BLE_GAP_EVT_DISCONNECTED || evt_id == BLE_GAP_EVT_CONNECTED ) + { + for(uint8_t i=0; i<_server.svc_count; i++) + { + if ( evt_id == BLE_GAP_EVT_DISCONNECTED ) + { + _server.svc_list[i]->_disconnect_cb(); + }else + { + _server.svc_list[i]->_connect_cb(); + } + } + } + } // Server Characteristics - for(int i=0; i<_server.chr_count; i++) + for(uint8_t i=0; i<_server.chr_count; i++) { _server.chr_list[i]->_eventHandler(evt); } @@ -86,7 +105,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) { bool matched = false; - switch(evt->header.evt_id) + switch(evt_id) { case BLE_GATTC_EVT_HVX: case BLE_GATTC_EVT_WRITE_RSP: @@ -104,9 +123,10 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } // disconnect Client Services & Characteristics of the disconnected handle - if ( evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED ) + if ( evt_id == BLE_GAP_EVT_DISCONNECTED ) { - for(int i=0; i<_client.svc_count; i++) + // Client + for(uint8_t i=0; i<_client.svc_count; i++) { if ( evt_conn_hdl == _client.svc_list[i]->_conn_hdl) { @@ -114,7 +134,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } } - for(int i=0; i<_client.chr_count; i++) + // TODO merge to above loop + for(uint8_t i=0; i<_client.chr_count; i++) { if ( evt_conn_hdl == _client.chr_list[i]->connHandle() ) { @@ -124,7 +145,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } // GATTC Read Characteristic by UUID procedure - if ( evt->header.evt_id == BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP ) + if ( evt_id == BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP ) { ble_gattc_evt_char_val_by_uuid_read_rsp_t* rd_rsp = &evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp;