Add support for write authorization for Characteristics.
This commit is contained in:
parent
7ac6aea06b
commit
7a298b6a43
3 changed files with 69 additions and 14 deletions
|
@ -18,6 +18,8 @@
|
|||
#define __GATT_CHARACTERISTIC_H__
|
||||
|
||||
#include "GattAttribute.h"
|
||||
#include "GattCharacteristicWriteCBParams.h"
|
||||
#include "FunctionPointerWithContext.h"
|
||||
|
||||
class GattCharacteristic {
|
||||
public:
|
||||
|
@ -310,24 +312,65 @@ public:
|
|||
* 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 @@ private:
|
|||
uint8_t _properties;
|
||||
GattAttribute **_descriptors;
|
||||
uint8_t _descriptorCount;
|
||||
bool enableWriteAuthorization;
|
||||
FunctionPointerWithContext<GattCharacteristicWriteAuthCBParams *> writeAuthorizationCallback;
|
||||
|
||||
private:
|
||||
/* disallow copy and assignment */
|
||||
|
|
|
@ -33,4 +33,13 @@ struct GattCharacteristicWriteCBParams {
|
|||
const uint8_t *data; /**< Incoming data, variable length. */
|
||||
};
|
||||
|
||||
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__*/
|
||||
|
|
|
@ -26,11 +26,12 @@ class GattServerEvents
|
|||
{
|
||||
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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue