Updated
Fork of BLE_API by
public/GattClient.h@511:9f5046c0f741, 2015-06-19 (annotated)
- Committer:
- rgrover1
- Date:
- Fri Jun 19 15:52:04 2015 +0100
- Revision:
- 511:9f5046c0f741
- Parent:
- 510:39612adfbf68
- Child:
- 512:0584e40bece2
Synchronized with git rev 5f0668ee
Author: Rohit Grover
rename GattCharacteristicCallbackParams.h -> GattCallbackParamTypes.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 490:58a0313fffb5 | 1 | /* mbed Microcontroller Library |
rgrover1 | 490:58a0313fffb5 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 490:58a0313fffb5 | 3 | * |
rgrover1 | 490:58a0313fffb5 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 490:58a0313fffb5 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 490:58a0313fffb5 | 6 | * You may obtain a copy of the License at |
rgrover1 | 490:58a0313fffb5 | 7 | * |
rgrover1 | 490:58a0313fffb5 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 490:58a0313fffb5 | 9 | * |
rgrover1 | 490:58a0313fffb5 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 490:58a0313fffb5 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 490:58a0313fffb5 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 490:58a0313fffb5 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 490:58a0313fffb5 | 14 | * limitations under the License. |
rgrover1 | 490:58a0313fffb5 | 15 | */ |
rgrover1 | 490:58a0313fffb5 | 16 | |
rgrover1 | 490:58a0313fffb5 | 17 | #ifndef __GATT_CLIENT_H__ |
rgrover1 | 490:58a0313fffb5 | 18 | #define __GATT_CLIENT_H__ |
rgrover1 | 490:58a0313fffb5 | 19 | |
rgrover1 | 495:b0d77b1db2df | 20 | #include "Gap.h" |
rgrover1 | 495:b0d77b1db2df | 21 | #include "GattAttribute.h" |
rgrover1 | 497:926d444599e8 | 22 | #include "ServiceDiscovery.h" |
rgrover1 | 495:b0d77b1db2df | 23 | |
rgrover1 | 511:9f5046c0f741 | 24 | #include "GattCallbackParamTypes.h" |
rgrover1 | 508:31e63ad6923f | 25 | |
rgrover1 | 490:58a0313fffb5 | 26 | class GattClient { |
rgrover1 | 495:b0d77b1db2df | 27 | public: |
rgrover1 | 510:39612adfbf68 | 28 | typedef void (*ReadCallback_t)(const GattReadCallbackParams *params); |
rgrover1 | 501:ff6801633d2c | 29 | |
rgrover1 | 504:817c6240e9d9 | 30 | enum WriteOp_t { |
rgrover1 | 504:817c6240e9d9 | 31 | GATT_OP_INVALID = 0x00, /**< Invalid Operation. */ |
rgrover1 | 504:817c6240e9d9 | 32 | GATT_OP_WRITE_REQ = 0x01, /**< Write Request. */ |
rgrover1 | 504:817c6240e9d9 | 33 | GATT_OP_WRITE_CMD = 0x02, /**< Write Command. */ |
rgrover1 | 504:817c6240e9d9 | 34 | GATT_OP_SIGN_WRITE_CMD = 0x03, /**< Signed Write Command. */ |
rgrover1 | 504:817c6240e9d9 | 35 | GATT_OP_PREP_WRITE_REQ = 0x04, /**< Prepare Write Request. */ |
rgrover1 | 504:817c6240e9d9 | 36 | GATT_OP_EXEC_WRITE_REQ = 0x05, /**< Execute Write Request. */ |
rgrover1 | 504:817c6240e9d9 | 37 | }; |
rgrover1 | 504:817c6240e9d9 | 38 | |
rgrover1 | 510:39612adfbf68 | 39 | typedef void (*WriteCallback_t)(const GattWriteCallbackParams *params); |
rgrover1 | 504:817c6240e9d9 | 40 | |
rgrover1 | 497:926d444599e8 | 41 | public: |
rgrover1 | 497:926d444599e8 | 42 | /** |
rgrover1 | 497:926d444599e8 | 43 | * Launch service discovery. Once launched, service discovery will remain |
rgrover1 | 497:926d444599e8 | 44 | * active with callbacks being issued back into the application for matching |
rgrover1 | 497:926d444599e8 | 45 | * services/characteristics. isActive() can be used to determine status; and |
rgrover1 | 497:926d444599e8 | 46 | * a termination callback (if setup) will be invoked at the end. Service |
rgrover1 | 497:926d444599e8 | 47 | * discovery can be terminated prematurely if needed using terminate(). |
rgrover1 | 497:926d444599e8 | 48 | * |
rgrover1 | 497:926d444599e8 | 49 | * @param connectionHandle |
rgrover1 | 497:926d444599e8 | 50 | * Handle for the connection with the peer. |
rgrover1 | 497:926d444599e8 | 51 | * @param sc |
rgrover1 | 497:926d444599e8 | 52 | * This is the application callback for matching service. |
rgrover1 | 497:926d444599e8 | 53 | * @param cc |
rgrover1 | 497:926d444599e8 | 54 | * This is the application callback for matching characteristic. |
rgrover1 | 497:926d444599e8 | 55 | * @param matchingServiceUUID |
rgrover1 | 497:926d444599e8 | 56 | * UUID based filter for specifying a service in which the application is |
rgrover1 | 497:926d444599e8 | 57 | * interested. |
rgrover1 | 497:926d444599e8 | 58 | * @param matchingCharacteristicUUIDIn |
rgrover1 | 497:926d444599e8 | 59 | * UUID based filter for specifying characteristic in which the application |
rgrover1 | 497:926d444599e8 | 60 | * is interested. |
rgrover1 | 497:926d444599e8 | 61 | * |
rgrover1 | 497:926d444599e8 | 62 | * @Note Using wildcard values for both service-UUID and characteristic- |
rgrover1 | 497:926d444599e8 | 63 | * UUID will result in complete service discovery--callbacks being |
rgrover1 | 497:926d444599e8 | 64 | * called for every service and characteristic. |
rgrover1 | 497:926d444599e8 | 65 | * |
rgrover1 | 497:926d444599e8 | 66 | * @return |
rgrover1 | 497:926d444599e8 | 67 | * BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error. |
rgrover1 | 497:926d444599e8 | 68 | */ |
rgrover1 | 497:926d444599e8 | 69 | virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle, |
rgrover1 | 497:926d444599e8 | 70 | ServiceDiscovery::ServiceCallback_t sc = NULL, |
rgrover1 | 497:926d444599e8 | 71 | ServiceDiscovery::CharacteristicCallback_t cc = NULL, |
rgrover1 | 497:926d444599e8 | 72 | const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN), |
rgrover1 | 497:926d444599e8 | 73 | const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)) = 0; |
rgrover1 | 497:926d444599e8 | 74 | |
rgrover1 | 497:926d444599e8 | 75 | virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) = 0; |
rgrover1 | 497:926d444599e8 | 76 | |
rgrover1 | 497:926d444599e8 | 77 | /** |
rgrover1 | 497:926d444599e8 | 78 | * Is service-discovery currently active? |
rgrover1 | 497:926d444599e8 | 79 | */ |
rgrover1 | 497:926d444599e8 | 80 | virtual bool isServiceDiscoveryActive(void) const = 0; |
rgrover1 | 497:926d444599e8 | 81 | |
rgrover1 | 497:926d444599e8 | 82 | /** |
rgrover1 | 497:926d444599e8 | 83 | * Terminate an ongoing service-discovery. This should result in an |
rgrover1 | 497:926d444599e8 | 84 | * invocation of the TerminationCallback if service-discovery is active. |
rgrover1 | 497:926d444599e8 | 85 | */ |
rgrover1 | 497:926d444599e8 | 86 | virtual void terminateServiceDiscovery(void) = 0; |
rgrover1 | 497:926d444599e8 | 87 | |
rgrover1 | 495:b0d77b1db2df | 88 | /* Initiate a Gatt Client read procedure by attribute-handle.*/ |
rgrover1 | 495:b0d77b1db2df | 89 | virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const = 0; |
rgrover1 | 495:b0d77b1db2df | 90 | |
rgrover1 | 504:817c6240e9d9 | 91 | virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, size_t length, const uint8_t *value) const = 0; |
rgrover1 | 497:926d444599e8 | 92 | |
rgrover1 | 490:58a0313fffb5 | 93 | #if 0 |
rgrover1 | 490:58a0313fffb5 | 94 | public: |
rgrover1 | 490:58a0313fffb5 | 95 | /* Event callback handlers. */ |
rgrover1 | 490:58a0313fffb5 | 96 | typedef void (*EventCallback_t)(GattAttribute::Handle_t attributeHandle); |
rgrover1 | 490:58a0313fffb5 | 97 | typedef void (*ServerEventCallback_t)(void); /**< independent of any particular attribute */ |
rgrover1 | 490:58a0313fffb5 | 98 | |
rgrover1 | 490:58a0313fffb5 | 99 | friend class BLEDevice; |
rgrover1 | 490:58a0313fffb5 | 100 | private: |
rgrover1 | 490:58a0313fffb5 | 101 | /* These functions must be defined in the sub-class */ |
rgrover1 | 490:58a0313fffb5 | 102 | virtual ble_error_t addService(GattService &) = 0; |
rgrover1 | 490:58a0313fffb5 | 103 | virtual ble_error_t readValue(GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0; |
rgrover1 | 490:58a0313fffb5 | 104 | virtual ble_error_t readValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t attributeHandle, uint8_t buffer[], uint16_t *lengthP) = 0; |
rgrover1 | 490:58a0313fffb5 | 105 | virtual ble_error_t updateValue(GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0; |
rgrover1 | 490:58a0313fffb5 | 106 | virtual ble_error_t updateValue(Gap::Handle_t connectionHandle, GattAttribute::Handle_t, const uint8_t[], uint16_t, bool localOnly = false) = 0; |
rgrover1 | 490:58a0313fffb5 | 107 | virtual ble_error_t initializeGATTDatabase(void) = 0; |
rgrover1 | 490:58a0313fffb5 | 108 | |
rgrover1 | 490:58a0313fffb5 | 109 | // ToDo: For updateValue, check the CCCD to see if the value we are |
rgrover1 | 490:58a0313fffb5 | 110 | // updating has the notify or indicate bits sent, and if BOTH are set |
rgrover1 | 490:58a0313fffb5 | 111 | // be sure to call sd_ble_gatts_hvx() twice with notify then indicate! |
rgrover1 | 490:58a0313fffb5 | 112 | // Strange use case, but valid and must be covered! |
rgrover1 | 490:58a0313fffb5 | 113 | |
rgrover1 | 490:58a0313fffb5 | 114 | void setOnDataSent(void (*callback)(unsigned count)) {onDataSent.add(callback);} |
rgrover1 | 490:58a0313fffb5 | 115 | template <typename T> |
rgrover1 | 490:58a0313fffb5 | 116 | void setOnDataSent(T *objPtr, void (T::*memberPtr)(unsigned count)) { |
rgrover1 | 490:58a0313fffb5 | 117 | onDataSent.add(objPtr, memberPtr); |
rgrover1 | 490:58a0313fffb5 | 118 | } |
rgrover1 | 510:39612adfbf68 | 119 | void setOnDataWritten(void (*callback)(const GattWriteCallbackParams *eventDataP)) {onDataWritten.add(callback);} |
rgrover1 | 490:58a0313fffb5 | 120 | template <typename T> |
rgrover1 | 510:39612adfbf68 | 121 | void setOnDataWritten(T *objPtr, void (T::*memberPtr)(const GattWriteCallbackParams *context)) { |
rgrover1 | 490:58a0313fffb5 | 122 | onDataWritten.add(objPtr, memberPtr); |
rgrover1 | 490:58a0313fffb5 | 123 | } |
rgrover1 | 490:58a0313fffb5 | 124 | |
rgrover1 | 490:58a0313fffb5 | 125 | /** |
rgrover1 | 490:58a0313fffb5 | 126 | * A virtual function to allow underlying stacks to indicate if they support |
rgrover1 | 490:58a0313fffb5 | 127 | * onDataRead(). It should be overridden to return true as applicable. |
rgrover1 | 490:58a0313fffb5 | 128 | */ |
rgrover1 | 490:58a0313fffb5 | 129 | virtual bool isOnDataReadAvailable() const { |
rgrover1 | 490:58a0313fffb5 | 130 | return false; |
rgrover1 | 490:58a0313fffb5 | 131 | } |
rgrover1 | 510:39612adfbf68 | 132 | ble_error_t setOnDataRead(void (*callback)(const GattReadCallbackParams *eventDataP)) { |
rgrover1 | 490:58a0313fffb5 | 133 | if (!isOnDataReadAvailable()) { |
rgrover1 | 490:58a0313fffb5 | 134 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 490:58a0313fffb5 | 135 | } |
rgrover1 | 490:58a0313fffb5 | 136 | |
rgrover1 | 490:58a0313fffb5 | 137 | onDataRead.add(callback); |
rgrover1 | 490:58a0313fffb5 | 138 | return BLE_ERROR_NONE; |
rgrover1 | 490:58a0313fffb5 | 139 | } |
rgrover1 | 490:58a0313fffb5 | 140 | template <typename T> |
rgrover1 | 510:39612adfbf68 | 141 | ble_error_t setOnDataRead(T *objPtr, void (T::*memberPtr)(const GattReadCallbackParams *context)) { |
rgrover1 | 490:58a0313fffb5 | 142 | if (!isOnDataReadAvailable()) { |
rgrover1 | 490:58a0313fffb5 | 143 | return BLE_ERROR_NOT_IMPLEMENTED; |
rgrover1 | 490:58a0313fffb5 | 144 | } |
rgrover1 | 490:58a0313fffb5 | 145 | |
rgrover1 | 490:58a0313fffb5 | 146 | onDataRead.add(objPtr, memberPtr); |
rgrover1 | 490:58a0313fffb5 | 147 | return BLE_ERROR_NONE; |
rgrover1 | 490:58a0313fffb5 | 148 | } |
rgrover1 | 490:58a0313fffb5 | 149 | void setOnUpdatesEnabled(EventCallback_t callback) {onUpdatesEnabled = callback;} |
rgrover1 | 490:58a0313fffb5 | 150 | void setOnUpdatesDisabled(EventCallback_t callback) {onUpdatesDisabled = callback;} |
rgrover1 | 490:58a0313fffb5 | 151 | void setOnConfirmationReceived(EventCallback_t callback) {onConfirmationReceived = callback;} |
rgrover1 | 490:58a0313fffb5 | 152 | |
rgrover1 | 490:58a0313fffb5 | 153 | protected: |
rgrover1 | 510:39612adfbf68 | 154 | void handleDataWrittenEvent(const GattWriteCallbackParams *params) { |
rgrover1 | 490:58a0313fffb5 | 155 | if (onDataWritten.hasCallbacksAttached()) { |
rgrover1 | 490:58a0313fffb5 | 156 | onDataWritten.call(params); |
rgrover1 | 490:58a0313fffb5 | 157 | } |
rgrover1 | 490:58a0313fffb5 | 158 | } |
rgrover1 | 490:58a0313fffb5 | 159 | |
rgrover1 | 510:39612adfbf68 | 160 | void handleDataReadEvent(const GattReadCallbackParams *params) { |
rgrover1 | 490:58a0313fffb5 | 161 | if (onDataRead.hasCallbacksAttached()) { |
rgrover1 | 490:58a0313fffb5 | 162 | onDataRead.call(params); |
rgrover1 | 490:58a0313fffb5 | 163 | } |
rgrover1 | 490:58a0313fffb5 | 164 | } |
rgrover1 | 490:58a0313fffb5 | 165 | |
rgrover1 | 490:58a0313fffb5 | 166 | void handleEvent(GattServerEvents::gattEvent_e type, GattAttribute::Handle_t charHandle) { |
rgrover1 | 490:58a0313fffb5 | 167 | switch (type) { |
rgrover1 | 490:58a0313fffb5 | 168 | case GattServerEvents::GATT_EVENT_UPDATES_ENABLED: |
rgrover1 | 490:58a0313fffb5 | 169 | if (onUpdatesEnabled) { |
rgrover1 | 490:58a0313fffb5 | 170 | onUpdatesEnabled(charHandle); |
rgrover1 | 490:58a0313fffb5 | 171 | } |
rgrover1 | 490:58a0313fffb5 | 172 | break; |
rgrover1 | 490:58a0313fffb5 | 173 | case GattServerEvents::GATT_EVENT_UPDATES_DISABLED: |
rgrover1 | 490:58a0313fffb5 | 174 | if (onUpdatesDisabled) { |
rgrover1 | 490:58a0313fffb5 | 175 | onUpdatesDisabled(charHandle); |
rgrover1 | 490:58a0313fffb5 | 176 | } |
rgrover1 | 490:58a0313fffb5 | 177 | break; |
rgrover1 | 490:58a0313fffb5 | 178 | case GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED: |
rgrover1 | 490:58a0313fffb5 | 179 | if (onConfirmationReceived) { |
rgrover1 | 490:58a0313fffb5 | 180 | onConfirmationReceived(charHandle); |
rgrover1 | 490:58a0313fffb5 | 181 | } |
rgrover1 | 490:58a0313fffb5 | 182 | break; |
rgrover1 | 490:58a0313fffb5 | 183 | default: |
rgrover1 | 490:58a0313fffb5 | 184 | break; |
rgrover1 | 490:58a0313fffb5 | 185 | } |
rgrover1 | 490:58a0313fffb5 | 186 | } |
rgrover1 | 490:58a0313fffb5 | 187 | |
rgrover1 | 490:58a0313fffb5 | 188 | void handleDataSentEvent(unsigned count) { |
rgrover1 | 490:58a0313fffb5 | 189 | if (onDataSent.hasCallbacksAttached()) { |
rgrover1 | 490:58a0313fffb5 | 190 | onDataSent.call(count); |
rgrover1 | 490:58a0313fffb5 | 191 | } |
rgrover1 | 490:58a0313fffb5 | 192 | } |
rgrover1 | 490:58a0313fffb5 | 193 | |
rgrover1 | 490:58a0313fffb5 | 194 | protected: |
rgrover1 | 490:58a0313fffb5 | 195 | uint8_t serviceCount; |
rgrover1 | 490:58a0313fffb5 | 196 | uint8_t characteristicCount; |
rgrover1 | 490:58a0313fffb5 | 197 | |
rgrover1 | 490:58a0313fffb5 | 198 | private: |
rgrover1 | 490:58a0313fffb5 | 199 | CallChainOfFunctionPointersWithContext<unsigned> onDataSent; |
rgrover1 | 510:39612adfbf68 | 200 | CallChainOfFunctionPointersWithContext<const GattWriteCallbackParams *> onDataWritten; |
rgrover1 | 510:39612adfbf68 | 201 | CallChainOfFunctionPointersWithContext<const GattReadCallbackParams *> onDataRead; |
rgrover1 | 490:58a0313fffb5 | 202 | EventCallback_t onUpdatesEnabled; |
rgrover1 | 490:58a0313fffb5 | 203 | EventCallback_t onUpdatesDisabled; |
rgrover1 | 490:58a0313fffb5 | 204 | EventCallback_t onConfirmationReceived; |
rgrover1 | 490:58a0313fffb5 | 205 | #endif |
rgrover1 | 490:58a0313fffb5 | 206 | |
rgrover1 | 490:58a0313fffb5 | 207 | protected: |
rgrover1 | 490:58a0313fffb5 | 208 | GattClient() { |
rgrover1 | 490:58a0313fffb5 | 209 | /* empty */ |
rgrover1 | 490:58a0313fffb5 | 210 | } |
rgrover1 | 490:58a0313fffb5 | 211 | |
rgrover1 | 490:58a0313fffb5 | 212 | private: |
rgrover1 | 490:58a0313fffb5 | 213 | /* disallow copy and assignment */ |
rgrover1 | 490:58a0313fffb5 | 214 | GattClient(const GattClient &); |
rgrover1 | 490:58a0313fffb5 | 215 | GattClient& operator=(const GattClient &); |
rgrover1 | 490:58a0313fffb5 | 216 | }; |
rgrover1 | 490:58a0313fffb5 | 217 | |
rgrover1 | 490:58a0313fffb5 | 218 | #endif // ifndef __GATT_CLIENT_H__ |