nordic

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Mon Nov 02 09:05:10 2015 +0000
Revision:
454:38a41862ab4c
Parent:
450:fd09f590751b
Child:
456:e33de7c4574c
Synchronized with git rev a9f33e16
Author: Bogdan Marinescu
Updated dependencies

Who changed what in which revision?

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