[central] callback support for chars

- add enableNotify()
This commit is contained in:
hathach committed 2017-03-23 01:57:44 +07:00
1 parent 71cc024ee4
commit 9f40cbfb1e
6 files changed
+157 -8

No files matched your search

+25 -1
View File
@@ -36,7 +36,7 @@
#include "bluefruit.h"
#define BLE_CENTRAL_TIMEOUT 10000
#define BLE_CENTRAL_TIMEOUT 10000
/**
* Constructor
@@ -49,6 +49,9 @@ BLECentral::BLECentral(void)
_evt_buf = NULL;
_evt_bufsize = 0;
_chars_count = 0;
for(uint8_t i=0; i<BLE_CENTRAL_MAX_CHARS; i++) _chars_list[i] = NULL;
_scan_cb = NULL;
_scan_param = (ble_gap_scan_params_t) {
.active = 1,
@@ -71,6 +74,14 @@ void BLECentral::begin(void)
_evt_sem = xSemaphoreCreateBinary();
}
bool BLECentral::_registerCharacteristic(BLECentralCharacteristic* chr)
{
if ( _chars_count == BLE_CENTRAL_MAX_CHARS ) return false;
_chars_list[ _chars_count++ ] = chr;
return true;
}
/*------------------------------------------------------------------*/
/* Scan and Parser
*------------------------------------------------------------------*/
@@ -352,6 +363,9 @@ void BLECentral::_event_handler(ble_evt_t* evt)
}
break;
/*------------------------------------------------------------------*/
/* DISCOVERY
*------------------------------------------------------------------*/
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
{
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
@@ -429,4 +443,14 @@ void BLECentral::_event_handler(ble_evt_t* evt)
default: break;
}
// GATT characteristics event handler
uint16_t conn_handle = evt->evt.common_evt.conn_handle;
if ( conn_handle == _conn_hdl)
{
for(int i=0; i<_chars_count; i++)
{
_chars_list[i]->_eventHandler(evt);
}
}
}
+16 -2
View File
@@ -46,6 +46,9 @@
#include "BLECentralCharacteristic.h"
#include "BLECentralService.h"
#define BLE_CENTRAL_MAX_CHARS 20
class AdafruitBluefruit;
class BLECentral
@@ -91,8 +94,6 @@ class BLECentral
bool discoverCharacteristic(BLECentralCharacteristic& chr);
// Called by discoverCharacteristic(), user should not call this directly
uint16_t _discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count);
/*------------------------------------------------------------------*/
/* CALLBACKS
@@ -103,6 +104,15 @@ class BLECentral
void setConnectCallback ( connect_callback_t fp);
void setDisconnectCallback( disconnect_callback_t fp);
/*------------------------------------------------------------------*/
/* INTERNAL USAGE ONLY
*------------------------------------------------------------------*/
// Called by discoverCharacteristic(), user should not call this directly
uint16_t _discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count);
bool _registerCharacteristic(BLECentralCharacteristic* chr);
private:
uint16_t _conn_hdl;
@@ -110,6 +120,10 @@ class BLECentral
void* _evt_buf;
uint16_t _evt_bufsize;
BLECentralCharacteristic* _chars_list[BLE_CENTRAL_MAX_CHARS];
uint8_t _chars_count;
ble_gap_scan_params_t _scan_param;
scan_callback_t _scan_cb;
@@ -38,18 +38,25 @@
#define MAX_DESCIRPTORS 8
void BLECentralCharacteristic::_init(void)
{
_cccd_handle = 0;
_notify_cb = NULL;
}
BLECentralCharacteristic::BLECentralCharacteristic(void)
: uuid()
{
varclr(&_chr);
_cccd_handle = 0;
_init();
}
BLECentralCharacteristic::BLECentralCharacteristic(ble_gattc_char_t* gattc_char)
: uuid(gattc_char->uuid)
{
_chr = (*gattc_char);
_cccd_handle = 0;
_init();
}
uint16_t BLECentralCharacteristic::valueHandle()
@@ -79,3 +86,65 @@ bool BLECentralCharacteristic::discoverDescriptor(void)
return true;
}
void BLECentralCharacteristic::begin(void)
{
// Currently Only register to Bluefruit if callback is installed
if ( _notify_cb )
{
(void) Bluefruit.Central._registerCharacteristic(this);
}
}
uint16_t BLECentralCharacteristic::read(void* buffer, int bufsize, uint16_t offset)
{
VERIFY_STATUS( sd_ble_gattc_read(Bluefruit.Central.connHandle(), _chr.handle_value, offset), 0 );
return ERROR_NONE;
}
void BLECentralCharacteristic::setNotifyCallback(notify_cb_t fp)
{
_notify_cb = fp;
}
bool BLECentralCharacteristic::enableNotify(void)
{
uint16_t value = 0x0001;
ble_gattc_write_params_t param =
{
.write_op = BLE_GATT_OP_WRITE_CMD,
.flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_WRITE,
.handle = _cccd_handle,
.offset = 0,
.len = 2,
.p_value = (uint8_t*) &value
};
// TODO consume a TX buffer
VERIFY_STATUS( sd_ble_gattc_write(Bluefruit.Central.connHandle(), &param), false );
return true;
}
void BLECentralCharacteristic::_eventHandler(ble_evt_t* event)
{
switch(event->header.evt_id)
{
case BLE_GATTC_EVT_HVX:
{
ble_gattc_evt_hvx_t* hvx = &event->evt.gattc_evt.params.hvx;
if ( hvx->type == BLE_GATT_HVX_NOTIFICATION )
{
if (_notify_cb) _notify_cb(*this, hvx->data, hvx->len);
}else
{
}
}
break;
default: break;
}
}
@@ -48,13 +48,36 @@ class BLECentralCharacteristic
BLECentralCharacteristic(void);
BLECentralCharacteristic(ble_gattc_char_t* gattc_char);
bool discoverDescriptor(void);
void begin(void);
uint16_t valueHandle();
bool discoverDescriptor(void);
/*------------- Read -------------*/
uint16_t read(void* buffer, int bufsize, uint16_t offset = 0);
/*------------- Write -------------*/
/*------------- Notify -------------*/
bool enableNotify(void);
/*------------- Callbacks -------------*/
typedef void (*notify_cb_t ) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
typedef void (*indicate_cb_t) (BLECentralCharacteristic& chr, uint8_t* data, uint16_t len);
void setNotifyCallback(notify_cb_t fp);
private:
ble_gattc_char_t _chr;
uint16_t _cccd_handle;
notify_cb_t _notify_cb;
void _init(void);
void _eventHandler(ble_evt_t* event);
friend class BLECentral;
};
#endif /* BLECENTRALCHARACTERISTIC_H_ */
@@ -50,6 +50,11 @@ err_t BLECentralUart::begin(void)
return ERROR_NONE;
}
void bleuart_central_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len)
{
}
bool BLECentralUart::discover(uint16_t start_handle)
{
// Call BLECentralService discover
@@ -73,6 +78,18 @@ bool BLECentralUart::discover(uint16_t start_handle)
}
}
// true if both RXD & TXD are found
return ( _rxd.valueHandle() && _txd.valueHandle() );
// Check if both RXD & TXD are found
VERIFY ( _rxd.valueHandle() && _txd.valueHandle() );
_txd.setNotifyCallback(bleuart_central_notify_cb);
_rxd.begin();
_txd.begin();
return true;
}
bool BLECentralUart::enableNotify(void)
{
return _txd.enableNotify();
}
@@ -52,6 +52,8 @@ class BLECentralUart : public BLECentralService
virtual err_t begin(void);
virtual bool discover(uint16_t start_handle = 1);
bool enableNotify(void);
private:
BLECentralCharacteristic _txd;
BLECentralCharacteristic _rxd;