Merge branch 'jeremybrodt-readcallback'.

Fixes #33.
This commit is contained in:
Rohit Grover 2015-02-23 09:11:31 +00:00
commit a3df6ff03f
3 changed files with 75 additions and 0 deletions

View file

@ -250,6 +250,27 @@ public:
void onDataWritten(void (*callback)(const GattCharacteristicWriteCBParams *eventDataP));
template <typename T> void onDataWritten(T * objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context));
/**
* Setup a callback for when a characteristic is being read by a client.
*
* @Note: this functionality may not be available on all underlying stacks.
* You could use GattCharacteristic::setReadAuthorizationCallback() as an
* alternative.
*
* @Note: it is possible to chain together multiple onDataRead callbacks
* (potentially from different modules of an application) to receive updates
* to characteristics. Services may add their own onDataRead callbacks
* behind the scenes to trap interesting events.
*
* @Note: it is also possible to setup a callback into a member function of
* some object.
*
* @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
* else BLE_ERROR_NONE.
*/
ble_error_t onDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP));
template <typename T> ble_error_t onDataRead(T * objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context));
void onUpdatesEnabled(GattServer::EventCallback_t callback);
void onUpdatesDisabled(GattServer::EventCallback_t callback);
void onConfirmationReceived(GattServer::EventCallback_t callback);
@ -551,6 +572,16 @@ BLEDevice::onDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristi
transport->getGattServer().setOnDataWritten(objPtr, memberPtr);
}
inline ble_error_t
BLEDevice::onDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {
return transport->getGattServer().setOnDataRead(callback);
}
template <typename T> inline ble_error_t
BLEDevice::onDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
return transport->getGattServer().setOnDataRead(objPtr, memberPtr);
}
inline void
BLEDevice::onUpdatesEnabled(GattServer::EventCallback_t callback)
{

View file

@ -33,6 +33,17 @@ struct GattCharacteristicWriteCBParams {
const uint8_t *data; /**< Incoming data, variable length. */
};
struct GattCharacteristicReadCBParams {
GattAttribute::Handle_t charHandle;
enum Type {
GATTS_CHAR_OP_INVALID = 0x00, /**< Invalid Operation. */
GATTS_CHAR_OP_READ_REQ = 0x0A, /**< Read Request. */
} op; /**< Type of write operation, */
uint16_t offset; /**< Offset for the read operation. */
uint16_t *len; /**< Length of the outgoing data. */
uint8_t *data; /**< Outgoing data, variable length. */
};
struct GattCharacteristicWriteAuthCBParams {
GattAttribute::Handle_t charHandle;
uint16_t offset; /**< Offset for the write operation. */

View file

@ -34,6 +34,7 @@ protected:
characteristicCount(0),
onDataSent(),
onDataWritten(),
onDataRead(),
onUpdatesEnabled(NULL),
onUpdatesDisabled(NULL),
onConfirmationReceived(NULL) {
@ -63,6 +64,31 @@ private:
void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattCharacteristicWriteCBParams *context)) {
onDataWritten.add(objPtr, memberPtr);
}
/**
* A virtual function to allow underlying stacks to indicate if they support
* onDataRead(). It should be overridden to return true as applicable.
*/
virtual bool isOnDataReadAvaialble() const {
return false;
}
ble_error_t setOnDataRead(void (*callback)(const GattCharacteristicReadCBParams *eventDataP)) {
if (!isOnDataReadAvaialble()) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
onDataRead.add(callback);
return BLE_ERROR_NONE;
}
template <typename T>
ble_error_t setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattCharacteristicReadCBParams *context)) {
if (!isOnDataReadAvaialble()) {
return BLE_ERROR_NOT_IMPLEMENTED;
}
onDataRead.add(objPtr, memberPtr);
return BLE_ERROR_NONE;
}
void setOnUpdatesEnabled(EventCallback_t callback) {onUpdatesEnabled = callback;}
void setOnUpdatesDisabled(EventCallback_t callback) {onUpdatesDisabled = callback;}
void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;}
@ -74,6 +100,12 @@ protected:
}
}
void handleDataReadEvent(const GattCharacteristicReadCBParams *params) {
if (onDataRead.hasCallbacksAttached()) {
onDataRead.call(params);
}
}
void handleEvent(GattServerEvents::gattEvent_e type, uint16_t charHandle) {
switch (type) {
case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
@ -109,6 +141,7 @@ protected:
private:
CallChainOfFunctionPointersWithContext<unsigned> onDataSent;
CallChainOfFunctionPointersWithContext<const GattCharacteristicWriteCBParams *> onDataWritten;
CallChainOfFunctionPointersWithContext<const GattCharacteristicReadCBParams *> onDataRead;
EventCallback_t onUpdatesEnabled;
EventCallback_t onUpdatesDisabled;
EventCallback_t onConfirmationReceived;