test code 123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Committer:
youkee
Date:
Thu Sep 01 05:14:03 2016 +0000
Revision:
0:1ad0e04b1bc5
change internal time from 1s to 200ms

Who changed what in which revision?

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