diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 80721711..e03b5ad1 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -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; ievt.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); + } + } } diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index eb300ed5..b17fd149 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -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; diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp index 51928586..fc115de2 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -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(), ¶m), 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; + } +} diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h index 37fb7b2d..54b1340f 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -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_ */ diff --git a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp index a15dae5b..3df56a23 100644 --- a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp +++ b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp @@ -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(); } diff --git a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h index 0e0982ac..7e54458b 100644 --- a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h +++ b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h @@ -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;