From 041bbe24b39190ac0b624b2709fc38dea17ad0a7 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 12 Feb 2015 17:22:59 +0100 Subject: o wip. --- Bluetooth.cpp | 8 ++------ Bluetooth.h | 10 +++++----- ByteBuffer.cpp | 20 +++++++++++++++++++- ByteBuffer.h | 4 ++++ LinuxBluetooth.cpp | 12 ++++-------- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Bluetooth.cpp b/Bluetooth.cpp index 737f04f..5dca8fa 100644 --- a/Bluetooth.cpp +++ b/Bluetooth.cpp @@ -119,17 +119,13 @@ namespace trygvis { AttributeData* AttributeData::fromByteBuffer(ByteBuffer &bytes, uint8_t length) { uint16_t handle = bytes.get16le(); - uint8_t* value = new uint8_t[length]; - bytes.copy(value, length); - return new AttributeData(handle, value); + return new AttributeData(handle, bytes.view(length)); } - AttributeData::AttributeData(uint16_t handle, uint8_t *bytes) : handle(handle), bytes(bytes) { - + AttributeData::AttributeData(uint16_t handle, ByteBuffer bytes) : handle(handle), value(value) { } AttributeData::~AttributeData() { - delete bytes; } // ----------------------------------------------------------------------- diff --git a/Bluetooth.h b/Bluetooth.h index 586ac66..a0fd2c6 100644 --- a/Bluetooth.h +++ b/Bluetooth.h @@ -126,14 +126,14 @@ namespace trygvis { class AttributeData { public: - static AttributeData* fromByteBuffer(ByteBuffer &bytes, uint8_t length); + static AttributeData* fromByteBuffer(ByteBuffer &value, uint8_t length); + + const uint16_t handle; + const ByteBuffer value; private: - AttributeData(uint16_t handle, uint8_t* bytes); + AttributeData(uint16_t handle, ByteBuffer value); ~AttributeData(); - - uint16_t handle; - uint8_t *bytes; }; // BluetoothAdapter &getAdapter(int hciDevice); diff --git a/ByteBuffer.cpp b/ByteBuffer.cpp index 70b9a8e..912e697 100644 --- a/ByteBuffer.cpp +++ b/ByteBuffer.cpp @@ -1,6 +1,8 @@ #include "ByteBuffer.h" #include #include +#include +#include using namespace std; @@ -35,7 +37,7 @@ uint16_t ByteBuffer::get16le() { canAccessIndex(cursor + 1); uint16_t value; value = bytes[zero + cursor++]; - value = ((uint16_t) bytes[zero + cursor++]) << 8; + value |= ((uint16_t) bytes[zero + cursor++]) << 8; return value; } @@ -46,6 +48,12 @@ void ByteBuffer::copy(uint8_t *bytes, size_t length) { cursor += length; } +ByteBuffer ByteBuffer::view(size_t length) { + canAccessIndex(cursor + length); + size_t s = zero + cursor + length; + return ByteBuffer(bytes, s, s, cursor); +} + void ByteBuffer::checkAndUpdateSize(size_t newBytes) { size_t newSize = zero + cursor + newBytes; if (newSize >= capacity) { @@ -60,3 +68,13 @@ void ByteBuffer::canAccessIndex(size_t index) { throw ByteBufferException(string("Out of bounds! zero=") + to_string(zero) + ", index=" + to_string(index) + ", size=" + to_string(size)); } } + +std::string ByteBuffer::toString() const { + stringstream s; + + for(size_t i = zero; i < size; i++) { + s << hex << setfill('0') << setw(2) << bytes[i] << " "; + } + + return string(s.str()); +} diff --git a/ByteBuffer.h b/ByteBuffer.h index 444943d..c93b5f1 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -36,6 +36,10 @@ public: void copy(uint8_t *bytes, size_t length); + ByteBuffer view(size_t length); + + std::string toString() const; + private: void checkAndUpdateSize(size_t count); diff --git a/LinuxBluetooth.cpp b/LinuxBluetooth.cpp index d274f84..a15f350 100644 --- a/LinuxBluetooth.cpp +++ b/LinuxBluetooth.cpp @@ -165,15 +165,12 @@ namespace trygvis { D << "read: " << r << " bytes"; - vector* values = AttPdu::parseReadByGroupType(in); + vector *values = AttPdu::parseReadByGroupType(in); - D << "READ_BY_GROUP_TYPE response has " + (values->size()) + " values"; + D << "READ_BY_GROUP_TYPE response has " + to_string(values->size()) + " values"; - for(auto& data: *values) { -// strstring s; -// s << hex << setfill('0') << setw(2) << ar[i] << " "; -// data.bytes -// D << data. + for (auto &data: *values) { + D << "handle: " << data->handle << ", value: " << data->value.toString(); } delete values; @@ -342,5 +339,4 @@ namespace trygvis { BluetoothAdapter *getAdapter(int hciDevice) { return new LinuxBluetoothAdapter(hciDevice); } - }; -- cgit v1.2.3