Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.
Fork of BLE_API by
Diff: public/GattCharacteristic.h
- Revision:
- 253:097be53aea02
- Parent:
- 252:69d049317cae
- Child:
- 260:ea7f9f14cc15
--- a/public/GattCharacteristic.h Fri Dec 12 13:32:24 2014 +0000 +++ b/public/GattCharacteristic.h Fri Dec 12 13:32:24 2014 +0000 @@ -334,15 +334,17 @@ _properties(props), _descriptors(descriptors), _descriptorCount(numDescriptors), + enabledReadAuthorization(false), enabledWriteAuthorization(false), + readAuthorizationCallback(), writeAuthorizationCallback() { /* empty */ } + /** + * Authorization. + */ public: - /** - * Setup write authorization. - */ void setWriteAuthorizationCallback(void (*callback)(GattCharacteristicWriteAuthCBParams *)) { writeAuthorizationCallback.attach(callback); enabledWriteAuthorization = true; @@ -352,6 +354,15 @@ writeAuthorizationCallback.attach(object, member); enabledWriteAuthorization = true; } + void setReadAuthorizationCallback(void (*callback)(GattCharacteristicReadAuthCBParams *)) { + readAuthorizationCallback.attach(callback); + enabledReadAuthorization = true; + } + template <typename T> + void setReadAuthorizationCallback(T *object, void (T::*member)(GattCharacteristicReadAuthCBParams *)) { + readAuthorizationCallback.attach(object, member); + enabledReadAuthorization = true; + } /** * Helper function meant to be called from the guts of the BLE stack to @@ -369,11 +380,30 @@ return params->authorizationReply; } - GattAttribute& getValueAttribute() {return _valueAttribute; } - const GattAttribute& getValueAttribute() const {return _valueAttribute; } + /** + * Helper function meant to be called from the guts of the BLE stack to + * determine the authorization reply for a read request. + * @param params to capture the context of the read-auth request; and also contains an out-parameter for reply. + * @return true if the read is authorized to proceed. + */ + bool authorizeRead(GattCharacteristicReadAuthCBParams *params) { + if (!isReadAuthorizationEnabled()) { + return true; + } + + params->authorizationReply = true; /* initialized to true by default */ + readAuthorizationCallback.call(params); + return params->authorizationReply; + } + + /* accessors */ +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;} + uint8_t getProperties(void) const {return _properties; } + uint8_t getDescriptorCount(void) const {return _descriptorCount; } + bool isReadAuthorizationEnabled() const {return enabledReadAuthorization; } bool isWriteAuthorizationEnabled() const {return enabledWriteAuthorization; } GattAttribute *getDescriptor(uint8_t index) { @@ -389,7 +419,10 @@ uint8_t _properties; GattAttribute **_descriptors; uint8_t _descriptorCount; + + bool enabledReadAuthorization; bool enabledWriteAuthorization; + FunctionPointerWithContext<GattCharacteristicReadAuthCBParams *> readAuthorizationCallback; FunctionPointerWithContext<GattCharacteristicWriteAuthCBParams *> writeAuthorizationCallback; private: