Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16
bryantaylor 0:eafc3fd41f75 17 #include "ble/DiscoveredCharacteristic.h"
bryantaylor 0:eafc3fd41f75 18 #include "ble/GattClient.h"
bryantaylor 0:eafc3fd41f75 19
bryantaylor 0:eafc3fd41f75 20 ble_error_t
bryantaylor 0:eafc3fd41f75 21 DiscoveredCharacteristic::read(uint16_t offset) const
bryantaylor 0:eafc3fd41f75 22 {
bryantaylor 0:eafc3fd41f75 23 if (!props.read()) {
bryantaylor 0:eafc3fd41f75 24 return BLE_ERROR_OPERATION_NOT_PERMITTED;
bryantaylor 0:eafc3fd41f75 25 }
bryantaylor 0:eafc3fd41f75 26
bryantaylor 0:eafc3fd41f75 27 if (!gattc) {
bryantaylor 0:eafc3fd41f75 28 return BLE_ERROR_INVALID_STATE;
bryantaylor 0:eafc3fd41f75 29 }
bryantaylor 0:eafc3fd41f75 30
bryantaylor 0:eafc3fd41f75 31 return gattc->read(connHandle, valueHandle, offset);
bryantaylor 0:eafc3fd41f75 32 }
bryantaylor 0:eafc3fd41f75 33
bryantaylor 0:eafc3fd41f75 34 struct OneShotReadCallback {
bryantaylor 0:eafc3fd41f75 35 static void launch(GattClient* client, Gap::Handle_t connHandle,
bryantaylor 0:eafc3fd41f75 36 GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) {
bryantaylor 0:eafc3fd41f75 37 OneShotReadCallback* oneShot = new OneShotReadCallback(client, connHandle, handle, cb);
bryantaylor 0:eafc3fd41f75 38 oneShot->attach();
bryantaylor 0:eafc3fd41f75 39 // delete will be made when this callback is called
bryantaylor 0:eafc3fd41f75 40 }
bryantaylor 0:eafc3fd41f75 41
bryantaylor 0:eafc3fd41f75 42 private:
bryantaylor 0:eafc3fd41f75 43 OneShotReadCallback(GattClient* client, Gap::Handle_t connHandle,
bryantaylor 0:eafc3fd41f75 44 GattAttribute::Handle_t handle, const GattClient::ReadCallback_t& cb) :
bryantaylor 0:eafc3fd41f75 45 _client(client),
bryantaylor 0:eafc3fd41f75 46 _connHandle(connHandle),
bryantaylor 0:eafc3fd41f75 47 _handle(handle),
bryantaylor 0:eafc3fd41f75 48 _callback(cb) { }
bryantaylor 0:eafc3fd41f75 49
bryantaylor 0:eafc3fd41f75 50 void attach() {
bryantaylor 0:eafc3fd41f75 51 _client->onDataRead(makeFunctionPointer(this, &OneShotReadCallback::call));
bryantaylor 0:eafc3fd41f75 52 }
bryantaylor 0:eafc3fd41f75 53
bryantaylor 0:eafc3fd41f75 54 void call(const GattReadCallbackParams* params) {
bryantaylor 0:eafc3fd41f75 55 // verifiy that it is the right characteristic on the right connection
bryantaylor 0:eafc3fd41f75 56 if (params->connHandle == _connHandle && params->handle == _handle) {
bryantaylor 0:eafc3fd41f75 57 _callback(params);
bryantaylor 0:eafc3fd41f75 58 _client->onDataRead().detach(makeFunctionPointer(this, &OneShotReadCallback::call));
bryantaylor 0:eafc3fd41f75 59 delete this;
bryantaylor 0:eafc3fd41f75 60 }
bryantaylor 0:eafc3fd41f75 61 }
bryantaylor 0:eafc3fd41f75 62
bryantaylor 0:eafc3fd41f75 63 GattClient* _client;
bryantaylor 0:eafc3fd41f75 64 Gap::Handle_t _connHandle;
bryantaylor 0:eafc3fd41f75 65 GattAttribute::Handle_t _handle;
bryantaylor 0:eafc3fd41f75 66 GattClient::ReadCallback_t _callback;
bryantaylor 0:eafc3fd41f75 67 };
bryantaylor 0:eafc3fd41f75 68
bryantaylor 0:eafc3fd41f75 69 ble_error_t DiscoveredCharacteristic::read(uint16_t offset, const GattClient::ReadCallback_t& onRead) const {
bryantaylor 0:eafc3fd41f75 70 ble_error_t error = read(offset);
bryantaylor 0:eafc3fd41f75 71 if (error) {
bryantaylor 0:eafc3fd41f75 72 return error;
bryantaylor 0:eafc3fd41f75 73 }
bryantaylor 0:eafc3fd41f75 74
bryantaylor 0:eafc3fd41f75 75 OneShotReadCallback::launch(gattc, connHandle, valueHandle, onRead);
bryantaylor 0:eafc3fd41f75 76
bryantaylor 0:eafc3fd41f75 77 return error;
bryantaylor 0:eafc3fd41f75 78 }
bryantaylor 0:eafc3fd41f75 79
bryantaylor 0:eafc3fd41f75 80 ble_error_t
bryantaylor 0:eafc3fd41f75 81 DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value) const
bryantaylor 0:eafc3fd41f75 82 {
bryantaylor 0:eafc3fd41f75 83 if (!props.write()) {
bryantaylor 0:eafc3fd41f75 84 return BLE_ERROR_OPERATION_NOT_PERMITTED;
bryantaylor 0:eafc3fd41f75 85 }
bryantaylor 0:eafc3fd41f75 86
bryantaylor 0:eafc3fd41f75 87 if (!gattc) {
bryantaylor 0:eafc3fd41f75 88 return BLE_ERROR_INVALID_STATE;
bryantaylor 0:eafc3fd41f75 89 }
bryantaylor 0:eafc3fd41f75 90
bryantaylor 0:eafc3fd41f75 91 return gattc->write(GattClient::GATT_OP_WRITE_REQ, connHandle, valueHandle, length, value);
bryantaylor 0:eafc3fd41f75 92 }
bryantaylor 0:eafc3fd41f75 93
bryantaylor 0:eafc3fd41f75 94 ble_error_t
bryantaylor 0:eafc3fd41f75 95 DiscoveredCharacteristic::writeWoResponse(uint16_t length, const uint8_t *value) const
bryantaylor 0:eafc3fd41f75 96 {
bryantaylor 0:eafc3fd41f75 97 if (!props.writeWoResp()) {
bryantaylor 0:eafc3fd41f75 98 return BLE_ERROR_OPERATION_NOT_PERMITTED;
bryantaylor 0:eafc3fd41f75 99 }
bryantaylor 0:eafc3fd41f75 100
bryantaylor 0:eafc3fd41f75 101 if (!gattc) {
bryantaylor 0:eafc3fd41f75 102 return BLE_ERROR_INVALID_STATE;
bryantaylor 0:eafc3fd41f75 103 }
bryantaylor 0:eafc3fd41f75 104
bryantaylor 0:eafc3fd41f75 105 return gattc->write(GattClient::GATT_OP_WRITE_CMD, connHandle, valueHandle, length, value);
bryantaylor 0:eafc3fd41f75 106 }
bryantaylor 0:eafc3fd41f75 107
bryantaylor 0:eafc3fd41f75 108 struct OneShotWriteCallback {
bryantaylor 0:eafc3fd41f75 109 static void launch(GattClient* client, Gap::Handle_t connHandle,
bryantaylor 0:eafc3fd41f75 110 GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) {
bryantaylor 0:eafc3fd41f75 111 OneShotWriteCallback* oneShot = new OneShotWriteCallback(client, connHandle, handle, cb);
bryantaylor 0:eafc3fd41f75 112 oneShot->attach();
bryantaylor 0:eafc3fd41f75 113 // delete will be made when this callback is called
bryantaylor 0:eafc3fd41f75 114 }
bryantaylor 0:eafc3fd41f75 115
bryantaylor 0:eafc3fd41f75 116 private:
bryantaylor 0:eafc3fd41f75 117 OneShotWriteCallback(GattClient* client, Gap::Handle_t connHandle,
bryantaylor 0:eafc3fd41f75 118 GattAttribute::Handle_t handle, const GattClient::WriteCallback_t& cb) :
bryantaylor 0:eafc3fd41f75 119 _client(client),
bryantaylor 0:eafc3fd41f75 120 _connHandle(connHandle),
bryantaylor 0:eafc3fd41f75 121 _handle(handle),
bryantaylor 0:eafc3fd41f75 122 _callback(cb) { }
bryantaylor 0:eafc3fd41f75 123
bryantaylor 0:eafc3fd41f75 124 void attach() {
bryantaylor 0:eafc3fd41f75 125 _client->onDataWritten(makeFunctionPointer(this, &OneShotWriteCallback::call));
bryantaylor 0:eafc3fd41f75 126 }
bryantaylor 0:eafc3fd41f75 127
bryantaylor 0:eafc3fd41f75 128 void call(const GattWriteCallbackParams* params) {
bryantaylor 0:eafc3fd41f75 129 // verifiy that it is the right characteristic on the right connection
bryantaylor 0:eafc3fd41f75 130 if (params->connHandle == _connHandle && params->handle == _handle) {
bryantaylor 0:eafc3fd41f75 131 _callback(params);
bryantaylor 0:eafc3fd41f75 132 _client->onDataWritten().detach(makeFunctionPointer(this, &OneShotWriteCallback::call));
bryantaylor 0:eafc3fd41f75 133 delete this;
bryantaylor 0:eafc3fd41f75 134 }
bryantaylor 0:eafc3fd41f75 135 }
bryantaylor 0:eafc3fd41f75 136
bryantaylor 0:eafc3fd41f75 137 GattClient* _client;
bryantaylor 0:eafc3fd41f75 138 Gap::Handle_t _connHandle;
bryantaylor 0:eafc3fd41f75 139 GattAttribute::Handle_t _handle;
bryantaylor 0:eafc3fd41f75 140 GattClient::WriteCallback_t _callback;
bryantaylor 0:eafc3fd41f75 141 };
bryantaylor 0:eafc3fd41f75 142
bryantaylor 0:eafc3fd41f75 143 ble_error_t DiscoveredCharacteristic::write(uint16_t length, const uint8_t *value, const GattClient::WriteCallback_t& onRead) const {
bryantaylor 0:eafc3fd41f75 144 ble_error_t error = write(length, value);
bryantaylor 0:eafc3fd41f75 145 if (error) {
bryantaylor 0:eafc3fd41f75 146 return error;
bryantaylor 0:eafc3fd41f75 147 }
bryantaylor 0:eafc3fd41f75 148
bryantaylor 0:eafc3fd41f75 149 OneShotWriteCallback::launch(gattc, connHandle, valueHandle, onRead);
bryantaylor 0:eafc3fd41f75 150
bryantaylor 0:eafc3fd41f75 151 return error;
bryantaylor 0:eafc3fd41f75 152 }
bryantaylor 0:eafc3fd41f75 153
bryantaylor 0:eafc3fd41f75 154 ble_error_t DiscoveredCharacteristic::discoverDescriptors(
bryantaylor 0:eafc3fd41f75 155 const CharacteristicDescriptorDiscovery::DiscoveryCallback_t& onCharacteristicDiscovered,
bryantaylor 0:eafc3fd41f75 156 const CharacteristicDescriptorDiscovery::TerminationCallback_t& onTermination) const {
bryantaylor 0:eafc3fd41f75 157
bryantaylor 0:eafc3fd41f75 158 if(!gattc) {
bryantaylor 0:eafc3fd41f75 159 return BLE_ERROR_INVALID_STATE;
bryantaylor 0:eafc3fd41f75 160 }
bryantaylor 0:eafc3fd41f75 161
bryantaylor 0:eafc3fd41f75 162 ble_error_t err = gattc->discoverCharacteristicDescriptors(
bryantaylor 0:eafc3fd41f75 163 *this, onCharacteristicDiscovered, onTermination
bryantaylor 0:eafc3fd41f75 164 );
bryantaylor 0:eafc3fd41f75 165
bryantaylor 0:eafc3fd41f75 166 return err;
bryantaylor 0:eafc3fd41f75 167 }