diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp index cc998fa0..3fa476b2 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.cpp @@ -496,12 +496,10 @@ uint32_t BLECharacteristic::read32(void) return read(&num, sizeof(num)) ? num : 0; } -/*------------------------------------------------------------------*/ -/* NOTIFY - *------------------------------------------------------------------*/ -bool BLECharacteristic::notifyEnabled(void) + +uint16_t BLECharacteristic::getCccd(void) { - VERIFY( _properties.notify && Bluefruit.connected() ); + VERIFY( Bluefruit.connected() && (_handles.cccd_handle != BLE_GATT_HANDLE_INVALID), 0 ); uint16_t cccd; ble_gatts_value_t value = @@ -522,7 +520,16 @@ bool BLECharacteristic::notifyEnabled(void) VERIFY_STATUS(err); } - return (cccd & BLE_GATT_HVX_NOTIFICATION); + return cccd; +} + +/*------------------------------------------------------------------*/ +/* NOTIFY + *------------------------------------------------------------------*/ +bool BLECharacteristic::notifyEnabled(void) +{ + VERIFY( _properties.notify ); + return (getCccd() & BLE_GATT_HVX_NOTIFICATION); } bool BLECharacteristic::notify(const void* data, uint16_t len) @@ -557,7 +564,7 @@ bool BLECharacteristic::notify(const void* data, uint16_t len) VERIFY_STATUS( sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params), false ); remaining -= packet_len; - u8data += packet_len; + u8data += packet_len; } } else @@ -593,3 +600,87 @@ bool BLECharacteristic::notify32(int num) { return notify32( (uint32_t) num); } + +/*------------------------------------------------------------------*/ +/* NOTIFY + *------------------------------------------------------------------*/ +bool BLECharacteristic::indicateEnabled(void) +{ + VERIFY( _properties.indicate ); + return (getCccd() & BLE_GATT_HVX_INDICATION); +} + +bool BLECharacteristic::indicate(const void* data, uint16_t len) +{ + VERIFY( _properties.indicate ); + + // could not exceed max len + uint16_t remaining = min16(len, _max_len); + + if ( indicateEnabled() ) + { + uint16_t const max_payload = Bluefruit.Gap.getMTU( Bluefruit.connHandle() ) - 3; + const uint8_t* u8data = (const uint8_t*) data; + + while ( remaining ) + { + uint16_t packet_len = min16(max_payload, remaining); + + ble_gatts_hvx_params_t hvx_params = + { + .handle = _handles.value_handle, + .type = BLE_GATT_HVX_INDICATION, + .offset = 0, + .p_len = &packet_len, + .p_data = (uint8_t*) u8data, + }; + + LOG_LV2("CHR", "Indicate %d bytes", packet_len); + err_t err; + + // If previous indicate is still in progress, wait for a while and try again + while ( NRF_ERROR_BUSY == (err = sd_ble_gatts_hvx(Bluefruit.connHandle(), &hvx_params)) ) + { + // TODO should wait for BLE_GATTS_EVT_HVC event + delay( Bluefruit.connInterval() ); + } + + VERIFY_STATUS( err, false ); + + remaining -= packet_len; + u8data += packet_len; + } + } + else + { + write(data, remaining); + return false; + } + + return true; +} + +bool BLECharacteristic::indicate(const char * str) +{ + return indicate( (const uint8_t*) str, strlen(str) ); +} + +bool BLECharacteristic::indicate8(uint8_t num) +{ + return indicate( (uint8_t*) &num, sizeof(num)); +} + +bool BLECharacteristic::indicate16(uint16_t num) +{ + return indicate( (uint8_t*) &num, sizeof(num)); +} + +bool BLECharacteristic::indicate32(uint32_t num) +{ + return indicate( (uint8_t*) &num, sizeof(num)); +} + +bool BLECharacteristic::indicate32(int num) +{ + return indicate32( (uint32_t) num); +} diff --git a/libraries/Bluefruit52Lib/src/BLECharacteristic.h b/libraries/Bluefruit52Lib/src/BLECharacteristic.h index 9e0dac2d..ece72bee 100644 --- a/libraries/Bluefruit52Lib/src/BLECharacteristic.h +++ b/libraries/Bluefruit52Lib/src/BLECharacteristic.h @@ -135,6 +135,8 @@ class BLECharacteristic uint32_t read32(void); /*------------- Notify -------------*/ + uint16_t getCccd(void); + bool notifyEnabled(void); bool notify(const void* data, uint16_t len); @@ -145,6 +147,17 @@ class BLECharacteristic bool notify32 (uint32_t num); bool notify32 (int num); + /*------------- Indicate -------------*/ + bool indicateEnabled(void); + + bool indicate(const void* data, uint16_t len); + bool indicate(const char* str); + + bool indicate8 (uint8_t num); + bool indicate16 (uint16_t num); + bool indicate32 (uint32_t num); + bool indicate32 (int num); + /*------------- Internal Functions -------------*/ virtual void _eventHandler(ble_evt_t* event); diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index f7765295..b14ed533 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -49,6 +49,12 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe int32_t count = 0; ble_gattc_handle_range_t hdl_range = { .start_handle = start_hdl, .end_handle = end_hdl }; + while( _adamsg.isWaiting() ) + { + // TODO multiple peripherals + delay( Bluefruit.connInterval() ); + } + _adamsg.begin(true); _adamsg.prepare(buffer, bufsize); @@ -68,6 +74,25 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe return (count < 0) ? 0 : count; } +//bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl) +//{ +// while( _adamsg.isWaiting() ) +// { +// // TODO multiple peripherals +// delay( Bluefruit.connInterval() ); +// } +// +// _adamsg.begin(true); +// _adamsg.prepare(NULL, 0); +// +// // Wait for HVC event +// int32_t result = _adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT); +// +// _adamsg.stop(); +// +// return (result == 0); +//} + void BLEGatt::_eventHandler(ble_evt_t* evt) { // conn handle has fixed offset regardless of event type diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.h b/libraries/Bluefruit52Lib/src/BLEGatt.h index ce19093e..a930b2a2 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.h +++ b/libraries/Bluefruit52Lib/src/BLEGatt.h @@ -59,6 +59,7 @@ class BLEGatt BLEGatt(void); uint16_t readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffer, uint16_t bufsize, uint16_t start_hdl = 1, uint16_t end_hdl = 0xffff); +// bool waitForIndicateConfirm(uint16_t conn_hdl); /*------------------------------------------------------------------*/ /* INTERNAL USAGE ONLY