From 2e886d43df181121cb5dffa2137a30eeea2b46a5 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 19 Jul 2017 12:47:21 +0700 Subject: [PATCH] user ada msg for Discovery --- .../src/BLEClientCharacteristic.cpp | 2 +- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 117 ++++++++++-------- libraries/Bluefruit52Lib/src/BLEDiscovery.h | 8 +- libraries/Bluefruit52Lib/src/bluefruit.cpp | 4 +- .../Bluefruit52Lib/src/utility/AdaMsg.cpp | 7 +- 5 files changed, 73 insertions(+), 65 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index 580222f2..879a4e67 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -128,7 +128,7 @@ bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle) ble_gattc_desc_t descs[MAX_DESCIRPTORS]; }disc_rsp; - uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, MAX_DESCIRPTORS); + uint16_t count = Bluefruit.Discovery._discoverDescriptor(conn_handle, (ble_gattc_evt_desc_disc_rsp_t*) &disc_rsp, sizeof(disc_rsp)); // only care CCCD for now for(uint16_t i=0; icount, max_count); - if (result) + // timeout or has no data (due to GATT Error) + if ( count <= 0 ) return false; + + uint16_t desc_count = disc_desc->count; + if (desc_count) { - for(uint16_t i=0; idescs[i].uuid.uuid, disc_desc->descs[i].handle); } @@ -167,80 +172,82 @@ uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_d // increase handle range for next discovery // should be +1 more, but that will cause missing on the next Characteristic !!!!! // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result - _hdl_range.start_handle = disc_desc->descs[result-1].handle; + _hdl_range.start_handle = disc_desc->descs[desc_count-1].handle; } - return result; + return desc_count; } void BLEDiscovery::_event_handler(ble_evt_t* evt) { + ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; + switch ( evt->header.evt_id ) { case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP: { - ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp; LOG_LV2(Discover, "[SVC] Service Count: %d", svc_rsp->count); - if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS && svc_rsp->count && _evt_buf) + if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) { - // Only support 1 service - - _evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t)); - memcpy(_evt_buf, svc_rsp, _evt_bufsize); + // Only 1 service at a time + if (svc_rsp->count) + { + _adamsg.feed(svc_rsp, sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t)); + } }else { - _evt_bufsize = 0; // no data + LOG_LV1(Discover, "[SVC] Gatt Status = 0x%04X", gattc->gatt_status); } - xSemaphoreGive(_sem); + _adamsg.complete(); } break; case BLE_GATTC_EVT_CHAR_DISC_RSP: { - ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; ble_gattc_evt_char_disc_rsp_t* chr_rsp = &gattc->params.char_disc_rsp; LOG_LV2(Discover, "[CHR] Characteristic Count: %d", chr_rsp->count); - if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && chr_rsp->count && _evt_buf ) - { - // TODO support only 1 discovered char now - _evt_bufsize = min16(_evt_bufsize, sizeof(ble_gattc_evt_char_disc_rsp_t)); - memcpy(_evt_buf, chr_rsp, _evt_bufsize); + if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) + { + if ( chr_rsp->count ) + { + // TODO support only 1 discovered char now + _adamsg.feed(chr_rsp, sizeof(ble_gattc_evt_char_disc_rsp_t)); + } }else { - _evt_bufsize = 0; // no data + LOG_LV1(Discover, "[CHR] Gatt Status = 0x%04X", gattc->gatt_status); } - xSemaphoreGive(_sem); + _adamsg.complete(); } break; case BLE_GATTC_EVT_DESC_DISC_RSP: { - ble_gattc_evt_t* gattc = &evt->evt.gattc_evt; ble_gattc_evt_desc_disc_rsp_t* desc_rsp = &gattc->params.desc_disc_rsp; LOG_LV2(Discover, "[DESC] Descriptor Count: %d", desc_rsp->count); - if ( (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) && desc_rsp->count && _evt_buf ) + if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS) { - // Copy up to bufsize - uint16_t len = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (desc_rsp->count-1)*sizeof(ble_gattc_desc_t); - _evt_bufsize = min16(_evt_bufsize, len); - - memcpy(_evt_buf, desc_rsp, _evt_bufsize); + if ( desc_rsp->count ) + { + uint16_t len = sizeof(ble_gattc_evt_desc_disc_rsp_t) + (desc_rsp->count-1)*sizeof(ble_gattc_desc_t); + _adamsg.feed(desc_rsp, len); + } }else { - _evt_bufsize = 0; // no data + LOG_LV1(Discover, "[DESC] Gatt Status = 0x%04X", gattc->gatt_status); } - xSemaphoreGive(_sem); + _adamsg.complete(); } break; diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.h b/libraries/Bluefruit52Lib/src/BLEDiscovery.h index c66611f4..108460f9 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.h +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.h @@ -49,10 +49,8 @@ class BLEDiscovery { private: ble_gattc_handle_range_t _hdl_range; - void* _evt_buf; - uint16_t _evt_bufsize; - - SemaphoreHandle_t _sem; + AdaMsg _adamsg; + bool _begun; void _event_handler(ble_evt_t* evt); @@ -103,7 +101,7 @@ class BLEDiscovery * code. User should not call these directly *------------------------------------------------------------------*/ bool _discoverService(uint16_t conn_handle, BLEClientService& svc, uint16_t start_handle = 1); - uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count); + uint16_t _discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t bufsize); friend class AdafruitBluefruit; }; diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index bd952630..6a141149 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -509,12 +509,10 @@ void adafruit_ble_task(void* arg) */ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) { - LOG_LV1(BLE, dbg_ble_event_str(evt->header.evt_id)); - // conn handle has fixed offset regardless of event type const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle; - LOG_LV2(BLE, "Conn Handle = %d", evt_conn_hdl); + LOG_LV1(BLE, "%s : Conn Handle = %d", dbg_ble_event_str(evt->header.evt_id), evt_conn_hdl); // GAP handler Gap._eventHandler(evt); diff --git a/libraries/Bluefruit52Lib/src/utility/AdaMsg.cpp b/libraries/Bluefruit52Lib/src/utility/AdaMsg.cpp index a3084486..a40e3386 100644 --- a/libraries/Bluefruit52Lib/src/utility/AdaMsg.cpp +++ b/libraries/Bluefruit52Lib/src/utility/AdaMsg.cpp @@ -67,12 +67,17 @@ void AdaMsg::prepare(void* buf, uint16_t bufsize) xferlen = 0; } +/** + * + * @param ms + * @return -1 if timeout + */ int32_t AdaMsg::waitUntilComplete(uint32_t ms) { if (_dynamic) { _sem = xSemaphoreCreateBinary(); - VERIFY(_sem, 0); + VERIFY(_sem, -1); } int result = -1;