add characteristic write, write_resp() support

This commit is contained in:
hathach committed 2017-03-29 00:38:54 +07:00
1 parent 1f2efb042a
commit f08759ad64
4 files changed
+176 -29

No files matched your search

+44 -2
View File
@@ -36,7 +36,7 @@
#include "bluefruit.h"
#define BLE_CENTRAL_TIMEOUT 10000
#define BLE_CENTRAL_TIMEOUT 3000
/**
* Constructor
@@ -49,6 +49,8 @@ BLECentral::BLECentral(void)
_evt_buf = NULL;
_evt_bufsize = 0;
_txpacket_sem = NULL;
_chars_count = 0;
for(uint8_t i=0; i<BLE_CENTRAL_MAX_CHARS; i++) _chars_list[i] = NULL;
@@ -82,6 +84,12 @@ bool BLECentral::_registerCharacteristic(BLECentralCharacteristic* chr)
return true;
}
bool BLECentral::getTxPacket(uint32_t ms)
{
VERIFY(_txpacket_sem != NULL);
return xSemaphoreTake(_txpacket_sem, ms2tick(ms));
}
/*------------------------------------------------------------------*/
/* Scan and Parser
*------------------------------------------------------------------*/
@@ -347,6 +355,11 @@ void BLECentral::_event_handler(ble_evt_t* evt)
// TODO multiple connections
_conn_hdl = evt->evt.gap_evt.conn_handle;
// Init transmission buffer for notification. TODO multiple connections
uint8_t txbuf_max;
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
_txpacket_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
if ( _connect_cb ) _connect_cb();
}
}
@@ -357,11 +370,25 @@ void BLECentral::_event_handler(ble_evt_t* evt)
_conn_hdl = BLE_CONN_HANDLE_INVALID;
// TODO multiple connections
vSemaphoreDelete(_txpacket_sem);
_txpacket_sem = NULL;
if ( _disconnect_cb ) _disconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
startScanning();
break;
case BLE_EVT_TX_COMPLETE:
if ( _txpacket_sem )
{
for(uint8_t i=0; i<evt->evt.common_evt.params.tx_complete.count; i++)
{
xSemaphoreGive(_txpacket_sem);
}
}
break;
case BLE_GAP_EVT_TIMEOUT:
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
{
@@ -458,7 +485,22 @@ void BLECentral::_event_handler(ble_evt_t* evt)
{
for(int i=0; i<_chars_count; i++)
{
_chars_list[i]->_eventHandler(evt);
bool matched = false;
switch(evt->header.evt_id)
{
case BLE_GATTC_EVT_HVX:
case BLE_GATTC_EVT_WRITE_RSP:
case BLE_GATTC_EVT_READ_RSP:
// write & read response's handle has same offset.
matched = (_chars_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle);
break;
default: break;
}
// invoke charactersistic handler if matched
if ( matched ) _chars_list[i]->_eventHandler(evt);
}
}
}