sa

Fork of nRF51822 by Nordic Semiconductor

Committer:
nakamae
Date:
Thu Dec 29 07:05:48 2016 +0000
Revision:
639:fdeb2820ef26
Parent:
638:c90ae1400bf2
new;

Who changed what in which revision?

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