Implemnt gattc long write using AdaMsg
This commit is contained in:
1 parent
c3ca58cba3
commit
8235db0ebf
2 files changed
+54
-77
No files matched your search
@@ -46,20 +46,16 @@ void BLEClientCharacteristic::_init(void)
|
||||
|
||||
_notify_cb = NULL;
|
||||
_use_AdaCallback = true;
|
||||
|
||||
_sem = NULL;
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
}
|
||||
|
||||
BLEClientCharacteristic::BLEClientCharacteristic(void)
|
||||
: uuid()
|
||||
: uuid(), _adamsg()
|
||||
{
|
||||
_init();
|
||||
}
|
||||
|
||||
BLEClientCharacteristic::BLEClientCharacteristic(BLEUuid bleuuid)
|
||||
: uuid(bleuuid)
|
||||
: uuid(bleuuid), _adamsg()
|
||||
{
|
||||
_init();
|
||||
}
|
||||
@@ -106,7 +102,7 @@ bool BLEClientCharacteristic::discoverDescriptor(uint16_t conn_handle)
|
||||
if ( disc_rsp.descs[i].uuid.type == BLE_UUID_TYPE_BLE &&
|
||||
disc_rsp.descs[i].uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG )
|
||||
{
|
||||
LOG_LV1(Discovery, "Found CCDD: handle = %d", disc_rsp.descs[i].handle);
|
||||
LOG_LV2(Discovery, "Found CCDD: handle = %d", disc_rsp.descs[i].handle);
|
||||
_cccd_handle = disc_rsp.descs[i].handle;
|
||||
}
|
||||
}
|
||||
@@ -123,6 +119,8 @@ void BLEClientCharacteristic::begin(void)
|
||||
|
||||
// Register to Bluefruit (required for callback and write response)
|
||||
(void) Bluefruit.Gatt._addCharacteristic(this);
|
||||
|
||||
_adamsg.begin(true);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
@@ -134,21 +132,21 @@ uint16_t BLEClientCharacteristic::read(void* buffer, uint16_t bufsize)
|
||||
uint16_t rxlen = 0;
|
||||
|
||||
// Semaphore to wait for BLE_GATTC_EVT_READ_RSP event
|
||||
_sem = xSemaphoreCreateBinary();
|
||||
|
||||
_evt_buf = buffer;
|
||||
_evt_bufsize = bufsize;
|
||||
|
||||
sd_ble_gattc_read(_service->connHandle(), _chr.handle_value, rxlen);
|
||||
|
||||
xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
|
||||
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
|
||||
vSemaphoreDelete(_sem);
|
||||
_sem = NULL;
|
||||
// _sem = xSemaphoreCreateBinary();
|
||||
//
|
||||
// _evt_buf = buffer;
|
||||
// _evt_bufsize = bufsize;
|
||||
//
|
||||
// sd_ble_gattc_read(_service->connHandle(), _chr.handle_value, rxlen);
|
||||
//
|
||||
// xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
//
|
||||
//
|
||||
// _evt_buf = NULL;
|
||||
// _evt_bufsize = 0;
|
||||
//
|
||||
// vSemaphoreDelete(_sem);
|
||||
// _sem = NULL;
|
||||
|
||||
return rxlen;
|
||||
}
|
||||
@@ -156,14 +154,6 @@ uint16_t BLEClientCharacteristic::read(void* buffer, uint16_t bufsize)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* WRITE
|
||||
*------------------------------------------------------------------*/
|
||||
err_t BLEClientCharacteristic::_write_and_wait_rsp(ble_gattc_write_params_t* param, uint32_t ms)
|
||||
{
|
||||
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), param) );
|
||||
|
||||
// Wait for WRITE_RESP event
|
||||
return xSemaphoreTake(_sem, ms2tick(ms) ) ? ERROR_NONE : NRF_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
{
|
||||
VERIFY( _chr.char_props.write, 0 );
|
||||
@@ -174,12 +164,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
enum { MTU_MPS = 20 } ;
|
||||
|
||||
const bool long_write = (len > MTU_MPS);
|
||||
const uint8_t* u8data = (const uint8_t*) data;
|
||||
|
||||
uint32_t status = ERROR_NONE;
|
||||
|
||||
// Write Response requires to wait for BLE_GATTC_EVT_WRITE_RSP event
|
||||
_sem = xSemaphoreCreateBinary();
|
||||
int count = 0;
|
||||
|
||||
// CMD WRITE_REQUEST for single transaction
|
||||
if ( !long_write )
|
||||
@@ -191,17 +176,18 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
.handle = _chr.handle_value,
|
||||
.offset = 0,
|
||||
.len = len,
|
||||
.p_value = (uint8_t*) u8data
|
||||
.p_value = (uint8_t*) data
|
||||
};
|
||||
|
||||
status = _write_and_wait_rsp(¶m, BLE_GENERIC_TIMEOUT);
|
||||
_adamsg.prepare( (void*) data, len);
|
||||
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), ¶m) );
|
||||
|
||||
// len is alwasy 0 in BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_WRITE_REQ
|
||||
count = (_adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT) < 0 ? 0 : len);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*------------- Long Write Sequence -------------*/
|
||||
_evt_buf = (void*) data;
|
||||
_evt_bufsize = len;
|
||||
|
||||
ble_gattc_write_params_t param =
|
||||
{
|
||||
.write_op = BLE_GATT_OP_PREP_WRITE_REQ,
|
||||
@@ -212,20 +198,15 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
.p_value = (uint8_t*) data
|
||||
};
|
||||
|
||||
status = _write_and_wait_rsp(¶m, (len/(MTU_MPS-2)+1) * BLE_GENERIC_TIMEOUT);
|
||||
|
||||
_evt_buf = NULL;
|
||||
_evt_bufsize = 0;
|
||||
_adamsg.prepare( (void*) data, len);
|
||||
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), ¶m) );
|
||||
count = _adamsg.waitUntilComplete( (len/(MTU_MPS-2)+1) * BLE_GENERIC_TIMEOUT );
|
||||
|
||||
// delay to swallow last WRITE RESPONSE
|
||||
// delay(20);
|
||||
}
|
||||
|
||||
vSemaphoreDelete(_sem);
|
||||
_sem = NULL;
|
||||
|
||||
VERIFY_STATUS(status, 0);
|
||||
return len;
|
||||
return (count < 0) ? 0 : count;
|
||||
}
|
||||
|
||||
uint16_t BLEClientCharacteristic::write(const void* data, uint16_t len)
|
||||
@@ -353,19 +334,24 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
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;
|
||||
(void) wr_rsp;
|
||||
|
||||
// Give up if failed
|
||||
// if ( gatt_status != BLE_GATT_STATUS_SUCCESS )
|
||||
// {
|
||||
// if (_sem) xSemaphoreGive(_sem);
|
||||
// break;
|
||||
// }
|
||||
|
||||
if ( wr_rsp->write_op == BLE_GATT_OP_WRITE_REQ)
|
||||
{
|
||||
if (_sem) xSemaphoreGive(_sem);
|
||||
_adamsg.feed(NULL, wr_rsp->len); // len is known to be zero
|
||||
_adamsg.complete();
|
||||
}else if ( wr_rsp->write_op == BLE_GATT_OP_PREP_WRITE_REQ)
|
||||
{
|
||||
enum { MTU_MPS = 20 } ;
|
||||
|
||||
_evt_buf += wr_rsp->len;
|
||||
_evt_bufsize -= wr_rsp->len;
|
||||
|
||||
uint16_t packet_len = min16(_evt_bufsize, MTU_MPS-2);
|
||||
_adamsg.feed(NULL, wr_rsp->len);
|
||||
uint16_t packet_len = min16(_adamsg.remaining, MTU_MPS-2);
|
||||
|
||||
// still has data, continue to prepare
|
||||
if ( packet_len )
|
||||
@@ -378,15 +364,13 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
.handle = _chr.handle_value,
|
||||
.offset = (uint16_t) wr_rsp->offset + wr_rsp->len,
|
||||
.len = packet_len,
|
||||
.p_value = (uint8_t*) _evt_buf
|
||||
.p_value = (uint8_t*) _adamsg.buffer
|
||||
};
|
||||
|
||||
uint32_t status = sd_ble_gattc_write(_service->connHandle(), ¶m);
|
||||
|
||||
if ( ERROR_NONE != status )
|
||||
// give up if cannot write
|
||||
if ( ERROR_NONE != sd_ble_gattc_write(_service->connHandle(), ¶m) )
|
||||
{
|
||||
// give up if cannot write
|
||||
if (_sem) xSemaphoreGive(_sem);
|
||||
_adamsg.complete();
|
||||
}
|
||||
}else
|
||||
{
|
||||
@@ -403,7 +387,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
// Last BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_EXEC_WRITE_REQ does not
|
||||
// contain characteristic's handle. Therefore BLEGatt couldn't forward the
|
||||
// event to us. Just skip the wait for now
|
||||
if (_sem) xSemaphoreGive(_sem);
|
||||
_adamsg.complete();
|
||||
}
|
||||
}else
|
||||
{
|
||||
@@ -417,15 +401,13 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
|
||||
ble_gattc_evt_read_rsp_t* rd_rsp = (ble_gattc_evt_read_rsp_t*) &evt->evt.gattc_evt.params.read_rsp;
|
||||
|
||||
// process is complete if len is less than MTU or we get BLE_GATT_STATUS_ATTERR_INVALID_OFFSET
|
||||
// if (gatt_status == BLE_GATT_STATUS_SUCCESS)
|
||||
// {
|
||||
if (gatt_status == BLE_GATT_STATUS_SUCCESS)
|
||||
{
|
||||
// if ( _evt_bufsize && _evt_buf )
|
||||
// {
|
||||
//// memcpy(_evt_buf + , rd_rsp->data, rd_rsp->len);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (_sem) xSemaphoreGive(_sem);
|
||||
{
|
||||
// memcpy(_evt_buf + , rd_rsp->data, rd_rsp->len);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
Reference in new issue
Block a user