Merge branch 'master' into develop

This commit is contained in:
hathach committed 2019-02-13 22:25:26 +07:00
commit 333f151ff5
6 files changed
+123 -18

No files matched your search

+1 -1
View File
@@ -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;
@@ -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)
{
@@ -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;
@@ -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,
+16 -1
View File
@@ -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);
@@ -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;
}