Bike service

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Mon Jan 11 08:51:49 2016 +0000
Revision:
1089:8e810a8e083e
Parent:
1088:709ebced28ab
Child:
1090:148d8b9b56a5
Synchronized with git rev 65ed5c13
Author: Vincent Coubard
Merge pull request #145 from rgrover/develop

transparenly support existing applications which may have used Gap::ADDR_TYPE_*

Who changed what in which revision?

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