Software can read HMC5883L

Dependents:   DOOR_BLE_PROGRAM

Fork of BLE_API by Bluetooth Low Energy

Committer:
vcoubard
Date:
Wed Apr 06 19:13:54 2016 +0100
Revision:
1135:22aada733dbd
Parent:
1131:692ddf04fc42
Synchronized with git rev 17e37360
Author: Vincent Coubard
Merge pull request #161 from ARMmbed/develop

merge branch develop (v2.4.0)

Who changed what in which revision?

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