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@740:8d52f0f8efd9, 2015-07-21 (annotated)
- Committer:
- rgrover1
- Date:
- Tue Jul 21 13:25:38 2015 +0100
- Revision:
- 740:8d52f0f8efd9
- Parent:
- 734:4872b70437ce
- Child:
- 741:d6dceefb844e
Synchronized with git rev 9d289f81
Author: Rohit Grover
minor changes to comments for porting.
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 | 734:4872b70437ce | 55 | virtual ble_error_t addService(GattService &service) { |
rgrover1 | 734:4872b70437ce | 56 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 57 | (void)service; |
rgrover1 | 734:4872b70437ce | 58 | |
rgrover1 | 728:997ba5e7b3b6 | 59 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 60 | } |
rgrover1 | 710:b2e1a2660ec2 | 61 | |
rgrover1 | 710:b2e1a2660ec2 | 62 | /** |
rgrover1 | 710:b2e1a2660ec2 | 63 | * Read the value of a characteristic from the local GattServer |
rgrover1 | 710:b2e1a2660ec2 | 64 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 65 | * Attribute handle for the value attribute of the characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 66 | * @param[out] buffer |
rgrover1 | 710:b2e1a2660ec2 | 67 | * A buffer to hold the value being read. |
rgrover1 | 710:b2e1a2660ec2 | 68 | * @param[in/out] lengthP |
rgrover1 | 710:b2e1a2660ec2 | 69 | * Length of the buffer being supplied. If the attribute |
rgrover1 | 710:b2e1a2660ec2 | 70 | * value is longer than the size of the supplied buffer, |
rgrover1 | 710:b2e1a2660ec2 | 71 | * this variable will hold upon return the total attribute value length |
rgrover1 | 710:b2e1a2660ec2 | 72 | * (excluding offset). The application may use this |
rgrover1 | 710:b2e1a2660ec2 | 73 | * information to allocate a suitable buffer size. |
rgrover1 | 710:b2e1a2660ec2 | 74 | * |
rgrover1 | 710:b2e1a2660ec2 | 75 | * @return BLE_ERROR_NONE if a value was read successfully into the buffer. |
rgrover1 | 710:b2e1a2660ec2 | 76 | */ |
rgrover1 | 710:b2e1a2660ec2 | 77 | virtual ble_error_t read(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) { |
rgrover1 | 734:4872b70437ce | 78 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 79 | (void)attributeHandle; |
rgrover1 | 734:4872b70437ce | 80 | (void)buffer; |
rgrover1 | 734:4872b70437ce | 81 | (void)lengthP; |
rgrover1 | 734:4872b70437ce | 82 | |
rgrover1 | 728:997ba5e7b3b6 | 83 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 84 | } |
rgrover1 | 710:b2e1a2660ec2 | 85 | |
rgrover1 | 710:b2e1a2660ec2 | 86 | /** |
rgrover1 | 710:b2e1a2660ec2 | 87 | * Read the value of a characteristic from the local GattServer |
rgrover1 | 710:b2e1a2660ec2 | 88 | * @param[in] connectionHandle |
rgrover1 | 710:b2e1a2660ec2 | 89 | * Connection Handle. |
rgrover1 | 710:b2e1a2660ec2 | 90 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 91 | * Attribute handle for the value attribute of the characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 92 | * @param[out] buffer |
rgrover1 | 710:b2e1a2660ec2 | 93 | * A buffer to hold the value being read. |
rgrover1 | 710:b2e1a2660ec2 | 94 | * @param[in/out] lengthP |
rgrover1 | 710:b2e1a2660ec2 | 95 | * Length of the buffer being supplied. If the attribute |
rgrover1 | 710:b2e1a2660ec2 | 96 | * value is longer than the size of the supplied buffer, |
rgrover1 | 710:b2e1a2660ec2 | 97 | * this variable will hold upon return the total attribute value length |
rgrover1 | 710:b2e1a2660ec2 | 98 | * (excluding offset). The application may use this |
rgrover1 | 710:b2e1a2660ec2 | 99 | * information to allocate a suitable buffer size. |
rgrover1 | 710:b2e1a2660ec2 | 100 | * |
rgrover1 | 710:b2e1a2660ec2 | 101 | * @return BLE_ERROR_NONE if a value was read successfully into the buffer. |
rgrover1 | 710:b2e1a2660ec2 | 102 | * |
rgrover1 | 710:b2e1a2660ec2 | 103 | * @note This API is a version of above with an additional connection handle |
rgrover1 | 710:b2e1a2660ec2 | 104 | * parameter to allow fetches for connection-specific multivalued |
rgrover1 | 733:718a3566b4ce | 105 | * attributes (such as the CCCDs). |
rgrover1 | 710:b2e1a2660ec2 | 106 | */ |
rgrover1 | 710:b2e1a2660ec2 | 107 | virtual ble_error_t read(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t *buffer, uint16_t *lengthP) { |
rgrover1 | 734:4872b70437ce | 108 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 109 | (void)connectionHandle; |
rgrover1 | 734:4872b70437ce | 110 | (void)attributeHandle; |
rgrover1 | 734:4872b70437ce | 111 | (void)buffer; |
rgrover1 | 734:4872b70437ce | 112 | (void)lengthP; |
rgrover1 | 734:4872b70437ce | 113 | |
rgrover1 | 728:997ba5e7b3b6 | 114 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 115 | } |
rgrover1 | 710:b2e1a2660ec2 | 116 | |
rgrover1 | 710:b2e1a2660ec2 | 117 | /** |
rgrover1 | 710:b2e1a2660ec2 | 118 | * Update the value of a characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 119 | * |
rgrover1 | 710:b2e1a2660ec2 | 120 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 121 | * Handle for the value attribute of the Characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 122 | * @param[in] value |
rgrover1 | 710:b2e1a2660ec2 | 123 | * A pointer to a buffer holding the new value |
rgrover1 | 710:b2e1a2660ec2 | 124 | * @param[in] size |
rgrover1 | 710:b2e1a2660ec2 | 125 | * Size of the new value (in bytes). |
rgrover1 | 710:b2e1a2660ec2 | 126 | * @param[in] localOnly |
rgrover1 | 710:b2e1a2660ec2 | 127 | * Should this update be kept on the local |
rgrover1 | 710:b2e1a2660ec2 | 128 | * GattServer regardless of the state of the |
rgrover1 | 710:b2e1a2660ec2 | 129 | * notify/indicate flag in the CCCD for this |
rgrover1 | 710:b2e1a2660ec2 | 130 | * Characteristic? If set to true, no notification |
rgrover1 | 710:b2e1a2660ec2 | 131 | * or indication is generated. |
rgrover1 | 710:b2e1a2660ec2 | 132 | * |
rgrover1 | 710:b2e1a2660ec2 | 133 | * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. |
rgrover1 | 710:b2e1a2660ec2 | 134 | */ |
rgrover1 | 734:4872b70437ce | 135 | virtual ble_error_t write(GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) { |
rgrover1 | 734:4872b70437ce | 136 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 137 | (void)attributeHandle; |
rgrover1 | 734:4872b70437ce | 138 | (void)value; |
rgrover1 | 734:4872b70437ce | 139 | (void)size; |
rgrover1 | 734:4872b70437ce | 140 | (void)localOnly; |
rgrover1 | 734:4872b70437ce | 141 | |
rgrover1 | 728:997ba5e7b3b6 | 142 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 143 | } |
rgrover1 | 710:b2e1a2660ec2 | 144 | |
rgrover1 | 710:b2e1a2660ec2 | 145 | /** |
rgrover1 | 710:b2e1a2660ec2 | 146 | * Update the value of a characteristic on the local GattServer. A version |
rgrover1 | 710:b2e1a2660ec2 | 147 | * of the same as above with connection handle parameter to allow updates |
rgrover1 | 733:718a3566b4ce | 148 | * for connection-specific multivalued attributes (such as the CCCDs). |
rgrover1 | 710:b2e1a2660ec2 | 149 | * |
rgrover1 | 710:b2e1a2660ec2 | 150 | * @param[in] connectionHandle |
rgrover1 | 710:b2e1a2660ec2 | 151 | * Connection Handle. |
rgrover1 | 710:b2e1a2660ec2 | 152 | * @param[in] attributeHandle |
rgrover1 | 710:b2e1a2660ec2 | 153 | * Handle for the value attribute of the Characteristic. |
rgrover1 | 710:b2e1a2660ec2 | 154 | * @param[in] value |
rgrover1 | 710:b2e1a2660ec2 | 155 | * A pointer to a buffer holding the new value |
rgrover1 | 710:b2e1a2660ec2 | 156 | * @param[in] size |
rgrover1 | 710:b2e1a2660ec2 | 157 | * Size of the new value (in bytes). |
rgrover1 | 710:b2e1a2660ec2 | 158 | * @param[in] localOnly |
rgrover1 | 710:b2e1a2660ec2 | 159 | * Should this update be kept on the local |
rgrover1 | 710:b2e1a2660ec2 | 160 | * GattServer regardless of the state of the |
rgrover1 | 710:b2e1a2660ec2 | 161 | * notify/indicate flag in the CCCD for this |
rgrover1 | 710:b2e1a2660ec2 | 162 | * Characteristic? If set to true, no notification |
rgrover1 | 710:b2e1a2660ec2 | 163 | * or indication is generated. |
rgrover1 | 710:b2e1a2660ec2 | 164 | * |
rgrover1 | 710:b2e1a2660ec2 | 165 | * @return BLE_ERROR_NONE if we have successfully set the value of the attribute. |
rgrover1 | 710:b2e1a2660ec2 | 166 | */ |
rgrover1 | 734:4872b70437ce | 167 | virtual ble_error_t write(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, const uint8_t *value, uint16_t size, bool localOnly = false) { |
rgrover1 | 734:4872b70437ce | 168 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 169 | (void)connectionHandle; |
rgrover1 | 734:4872b70437ce | 170 | (void)attributeHandle; |
rgrover1 | 734:4872b70437ce | 171 | (void)value; |
rgrover1 | 734:4872b70437ce | 172 | (void)size; |
rgrover1 | 734:4872b70437ce | 173 | (void)localOnly; |
rgrover1 | 734:4872b70437ce | 174 | |
rgrover1 | 728:997ba5e7b3b6 | 175 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 728:997ba5e7b3b6 | 176 | } |
rgrover1 | 728:997ba5e7b3b6 | 177 | |
rgrover1 | 728:997ba5e7b3b6 | 178 | /** |
rgrover1 | 728:997ba5e7b3b6 | 179 | * Determine the updates-enabled status (notification/indication) for the current connection from a characteristic's CCCD. |
rgrover1 | 728:997ba5e7b3b6 | 180 | * |
rgrover1 | 728:997ba5e7b3b6 | 181 | * @param characteristic |
rgrover1 | 728:997ba5e7b3b6 | 182 | * The characteristic |
rgrover1 | 728:997ba5e7b3b6 | 183 | * @param[out] enabledP |
rgrover1 | 728:997ba5e7b3b6 | 184 | * Upon return, *enabledP is true if updates are enabled, else false. |
rgrover1 | 728:997ba5e7b3b6 | 185 | * |
rgrover1 | 728:997ba5e7b3b6 | 186 | * @return BLE_ERROR_NONE if the connection and handle are found. false otherwise. |
rgrover1 | 728:997ba5e7b3b6 | 187 | */ |
rgrover1 | 728:997ba5e7b3b6 | 188 | virtual ble_error_t areUpdatesEnabled(const GattCharacteristic &characteristic, bool *enabledP) { |
rgrover1 | 734:4872b70437ce | 189 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 190 | (void)characteristic; |
rgrover1 | 734:4872b70437ce | 191 | (void)enabledP; |
rgrover1 | 734:4872b70437ce | 192 | |
rgrover1 | 728:997ba5e7b3b6 | 193 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 728:997ba5e7b3b6 | 194 | } |
rgrover1 | 728:997ba5e7b3b6 | 195 | |
rgrover1 | 728:997ba5e7b3b6 | 196 | /** |
rgrover1 | 728:997ba5e7b3b6 | 197 | * Determine the connection-specific updates-enabled status (notification/indication) from a characteristic's CCCD. |
rgrover1 | 728:997ba5e7b3b6 | 198 | * |
rgrover1 | 728:997ba5e7b3b6 | 199 | * @param connectionHandle |
rgrover1 | 728:997ba5e7b3b6 | 200 | * The connection handle |
rgrover1 | 728:997ba5e7b3b6 | 201 | * @param[out] enabledP |
rgrover1 | 728:997ba5e7b3b6 | 202 | * Upon return, *enabledP is true if updates are enabled, else false. |
rgrover1 | 728:997ba5e7b3b6 | 203 | * |
rgrover1 | 728:997ba5e7b3b6 | 204 | * @param characteristic |
rgrover1 | 728:997ba5e7b3b6 | 205 | * The characteristic |
rgrover1 | 728:997ba5e7b3b6 | 206 | * |
rgrover1 | 728:997ba5e7b3b6 | 207 | * @return BLE_ERROR_NONE if the connection and handle are found. false otherwise. |
rgrover1 | 728:997ba5e7b3b6 | 208 | */ |
rgrover1 | 728:997ba5e7b3b6 | 209 | virtual ble_error_t areUpdatesEnabled(Gap::Handle_t connectionHandle, const GattCharacteristic &characteristic, bool *enabledP) { |
rgrover1 | 734:4872b70437ce | 210 | /* avoid compiler warnings about unused variables */ |
rgrover1 | 734:4872b70437ce | 211 | (void)connectionHandle; |
rgrover1 | 734:4872b70437ce | 212 | (void)characteristic; |
rgrover1 | 734:4872b70437ce | 213 | (void)enabledP; |
rgrover1 | 734:4872b70437ce | 214 | |
rgrover1 | 728:997ba5e7b3b6 | 215 | return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 724:80e065731d70 | 216 | } |
rgrover1 | 724:80e065731d70 | 217 | |
rgrover1 | 724:80e065731d70 | 218 | /** |
rgrover1 | 710:b2e1a2660ec2 | 219 | * A virtual function to allow underlying stacks to indicate if they support |
rgrover1 | 710:b2e1a2660ec2 | 220 | * onDataRead(). It should be overridden to return true as applicable. |
rgrover1 | 710:b2e1a2660ec2 | 221 | */ |
rgrover1 | 710:b2e1a2660ec2 | 222 | virtual bool isOnDataReadAvailable() const { |
rgrover1 | 740:8d52f0f8efd9 | 223 | return false; /* Requesting action from porter(s): override this API if this capability is supported. */ |
rgrover1 | 710:b2e1a2660ec2 | 224 | } |
rgrover1 | 710:b2e1a2660ec2 | 225 | |
rgrover1 | 710:b2e1a2660ec2 | 226 | /* |
rgrover1 | 710:b2e1a2660ec2 | 227 | * APIs with non-virtual implementations. |
rgrover1 | 710:b2e1a2660ec2 | 228 | */ |
rgrover1 | 710:b2e1a2660ec2 | 229 | public: |
rgrover1 | 710:b2e1a2660ec2 | 230 | /** |
rgrover1 | 710:b2e1a2660ec2 | 231 | * Add a callback for the GATT event DATA_SENT (which is triggered when |
rgrover1 | 710:b2e1a2660ec2 | 232 | * updates are sent out by GATT in the form of notifications). |
rgrover1 | 710:b2e1a2660ec2 | 233 | * |
rgrover1 | 710:b2e1a2660ec2 | 234 | * @Note: it is possible to chain together multiple onDataSent callbacks |
rgrover1 | 710:b2e1a2660ec2 | 235 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 236 | * to characteristics. |
rgrover1 | 710:b2e1a2660ec2 | 237 | * |
rgrover1 | 710:b2e1a2660ec2 | 238 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 239 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 240 | */ |
rgrover1 | 710:b2e1a2660ec2 | 241 | void onDataSent(void (*callback)(unsigned count)) {dataSentCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 242 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 243 | void onDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { |
rgrover1 | 710:b2e1a2660ec2 | 244 | dataSentCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 245 | } |
rgrover1 | 710:b2e1a2660ec2 | 246 | |
rgrover1 | 710:b2e1a2660ec2 | 247 | /** |
rgrover1 | 710:b2e1a2660ec2 | 248 | * Setup a callback for when an attribute has its value updated by or at the |
rgrover1 | 710:b2e1a2660ec2 | 249 | * connected peer. For a peripheral, this callback triggered when the local |
rgrover1 | 710:b2e1a2660ec2 | 250 | * GATT server has an attribute updated by a write command from the peer. |
rgrover1 | 710:b2e1a2660ec2 | 251 | * For a Central, this callback is triggered when a response is received for |
rgrover1 | 710:b2e1a2660ec2 | 252 | * a write request. |
rgrover1 | 710:b2e1a2660ec2 | 253 | * |
rgrover1 | 710:b2e1a2660ec2 | 254 | * @Note: it is possible to chain together multiple onDataWritten callbacks |
rgrover1 | 710:b2e1a2660ec2 | 255 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 256 | * to characteristics. Many services, such as DFU and UART add their own |
rgrover1 | 710:b2e1a2660ec2 | 257 | * onDataWritten callbacks behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 258 | * |
rgrover1 | 710:b2e1a2660ec2 | 259 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 260 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 261 | */ |
rgrover1 | 710:b2e1a2660ec2 | 262 | void onDataWritten(void (*callback)(const GattWriteCallbackParams *eventDataP)) {dataWrittenCallChain.add(callback);} |
rgrover1 | 710:b2e1a2660ec2 | 263 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 264 | void onDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 265 | dataWrittenCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 266 | } |
rgrover1 | 710:b2e1a2660ec2 | 267 | |
rgrover1 | 710:b2e1a2660ec2 | 268 | /** |
rgrover1 | 710:b2e1a2660ec2 | 269 | * Setup a callback to be invoked on the peripheral when an attribute is |
rgrover1 | 710:b2e1a2660ec2 | 270 | * being read by a remote client. |
rgrover1 | 710:b2e1a2660ec2 | 271 | * |
rgrover1 | 710:b2e1a2660ec2 | 272 | * @Note: this functionality may not be available on all underlying stacks. |
rgrover1 | 710:b2e1a2660ec2 | 273 | * You could use GattCharacteristic::setReadAuthorizationCallback() as an |
rgrover1 | 710:b2e1a2660ec2 | 274 | * alternative. |
rgrover1 | 710:b2e1a2660ec2 | 275 | * |
rgrover1 | 710:b2e1a2660ec2 | 276 | * @Note: it is possible to chain together multiple onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 277 | * (potentially from different modules of an application) to receive updates |
rgrover1 | 710:b2e1a2660ec2 | 278 | * to characteristics. Services may add their own onDataRead callbacks |
rgrover1 | 710:b2e1a2660ec2 | 279 | * behind the scenes to trap interesting events. |
rgrover1 | 710:b2e1a2660ec2 | 280 | * |
rgrover1 | 710:b2e1a2660ec2 | 281 | * @Note: it is also possible to setup a callback into a member function of |
rgrover1 | 710:b2e1a2660ec2 | 282 | * some object. |
rgrover1 | 710:b2e1a2660ec2 | 283 | * |
rgrover1 | 710:b2e1a2660ec2 | 284 | * @return BLE_ERROR_NOT_IMPLEMENTED if this functionality isn't available; |
rgrover1 | 710:b2e1a2660ec2 | 285 | * else BLE_ERROR_NONE. |
rgrover1 | 710:b2e1a2660ec2 | 286 | */ |
rgrover1 | 710:b2e1a2660ec2 | 287 | ble_error_t onDataRead(void (*callback)(const GattReadCallbackParams *eventDataP)) { |
rgrover1 | 710:b2e1a2660ec2 | 288 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 289 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 290 | } |
rgrover1 | 710:b2e1a2660ec2 | 291 | |
rgrover1 | 710:b2e1a2660ec2 | 292 | dataReadCallChain.add(callback); |
rgrover1 | 710:b2e1a2660ec2 | 293 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 294 | } |
rgrover1 | 710:b2e1a2660ec2 | 295 | template <typename T> |
rgrover1 | 710:b2e1a2660ec2 | 296 | ble_error_t onDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) { |
rgrover1 | 710:b2e1a2660ec2 | 297 | if (!isOnDataReadAvailable()) { |
rgrover1 | 710:b2e1a2660ec2 | 298 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 710:b2e1a2660ec2 | 299 | } |
rgrover1 | 710:b2e1a2660ec2 | 300 | |
rgrover1 | 710:b2e1a2660ec2 | 301 | dataReadCallChain.add(objPtr, memberPtr); |
rgrover1 | 710:b2e1a2660ec2 | 302 | return BLE_ERROR_NONE; |
rgrover1 | 710:b2e1a2660ec2 | 303 | } |
rgrover1 | 710:b2e1a2660ec2 | 304 | |
rgrover1 | 710:b2e1a2660ec2 | 305 | /** |
rgrover1 | 710:b2e1a2660ec2 | 306 | * Setup a callback for when notifications/indications are enabled for a |
rgrover1 | 710:b2e1a2660ec2 | 307 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 308 | */ |
rgrover1 | 710:b2e1a2660ec2 | 309 | void onUpdatesEnabled(EventCallback_t callback) {updatesEnabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 310 | |
rgrover1 | 710:b2e1a2660ec2 | 311 | /** |
rgrover1 | 710:b2e1a2660ec2 | 312 | * Setup a callback for when notifications/indications are disabled for a |
rgrover1 | 710:b2e1a2660ec2 | 313 | * characteristic on the local GattServer. |
rgrover1 | 710:b2e1a2660ec2 | 314 | */ |
rgrover1 | 710:b2e1a2660ec2 | 315 | void onUpdatesDisabled(EventCallback_t callback) {updatesDisabledCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 316 | |
rgrover1 | 710:b2e1a2660ec2 | 317 | /** |
rgrover1 | 710:b2e1a2660ec2 | 318 | * Setup a callback for when the GATT server receives a response for an |
rgrover1 | 710:b2e1a2660ec2 | 319 | * indication event sent previously. |
rgrover1 | 710:b2e1a2660ec2 | 320 | */ |
rgrover1 | 710:b2e1a2660ec2 | 321 | void onConfirmationReceived(EventCallback_t callback) {confirmationReceivedCallback = callback;} |
rgrover1 | 710:b2e1a2660ec2 | 322 | |
rgrover1 | 710:b2e1a2660ec2 | 323 | /* Entry points for the underlying stack to report events back to the user. */ |
rgrover1 | 710:b2e1a2660ec2 | 324 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 325 | void handleDataWrittenEvent(const GattWriteCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 326 | if (dataWrittenCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 327 | dataWrittenCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 328 | } |
rgrover1 | 710:b2e1a2660ec2 | 329 | } |
rgrover1 | 710:b2e1a2660ec2 | 330 | |
rgrover1 | 710:b2e1a2660ec2 | 331 | void handleDataReadEvent(const GattReadCallbackParams *params) { |
rgrover1 | 710:b2e1a2660ec2 | 332 | if (dataReadCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 333 | dataReadCallChain.call(params); |
rgrover1 | 710:b2e1a2660ec2 | 334 | } |
rgrover1 | 710:b2e1a2660ec2 | 335 | } |
rgrover1 | 710:b2e1a2660ec2 | 336 | |
rgrover1 | 728:997ba5e7b3b6 | 337 | void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t attributeHandle) { |
rgrover1 | 710:b2e1a2660ec2 | 338 | switch (type) { |
rgrover1 | 710:b2e1a2660ec2 | 339 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
rgrover1 | 710:b2e1a2660ec2 | 340 | if (updatesEnabledCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 341 | updatesEnabledCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 342 | } |
rgrover1 | 710:b2e1a2660ec2 | 343 | break; |
rgrover1 | 710:b2e1a2660ec2 | 344 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
rgrover1 | 710:b2e1a2660ec2 | 345 | if (updatesDisabledCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 346 | updatesDisabledCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 347 | } |
rgrover1 | 710:b2e1a2660ec2 | 348 | break; |
rgrover1 | 710:b2e1a2660ec2 | 349 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
rgrover1 | 710:b2e1a2660ec2 | 350 | if (confirmationReceivedCallback) { |
rgrover1 | 728:997ba5e7b3b6 | 351 | confirmationReceivedCallback(attributeHandle); |
rgrover1 | 710:b2e1a2660ec2 | 352 | } |
rgrover1 | 710:b2e1a2660ec2 | 353 | break; |
rgrover1 | 710:b2e1a2660ec2 | 354 | default: |
rgrover1 | 710:b2e1a2660ec2 | 355 | break; |
rgrover1 | 710:b2e1a2660ec2 | 356 | } |
rgrover1 | 710:b2e1a2660ec2 | 357 | } |
rgrover1 | 710:b2e1a2660ec2 | 358 | |
rgrover1 | 710:b2e1a2660ec2 | 359 | void handleDataSentEvent(unsigned count) { |
rgrover1 | 710:b2e1a2660ec2 | 360 | if (dataSentCallChain.hasCallbacksAttached()) { |
rgrover1 | 710:b2e1a2660ec2 | 361 | dataSentCallChain.call(count); |
rgrover1 | 710:b2e1a2660ec2 | 362 | } |
rgrover1 | 710:b2e1a2660ec2 | 363 | } |
rgrover1 | 710:b2e1a2660ec2 | 364 | |
rgrover1 | 710:b2e1a2660ec2 | 365 | protected: |
rgrover1 | 710:b2e1a2660ec2 | 366 | uint8_t serviceCount; |
rgrover1 | 710:b2e1a2660ec2 | 367 | uint8_t characteristicCount; |
rgrover1 | 710:b2e1a2660ec2 | 368 | |
rgrover1 | 710:b2e1a2660ec2 | 369 | private: |
rgrover1 | 710:b2e1a2660ec2 | 370 | CallChainOfFunctionPointersWithContext<unsigned> dataSentCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 371 | CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams *> dataWrittenCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 372 | CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> dataReadCallChain; |
rgrover1 | 710:b2e1a2660ec2 | 373 | EventCallback_t updatesEnabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 374 | EventCallback_t updatesDisabledCallback; |
rgrover1 | 710:b2e1a2660ec2 | 375 | EventCallback_t confirmationReceivedCallback; |
rgrover1 | 710:b2e1a2660ec2 | 376 | |
rgrover1 | 710:b2e1a2660ec2 | 377 | private: |
rgrover1 | 710:b2e1a2660ec2 | 378 | /* disallow copy and assignment */ |
rgrover1 | 710:b2e1a2660ec2 | 379 | GattServer(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 380 | GattServer& operator=(const GattServer &); |
rgrover1 | 710:b2e1a2660ec2 | 381 | }; |
rgrover1 | 710:b2e1a2660ec2 | 382 | |
rgrover1 | 710:b2e1a2660ec2 | 383 | #endif // ifndef __GATT_SERVER_H__ |