From 5928b5e4434e00550418730fabde70d87c544b70 Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Thu, 10 Dec 2015 17:21:19 +0000 Subject: [PATCH] microbit: Added support for explicit characteristic notification Required to allow the mbed layer to avoid caching large amounts of state in its GattCharacteristics n.b. this should be removed once all optimisations to the mbed layer are completed. --- source/nRF5xGattServer.cpp | 32 ++++++++++++++++++++++++++++++++ source/nRF5xGattServer.h | 1 + 2 files changed, 33 insertions(+) diff --git a/source/nRF5xGattServer.cpp b/source/nRF5xGattServer.cpp index f561b9d..c49d8a1 100644 --- a/source/nRF5xGattServer.cpp +++ b/source/nRF5xGattServer.cpp @@ -277,6 +277,38 @@ ble_error_t nRF5xGattServer::write(Gap::Handle_t connectionHandle, GattAttribute return returnValue; } +/** + * Perform an explicit BLE notification of a given attribute. + * + * @param[in] attributeHandle + * Handle for the value attribute of the Characteristic. + * @param[in] value + * A pointer to a buffer holding the new value + * @param[in] size + * Size of the new value (in bytes). + * + * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. + */ +ble_error_t nRF5xGattServer::notify(GattAttribute::Handle_t attributeHandle, const uint8_t buffer[], uint16_t len) +{ + uint16_t gapConnectionHandle = nRF5xGap::getInstance().getConnectionHandle(); + ble_gatts_hvx_params_t hvx_params; + + hvx_params.handle = attributeHandle; + hvx_params.type = BLE_GATT_HVX_NOTIFICATION; + hvx_params.offset = 0; + hvx_params.p_data = const_cast(buffer); + hvx_params.p_len = &len; + + error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); + + if (error == ERROR_NONE) + return BLE_ERROR_NONE; + else + return BLE_STACK_BUSY; +} + + ble_error_t nRF5xGattServer::areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) { /* Forward the call with the default connection handle. */ diff --git a/source/nRF5xGattServer.h b/source/nRF5xGattServer.h index f929f8c..f606ff9 100644 --- a/source/nRF5xGattServer.h +++ b/source/nRF5xGattServer.h @@ -33,6 +33,7 @@ public: virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP); virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false); + virtual ble_error_t notify(GattAttribute::Handle_t, const uint8_t[], uint16_t); virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP); virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP); virtual ble_error_t reset(void);