High level Bluetooth Low Energy API and radio abstraction layer
Dependents: BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate BLE_ANCS_SDAPI_IRC ... more
Overview
The BLE_API is a high level abstraction for using Bluetooth Low Energy on multiple platforms. For details and examples using the BLE_API please see the BLE_API Summary Page. Or click on the API Documentation tab above.
Supported Services
Supported services can be found in the BLE_API/services folder.
ble/GattServer.h@731:401c2bc0c72f, 2015-07-06 (annotated)
- Committer:
- rgrover1
- Date:
- Mon Jul 06 10:10:34 2015 +0100
- Revision:
- 731:401c2bc0c72f
- Parent:
- 729:951b577529c9
- Child:
- 732:916f36dd93f8
Synchronized with git rev c7dbf2b0
Author: James Crosby
use mbed-classic to build (this will probably be a target-dependendency in the future)
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 | 728:997ba5e7b3b6 | 56 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): 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 | 728:997ba5e7b3b6 | 75 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): 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 | 731:401c2bc0c72f | 97 | * attributes (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 | 728:997ba5e7b3b6 | 100 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): 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 | 728:997ba5e7b3b6 | 122 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): 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 | 731:401c2bc0c72f | 128 | * for connection-specific multivalued attributes (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 | 728:997ba5e7b3b6 | 148 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 728:997ba5e7b3b6 | 149 | } |
rgrover1 | 728:997ba5e7b3b6 | 150 | |
rgrover1 | 728:997ba5e7b3b6 | 151 | /** |
rgrover1 | 728:997ba5e7b3b6 | 152 | * Determine the updates-enabled status (notification/indication) for the current connection from a characteristic's CCCD. |
rgrover1 | 728:997ba5e7b3b6 | 153 | * |
rgrover1 | 728:997ba5e7b3b6 | 154 | * @param characteristic |
rgrover1 | 728:997ba5e7b3b6 | 155 | * The characteristic |
rgrover1 | 728:997ba5e7b3b6 | 156 | * @param[out] enabledP |
rgrover1 | 728:997ba5e7b3b6 | 157 | * Upon return, *enabledP is true if updates are enabled, else false. |
rgrover1 | 728:997ba5e7b3b6 | 158 | * |
rgrover1 | 728:997ba5e7b3b6 | 159 | * @return BLE_ERROR_NONE if the connection and handle are found. false otherwise. |
rgrover1 | 728:997ba5e7b3b6 | 160 | */ |
rgrover1 | 728:997ba5e7b3b6 | 161 | virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) { |
rgrover1 | 728:997ba5e7b3b6 | 162 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 728:997ba5e7b3b6 | 163 | } |
rgrover1 | 728:997ba5e7b3b6 | 164 | |
rgrover1 | 728:997ba5e7b3b6 | 165 | /** |
rgrover1 | 728:997ba5e7b3b6 | 166 | * Determine the connection-specific updates-enabled status (notification/indication) from a characteristic's CCCD. |
rgrover1 | 728:997ba5e7b3b6 | 167 | * |
rgrover1 | 728:997ba5e7b3b6 | 168 | * @param connectionHandle |
rgrover1 | 728:997ba5e7b3b6 | 169 | * The connection handle |
rgrover1 | 728:997ba5e7b3b6 | 170 | * @param[out] enabledP |
rgrover1 | 728:997ba5e7b3b6 | 171 | * Upon return, *enabledP is true if updates are enabled, else false. |
rgrover1 | 728:997ba5e7b3b6 | 172 | * |
rgrover1 | 728:997ba5e7b3b6 | 173 | * @param characteristic |
rgrover1 | 728:997ba5e7b3b6 | 174 | * The characteristic |
rgrover1 | 728:997ba5e7b3b6 | 175 | * |
rgrover1 | 728:997ba5e7b3b6 | 176 | * @return BLE_ERROR_NONE if the connection and handle are found. false otherwise. |
rgrover1 | 728:997ba5e7b3b6 | 177 | */ |
rgrover1 | 728:997ba5e7b3b6 | 178 | virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) { |
rgrover1 | 728:997ba5e7b3b6 | 179 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 724:80e065731d70 | 180 | } |
rgrover1 | 724:80e065731d70 | 181 | |
rgrover1 | 724:80e065731d70 | 182 | /** |
rgrover1 | 710:b2e1a2660ec2 | 183 | * A virtual function to allow underlying stacks to indicate if they support |
rgrover1 | 710:b2e1a2660ec2 | 184 | * onDataRead(). It should be overridden to return true as applicable. |
rgrover1 | 710:b2e1a2660ec2 | 185 | */ |
rgrover1 | 710:b2e1a2660ec2 | 186 | virtual bool isOnDataReadAvailable() const { |
rgrover1 | 710:b2e1a2660ec2 | 187 | return false; /* default implementation; override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 188 | } |
rgrover1 | 710:b2e1a2660ec2 | 189 | |
rgrover1 | 710:b2e1a2660ec2 | 190 | /* |
rgrover1 | 710:b2e1a2660ec2 | 191 | * APIs with non-virtual implementations. |
rgrover1 | 710:b2e1a2660ec2 | 192 | */ |
rgrover1 | 710:b2e1a2660ec2 | 193 | public: |
rgrover1 | 710:b2e1a2660ec2 | 194 | /** |
rgrover1 | 710:b2e1a2660ec2 | 195 | * Add a callback for the GATT event DATA_SENT (which is triggered when |
rgrover1 | 710:b2e1a2660ec2 | 196 | * updates are sent out by GATT in the form of notifications). |
rgrover1 | 710:b2e1a2660ec2 | 197 | * |
rgrover1 | 710:b2e1a2660ec2 | 198 | * @Note: it is possible to chain together multiple onDataSent callbacks |
rgrover1 | 710:b2e1a2660ec2 | 199 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 200 | * to characteristics. |
rgrover1 | 710:b2e1a2660ec2 | 201 | * |
rgrover1 | 710:b2e1a2660ec2 | 202 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 203 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 204 | */ |
rgrover1 | 710:b2e1a2660ec2 | 205 | void onDataSent(void (*callback)(unsigned count)) {dataSentCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 206 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 207 | void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { |
rgrover1 | 710:b2e1a2660ec2 | 208 | dataSentCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 209 | } |
rgrover1 | 710:b2e1a2660ec2 | 210 | |
rgrover1 | 710:b2e1a2660ec2 | 211 | /** |
rgrover1 | 710:b2e1a2660ec2 | 212 | * Setup a callback for when an attribute has its value updated by or at the |
rgrover1 | 710:b2e1a2660ec2 | 213 | * connected peer. For a peripheral, this callback triggered when the local |
rgrover1 | 710:b2e1a2660ec2 | 214 | * GATT server has an attribute updated by a write command from the peer. |
rgrover1 | 710:b2e1a2660ec2 | 215 | * For a Central, this callback is triggered when a response is received for |
rgrover1 | 710:b2e1a2660ec2 | 216 | * a write request. |
rgrover1 | 710:b2e1a2660ec2 | 217 | * |
rgrover1 | 710:b2e1a2660ec2 | 218 | * @Note: it is possible to chain together multiple onDataWritten callbacks |
rgrover1 | 710:b2e1a2660ec2 | 219 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 220 | * to characteristics. Many services, such as DFU and UART add their own |
rgrover1 | 710:b2e1a2660ec2 | 221 | * onDataWritten callbacks behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 222 | * |
rgrover1 | 710:b2e1a2660ec2 | 223 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 224 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 225 | */ |
rgrover1 | 710:b2e1a2660ec2 | 226 | void onDataWritten(void (*callback)(const GattWriteCallbackParams *eventDataP)) {dataWrittenCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 227 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 228 | void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 229 | dataWrittenCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 230 | } |
rgrover1 | 710:b2e1a2660ec2 | 231 | |
rgrover1 | 710:b2e1a2660ec2 | 232 | /** |
rgrover1 | 710:b2e1a2660ec2 | 233 | * Setup a callback to be invoked on the peripheral when an attribute is |
rgrover1 | 710:b2e1a2660ec2 | 234 | * being read by a remote client. |
rgrover1 | 710:b2e1a2660ec2 | 235 | * |
rgrover1 | 710:b2e1a2660ec2 | 236 | * @Note: this functionality may not be available on all underlying stacks. |
rgrover1 | 710:b2e1a2660ec2 | 237 | * You could use GattCharacteristic::setReadAuthorizationCallback() as an |
rgrover1 | 710:b2e1a2660ec2 | 238 | * alternative. |
rgrover1 | 710:b2e1a2660ec2 | 239 | * |
rgrover1 | 710:b2e1a2660ec2 | 240 | * @Note: it is possible to chain together multiple onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 241 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 242 | * to characteristics. Services may add their own onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 243 | * behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 244 | * |
rgrover1 | 710:b2e1a2660ec2 | 245 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 246 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 247 | * |
rgrover1 | 710:b2e1a2660ec2 | 248 | * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available; |
rgrover1 | 710:b2e1a2660ec2 | 249 | * else BLE_ERROR_NONE. |
rgrover1 | 710:b2e1a2660ec2 | 250 | */ |
rgrover1 | 710:b2e1a2660ec2 | 251 | ble_error_t onDataRead(void (*callback)(const GattReadCallbackParams *eventDataP)) { |
rgrover1 | 710:b2e1a2660ec2 | 252 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 253 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 254 | } |
rgrover1 | 710:b2e1a2660ec2 | 255 | |
rgrover1 | 710:b2e1a2660ec2 | 256 | dataReadCallChain.add(callback); |
rgrover1 | 710:b2e1a2660ec2 | 257 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 258 | } |
rgrover1 | 710:b2e1a2660ec2 | 259 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 260 | ble_error_t onDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 261 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 262 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 263 | } |
rgrover1 | 710:b2e1a2660ec2 | 264 | |
rgrover1 | 710:b2e1a2660ec2 | 265 | dataReadCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 266 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 267 | } |
rgrover1 | 710:b2e1a2660ec2 | 268 | |
rgrover1 | 710:b2e1a2660ec2 | 269 | /** |
rgrover1 | 710:b2e1a2660ec2 | 270 | * Setup a callback for when notifications/indications are enabled for a |
rgrover1 | 710:b2e1a2660ec2 | 271 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 272 | */ |
rgrover1 | 710:b2e1a2660ec2 | 273 | void onUpdatesEnabled(EventCallback_t callback) {updatesEnabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 274 | |
rgrover1 | 710:b2e1a2660ec2 | 275 | /** |
rgrover1 | 710:b2e1a2660ec2 | 276 | * Setup a callback for when notifications/indications are disabled for a |
rgrover1 | 710:b2e1a2660ec2 | 277 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 278 | */ |
rgrover1 | 710:b2e1a2660ec2 | 279 | void onUpdatesDisabled(EventCallback_t callback) {updatesDisabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 280 | |
rgrover1 | 710:b2e1a2660ec2 | 281 | /** |
rgrover1 | 710:b2e1a2660ec2 | 282 | * Setup a callback for when the GATT server receives a response for an |
rgrover1 | 710:b2e1a2660ec2 | 283 | * indication event sent previously. |
rgrover1 | 710:b2e1a2660ec2 | 284 | */ |
rgrover1 | 710:b2e1a2660ec2 | 285 | void onConfirmationReceived(EventCallback_t callback) {confirmationReceivedCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 286 | |
rgrover1 | 710:b2e1a2660ec2 | 287 | /* Entry points for the underlying stack to report events back to the user. */ |
rgrover1 | 710:b2e1a2660ec2 | 288 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 289 | void handleDataWrittenEvent(const GattWriteCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 290 | if (dataWrittenCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 291 | dataWrittenCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 292 | } |
rgrover1 | 710:b2e1a2660ec2 | 293 | } |
rgrover1 | 710:b2e1a2660ec2 | 294 | |
rgrover1 | 710:b2e1a2660ec2 | 295 | void handleDataReadEvent(const GattReadCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 296 | if (dataReadCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 297 | dataReadCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 298 | } |
rgrover1 | 710:b2e1a2660ec2 | 299 | } |
rgrover1 | 710:b2e1a2660ec2 | 300 | |
rgrover1 | 728:997ba5e7b3b6 | 301 | void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t attributeHandle) { |
rgrover1 | 710:b2e1a2660ec2 | 302 | switch (type) { |
rgrover1 | 710:b2e1a2660ec2 | 303 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
rgrover1 | 710:b2e1a2660ec2 | 304 | if (updatesEnabledCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 305 | updatesEnabledCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 306 | } |
rgrover1 | 710:b2e1a2660ec2 | 307 | break; |
rgrover1 | 710:b2e1a2660ec2 | 308 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
rgrover1 | 710:b2e1a2660ec2 | 309 | if (updatesDisabledCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 310 | updatesDisabledCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 311 | } |
rgrover1 | 710:b2e1a2660ec2 | 312 | break; |
rgrover1 | 710:b2e1a2660ec2 | 313 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
rgrover1 | 710:b2e1a2660ec2 | 314 | if (confirmationReceivedCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 315 | confirmationReceivedCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 316 | } |
rgrover1 | 710:b2e1a2660ec2 | 317 | break; |
rgrover1 | 710:b2e1a2660ec2 | 318 | default: |
rgrover1 | 710:b2e1a2660ec2 | 319 | break; |
rgrover1 | 710:b2e1a2660ec2 | 320 | } |
rgrover1 | 710:b2e1a2660ec2 | 321 | } |
rgrover1 | 710:b2e1a2660ec2 | 322 | |
rgrover1 | 710:b2e1a2660ec2 | 323 | void handleDataSentEvent(unsigned count) { |
rgrover1 | 710:b2e1a2660ec2 | 324 | if (dataSentCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 325 | dataSentCallChain.call(count); |
rgrover1 | 710:b2e1a2660ec2 | 326 | } |
rgrover1 | 710:b2e1a2660ec2 | 327 | } |
rgrover1 | 710:b2e1a2660ec2 | 328 | |
rgrover1 | 710:b2e1a2660ec2 | 329 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 330 | uint8_t serviceCount; |
rgrover1 | 710:b2e1a2660ec2 | 331 | uint8_t characteristicCount; |
rgrover1 | 710:b2e1a2660ec2 | 332 | |
rgrover1 | 710:b2e1a2660ec2 | 333 | private: |
rgrover1 | 710:b2e1a2660ec2 | 334 | CallChainOfFunctionPointersWithContext<unsigned> dataSentCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 335 | CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams *> dataWrittenCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 336 | CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> dataReadCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 337 | EventCallback_t updatesEnabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 338 | EventCallback_t updatesDisabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 339 | EventCallback_t confirmationReceivedCallback; |
rgrover1 | 710:b2e1a2660ec2 | 340 | |
rgrover1 | 710:b2e1a2660ec2 | 341 | private: |
rgrover1 | 710:b2e1a2660ec2 | 342 | /* disallow copy and assignment */ |
rgrover1 | 710:b2e1a2660ec2 | 343 | GattServer(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 344 | GattServer& operator=(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 345 | }; |
rgrover1 | 710:b2e1a2660ec2 | 346 | |
rgrover1 | 710:b2e1a2660ec2 | 347 | #endif // ifndef __GATT_SERVER_H__ |