From e05bdc9030c7c184a7df79144929b7ee94569506 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 13 Feb 2019 14:57:36 +0700 Subject: [PATCH] implement BLECharacteristic long write #91,#220 --- cores/nRF5/utility/debug.cpp | 2 +- .../Bluefruit52Lib/src/BLECharacteristic.cpp | 82 +++++++++++++++++-- .../Bluefruit52Lib/src/BLECharacteristic.h | 6 ++ .../src/BLEClientCharacteristic.cpp | 8 +- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 17 +++- libraries/Bluefruit52Lib/src/bluefruit.cpp | 9 ++ 6 files changed, 113 insertions(+), 11 deletions(-) diff --git a/cores/nRF5/utility/debug.cpp b/cores/nRF5/utility/debug.cpp index e059ef71..d7862020 100644 --- a/cores/nRF5/utility/debug.cpp +++ b/cores/nRF5/utility/debug.cpp @@ -169,7 +169,7 @@ static void dump_str_line(uint8_t const* buf, uint16_t count) void dbgDumpMemory(void const *buf, uint8_t size, uint16_t count, bool printOffset) { - if ( !buf ) + if ( !buf || !count ) { printf("NULL\n"); return; diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index 2d020855..91463b6e 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -58,6 +58,10 @@ void BLECharacteristic::_init(void) _handles.sccd_handle = BLE_GATT_HANDLE_INVALID; _handles.cccd_handle = BLE_GATT_HANDLE_INVALID; + _long_wr.buffer = NULL; + _long_wr.bufsize = 0; + _long_wr.count = 0; + _rd_authorize_cb = NULL; _wr_authorize_cb = NULL; _wr_cb = NULL; @@ -337,15 +341,84 @@ err_t BLECharacteristic::addDescriptor(BLEUuid bleuuid, void const * content, ui */ void BLECharacteristic::_eventHandler(ble_evt_t* event) { + uint16_t const conn_hdl = event->evt.common_evt.conn_handle; + switch(event->header.evt_id) { case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: { ble_gatts_evt_rw_authorize_request_t * request = &event->evt.gatts_evt.params.authorize_request; - if ( (request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) && (_wr_authorize_cb != NULL)) + if (request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) { - _wr_authorize_cb(*this, &request->request.write); + ble_gatts_evt_write_t * wr_req = &request->request.write; + + switch(wr_req->op) + { + case BLE_GATTS_OP_PREP_WRITE_REQ: + { + // Prepare Long Write + if ( !_long_wr.buffer ) + { + // Allocate long write buffer if not previously + _long_wr.bufsize = 1024; // TODO bufsize is 10x MTU + _long_wr.buffer = (uint8_t*) rtos_malloc(_long_wr.bufsize); + _long_wr.count = 0; + } + + ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE }; + + if ( wr_req->offset + wr_req->len > _long_wr.bufsize ) + { + reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_PREPARE_QUEUE_FULL; + }else + { + reply.params.write = ((ble_gatts_authorize_params_t) { + .gatt_status = BLE_GATT_STATUS_SUCCESS, + .update = 1, + .offset = wr_req->offset, + .len = wr_req->len, + .p_data = wr_req->data + }); + + memcpy(_long_wr.buffer+wr_req->offset, wr_req->data, wr_req->len); + _long_wr.count = max16(_long_wr.count, wr_req->offset + wr_req->len); + } + + sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply); + } + break; + + case BLE_GATTS_OP_EXEC_WRITE_REQ_NOW: + // Execute Long Write + if ( _long_wr.buffer ) + { + ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE }; + reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS; + + sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply); + + // Long write complete, call write callback if set + if (_wr_cb) _wr_cb(*this, _long_wr.buffer, _long_wr.count, 0); + + // free up memory + rtos_free(_long_wr.buffer); + _long_wr.buffer = NULL; + _long_wr.bufsize = _long_wr.count = 0; + } + break; + + case BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL: + // Cancel Long Write + break; + + case BLE_GATTS_OP_WRITE_REQ: + // Write Request with authorization + if (_wr_authorize_cb != NULL) _wr_authorize_cb(*this, &request->request.write); + break; + + default: break; + } } if ( (request->type == BLE_GATTS_AUTHORIZE_TYPE_READ) && (_rd_authorize_cb != NULL)) @@ -359,12 +432,12 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event) { ble_gatts_evt_write_t* request = &event->evt.gatts_evt.params.write; + LOG_LV2_BUFFER(NULL, request->data, request->len); + // Value write if (request->handle == _handles.value_handle) { LOG_LV2("GATTS", "attr's value, uuid = 0x%04X", request->uuid.uuid); - LOG_LV2_BUFFER(NULL, request->data, request->len); - // TODO Ada callback if (_wr_cb) { @@ -385,7 +458,6 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event) if ( request->handle == _handles.cccd_handle ) { LOG_LV2("GATTS", "attr's cccd"); - LOG_LV2_BUFFER(NULL, request->data, request->len); // Invoke callback if set if (_cccd_wr_cb) diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 16c10ece..f0b1d9e1 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -178,6 +178,12 @@ class BLECharacteristic ble_gatts_attr_md_t _attr_meta; ble_gatts_char_handles_t _handles; + struct { + uint8_t* buffer; + uint16_t bufsize; + uint16_t count; + }_long_wr; + /*------------- Callback pointers -------------*/ read_authorize_cb_t _rd_authorize_cb; write_authorize_cb_t _wr_authorize_cb; diff --git a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp index dbc35ef1..6e402665 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -461,15 +461,15 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) { // len is known to be zero for WRITE_REQ _adamsg.complete(); - }else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ) + } + else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ) { uint16_t const max_payload = Bluefruit.Gap.getMTU( _service->connHandle() ) - 3; uint16_t packet_len = min16(_adamsg.remaining, max_payload-2); - // still has data, continue to prepare if ( packet_len ) { - // Long Write Prepare + // still has data, continue to prepare Long Write sequence ble_gattc_write_params_t param = { .write_op = BLE_GATT_OP_PREP_WRITE_REQ, @@ -487,7 +487,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) } }else { - // Long Write Execute + // All data is prepared, execute Long Write ble_gattc_write_params_t param = { .write_op = BLE_GATT_OP_EXEC_WRITE_REQ, diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 103293fd..06177135 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -124,11 +124,25 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) BLECharacteristic* chr = _server.chr_list[i]; uint16_t req_handle = BLE_GATT_HANDLE_INVALID; + // BLE_GATTS_OP_EXEC_WRITE_REQ_NOW has no handle, uuid only command op + bool exec_write_now = false; + switch (evt_id) { case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + { // Handle has the same offset for read & write request req_handle = evt->evt.gatts_evt.params.authorize_request.request.read.handle; + + if ( evt->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE ) + { + ble_gatts_evt_write_t * wr_req = &evt->evt.gatts_evt.params.authorize_request.request.write; + if ( wr_req->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW ) + { + exec_write_now = true; + } + } + } break; case BLE_GATTS_EVT_WRITE: @@ -139,7 +153,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } // invoke characteristic handler if matched - if ((req_handle != BLE_GATT_HANDLE_INVALID) && (req_handle == chr->handles().value_handle || req_handle == chr->handles().cccd_handle )) + if ( exec_write_now || + ((req_handle != BLE_GATT_HANDLE_INVALID) && (req_handle == chr->handles().value_handle || req_handle == chr->handles().cccd_handle)) ) { chr->_eventHandler(evt); diff --git a/libraries/Bluefruit52Lib/src/bluefruit.cpp b/libraries/Bluefruit52Lib/src/bluefruit.cpp index 81eff500..7e7848e7 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -816,6 +816,15 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0); break; + case BLE_EVT_USER_MEM_REQUEST: + // We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ) + sd_ble_user_mem_reply(evt_conn_hdl, NULL); + break; + + case BLE_EVT_USER_MEM_RELEASE: + // nothing to do + break; + default: break; } }