hr with 30102

Dependencies:   BLE_API mbed X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
mssarwar
Date:
Sun Jul 30 05:52:57 2017 +0000
Revision:
80:808f6f367f45
with 30102;

Who changed what in which revision?

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