add connection handel to BLECentralService

- add  multi-connection support to
BLECentralService/BLECentralCharacteristic
This commit is contained in:
hathach committed 2017-03-30 23:07:41 +07:00
1 parent f4b4ec0470
commit b7f33a2e64
5 files changed
+23 -40

No files matched your search

@@ -45,8 +45,6 @@ BLECentral::BLECentral(void)
{
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_txpacket_sem = NULL;
_scan_cb = NULL;
_scan_param = (ble_gap_scan_params_t) {
.active = 1,
@@ -68,12 +66,6 @@ void BLECentral::begin(void)
Bluefruit.Discovery.begin();
}
bool BLECentral::getTxPacket(uint32_t ms)
{
VERIFY(_txpacket_sem != NULL);
return xSemaphoreTake(_txpacket_sem, ms2tick(ms));
}
/*------------------------------------------------------------------*/
/* Scan and Parser
*------------------------------------------------------------------*/
@@ -252,11 +244,6 @@ 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();
}
}
@@ -267,25 +254,11 @@ 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)
{
@@ -83,8 +83,6 @@ class BLECentral
bool connected (void);
uint16_t connHandle (void);
bool getTxPacket(uint32_t ms);
/*------------------------------------------------------------------*/
/* CALLBACKS
*------------------------------------------------------------------*/
@@ -97,9 +95,6 @@ class BLECentral
private:
uint16_t _conn_hdl;
// Transmission Buffer Count for HVX notification, max is seen at 7
SemaphoreHandle_t _txpacket_sem;
ble_gap_scan_params_t _scan_param;
scan_callback_t _scan_cb;
@@ -171,13 +171,15 @@ uint16_t BLECentralCharacteristic::write(const void* data, int len)
// SD132 v3.0 could negotiate MTU to higher number
const uint16_t MTU_MPS = 20;
const uint16_t conn_handle = _service->connHandle();
const uint8_t* u8data = (const uint8_t*) data;
int remaining = len;
while( remaining )
{
// Write CMD consume a TX buffer
if ( !Bluefruit.Central.getTxPacket(100) ) return BLE_ERROR_NO_TX_PACKETS;
if ( !Bluefruit.Gap.getTxPacket(conn_handle) ) return BLE_ERROR_NO_TX_PACKETS;
uint16_t packet_len = min16(MTU_MPS, remaining);
@@ -191,7 +193,7 @@ uint16_t BLECentralCharacteristic::write(const void* data, int len)
.p_value = (uint8_t* ) u8data
};
VERIFY_STATUS( sd_ble_gattc_write(Bluefruit.Central.connHandle(), &param), len - remaining );
VERIFY_STATUS( sd_ble_gattc_write(conn_handle, &param), len - remaining );
remaining -= packet_len;
u8data += packet_len;
@@ -207,6 +209,8 @@ void BLECentralCharacteristic::setNotifyCallback(notify_cb_t fp)
bool BLECentralCharacteristic::writeCCCD(uint16_t value)
{
const uint16_t conn_handle = _service->connHandle();
ble_gattc_write_params_t param =
{
.write_op = BLE_GATT_OP_WRITE_CMD,
@@ -218,9 +222,9 @@ bool BLECentralCharacteristic::writeCCCD(uint16_t value)
};
// Write consume a TX buffer
if ( !Bluefruit.Central.getTxPacket(100) ) return BLE_ERROR_NO_TX_PACKETS;
if ( !Bluefruit.Gap.getTxPacket(conn_handle) ) return BLE_ERROR_NO_TX_PACKETS;
VERIFY_STATUS( sd_ble_gattc_write(Bluefruit.Central.connHandle(), &param), false );
VERIFY_STATUS( sd_ble_gattc_write(conn_handle, &param), false );
return true;
}
@@ -42,6 +42,7 @@ BLECentralService* BLECentralService::lastService = NULL;
void BLECentralService::_init(void)
{
_discovered = false;
_conn_hdl = BLE_CONN_HANDLE_INVALID;
}
BLECentralService::BLECentralService(void)
@@ -70,6 +71,7 @@ bool BLECentralService::begin(void)
bool BLECentralService::discover(uint16_t conn_handle)
{
_conn_hdl = conn_handle;
VERIFY( Bluefruit.Discovery._discoverService(conn_handle, *this) );
_discovered = true;
return true;
@@ -80,8 +82,14 @@ bool BLECentralService::discovered(void)
return _discovered;
}
uint16_t BLECentralService::connHandle(void)
{
return _conn_hdl;
}
void BLECentralService::disconnect(void)
{
_discovered = false;
_conn_hdl = BLE_CONN_HANDLE_INVALID;
// inherited service may want to clean up its own characteristic
}
@@ -42,7 +42,8 @@
class BLECentralService
{
protected:
bool _discovered;
bool _discovered;
uint16_t _conn_hdl;
void _init(void);
virtual void disconnect(void);
@@ -55,10 +56,12 @@ class BLECentralService
BLECentralService(void);
BLECentralService(BLEUuid bleuuid);
virtual bool begin(void);
virtual bool begin(void);
virtual bool discover (uint16_t conn_handle);
bool discovered(void);
virtual bool discover (uint16_t conn_handle);
bool discovered(void);
uint16_t connHandle(void);
friend class BLEGatt;
};