add connect/disconnect callback #8
This commit is contained in:
1 parent
0bbb930286
commit
3148b1ec88
2 files changed
+25
-3
No files matched your search
@@ -98,9 +98,11 @@ AdafruitBluefruit::AdafruitBluefruit(void)
|
||||
_chars_count = 0;
|
||||
for(uint8_t i=0; i<BLE_MAX_CHARS; i++) _chars_list[i] = NULL;
|
||||
|
||||
|
||||
varclr(&_enc_key);
|
||||
varclr(&_peer_id);
|
||||
|
||||
_connect_cb = NULL;
|
||||
_discconnect_cb = NULL;
|
||||
}
|
||||
|
||||
err_t AdafruitBluefruit::begin(void)
|
||||
@@ -197,6 +199,16 @@ void AdafruitBluefruit::disconnect(void)
|
||||
}
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::setConnectCallback ( void (*fp) (uint32_t hostID) )
|
||||
{
|
||||
_connect_cb = fp;
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::setDisconnectCallback( void (*fp) (uint32_t hostID, uint8_t reason))
|
||||
{
|
||||
_discconnect_cb = fp;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Advertising
|
||||
*------------------------------------------------------------------*/
|
||||
@@ -510,12 +522,14 @@ void AdafruitBluefruit::_poll(void)
|
||||
case BLE_GAP_EVT_CONNECTED:
|
||||
if (_led_conn) digitalWrite(LED_CONN, HIGH);
|
||||
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
_conn_hdl = evt->evt.gap_evt.conn_handle;
|
||||
_peer_addr = evt->evt.gap_evt.params.connected.peer_addr;
|
||||
|
||||
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);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
@@ -526,6 +540,8 @@ void AdafruitBluefruit::_poll(void)
|
||||
|
||||
vSemaphoreDelete(_txbuf_sem);
|
||||
|
||||
if ( _discconnect_cb ) _discconnect_cb(0, evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
startAdvertising();
|
||||
break;
|
||||
|
||||
|
||||
@@ -101,7 +101,6 @@ class AdafruitBluefruit
|
||||
err_t startAdvertising(void);
|
||||
void stopAdvertising(void);
|
||||
|
||||
|
||||
// Add service without using BLEService instance
|
||||
err_t addService(uint16_t uuid16);
|
||||
err_t addService(uint8_t const uuid128[]);
|
||||
@@ -114,6 +113,10 @@ class AdafruitBluefruit
|
||||
void disconnect(void);
|
||||
bool connected(void);
|
||||
|
||||
/*------------- Callback -------------*/
|
||||
void setConnectCallback ( void (*fp) (uint32_t hostID) );
|
||||
void setDisconnectCallback( void (*fp) (uint32_t hostID, uint8_t reason) );
|
||||
|
||||
// internal usage only
|
||||
err_t _registerCharacteristic(BLECharacteristic* chars);
|
||||
void _sd_event_isr(void);
|
||||
@@ -148,6 +151,9 @@ class AdafruitBluefruit
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
SemaphoreHandle_t _txbuf_sem;
|
||||
|
||||
void (*_connect_cb) (uint32_t hostID);
|
||||
void (*_discconnect_cb) (uint32_t hostID, uint8_t reason);
|
||||
|
||||
bool _addToAdv(bool scan_resp, uint8_t type, const void* data, uint8_t len);
|
||||
void _poll(void);
|
||||
|
||||
|
||||
Reference in new issue
Block a user