High level Bluetooth Low Energy API and radio abstraction layer

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate BLE_ANCS_SDAPI_IRC ... more

Overview

The BLE_API is a high level abstraction for using Bluetooth Low Energy on multiple platforms. For details and examples using the BLE_API please see the BLE_API Summary Page. Or click on the API Documentation tab above.

Supported Services

Supported services can be found in the BLE_API/services folder.

Committer:
vcoubard
Date:
Wed Apr 06 19:13:46 2016 +0100
Revision:
1131:692ddf04fc42
Parent:
1090:148d8b9b56a5
Child:
1135:22aada733dbd
Synchronized with git rev 13bf70b6
Author: Rohit Grover
Release 2.1.5
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic. Also contains some improvements to documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 1131:692ddf04fc42 1 /* mbed Microcontroller Library
vcoubard 1131:692ddf04fc42 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 1131:692ddf04fc42 3 *
vcoubard 1131:692ddf04fc42 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 1131:692ddf04fc42 5 * you may not use this file except in compliance with the License.
vcoubard 1131:692ddf04fc42 6 * You may obtain a copy of the License at
vcoubard 1131:692ddf04fc42 7 *
vcoubard 1131:692ddf04fc42 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 1131:692ddf04fc42 9 *
vcoubard 1131:692ddf04fc42 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 1131:692ddf04fc42 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 1131:692ddf04fc42 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 1131:692ddf04fc42 13 * See the License for the specific language governing permissions and
vcoubard 1131:692ddf04fc42 14 * limitations under the License.
vcoubard 1131:692ddf04fc42 15 */
vcoubard 1131:692ddf04fc42 16
vcoubard 1131:692ddf04fc42 17 #ifndef __GATT_SERVER_H__
vcoubard 1131:692ddf04fc42 18 #define __GATT_SERVER_H__
vcoubard 1131:692ddf04fc42 19
vcoubard 1131:692ddf04fc42 20 #include "Gap.h"
vcoubard 1131:692ddf04fc42 21 #include "GattService.h"
vcoubard 1131:692ddf04fc42 22 #include "GattAttribute.h"
vcoubard 1131:692ddf04fc42 23 #include "GattServerEvents.h"
vcoubard 1131:692ddf04fc42 24 #include "GattCallbackParamTypes.h"
vcoubard 1131:692ddf04fc42 25 #include "CallChainOfFunctionPointersWithContext.h"
vcoubard 1131:692ddf04fc42 26
vcoubard 1131:692ddf04fc42 27 class GattServer {
vcoubard 1131:692ddf04fc42 28 public:
vcoubard 1131:692ddf04fc42 29
vcoubard 1131:692ddf04fc42 30 /* Event callback handlers. */
vcoubard 1131:692ddf04fc42 31 typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
vcoubard 1131:692ddf04fc42 32 typedef CallChainOfFunctionPointersWithContext<unsigned> DataSentCallbackChain_t;
vcoubard 1131:692ddf04fc42 33
vcoubard 1131:692ddf04fc42 34 typedef FunctionPointerWithContext<const GattWriteCallbackParams*> DataWrittenCallback_t;
vcoubard 1131:692ddf04fc42 35 typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*> DataWrittenCallbackChain_t;
vcoubard 1131:692ddf04fc42 36
vcoubard 1131:692ddf04fc42 37 typedef FunctionPointerWithContext<const GattReadCallbackParams*> DataReadCallback_t;
vcoubard 1131:692ddf04fc42 38 typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> DataReadCallbackChain_t;
vcoubard 1131:692ddf04fc42 39
vcoubard 1131:692ddf04fc42 40 typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;
vcoubard 1131:692ddf04fc42 41
vcoubard 1131:692ddf04fc42 42 protected:
vcoubard 1131:692ddf04fc42 43 GattServer() :
vcoubard 1131:692ddf04fc42 44 serviceCount(0),
vcoubard 1131:692ddf04fc42 45 characteristicCount(0),
vcoubard 1131:692ddf04fc42 46 dataSentCallChain(),
vcoubard 1131:692ddf04fc42 47 dataWrittenCallChain(),
vcoubard 1131:692ddf04fc42 48 dataReadCallChain(),
vcoubard 1131:692ddf04fc42 49 updatesEnabledCallback(NULL),
vcoubard 1131:692ddf04fc42 50 updatesDisabledCallback(NULL),
vcoubard 1131:692ddf04fc42 51 confirmationReceivedCallback(NULL) {
vcoubard 1131:692ddf04fc42 52 /* empty */
vcoubard 1131:692ddf04fc42 53 }
vcoubard 1131:692ddf04fc42 54
vcoubard 1131:692ddf04fc42 55 /*
vcoubard 1131:692ddf04fc42 56 * The following functions are meant to be overridden in the platform-specific sub-class.
vcoubard 1131:692ddf04fc42 57 */
vcoubard 1131:692ddf04fc42 58 public:
vcoubard 1131:692ddf04fc42 59
vcoubard 1131:692ddf04fc42 60 /**
vcoubard 1131:692ddf04fc42 61 * Add a service declaration to the local server ATT table. Also add the
vcoubard 1131:692ddf04fc42 62 * characteristics contained within.
vcoubard 1131:692ddf04fc42 63 */
vcoubard 1131:692ddf04fc42 64 virtual ble_error_t addService(GattService &service) {
vcoubard 1131:692ddf04fc42 65 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 66 (void)service;
vcoubard 1131:692ddf04fc42 67
vcoubard 1131:692ddf04fc42 68 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 69 }
vcoubard 1131:692ddf04fc42 70
vcoubard 1131:692ddf04fc42 71 /**
vcoubard 1131:692ddf04fc42 72 * Read the value of a characteristic from the local GATT server.
vcoubard 1131:692ddf04fc42 73 * @param[in] attributeHandle
vcoubard 1131:692ddf04fc42 74 * Attribute handle for the value attribute of the characteristic.
vcoubard 1131:692ddf04fc42 75 * @param[out] buffer
vcoubard 1131:692ddf04fc42 76 * A buffer to hold the value being read.
vcoubard 1131:692ddf04fc42 77 * @param[in/out] lengthP
vcoubard 1131:692ddf04fc42 78 * Length of the buffer being supplied. If the attribute
vcoubard 1131:692ddf04fc42 79 * value is longer than the size of the supplied buffer,
vcoubard 1131:692ddf04fc42 80 * this variable will hold upon return the total attribute value length
vcoubard 1131:692ddf04fc42 81 * (excluding offset). The application may use this
vcoubard 1131:692ddf04fc42 82 * information to allocate a suitable buffer size.
vcoubard 1131:692ddf04fc42 83 *
vcoubard 1131:692ddf04fc42 84 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
vcoubard 1131:692ddf04fc42 85 */
vcoubard 1131:692ddf04fc42 86 virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) {
vcoubard 1131:692ddf04fc42 87 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 88 (void)attributeHandle;
vcoubard 1131:692ddf04fc42 89 (void)buffer;
vcoubard 1131:692ddf04fc42 90 (void)lengthP;
vcoubard 1131:692ddf04fc42 91
vcoubard 1131:692ddf04fc42 92 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 93 }
vcoubard 1131:692ddf04fc42 94
vcoubard 1131:692ddf04fc42 95 /**
vcoubard 1131:692ddf04fc42 96 * Read the value of a characteristic from the local GATT server.
vcoubard 1131:692ddf04fc42 97 * @param[in] connectionHandle
vcoubard 1131:692ddf04fc42 98 * Connection handle.
vcoubard 1131:692ddf04fc42 99 * @param[in] attributeHandle
vcoubard 1131:692ddf04fc42 100 * Attribute handle for the value attribute of the characteristic.
vcoubard 1131:692ddf04fc42 101 * @param[out] buffer
vcoubard 1131:692ddf04fc42 102 * A buffer to hold the value being read.
vcoubard 1131:692ddf04fc42 103 * @param[in/out] lengthP
vcoubard 1131:692ddf04fc42 104 * Length of the buffer being supplied. If the attribute
vcoubard 1131:692ddf04fc42 105 * value is longer than the size of the supplied buffer,
vcoubard 1131:692ddf04fc42 106 * this variable will hold upon return the total attribute value length
vcoubard 1131:692ddf04fc42 107 * (excluding offset). The application may use this
vcoubard 1131:692ddf04fc42 108 * information to allocate a suitable buffer size.
vcoubard 1131:692ddf04fc42 109 *
vcoubard 1131:692ddf04fc42 110 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
vcoubard 1131:692ddf04fc42 111 *
vcoubard 1131:692ddf04fc42 112 * @note This API is a version of the above, with an additional connection handle
vcoubard 1131:692ddf04fc42 113 * parameter to allow fetches for connection-specific multivalued
vcoubard 1131:692ddf04fc42 114 * attributes (such as the CCCDs).
vcoubard 1131:692ddf04fc42 115 */
vcoubard 1131:692ddf04fc42 116 virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t *buffer, uint16_t *lengthP) {
vcoubard 1131:692ddf04fc42 117 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 118 (void)connectionHandle;
vcoubard 1131:692ddf04fc42 119 (void)attributeHandle;
vcoubard 1131:692ddf04fc42 120 (void)buffer;
vcoubard 1131:692ddf04fc42 121 (void)lengthP;
vcoubard 1131:692ddf04fc42 122
vcoubard 1131:692ddf04fc42 123 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 124 }
vcoubard 1131:692ddf04fc42 125
vcoubard 1131:692ddf04fc42 126 /**
vcoubard 1131:692ddf04fc42 127 * Update the value of a characteristic on the local GATT server.
vcoubard 1131:692ddf04fc42 128 *
vcoubard 1131:692ddf04fc42 129 * @param[in] attributeHandle
vcoubard 1131:692ddf04fc42 130 * Handle for the value attribute of the characteristic.
vcoubard 1131:692ddf04fc42 131 * @param[in] value
vcoubard 1131:692ddf04fc42 132 * A pointer to a buffer holding the new value.
vcoubard 1131:692ddf04fc42 133 * @param[in] size
vcoubard 1131:692ddf04fc42 134 * Size of the new value (in bytes).
vcoubard 1131:692ddf04fc42 135 * @param[in] localOnly
vcoubard 1131:692ddf04fc42 136 * Should this update be kept on the local
vcoubard 1131:692ddf04fc42 137 * GATT server regardless of the state of the
vcoubard 1131:692ddf04fc42 138 * notify/indicate flag in the CCCD for this
vcoubard 1131:692ddf04fc42 139 * Characteristic? If set to true, no notification
vcoubard 1131:692ddf04fc42 140 * or indication is generated.
vcoubard 1131:692ddf04fc42 141 *
vcoubard 1131:692ddf04fc42 142 * @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
vcoubard 1131:692ddf04fc42 143 */
vcoubard 1131:692ddf04fc42 144 virtual ble_error_t write(GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) {
vcoubard 1131:692ddf04fc42 145 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 146 (void)attributeHandle;
vcoubard 1131:692ddf04fc42 147 (void)value;
vcoubard 1131:692ddf04fc42 148 (void)size;
vcoubard 1131:692ddf04fc42 149 (void)localOnly;
vcoubard 1131:692ddf04fc42 150
vcoubard 1131:692ddf04fc42 151 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 152 }
vcoubard 1131:692ddf04fc42 153
vcoubard 1131:692ddf04fc42 154 /**
vcoubard 1131:692ddf04fc42 155 * Update the value of a characteristic on the local GATT server. A version
vcoubard 1131:692ddf04fc42 156 * of the same as the above, with a connection handle parameter to allow updates
vcoubard 1131:692ddf04fc42 157 * for connection-specific multivalued attributes (such as the CCCDs).
vcoubard 1131:692ddf04fc42 158 *
vcoubard 1131:692ddf04fc42 159 * @param[in] connectionHandle
vcoubard 1131:692ddf04fc42 160 * Connection handle.
vcoubard 1131:692ddf04fc42 161 * @param[in] attributeHandle
vcoubard 1131:692ddf04fc42 162 * Handle for the value attribute of the characteristic.
vcoubard 1131:692ddf04fc42 163 * @param[in] value
vcoubard 1131:692ddf04fc42 164 * A pointer to a buffer holding the new value.
vcoubard 1131:692ddf04fc42 165 * @param[in] size
vcoubard 1131:692ddf04fc42 166 * Size of the new value (in bytes).
vcoubard 1131:692ddf04fc42 167 * @param[in] localOnly
vcoubard 1131:692ddf04fc42 168 * Should this update be kept on the local
vcoubard 1131:692ddf04fc42 169 * GattServer regardless of the state of the
vcoubard 1131:692ddf04fc42 170 * notify/indicate flag in the CCCD for this
vcoubard 1131:692ddf04fc42 171 * Characteristic? If set to true, no notification
vcoubard 1131:692ddf04fc42 172 * or indication is generated.
vcoubard 1131:692ddf04fc42 173 *
vcoubard 1131:692ddf04fc42 174 * @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
vcoubard 1131:692ddf04fc42 175 */
vcoubard 1131:692ddf04fc42 176 virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) {
vcoubard 1131:692ddf04fc42 177 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 178 (void)connectionHandle;
vcoubard 1131:692ddf04fc42 179 (void)attributeHandle;
vcoubard 1131:692ddf04fc42 180 (void)value;
vcoubard 1131:692ddf04fc42 181 (void)size;
vcoubard 1131:692ddf04fc42 182 (void)localOnly;
vcoubard 1131:692ddf04fc42 183
vcoubard 1131:692ddf04fc42 184 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 185 }
vcoubard 1131:692ddf04fc42 186
vcoubard 1131:692ddf04fc42 187 /**
vcoubard 1131:692ddf04fc42 188 * Determine the updates-enabled status (notification or indication) for the current connection from a characteristic's CCCD.
vcoubard 1131:692ddf04fc42 189 *
vcoubard 1131:692ddf04fc42 190 * @param characteristic
vcoubard 1131:692ddf04fc42 191 * The characteristic.
vcoubard 1131:692ddf04fc42 192 * @param[out] enabledP
vcoubard 1131:692ddf04fc42 193 * Upon return, *enabledP is true if updates are enabled, else false.
vcoubard 1131:692ddf04fc42 194 *
vcoubard 1131:692ddf04fc42 195 * @return BLE_ERROR_NONE if the connection and handle are found. False otherwise.
vcoubard 1131:692ddf04fc42 196 */
vcoubard 1131:692ddf04fc42 197 virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) {
vcoubard 1131:692ddf04fc42 198 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 199 (void)characteristic;
vcoubard 1131:692ddf04fc42 200 (void)enabledP;
vcoubard 1131:692ddf04fc42 201
vcoubard 1131:692ddf04fc42 202 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 203 }
vcoubard 1131:692ddf04fc42 204
vcoubard 1131:692ddf04fc42 205 /**
vcoubard 1131:692ddf04fc42 206 * Determine the connection-specific updates-enabled status (notification or indication) from a characteristic's CCCD.
vcoubard 1131:692ddf04fc42 207 *
vcoubard 1131:692ddf04fc42 208 * @param connectionHandle
vcoubard 1131:692ddf04fc42 209 * The connection handle.
vcoubard 1131:692ddf04fc42 210 * @param[out] enabledP
vcoubard 1131:692ddf04fc42 211 * Upon return, *enabledP is true if updates are enabled, else false.
vcoubard 1131:692ddf04fc42 212 *
vcoubard 1131:692ddf04fc42 213 * @param characteristic
vcoubard 1131:692ddf04fc42 214 * The characteristic.
vcoubard 1131:692ddf04fc42 215 *
vcoubard 1131:692ddf04fc42 216 * @return BLE_ERROR_NONE if the connection and handle are found. False otherwise.
vcoubard 1131:692ddf04fc42 217 */
vcoubard 1131:692ddf04fc42 218 virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) {
vcoubard 1131:692ddf04fc42 219 /* Avoid compiler warnings about unused variables. */
vcoubard 1131:692ddf04fc42 220 (void)connectionHandle;
vcoubard 1131:692ddf04fc42 221 (void)characteristic;
vcoubard 1131:692ddf04fc42 222 (void)enabledP;
vcoubard 1131:692ddf04fc42 223
vcoubard 1131:692ddf04fc42 224 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 225 }
vcoubard 1131:692ddf04fc42 226
vcoubard 1131:692ddf04fc42 227 /**
vcoubard 1131:692ddf04fc42 228 * A virtual function to allow underlying stacks to indicate if they support
vcoubard 1131:692ddf04fc42 229 * onDataRead(). It should be overridden to return true as applicable.
vcoubard 1131:692ddf04fc42 230 */
vcoubard 1131:692ddf04fc42 231 virtual bool isOnDataReadAvailable() const {
vcoubard 1131:692ddf04fc42 232 return false; /* Requesting action from porters: override this API if this capability is supported. */
vcoubard 1131:692ddf04fc42 233 }
vcoubard 1131:692ddf04fc42 234
vcoubard 1131:692ddf04fc42 235 /*
vcoubard 1131:692ddf04fc42 236 * APIs with non-virtual implementations.
vcoubard 1131:692ddf04fc42 237 */
vcoubard 1131:692ddf04fc42 238 public:
vcoubard 1131:692ddf04fc42 239 /**
vcoubard 1131:692ddf04fc42 240 * Add a callback for the GATT event DATA_SENT (which is triggered when
vcoubard 1131:692ddf04fc42 241 * updates are sent out by GATT in the form of notifications).
vcoubard 1131:692ddf04fc42 242 *
vcoubard 1131:692ddf04fc42 243 * @Note: It is possible to chain together multiple onDataSent callbacks
vcoubard 1131:692ddf04fc42 244 * (potentially from different modules of an application) to receive updates
vcoubard 1131:692ddf04fc42 245 * to characteristics.
vcoubard 1131:692ddf04fc42 246 *
vcoubard 1131:692ddf04fc42 247 * @Note: It is also possible to set up a callback into a member function of
vcoubard 1131:692ddf04fc42 248 * some object.
vcoubard 1131:692ddf04fc42 249 */
vcoubard 1131:692ddf04fc42 250 void onDataSent(const DataSentCallback_t& callback) {dataSentCallChain.add(callback);}
vcoubard 1131:692ddf04fc42 251 template <typename T>
vcoubard 1131:692ddf04fc42 252 void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
vcoubard 1131:692ddf04fc42 253 dataSentCallChain.add(objPtr, memberPtr);
vcoubard 1131:692ddf04fc42 254 }
vcoubard 1131:692ddf04fc42 255
vcoubard 1131:692ddf04fc42 256 /**
vcoubard 1131:692ddf04fc42 257 * @brief get the callback chain called when the event DATA_EVENT is triggered.
vcoubard 1131:692ddf04fc42 258 */
vcoubard 1131:692ddf04fc42 259 DataSentCallbackChain_t& onDataSent() {
vcoubard 1131:692ddf04fc42 260 return dataSentCallChain;
vcoubard 1131:692ddf04fc42 261 }
vcoubard 1131:692ddf04fc42 262
vcoubard 1131:692ddf04fc42 263 /**
vcoubard 1131:692ddf04fc42 264 * Set up a callback for when an attribute has its value updated by or at the
vcoubard 1131:692ddf04fc42 265 * connected peer. For a peripheral, this callback is triggered when the local
vcoubard 1131:692ddf04fc42 266 * GATT server has an attribute updated by a write command from the peer.
vcoubard 1131:692ddf04fc42 267 * For a central, this callback is triggered when a response is received for
vcoubard 1131:692ddf04fc42 268 * a write request.
vcoubard 1131:692ddf04fc42 269 *
vcoubard 1131:692ddf04fc42 270 * @Note: It is possible to chain together multiple onDataWritten callbacks
vcoubard 1131:692ddf04fc42 271 * (potentially from different modules of an application) to receive updates
vcoubard 1131:692ddf04fc42 272 * to characteristics. Many services, such as DFU and UART, add their own
vcoubard 1131:692ddf04fc42 273 * onDataWritten callbacks behind the scenes to trap interesting events.
vcoubard 1131:692ddf04fc42 274 *
vcoubard 1131:692ddf04fc42 275 * @Note: It is also possible to set up a callback into a member function of
vcoubard 1131:692ddf04fc42 276 * some object.
vcoubard 1131:692ddf04fc42 277 *
vcoubard 1131:692ddf04fc42 278 * @Note It is possible to unregister a callback using onDataWritten().detach(callback)
vcoubard 1131:692ddf04fc42 279 */
vcoubard 1131:692ddf04fc42 280 void onDataWritten(const DataWrittenCallback_t& callback) {dataWrittenCallChain.add(callback);}
vcoubard 1131:692ddf04fc42 281 template <typename T>
vcoubard 1131:692ddf04fc42 282 void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) {
vcoubard 1131:692ddf04fc42 283 dataWrittenCallChain.add(objPtr, memberPtr);
vcoubard 1131:692ddf04fc42 284 }
vcoubard 1131:692ddf04fc42 285
vcoubard 1131:692ddf04fc42 286 /**
vcoubard 1131:692ddf04fc42 287 * @brief provide access to the callchain of data written event callbacks
vcoubard 1131:692ddf04fc42 288 * It is possible to register callbacks using onDataWritten().add(callback);
vcoubard 1131:692ddf04fc42 289 * It is possible to unregister callbacks using onDataWritten().detach(callback)
vcoubard 1131:692ddf04fc42 290 * @return The data written event callbacks chain
vcoubard 1131:692ddf04fc42 291 */
vcoubard 1131:692ddf04fc42 292 DataWrittenCallbackChain_t& onDataWritten() {
vcoubard 1131:692ddf04fc42 293 return dataWrittenCallChain;
vcoubard 1131:692ddf04fc42 294 }
vcoubard 1131:692ddf04fc42 295
vcoubard 1131:692ddf04fc42 296 /**
vcoubard 1131:692ddf04fc42 297 * Setup a callback to be invoked on the peripheral when an attribute is
vcoubard 1131:692ddf04fc42 298 * being read by a remote client.
vcoubard 1131:692ddf04fc42 299 *
vcoubard 1131:692ddf04fc42 300 * @Note: This functionality may not be available on all underlying stacks.
vcoubard 1131:692ddf04fc42 301 * You could use GattCharacteristic::setReadAuthorizationCallback() as an
vcoubard 1131:692ddf04fc42 302 * alternative. Refer to isOnDataReadAvailable().
vcoubard 1131:692ddf04fc42 303 *
vcoubard 1131:692ddf04fc42 304 * @Note: It is possible to chain together multiple onDataRead callbacks
vcoubard 1131:692ddf04fc42 305 * (potentially from different modules of an application) to receive updates
vcoubard 1131:692ddf04fc42 306 * to characteristics. Services may add their own onDataRead callbacks
vcoubard 1131:692ddf04fc42 307 * behind the scenes to trap interesting events.
vcoubard 1131:692ddf04fc42 308 *
vcoubard 1131:692ddf04fc42 309 * @Note: It is also possible to set up a callback into a member function of
vcoubard 1131:692ddf04fc42 310 * some object.
vcoubard 1131:692ddf04fc42 311 *
vcoubard 1131:692ddf04fc42 312 * @Note It is possible to unregister a callback using onDataRead().detach(callback)
vcoubard 1131:692ddf04fc42 313 *
vcoubard 1131:692ddf04fc42 314 * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
vcoubard 1131:692ddf04fc42 315 * else BLE_ERROR_NONE.
vcoubard 1131:692ddf04fc42 316 */
vcoubard 1131:692ddf04fc42 317 ble_error_t onDataRead(const DataReadCallback_t& callback) {
vcoubard 1131:692ddf04fc42 318 if (!isOnDataReadAvailable()) {
vcoubard 1131:692ddf04fc42 319 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 1131:692ddf04fc42 320 }
vcoubard 1131:692ddf04fc42 321
vcoubard 1131:692ddf04fc42 322 dataReadCallChain.add(callback);
vcoubard 1131:692ddf04fc42 323 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 324 }
vcoubard 1131:692ddf04fc42 325 template <typename T>
vcoubard 1131:692ddf04fc42 326 ble_error_t onDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) {
vcoubard 1131:692ddf04fc42 327 if (!isOnDataReadAvailable()) {
vcoubard 1131:692ddf04fc42 328 return BLE_ERROR_NOT_IMPLEMENTED;
vcoubard 1131:692ddf04fc42 329 }
vcoubard 1131:692ddf04fc42 330
vcoubard 1131:692ddf04fc42 331 dataReadCallChain.add(objPtr, memberPtr);
vcoubard 1131:692ddf04fc42 332 return BLE_ERROR_NONE;
vcoubard 1131:692ddf04fc42 333 }
vcoubard 1131:692ddf04fc42 334
vcoubard 1131:692ddf04fc42 335 /**
vcoubard 1131:692ddf04fc42 336 * @brief provide access to the callchain of data read event callbacks
vcoubard 1131:692ddf04fc42 337 * It is possible to register callbacks using onDataRead().add(callback);
vcoubard 1131:692ddf04fc42 338 * It is possible to unregister callbacks using onDataRead().detach(callback)
vcoubard 1131:692ddf04fc42 339 * @return The data read event callbacks chain
vcoubard 1131:692ddf04fc42 340 */
vcoubard 1131:692ddf04fc42 341 DataReadCallbackChain_t& onDataRead() {
vcoubard 1131:692ddf04fc42 342 return dataReadCallChain;
vcoubard 1131:692ddf04fc42 343 }
vcoubard 1131:692ddf04fc42 344
vcoubard 1131:692ddf04fc42 345 /**
vcoubard 1131:692ddf04fc42 346 * Set up a callback for when notifications or indications are enabled for a
vcoubard 1131:692ddf04fc42 347 * characteristic on the local GATT server.
vcoubard 1131:692ddf04fc42 348 */
vcoubard 1131:692ddf04fc42 349 void onUpdatesEnabled(EventCallback_t callback) {updatesEnabledCallback = callback;}
vcoubard 1131:692ddf04fc42 350
vcoubard 1131:692ddf04fc42 351 /**
vcoubard 1131:692ddf04fc42 352 * Set up a callback for when notifications or indications are disabled for a
vcoubard 1131:692ddf04fc42 353 * characteristic on the local GATT server.
vcoubard 1131:692ddf04fc42 354 */
vcoubard 1131:692ddf04fc42 355 void onUpdatesDisabled(EventCallback_t callback) {updatesDisabledCallback = callback;}
vcoubard 1131:692ddf04fc42 356
vcoubard 1131:692ddf04fc42 357 /**
vcoubard 1131:692ddf04fc42 358 * Set up a callback for when the GATT server receives a response for an
vcoubard 1131:692ddf04fc42 359 * indication event sent previously.
vcoubard 1131:692ddf04fc42 360 */
vcoubard 1131:692ddf04fc42 361 void onConfirmationReceived(EventCallback_t callback) {confirmationReceivedCallback = callback;}
vcoubard 1131:692ddf04fc42 362
vcoubard 1131:692ddf04fc42 363 /* Entry points for the underlying stack to report events back to the user. */
vcoubard 1131:692ddf04fc42 364 protected:
vcoubard 1131:692ddf04fc42 365 void handleDataWrittenEvent(const GattWriteCallbackParams *params) {
vcoubard 1131:692ddf04fc42 366 dataWrittenCallChain.call(params);
vcoubard 1131:692ddf04fc42 367 }
vcoubard 1131:692ddf04fc42 368
vcoubard 1131:692ddf04fc42 369 void handleDataReadEvent(const GattReadCallbackParams *params) {
vcoubard 1131:692ddf04fc42 370 dataReadCallChain.call(params);
vcoubard 1131:692ddf04fc42 371 }
vcoubard 1131:692ddf04fc42 372
vcoubard 1131:692ddf04fc42 373 void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t attributeHandle) {
vcoubard 1131:692ddf04fc42 374 switch (type) {
vcoubard 1131:692ddf04fc42 375 case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
vcoubard 1131:692ddf04fc42 376 if (updatesEnabledCallback) {
vcoubard 1131:692ddf04fc42 377 updatesEnabledCallback(attributeHandle);
vcoubard 1131:692ddf04fc42 378 }
vcoubard 1131:692ddf04fc42 379 break;
vcoubard 1131:692ddf04fc42 380 case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
vcoubard 1131:692ddf04fc42 381 if (updatesDisabledCallback) {
vcoubard 1131:692ddf04fc42 382 updatesDisabledCallback(attributeHandle);
vcoubard 1131:692ddf04fc42 383 }
vcoubard 1131:692ddf04fc42 384 break;
vcoubard 1131:692ddf04fc42 385 case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
vcoubard 1131:692ddf04fc42 386 if (confirmationReceivedCallback) {
vcoubard 1131:692ddf04fc42 387 confirmationReceivedCallback(attributeHandle);
vcoubard 1131:692ddf04fc42 388 }
vcoubard 1131:692ddf04fc42 389 break;
vcoubard 1131:692ddf04fc42 390 default:
vcoubard 1131:692ddf04fc42 391 break;
vcoubard 1131:692ddf04fc42 392 }
vcoubard 1131:692ddf04fc42 393 }
vcoubard 1131:692ddf04fc42 394
vcoubard 1131:692ddf04fc42 395 void handleDataSentEvent(unsigned count) {
vcoubard 1131:692ddf04fc42 396 dataSentCallChain.call(count);
vcoubard 1131:692ddf04fc42 397 }
vcoubard 1131:692ddf04fc42 398
vcoubard 1131:692ddf04fc42 399 protected:
vcoubard 1131:692ddf04fc42 400 uint8_t serviceCount;
vcoubard 1131:692ddf04fc42 401 uint8_t characteristicCount;
vcoubard 1131:692ddf04fc42 402
vcoubard 1131:692ddf04fc42 403 private:
vcoubard 1131:692ddf04fc42 404 DataSentCallbackChain_t dataSentCallChain;
vcoubard 1131:692ddf04fc42 405 DataWrittenCallbackChain_t dataWrittenCallChain;
vcoubard 1131:692ddf04fc42 406 DataReadCallbackChain_t dataReadCallChain;
vcoubard 1131:692ddf04fc42 407 EventCallback_t updatesEnabledCallback;
vcoubard 1131:692ddf04fc42 408 EventCallback_t updatesDisabledCallback;
vcoubard 1131:692ddf04fc42 409 EventCallback_t confirmationReceivedCallback;
vcoubard 1131:692ddf04fc42 410
vcoubard 1131:692ddf04fc42 411 private:
vcoubard 1131:692ddf04fc42 412 /* Disallow copy and assignment. */
vcoubard 1131:692ddf04fc42 413 GattServer(const GattServer &);
vcoubard 1131:692ddf04fc42 414 GattServer& operator=(const GattServer &);
vcoubard 1131:692ddf04fc42 415 };
vcoubard 1131:692ddf04fc42 416
rgrover1 710:b2e1a2660ec2 417 #endif // ifndef __GATT_SERVER_H__