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