None

Dependencies:   nrf51-sdk

Dependents:   microbit-dal

Fork of nRF51822 by Lancaster University

Committer:
LancasterUniversity
Date:
Wed Apr 06 22:38:43 2016 +0100
Revision:
615:65ea2acfc6a2
Parent:
592:f9574772b816
Child:
616:a8f9b022d8fd
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.

Who changed what in which revision?

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