wrap up gattc long write

This commit is contained in:
hathach committed 2017-04-12 17:47:40 +07:00
1 parent c718eff435
commit 482f0e987b
2 files changed
+20 -14

No files matched your search

@@ -137,13 +137,15 @@ err_t BLEClientCharacteristic::_write_and_wait_rsp(ble_gattc_write_params_t* par
return xSemaphoreTake(_sem, ms2tick(BLE_GENERIC_TIMEOUT) ) ? ERROR_NONE : NRF_ERROR_TIMEOUT;
}
uint16_t BLEClientCharacteristic::write_resp(const void* data, int len)
uint16_t BLEClientCharacteristic::write_resp(const void* data, int len32)
{
// 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
// For BLE_GATT_OP_PREP_WRITE_REQ, max data is 18 ( 2 byte is used for offset)
enum { MTU_MPS = 20 } ;
uint16_t len = (uint16_t) len32;
const bool long_write = (len > MTU_MPS);
const uint8_t* u8data = (const uint8_t*) data;
@@ -162,7 +164,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len)
.handle = _chr.handle_value,
.offset = 0,
.len = len,
.p_value = (uint8_t* ) u8data
.p_value = (uint8_t*) u8data
};
status = _write_and_wait_rsp(&param);
@@ -172,7 +174,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len)
/*------------- Long Write Prepare -------------*/
const uint16_t max_packet = MTU_MPS - 2; // 2 bytes for offset
int remaining = len;
uint16_t remaining = len;
while( remaining )
{
uint16_t packet_len = min16(max_packet, remaining);
@@ -182,12 +184,11 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len)
.write_op = BLE_GATT_OP_PREP_WRITE_REQ,
.flags = 0,
.handle = _chr.handle_value,
.offset = (uint16_t) len-remaining,
.offset = len-remaining,
.len = packet_len,
.p_value = (uint8_t* ) u8data
.p_value = (uint8_t*) u8data
};
status = _write_and_wait_rsp(&param);
if ( ERROR_NONE != status ) break;
remaining -= packet_len;
@@ -205,13 +206,18 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, int len)
.p_value = NULL
};
status = _write_and_wait_rsp(&param);
// Last BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_EXEC_WRITE_REQ does not
// contain characteristic's handle. Therefor BLEGatt couldn't forward the
// event to us. Just skip the wait for now
// status = _write_and_wait_rsp(&param);
status = sd_ble_gattc_write(_service->connHandle(), &param);
}
vSemaphoreDelete(_sem);
_sem = NULL;
return (status == ERROR_NONE) ? (uint16_t) len : 0;
VERIFY_STATUS(status, 0);
return len;
}
uint16_t BLEClientCharacteristic::write(const void* data, int len)
@@ -338,7 +344,7 @@ void BLEClientCharacteristic::_eventHandler(ble_evt_t* evt)
if (_sem)
{
// TODO check evt->evt.gattc_evt.gatt_status
if (_sem) xSemaphoreGive(_sem);
xSemaphoreGive(_sem);
}
}
break;