just a fork

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:54 2016 +0100
Revision:
1135:22aada733dbd
Parent:
1131:692ddf04fc42
Child:
1165:0717ca5ed305
Synchronized with git rev 17e37360
Author: Vincent Coubard
Merge pull request #161 from ARMmbed/develop

merge branch develop (v2.4.0)

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