diff --git a/libraries/Bluefruit52Lib/src/BLECentral.cpp b/libraries/Bluefruit52Lib/src/BLECentral.cpp index 62d6ec6c..934fb56e 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentral.cpp @@ -247,7 +247,7 @@ bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle) if ( (disc_svc.count == 1) && (uuid == disc_svc.services[0].uuid) ) { _disc_hdl_range = disc_svc.services[0].handle_range; - LOG_LV1(Discover, "[SVC] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + LOG_LV1(Discover, "[SVC] Found 0x%04X, Handle start = %d, end = %d", uuid._uuid.uuid, _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); _disc_hdl_range.start_handle++; // increase for next discovery return true; @@ -256,32 +256,48 @@ bool BLECentral::discoverService(BLEUuid uuid, uint16_t start_handle) return false; } -bool BLECentral::discoverCharacteristic(BLECentralCharacteristic& chr) +uint8_t BLECentral::discoverCharacteristic(BLECentralCharacteristic* chr[], uint8_t count) { - ble_gattc_evt_char_disc_rsp_t disc_chr; + uint8_t found = 0; - _evt_buf = &disc_chr; - _evt_bufsize = sizeof(disc_chr); - - LOG_LV1(Discover, "[CHR] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); - - VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_disc_hdl_range), false ); - - // wait for discovery event: timeout or has no data - if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) ) return false; - - if ( disc_chr.count > 0 ) + while( found < count ) { - chr = BLECentralCharacteristic(&disc_chr.chars[0]); + ble_gattc_evt_char_disc_rsp_t disc_chr; + + _evt_buf = &disc_chr; + _evt_bufsize = sizeof(disc_chr); + + LOG_LV1(Discover, "[CHR] Handle start = %d, end = %d", _disc_hdl_range.start_handle, _disc_hdl_range.end_handle); + + VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &_disc_hdl_range), found ); + + // wait for discovery event: timeout or has no data + // Assume only 1 characteristic discovered each + if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || (_evt_bufsize == 0) || (disc_chr.count == 0) ) break; // increase handle range for next discovery _disc_hdl_range.start_handle = disc_chr.chars[0].handle_value + 1; - // Discovery All descriptors as well - chr.discoverDescriptor(); + // Look for matched uuid + for (uint8_t i=0; iuuid == disc_chr.chars[0].uuid ) + { + LOG_LV1(Discover, "[CHR] Found 0x%04X, handle = %d", disc_chr.chars[0].uuid.uuid, disc_chr.chars[0].handle_value); + + chr[i]->_chr = disc_chr.chars[0]; + + // Discovery All descriptors as well + chr[i]->discoverDescriptor(); + + found++; + + break; + } + } } - return true; + return found; } uint16_t BLECentral::_discoverDescriptor(ble_gattc_evt_desc_disc_rsp_t* disc_desc, uint16_t max_count) diff --git a/libraries/Bluefruit52Lib/src/BLECentral.h b/libraries/Bluefruit52Lib/src/BLECentral.h index 0d0f170d..d5823b0d 100644 --- a/libraries/Bluefruit52Lib/src/BLECentral.h +++ b/libraries/Bluefruit52Lib/src/BLECentral.h @@ -90,8 +90,8 @@ class BLECentral /*------------------------------------------------------------------*/ /* GATTC Discovery *------------------------------------------------------------------*/ - bool discoverService(BLEUuid uuid, uint16_t start_handle = 1); - bool discoverCharacteristic(BLECentralCharacteristic& chr); + bool discoverService(BLEUuid uuid, uint16_t start_handle = 1); + uint8_t discoverCharacteristic(BLECentralCharacteristic* chr[], uint8_t count); /*------------------------------------------------------------------*/ diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp index ab26d871..9ad6a52c 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.cpp @@ -41,6 +41,7 @@ void BLECentralCharacteristic::_init(void) { + varclr(&_chr); _cccd_handle = 0; _notify_cb = NULL; @@ -50,14 +51,12 @@ void BLECentralCharacteristic::_init(void) BLECentralCharacteristic::BLECentralCharacteristic(void) : uuid() { - varclr(&_chr); _init(); } -BLECentralCharacteristic::BLECentralCharacteristic(ble_gattc_char_t* gattc_char) - : uuid(gattc_char->uuid) +BLECentralCharacteristic::BLECentralCharacteristic(BLEUuid bleuuid) + : uuid(bleuuid) { - _chr = (*gattc_char); _init(); } @@ -96,6 +95,9 @@ bool BLECentralCharacteristic::discoverDescriptor(void) void BLECentralCharacteristic::begin(void) { + // Add UUID128 if needed + uuid.begin(); + _service = BLECentralService::lastService; // Register to Bluefruit (required for callback and write response) diff --git a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h index 5c05289d..27f80b1f 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECentralCharacteristic.h @@ -49,7 +49,7 @@ class BLECentralCharacteristic BLEUuid uuid; BLECentralCharacteristic(void); - BLECentralCharacteristic(ble_gattc_char_t* gattc_char); + BLECentralCharacteristic(BLEUuid bleuuid); bool discoverDescriptor(void); void begin(void); @@ -75,11 +75,11 @@ class BLECentralCharacteristic void setNotifyCallback(notify_cb_t fp); private: - ble_gattc_char_t _chr; - uint16_t _cccd_handle; + ble_gattc_char_t _chr; + uint16_t _cccd_handle; BLECentralService* _service; - notify_cb_t _notify_cb; + notify_cb_t _notify_cb; SemaphoreHandle_t _sem; diff --git a/libraries/Bluefruit52Lib/src/BLECentralService.cpp b/libraries/Bluefruit52Lib/src/BLECentralService.cpp index 7d39e0e2..0c218d0f 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralService.cpp +++ b/libraries/Bluefruit52Lib/src/BLECentralService.cpp @@ -41,6 +41,7 @@ BLECentralService* BLECentralService::lastService = NULL; void BLECentralService::_init(void) { + _discovered = false; } BLECentralService::BLECentralService(void) @@ -61,14 +62,24 @@ err_t BLECentralService::begin(void) // Add UUID128 if needed uuid.begin(); + lastService = this; + return ERROR_NONE; } bool BLECentralService::discover(uint16_t start_handle) { VERIFY( Bluefruit.Central.discoverService(uuid, start_handle) ); - - lastService = this; - + _discovered = true; return true; } + +bool BLECentralService::discovered(void) +{ + return _discovered; +} + +void BLECentralService::disconnect(void) +{ + // do nothing, inherited service may want to clean up its own +} diff --git a/libraries/Bluefruit52Lib/src/BLECentralService.h b/libraries/Bluefruit52Lib/src/BLECentralService.h index 6046447d..0e90f3f9 100644 --- a/libraries/Bluefruit52Lib/src/BLECentralService.h +++ b/libraries/Bluefruit52Lib/src/BLECentralService.h @@ -42,6 +42,8 @@ class BLECentralService { protected: + bool _discovered; + void _init(void); public: @@ -55,6 +57,8 @@ class BLECentralService virtual err_t begin(void); virtual bool discover(uint16_t start_handle = 1); + bool discovered(void); + virtual void disconnect(void); }; diff --git a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp index b93b3c04..306a6bbf 100644 --- a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp +++ b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.cpp @@ -36,65 +36,66 @@ #include "bluefruit.h" -BLECentralUart::BLECentralUart(uint16_t fifo_depth) - : BLECentralService(BLEUART_UUID_SERVICE), _txd(), _rxd(), _fifo(fifo_depth, 1) -{ +void bleuart_central_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len); +BLECentralUart::BLECentralUart(uint16_t fifo_depth) + : BLECentralService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD), + _fifo(fifo_depth, 1) +{ + _rx_cb = NULL; } err_t BLECentralUart::begin(void) { - // Add UUID128 if needed - uuid.begin(); + // Invoke basde class begin() + BLECentralService::begin(); + + _rxd.begin(); + _txd.begin(); + + // set up notify callback + _txd.setNotifyCallback(bleuart_central_notify_cb); return ERROR_NONE; } +bool BLECentralUart::enableNotify(void) +{ + return _txd.enableNotify(); +} + +void BLECentralUart::setRxCallback( rx_callback_t fp) +{ + _rx_cb = fp; +} + +bool BLECentralUart::discover(uint16_t start_handle) +{ + // Call BLECentralService discover + VERIFY( BLECentralService::discover(start_handle) ); + _discovered = false; + + // Discover TXD, RXD characteristics + BLECentralCharacteristic* chr_arr[] = { &_rxd, &_txd }; + VERIFY( 2 == Bluefruit.Central.discoverCharacteristic(chr_arr, 2) ); + + _discovered = true; + + return true; +} + +void BLECentralUart::disconnect(void) +{ + +} + void bleuart_central_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len) { BLECentralUart& uart_svc = (BLECentralUart&) chr.parentService(); uart_svc._fifo.write(data, len); // invoke callback -} - -bool BLECentralUart::discover(uint16_t start_handle) -{ - // Call BLECentralService discover - VERIFY( BLECentralService::discover(start_handle) ); - - // Discover TXD, RXD characteristics - BLECentralCharacteristic chr_temp; - while ( Bluefruit.Central.discoverCharacteristic(chr_temp) ) - { - uint16_t uuid16; - chr_temp.uuid.get(&uuid16); - - if (uuid16 == 0x0002) - { - _rxd = chr_temp; - LOG_LV1(BLECentralUart, "Found RXD: handle = %d", _rxd.valueHandle()); - }else if (uuid16 == 0x0003) - { - _txd = chr_temp; - LOG_LV1(BLECentralUart, "Found TXD: handle = %d", _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(); + if ( uart_svc._rx_cb ) uart_svc._rx_cb(); } /*------------------------------------------------------------------*/ @@ -119,6 +120,7 @@ size_t BLECentralUart::write (uint8_t b) size_t BLECentralUart::write (const uint8_t *content, size_t len) { // do nothing + return _rxd.write(content, len); } int BLECentralUart::available (void) diff --git a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h index 86d89c00..b29013b4 100644 --- a/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h +++ b/libraries/Bluefruit52Lib/src/central_services/BLECentralUart.h @@ -47,12 +47,16 @@ class BLECentralUart : public BLECentralService, public Stream { public: + typedef void (*rx_callback_t) (void); + BLECentralUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH); virtual err_t begin(void); virtual bool discover(uint16_t start_handle = 1); + virtual void disconnect(void); bool enableNotify(void); + void setRxCallback( rx_callback_t fp); // Stream API virtual int read ( void ); @@ -72,6 +76,7 @@ class BLECentralUart : public BLECentralService, public Stream BLECentralCharacteristic _rxd; Adafruit_FIFO _fifo; + rx_callback_t _rx_cb; friend void bleuart_central_notify_cb(BLECentralCharacteristic& chr, uint8_t* data, uint16_t len); };