Improve readability with getHandle inline
Fork of BLE_API by
Revision 251:5d9118449482, committed 2014-12-12
- Comitter:
- rgrover1
- Date:
- Fri Dec 12 13:32:24 2014 +0000
- Parent:
- 250:792e326c2dfd
- Child:
- 252:6862d374e613
- Commit message:
- Synchronized with git rev 7a298b6a
Author: Rohit Grover
Add support for write authorization for Characteristics.
Changed in this revision
--- a/public/GattCharacteristic.h Fri Dec 12 13:32:23 2014 +0000 +++ b/public/GattCharacteristic.h Fri Dec 12 13:32:24 2014 +0000 @@ -18,6 +18,8 @@ #define __GATT_CHARACTERISTIC_H__ #include "GattAttribute.h" +#include "GattCharacteristicWriteCBParams.h" +#include "FunctionPointerWithContext.h" class GattCharacteristic { public: @@ -310,24 +312,65 @@ * A pointer to an array of descriptors to be included within this characteristic * @param[in] numDescriptors * The number of descriptors + * @param[in] writeAuthorizationIn + * Do the attribute(s) of this characteristic have write + * authorization enabled? if so, Write Authorization will be + * requested from the application on every write request + * operation (but not write command). * * @NOTE: If valuePtr == NULL, initialLength == 0, and properties == READ * for the value attribute of a characteristic, then that particular * characteristic may be considered optional and dropped while * instantiating the service with the underlying BLE stack. */ - GattCharacteristic(const UUID &uuid, uint8_t *valuePtr = NULL, uint16_t initialLen = 0, uint16_t maxLen = 0, - uint8_t props = BLE_GATT_CHAR_PROPERTIES_NONE, - GattAttribute *descriptors[] = NULL, unsigned numDescriptors = 0) : - _valueAttribute(uuid, valuePtr, initialLen, maxLen), _properties(props), _descriptors(descriptors), _descriptorCount(numDescriptors) { + GattCharacteristic(const UUID &uuid, + uint8_t *valuePtr = NULL, + uint16_t initialLen = 0, + uint16_t maxLen = 0, + uint8_t props = BLE_GATT_CHAR_PROPERTIES_NONE, + GattAttribute *descriptors[] = NULL, + unsigned numDescriptors = 0) : + _valueAttribute(uuid, valuePtr, initialLen, maxLen), + _properties(props), + _descriptors(descriptors), + _descriptorCount(numDescriptors), + enableWriteAuthorization(false), + writeAuthorizationCallback() { + /* empty */ } public: - GattAttribute& getValueAttribute() {return _valueAttribute; } - const GattAttribute& getValueAttribute() const {return _valueAttribute; } - GattAttribute::Handle_t getValueHandle(void) const {return getValueAttribute().getHandle();} - uint8_t getProperties(void) const {return _properties; } - uint8_t getDescriptorCount(void) const {return _descriptorCount;} + /** + * Setup write authorization. + */ + void setWriteAuthorizationCallback(void (*callback)(GattCharacteristicWriteAuthCBParams *)) { + enableWriteAuthorization = true; + writeAuthorizationCallback.attach(callback); + } + template <typename T> + void setWriteAuthorizationCallback(T *object, void (T::*member)(GattCharacteristicWriteAuthCBParams *)) { + enableWriteAuthorization = true; + writeAuthorizationCallback.attach(object, member); + } + + /** + * Helper function meant to be called from the guts of the BLE stack to + * determine the authorization reply for a write request. + * @param params to capture the context of the write-auth request; and also contains an out-parameter for reply. + * @return true if the write is authorized to proceed. + */ + bool authorizeWrite(GattCharacteristicWriteAuthCBParams *params) { + params->authorizationReply = true; /* initialized to true by default */ + writeAuthorizationCallback.call(params); + return params->authorizationReply; + } + + GattAttribute& getValueAttribute() {return _valueAttribute; } + const GattAttribute& getValueAttribute() const {return _valueAttribute; } + GattAttribute::Handle_t getValueHandle(void) const {return getValueAttribute().getHandle();} + uint8_t getProperties(void) const {return _properties; } + uint8_t getDescriptorCount(void) const {return _descriptorCount;} + bool isWriteAuthorizationEnabled() const {return enableWriteAuthorization;} GattAttribute *getDescriptor(uint8_t index) { if (index >= _descriptorCount) { @@ -342,6 +385,8 @@ uint8_t _properties; GattAttribute **_descriptors; uint8_t _descriptorCount; + bool enableWriteAuthorization; + FunctionPointerWithContext<GattCharacteristicWriteAuthCBParams *> writeAuthorizationCallback; private: /* disallow copy and assignment */
--- a/public/GattCharacteristicWriteCBParams.h Fri Dec 12 13:32:23 2014 +0000 +++ b/public/GattCharacteristicWriteCBParams.h Fri Dec 12 13:32:24 2014 +0000 @@ -33,4 +33,13 @@ const uint8_t *data; /**< Incoming data, variable length. */ }; -#endif /*__GATT_CHARACTERISTIC_WRITE_CB_PARAMS_H__*/ +struct GattCharacteristicWriteAuthCBParams { + GattAttribute::Handle_t charHandle; + uint16_t offset; /**< Offset for the write operation. */ + uint16_t len; /**< Length of the incoming data. */ + const uint8_t *data; /**< Incoming data, variable length. */ + bool authorizationReply; /* This is the out parameter which needs to be set to true by the callback if the + * request is to proceed; false otherwise. */ +}; + +#endif /*__GATT_CHARACTERISTIC_WRITE_CB_PARAMS_H__*/ \ No newline at end of file
--- a/public/GattServerEvents.h Fri Dec 12 13:32:23 2014 +0000 +++ b/public/GattServerEvents.h Fri Dec 12 13:32:24 2014 +0000 @@ -26,11 +26,12 @@ { public: typedef enum gattEvent_e { - GATT_EVENT_DATA_SENT = 1, /**< Fired when a msg was successfully sent out (notify only?) */ - GATT_EVENT_DATA_WRITTEN = 2, /**< Client wrote data to Server (separate into char and descriptor writes?) */ - GATT_EVENT_UPDATES_ENABLED = 3, /**< Notify/Indicate Enabled in CCCD */ - GATT_EVENT_UPDATES_DISABLED = 4, /**< Notify/Indicate Disabled in CCCD */ - GATT_EVENT_CONFIRMATION_RECEIVED = 5 /**< Response received from Indicate message */ + GATT_EVENT_DATA_SENT = 1, /**< Fired when a msg was successfully sent out (notify only?) */ + GATT_EVENT_DATA_WRITTEN = 2, /**< Client wrote data to Server (separate into char and descriptor writes?) */ + GATT_EVENT_UPDATES_ENABLED = 3, /**< Notify/Indicate Enabled in CCCD */ + GATT_EVENT_UPDATES_DISABLED = 4, /**< Notify/Indicate Disabled in CCCD */ + GATT_EVENT_CONFIRMATION_RECEIVED = 5, /**< Response received from Indicate message */ + GATT_EVENT_WRITE_AUTHORIZATION_REQ = 6, /**< Request application to authorize write */ } gattEvent_t; };