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