more central support

- rename BLE_GAP_MAX_CONN to BLE_MAX_CONN
- change rx callback of BLEClientUart
- better support multiple connection
This commit is contained in:
hathach committed 2017-07-10 23:39:18 +07:00
1 parent d4e3244341
commit 9a74a6f300
9 files changed
+67 -73

No files matched your search

+2
View File
@@ -18,6 +18,8 @@
- Updated for latest Bluefruit LE Connect version with RGBW support
- Implement Gatt readCharByUuid()
- Implement Gap getPeerName()
- Change Bluefruit.peerAddr() to Bluefruit.getPeerAddr()
- change rx callback of BLEClientUart
### New Examples
@@ -132,19 +132,24 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
Serial.println("Bluefruit will auto start scanning (default)");
}
void uart_rx_callback(void)
/**
* Callback invoked when uart received data
* @param uart_svc Reference object to the service where the data
* arrived. In this example it is clientUart
*/
void uart_rx_callback(BLEClientUart& uart_svc)
{
Serial.print("[RX]: ");
while ( clientUart.available() )
while ( uart_svc.available() )
{
Serial.print( (char) clientUart.read() );
Serial.print( (char) uart_svc.read() );
}
Serial.println();
}
void loop()
void loop()
{
if ( Bluefruit.Central.connected() )
{
+47 -60
View File
@@ -42,8 +42,6 @@
*/
BLECentral::BLECentral(void)
{
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_scan_cb = NULL;
_scan_param = (ble_gap_scan_params_t) {
.active = 1,
@@ -200,7 +198,7 @@ bool BLECentral::connected(uint16_t conn_handle)
*/
bool BLECentral::connected(void)
{
for (uint8_t conn=0; conn<BLE_GAP_MAX_CONN; conn++)
for (uint8_t conn=0; conn<BLE_MAX_CONN; conn++)
{
// skip Peripherl Role handle
if (conn != Bluefruit.connHandle() )
@@ -232,66 +230,55 @@ void BLECentral::_event_handler(ble_evt_t* evt)
// conn handle has fixed offset regardless of event type
const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle;
/* Only handle Central events with matched connection handle
* or a few special one
* - Connected event
* - Advertising Report
* - Advertising timeout (could be connected and advertising at the same time)
/* PrPh handle connection is already filtered. Only handle Central events or
* connection handle is BLE_CONN_HANDLE_INVALID (e.g BLE_GAP_EVT_ADV_REPORT)
*/
if ( evt_conn_hdl == _conn_hdl ||
evt->header.evt_id == BLE_GAP_EVT_CONNECTED ||
evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT ||
evt->header.evt_id == BLE_GAP_EVT_TIMEOUT )
switch ( evt->header.evt_id )
{
switch ( evt->header.evt_id )
case BLE_GAP_EVT_ADV_REPORT:
{
case BLE_GAP_EVT_ADV_REPORT:
{
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
if (_scan_cb) _scan_cb(adv_report);
}
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_BLUE);
// TODO multiple connections
_conn_hdl = evt->evt.gap_evt.conn_handle;
if ( _connect_cb )
{
ada_callback(NULL, _connect_cb, evt->evt.gap_evt.conn_handle);
}
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
if (Bluefruit._led_conn) ledOff(LED_BLUE);
_conn_hdl = BLE_CONN_HANDLE_INVALID;
if ( _disconnect_cb ) _disconnect_cb(evt_conn_hdl, 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)
{
// TODO Advance Scanning
// Restart Scanning
startScanning();
}
break;
default: break;
// evt_conn_hdl is equal to BLE_CONN_HANDLE_INVALID
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
if (_scan_cb) _scan_cb(adv_report);
}
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_BLUE);
if ( _connect_cb )
{
ada_callback(NULL, _connect_cb, evt->evt.gap_evt.conn_handle);
}
}
}
break;
case BLE_GAP_EVT_DISCONNECTED:
if (Bluefruit._led_conn) ledOff(LED_BLUE);
_conn_hdl = BLE_CONN_HANDLE_INVALID;
if ( _disconnect_cb ) _disconnect_cb(evt_conn_hdl, 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)
{
// TODO Advance Scanning
// Restart Scanning
startScanning();
}
break;
default: break;
}
}
+1 -1
View File
@@ -64,7 +64,7 @@ bool BLEGap::getTxPacket(void)
bool BLEGap::getTxPacket(uint16_t conn_handle)
{
VERIFY( (conn_handle < BLE_GAP_MAX_CONN) && (_peers[conn_handle].txpacket_sem != NULL) );
VERIFY( (conn_handle < BLE_MAX_CONN) && (_peers[conn_handle].txpacket_sem != NULL) );
return xSemaphoreTake(_peers[conn_handle].txpacket_sem, ms2tick(BLE_GENERIC_TIMEOUT));
}
+1 -3
View File
@@ -40,8 +40,6 @@
#include "bluefruit_common.h"
#include "BLEUuid.h"
#define BLE_GAP_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN)
class BLEGap
{
public:
@@ -75,7 +73,7 @@ class BLEGap
SemaphoreHandle_t txpacket_sem;
} gap_peer_t;
gap_peer_t _peers[BLE_GAP_MAX_CONN];
gap_peer_t _peers[BLE_MAX_CONN];
};
#endif /* BLEGAP_H_ */
+1 -1
View File
@@ -49,7 +49,7 @@
#define BLE_GATT_MAX_SERVER_CHARS 40
#define BLE_GATT_MAX_CLIENT_SERVICE 20
#define BLE_GATT_MAX_CLIENT_CHARS 30
#define BLE_GATT_MAX_CLIENT_CHARS 40
class BLEGatt
+2
View File
@@ -47,6 +47,8 @@
#define BLE_CENTRAL_MAX_CONN 4
#define BLE_CENTRAL_MAX_SECURE_CONN 1 // should be enough
#define BLE_MAX_CONN (BLE_CENTRAL_MAX_CONN+BLE_PRPH_MAX_CONN)
#include "BLEUuid.h"
#include "BLEAdvertising.h"
#include "BLECharacteristic.h"
@@ -50,8 +50,8 @@ bool BLEClientUart::begin(void)
// Invoke base class begin()
BLEClientService::begin();
_rxd.begin();
_txd.begin();
_rxd.begin(this);
_txd.begin(this);
// set up notify callback
_txd.setNotifyCallback(bleuart_central_notify_cb);
@@ -100,7 +100,7 @@ void bleuart_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint
uart_svc._fifo.write(data, len);
// invoke callback
if ( uart_svc._rx_cb ) uart_svc._rx_cb();
if ( uart_svc._rx_cb ) uart_svc._rx_cb(uart_svc);
}
/*------------------------------------------------------------------*/
@@ -47,7 +47,7 @@
class BLEClientUart : public BLEClientService, public Stream
{
public:
typedef void (*rx_callback_t) (void);
typedef void (*rx_callback_t) (BLEClientUart& svc);
BLEClientUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH);