mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
be_bryan 0:b74591d5ab33 1 /* mbed Microcontroller Library
be_bryan 0:b74591d5ab33 2 * Copyright (c) 2006-2013 ARM Limited
be_bryan 0:b74591d5ab33 3 *
be_bryan 0:b74591d5ab33 4 * Licensed under the Apache License, Version 2.0 (the "License");
be_bryan 0:b74591d5ab33 5 * you may not use this file except in compliance with the License.
be_bryan 0:b74591d5ab33 6 * You may obtain a copy of the License at
be_bryan 0:b74591d5ab33 7 *
be_bryan 0:b74591d5ab33 8 * http://www.apache.org/licenses/LICENSE-2.0
be_bryan 0:b74591d5ab33 9 *
be_bryan 0:b74591d5ab33 10 * Unless required by applicable law or agreed to in writing, software
be_bryan 0:b74591d5ab33 11 * distributed under the License is distributed on an "AS IS" BASIS,
be_bryan 0:b74591d5ab33 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
be_bryan 0:b74591d5ab33 13 * See the License for the specific language governing permissions and
be_bryan 0:b74591d5ab33 14 * limitations under the License.
be_bryan 0:b74591d5ab33 15 */
be_bryan 0:b74591d5ab33 16
be_bryan 0:b74591d5ab33 17 #ifndef MBED_GATT_SERVER_H__
be_bryan 0:b74591d5ab33 18 #define MBED_GATT_SERVER_H__
be_bryan 0:b74591d5ab33 19
be_bryan 0:b74591d5ab33 20 #include "Gap.h"
be_bryan 0:b74591d5ab33 21 #include "GattService.h"
be_bryan 0:b74591d5ab33 22 #include "GattAttribute.h"
be_bryan 0:b74591d5ab33 23 #include "GattServerEvents.h"
be_bryan 0:b74591d5ab33 24 #include "GattCallbackParamTypes.h"
be_bryan 0:b74591d5ab33 25 #include "CallChainOfFunctionPointersWithContext.h"
be_bryan 0:b74591d5ab33 26
be_bryan 0:b74591d5ab33 27 /**
be_bryan 0:b74591d5ab33 28 * @addtogroup ble
be_bryan 0:b74591d5ab33 29 * @{
be_bryan 0:b74591d5ab33 30 * @addtogroup gatt
be_bryan 0:b74591d5ab33 31 * @{
be_bryan 0:b74591d5ab33 32 * @addtogroup server
be_bryan 0:b74591d5ab33 33 * @{
be_bryan 0:b74591d5ab33 34 */
be_bryan 0:b74591d5ab33 35
be_bryan 0:b74591d5ab33 36 /**
be_bryan 0:b74591d5ab33 37 * Construct and operates a GATT server.
be_bryan 0:b74591d5ab33 38 *
be_bryan 0:b74591d5ab33 39 * A Gatt server is a collection of GattService; these services contain
be_bryan 0:b74591d5ab33 40 * characteristics that a peer connected to the device may read or write.
be_bryan 0:b74591d5ab33 41 * These characteristics may also emit updates to subscribed clients when their
be_bryan 0:b74591d5ab33 42 * values change.
be_bryan 0:b74591d5ab33 43 *
be_bryan 0:b74591d5ab33 44 * @p Server Layout
be_bryan 0:b74591d5ab33 45 *
be_bryan 0:b74591d5ab33 46 * Application code can add a GattService object to the server with the help of
be_bryan 0:b74591d5ab33 47 * the function addService(). That function registers all the GattCharacteristic
be_bryan 0:b74591d5ab33 48 * enclosed in the service, as well as all the characteristics descriptors (see
be_bryan 0:b74591d5ab33 49 * GattAttribute) these characteristics contain. Service registration assigns
be_bryan 0:b74591d5ab33 50 * a unique handle to the various attributes being part of the service; this
be_bryan 0:b74591d5ab33 51 * handle should be used for subsequent read or write of these components.
be_bryan 0:b74591d5ab33 52 *
be_bryan 0:b74591d5ab33 53 * There are no primitives defined to remove a single service; however, a call to
be_bryan 0:b74591d5ab33 54 * the function reset() removes all services previously registered in the
be_bryan 0:b74591d5ab33 55 * GattServer.
be_bryan 0:b74591d5ab33 56 *
be_bryan 0:b74591d5ab33 57 * @p Characteristic and attributes access
be_bryan 0:b74591d5ab33 58 *
be_bryan 0:b74591d5ab33 59 * Values of the characteristic and the characteristic descriptor present in the
be_bryan 0:b74591d5ab33 60 * GattServer must be accessed through the handle assigned to them when the service
be_bryan 0:b74591d5ab33 61 * has been registered; the GattServer class offers several flavors of read()
be_bryan 0:b74591d5ab33 62 * and write() functions that retrieve or mutate an attribute value.
be_bryan 0:b74591d5ab33 63 *
be_bryan 0:b74591d5ab33 64 * Application code can query if a client has subscribed to a given
be_bryan 0:b74591d5ab33 65 * characteristic's value update by invoking the function areUpdatesEnabled().
be_bryan 0:b74591d5ab33 66 *
be_bryan 0:b74591d5ab33 67 * @p Events
be_bryan 0:b74591d5ab33 68 *
be_bryan 0:b74591d5ab33 69 * The GattServer allows application code to register several event handlers that
be_bryan 0:b74591d5ab33 70 * can be used to monitor client and server activities:
be_bryan 0:b74591d5ab33 71 * - onDataSent(): Register an event handler that is called when a
be_bryan 0:b74591d5ab33 72 * characteristic value update has been sent to a client.
be_bryan 0:b74591d5ab33 73 * - onDataWriten(): Register an event handler that is called when a
be_bryan 0:b74591d5ab33 74 * client has written an attribute of the server.
be_bryan 0:b74591d5ab33 75 * - onDataRead(): Register an event handler that is called when a
be_bryan 0:b74591d5ab33 76 * client has read an attribute of the server.
be_bryan 0:b74591d5ab33 77 * - onUpdatesEnabled: Register an event handler that is called when a
be_bryan 0:b74591d5ab33 78 * client subscribes to updates of a characteristic.
be_bryan 0:b74591d5ab33 79 * - onUpdatesDisabled: Register an event handler that is called when a
be_bryan 0:b74591d5ab33 80 * client unsubscribes from updates of a characteristic.
be_bryan 0:b74591d5ab33 81 * - onConfimationReceived: Register an event handler that is called
be_bryan 0:b74591d5ab33 82 * when a client acknowledges a characteristic value notification.
be_bryan 0:b74591d5ab33 83 *
be_bryan 0:b74591d5ab33 84 * @note The term characteristic value update is used to represent
be_bryan 0:b74591d5ab33 85 * Characteristic Value Notification and Characteristic Value Indication when
be_bryan 0:b74591d5ab33 86 * the nature of the server initiated is not relevant.
be_bryan 0:b74591d5ab33 87 */
be_bryan 0:b74591d5ab33 88 class GattServer {
be_bryan 0:b74591d5ab33 89 public:
be_bryan 0:b74591d5ab33 90 /**
be_bryan 0:b74591d5ab33 91 * Event handler invoked when the server has sent data to a client.
be_bryan 0:b74591d5ab33 92 *
be_bryan 0:b74591d5ab33 93 * @see onDataSent().
be_bryan 0:b74591d5ab33 94 */
be_bryan 0:b74591d5ab33 95 typedef FunctionPointerWithContext<unsigned> DataSentCallback_t;
be_bryan 0:b74591d5ab33 96
be_bryan 0:b74591d5ab33 97 /**
be_bryan 0:b74591d5ab33 98 * Callchain of DataSentCallback_t objects.
be_bryan 0:b74591d5ab33 99 *
be_bryan 0:b74591d5ab33 100 * @see onDataSent().
be_bryan 0:b74591d5ab33 101 */
be_bryan 0:b74591d5ab33 102 typedef CallChainOfFunctionPointersWithContext<unsigned>
be_bryan 0:b74591d5ab33 103 DataSentCallbackChain_t;
be_bryan 0:b74591d5ab33 104
be_bryan 0:b74591d5ab33 105 /**
be_bryan 0:b74591d5ab33 106 * Event handler invoked when the client has written an attribute of the
be_bryan 0:b74591d5ab33 107 * server.
be_bryan 0:b74591d5ab33 108 *
be_bryan 0:b74591d5ab33 109 * @see onDataWritten().
be_bryan 0:b74591d5ab33 110 */
be_bryan 0:b74591d5ab33 111 typedef FunctionPointerWithContext<const GattWriteCallbackParams*>
be_bryan 0:b74591d5ab33 112 DataWrittenCallback_t;
be_bryan 0:b74591d5ab33 113
be_bryan 0:b74591d5ab33 114 /**
be_bryan 0:b74591d5ab33 115 * Callchain of DataWrittenCallback_t objects.
be_bryan 0:b74591d5ab33 116 *
be_bryan 0:b74591d5ab33 117 * @see onDataWritten().
be_bryan 0:b74591d5ab33 118 */
be_bryan 0:b74591d5ab33 119 typedef CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams*>
be_bryan 0:b74591d5ab33 120 DataWrittenCallbackChain_t;
be_bryan 0:b74591d5ab33 121
be_bryan 0:b74591d5ab33 122 /**
be_bryan 0:b74591d5ab33 123 * Event handler invoked when the client has read an attribute of the server.
be_bryan 0:b74591d5ab33 124 *
be_bryan 0:b74591d5ab33 125 * @see onDataRead().
be_bryan 0:b74591d5ab33 126 */
be_bryan 0:b74591d5ab33 127 typedef FunctionPointerWithContext<const GattReadCallbackParams*>
be_bryan 0:b74591d5ab33 128 DataReadCallback_t;
be_bryan 0:b74591d5ab33 129
be_bryan 0:b74591d5ab33 130 /**
be_bryan 0:b74591d5ab33 131 * Callchain of DataReadCallback_t.
be_bryan 0:b74591d5ab33 132 *
be_bryan 0:b74591d5ab33 133 * @see onDataRead().
be_bryan 0:b74591d5ab33 134 */
be_bryan 0:b74591d5ab33 135 typedef CallChainOfFunctionPointersWithContext<const GattReadCallbackParams*>
be_bryan 0:b74591d5ab33 136 DataReadCallbackChain_t;
be_bryan 0:b74591d5ab33 137
be_bryan 0:b74591d5ab33 138 /**
be_bryan 0:b74591d5ab33 139 * Event handler invoked when the GattServer is reset.
be_bryan 0:b74591d5ab33 140 *
be_bryan 0:b74591d5ab33 141 * @see onShutdown() reset()
be_bryan 0:b74591d5ab33 142 */
be_bryan 0:b74591d5ab33 143 typedef FunctionPointerWithContext<const GattServer *>
be_bryan 0:b74591d5ab33 144 GattServerShutdownCallback_t;
be_bryan 0:b74591d5ab33 145
be_bryan 0:b74591d5ab33 146 /**
be_bryan 0:b74591d5ab33 147 * Callchain of GattServerShutdownCallback_t.
be_bryan 0:b74591d5ab33 148 *
be_bryan 0:b74591d5ab33 149 * @see onShutdown() reset()
be_bryan 0:b74591d5ab33 150 */
be_bryan 0:b74591d5ab33 151 typedef CallChainOfFunctionPointersWithContext<const GattServer*>
be_bryan 0:b74591d5ab33 152 GattServerShutdownCallbackChain_t;
be_bryan 0:b74591d5ab33 153
be_bryan 0:b74591d5ab33 154 /**
be_bryan 0:b74591d5ab33 155 * Event handler that handles subscription to characteristic updates,
be_bryan 0:b74591d5ab33 156 * unsubscription from characteristic updates and notification confirmation.
be_bryan 0:b74591d5ab33 157 *
be_bryan 0:b74591d5ab33 158 * @see onUpdatesEnabled() onUpdateDisabled() onConfirmationReceived()
be_bryan 0:b74591d5ab33 159 */
be_bryan 0:b74591d5ab33 160 typedef FunctionPointerWithContext<GattAttribute::Handle_t> EventCallback_t;
be_bryan 0:b74591d5ab33 161
be_bryan 0:b74591d5ab33 162 protected:
be_bryan 0:b74591d5ab33 163 /**
be_bryan 0:b74591d5ab33 164 * Construct a GattServer instance.
be_bryan 0:b74591d5ab33 165 */
be_bryan 0:b74591d5ab33 166 GattServer() :
be_bryan 0:b74591d5ab33 167 serviceCount(0),
be_bryan 0:b74591d5ab33 168 characteristicCount(0),
be_bryan 0:b74591d5ab33 169 dataSentCallChain(),
be_bryan 0:b74591d5ab33 170 dataWrittenCallChain(),
be_bryan 0:b74591d5ab33 171 dataReadCallChain(),
be_bryan 0:b74591d5ab33 172 updatesEnabledCallback(NULL),
be_bryan 0:b74591d5ab33 173 updatesDisabledCallback(NULL),
be_bryan 0:b74591d5ab33 174 confirmationReceivedCallback(NULL) {
be_bryan 0:b74591d5ab33 175 }
be_bryan 0:b74591d5ab33 176
be_bryan 0:b74591d5ab33 177 /*
be_bryan 0:b74591d5ab33 178 * The following functions are meant to be overridden in the platform
be_bryan 0:b74591d5ab33 179 * specific subclass.
be_bryan 0:b74591d5ab33 180 */
be_bryan 0:b74591d5ab33 181 public:
be_bryan 0:b74591d5ab33 182
be_bryan 0:b74591d5ab33 183 /**
be_bryan 0:b74591d5ab33 184 * Add a service declaration to the local attribute server table.
be_bryan 0:b74591d5ab33 185 *
be_bryan 0:b74591d5ab33 186 * This functions inserts a service declaration in the attribute table
be_bryan 0:b74591d5ab33 187 * followed by the characteristic declarations (including characteristic
be_bryan 0:b74591d5ab33 188 * descriptors) present in @p service.
be_bryan 0:b74591d5ab33 189 *
be_bryan 0:b74591d5ab33 190 * The process assigns a unique attribute handle to all the elements added
be_bryan 0:b74591d5ab33 191 * into the attribute table. This handle is an ID that must be used for
be_bryan 0:b74591d5ab33 192 * subsequent interractions with the elements.
be_bryan 0:b74591d5ab33 193 *
be_bryan 0:b74591d5ab33 194 * @note There is no mirror function that removes a single service.
be_bryan 0:b74591d5ab33 195 * Application code can remove all the registered services by calling
be_bryan 0:b74591d5ab33 196 * reset().
be_bryan 0:b74591d5ab33 197 *
be_bryan 0:b74591d5ab33 198 * @important Service, characteristics and descriptors objects registered
be_bryan 0:b74591d5ab33 199 * within the GattServer must remain reachable until reset() is called.
be_bryan 0:b74591d5ab33 200 *
be_bryan 0:b74591d5ab33 201 * @param[in] service The service to be added; attribute handle of services,
be_bryan 0:b74591d5ab33 202 * characteristic and characteristic descriptors are updated by the
be_bryan 0:b74591d5ab33 203 * process.
be_bryan 0:b74591d5ab33 204 *
be_bryan 0:b74591d5ab33 205 * @return BLE_ERROR_NONE if the service was successfully added.
be_bryan 0:b74591d5ab33 206 */
be_bryan 0:b74591d5ab33 207 virtual ble_error_t addService(GattService &service)
be_bryan 0:b74591d5ab33 208 {
be_bryan 0:b74591d5ab33 209 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 210 (void)service;
be_bryan 0:b74591d5ab33 211
be_bryan 0:b74591d5ab33 212 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 213 is supported. */
be_bryan 0:b74591d5ab33 214 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 215 }
be_bryan 0:b74591d5ab33 216
be_bryan 0:b74591d5ab33 217 /**
be_bryan 0:b74591d5ab33 218 * Read the value of an attribute present in the local GATT server.
be_bryan 0:b74591d5ab33 219 *
be_bryan 0:b74591d5ab33 220 * @param[in] attributeHandle Handle of the attribute to read.
be_bryan 0:b74591d5ab33 221 * @param[out] buffer A buffer to hold the value being read.
be_bryan 0:b74591d5ab33 222 * @param[in,out] lengthP Length of the buffer being supplied. If the
be_bryan 0:b74591d5ab33 223 * attribute value is longer than the size of the supplied buffer, this
be_bryan 0:b74591d5ab33 224 * variable holds upon return the total attribute value length (excluding
be_bryan 0:b74591d5ab33 225 * offset). The application may use this information to allocate a suitable
be_bryan 0:b74591d5ab33 226 * buffer size.
be_bryan 0:b74591d5ab33 227 *
be_bryan 0:b74591d5ab33 228 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
be_bryan 0:b74591d5ab33 229 *
be_bryan 0:b74591d5ab33 230 * @important read(Gap::Handle_t, GattAttribute::Handle_t, uint8_t *, uint16_t *)
be_bryan 0:b74591d5ab33 231 * must be used to read Client Characteristic Configuration Descriptor (CCCD)
be_bryan 0:b74591d5ab33 232 * because the value of this type of attribute depends on the connection.
be_bryan 0:b74591d5ab33 233 */
be_bryan 0:b74591d5ab33 234 virtual ble_error_t read(
be_bryan 0:b74591d5ab33 235 GattAttribute::Handle_t attributeHandle,
be_bryan 0:b74591d5ab33 236 uint8_t buffer[],
be_bryan 0:b74591d5ab33 237 uint16_t *lengthP
be_bryan 0:b74591d5ab33 238 ) {
be_bryan 0:b74591d5ab33 239 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 240 (void)attributeHandle;
be_bryan 0:b74591d5ab33 241 (void)buffer;
be_bryan 0:b74591d5ab33 242 (void)lengthP;
be_bryan 0:b74591d5ab33 243
be_bryan 0:b74591d5ab33 244 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 245 is supported. */
be_bryan 0:b74591d5ab33 246 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 247 }
be_bryan 0:b74591d5ab33 248
be_bryan 0:b74591d5ab33 249 /**
be_bryan 0:b74591d5ab33 250 * Read the value of an attribute present in the local GATT server.
be_bryan 0:b74591d5ab33 251 *
be_bryan 0:b74591d5ab33 252 * The connection handle allows application code to read the value of a
be_bryan 0:b74591d5ab33 253 * Client Characteristic Configuration Descriptor for a given connection.
be_bryan 0:b74591d5ab33 254 *
be_bryan 0:b74591d5ab33 255 * @param[in] connectionHandle Connection handle.
be_bryan 0:b74591d5ab33 256 * @param[in] attributeHandle Attribute handle for the value attribute of
be_bryan 0:b74591d5ab33 257 * the characteristic.
be_bryan 0:b74591d5ab33 258 * @param[out] buffer A buffer to hold the value being read.
be_bryan 0:b74591d5ab33 259 * @param[in,out] lengthP Length of the buffer being supplied. If the
be_bryan 0:b74591d5ab33 260 * attribute value is longer than the size of the supplied buffer, this
be_bryan 0:b74591d5ab33 261 * variable holds upon return the total attribute value length (excluding
be_bryan 0:b74591d5ab33 262 * offset). The application may use this information to allocate a suitable
be_bryan 0:b74591d5ab33 263 * buffer size.
be_bryan 0:b74591d5ab33 264 *
be_bryan 0:b74591d5ab33 265 * @return BLE_ERROR_NONE if a value was read successfully into the buffer.
be_bryan 0:b74591d5ab33 266 */
be_bryan 0:b74591d5ab33 267 virtual ble_error_t read(
be_bryan 0:b74591d5ab33 268 Gap::Handle_t connectionHandle,
be_bryan 0:b74591d5ab33 269 GattAttribute::Handle_t attributeHandle,
be_bryan 0:b74591d5ab33 270 uint8_t *buffer,
be_bryan 0:b74591d5ab33 271 uint16_t *lengthP
be_bryan 0:b74591d5ab33 272 ) {
be_bryan 0:b74591d5ab33 273 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 274 (void)connectionHandle;
be_bryan 0:b74591d5ab33 275 (void)attributeHandle;
be_bryan 0:b74591d5ab33 276 (void)buffer;
be_bryan 0:b74591d5ab33 277 (void)lengthP;
be_bryan 0:b74591d5ab33 278
be_bryan 0:b74591d5ab33 279 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 280 is supported. */
be_bryan 0:b74591d5ab33 281 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 282 }
be_bryan 0:b74591d5ab33 283
be_bryan 0:b74591d5ab33 284 /**
be_bryan 0:b74591d5ab33 285 * Update the value of an attribute present in the local GATT server.
be_bryan 0:b74591d5ab33 286 *
be_bryan 0:b74591d5ab33 287 * @param[in] attributeHandle Handle of the attribute to write.
be_bryan 0:b74591d5ab33 288 * @param[in] value A pointer to a buffer holding the new value.
be_bryan 0:b74591d5ab33 289 * @param[in] size Size in bytes of the new value (in bytes).
be_bryan 0:b74591d5ab33 290 * @param[in] localOnly If this flag is false and the attribute handle
be_bryan 0:b74591d5ab33 291 * written is a characteristic value, then the server sends an update
be_bryan 0:b74591d5ab33 292 * containing the new value to all clients that have subscribed to the
be_bryan 0:b74591d5ab33 293 * characteristic's notifications or indications. Otherwise, the update does
be_bryan 0:b74591d5ab33 294 * not generate a single server initiated event.
be_bryan 0:b74591d5ab33 295 *
be_bryan 0:b74591d5ab33 296 * @return BLE_ERROR_NONE if the attribute value has been successfully
be_bryan 0:b74591d5ab33 297 * updated.
be_bryan 0:b74591d5ab33 298 */
be_bryan 0:b74591d5ab33 299 virtual ble_error_t write(
be_bryan 0:b74591d5ab33 300 GattAttribute::Handle_t attributeHandle,
be_bryan 0:b74591d5ab33 301 const uint8_t *value,
be_bryan 0:b74591d5ab33 302 uint16_t size,
be_bryan 0:b74591d5ab33 303 bool localOnly = false
be_bryan 0:b74591d5ab33 304 ) {
be_bryan 0:b74591d5ab33 305 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 306 (void)attributeHandle;
be_bryan 0:b74591d5ab33 307 (void)value;
be_bryan 0:b74591d5ab33 308 (void)size;
be_bryan 0:b74591d5ab33 309 (void)localOnly;
be_bryan 0:b74591d5ab33 310
be_bryan 0:b74591d5ab33 311 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 312 is supported. */
be_bryan 0:b74591d5ab33 313 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 314 }
be_bryan 0:b74591d5ab33 315
be_bryan 0:b74591d5ab33 316 /**
be_bryan 0:b74591d5ab33 317 * Update the value of an attribute present in the local GATT server.
be_bryan 0:b74591d5ab33 318 *
be_bryan 0:b74591d5ab33 319 * The connection handle parameter allows application code to direct
be_bryan 0:b74591d5ab33 320 * notification or indication resulting from the update to a specific client.
be_bryan 0:b74591d5ab33 321 *
be_bryan 0:b74591d5ab33 322 * @param[in] connectionHandle Connection handle.
be_bryan 0:b74591d5ab33 323 * @param[in] attributeHandle Handle for the value attribute of the
be_bryan 0:b74591d5ab33 324 * characteristic.
be_bryan 0:b74591d5ab33 325 * @param[in] value A pointer to a buffer holding the new value.
be_bryan 0:b74591d5ab33 326 * @param[in] size Size of the new value (in bytes).
be_bryan 0:b74591d5ab33 327 * @param[in] localOnly If this flag is false and the attribute handle
be_bryan 0:b74591d5ab33 328 * written is a characteristic value, then the server sends an update
be_bryan 0:b74591d5ab33 329 * containing the new value to the client identified by the parameter
be_bryan 0:b74591d5ab33 330 * @p connectionHandle if it is subscribed to the characteristic's
be_bryan 0:b74591d5ab33 331 * notifications or indications. Otherwise, the update does not generate a
be_bryan 0:b74591d5ab33 332 * single server initiated event.
be_bryan 0:b74591d5ab33 333 *
be_bryan 0:b74591d5ab33 334 * @return BLE_ERROR_NONE if the attribute value has been successfully
be_bryan 0:b74591d5ab33 335 * updated.
be_bryan 0:b74591d5ab33 336 */
be_bryan 0:b74591d5ab33 337 virtual ble_error_t write(
be_bryan 0:b74591d5ab33 338 Gap::Handle_t connectionHandle,
be_bryan 0:b74591d5ab33 339 GattAttribute::Handle_t attributeHandle,
be_bryan 0:b74591d5ab33 340 const uint8_t *value,
be_bryan 0:b74591d5ab33 341 uint16_t size,
be_bryan 0:b74591d5ab33 342 bool localOnly = false
be_bryan 0:b74591d5ab33 343 ) {
be_bryan 0:b74591d5ab33 344 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 345 (void)connectionHandle;
be_bryan 0:b74591d5ab33 346 (void)attributeHandle;
be_bryan 0:b74591d5ab33 347 (void)value;
be_bryan 0:b74591d5ab33 348 (void)size;
be_bryan 0:b74591d5ab33 349 (void)localOnly;
be_bryan 0:b74591d5ab33 350
be_bryan 0:b74591d5ab33 351 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 352 is supported. */
be_bryan 0:b74591d5ab33 353 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 354 }
be_bryan 0:b74591d5ab33 355
be_bryan 0:b74591d5ab33 356 /**
be_bryan 0:b74591d5ab33 357 * Determine if one of the connected clients has subscribed to notifications
be_bryan 0:b74591d5ab33 358 * or indications of the characteristic in input.
be_bryan 0:b74591d5ab33 359 *
be_bryan 0:b74591d5ab33 360 * @param[in] characteristic The characteristic.
be_bryan 0:b74591d5ab33 361 * @param[out] enabledP Upon return, *enabledP is true if updates are
be_bryan 0:b74591d5ab33 362 * enabled for a connected client; otherwise, *enabledP is false.
be_bryan 0:b74591d5ab33 363 *
be_bryan 0:b74591d5ab33 364 * @return BLE_ERROR_NONE if the connection and handle are found. False
be_bryan 0:b74591d5ab33 365 * otherwise.
be_bryan 0:b74591d5ab33 366 */
be_bryan 0:b74591d5ab33 367 virtual ble_error_t areUpdatesEnabled(
be_bryan 0:b74591d5ab33 368 const GattCharacteristic &characteristic,
be_bryan 0:b74591d5ab33 369 bool *enabledP
be_bryan 0:b74591d5ab33 370 ) {
be_bryan 0:b74591d5ab33 371 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 372 (void)characteristic;
be_bryan 0:b74591d5ab33 373 (void)enabledP;
be_bryan 0:b74591d5ab33 374
be_bryan 0:b74591d5ab33 375 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 376 is supported. */
be_bryan 0:b74591d5ab33 377 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 378 }
be_bryan 0:b74591d5ab33 379
be_bryan 0:b74591d5ab33 380 /**
be_bryan 0:b74591d5ab33 381 * Determine if an identified client has subscribed to notifications or
be_bryan 0:b74591d5ab33 382 * indications of a given characteristic.
be_bryan 0:b74591d5ab33 383 *
be_bryan 0:b74591d5ab33 384 * @param[in] connectionHandle The connection handle.
be_bryan 0:b74591d5ab33 385 * @param[in] characteristic The characteristic.
be_bryan 0:b74591d5ab33 386 * @param[out] enabledP Upon return, *enabledP is true if the client
be_bryan 0:b74591d5ab33 387 * identified by @p connectionHandle has subscribed to notifications or
be_bryan 0:b74591d5ab33 388 * indications of @p characteristic; otherwise, *enabledP is false.
be_bryan 0:b74591d5ab33 389 *
be_bryan 0:b74591d5ab33 390 * @return BLE_ERROR_NONE if the connection and handle are found. False
be_bryan 0:b74591d5ab33 391 * otherwise.
be_bryan 0:b74591d5ab33 392 */
be_bryan 0:b74591d5ab33 393 virtual ble_error_t areUpdatesEnabled(
be_bryan 0:b74591d5ab33 394 Gap::Handle_t connectionHandle,
be_bryan 0:b74591d5ab33 395 const GattCharacteristic &characteristic,
be_bryan 0:b74591d5ab33 396 bool *enabledP
be_bryan 0:b74591d5ab33 397 ) {
be_bryan 0:b74591d5ab33 398 /* Avoid compiler warnings about unused variables. */
be_bryan 0:b74591d5ab33 399 (void)connectionHandle;
be_bryan 0:b74591d5ab33 400 (void)characteristic;
be_bryan 0:b74591d5ab33 401 (void)enabledP;
be_bryan 0:b74591d5ab33 402
be_bryan 0:b74591d5ab33 403 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 404 is supported. */
be_bryan 0:b74591d5ab33 405 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 406 }
be_bryan 0:b74591d5ab33 407
be_bryan 0:b74591d5ab33 408 /**
be_bryan 0:b74591d5ab33 409 * Indicate if the underlying stack emit events when an attribute is read by
be_bryan 0:b74591d5ab33 410 * a client.
be_bryan 0:b74591d5ab33 411 *
be_bryan 0:b74591d5ab33 412 * @important This function should be overridden to return true if
be_bryan 0:b74591d5ab33 413 * applicable.
be_bryan 0:b74591d5ab33 414 *
be_bryan 0:b74591d5ab33 415 * @return true if onDataRead is supported; false otherwise.
be_bryan 0:b74591d5ab33 416 */
be_bryan 0:b74591d5ab33 417 virtual bool isOnDataReadAvailable() const
be_bryan 0:b74591d5ab33 418 {
be_bryan 0:b74591d5ab33 419 /* Requesting action from porters: override this API if this capability
be_bryan 0:b74591d5ab33 420 is supported. */
be_bryan 0:b74591d5ab33 421 return false;
be_bryan 0:b74591d5ab33 422 }
be_bryan 0:b74591d5ab33 423
be_bryan 0:b74591d5ab33 424 /*
be_bryan 0:b74591d5ab33 425 * APIs with nonvirtual implementations.
be_bryan 0:b74591d5ab33 426 */
be_bryan 0:b74591d5ab33 427 public:
be_bryan 0:b74591d5ab33 428 /**
be_bryan 0:b74591d5ab33 429 * Add an event handler that monitors emission of characteristic value
be_bryan 0:b74591d5ab33 430 * updates.
be_bryan 0:b74591d5ab33 431 *
be_bryan 0:b74591d5ab33 432 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 433 *
be_bryan 0:b74591d5ab33 434 * @note It is possible to chain together multiple onDataSent callbacks
be_bryan 0:b74591d5ab33 435 * (potentially from different modules of an application).
be_bryan 0:b74591d5ab33 436 */
be_bryan 0:b74591d5ab33 437 void onDataSent(const DataSentCallback_t &callback)
be_bryan 0:b74591d5ab33 438 {
be_bryan 0:b74591d5ab33 439 dataSentCallChain.add(callback);
be_bryan 0:b74591d5ab33 440 }
be_bryan 0:b74591d5ab33 441
be_bryan 0:b74591d5ab33 442 /**
be_bryan 0:b74591d5ab33 443 * Add an event handler that monitors emission of characteristic value
be_bryan 0:b74591d5ab33 444 * updates.
be_bryan 0:b74591d5ab33 445 *
be_bryan 0:b74591d5ab33 446 * @param[in] objPtr Pointer to the instance that is used to invoke the
be_bryan 0:b74591d5ab33 447 * event handler.
be_bryan 0:b74591d5ab33 448 * @param[in] memberPtr Event handler being registered. It is a member
be_bryan 0:b74591d5ab33 449 * function.
be_bryan 0:b74591d5ab33 450 */
be_bryan 0:b74591d5ab33 451 template <typename T>
be_bryan 0:b74591d5ab33 452 void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count))
be_bryan 0:b74591d5ab33 453 {
be_bryan 0:b74591d5ab33 454 dataSentCallChain.add(objPtr, memberPtr);
be_bryan 0:b74591d5ab33 455 }
be_bryan 0:b74591d5ab33 456
be_bryan 0:b74591d5ab33 457 /**
be_bryan 0:b74591d5ab33 458 * Access the callchain of data sent event handlers.
be_bryan 0:b74591d5ab33 459 *
be_bryan 0:b74591d5ab33 460 * @return A reference to the DATA_SENT event callback chain.
be_bryan 0:b74591d5ab33 461 */
be_bryan 0:b74591d5ab33 462 DataSentCallbackChain_t &onDataSent()
be_bryan 0:b74591d5ab33 463 {
be_bryan 0:b74591d5ab33 464 return dataSentCallChain;
be_bryan 0:b74591d5ab33 465 }
be_bryan 0:b74591d5ab33 466
be_bryan 0:b74591d5ab33 467 /**
be_bryan 0:b74591d5ab33 468 * Set an event handler that is called after
be_bryan 0:b74591d5ab33 469 * a connected peer has written an attribute.
be_bryan 0:b74591d5ab33 470 *
be_bryan 0:b74591d5ab33 471 * @param[in] callback The event handler being registered.
be_bryan 0:b74591d5ab33 472 *
be_bryan 0:b74591d5ab33 473 * @important It is possible to set multiple event handlers. Registered
be_bryan 0:b74591d5ab33 474 * handlers may be removed with onDataWritten().detach(callback).
be_bryan 0:b74591d5ab33 475 */
be_bryan 0:b74591d5ab33 476 void onDataWritten(const DataWrittenCallback_t &callback)
be_bryan 0:b74591d5ab33 477 {
be_bryan 0:b74591d5ab33 478 dataWrittenCallChain.add(callback);
be_bryan 0:b74591d5ab33 479 }
be_bryan 0:b74591d5ab33 480
be_bryan 0:b74591d5ab33 481 /**
be_bryan 0:b74591d5ab33 482 * Set an event handler that is called after
be_bryan 0:b74591d5ab33 483 * a connected peer has written an attribute.
be_bryan 0:b74591d5ab33 484 *
be_bryan 0:b74591d5ab33 485 * @param[in] objPtr Pointer to the instance that is used to invoke the
be_bryan 0:b74591d5ab33 486 * event handler (@p memberPtr).
be_bryan 0:b74591d5ab33 487 * @param[in] memberPtr Event handler being registered. It is a member
be_bryan 0:b74591d5ab33 488 * function.
be_bryan 0:b74591d5ab33 489 */
be_bryan 0:b74591d5ab33 490 template <typename T>
be_bryan 0:b74591d5ab33 491 void onDataWritten(
be_bryan 0:b74591d5ab33 492 T *objPtr,
be_bryan 0:b74591d5ab33 493 void (T::*memberPtr)(const GattWriteCallbackParams *context)
be_bryan 0:b74591d5ab33 494 ) {
be_bryan 0:b74591d5ab33 495 dataWrittenCallChain.add(objPtr, memberPtr);
be_bryan 0:b74591d5ab33 496 }
be_bryan 0:b74591d5ab33 497
be_bryan 0:b74591d5ab33 498 /**
be_bryan 0:b74591d5ab33 499 * Access the callchain of data written event handlers.
be_bryan 0:b74591d5ab33 500 *
be_bryan 0:b74591d5ab33 501 * @return A reference to the data written event callbacks chain.
be_bryan 0:b74591d5ab33 502 *
be_bryan 0:b74591d5ab33 503 * @note It is possible to register callbacks using
be_bryan 0:b74591d5ab33 504 * onDataWritten().add(callback).
be_bryan 0:b74591d5ab33 505 *
be_bryan 0:b74591d5ab33 506 * @note It is possible to unregister callbacks using
be_bryan 0:b74591d5ab33 507 * onDataWritten().detach(callback).
be_bryan 0:b74591d5ab33 508 */
be_bryan 0:b74591d5ab33 509 DataWrittenCallbackChain_t &onDataWritten()
be_bryan 0:b74591d5ab33 510 {
be_bryan 0:b74591d5ab33 511 return dataWrittenCallChain;
be_bryan 0:b74591d5ab33 512 }
be_bryan 0:b74591d5ab33 513
be_bryan 0:b74591d5ab33 514 /**
be_bryan 0:b74591d5ab33 515 * Set an event handler that monitors attribute reads from connected clients.
be_bryan 0:b74591d5ab33 516 *
be_bryan 0:b74591d5ab33 517 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 518 *
be_bryan 0:b74591d5ab33 519 * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available;
be_bryan 0:b74591d5ab33 520 * else BLE_ERROR_NONE.
be_bryan 0:b74591d5ab33 521 *
be_bryan 0:b74591d5ab33 522 * @note This functionality may not be available on all underlying stacks.
be_bryan 0:b74591d5ab33 523 * Application code may work around that limitation by monitoring read
be_bryan 0:b74591d5ab33 524 * requests instead of read events.
be_bryan 0:b74591d5ab33 525 *
be_bryan 0:b74591d5ab33 526 * @see GattCharacteristic::setReadAuthorizationCallback()
be_bryan 0:b74591d5ab33 527 * @see isOnDataReadAvailable().
be_bryan 0:b74591d5ab33 528 *
be_bryan 0:b74591d5ab33 529 * @important It is possible to set multiple event handlers. Registered
be_bryan 0:b74591d5ab33 530 * handlers may be removed with onDataRead().detach(callback).
be_bryan 0:b74591d5ab33 531 */
be_bryan 0:b74591d5ab33 532 ble_error_t onDataRead(const DataReadCallback_t &callback)
be_bryan 0:b74591d5ab33 533 {
be_bryan 0:b74591d5ab33 534 if (!isOnDataReadAvailable()) {
be_bryan 0:b74591d5ab33 535 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 536 }
be_bryan 0:b74591d5ab33 537
be_bryan 0:b74591d5ab33 538 dataReadCallChain.add(callback);
be_bryan 0:b74591d5ab33 539 return BLE_ERROR_NONE;
be_bryan 0:b74591d5ab33 540 }
be_bryan 0:b74591d5ab33 541
be_bryan 0:b74591d5ab33 542 /**
be_bryan 0:b74591d5ab33 543 * Set an event handler that monitors attribute reads from connected clients.
be_bryan 0:b74591d5ab33 544 *
be_bryan 0:b74591d5ab33 545 * @param[in] objPtr Pointer to the instance that is used to invoke the
be_bryan 0:b74591d5ab33 546 * event handler (@p memberPtr).
be_bryan 0:b74591d5ab33 547 * @param[in] memberPtr Event handler being registered. It is a member
be_bryan 0:b74591d5ab33 548 * function.
be_bryan 0:b74591d5ab33 549 */
be_bryan 0:b74591d5ab33 550 template <typename T>
be_bryan 0:b74591d5ab33 551 ble_error_t onDataRead(
be_bryan 0:b74591d5ab33 552 T *objPtr,
be_bryan 0:b74591d5ab33 553 void (T::*memberPtr)(const GattReadCallbackParams *context)
be_bryan 0:b74591d5ab33 554 ) {
be_bryan 0:b74591d5ab33 555 if (!isOnDataReadAvailable()) {
be_bryan 0:b74591d5ab33 556 return BLE_ERROR_NOT_IMPLEMENTED;
be_bryan 0:b74591d5ab33 557 }
be_bryan 0:b74591d5ab33 558
be_bryan 0:b74591d5ab33 559 dataReadCallChain.add(objPtr, memberPtr);
be_bryan 0:b74591d5ab33 560 return BLE_ERROR_NONE;
be_bryan 0:b74591d5ab33 561 }
be_bryan 0:b74591d5ab33 562
be_bryan 0:b74591d5ab33 563 /**
be_bryan 0:b74591d5ab33 564 * Access the callchain of data read event handlers.
be_bryan 0:b74591d5ab33 565 *
be_bryan 0:b74591d5ab33 566 * @return A reference to the data read event callbacks chain.
be_bryan 0:b74591d5ab33 567 *
be_bryan 0:b74591d5ab33 568 * @note It is possible to register callbacks using
be_bryan 0:b74591d5ab33 569 * onDataRead().add(callback).
be_bryan 0:b74591d5ab33 570 *
be_bryan 0:b74591d5ab33 571 * @note It is possible to unregister callbacks using
be_bryan 0:b74591d5ab33 572 * onDataRead().detach(callback).
be_bryan 0:b74591d5ab33 573 */
be_bryan 0:b74591d5ab33 574 DataReadCallbackChain_t &onDataRead()
be_bryan 0:b74591d5ab33 575 {
be_bryan 0:b74591d5ab33 576 return dataReadCallChain;
be_bryan 0:b74591d5ab33 577 }
be_bryan 0:b74591d5ab33 578
be_bryan 0:b74591d5ab33 579 /**
be_bryan 0:b74591d5ab33 580 * Set an event handler that monitors shutdown or reset of the GattServer.
be_bryan 0:b74591d5ab33 581 *
be_bryan 0:b74591d5ab33 582 * The event handler is invoked when the GattServer instance is about
be_bryan 0:b74591d5ab33 583 * to be shut down. This can result in a call to reset() or BLE::reset().
be_bryan 0:b74591d5ab33 584 *
be_bryan 0:b74591d5ab33 585 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 586 *
be_bryan 0:b74591d5ab33 587 * @note It is possible to set up multiple shutdown event handlers.
be_bryan 0:b74591d5ab33 588 *
be_bryan 0:b74591d5ab33 589 * @note It is possible to unregister a callback using
be_bryan 0:b74591d5ab33 590 * onShutdown().detach(callback)
be_bryan 0:b74591d5ab33 591 */
be_bryan 0:b74591d5ab33 592 void onShutdown(const GattServerShutdownCallback_t &callback)
be_bryan 0:b74591d5ab33 593 {
be_bryan 0:b74591d5ab33 594 shutdownCallChain.add(callback);
be_bryan 0:b74591d5ab33 595 }
be_bryan 0:b74591d5ab33 596
be_bryan 0:b74591d5ab33 597 /**
be_bryan 0:b74591d5ab33 598 * Set an event handler that monitors shutdown or reset of the GattServer.
be_bryan 0:b74591d5ab33 599 *
be_bryan 0:b74591d5ab33 600 * The event handler is invoked when the GattServer instance is about
be_bryan 0:b74591d5ab33 601 * to be shut down. This can result of a call to reset() or BLE::reset().
be_bryan 0:b74591d5ab33 602 *
be_bryan 0:b74591d5ab33 603 * @param[in] objPtr Pointer to the instance that is used to invoke the
be_bryan 0:b74591d5ab33 604 * event handler (@p memberPtr).
be_bryan 0:b74591d5ab33 605 * @param[in] memberPtr Event handler being registered. It is a member
be_bryan 0:b74591d5ab33 606 * function.
be_bryan 0:b74591d5ab33 607 */
be_bryan 0:b74591d5ab33 608 template <typename T>
be_bryan 0:b74591d5ab33 609 void onShutdown(T *objPtr, void (T::*memberPtr)(const GattServer *))
be_bryan 0:b74591d5ab33 610 {
be_bryan 0:b74591d5ab33 611 shutdownCallChain.add(objPtr, memberPtr);
be_bryan 0:b74591d5ab33 612 }
be_bryan 0:b74591d5ab33 613
be_bryan 0:b74591d5ab33 614 /**
be_bryan 0:b74591d5ab33 615 * Access the callchain of shutdown event handlers.
be_bryan 0:b74591d5ab33 616 *
be_bryan 0:b74591d5ab33 617 * @return A reference to the shutdown event callbacks chain.
be_bryan 0:b74591d5ab33 618 *
be_bryan 0:b74591d5ab33 619 * @note It is possible to register callbacks using
be_bryan 0:b74591d5ab33 620 * onShutdown().add(callback).
be_bryan 0:b74591d5ab33 621 *
be_bryan 0:b74591d5ab33 622 * @note It is possible to unregister callbacks using
be_bryan 0:b74591d5ab33 623 * onShutdown().detach(callback).
be_bryan 0:b74591d5ab33 624 */
be_bryan 0:b74591d5ab33 625 GattServerShutdownCallbackChain_t& onShutdown()
be_bryan 0:b74591d5ab33 626 {
be_bryan 0:b74591d5ab33 627 return shutdownCallChain;
be_bryan 0:b74591d5ab33 628 }
be_bryan 0:b74591d5ab33 629
be_bryan 0:b74591d5ab33 630 /**
be_bryan 0:b74591d5ab33 631 * Set up an event handler that monitors subscription to characteristic
be_bryan 0:b74591d5ab33 632 * updates.
be_bryan 0:b74591d5ab33 633 *
be_bryan 0:b74591d5ab33 634 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 635 */
be_bryan 0:b74591d5ab33 636 void onUpdatesEnabled(EventCallback_t callback)
be_bryan 0:b74591d5ab33 637 {
be_bryan 0:b74591d5ab33 638 updatesEnabledCallback = callback;
be_bryan 0:b74591d5ab33 639 }
be_bryan 0:b74591d5ab33 640
be_bryan 0:b74591d5ab33 641 /**
be_bryan 0:b74591d5ab33 642 * Set up an event handler that monitors unsubscription from characteristic
be_bryan 0:b74591d5ab33 643 * updates.
be_bryan 0:b74591d5ab33 644 *
be_bryan 0:b74591d5ab33 645 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 646 */
be_bryan 0:b74591d5ab33 647 void onUpdatesDisabled(EventCallback_t callback)
be_bryan 0:b74591d5ab33 648 {
be_bryan 0:b74591d5ab33 649 updatesDisabledCallback = callback;
be_bryan 0:b74591d5ab33 650 }
be_bryan 0:b74591d5ab33 651
be_bryan 0:b74591d5ab33 652 /**
be_bryan 0:b74591d5ab33 653 * Set up an event handler that monitors notification acknowledgment.
be_bryan 0:b74591d5ab33 654 *
be_bryan 0:b74591d5ab33 655 * The event handler is called when a client sends a confirmation that it has
be_bryan 0:b74591d5ab33 656 * correctly received a notification from the server.
be_bryan 0:b74591d5ab33 657 *
be_bryan 0:b74591d5ab33 658 * @param[in] callback Event handler being registered.
be_bryan 0:b74591d5ab33 659 */
be_bryan 0:b74591d5ab33 660 void onConfirmationReceived(EventCallback_t callback)
be_bryan 0:b74591d5ab33 661 {
be_bryan 0:b74591d5ab33 662 confirmationReceivedCallback = callback;
be_bryan 0:b74591d5ab33 663 }
be_bryan 0:b74591d5ab33 664
be_bryan 0:b74591d5ab33 665 /* Entry points for the underlying stack to report events back to the user. */
be_bryan 0:b74591d5ab33 666 protected:
be_bryan 0:b74591d5ab33 667 /**
be_bryan 0:b74591d5ab33 668 * Helper function that notifies all registered handlers of an occurrence
be_bryan 0:b74591d5ab33 669 * of a data written event.
be_bryan 0:b74591d5ab33 670 *
be_bryan 0:b74591d5ab33 671 * @important Vendor implementation must invoke this function after one of
be_bryan 0:b74591d5ab33 672 * the GattServer attributes has been written.
be_bryan 0:b74591d5ab33 673 *
be_bryan 0:b74591d5ab33 674 * @param[in] params The data written parameters passed to the registered
be_bryan 0:b74591d5ab33 675 * handlers.
be_bryan 0:b74591d5ab33 676 */
be_bryan 0:b74591d5ab33 677 void handleDataWrittenEvent(const GattWriteCallbackParams *params)
be_bryan 0:b74591d5ab33 678 {
be_bryan 0:b74591d5ab33 679 dataWrittenCallChain.call(params);
be_bryan 0:b74591d5ab33 680 }
be_bryan 0:b74591d5ab33 681
be_bryan 0:b74591d5ab33 682 /**
be_bryan 0:b74591d5ab33 683 * Helper function that notifies all registered handlers of an occurrence
be_bryan 0:b74591d5ab33 684 * of a data read event.
be_bryan 0:b74591d5ab33 685 *
be_bryan 0:b74591d5ab33 686 * @important Vendor implementation must invoke this function after one of
be_bryan 0:b74591d5ab33 687 * the GattServer attributes has been read.
be_bryan 0:b74591d5ab33 688 *
be_bryan 0:b74591d5ab33 689 * @param[in] params The data read parameters passed to the registered
be_bryan 0:b74591d5ab33 690 * handlers.
be_bryan 0:b74591d5ab33 691 */
be_bryan 0:b74591d5ab33 692 void handleDataReadEvent(const GattReadCallbackParams *params)
be_bryan 0:b74591d5ab33 693 {
be_bryan 0:b74591d5ab33 694 dataReadCallChain.call(params);
be_bryan 0:b74591d5ab33 695 }
be_bryan 0:b74591d5ab33 696
be_bryan 0:b74591d5ab33 697 /**
be_bryan 0:b74591d5ab33 698 * Helper function that notifies the registered handler of an occurrence
be_bryan 0:b74591d5ab33 699 * of updates enabled, updates disabled or confirmation received events.
be_bryan 0:b74591d5ab33 700 *
be_bryan 0:b74591d5ab33 701 * @important Vendor implementation must invoke this function when a client
be_bryan 0:b74591d5ab33 702 * subscribes to characteristic updates, unsubscribes from characteristic
be_bryan 0:b74591d5ab33 703 * updates or a notification confirmation has been received.
be_bryan 0:b74591d5ab33 704 *
be_bryan 0:b74591d5ab33 705 * @param[in] type The type of event that occurred.
be_bryan 0:b74591d5ab33 706 * @param[in] attributeHandle The handle of the attribute concerned by the
be_bryan 0:b74591d5ab33 707 * event.
be_bryan 0:b74591d5ab33 708 */
be_bryan 0:b74591d5ab33 709 void handleEvent(
be_bryan 0:b74591d5ab33 710 GattServerEvents::gattEvent_e type,
be_bryan 0:b74591d5ab33 711 GattAttribute::Handle_t attributeHandle
be_bryan 0:b74591d5ab33 712 ) {
be_bryan 0:b74591d5ab33 713 switch (type) {
be_bryan 0:b74591d5ab33 714 case GattServerEvents::GATT_EVENT_UPDATES_ENABLED:
be_bryan 0:b74591d5ab33 715 if (updatesEnabledCallback) {
be_bryan 0:b74591d5ab33 716 updatesEnabledCallback(attributeHandle);
be_bryan 0:b74591d5ab33 717 }
be_bryan 0:b74591d5ab33 718 break;
be_bryan 0:b74591d5ab33 719 case GattServerEvents::GATT_EVENT_UPDATES_DISABLED:
be_bryan 0:b74591d5ab33 720 if (updatesDisabledCallback) {
be_bryan 0:b74591d5ab33 721 updatesDisabledCallback(attributeHandle);
be_bryan 0:b74591d5ab33 722 }
be_bryan 0:b74591d5ab33 723 break;
be_bryan 0:b74591d5ab33 724 case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED:
be_bryan 0:b74591d5ab33 725 if (confirmationReceivedCallback) {
be_bryan 0:b74591d5ab33 726 confirmationReceivedCallback(attributeHandle);
be_bryan 0:b74591d5ab33 727 }
be_bryan 0:b74591d5ab33 728 break;
be_bryan 0:b74591d5ab33 729 default:
be_bryan 0:b74591d5ab33 730 break;
be_bryan 0:b74591d5ab33 731 }
be_bryan 0:b74591d5ab33 732 }
be_bryan 0:b74591d5ab33 733
be_bryan 0:b74591d5ab33 734 /**
be_bryan 0:b74591d5ab33 735 * Helper function that notifies all registered handlers of an occurrence
be_bryan 0:b74591d5ab33 736 * of a data sent event.
be_bryan 0:b74591d5ab33 737 *
be_bryan 0:b74591d5ab33 738 * @important Vendor implementation must invoke this function after the
be_bryan 0:b74591d5ab33 739 * emission of a notification or an indication.
be_bryan 0:b74591d5ab33 740 *
be_bryan 0:b74591d5ab33 741 * @param[in] count Number of packets sent.
be_bryan 0:b74591d5ab33 742 */
be_bryan 0:b74591d5ab33 743 void handleDataSentEvent(unsigned count)
be_bryan 0:b74591d5ab33 744 {
be_bryan 0:b74591d5ab33 745 dataSentCallChain.call(count);
be_bryan 0:b74591d5ab33 746 }
be_bryan 0:b74591d5ab33 747
be_bryan 0:b74591d5ab33 748 public:
be_bryan 0:b74591d5ab33 749 /**
be_bryan 0:b74591d5ab33 750 * Shut down the GattServer instance.
be_bryan 0:b74591d5ab33 751 *
be_bryan 0:b74591d5ab33 752 * This function notifies all event handlers listening for shutdown events
be_bryan 0:b74591d5ab33 753 * that the GattServer is about to be shut down; then it clears all
be_bryan 0:b74591d5ab33 754 * GattServer state.
be_bryan 0:b74591d5ab33 755 *
be_bryan 0:b74591d5ab33 756 * @note This function is meant to be overridden in the platform-specific
be_bryan 0:b74591d5ab33 757 * subclass. Overides must call the parent function before any cleanup.
be_bryan 0:b74591d5ab33 758 *
be_bryan 0:b74591d5ab33 759 * @return BLE_ERROR_NONE on success.
be_bryan 0:b74591d5ab33 760 */
be_bryan 0:b74591d5ab33 761 virtual ble_error_t reset(void)
be_bryan 0:b74591d5ab33 762 {
be_bryan 0:b74591d5ab33 763 /* Notify that the instance is about to shutdown */
be_bryan 0:b74591d5ab33 764 shutdownCallChain.call(this);
be_bryan 0:b74591d5ab33 765 shutdownCallChain.clear();
be_bryan 0:b74591d5ab33 766
be_bryan 0:b74591d5ab33 767 serviceCount = 0;
be_bryan 0:b74591d5ab33 768 characteristicCount = 0;
be_bryan 0:b74591d5ab33 769
be_bryan 0:b74591d5ab33 770 dataSentCallChain.clear();
be_bryan 0:b74591d5ab33 771 dataWrittenCallChain.clear();
be_bryan 0:b74591d5ab33 772 dataReadCallChain.clear();
be_bryan 0:b74591d5ab33 773 updatesEnabledCallback = NULL;
be_bryan 0:b74591d5ab33 774 updatesDisabledCallback = NULL;
be_bryan 0:b74591d5ab33 775 confirmationReceivedCallback = NULL;
be_bryan 0:b74591d5ab33 776
be_bryan 0:b74591d5ab33 777 return BLE_ERROR_NONE;
be_bryan 0:b74591d5ab33 778 }
be_bryan 0:b74591d5ab33 779
be_bryan 0:b74591d5ab33 780 protected:
be_bryan 0:b74591d5ab33 781 /**
be_bryan 0:b74591d5ab33 782 * The total number of services added to the ATT table.
be_bryan 0:b74591d5ab33 783 */
be_bryan 0:b74591d5ab33 784 uint8_t serviceCount;
be_bryan 0:b74591d5ab33 785
be_bryan 0:b74591d5ab33 786 /**
be_bryan 0:b74591d5ab33 787 * The total number of characteristics added to the ATT table.
be_bryan 0:b74591d5ab33 788 */
be_bryan 0:b74591d5ab33 789 uint8_t characteristicCount;
be_bryan 0:b74591d5ab33 790
be_bryan 0:b74591d5ab33 791 private:
be_bryan 0:b74591d5ab33 792 /**
be_bryan 0:b74591d5ab33 793 * Callchain containing all registered callback handlers for data sent
be_bryan 0:b74591d5ab33 794 * events.
be_bryan 0:b74591d5ab33 795 */
be_bryan 0:b74591d5ab33 796 DataSentCallbackChain_t dataSentCallChain;
be_bryan 0:b74591d5ab33 797
be_bryan 0:b74591d5ab33 798 /**
be_bryan 0:b74591d5ab33 799 * Callchain containing all registered callback handlers for data written
be_bryan 0:b74591d5ab33 800 * events.
be_bryan 0:b74591d5ab33 801 */
be_bryan 0:b74591d5ab33 802 DataWrittenCallbackChain_t dataWrittenCallChain;
be_bryan 0:b74591d5ab33 803
be_bryan 0:b74591d5ab33 804 /**
be_bryan 0:b74591d5ab33 805 * Callchain containing all registered callback handlers for data read
be_bryan 0:b74591d5ab33 806 * events.
be_bryan 0:b74591d5ab33 807 */
be_bryan 0:b74591d5ab33 808 DataReadCallbackChain_t dataReadCallChain;
be_bryan 0:b74591d5ab33 809
be_bryan 0:b74591d5ab33 810 /**
be_bryan 0:b74591d5ab33 811 * Callchain containing all registered callback handlers for shutdown
be_bryan 0:b74591d5ab33 812 * events.
be_bryan 0:b74591d5ab33 813 */
be_bryan 0:b74591d5ab33 814 GattServerShutdownCallbackChain_t shutdownCallChain;
be_bryan 0:b74591d5ab33 815
be_bryan 0:b74591d5ab33 816 /**
be_bryan 0:b74591d5ab33 817 * The registered callback handler for updates enabled events.
be_bryan 0:b74591d5ab33 818 */
be_bryan 0:b74591d5ab33 819 EventCallback_t updatesEnabledCallback;
be_bryan 0:b74591d5ab33 820
be_bryan 0:b74591d5ab33 821 /**
be_bryan 0:b74591d5ab33 822 * The registered callback handler for updates disabled events.
be_bryan 0:b74591d5ab33 823 */
be_bryan 0:b74591d5ab33 824 EventCallback_t updatesDisabledCallback;
be_bryan 0:b74591d5ab33 825
be_bryan 0:b74591d5ab33 826 /**
be_bryan 0:b74591d5ab33 827 * The registered callback handler for confirmation received events.
be_bryan 0:b74591d5ab33 828 */
be_bryan 0:b74591d5ab33 829 EventCallback_t confirmationReceivedCallback;
be_bryan 0:b74591d5ab33 830
be_bryan 0:b74591d5ab33 831 private:
be_bryan 0:b74591d5ab33 832 /* Disallow copy and assignment. */
be_bryan 0:b74591d5ab33 833 GattServer(const GattServer &);
be_bryan 0:b74591d5ab33 834 GattServer& operator=(const GattServer &);
be_bryan 0:b74591d5ab33 835 };
be_bryan 0:b74591d5ab33 836
be_bryan 0:b74591d5ab33 837 /**
be_bryan 0:b74591d5ab33 838 * @}
be_bryan 0:b74591d5ab33 839 * @}
be_bryan 0:b74591d5ab33 840 * @}
be_bryan 0:b74591d5ab33 841 */
be_bryan 0:b74591d5ab33 842
be_bryan 0:b74591d5ab33 843 #endif /* ifndef MBED_GATT_SERVER_H__ */