enhance indicat API() to wait for confirm or timeout from peer.
This commit is contained in:
1 parent
fc2e3603c8
commit
9e0a6ff775
4 files changed
+68
-43
No files matched your search
@@ -619,7 +619,9 @@ bool BLECharacteristic::indicate(const void* data, uint16_t len)
|
||||
|
||||
if ( indicateEnabled() )
|
||||
{
|
||||
uint16_t const max_payload = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3;
|
||||
uint16_t conn_hdl = Bluefruit.connHandle();
|
||||
|
||||
uint16_t const max_payload = Bluefruit.Gap.getMTU( conn_hdl ) - 3;
|
||||
const uint8_t* u8data = (const uint8_t*) data;
|
||||
|
||||
while ( remaining )
|
||||
@@ -636,16 +638,10 @@ bool BLECharacteristic::indicate(const void* data, uint16_t len)
|
||||
};
|
||||
|
||||
LOG_LV2("CHR", "Indicate %d bytes", packet_len);
|
||||
err_t err;
|
||||
|
||||
// If previous indicate is still in progress, wait for a while and try again
|
||||
while ( NRF_ERROR_BUSY == (err = sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params)) )
|
||||
{
|
||||
// TODO should wait for BLE_GATTS_EVT_HVC event
|
||||
delay( Bluefruit.connInterval() );
|
||||
}
|
||||
|
||||
VERIFY_STATUS( err, false );
|
||||
// Blocking wait until receiving confirmation from peer
|
||||
VERIFY_STATUS( sd_ble_gatts_hvx( conn_hdl, &hvx_params), false );
|
||||
VERIFY ( Bluefruit.Gatt.waitForIndicateConfirm(conn_hdl) );
|
||||
|
||||
remaining -= packet_len;
|
||||
u8data += packet_len;
|
||||
|
||||
@@ -105,11 +105,17 @@ class BLEGap
|
||||
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
|
||||
SemaphoreHandle_t indicate_confirm_sem;
|
||||
bool hvc_received;
|
||||
} gap_peer_t;
|
||||
|
||||
gap_peer_t _peers[BLE_MAX_CONN];
|
||||
|
||||
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
friend class BLEGatt;
|
||||
};
|
||||
|
||||
#endif /* BLEGAP_H_ */
|
||||
@@ -74,24 +74,20 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
|
||||
return (count < 0) ? 0 : count;
|
||||
}
|
||||
|
||||
//bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
|
||||
//{
|
||||
// while( _adamsg.isWaiting() )
|
||||
// {
|
||||
// // TODO multiple peripherals
|
||||
// delay( Bluefruit.connInterval() );
|
||||
// }
|
||||
//
|
||||
// _adamsg.begin(true);
|
||||
// _adamsg.prepare(NULL, 0);
|
||||
//
|
||||
// // Wait for HVC event
|
||||
// int32_t result = _adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT);
|
||||
//
|
||||
// _adamsg.stop();
|
||||
//
|
||||
// return (result == 0);
|
||||
//}
|
||||
bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
|
||||
{
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(conn_hdl);
|
||||
|
||||
// hvi confirm semaphore is created on the fly
|
||||
peer->indicate_confirm_sem = xSemaphoreCreateBinary();
|
||||
|
||||
xSemaphoreTake(peer->indicate_confirm_sem, portMAX_DELAY);
|
||||
|
||||
vSemaphoreDelete(peer->indicate_confirm_sem);
|
||||
peer->indicate_confirm_sem = NULL;
|
||||
|
||||
return peer->hvc_received;
|
||||
}
|
||||
|
||||
void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
@@ -204,25 +200,51 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
}
|
||||
|
||||
// GATTC Read Characteristic by UUID procedure
|
||||
if ( evt_id == BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP )
|
||||
switch ( evt_id )
|
||||
{
|
||||
ble_gattc_evt_char_val_by_uuid_read_rsp_t* rd_rsp = &evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp;
|
||||
|
||||
if (rd_rsp->count)
|
||||
case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
|
||||
{
|
||||
#if SD_VER < 500
|
||||
_adamsg.feed(rd_rsp->handle_value[0].p_value, rd_rsp->value_len);
|
||||
#else
|
||||
ble_gattc_handle_value_t hdl_value;
|
||||
ble_gattc_evt_char_val_by_uuid_read_rsp_t* rd_rsp = &evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp;
|
||||
|
||||
if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) )
|
||||
{
|
||||
_adamsg.feed(hdl_value.p_value, rd_rsp->value_len);
|
||||
}
|
||||
#endif
|
||||
if (rd_rsp->count)
|
||||
{
|
||||
#if SD_VER < 500
|
||||
_adamsg.feed(rd_rsp->handle_value[0].p_value, rd_rsp->value_len);
|
||||
#else
|
||||
ble_gattc_handle_value_t hdl_value;
|
||||
|
||||
_adamsg.complete();
|
||||
if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) )
|
||||
{
|
||||
_adamsg.feed(hdl_value.p_value, rd_rsp->value_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
_adamsg.complete();
|
||||
}
|
||||
}
|
||||
|
||||
case BLE_GATTS_EVT_HVC:
|
||||
{
|
||||
LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle);
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
|
||||
|
||||
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
|
||||
peer->hvc_received = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTS_EVT_TIMEOUT:
|
||||
{
|
||||
LOG_LV2("GATTS", "Timeout Source = %d", evt->evt.gatts_evt.params.timeout.src);
|
||||
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
|
||||
|
||||
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
|
||||
peer->hvc_received = false;
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,8 @@ class BLEGatt
|
||||
BLEGatt(void);
|
||||
|
||||
uint16_t readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffer, uint16_t bufsize, uint16_t start_hdl = 1, uint16_t end_hdl = 0xffff);
|
||||
// bool waitForIndicateConfirm(uint16_t conn_hdl);
|
||||
|
||||
bool waitForIndicateConfirm(uint16_t conn_hdl);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
|
||||
Reference in new issue
Block a user