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