Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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