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 73e16b2a..5bc9fb17 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -59,6 +59,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; @@ -344,26 +348,98 @@ 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) { - if ( _use_ada_cb.write_authorize ) - { - uint8_t* data = (uint8_t*) rtos_malloc(sizeof(request->request.write)); - VERIFY(data,); + ble_gatts_evt_write_t * wr_req = &request->request.write; - memcpy(data, &request->request.write, sizeof(request->request.write)); - - // data is free after callback - ada_callback(data, _wr_authorize_cb, this, data); - }else + switch(wr_req->op) { - _wr_authorize_cb(this, &request->request.write); + 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) + { + if ( _use_ada_cb.write_authorize ) + { + uint8_t* data = (uint8_t*) rtos_malloc(sizeof(request->request.write)); + VERIFY(data,); + + memcpy(data, &request->request.write, sizeof(request->request.write)); + + // data is free after callback + ada_callback(data, _wr_authorize_cb, this, data); + }else + { + _wr_authorize_cb(this, &request->request.write); + } + } + break; + + default: break; } } @@ -391,11 +467,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); if (_wr_cb) { @@ -418,7 +495,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); if (_cccd_wr_cb) { diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index f6a2cb1c..540f1612 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -184,6 +184,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 6b78e044..f806cf1b 100644 --- a/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLEClientCharacteristic.cpp @@ -478,10 +478,9 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt) uint16_t const max_payload = conn->getMtu() - 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, @@ -499,7 +498,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 e81a1c1d..ebb014ed 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -112,11 +112,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: @@ -127,7 +141,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 f4fa12cb..abc82a00 100644 --- a/libraries/Bluefruit52Lib/src/bluefruit.cpp +++ b/libraries/Bluefruit52Lib/src/bluefruit.cpp @@ -919,6 +919,15 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt) } break; + case BLE_EVT_USER_MEM_REQUEST: + // We will handle Long Write sequence (RW Authorize PREP_WRITE_REQ) + sd_ble_user_mem_reply(conn_hdl, NULL); + break; + + case BLE_EVT_USER_MEM_RELEASE: + // nothing to do + break; + default: break; }