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