move callback from BLEGap back to Bluefruit & BLECentral

This commit is contained in:
hathach committed 2017-07-15 00:38:25 +07:00
1 parent 5c4869f2a0
commit d7fe49b4c3
6 files changed
+35 -70

No files matched your search

+13 -9
View File
@@ -44,6 +44,9 @@ BLECentral::BLECentral(void)
{
_ppcp_min_conn = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
_ppcp_max_conn = BLE_GAP_CONN_MAX_INTERVAL_DFLT;
_connect_cb = NULL;
_disconnect_cb = NULL;
}
void BLECentral::begin(void)
@@ -123,15 +126,14 @@ bool BLECentral::connected(void)
void BLECentral::setConnectCallback( BLEGap::connect_callback_t fp)
{
Bluefruit.Gap.setConnectCallback(fp, BLE_GAP_ROLE_CENTRAL);
_connect_cb = fp;
}
void BLECentral::setDisconnectCallback( BLEGap::disconnect_callback_t fp)
{
Bluefruit.Gap.setDisconnectCallback(fp, BLE_GAP_ROLE_CENTRAL);
_disconnect_cb = fp;
}
/**
* Event is forwarded from Bluefruit Poll() method
* @param event
@@ -147,17 +149,19 @@ void BLECentral::_event_handler(ble_evt_t* evt)
switch ( evt->header.evt_id )
{
case BLE_GAP_EVT_CONNECTED:
{ // Note callback is invoked by BLEGap
ble_gap_evt_connected_t* para = &evt->evt.gap_evt.params.connected;
if (para->role == BLE_GAP_ROLE_CENTRAL)
if ( Bluefruit.Gap.getRole(evt_conn_hdl) == BLE_GAP_ROLE_CENTRAL)
{
// Invoke callback
if ( _connect_cb) ada_callback(NULL, _connect_cb, evt_conn_hdl);
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
// Note callback is invoked by BLEGap
if ( Bluefruit.Gap.getRole(evt_conn_hdl) == BLE_GAP_ROLE_CENTRAL)
{
// Invoke callback
if ( _disconnect_cb) ada_callback(NULL, _disconnect_cb, evt_conn_hdl);
}
break;
default: break;
+4 -3
View File
@@ -49,7 +49,6 @@
#include "BLEClientService.h"
class AdafruitBluefruit;
class BLECentral
@@ -67,7 +66,6 @@ class BLECentral
bool connect(const ble_gap_evt_adv_report_t* adv_report);
bool connect(const ble_gap_addr_t *peer_addr);
bool disconnect(uint16_t conn_handle);
bool connected (uint16_t conn_handle); // If connected to a specific peripheral
@@ -82,7 +80,10 @@ class BLECentral
uint16_t _ppcp_min_conn;
uint16_t _ppcp_max_conn;
void _event_handler(ble_evt_t* evt);
BLEGap::connect_callback_t _connect_cb;
BLEGap::disconnect_callback_t _disconnect_cb;
void _event_handler(ble_evt_t* evt);
friend class AdafruitBluefruit;
};
-41
View File
@@ -39,8 +39,6 @@
BLEGap::BLEGap(void)
{
varclr(&_prph_cb);
varclr(&_central_cb);
varclr(&_peers);
}
@@ -54,28 +52,6 @@ uint8_t BLEGap::getRole(uint16_t conn_handle)
return _peers[conn_handle].role;
}
void BLEGap::setConnectCallback(connect_callback_t fp, uint8_t role)
{
if (role == BLE_GAP_ROLE_PERIPH)
{
_prph_cb.connect_cb = fp;
}else
{
_central_cb.connect_cb = fp;
}
}
void BLEGap::setDisconnectCallback(disconnect_callback_t fp, uint8_t role)
{
if (role == BLE_GAP_ROLE_PERIPH)
{
_prph_cb.disconnect_cb = fp;
}else
{
_central_cb.disconnect_cb = fp;
}
}
uint8_t BLEGap::getPeerAddr(uint16_t conn_handle, uint8_t addr[6])
{
memcpy(addr, _peers[conn_handle].addr.addr, BLE_GAP_ADDR_LEN);
@@ -124,15 +100,6 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
uint8_t txbuf_max;
(void) sd_ble_tx_packet_count_get(conn_handle, &txbuf_max);
peer->txpacket_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
// Invoke callback according to the role
if ( peer->role == BLE_GAP_ROLE_PERIPH)
{
if (_prph_cb.connect_cb) ada_callback(NULL, _prph_cb.connect_cb, conn_handle);
}else
{
if (_central_cb.connect_cb) ada_callback(NULL, _central_cb.connect_cb, conn_handle);
}
}
break;
@@ -145,14 +112,6 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
vSemaphoreDelete( peer->txpacket_sem );
peer->txpacket_sem = NULL;
if ( peer->role == BLE_GAP_ROLE_PERIPH)
{
if (_prph_cb.disconnect_cb) ada_callback(NULL, _prph_cb.disconnect_cb, conn_handle, para->reason);
}else
{
if (_central_cb.disconnect_cb) ada_callback(NULL, _central_cb.disconnect_cb, conn_handle, para->reason);
}
}
break;
-9
View File
@@ -58,10 +58,6 @@ class BLEGap
uint16_t getPeerName(uint16_t conn_handle, char* buf, uint16_t bufsize);
bool getTxPacket(uint16_t conn_handle);
/*------------- Callbacks -------------*/
void setConnectCallback (connect_callback_t fp, uint8_t role);
void setDisconnectCallback(disconnect_callback_t fp, uint8_t role);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
* Although declare as public, it is meant to be invoked by internal
@@ -70,11 +66,6 @@ class BLEGap
void _eventHandler(ble_evt_t* evt);
private:
struct {
connect_callback_t connect_cb;
disconnect_callback_t disconnect_cb;
}_prph_cb, _central_cb;
// Array of TX Packet semaphore, indexed by connection handle
// Peer info where conn_handle serves as index
typedef struct {
+12 -5
View File
@@ -95,9 +95,12 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_bonded = false;
_ppcp_min_conn = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
_ppcp_max_conn = BLE_GAP_CONN_MAX_INTERVAL_DFLT;
_conn_interval = 0;
_ppcp_min_conn = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
_ppcp_max_conn = BLE_GAP_CONN_MAX_INTERVAL_DFLT;
_conn_interval = 0;
_connect_cb = NULL;
_disconnect_cb = NULL;
COMMENT_OUT(
_auth_type = BLE_GAP_AUTH_KEY_TYPE_NONE;
@@ -315,12 +318,12 @@ bool AdafruitBluefruit::disconnect(void)
void AdafruitBluefruit::setConnectCallback( BLEGap::connect_callback_t fp )
{
Gap.setConnectCallback(fp, BLE_GAP_ROLE_PERIPH);
_connect_cb = fp;
}
void AdafruitBluefruit::setDisconnectCallback( BLEGap::disconnect_callback_t fp )
{
Gap.setDisconnectCallback(fp, BLE_GAP_ROLE_PERIPH);
_disconnect_cb = fp;
}
uint16_t AdafruitBluefruit::connHandle(void)
@@ -492,6 +495,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// Null, value is set by sd_ble_gap_ppcp_set will be used
VERIFY_STATUS( sd_ble_gap_conn_param_update(_conn_hdl, NULL), );
}
if (_connect_cb) ada_callback(NULL, _connect_cb, _conn_hdl);
}
}
break;
@@ -511,6 +516,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
// Save all configured cccd
if (_bonded) _saveBondedCCCD();
if (_disconnect_cb) ada_callback(NULL, _disconnect_cb, _conn_hdl);
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_bonded = false;
break;
+6 -3
View File
@@ -94,15 +94,15 @@ class AdafruitBluefruit
/*------------------------------------------------------------------*/
/* Lower Level Classes (Bluefruit.Advertising.*, etc.)
*------------------------------------------------------------------*/
BLEGap Gap;
BLEGatt Gatt;
BLEAdvertising Advertising;
BLEAdvertisingData ScanResponse;
BLEScanner Scanner;
BLECentral Central;
BLEDiscovery Discovery;
BLEGap Gap;
BLEGatt Gatt;
/*------------------------------------------------------------------*/
/* General Purpose Functions
*------------------------------------------------------------------*/
@@ -178,6 +178,9 @@ class AdafruitBluefruit
uint16_t _conn_hdl;
bool _bonded;
BLEGap::connect_callback_t _connect_cb;
BLEGap::disconnect_callback_t _disconnect_cb;
public: // TODO temporary for bledfu to load bonding data
struct
{