From 5926b05afa21eaac36c185e7fc458710efa30b02 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 21 Feb 2015 23:58:39 +0100 Subject: o Support for reading and writing characteristics. --- ble/LinuxBluetooth.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'ble/LinuxBluetooth.cpp') diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index d62bf23..435dd63 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -1,6 +1,5 @@ #include "BluetoothImpl.h" -#include #include #include #include @@ -8,7 +7,6 @@ #include #include #include -#include // Got to love magic constants. Taken from bluez.git/tools/btgatt-client.c #define ATT_CID 4 @@ -77,6 +75,10 @@ public: void discoverServices() override; + void writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) override; + + ByteBuffer readValue(const BluetoothGattCharacteristic &c) override; + private: vector discoverServices(uint16_t startHandle); @@ -224,6 +226,42 @@ uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) { return u; } +void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) { + DF; + + D << "Writing to characteristic " << c.getUuid() << ": " << bytes.toString(); + + shared_ptr buffer(new uint8_t[MAX_MTU]); + ByteBuffer out = ByteBuffer(buffer, MAX_MTU); + + AttPdu::makeWrite(out, c.getValueHandle(), bytes); + + ByteBuffer in = writeAndRead(out, buffer, MAX_MTU); + + AttPdu::parseWrite(in); +} + +ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) { + DF; + + shared_ptr buffer(new uint8_t[MAX_MTU]); + ByteBuffer out = ByteBuffer(buffer, MAX_MTU); + + AttPdu::makeRead(out, c.getValueHandle()); + + ByteBuffer in = writeAndRead(out, buffer, MAX_MTU); + + AttPdu::parseRead(in); + +// D << "READ response has " + to_string(in.getBytesLeft()) + " bytes"; + + auto response = in.view(); + + D << "Value of characteristic " << c.getUuid() << "=" << response.toString(); + + return response; +} + void LinuxBluetoothGatt::discoverServices() { uint16_t startHandle = 0x0001; @@ -320,7 +358,7 @@ ByteBuffer LinuxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr throw BluetoothException(&device, "read(): " + errnoAsString()); } - auto in = ByteBuffer(buffer, (size_t) r); + auto in = ByteBuffer(buffer, (size_t) r, (size_t) r); D << "read: " << r << " bytes: " << in.toString(); -- cgit v1.2.3