Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:0e018d759a2a 1 /* mbed Microcontroller Library
switches 0:0e018d759a2a 2 * Copyright (c) 2006-2013 ARM Limited
switches 0:0e018d759a2a 3 *
switches 0:0e018d759a2a 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:0e018d759a2a 5 * you may not use this file except in compliance with the License.
switches 0:0e018d759a2a 6 * You may obtain a copy of the License at
switches 0:0e018d759a2a 7 *
switches 0:0e018d759a2a 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:0e018d759a2a 9 *
switches 0:0e018d759a2a 10 * Unless required by applicable law or agreed to in writing, software
switches 0:0e018d759a2a 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:0e018d759a2a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:0e018d759a2a 13 * See the License for the specific language governing permissions and
switches 0:0e018d759a2a 14 * limitations under the License.
switches 0:0e018d759a2a 15 */
switches 0:0e018d759a2a 16
switches 0:0e018d759a2a 17 #ifndef __GATT_SERVER_H__
switches 0:0e018d759a2a 18 #define __GATT_SERVER_H__
switches 0:0e018d759a2a 19
switches 0:0e018d759a2a 20 #include "Gap.h"
switches 0:0e018d759a2a 21 #include "GattService.h"
switches 0:0e018d759a2a 22 #include "GattAttribute.h"
switches 0:0e018d759a2a 23 #include "GattServerEvents.h"
switches 0:0e018d759a2a 24 #include "GattCallbackParamTypes.h"
switches 0:0e018d759a2a 25 #include "CallChainOfFunctionPointersWithContext.h"
switches 0:0e018d759a2a 26
switches 0:0e018d759a2a 27 class GattServer {
switches 0:0e018d759a2a 28 public:
switches 0:0e018d759a2a 29 /**
switches 0:0e018d759a2a 30 * Type for the registered callbacks added to the data sent callchain.
switches 0:0e018d759a2a 31 * Refer to GattServer::onDataSent().
switches 0:0e018d759a2a 32 */
switches 0:0e018d759a2a 33 typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
switches 0:0e018d759a2a 34 /**
switches 0:0e018d759a2a 35 * Type for the data sent event callchain. Refer to GattServer::onDataSent().
switches 0:0e018d759a2a 36 */
switches 0:0e018d759a2a 37 typedef CallChainOfFunctionPointersWithContext<unsigned> DataSentCallbackChain_t;
switches 0:0e018d759a2a 38
switches 0:0e018d759a2a 39 /**
switches 0:0e018d759a2a 40 * Type for the registered callbacks added to the data written callchain.
switches 0:0e018d759a2a 41 * Refer to GattServer::onDataWritten().
switches 0:0e018d759a2a 42 */
switches 0:0e018d759a2a 43 typedef FunctionPointerWithContext<const GattWriteCallbackParams*> DataWrittenCallback_t;
switches 0:0e018d759a2a 44 /**
switches 0:0e018d759a2a 45 * Type for the data written event callchain. Refer to GattServer::onDataWritten().
switches 0:0e018d759a2a 46 */
switches 0:0e018d759a2a 47 typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*> DataWrittenCallbackChain_t;
switches 0:0e018d759a2a 48
switches 0:0e018d759a2a 49 /**
switches 0:0e018d759a2a 50 * Type for the registered callbacks added to the data read callchain.
switches 0:0e018d759a2a 51 * Refer to GattServer::onDataRead().
switches 0:0e018d759a2a 52 */
switches 0:0e018d759a2a 53 typedef FunctionPointerWithContext<const GattReadCallbackParams*> DataReadCallback_t;
switches 0:0e018d759a2a 54 /**
switches 0:0e018d759a2a 55 * Type for the data read event callchain. Refer to GattServer::onDataRead().
switches 0:0e018d759a2a 56 */
switches 0:0e018d759a2a 57 typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> DataReadCallbackChain_t;
switches 0:0e018d759a2a 58
switches 0:0e018d759a2a 59 /**
switches 0:0e018d759a2a 60 * Type for the registered callbacks added to the shutdown callchain.
switches 0:0e018d759a2a 61 * Refer to GattServer::onShutdown().
switches 0:0e018d759a2a 62 */
switches 0:0e018d759a2a 63 typedef FunctionPointerWithContext<const GattServer *> GattServerShutdownCallback_t;
switches 0:0e018d759a2a 64 /**
switches 0:0e018d759a2a 65 * Type for the shutdown event callchain. Refer to GattServer::onShutdown().
switches 0:0e018d759a2a 66 */
switches 0:0e018d759a2a 67 typedef CallChainOfFunctionPointersWithContext<const GattServer *> GattServerShutdownCallbackChain_t;
switches 0:0e018d759a2a 68
switches 0:0e018d759a2a 69 /**
switches 0:0e018d759a2a 70 * Type for the registered callback for various events. Refer to
switches 0:0e018d759a2a 71 * GattServer::onUpdatesEnabled(), GattServer::onUpdateDisabled() and
switches 0:0e018d759a2a 72 * GattServer::onConfirmationReceived().
switches 0:0e018d759a2a 73 */
switches 0:0e018d759a2a 74 typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;
switches 0:0e018d759a2a 75
switches 0:0e018d759a2a 76 protected:
switches 0:0e018d759a2a 77 /**
switches 0:0e018d759a2a 78 * Construct a GattServer instance.
switches 0:0e018d759a2a 79 */
switches 0:0e018d759a2a 80 GattServer() :
switches 0:0e018d759a2a 81 serviceCount(0),
switches 0:0e018d759a2a 82 characteristicCount(0),
switches 0:0e018d759a2a 83 dataSentCallChain(),
switches 0:0e018d759a2a 84 dataWrittenCallChain(),
switches 0:0e018d759a2a 85 dataReadCallChain(),
switches 0:0e018d759a2a 86 updatesEnabledCallback(NULL),
switches 0:0e018d759a2a 87 updatesDisabledCallback(NULL),
switches 0:0e018d759a2a 88 confirmationReceivedCallback(NULL) {
switches 0:0e018d759a2a 89 /* empty */
switches 0:0e018d759a2a 90 }
switches 0:0e018d759a2a 91
switches 0:0e018d759a2a 92 /*
switches 0:0e018d759a2a 93 * The following functions are meant to be overridden in the platform-specific sub-class.
switches 0:0e018d759a2a 94 */
switches 0:0e018d759a2a 95 public:
switches 0:0e018d759a2a 96
switches 0:0e018d759a2a 97 /**
switches 0:0e018d759a2a 98 * Add a service declaration to the local server ATT table. Also add the
switches 0:0e018d759a2a 99 * characteristics contained within.
switches 0:0e018d759a2a 100 *
switches 0:0e018d759a2a 101 * @param[in] service
switches 0:0e018d759a2a 102 * The service to be added.
switches 0:0e018d759a2a 103 *
switches 0:0e018d759a2a 104 * @return BLE_ERROR_NONE if the service was successfully added.
switches 0:0e018d759a2a 105 */
switches 0:0e018d759a2a 106 virtual ble_error_t addService(GattService &service) {
switches 0:0e018d759a2a 107 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 108 (void)service;
switches 0:0e018d759a2a 109
switches 0:0e018d759a2a 110 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 111 }
switches 0:0e018d759a2a 112
switches 0:0e018d759a2a 113 /**
switches 0:0e018d759a2a 114 * Read the value of a characteristic from the local GATT server.
switches 0:0e018d759a2a 115 *
switches 0:0e018d759a2a 116 * @param[in] attributeHandle
switches 0:0e018d759a2a 117 * Attribute handle for the value attribute of the characteristic.
switches 0:0e018d759a2a 118 * @param[out] buffer
switches 0:0e018d759a2a 119 * A buffer to hold the value being read.
switches 0:0e018d759a2a 120 * @param[in,out] lengthP
switches 0:0e018d759a2a 121 * Length of the buffer being supplied. If the attribute
switches 0:0e018d759a2a 122 * value is longer than the size of the supplied buffer,
switches 0:0e018d759a2a 123 * this variable will hold upon return the total attribute value length
switches 0:0e018d759a2a 124 * (excluding offset). The application may use this
switches 0:0e018d759a2a 125 * information to allocate a suitable buffer size.
switches 0:0e018d759a2a 126 *
switches 0:0e018d759a2a 127 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
switches 0:0e018d759a2a 128 */
switches 0:0e018d759a2a 129 virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) {
switches 0:0e018d759a2a 130 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 131 (void)attributeHandle;
switches 0:0e018d759a2a 132 (void)buffer;
switches 0:0e018d759a2a 133 (void)lengthP;
switches 0:0e018d759a2a 134
switches 0:0e018d759a2a 135 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 136 }
switches 0:0e018d759a2a 137
switches 0:0e018d759a2a 138 /**
switches 0:0e018d759a2a 139 * Read the value of a characteristic from the local GATT server.
switches 0:0e018d759a2a 140 *
switches 0:0e018d759a2a 141 * @param[in] connectionHandle
switches 0:0e018d759a2a 142 * Connection handle.
switches 0:0e018d759a2a 143 * @param[in] attributeHandle
switches 0:0e018d759a2a 144 * Attribute handle for the value attribute of the characteristic.
switches 0:0e018d759a2a 145 * @param[out] buffer
switches 0:0e018d759a2a 146 * A buffer to hold the value being read.
switches 0:0e018d759a2a 147 * @param[in,out] lengthP
switches 0:0e018d759a2a 148 * Length of the buffer being supplied. If the attribute
switches 0:0e018d759a2a 149 * value is longer than the size of the supplied buffer,
switches 0:0e018d759a2a 150 * this variable will hold upon return the total attribute value length
switches 0:0e018d759a2a 151 * (excluding offset). The application may use this
switches 0:0e018d759a2a 152 * information to allocate a suitable buffer size.
switches 0:0e018d759a2a 153 *
switches 0:0e018d759a2a 154 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
switches 0:0e018d759a2a 155 *
switches 0:0e018d759a2a 156 * @note This API is a version of the above, with an additional connection handle
switches 0:0e018d759a2a 157 * parameter to allow fetches for connection-specific multivalued
switches 0:0e018d759a2a 158 * attributes (such as the CCCDs).
switches 0:0e018d759a2a 159 */
switches 0:0e018d759a2a 160 virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t *buffer, uint16_t *lengthP) {
switches 0:0e018d759a2a 161 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 162 (void)connectionHandle;
switches 0:0e018d759a2a 163 (void)attributeHandle;
switches 0:0e018d759a2a 164 (void)buffer;
switches 0:0e018d759a2a 165 (void)lengthP;
switches 0:0e018d759a2a 166
switches 0:0e018d759a2a 167 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 168 }
switches 0:0e018d759a2a 169
switches 0:0e018d759a2a 170 /**
switches 0:0e018d759a2a 171 * Update the value of a characteristic on the local GATT server.
switches 0:0e018d759a2a 172 *
switches 0:0e018d759a2a 173 * @param[in] attributeHandle
switches 0:0e018d759a2a 174 * Handle for the value attribute of the characteristic.
switches 0:0e018d759a2a 175 * @param[in] value
switches 0:0e018d759a2a 176 * A pointer to a buffer holding the new value.
switches 0:0e018d759a2a 177 * @param[in] size
switches 0:0e018d759a2a 178 * Size of the new value (in bytes).
switches 0:0e018d759a2a 179 * @param[in] localOnly
switches 0:0e018d759a2a 180 * Should this update be kept on the local
switches 0:0e018d759a2a 181 * GATT server regardless of the state of the
switches 0:0e018d759a2a 182 * notify/indicate flag in the CCCD for this
switches 0:0e018d759a2a 183 * Characteristic? If set to true, no notification
switches 0:0e018d759a2a 184 * or indication is generated.
switches 0:0e018d759a2a 185 *
switches 0:0e018d759a2a 186 * @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
switches 0:0e018d759a2a 187 */
switches 0:0e018d759a2a 188 virtual ble_error_t write(GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) {
switches 0:0e018d759a2a 189 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 190 (void)attributeHandle;
switches 0:0e018d759a2a 191 (void)value;
switches 0:0e018d759a2a 192 (void)size;
switches 0:0e018d759a2a 193 (void)localOnly;
switches 0:0e018d759a2a 194
switches 0:0e018d759a2a 195 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 196 }
switches 0:0e018d759a2a 197
switches 0:0e018d759a2a 198 /**
switches 0:0e018d759a2a 199 * Update the value of a characteristic on the local GATT server. A version
switches 0:0e018d759a2a 200 * of the same as the above, with a connection handle parameter to allow updates
switches 0:0e018d759a2a 201 * for connection-specific multivalued attributes (such as the CCCDs).
switches 0:0e018d759a2a 202 *
switches 0:0e018d759a2a 203 * @param[in] connectionHandle
switches 0:0e018d759a2a 204 * Connection handle.
switches 0:0e018d759a2a 205 * @param[in] attributeHandle
switches 0:0e018d759a2a 206 * Handle for the value attribute of the characteristic.
switches 0:0e018d759a2a 207 * @param[in] value
switches 0:0e018d759a2a 208 * A pointer to a buffer holding the new value.
switches 0:0e018d759a2a 209 * @param[in] size
switches 0:0e018d759a2a 210 * Size of the new value (in bytes).
switches 0:0e018d759a2a 211 * @param[in] localOnly
switches 0:0e018d759a2a 212 * Should this update be kept on the local
switches 0:0e018d759a2a 213 * GattServer regardless of the state of the
switches 0:0e018d759a2a 214 * notify/indicate flag in the CCCD for this
switches 0:0e018d759a2a 215 * Characteristic? If set to true, no notification
switches 0:0e018d759a2a 216 * or indication is generated.
switches 0:0e018d759a2a 217 *
switches 0:0e018d759a2a 218 * @return BLE_ERROR_NONE if we have successfully set the value of the attribute.
switches 0:0e018d759a2a 219 */
switches 0:0e018d759a2a 220 virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) {
switches 0:0e018d759a2a 221 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 222 (void)connectionHandle;
switches 0:0e018d759a2a 223 (void)attributeHandle;
switches 0:0e018d759a2a 224 (void)value;
switches 0:0e018d759a2a 225 (void)size;
switches 0:0e018d759a2a 226 (void)localOnly;
switches 0:0e018d759a2a 227
switches 0:0e018d759a2a 228 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 229 }
switches 0:0e018d759a2a 230
switches 0:0e018d759a2a 231 /**
switches 0:0e018d759a2a 232 * Determine the updates-enabled status (notification or indication) for the current connection from a characteristic's CCCD.
switches 0:0e018d759a2a 233 *
switches 0:0e018d759a2a 234 * @param[in] characteristic
switches 0:0e018d759a2a 235 * The characteristic.
switches 0:0e018d759a2a 236 * @param[out] enabledP
switches 0:0e018d759a2a 237 * Upon return, *enabledP is true if updates are enabled, else false.
switches 0:0e018d759a2a 238 *
switches 0:0e018d759a2a 239 * @return BLE_ERROR_NONE if the connection and handle are found. False otherwise.
switches 0:0e018d759a2a 240 */
switches 0:0e018d759a2a 241 virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) {
switches 0:0e018d759a2a 242 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 243 (void)characteristic;
switches 0:0e018d759a2a 244 (void)enabledP;
switches 0:0e018d759a2a 245
switches 0:0e018d759a2a 246 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 247 }
switches 0:0e018d759a2a 248
switches 0:0e018d759a2a 249 /**
switches 0:0e018d759a2a 250 * Determine the connection-specific updates-enabled status (notification or indication) from a characteristic's CCCD.
switches 0:0e018d759a2a 251 *
switches 0:0e018d759a2a 252 * @param[in] connectionHandle
switches 0:0e018d759a2a 253 * The connection handle.
switches 0:0e018d759a2a 254 * @param[in] characteristic
switches 0:0e018d759a2a 255 * The characteristic.
switches 0:0e018d759a2a 256 * @param[out] enabledP
switches 0:0e018d759a2a 257 * Upon return, *enabledP is true if updates are enabled, else false.
switches 0:0e018d759a2a 258 *
switches 0:0e018d759a2a 259 * @return BLE_ERROR_NONE if the connection and handle are found. False otherwise.
switches 0:0e018d759a2a 260 */
switches 0:0e018d759a2a 261 virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) {
switches 0:0e018d759a2a 262 /* Avoid compiler warnings about unused variables. */
switches 0:0e018d759a2a 263 (void)connectionHandle;
switches 0:0e018d759a2a 264 (void)characteristic;
switches 0:0e018d759a2a 265 (void)enabledP;
switches 0:0e018d759a2a 266
switches 0:0e018d759a2a 267 return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 268 }
switches 0:0e018d759a2a 269
switches 0:0e018d759a2a 270 /**
switches 0:0e018d759a2a 271 * A virtual function to allow underlying stacks to indicate if they support
switches 0:0e018d759a2a 272 * onDataRead(). It should be overridden to return true as applicable.
switches 0:0e018d759a2a 273 *
switches 0:0e018d759a2a 274 * @return true if onDataRead is supported, false otherwise.
switches 0:0e018d759a2a 275 */
switches 0:0e018d759a2a 276 virtual bool isOnDataReadAvailable() const {
switches 0:0e018d759a2a 277 return false; /* Requesting action from porters: override this API if this capability is supported. */
switches 0:0e018d759a2a 278 }
switches 0:0e018d759a2a 279
switches 0:0e018d759a2a 280 /*
switches 0:0e018d759a2a 281 * APIs with non-virtual implementations.
switches 0:0e018d759a2a 282 */
switches 0:0e018d759a2a 283 public:
switches 0:0e018d759a2a 284 /**
switches 0:0e018d759a2a 285 * Add a callback for the GATT event DATA_SENT (which is triggered when
switches 0:0e018d759a2a 286 * updates are sent out by GATT in the form of notifications).
switches 0:0e018d759a2a 287 *
switches 0:0e018d759a2a 288 * @param[in] callback
switches 0:0e018d759a2a 289 * Event handler being registered.
switches 0:0e018d759a2a 290 *
switches 0:0e018d759a2a 291 * @note It is possible to chain together multiple onDataSent callbacks
switches 0:0e018d759a2a 292 * (potentially from different modules of an application) to receive updates
switches 0:0e018d759a2a 293 * to characteristics.
switches 0:0e018d759a2a 294 *
switches 0:0e018d759a2a 295 * @note It is also possible to set up a callback into a member function of
switches 0:0e018d759a2a 296 * some object.
switches 0:0e018d759a2a 297 */
switches 0:0e018d759a2a 298 void onDataSent(const DataSentCallback_t& callback) {
switches 0:0e018d759a2a 299 dataSentCallChain.add(callback);
switches 0:0e018d759a2a 300 }
switches 0:0e018d759a2a 301
switches 0:0e018d759a2a 302 /**
switches 0:0e018d759a2a 303 * Same as GattServer::onDataSent(), but allows the possibility to add an object
switches 0:0e018d759a2a 304 * reference and member function as handler for DATA_SENT event
switches 0:0e018d759a2a 305 * callbacks.
switches 0:0e018d759a2a 306 *
switches 0:0e018d759a2a 307 * @param[in] objPtr
switches 0:0e018d759a2a 308 * Pointer to the object of a class defining the member callback
switches 0:0e018d759a2a 309 * function (@p memberPtr).
switches 0:0e018d759a2a 310 * @param[in] memberPtr
switches 0:0e018d759a2a 311 * The member callback (within the context of an object) to be
switches 0:0e018d759a2a 312 * invoked.
switches 0:0e018d759a2a 313 */
switches 0:0e018d759a2a 314 template <typename T>
switches 0:0e018d759a2a 315 void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) {
switches 0:0e018d759a2a 316 dataSentCallChain.add(objPtr, memberPtr);
switches 0:0e018d759a2a 317 }
switches 0:0e018d759a2a 318
switches 0:0e018d759a2a 319 /**
switches 0:0e018d759a2a 320 * @brief Provide access to the callchain of DATA_SENT event callbacks.
switches 0:0e018d759a2a 321 *
switches 0:0e018d759a2a 322 * @return A reference to the DATA_SENT event callback chain.
switches 0:0e018d759a2a 323 */
switches 0:0e018d759a2a 324 DataSentCallbackChain_t& onDataSent() {
switches 0:0e018d759a2a 325 return dataSentCallChain;
switches 0:0e018d759a2a 326 }
switches 0:0e018d759a2a 327
switches 0:0e018d759a2a 328 /**
switches 0:0e018d759a2a 329 * Set up a callback for when an attribute has its value updated by or at the
switches 0:0e018d759a2a 330 * connected peer. For a peripheral, this callback is triggered when the local
switches 0:0e018d759a2a 331 * GATT server has an attribute updated by a write command from the peer.
switches 0:0e018d759a2a 332 * For a central, this callback is triggered when a response is received for
switches 0:0e018d759a2a 333 * a write request.
switches 0:0e018d759a2a 334 *
switches 0:0e018d759a2a 335 * @param[in] callback
switches 0:0e018d759a2a 336 * Event handler being registered.
switches 0:0e018d759a2a 337 *
switches 0:0e018d759a2a 338 * @note It is possible to chain together multiple onDataWritten callbacks
switches 0:0e018d759a2a 339 * (potentially from different modules of an application) to receive updates
switches 0:0e018d759a2a 340 * to characteristics. Many services, such as DFU and UART, add their own
switches 0:0e018d759a2a 341 * onDataWritten callbacks behind the scenes to trap interesting events.
switches 0:0e018d759a2a 342 *
switches 0:0e018d759a2a 343 * @note It is also possible to set up a callback into a member function of
switches 0:0e018d759a2a 344 * some object.
switches 0:0e018d759a2a 345 *
switches 0:0e018d759a2a 346 * @note It is possible to unregister a callback using onDataWritten().detach(callback)
switches 0:0e018d759a2a 347 */
switches 0:0e018d759a2a 348 void onDataWritten(const DataWrittenCallback_t& callback) {
switches 0:0e018d759a2a 349 dataWrittenCallChain.add(callback);
switches 0:0e018d759a2a 350 }
switches 0:0e018d759a2a 351
switches 0:0e018d759a2a 352 /**
switches 0:0e018d759a2a 353 * Same as GattServer::onDataWritten(), but allows the possibility to add an object
switches 0:0e018d759a2a 354 * reference and member function as handler for data written event
switches 0:0e018d759a2a 355 * callbacks.
switches 0:0e018d759a2a 356 *
switches 0:0e018d759a2a 357 * @param[in] objPtr
switches 0:0e018d759a2a 358 * Pointer to the object of a class defining the member callback
switches 0:0e018d759a2a 359 * function (@p memberPtr).
switches 0:0e018d759a2a 360 * @param[in] memberPtr
switches 0:0e018d759a2a 361 * The member callback (within the context of an object) to be
switches 0:0e018d759a2a 362 * invoked.
switches 0:0e018d759a2a 363 */
switches 0:0e018d759a2a 364 template <typename T>
switches 0:0e018d759a2a 365 void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) {
switches 0:0e018d759a2a 366 dataWrittenCallChain.add(objPtr, memberPtr);
switches 0:0e018d759a2a 367 }
switches 0:0e018d759a2a 368
switches 0:0e018d759a2a 369 /**
switches 0:0e018d759a2a 370 * @brief Provide access to the callchain of data written event callbacks.
switches 0:0e018d759a2a 371 *
switches 0:0e018d759a2a 372 * @return A reference to the data written event callbacks chain.
switches 0:0e018d759a2a 373 *
switches 0:0e018d759a2a 374 * @note It is possible to register callbacks using onDataWritten().add(callback).
switches 0:0e018d759a2a 375 *
switches 0:0e018d759a2a 376 * @note It is possible to unregister callbacks using onDataWritten().detach(callback).
switches 0:0e018d759a2a 377 */
switches 0:0e018d759a2a 378 DataWrittenCallbackChain_t& onDataWritten() {
switches 0:0e018d759a2a 379 return dataWrittenCallChain;
switches 0:0e018d759a2a 380 }
switches 0:0e018d759a2a 381
switches 0:0e018d759a2a 382 /**
switches 0:0e018d759a2a 383 * Setup a callback to be invoked on the peripheral when an attribute is
switches 0:0e018d759a2a 384 * being read by a remote client.
switches 0:0e018d759a2a 385 *
switches 0:0e018d759a2a 386 * @param[in] callback
switches 0:0e018d759a2a 387 * Event handler being registered.
switches 0:0e018d759a2a 388 *
switches 0:0e018d759a2a 389 * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
switches 0:0e018d759a2a 390 * else BLE_ERROR_NONE.
switches 0:0e018d759a2a 391 *
switches 0:0e018d759a2a 392 * @note This functionality may not be available on all underlying stacks.
switches 0:0e018d759a2a 393 * You could use GattCharacteristic::setReadAuthorizationCallback() as an
switches 0:0e018d759a2a 394 * alternative. Refer to isOnDataReadAvailable().
switches 0:0e018d759a2a 395 *
switches 0:0e018d759a2a 396 * @note It is possible to chain together multiple onDataRead callbacks
switches 0:0e018d759a2a 397 * (potentially from different modules of an application) to receive updates
switches 0:0e018d759a2a 398 * to characteristics. Services may add their own onDataRead callbacks
switches 0:0e018d759a2a 399 * behind the scenes to trap interesting events.
switches 0:0e018d759a2a 400 *
switches 0:0e018d759a2a 401 * @note It is also possible to set up a callback into a member function of
switches 0:0e018d759a2a 402 * some object.
switches 0:0e018d759a2a 403 *
switches 0:0e018d759a2a 404 * @note It is possible to unregister a callback using onDataRead().detach(callback).
switches 0:0e018d759a2a 405 */
switches 0:0e018d759a2a 406 ble_error_t onDataRead(const DataReadCallback_t& callback) {
switches 0:0e018d759a2a 407 if (!isOnDataReadAvailable()) {
switches 0:0e018d759a2a 408 return BLE_ERROR_NOT_IMPLEMENTED;
switches 0:0e018d759a2a 409 }
switches 0:0e018d759a2a 410
switches 0:0e018d759a2a 411 dataReadCallChain.add(callback);
switches 0:0e018d759a2a 412 return BLE_ERROR_NONE;
switches 0:0e018d759a2a 413 }
switches 0:0e018d759a2a 414
switches 0:0e018d759a2a 415 /**
switches 0:0e018d759a2a 416 * Same as GattServer::onDataRead(), but allows the possibility to add an object
switches 0:0e018d759a2a 417 * reference and member function as handler for data read event
switches 0:0e018d759a2a 418 * callbacks.
switches 0:0e018d759a2a 419 *
switches 0:0e018d759a2a 420 * @param[in] objPtr
switches 0:0e018d759a2a 421 * Pointer to the object of a class defining the member callback
switches 0:0e018d759a2a 422 * function (@p memberPtr).
switches 0:0e018d759a2a 423 * @param[in] memberPtr
switches 0:0e018d759a2a 424 * The member callback (within the context of an object) to be
switches 0:0e018d759a2a 425 * invoked.
switches 0:0e018d759a2a 426 */
switches 0:0e018d759a2a 427 template <typename T>
switches 0:0e018d759a2a 428 ble_error_t onDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) {
switches 0:0e018d759a2a 429 if (!isOnDataReadAvailable()) {
switches 0:0e018d759a2a 430 return BLE_ERROR_NOT_IMPLEMENTED;
switches 0:0e018d759a2a 431 }
switches 0:0e018d759a2a 432
switches 0:0e018d759a2a 433 dataReadCallChain.add(objPtr, memberPtr);
switches 0:0e018d759a2a 434 return BLE_ERROR_NONE;
switches 0:0e018d759a2a 435 }
switches 0:0e018d759a2a 436
switches 0:0e018d759a2a 437 /**
switches 0:0e018d759a2a 438 * @brief Provide access to the callchain of data read event callbacks.
switches 0:0e018d759a2a 439 *
switches 0:0e018d759a2a 440 * @return A reference to the data read event callbacks chain.
switches 0:0e018d759a2a 441 *
switches 0:0e018d759a2a 442 * @note It is possible to register callbacks using onDataRead().add(callback).
switches 0:0e018d759a2a 443 *
switches 0:0e018d759a2a 444 * @note It is possible to unregister callbacks using onDataRead().detach(callback).
switches 0:0e018d759a2a 445 */
switches 0:0e018d759a2a 446 DataReadCallbackChain_t& onDataRead() {
switches 0:0e018d759a2a 447 return dataReadCallChain;
switches 0:0e018d759a2a 448 }
switches 0:0e018d759a2a 449
switches 0:0e018d759a2a 450 /**
switches 0:0e018d759a2a 451 * Setup a callback to be invoked to notify the user application that the
switches 0:0e018d759a2a 452 * GattServer instance is about to shutdown (possibly as a result of a call
switches 0:0e018d759a2a 453 * to BLE::shutdown()).
switches 0:0e018d759a2a 454 *
switches 0:0e018d759a2a 455 * @param[in] callback
switches 0:0e018d759a2a 456 * Event handler being registered.
switches 0:0e018d759a2a 457 *
switches 0:0e018d759a2a 458 * @note It is possible to chain together multiple onShutdown callbacks
switches 0:0e018d759a2a 459 * (potentially from different modules of an application) to be notified
switches 0:0e018d759a2a 460 * before the GattServer is shutdown.
switches 0:0e018d759a2a 461 *
switches 0:0e018d759a2a 462 * @note It is also possible to set up a callback into a member function of
switches 0:0e018d759a2a 463 * some object.
switches 0:0e018d759a2a 464 *
switches 0:0e018d759a2a 465 * @note It is possible to unregister a callback using onShutdown().detach(callback)
switches 0:0e018d759a2a 466 */
switches 0:0e018d759a2a 467 void onShutdown(const GattServerShutdownCallback_t& callback) {
switches 0:0e018d759a2a 468 shutdownCallChain.add(callback);
switches 0:0e018d759a2a 469 }
switches 0:0e018d759a2a 470
switches 0:0e018d759a2a 471 /**
switches 0:0e018d759a2a 472 * Same as GattServer::onShutdown(), but allows the possibility to add an object
switches 0:0e018d759a2a 473 * reference and member function as handler for shutdown event
switches 0:0e018d759a2a 474 * callbacks.
switches 0:0e018d759a2a 475 *
switches 0:0e018d759a2a 476 * @param[in] objPtr
switches 0:0e018d759a2a 477 * Pointer to the object of a class defining the member callback
switches 0:0e018d759a2a 478 * function (@p memberPtr).
switches 0:0e018d759a2a 479 * @param[in] memberPtr
switches 0:0e018d759a2a 480 * The member callback (within the context of an object) to be
switches 0:0e018d759a2a 481 * invoked.
switches 0:0e018d759a2a 482 */
switches 0:0e018d759a2a 483 template <typename T>
switches 0:0e018d759a2a 484 void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *)) {
switches 0:0e018d759a2a 485 shutdownCallChain.add(objPtr, memberPtr);
switches 0:0e018d759a2a 486 }
switches 0:0e018d759a2a 487
switches 0:0e018d759a2a 488 /**
switches 0:0e018d759a2a 489 * @brief Provide access to the callchain of shutdown event callbacks.
switches 0:0e018d759a2a 490 *
switches 0:0e018d759a2a 491 * @return A reference to the shutdown event callbacks chain.
switches 0:0e018d759a2a 492 *
switches 0:0e018d759a2a 493 * @note It is possible to register callbacks using onShutdown().add(callback).
switches 0:0e018d759a2a 494 *
switches 0:0e018d759a2a 495 * @note It is possible to unregister callbacks using onShutdown().detach(callback).
switches 0:0e018d759a2a 496 */
switches 0:0e018d759a2a 497 GattServerShutdownCallbackChain_t& onShutdown() {
switches 0:0e018d759a2a 498 return shutdownCallChain;
switches 0:0e018d759a2a 499 }
switches 0:0e018d759a2a 500
switches 0:0e018d759a2a 501 /**
switches 0:0e018d759a2a 502 * Set up a callback for when notifications or indications are enabled for a
switches 0:0e018d759a2a 503 * characteristic on the local GATT server.
switches 0:0e018d759a2a 504 *
switches 0:0e018d759a2a 505 * @param[in] callback
switches 0:0e018d759a2a 506 * Event handler being registered.
switches 0:0e018d759a2a 507 */
switches 0:0e018d759a2a 508 void onUpdatesEnabled(EventCallback_t callback) {
switches 0:0e018d759a2a 509 updatesEnabledCallback = callback;
switches 0:0e018d759a2a 510 }
switches 0:0e018d759a2a 511
switches 0:0e018d759a2a 512 /**
switches 0:0e018d759a2a 513 * Set up a callback for when notifications or indications are disabled for a
switches 0:0e018d759a2a 514 * characteristic on the local GATT server.
switches 0:0e018d759a2a 515 *
switches 0:0e018d759a2a 516 * @param[in] callback
switches 0:0e018d759a2a 517 * Event handler being registered.
switches 0:0e018d759a2a 518 */
switches 0:0e018d759a2a 519 void onUpdatesDisabled(EventCallback_t callback) {
switches 0:0e018d759a2a 520 updatesDisabledCallback = callback;
switches 0:0e018d759a2a 521 }
switches 0:0e018d759a2a 522
switches 0:0e018d759a2a 523 /**
switches 0:0e018d759a2a 524 * Set up a callback for when the GATT server receives a response for an
switches 0:0e018d759a2a 525 * indication event sent previously.
switches 0:0e018d759a2a 526 *
switches 0:0e018d759a2a 527 * @param[in] callback
switches 0:0e018d759a2a 528 * Event handler being registered.
switches 0:0e018d759a2a 529 */
switches 0:0e018d759a2a 530 void onConfirmationReceived(EventCallback_t callback) {
switches 0:0e018d759a2a 531 confirmationReceivedCallback = callback;
switches 0:0e018d759a2a 532 }
switches 0:0e018d759a2a 533
switches 0:0e018d759a2a 534 /* Entry points for the underlying stack to report events back to the user. */
switches 0:0e018d759a2a 535 protected:
switches 0:0e018d759a2a 536 /**
switches 0:0e018d759a2a 537 * Helper function that notifies all registered handlers of an occurrence
switches 0:0e018d759a2a 538 * of a data written event. This function is meant to be called from the
switches 0:0e018d759a2a 539 * BLE stack specific implementation when a data written event occurs.
switches 0:0e018d759a2a 540 *
switches 0:0e018d759a2a 541 * @param[in] params
switches 0:0e018d759a2a 542 * The data written parameters passed to the registered
switches 0:0e018d759a2a 543 * handlers.
switches 0:0e018d759a2a 544 */
switches 0:0e018d759a2a 545 void handleDataWrittenEvent(const GattWriteCallbackParams *params) {
switches 0:0e018d759a2a 546 dataWrittenCallChain.call(params);
switches 0:0e018d759a2a 547 }
switches 0:0e018d759a2a 548
switches 0:0e018d759a2a 549 /**
switches 0:0e018d759a2a 550 * Helper function that notifies all registered handlers of an occurrence
switches 0:0e018d759a2a 551 * of a data read event. This function is meant to be called from the
switches 0:0e018d759a2a 552 * BLE stack specific implementation when a data read event occurs.
switches 0:0e018d759a2a 553 *
switches 0:0e018d759a2a 554 * @param[in] params
switches 0:0e018d759a2a 555 * The data read parameters passed to the registered
switches 0:0e018d759a2a 556 * handlers.
switches 0:0e018d759a2a 557 */
switches 0:0e018d759a2a 558 void handleDataReadEvent(const GattReadCallbackParams *params) {
switches 0:0e018d759a2a 559 dataReadCallChain.call(params);
switches 0:0e018d759a2a 560 }
switches 0:0e018d759a2a 561
switches 0:0e018d759a2a 562 /**
switches 0:0e018d759a2a 563 * Helper function that notifies the registered handler of an occurrence
switches 0:0e018d759a2a 564 * of updates enabled, updates disabled and confirmation received events.
switches 0:0e018d759a2a 565 * This function is meant to be called from the BLE stack specific
switches 0:0e018d759a2a 566 * implementation when any of these events occurs.
switches 0:0e018d759a2a 567 *
switches 0:0e018d759a2a 568 * @param[in] type
switches 0:0e018d759a2a 569 * The type of event that occurred.
switches 0:0e018d759a2a 570 * @param[in] attributeHandle
switches 0:0e018d759a2a 571 * The handle of the attribute that was modified.
switches 0:0e018d759a2a 572 */
switches 0:0e018d759a2a 573 void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t attributeHandle) {
switches 0:0e018d759a2a 574 switch (type) {
switches 0:0e018d759a2a 575 case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
switches 0:0e018d759a2a 576 if (updatesEnabledCallback) {
switches 0:0e018d759a2a 577 updatesEnabledCallback(attributeHandle);
switches 0:0e018d759a2a 578 }
switches 0:0e018d759a2a 579 break;
switches 0:0e018d759a2a 580 case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
switches 0:0e018d759a2a 581 if (updatesDisabledCallback) {
switches 0:0e018d759a2a 582 updatesDisabledCallback(attributeHandle);
switches 0:0e018d759a2a 583 }
switches 0:0e018d759a2a 584 break;
switches 0:0e018d759a2a 585 case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
switches 0:0e018d759a2a 586 if (confirmationReceivedCallback) {
switches 0:0e018d759a2a 587 confirmationReceivedCallback(attributeHandle);
switches 0:0e018d759a2a 588 }
switches 0:0e018d759a2a 589 break;
switches 0:0e018d759a2a 590 default:
switches 0:0e018d759a2a 591 break;
switches 0:0e018d759a2a 592 }
switches 0:0e018d759a2a 593 }
switches 0:0e018d759a2a 594
switches 0:0e018d759a2a 595 /**
switches 0:0e018d759a2a 596 * Helper function that notifies all registered handlers of an occurrence
switches 0:0e018d759a2a 597 * of a data sent event. This function is meant to be called from the
switches 0:0e018d759a2a 598 * BLE stack specific implementation when a data sent event occurs.
switches 0:0e018d759a2a 599 *
switches 0:0e018d759a2a 600 * @param[in] count
switches 0:0e018d759a2a 601 * Number of packets sent.
switches 0:0e018d759a2a 602 */
switches 0:0e018d759a2a 603 void handleDataSentEvent(unsigned count) {
switches 0:0e018d759a2a 604 dataSentCallChain.call(count);
switches 0:0e018d759a2a 605 }
switches 0:0e018d759a2a 606
switches 0:0e018d759a2a 607 public:
switches 0:0e018d759a2a 608 /**
switches 0:0e018d759a2a 609 * Notify all registered onShutdown callbacks that the GattServer is
switches 0:0e018d759a2a 610 * about to be shutdown and clear all GattServer state of the
switches 0:0e018d759a2a 611 * associated object.
switches 0:0e018d759a2a 612 *
switches 0:0e018d759a2a 613 * This function is meant to be overridden in the platform-specific
switches 0:0e018d759a2a 614 * sub-class. Nevertheless, the sub-class is only expected to reset its
switches 0:0e018d759a2a 615 * state and not the data held in GattServer members. This shall be achieved
switches 0:0e018d759a2a 616 * by a call to GattServer::reset() from the sub-class' reset()
switches 0:0e018d759a2a 617 * implementation.
switches 0:0e018d759a2a 618 *
switches 0:0e018d759a2a 619 * @return BLE_ERROR_NONE on success.
switches 0:0e018d759a2a 620 */
switches 0:0e018d759a2a 621 virtual ble_error_t reset(void) {
switches 0:0e018d759a2a 622 /* Notify that the instance is about to shutdown */
switches 0:0e018d759a2a 623 shutdownCallChain.call(this);
switches 0:0e018d759a2a 624 shutdownCallChain.clear();
switches 0:0e018d759a2a 625
switches 0:0e018d759a2a 626 serviceCount = 0;
switches 0:0e018d759a2a 627 characteristicCount = 0;
switches 0:0e018d759a2a 628
switches 0:0e018d759a2a 629 dataSentCallChain.clear();
switches 0:0e018d759a2a 630 dataWrittenCallChain.clear();
switches 0:0e018d759a2a 631 dataReadCallChain.clear();
switches 0:0e018d759a2a 632 updatesEnabledCallback = NULL;
switches 0:0e018d759a2a 633 updatesDisabledCallback = NULL;
switches 0:0e018d759a2a 634 confirmationReceivedCallback = NULL;
switches 0:0e018d759a2a 635
switches 0:0e018d759a2a 636 return BLE_ERROR_NONE;
switches 0:0e018d759a2a 637 }
switches 0:0e018d759a2a 638
switches 0:0e018d759a2a 639 protected:
switches 0:0e018d759a2a 640 /**
switches 0:0e018d759a2a 641 * The total number of services added to the ATT table.
switches 0:0e018d759a2a 642 */
switches 0:0e018d759a2a 643 uint8_t serviceCount;
switches 0:0e018d759a2a 644 /**
switches 0:0e018d759a2a 645 * The total number of characteristics added to the ATT table.
switches 0:0e018d759a2a 646 */
switches 0:0e018d759a2a 647 uint8_t characteristicCount;
switches 0:0e018d759a2a 648
switches 0:0e018d759a2a 649 private:
switches 0:0e018d759a2a 650 /**
switches 0:0e018d759a2a 651 * Callchain containing all registered callback handlers for data sent
switches 0:0e018d759a2a 652 * events.
switches 0:0e018d759a2a 653 */
switches 0:0e018d759a2a 654 DataSentCallbackChain_t dataSentCallChain;
switches 0:0e018d759a2a 655 /**
switches 0:0e018d759a2a 656 * Callchain containing all registered callback handlers for data written
switches 0:0e018d759a2a 657 * events.
switches 0:0e018d759a2a 658 */
switches 0:0e018d759a2a 659 DataWrittenCallbackChain_t dataWrittenCallChain;
switches 0:0e018d759a2a 660 /**
switches 0:0e018d759a2a 661 * Callchain containing all registered callback handlers for data read
switches 0:0e018d759a2a 662 * events.
switches 0:0e018d759a2a 663 */
switches 0:0e018d759a2a 664 DataReadCallbackChain_t dataReadCallChain;
switches 0:0e018d759a2a 665 /**
switches 0:0e018d759a2a 666 * Callchain containing all registered callback handlers for shutdown
switches 0:0e018d759a2a 667 * events.
switches 0:0e018d759a2a 668 */
switches 0:0e018d759a2a 669 GattServerShutdownCallbackChain_t shutdownCallChain;
switches 0:0e018d759a2a 670 /**
switches 0:0e018d759a2a 671 * The registered callback handler for updates enabled events.
switches 0:0e018d759a2a 672 */
switches 0:0e018d759a2a 673 EventCallback_t updatesEnabledCallback;
switches 0:0e018d759a2a 674 /**
switches 0:0e018d759a2a 675 * The registered callback handler for updates disabled events.
switches 0:0e018d759a2a 676 */
switches 0:0e018d759a2a 677 EventCallback_t updatesDisabledCallback;
switches 0:0e018d759a2a 678 /**
switches 0:0e018d759a2a 679 * The registered callback handler for confirmation received events.
switches 0:0e018d759a2a 680 */
switches 0:0e018d759a2a 681 EventCallback_t confirmationReceivedCallback;
switches 0:0e018d759a2a 682
switches 0:0e018d759a2a 683 private:
switches 0:0e018d759a2a 684 /* Disallow copy and assignment. */
switches 0:0e018d759a2a 685 GattServer(const GattServer &);
switches 0:0e018d759a2a 686 GattServer& operator=(const GattServer &);
switches 0:0e018d759a2a 687 };
switches 0:0e018d759a2a 688
switches 0:0e018d759a2a 689 #endif /* ifndef __GATT_SERVER_H__ */