Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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