diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index d0f7c49a..62d6ec6c 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -36,7 +36,7 @@ #include "bluefruit.h" -#define BLE_CENTRAL_TIMEOUT 10000 +#define BLE_CENTRAL_TIMEOUT 3000 /** * Constructor @@ -49,6 +49,8 @@ BLECentral::BLECentral(void) _evt_buf = NULL; _evt_bufsize = 0; + _txpacket_sem = NULL; + _chars_count = 0; for(uint8_t i=0; ievt.gap_evt.conn_handle; + // Init transmission buffer for notification. TODO multiple connections + uint8_t txbuf_max; + (void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max); + _txpacket_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max); + if ( _connect_cb ) _connect_cb(); } } @@ -357,11 +370,25 @@ void BLECentral::_event_handler(ble_evt_t* evt) _conn_hdl = BLE_CONN_HANDLE_INVALID; + // TODO multiple connections + vSemaphoreDelete(_txpacket_sem); + _txpacket_sem = NULL; + if ( _disconnect_cb ) _disconnect_cb(evt->evt.gap_evt.params.disconnected.reason); startScanning(); break; + case BLE_EVT_TX_COMPLETE: + if ( _txpacket_sem ) + { + for(uint8_t i=0; ievt.common_evt.params.tx_complete.count; i++) + { + xSemaphoreGive(_txpacket_sem); + } + } + break; + case BLE_GAP_EVT_TIMEOUT: if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN) { @@ -458,7 +485,22 @@ void BLECentral::_event_handler(ble_evt_t* evt) { for(int i=0; i<_chars_count; i++) { - _chars_list[i]->_eventHandler(evt); + 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 67f8a96f..0d0f170d 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -63,29 +63,30 @@ class BLECentral *------------------------------------------------------------------*/ typedef void (*scan_callback_t) (ble_gap_evt_adv_report_t*); - void setScanCallback(scan_callback_t fp); - err_t startScanning(uint16_t timeout = 0); - err_t stopScanning(void); + void setScanCallback(scan_callback_t fp); + err_t startScanning(uint16_t timeout = 0); + err_t stopScanning(void); uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len); uint8_t* extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len); - bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid); /*------------------------------------------------------------------*/ /* *------------------------------------------------------------------*/ - err_t connect(const ble_gap_evt_adv_report_t* adv_report, - uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, - uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT); + err_t connect(const ble_gap_evt_adv_report_t* adv_report, + uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, + uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT); - err_t connect(const ble_gap_addr_t *peer_addr, - uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, - uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT); + err_t connect(const ble_gap_addr_t *peer_addr, + uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, + uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT); bool connected (void); uint16_t connHandle (void); + bool getTxPacket(uint32_t ms); + /*------------------------------------------------------------------*/ /* GATTC Discovery *------------------------------------------------------------------*/ @@ -118,10 +119,12 @@ class BLECentral void* _evt_buf; uint16_t _evt_bufsize; + // Transmission Buffer Count for HVX notification, max is seen at 7 + SemaphoreHandle_t _txpacket_sem; + 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 1b47a522..ab26d871 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -36,13 +36,15 @@ #include "bluefruit.h" -#define MAX_DESCIRPTORS 8 +#define MAX_DESCIRPTORS 8 +#define CENTRAL_CHR_TIMEOUT 100 void BLECentralCharacteristic::_init(void) { _cccd_handle = 0; _notify_cb = NULL; + _sem = NULL; } BLECentralCharacteristic::BLECentralCharacteristic(void) @@ -96,18 +98,99 @@ void BLECentralCharacteristic::begin(void) { _service = BLECentralService::lastService; - // Currently Only register to Bluefruit if callback is installed - if ( _notify_cb ) - { - (void) Bluefruit.Central._registerCharacteristic(this); - } + // Register to Bluefruit (required for callback and write response) + (void) Bluefruit.Central._registerCharacteristic(this); } -uint16_t BLECentralCharacteristic::read(void* buffer, int bufsize, uint16_t offset) +/*------------------------------------------------------------------*/ +/* READ + *------------------------------------------------------------------*/ +uint16_t BLECentralCharacteristic::read(void* buffer, int bufsize) { - VERIFY_STATUS( sd_ble_gattc_read(Bluefruit.Central.connHandle(), _chr.handle_value, offset), 0 ); +// VERIFY_STATUS( sd_ble_gattc_read(Bluefruit.Central.connHandle(), _chr.handle_value, offset), 0 ); - return ERROR_NONE; +// return ERROR_NONE; +} + +/*------------------------------------------------------------------*/ +/* WRITE + *------------------------------------------------------------------*/ +uint16_t BLECentralCharacteristic::write_resp(const void* data, int len) +{ + // Break into multiple MTU-3 packet + // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 + // SD132 v3.0 could negotiate MTU to higher number + const uint16_t MTU_MPS = 20; + + const uint8_t* u8data = (const uint8_t*) data; + + // Write Response requires to wait for BLE_GATTC_EVT_WRITE_RSP event + _sem = xSemaphoreCreateBinary(); + + int remaining = len; + while( remaining ) + { + // Write Req need to wait for response + uint16_t packet_len = min16(MTU_MPS, remaining); + + ble_gattc_write_params_t param = + { + .write_op = BLE_GATT_OP_WRITE_REQ, + .flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE, + .handle = _chr.handle_value, + .offset = 0, // For WRITE_CMD and WRITE_REQ, offset must be 0. + .len = packet_len, + .p_value = (uint8_t* ) u8data + }; + + if ( ERROR_NONE != sd_ble_gattc_write(Bluefruit.Central.connHandle(), ¶m) ) break; + + if ( !xSemaphoreTake(_sem, CENTRAL_CHR_TIMEOUT) ) break; + + remaining -= packet_len; + u8data += packet_len; + } + + vSemaphoreDelete(_sem); + _sem = NULL; + + return len - remaining; +} + +uint16_t BLECentralCharacteristic::write(const void* data, int len) +{ + // Break into multiple MTU-3 packet + // TODO Currently SD132 v2.0 MTU is fixed with max payload = 20 + // SD132 v3.0 could negotiate MTU to higher number + const uint16_t MTU_MPS = 20; + + const uint8_t* u8data = (const uint8_t*) data; + + int remaining = len; + while( remaining ) + { + // Write CMD consume a TX buffer + if ( !Bluefruit.Central.getTxPacket(100) ) return BLE_ERROR_NO_TX_PACKETS; + + uint16_t packet_len = min16(MTU_MPS, remaining); + + ble_gattc_write_params_t param = + { + .write_op = BLE_GATT_OP_WRITE_CMD, + .flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE, + .handle = _chr.handle_value, + .offset = 0, // For WRITE_CMD and WRITE_REQ, offset must be 0. + .len = packet_len, + .p_value = (uint8_t* ) u8data + }; + + VERIFY_STATUS( sd_ble_gattc_write(Bluefruit.Central.connHandle(), ¶m), len - remaining ); + + remaining -= packet_len; + u8data += packet_len; + } + + return len; } void BLECentralCharacteristic::setNotifyCallback(notify_cb_t fp) @@ -128,19 +211,21 @@ bool BLECentralCharacteristic::enableNotify(void) .p_value = (uint8_t*) &value }; - // TODO consume a TX buffer + // Write consume a TX buffer + if ( !Bluefruit.Central.getTxPacket(100) ) return BLE_ERROR_NO_TX_PACKETS; + VERIFY_STATUS( sd_ble_gattc_write(Bluefruit.Central.connHandle(), ¶m), false ); return true; } -void BLECentralCharacteristic::_eventHandler(ble_evt_t* event) +void BLECentralCharacteristic::_eventHandler(ble_evt_t* evt) { - switch(event->header.evt_id) + switch(evt->header.evt_id) { case BLE_GATTC_EVT_HVX: { - ble_gattc_evt_hvx_t* hvx = &event->evt.gattc_evt.params.hvx; + ble_gattc_evt_hvx_t* hvx = &evt->evt.gattc_evt.params.hvx; if ( hvx->type == BLE_GATT_HVX_NOTIFICATION ) { @@ -152,6 +237,19 @@ void BLECentralCharacteristic::_eventHandler(ble_evt_t* event) } break; + case BLE_GATTC_EVT_WRITE_RSP: + { + ble_gattc_evt_write_rsp_t* wr_rsp = (ble_gattc_evt_write_rsp_t*) &evt->evt.gattc_evt.params.write_rsp; + + if (wr_rsp->write_op == BLE_GATT_OP_WRITE_REQ &&_sem) + { + // TODO check evt->evt.gattc_evt.gatt_status + xSemaphoreGive(_sem); + } + } + break; + default: break; } } + diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h index 538443b7..5c05289d 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -58,9 +58,11 @@ class BLECentralCharacteristic BLECentralService& parentService(void); /*------------- Read -------------*/ - uint16_t read(void* buffer, int bufsize, uint16_t offset = 0); + uint16_t read(void* buffer, int bufsize); /*------------- Write -------------*/ + uint16_t write (const void* data, int len); + uint16_t write_resp(const void* data, int len); /*------------- Notify -------------*/ bool enableNotify(void); @@ -79,8 +81,10 @@ class BLECentralCharacteristic BLECentralService* _service; notify_cb_t _notify_cb; - void _init(void); - void _eventHandler(ble_evt_t* event); + SemaphoreHandle_t _sem; + + void _init (void); + void _eventHandler (ble_evt_t* event); friend class BLECentral; };