Implement Central Connect, callback for connect/disconnect
This commit is contained in:
1 parent
1e2b632998
commit
a3bb011e0e
4 files changed
+108
-28
No files matched your search
@@ -41,6 +41,8 @@
|
||||
*/
|
||||
BLECentral::BLECentral(void)
|
||||
{
|
||||
_conn_hdl = 0;
|
||||
|
||||
_scan_cb = NULL;
|
||||
_scan_param = (ble_gap_scan_params_t) {
|
||||
.active = 1,
|
||||
@@ -50,6 +52,9 @@ BLECentral::BLECentral(void)
|
||||
.window = 0x0050,
|
||||
.timeout = 0, // no timeout
|
||||
};
|
||||
|
||||
_connect_cb = NULL;
|
||||
_discconnect_cb = NULL;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
@@ -60,8 +65,9 @@ void BLECentral::setScanCallback(scan_callback_t fp)
|
||||
_scan_cb = fp;
|
||||
}
|
||||
|
||||
err_t BLECentral::startScanning(void)
|
||||
err_t BLECentral::startScanning(uint16_t timeout)
|
||||
{
|
||||
_scan_param.timeout = timeout;
|
||||
VERIFY_STATUS( sd_ble_gap_scan_start(&_scan_param) );
|
||||
Bluefruit._startConnLed(); // start blinking
|
||||
return ERROR_NONE;
|
||||
@@ -169,6 +175,21 @@ err_t BLECentral::connect(const ble_gap_evt_adv_report_t* adv_report, uint16_t m
|
||||
return connect(&adv_report->peer_addr, min_conn_interval, max_conn_interval);
|
||||
}
|
||||
|
||||
bool BLECentral::connected(void)
|
||||
{
|
||||
return (_conn_hdl != BLE_GATT_HANDLE_INVALID);
|
||||
}
|
||||
|
||||
void BLECentral::setConnectCallback( connect_callback_t fp)
|
||||
{
|
||||
_connect_cb = fp;
|
||||
}
|
||||
|
||||
void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
|
||||
{
|
||||
_discconnect_cb = fp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event is forwarded from Bluefruit Poll() method
|
||||
* @param event
|
||||
@@ -184,6 +205,43 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
{
|
||||
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
|
||||
|
||||
if (para->role == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
Bluefruit._stopConnLed();
|
||||
if (Bluefruit._led_conn) ledOn(LED_CONN);
|
||||
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
|
||||
if ( _connect_cb ) _connect_cb();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
if ( _conn_hdl == evt->evt.gap_evt.conn_handle)
|
||||
{
|
||||
if (Bluefruit._led_conn) ledOff(LED_CONN);
|
||||
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
|
||||
if ( _discconnect_cb ) _discconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
startScanning();
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN)
|
||||
{
|
||||
// Restart Scanning
|
||||
startScanning();
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,8 @@ class BLECentral
|
||||
*------------------------------------------------------------------*/
|
||||
typedef void (*scan_callback_t) (ble_gap_evt_adv_report_t*);
|
||||
|
||||
void setScanCallback(scan_callback_t fp);
|
||||
err_t startScanning(void);
|
||||
void setScanCallback(scan_callback_t fp);
|
||||
err_t startScanning(uint16_t timeout = 0);
|
||||
err_t stopScanning(void);
|
||||
|
||||
uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len);
|
||||
@@ -76,9 +76,22 @@ class BLECentral
|
||||
uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT,
|
||||
uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT);
|
||||
|
||||
bool connected(void);
|
||||
|
||||
typedef void (*connect_callback_t) (void);
|
||||
typedef void (*disconnect_callback_t) (uint8_t reason);
|
||||
|
||||
void setConnectCallback ( connect_callback_t fp);
|
||||
void setDisconnectCallback( disconnect_callback_t fp);
|
||||
|
||||
private:
|
||||
uint16_t _conn_hdl;
|
||||
|
||||
ble_gap_scan_params_t _scan_param;
|
||||
scan_callback_t _scan_cb;
|
||||
scan_callback_t _scan_cb;
|
||||
|
||||
connect_callback_t _connect_cb;
|
||||
disconnect_callback_t _discconnect_cb;
|
||||
|
||||
bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len);
|
||||
|
||||
|
||||
@@ -136,9 +136,9 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
|
||||
{
|
||||
.common_enable_params = { .vs_uuid_count = BLE_VENDOR_UUID_MAX },
|
||||
.gap_enable_params = {
|
||||
.periph_conn_count = _prph_enabled ? 1 : 0,
|
||||
.central_conn_count = _central_enabled ? BLE_CENTRAL_MAX_CONN : 0,
|
||||
.central_sec_count = _central_enabled ? BLE_CENTRAL_MAX_SECURE_CONN : 0,
|
||||
.periph_conn_count = (uint8_t) (_prph_enabled ? 1 : 0),
|
||||
.central_conn_count = (uint8_t) (_central_enabled ? BLE_CENTRAL_MAX_CONN : 0),
|
||||
.central_sec_count = (uint8_t) (_central_enabled ? BLE_CENTRAL_MAX_SECURE_CONN : 0),
|
||||
},
|
||||
.gatts_enable_params = {
|
||||
.service_changed = 1,
|
||||
@@ -243,12 +243,12 @@ void AdafruitBluefruit::disconnect(void)
|
||||
}
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::setConnectCallback ( void (*fp) (uint32_t hostID) )
|
||||
void AdafruitBluefruit::setConnectCallback ( connect_callback_t fp )
|
||||
{
|
||||
_connect_cb = fp;
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::setDisconnectCallback( void (*fp) (uint32_t hostID, uint8_t reason))
|
||||
void AdafruitBluefruit::setDisconnectCallback( disconnect_callback_t fp )
|
||||
{
|
||||
_discconnect_cb = fp;
|
||||
}
|
||||
@@ -605,36 +605,42 @@ void AdafruitBluefruit::_poll(void)
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
{
|
||||
// PRINT_MESS("Connected");
|
||||
|
||||
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
|
||||
|
||||
_stopConnLed();
|
||||
if (_led_conn) ledOn(LED_CONN);
|
||||
if (para->role == BLE_GAP_ROLE_PERIPH)
|
||||
{
|
||||
_stopConnLed();
|
||||
if (_led_conn) ledOn(LED_CONN);
|
||||
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
_peer_addr = para->peer_addr;
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
_peer_addr = para->peer_addr;
|
||||
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
|
||||
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(_conn_hdl, &txbuf_max);
|
||||
_txbuf_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
|
||||
if ( _connect_cb ) _connect_cb(0);
|
||||
if ( _connect_cb ) _connect_cb();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
// PRINT_MESS("Disconnected");
|
||||
|
||||
if (_led_conn) ledOff(LED_CONN);
|
||||
// Check if it is peripheral connection
|
||||
if (_conn_hdl == evt->evt.gap_evt.conn_handle)
|
||||
{
|
||||
if (_led_conn) ledOff(LED_CONN);
|
||||
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
varclr(&_peer_addr);
|
||||
_conn_hdl = BLE_GATT_HANDLE_INVALID;
|
||||
varclr(&_peer_addr);
|
||||
|
||||
vSemaphoreDelete(_txbuf_sem);
|
||||
vSemaphoreDelete(_txbuf_sem);
|
||||
|
||||
if ( _discconnect_cb ) _discconnect_cb(0, evt->evt.gap_evt.params.disconnected.reason);
|
||||
if ( _discconnect_cb ) _discconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
startAdvertising();
|
||||
startAdvertising();
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
|
||||
@@ -130,8 +130,11 @@ class AdafruitBluefruit
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Callback
|
||||
*------------------------------------------------------------------*/
|
||||
void setConnectCallback ( void (*fp) (uint32_t hostID) );
|
||||
void setDisconnectCallback( void (*fp) (uint32_t hostID, uint8_t reason) );
|
||||
typedef void (*connect_callback_t) (void);
|
||||
typedef void (*disconnect_callback_t) (uint8_t reason);
|
||||
|
||||
void setConnectCallback ( connect_callback_t fp);
|
||||
void setDisconnectCallback( disconnect_callback_t fp);
|
||||
|
||||
// internal usage only
|
||||
err_t _registerCharacteristic(BLECharacteristic* chars);
|
||||
@@ -176,8 +179,8 @@ class AdafruitBluefruit
|
||||
SemaphoreHandle_t _txbuf_sem;
|
||||
|
||||
/*------------- Callbacks -------------*/
|
||||
void (*_connect_cb) (uint32_t hostID);
|
||||
void (*_discconnect_cb) (uint32_t hostID, uint8_t reason);
|
||||
connect_callback_t _connect_cb;
|
||||
disconnect_callback_t _discconnect_cb;
|
||||
|
||||
bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len);
|
||||
void _poll(void);
|
||||
|
||||
Reference in new issue
Block a user