Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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