Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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