nordic
Fork of nRF51822 by
source/nRF5xGattClient.h@616:1751e2e2637a, 2016-01-12 (annotated)
- Committer:
- vcoubard
- Date:
- Tue Jan 12 19:58:03 2016 +0000
- Revision:
- 616:1751e2e2637a
- Parent:
- 593:f9574772b816
Synchronized with git rev 78b4ea55
Author: Vincent Coubard
Merge pull request #106 from ARMmbed/develop
merge version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 543:1bf9c597f44f | 1 | /* mbed Microcontroller Library |
vcoubard | 543:1bf9c597f44f | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 543:1bf9c597f44f | 3 | * |
vcoubard | 543:1bf9c597f44f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 543:1bf9c597f44f | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 543:1bf9c597f44f | 6 | * You may obtain a copy of the License at |
vcoubard | 543:1bf9c597f44f | 7 | * |
vcoubard | 543:1bf9c597f44f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 543:1bf9c597f44f | 9 | * |
vcoubard | 543:1bf9c597f44f | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 543:1bf9c597f44f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 543:1bf9c597f44f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 543:1bf9c597f44f | 13 | * See the License for the specific language governing permissions and |
vcoubard | 543:1bf9c597f44f | 14 | * limitations under the License. |
vcoubard | 543:1bf9c597f44f | 15 | */ |
vcoubard | 543:1bf9c597f44f | 16 | |
vcoubard | 543:1bf9c597f44f | 17 | #ifndef __NRF51822_GATT_CLIENT_H__ |
vcoubard | 543:1bf9c597f44f | 18 | #define __NRF51822_GATT_CLIENT_H__ |
vcoubard | 543:1bf9c597f44f | 19 | |
vcoubard | 543:1bf9c597f44f | 20 | #include "ble/GattClient.h" |
vcoubard | 543:1bf9c597f44f | 21 | #include "nRF5xServiceDiscovery.h" |
vcoubard | 593:f9574772b816 | 22 | #include "nRF5xCharacteristicDescriptorDiscoverer.h" |
vcoubard | 543:1bf9c597f44f | 23 | |
vcoubard | 543:1bf9c597f44f | 24 | class nRF5xGattClient : public GattClient |
vcoubard | 543:1bf9c597f44f | 25 | { |
vcoubard | 543:1bf9c597f44f | 26 | public: |
vcoubard | 543:1bf9c597f44f | 27 | /** |
vcoubard | 543:1bf9c597f44f | 28 | * When using S110, all Gatt client features will return |
vcoubard | 543:1bf9c597f44f | 29 | * BLE_ERROR_NOT_IMPLEMENTED |
vcoubard | 543:1bf9c597f44f | 30 | */ |
vcoubard | 543:1bf9c597f44f | 31 | #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
vcoubard | 543:1bf9c597f44f | 32 | |
vcoubard | 543:1bf9c597f44f | 33 | /** |
vcoubard | 543:1bf9c597f44f | 34 | * Launch service discovery. Once launched, service discovery will remain |
vcoubard | 543:1bf9c597f44f | 35 | * active with callbacks being issued back into the application for matching |
vcoubard | 543:1bf9c597f44f | 36 | * services/characteristics. isActive() can be used to determine status; and |
vcoubard | 543:1bf9c597f44f | 37 | * a termination callback (if setup) will be invoked at the end. Service |
vcoubard | 543:1bf9c597f44f | 38 | * discovery can be terminated prematurely if needed using terminate(). |
vcoubard | 543:1bf9c597f44f | 39 | * |
vcoubard | 543:1bf9c597f44f | 40 | * @param connectionHandle |
vcoubard | 543:1bf9c597f44f | 41 | * Handle for the connection with the peer. |
vcoubard | 543:1bf9c597f44f | 42 | * @param sc |
vcoubard | 543:1bf9c597f44f | 43 | * This is the application callback for matching service. Taken as |
vcoubard | 543:1bf9c597f44f | 44 | * NULL by default. Note: service discovery may still be active |
vcoubard | 543:1bf9c597f44f | 45 | * when this callback is issued; calling asynchronous BLE-stack |
vcoubard | 543:1bf9c597f44f | 46 | * APIs from within this application callback might cause the |
vcoubard | 543:1bf9c597f44f | 47 | * stack to abort service discovery. If this becomes an issue, it |
vcoubard | 543:1bf9c597f44f | 48 | * may be better to make local copy of the discoveredService and |
vcoubard | 543:1bf9c597f44f | 49 | * wait for service discovery to terminate before operating on the |
vcoubard | 543:1bf9c597f44f | 50 | * service. |
vcoubard | 543:1bf9c597f44f | 51 | * @param cc |
vcoubard | 543:1bf9c597f44f | 52 | * This is the application callback for matching characteristic. |
vcoubard | 543:1bf9c597f44f | 53 | * Taken as NULL by default. Note: service discovery may still be |
vcoubard | 543:1bf9c597f44f | 54 | * active when this callback is issued; calling asynchronous |
vcoubard | 543:1bf9c597f44f | 55 | * BLE-stack APIs from within this application callback might cause |
vcoubard | 543:1bf9c597f44f | 56 | * the stack to abort service discovery. If this becomes an issue, |
vcoubard | 543:1bf9c597f44f | 57 | * it may be better to make local copy of the discoveredCharacteristic |
vcoubard | 543:1bf9c597f44f | 58 | * and wait for service discovery to terminate before operating on the |
vcoubard | 543:1bf9c597f44f | 59 | * characteristic. |
vcoubard | 543:1bf9c597f44f | 60 | * @param matchingServiceUUID |
vcoubard | 543:1bf9c597f44f | 61 | * UUID based filter for specifying a service in which the application is |
vcoubard | 543:1bf9c597f44f | 62 | * interested. By default it is set as the wildcard UUID_UNKNOWN, |
vcoubard | 543:1bf9c597f44f | 63 | * in which case it matches all services. If characteristic-UUID |
vcoubard | 543:1bf9c597f44f | 64 | * filter (below) is set to the wildcard value, then a service |
vcoubard | 543:1bf9c597f44f | 65 | * callback will be invoked for the matching service (or for every |
vcoubard | 543:1bf9c597f44f | 66 | * service if the service filter is a wildcard). |
vcoubard | 543:1bf9c597f44f | 67 | * @param matchingCharacteristicUUIDIn |
vcoubard | 543:1bf9c597f44f | 68 | * UUID based filter for specifying characteristic in which the application |
vcoubard | 543:1bf9c597f44f | 69 | * is interested. By default it is set as the wildcard UUID_UKNOWN |
vcoubard | 543:1bf9c597f44f | 70 | * to match against any characteristic. If both service-UUID |
vcoubard | 543:1bf9c597f44f | 71 | * filter and characteristic-UUID filter are used with non- wildcard |
vcoubard | 543:1bf9c597f44f | 72 | * values, then only a single characteristic callback is |
vcoubard | 543:1bf9c597f44f | 73 | * invoked for the matching characteristic. |
vcoubard | 543:1bf9c597f44f | 74 | * |
vcoubard | 543:1bf9c597f44f | 75 | * @Note Using wildcard values for both service-UUID and characteristic- |
vcoubard | 543:1bf9c597f44f | 76 | * UUID will result in complete service discovery--callbacks being |
vcoubard | 543:1bf9c597f44f | 77 | * called for every service and characteristic. |
vcoubard | 543:1bf9c597f44f | 78 | * |
vcoubard | 543:1bf9c597f44f | 79 | * @return |
vcoubard | 543:1bf9c597f44f | 80 | * BLE_ERROR_NONE if service discovery is launched successfully; else an appropriate error. |
vcoubard | 543:1bf9c597f44f | 81 | */ |
vcoubard | 543:1bf9c597f44f | 82 | virtual ble_error_t launchServiceDiscovery(Gap::Handle_t connectionHandle, |
vcoubard | 543:1bf9c597f44f | 83 | ServiceDiscovery::ServiceCallback_t sc = NULL, |
vcoubard | 543:1bf9c597f44f | 84 | ServiceDiscovery::CharacteristicCallback_t cc = NULL, |
vcoubard | 543:1bf9c597f44f | 85 | const UUID &matchingServiceUUID = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN), |
vcoubard | 543:1bf9c597f44f | 86 | const UUID &matchingCharacteristicUUIDIn = UUID::ShortUUIDBytes_t(BLE_UUID_UNKNOWN)); |
vcoubard | 543:1bf9c597f44f | 87 | |
vcoubard | 543:1bf9c597f44f | 88 | virtual void onServiceDiscoveryTermination(ServiceDiscovery::TerminationCallback_t callback) { |
vcoubard | 593:f9574772b816 | 89 | _discovery.onTermination(callback); |
vcoubard | 543:1bf9c597f44f | 90 | } |
vcoubard | 543:1bf9c597f44f | 91 | |
vcoubard | 543:1bf9c597f44f | 92 | /** |
vcoubard | 543:1bf9c597f44f | 93 | * Is service-discovery currently active? |
vcoubard | 543:1bf9c597f44f | 94 | */ |
vcoubard | 543:1bf9c597f44f | 95 | virtual bool isServiceDiscoveryActive(void) const { |
vcoubard | 593:f9574772b816 | 96 | return _discovery.isActive(); |
vcoubard | 543:1bf9c597f44f | 97 | } |
vcoubard | 543:1bf9c597f44f | 98 | |
vcoubard | 543:1bf9c597f44f | 99 | /** |
vcoubard | 543:1bf9c597f44f | 100 | * Terminate an ongoing service-discovery. This should result in an |
vcoubard | 543:1bf9c597f44f | 101 | * invocation of the TerminationCallback if service-discovery is active. |
vcoubard | 543:1bf9c597f44f | 102 | */ |
vcoubard | 543:1bf9c597f44f | 103 | virtual void terminateServiceDiscovery(void) { |
vcoubard | 593:f9574772b816 | 104 | _discovery.terminate(); |
vcoubard | 543:1bf9c597f44f | 105 | } |
vcoubard | 543:1bf9c597f44f | 106 | |
vcoubard | 593:f9574772b816 | 107 | /** |
vcoubard | 593:f9574772b816 | 108 | * @brief Implementation of GattClient::discoverCharacteristicDescriptors |
vcoubard | 593:f9574772b816 | 109 | * @see GattClient::discoverCharacteristicDescriptors |
vcoubard | 593:f9574772b816 | 110 | */ |
vcoubard | 593:f9574772b816 | 111 | virtual ble_error_t discoverCharacteristicDescriptors( |
vcoubard | 593:f9574772b816 | 112 | const DiscoveredCharacteristic& characteristic, |
vcoubard | 593:f9574772b816 | 113 | const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& discoveryCallback, |
vcoubard | 593:f9574772b816 | 114 | const CharacteristicDescriptorDiscovery::TerminationCallback_t& terminationCallback |
vcoubard | 593:f9574772b816 | 115 | ); |
vcoubard | 593:f9574772b816 | 116 | |
vcoubard | 593:f9574772b816 | 117 | /** |
vcoubard | 593:f9574772b816 | 118 | * @brief Implementation of GattClient::isCharacteristicDiscoveryActive |
vcoubard | 593:f9574772b816 | 119 | * @see GattClient::isCharacteristicDiscoveryActive |
vcoubard | 593:f9574772b816 | 120 | */ |
vcoubard | 593:f9574772b816 | 121 | virtual bool isCharacteristicDescriptorsDiscoveryActive(const DiscoveredCharacteristic& characteristic) const; |
vcoubard | 593:f9574772b816 | 122 | |
vcoubard | 593:f9574772b816 | 123 | /** |
vcoubard | 593:f9574772b816 | 124 | * @brief Implementation of GattClient::terminateCharacteristicDiscovery |
vcoubard | 593:f9574772b816 | 125 | * @see GattClient::terminateCharacteristicDiscovery |
vcoubard | 593:f9574772b816 | 126 | */ |
vcoubard | 593:f9574772b816 | 127 | virtual void terminateCharacteristicDescriptorsDiscovery(const DiscoveredCharacteristic& characteristic); |
vcoubard | 593:f9574772b816 | 128 | |
vcoubard | 543:1bf9c597f44f | 129 | virtual ble_error_t read(Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, uint16_t offset) const { |
vcoubard | 543:1bf9c597f44f | 130 | uint32_t rc = sd_ble_gattc_read(connHandle, attributeHandle, offset); |
vcoubard | 543:1bf9c597f44f | 131 | if (rc == NRF_SUCCESS) { |
vcoubard | 543:1bf9c597f44f | 132 | return BLE_ERROR_NONE; |
vcoubard | 543:1bf9c597f44f | 133 | } |
vcoubard | 543:1bf9c597f44f | 134 | switch (rc) { |
vcoubard | 543:1bf9c597f44f | 135 | case NRF_ERROR_BUSY: |
vcoubard | 543:1bf9c597f44f | 136 | return BLE_STACK_BUSY; |
vcoubard | 543:1bf9c597f44f | 137 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 543:1bf9c597f44f | 138 | case NRF_ERROR_INVALID_STATE: |
vcoubard | 543:1bf9c597f44f | 139 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 543:1bf9c597f44f | 140 | default: |
vcoubard | 543:1bf9c597f44f | 141 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 543:1bf9c597f44f | 142 | } |
vcoubard | 543:1bf9c597f44f | 143 | } |
vcoubard | 543:1bf9c597f44f | 144 | |
vcoubard | 543:1bf9c597f44f | 145 | virtual ble_error_t write(GattClient::WriteOp_t cmd, Gap::Handle_t connHandle, GattAttribute::Handle_t attributeHandle, size_t length, const uint8_t *value) const { |
vcoubard | 543:1bf9c597f44f | 146 | ble_gattc_write_params_t writeParams; |
vcoubard | 543:1bf9c597f44f | 147 | writeParams.write_op = cmd; |
vcoubard | 543:1bf9c597f44f | 148 | writeParams.flags = 0; /* this is inconsequential */ |
vcoubard | 543:1bf9c597f44f | 149 | writeParams.handle = attributeHandle; |
vcoubard | 543:1bf9c597f44f | 150 | writeParams.offset = 0; |
vcoubard | 543:1bf9c597f44f | 151 | writeParams.len = length; |
vcoubard | 543:1bf9c597f44f | 152 | writeParams.p_value = const_cast<uint8_t *>(value); |
vcoubard | 543:1bf9c597f44f | 153 | |
vcoubard | 543:1bf9c597f44f | 154 | uint32_t rc = sd_ble_gattc_write(connHandle, &writeParams); |
vcoubard | 543:1bf9c597f44f | 155 | if (rc == NRF_SUCCESS) { |
vcoubard | 543:1bf9c597f44f | 156 | return BLE_ERROR_NONE; |
vcoubard | 543:1bf9c597f44f | 157 | } |
vcoubard | 543:1bf9c597f44f | 158 | switch (rc) { |
vcoubard | 543:1bf9c597f44f | 159 | case NRF_ERROR_BUSY: |
vcoubard | 543:1bf9c597f44f | 160 | return BLE_STACK_BUSY; |
vcoubard | 543:1bf9c597f44f | 161 | case BLE_ERROR_NO_TX_BUFFERS: |
vcoubard | 543:1bf9c597f44f | 162 | return BLE_ERROR_NO_MEM; |
vcoubard | 543:1bf9c597f44f | 163 | case BLE_ERROR_INVALID_CONN_HANDLE: |
vcoubard | 543:1bf9c597f44f | 164 | case NRF_ERROR_INVALID_STATE: |
vcoubard | 543:1bf9c597f44f | 165 | case NRF_ERROR_INVALID_ADDR: |
vcoubard | 543:1bf9c597f44f | 166 | default: |
vcoubard | 543:1bf9c597f44f | 167 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 543:1bf9c597f44f | 168 | } |
vcoubard | 543:1bf9c597f44f | 169 | } |
vcoubard | 543:1bf9c597f44f | 170 | |
vcoubard | 576:7023a8204a1b | 171 | /** |
vcoubard | 576:7023a8204a1b | 172 | * @brief Clear nRF5xGattClient's state. |
vcoubard | 576:7023a8204a1b | 173 | * |
vcoubard | 576:7023a8204a1b | 174 | * @return |
vcoubard | 576:7023a8204a1b | 175 | * BLE_ERROR_NONE if successful. |
vcoubard | 576:7023a8204a1b | 176 | */ |
vcoubard | 576:7023a8204a1b | 177 | virtual ble_error_t reset(void) { |
vcoubard | 576:7023a8204a1b | 178 | /* Clear all state that is from the parent, including private members */ |
vcoubard | 576:7023a8204a1b | 179 | if (GattClient::reset() != BLE_ERROR_NONE) { |
vcoubard | 576:7023a8204a1b | 180 | return BLE_ERROR_INVALID_STATE; |
vcoubard | 576:7023a8204a1b | 181 | } |
vcoubard | 576:7023a8204a1b | 182 | |
vcoubard | 576:7023a8204a1b | 183 | /* Clear derived class members */ |
vcoubard | 593:f9574772b816 | 184 | _discovery.reset(); |
vcoubard | 576:7023a8204a1b | 185 | |
vcoubard | 576:7023a8204a1b | 186 | return BLE_ERROR_NONE; |
vcoubard | 576:7023a8204a1b | 187 | } |
vcoubard | 576:7023a8204a1b | 188 | |
vcoubard | 543:1bf9c597f44f | 189 | public: |
vcoubard | 576:7023a8204a1b | 190 | /* |
vcoubard | 576:7023a8204a1b | 191 | * Allow instantiation from nRF5xn when required. |
vcoubard | 576:7023a8204a1b | 192 | */ |
vcoubard | 576:7023a8204a1b | 193 | friend class nRF5xn; |
vcoubard | 576:7023a8204a1b | 194 | |
vcoubard | 593:f9574772b816 | 195 | nRF5xGattClient() : _discovery(this) { |
vcoubard | 543:1bf9c597f44f | 196 | /* empty */ |
vcoubard | 543:1bf9c597f44f | 197 | } |
vcoubard | 543:1bf9c597f44f | 198 | |
vcoubard | 593:f9574772b816 | 199 | nRF5xServiceDiscovery& discovery() { |
vcoubard | 593:f9574772b816 | 200 | return _discovery; |
vcoubard | 593:f9574772b816 | 201 | } |
vcoubard | 593:f9574772b816 | 202 | |
vcoubard | 593:f9574772b816 | 203 | nRF5xCharacteristicDescriptorDiscoverer& characteristicDescriptorDiscoverer() { |
vcoubard | 593:f9574772b816 | 204 | return _characteristicDescriptorDiscoverer; |
vcoubard | 593:f9574772b816 | 205 | } |
vcoubard | 543:1bf9c597f44f | 206 | |
vcoubard | 543:1bf9c597f44f | 207 | private: |
vcoubard | 543:1bf9c597f44f | 208 | nRF5xGattClient(const nRF5xGattClient &); |
vcoubard | 543:1bf9c597f44f | 209 | const nRF5xGattClient& operator=(const nRF5xGattClient &); |
vcoubard | 543:1bf9c597f44f | 210 | |
vcoubard | 543:1bf9c597f44f | 211 | private: |
vcoubard | 593:f9574772b816 | 212 | nRF5xServiceDiscovery _discovery; |
vcoubard | 593:f9574772b816 | 213 | nRF5xCharacteristicDescriptorDiscoverer _characteristicDescriptorDiscoverer; |
vcoubard | 543:1bf9c597f44f | 214 | |
vcoubard | 543:1bf9c597f44f | 215 | #endif // if !S110 |
vcoubard | 543:1bf9c597f44f | 216 | }; |
vcoubard | 543:1bf9c597f44f | 217 | |
rgrover1 | 389:db85a09c27ef | 218 | #endif // ifndef __NRF51822_GATT_CLIENT_H__ |