Bike service
Fork of BLE_API by
ble/GattServer.h@710:b2e1a2660ec2, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:53:28 2015 +0100
- Revision:
- 710:b2e1a2660ec2
- Child:
- 721:8f92a1a943d6
Synchronized with git rev 7e8977d8
Author: Rohit Grover
Release 0.3.8
=============
This is a minor set of enhancements before we yotta-ize BLE_API.
Enhancements
~~~~~~~~~~~~
* Minor rework for class UUID; added a default and copy constructor; and a != operator.
* Added copy constructor and accessors for GapAdvertisingParams.
* GapScanningParams:: remove unnecessary checks for SCAN_TIMEOUT_MAX.
* Add a comment header block to explain why BLEDevice::init() may not be safe
to call from global static context.
* Introduce GattAttribute::INVALID_HANDLE.
* Replace some deprecated uses of Gap::address_t with Gap::Address_t.
Bugfixes
~~~~~~~~
* None.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 710:b2e1a2660ec2 | 1 | /* mbed Microcontroller Library |
rgrover1 | 710:b2e1a2660ec2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 710:b2e1a2660ec2 | 3 | * |
rgrover1 | 710:b2e1a2660ec2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 710:b2e1a2660ec2 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 710:b2e1a2660ec2 | 6 | * You may obtain a copy of the License at |
rgrover1 | 710:b2e1a2660ec2 | 7 | * |
rgrover1 | 710:b2e1a2660ec2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 710:b2e1a2660ec2 | 9 | * |
rgrover1 | 710:b2e1a2660ec2 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 710:b2e1a2660ec2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 710:b2e1a2660ec2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 710:b2e1a2660ec2 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 710:b2e1a2660ec2 | 14 | * limitations under the License. |
rgrover1 | 710:b2e1a2660ec2 | 15 | */ |
rgrover1 | 710:b2e1a2660ec2 | 16 | |
rgrover1 | 710:b2e1a2660ec2 | 17 | #ifndef __GATT_SERVER_H__ |
rgrover1 | 710:b2e1a2660ec2 | 18 | #define __GATT_SERVER_H__ |
rgrover1 | 710:b2e1a2660ec2 | 19 | |
rgrover1 | 710:b2e1a2660ec2 | 20 | #include "Gap.h" |
rgrover1 | 710:b2e1a2660ec2 | 21 | #include "GattService.h" |
rgrover1 | 710:b2e1a2660ec2 | 22 | #include "GattAttribute.h" |
rgrover1 | 710:b2e1a2660ec2 | 23 | #include "GattServerEvents.h" |
rgrover1 | 710:b2e1a2660ec2 | 24 | #include "GattCallbackParamTypes.h" |
rgrover1 | 710:b2e1a2660ec2 | 25 | #include "CallChainOfFunctionPointersWithContext.h" |
rgrover1 | 710:b2e1a2660ec2 | 26 | |
rgrover1 | 710:b2e1a2660ec2 | 27 | class GattServer { |
rgrover1 | 710:b2e1a2660ec2 | 28 | public: |
rgrover1 | 710:b2e1a2660ec2 | 29 | /* Event callback handlers. */ |
rgrover1 | 710:b2e1a2660ec2 | 30 | typedef void (*EventCallback_t)(GattAttribute::Handle_t attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 31 | typedef void (*ServerEventCallback_t)(void); /**< independent of any particular attribute */ |
rgrover1 | 710:b2e1a2660ec2 | 32 | |
rgrover1 | 710:b2e1a2660ec2 | 33 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 34 | GattServer() : |
rgrover1 | 710:b2e1a2660ec2 | 35 | serviceCount(0), |
rgrover1 | 710:b2e1a2660ec2 | 36 | characteristicCount(0), |
rgrover1 | 710:b2e1a2660ec2 | 37 | dataSentCallChain(), |
rgrover1 | 710:b2e1a2660ec2 | 38 | dataWrittenCallChain(), |
rgrover1 | 710:b2e1a2660ec2 | 39 | dataReadCallChain(), |
rgrover1 | 710:b2e1a2660ec2 | 40 | updatesEnabledCallback(NULL), |
rgrover1 | 710:b2e1a2660ec2 | 41 | updatesDisabledCallback(NULL), |
rgrover1 | 710:b2e1a2660ec2 | 42 | confirmationReceivedCallback(NULL) { |
rgrover1 | 710:b2e1a2660ec2 | 43 | /* empty */ |
rgrover1 | 710:b2e1a2660ec2 | 44 | } |
rgrover1 | 710:b2e1a2660ec2 | 45 | |
rgrover1 | 710:b2e1a2660ec2 | 46 | /* |
rgrover1 | 710:b2e1a2660ec2 | 47 | * The following functions are meant to be overridden in the platform-specific sub-class. |
rgrover1 | 710:b2e1a2660ec2 | 48 | */ |
rgrover1 | 710:b2e1a2660ec2 | 49 | public: |
rgrover1 | 710:b2e1a2660ec2 | 50 | |
rgrover1 | 710:b2e1a2660ec2 | 51 | /** |
rgrover1 | 710:b2e1a2660ec2 | 52 | * Add a service declaration to the local server ATT table. Also add the |
rgrover1 | 710:b2e1a2660ec2 | 53 | * characteristics contained within. |
rgrover1 | 710:b2e1a2660ec2 | 54 | */ |
rgrover1 | 710:b2e1a2660ec2 | 55 | virtual ble_error_t addService(GattService &) { |
rgrover1 | 710:b2e1a2660ec2 | 56 | return BLE_ERROR_NOT_IMPLEMENTED; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 57 | } |
rgrover1 | 710:b2e1a2660ec2 | 58 | |
rgrover1 | 710:b2e1a2660ec2 | 59 | /** |
rgrover1 | 710:b2e1a2660ec2 | 60 | * Read the value of a characteristic from the local GattServer |
rgrover1 | 710:b2e1a2660ec2 | 61 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 62 | * Attribute handle for the value attribute of the characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 63 | * @param[out] buffer |
rgrover1 | 710:b2e1a2660ec2 | 64 | * A buffer to hold the value being read. |
rgrover1 | 710:b2e1a2660ec2 | 65 | * @param[in/out] lengthP |
rgrover1 | 710:b2e1a2660ec2 | 66 | * Length of the buffer being supplied. If the attribute |
rgrover1 | 710:b2e1a2660ec2 | 67 | * value is longer than the size of the supplied buffer, |
rgrover1 | 710:b2e1a2660ec2 | 68 | * this variable will hold upon return the total attribute value length |
rgrover1 | 710:b2e1a2660ec2 | 69 | * (excluding offset). The application may use this |
rgrover1 | 710:b2e1a2660ec2 | 70 | * information to allocate a suitable buffer size. |
rgrover1 | 710:b2e1a2660ec2 | 71 | * |
rgrover1 | 710:b2e1a2660ec2 | 72 | * @return BLE_ERROR_NONE if a value was read successfully into the buffer. |
rgrover1 | 710:b2e1a2660ec2 | 73 | */ |
rgrover1 | 710:b2e1a2660ec2 | 74 | virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) { |
rgrover1 | 710:b2e1a2660ec2 | 75 | return BLE_ERROR_NOT_IMPLEMENTED; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 76 | } |
rgrover1 | 710:b2e1a2660ec2 | 77 | |
rgrover1 | 710:b2e1a2660ec2 | 78 | /** |
rgrover1 | 710:b2e1a2660ec2 | 79 | * Read the value of a characteristic from the local GattServer |
rgrover1 | 710:b2e1a2660ec2 | 80 | * @param[in] connectionHandle |
rgrover1 | 710:b2e1a2660ec2 | 81 | * Connection Handle. |
rgrover1 | 710:b2e1a2660ec2 | 82 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 83 | * Attribute handle for the value attribute of the characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 84 | * @param[out] buffer |
rgrover1 | 710:b2e1a2660ec2 | 85 | * A buffer to hold the value being read. |
rgrover1 | 710:b2e1a2660ec2 | 86 | * @param[in/out] lengthP |
rgrover1 | 710:b2e1a2660ec2 | 87 | * Length of the buffer being supplied. If the attribute |
rgrover1 | 710:b2e1a2660ec2 | 88 | * value is longer than the size of the supplied buffer, |
rgrover1 | 710:b2e1a2660ec2 | 89 | * this variable will hold upon return the total attribute value length |
rgrover1 | 710:b2e1a2660ec2 | 90 | * (excluding offset). The application may use this |
rgrover1 | 710:b2e1a2660ec2 | 91 | * information to allocate a suitable buffer size. |
rgrover1 | 710:b2e1a2660ec2 | 92 | * |
rgrover1 | 710:b2e1a2660ec2 | 93 | * @return BLE_ERROR_NONE if a value was read successfully into the buffer. |
rgrover1 | 710:b2e1a2660ec2 | 94 | * |
rgrover1 | 710:b2e1a2660ec2 | 95 | * @note This API is a version of above with an additional connection handle |
rgrover1 | 710:b2e1a2660ec2 | 96 | * parameter to allow fetches for connection-specific multivalued |
rgrover1 | 710:b2e1a2660ec2 | 97 | * attribtues (such as the CCCDs). |
rgrover1 | 710:b2e1a2660ec2 | 98 | */ |
rgrover1 | 710:b2e1a2660ec2 | 99 | virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t *buffer, uint16_t *lengthP) { |
rgrover1 | 710:b2e1a2660ec2 | 100 | return BLE_ERROR_NOT_IMPLEMENTED; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 101 | } |
rgrover1 | 710:b2e1a2660ec2 | 102 | |
rgrover1 | 710:b2e1a2660ec2 | 103 | /** |
rgrover1 | 710:b2e1a2660ec2 | 104 | * Update the value of a characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 105 | * |
rgrover1 | 710:b2e1a2660ec2 | 106 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 107 | * Handle for the value attribute of the Characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 108 | * @param[in] value |
rgrover1 | 710:b2e1a2660ec2 | 109 | * A pointer to a buffer holding the new value |
rgrover1 | 710:b2e1a2660ec2 | 110 | * @param[in] size |
rgrover1 | 710:b2e1a2660ec2 | 111 | * Size of the new value (in bytes). |
rgrover1 | 710:b2e1a2660ec2 | 112 | * @param[in] localOnly |
rgrover1 | 710:b2e1a2660ec2 | 113 | * Should this update be kept on the local |
rgrover1 | 710:b2e1a2660ec2 | 114 | * GattServer regardless of the state of the |
rgrover1 | 710:b2e1a2660ec2 | 115 | * notify/indicate flag in the CCCD for this |
rgrover1 | 710:b2e1a2660ec2 | 116 | * Characteristic? If set to true, no notification |
rgrover1 | 710:b2e1a2660ec2 | 117 | * or indication is generated. |
rgrover1 | 710:b2e1a2660ec2 | 118 | * |
rgrover1 | 710:b2e1a2660ec2 | 119 | * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. |
rgrover1 | 710:b2e1a2660ec2 | 120 | */ |
rgrover1 | 710:b2e1a2660ec2 | 121 | virtual ble_error_t write(GattAttribute::Handle_t, const uint8_t *, uint16_t, bool localOnly = false) { |
rgrover1 | 710:b2e1a2660ec2 | 122 | return BLE_ERROR_NOT_IMPLEMENTED; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 123 | } |
rgrover1 | 710:b2e1a2660ec2 | 124 | |
rgrover1 | 710:b2e1a2660ec2 | 125 | /** |
rgrover1 | 710:b2e1a2660ec2 | 126 | * Update the value of a characteristic on the local GattServer. A version |
rgrover1 | 710:b2e1a2660ec2 | 127 | * of the same as above with connection handle parameter to allow updates |
rgrover1 | 710:b2e1a2660ec2 | 128 | * for connection-specific multivalued attribtues (such as the CCCDs). |
rgrover1 | 710:b2e1a2660ec2 | 129 | * |
rgrover1 | 710:b2e1a2660ec2 | 130 | * @param[in] connectionHandle |
rgrover1 | 710:b2e1a2660ec2 | 131 | * Connection Handle. |
rgrover1 | 710:b2e1a2660ec2 | 132 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 133 | * Handle for the value attribute of the Characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 134 | * @param[in] value |
rgrover1 | 710:b2e1a2660ec2 | 135 | * A pointer to a buffer holding the new value |
rgrover1 | 710:b2e1a2660ec2 | 136 | * @param[in] size |
rgrover1 | 710:b2e1a2660ec2 | 137 | * Size of the new value (in bytes). |
rgrover1 | 710:b2e1a2660ec2 | 138 | * @param[in] localOnly |
rgrover1 | 710:b2e1a2660ec2 | 139 | * Should this update be kept on the local |
rgrover1 | 710:b2e1a2660ec2 | 140 | * GattServer regardless of the state of the |
rgrover1 | 710:b2e1a2660ec2 | 141 | * notify/indicate flag in the CCCD for this |
rgrover1 | 710:b2e1a2660ec2 | 142 | * Characteristic? If set to true, no notification |
rgrover1 | 710:b2e1a2660ec2 | 143 | * or indication is generated. |
rgrover1 | 710:b2e1a2660ec2 | 144 | * |
rgrover1 | 710:b2e1a2660ec2 | 145 | * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. |
rgrover1 | 710:b2e1a2660ec2 | 146 | */ |
rgrover1 | 710:b2e1a2660ec2 | 147 | virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t *, uint16_t, bool localOnly = false) { |
rgrover1 | 710:b2e1a2660ec2 | 148 | return BLE_ERROR_NOT_IMPLEMENTED; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 149 | } |
rgrover1 | 710:b2e1a2660ec2 | 150 | |
rgrover1 | 710:b2e1a2660ec2 | 151 | /** |
rgrover1 | 710:b2e1a2660ec2 | 152 | * A virtual function to allow underlying stacks to indicate if they support |
rgrover1 | 710:b2e1a2660ec2 | 153 | * onDataRead(). It should be overridden to return true as applicable. |
rgrover1 | 710:b2e1a2660ec2 | 154 | */ |
rgrover1 | 710:b2e1a2660ec2 | 155 | virtual bool isOnDataReadAvailable() const { |
rgrover1 | 710:b2e1a2660ec2 | 156 | return false; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 157 | } |
rgrover1 | 710:b2e1a2660ec2 | 158 | |
rgrover1 | 710:b2e1a2660ec2 | 159 | /* |
rgrover1 | 710:b2e1a2660ec2 | 160 | * APIs with non-virtual implementations. |
rgrover1 | 710:b2e1a2660ec2 | 161 | */ |
rgrover1 | 710:b2e1a2660ec2 | 162 | public: |
rgrover1 | 710:b2e1a2660ec2 | 163 | /** |
rgrover1 | 710:b2e1a2660ec2 | 164 | * Add a callback for the GATT event DATA_SENT (which is triggered when |
rgrover1 | 710:b2e1a2660ec2 | 165 | * updates are sent out by GATT in the form of notifications). |
rgrover1 | 710:b2e1a2660ec2 | 166 | * |
rgrover1 | 710:b2e1a2660ec2 | 167 | * @Note: it is possible to chain together multiple onDataSent callbacks |
rgrover1 | 710:b2e1a2660ec2 | 168 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 169 | * to characteristics. |
rgrover1 | 710:b2e1a2660ec2 | 170 | * |
rgrover1 | 710:b2e1a2660ec2 | 171 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 172 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 173 | */ |
rgrover1 | 710:b2e1a2660ec2 | 174 | void onDataSent(void (*callback)(unsigned count)) {dataSentCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 175 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 176 | void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { |
rgrover1 | 710:b2e1a2660ec2 | 177 | dataSentCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 178 | } |
rgrover1 | 710:b2e1a2660ec2 | 179 | |
rgrover1 | 710:b2e1a2660ec2 | 180 | /** |
rgrover1 | 710:b2e1a2660ec2 | 181 | * Setup a callback for when an attribute has its value updated by or at the |
rgrover1 | 710:b2e1a2660ec2 | 182 | * connected peer. For a peripheral, this callback triggered when the local |
rgrover1 | 710:b2e1a2660ec2 | 183 | * GATT server has an attribute updated by a write command from the peer. |
rgrover1 | 710:b2e1a2660ec2 | 184 | * For a Central, this callback is triggered when a response is received for |
rgrover1 | 710:b2e1a2660ec2 | 185 | * a write request. |
rgrover1 | 710:b2e1a2660ec2 | 186 | * |
rgrover1 | 710:b2e1a2660ec2 | 187 | * @Note: it is possible to chain together multiple onDataWritten callbacks |
rgrover1 | 710:b2e1a2660ec2 | 188 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 189 | * to characteristics. Many services, such as DFU and UART add their own |
rgrover1 | 710:b2e1a2660ec2 | 190 | * onDataWritten callbacks behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 191 | * |
rgrover1 | 710:b2e1a2660ec2 | 192 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 193 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 194 | */ |
rgrover1 | 710:b2e1a2660ec2 | 195 | void onDataWritten(void (*callback)(const GattWriteCallbackParams *eventDataP)) {dataWrittenCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 196 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 197 | void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 198 | dataWrittenCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 199 | } |
rgrover1 | 710:b2e1a2660ec2 | 200 | |
rgrover1 | 710:b2e1a2660ec2 | 201 | /** |
rgrover1 | 710:b2e1a2660ec2 | 202 | * Setup a callback to be invoked on the peripheral when an attribute is |
rgrover1 | 710:b2e1a2660ec2 | 203 | * being read by a remote client. |
rgrover1 | 710:b2e1a2660ec2 | 204 | * |
rgrover1 | 710:b2e1a2660ec2 | 205 | * @Note: this functionality may not be available on all underlying stacks. |
rgrover1 | 710:b2e1a2660ec2 | 206 | * You could use GattCharacteristic::setReadAuthorizationCallback() as an |
rgrover1 | 710:b2e1a2660ec2 | 207 | * alternative. |
rgrover1 | 710:b2e1a2660ec2 | 208 | * |
rgrover1 | 710:b2e1a2660ec2 | 209 | * @Note: it is possible to chain together multiple onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 210 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 211 | * to characteristics. Services may add their own onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 212 | * behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 213 | * |
rgrover1 | 710:b2e1a2660ec2 | 214 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 215 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 216 | * |
rgrover1 | 710:b2e1a2660ec2 | 217 | * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available; |
rgrover1 | 710:b2e1a2660ec2 | 218 | * else BLE_ERROR_NONE. |
rgrover1 | 710:b2e1a2660ec2 | 219 | */ |
rgrover1 | 710:b2e1a2660ec2 | 220 | ble_error_t onDataRead(void (*callback)(const GattReadCallbackParams *eventDataP)) { |
rgrover1 | 710:b2e1a2660ec2 | 221 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 222 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 223 | } |
rgrover1 | 710:b2e1a2660ec2 | 224 | |
rgrover1 | 710:b2e1a2660ec2 | 225 | dataReadCallChain.add(callback); |
rgrover1 | 710:b2e1a2660ec2 | 226 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 227 | } |
rgrover1 | 710:b2e1a2660ec2 | 228 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 229 | ble_error_t onDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 230 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 231 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 232 | } |
rgrover1 | 710:b2e1a2660ec2 | 233 | |
rgrover1 | 710:b2e1a2660ec2 | 234 | dataReadCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 235 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 236 | } |
rgrover1 | 710:b2e1a2660ec2 | 237 | |
rgrover1 | 710:b2e1a2660ec2 | 238 | /** |
rgrover1 | 710:b2e1a2660ec2 | 239 | * Setup a callback for when notifications/indications are enabled for a |
rgrover1 | 710:b2e1a2660ec2 | 240 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 241 | */ |
rgrover1 | 710:b2e1a2660ec2 | 242 | void onUpdatesEnabled(EventCallback_t callback) {updatesEnabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 243 | |
rgrover1 | 710:b2e1a2660ec2 | 244 | /** |
rgrover1 | 710:b2e1a2660ec2 | 245 | * Setup a callback for when notifications/indications are disabled for a |
rgrover1 | 710:b2e1a2660ec2 | 246 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 247 | */ |
rgrover1 | 710:b2e1a2660ec2 | 248 | void onUpdatesDisabled(EventCallback_t callback) {updatesDisabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 249 | |
rgrover1 | 710:b2e1a2660ec2 | 250 | /** |
rgrover1 | 710:b2e1a2660ec2 | 251 | * Setup a callback for when the GATT server receives a response for an |
rgrover1 | 710:b2e1a2660ec2 | 252 | * indication event sent previously. |
rgrover1 | 710:b2e1a2660ec2 | 253 | */ |
rgrover1 | 710:b2e1a2660ec2 | 254 | void onConfirmationReceived(EventCallback_t callback) {confirmationReceivedCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 255 | |
rgrover1 | 710:b2e1a2660ec2 | 256 | /* Entry points for the underlying stack to report events back to the user. */ |
rgrover1 | 710:b2e1a2660ec2 | 257 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 258 | void handleDataWrittenEvent(const GattWriteCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 259 | if (dataWrittenCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 260 | dataWrittenCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 261 | } |
rgrover1 | 710:b2e1a2660ec2 | 262 | } |
rgrover1 | 710:b2e1a2660ec2 | 263 | |
rgrover1 | 710:b2e1a2660ec2 | 264 | void handleDataReadEvent(const GattReadCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 265 | if (dataReadCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 266 | dataReadCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 267 | } |
rgrover1 | 710:b2e1a2660ec2 | 268 | } |
rgrover1 | 710:b2e1a2660ec2 | 269 | |
rgrover1 | 710:b2e1a2660ec2 | 270 | void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t charHandle) { |
rgrover1 | 710:b2e1a2660ec2 | 271 | switch (type) { |
rgrover1 | 710:b2e1a2660ec2 | 272 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
rgrover1 | 710:b2e1a2660ec2 | 273 | if (updatesEnabledCallback) { |
rgrover1 | 710:b2e1a2660ec2 | 274 | updatesEnabledCallback(charHandle); |
rgrover1 | 710:b2e1a2660ec2 | 275 | } |
rgrover1 | 710:b2e1a2660ec2 | 276 | break; |
rgrover1 | 710:b2e1a2660ec2 | 277 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
rgrover1 | 710:b2e1a2660ec2 | 278 | if (updatesDisabledCallback) { |
rgrover1 | 710:b2e1a2660ec2 | 279 | updatesDisabledCallback(charHandle); |
rgrover1 | 710:b2e1a2660ec2 | 280 | } |
rgrover1 | 710:b2e1a2660ec2 | 281 | break; |
rgrover1 | 710:b2e1a2660ec2 | 282 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
rgrover1 | 710:b2e1a2660ec2 | 283 | if (confirmationReceivedCallback) { |
rgrover1 | 710:b2e1a2660ec2 | 284 | confirmationReceivedCallback(charHandle); |
rgrover1 | 710:b2e1a2660ec2 | 285 | } |
rgrover1 | 710:b2e1a2660ec2 | 286 | break; |
rgrover1 | 710:b2e1a2660ec2 | 287 | default: |
rgrover1 | 710:b2e1a2660ec2 | 288 | break; |
rgrover1 | 710:b2e1a2660ec2 | 289 | } |
rgrover1 | 710:b2e1a2660ec2 | 290 | } |
rgrover1 | 710:b2e1a2660ec2 | 291 | |
rgrover1 | 710:b2e1a2660ec2 | 292 | void handleDataSentEvent(unsigned count) { |
rgrover1 | 710:b2e1a2660ec2 | 293 | if (dataSentCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 294 | dataSentCallChain.call(count); |
rgrover1 | 710:b2e1a2660ec2 | 295 | } |
rgrover1 | 710:b2e1a2660ec2 | 296 | } |
rgrover1 | 710:b2e1a2660ec2 | 297 | |
rgrover1 | 710:b2e1a2660ec2 | 298 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 299 | uint8_t serviceCount; |
rgrover1 | 710:b2e1a2660ec2 | 300 | uint8_t characteristicCount; |
rgrover1 | 710:b2e1a2660ec2 | 301 | |
rgrover1 | 710:b2e1a2660ec2 | 302 | private: |
rgrover1 | 710:b2e1a2660ec2 | 303 | CallChainOfFunctionPointersWithContext<unsigned> dataSentCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 304 | CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams *> dataWrittenCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 305 | CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> dataReadCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 306 | EventCallback_t updatesEnabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 307 | EventCallback_t updatesDisabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 308 | EventCallback_t confirmationReceivedCallback; |
rgrover1 | 710:b2e1a2660ec2 | 309 | |
rgrover1 | 710:b2e1a2660ec2 | 310 | private: |
rgrover1 | 710:b2e1a2660ec2 | 311 | /* disallow copy and assignment */ |
rgrover1 | 710:b2e1a2660ec2 | 312 | GattServer(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 313 | GattServer& operator=(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 314 | }; |
rgrover1 | 710:b2e1a2660ec2 | 315 | |
rgrover1 | 710:b2e1a2660ec2 | 316 | #endif // ifndef __GATT_SERVER_H__ |