BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:47:08 2018 +0000
Revision:
1:9c5af431a1f1
sdf

Who changed what in which revision?

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