From 52450ed3034b0ba058ea2c9f9baa2d5f78df6a94 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 17 Nov 2018 22:38:32 +0100 Subject: apps/ble-bts: o Adding start of health termometer service tool. apps/ble-read-characteristic: o Sart of new tool. apps/ble-inspect-device o Make adapter configurable. other: o UUID fixes and tests. --- ble/LinuxBluetooth.cpp | 21 ++++++++------- ble/misc.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 14 deletions(-) (limited to 'ble') diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 541093b..e567d62 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -251,15 +251,18 @@ Uuid readUuid(BluetoothDevice *device, ByteBuffer &bytes) { if (bytesLeft == 2) { uint8_t bs[16] = BLUETOOTH_UUID_INITIALIZER; - bs[2] = bytes.read8(); bs[3] = bytes.read8(); + bs[2] = bytes.read8(); return Uuid(bs); } else if (bytesLeft == 16) { - return {bytes.read8(), bytes.read8(), bytes.read8(), bytes.read8(), - bytes.read8(), bytes.read8(), bytes.read8(), bytes.read8(), - bytes.read8(), bytes.read8(), bytes.read8(), bytes.read8(), - bytes.read8(), bytes.read8(), bytes.read8(), bytes.read8()}; + uint8_t bs[16] = { + bytes.get8(15), bytes.get8(14), bytes.get8(13), bytes.get8(12), bytes.get8(11), bytes.get8(10), bytes.get8(9), bytes.get8(8), + bytes.get8(7), bytes.get8(6), bytes.get8(5), bytes.get8(4), bytes.get8(3), bytes.get8(2), bytes.get8(1), bytes.get8(0), + }; + bytes.skip(16); + + return Uuid{bs}; } else { throw BluetoothException(device, "Unexpected bytes left: " + to_string(bytesLeft)); } @@ -368,10 +371,10 @@ void LinuxBluetoothGatt::discoverServices() { uint16_t valueHandle = c.value.read16le(); auto uuid = readUuid(&device, c.value); -// D << "characteristic: handle: " << setw(2) << setfill('0') << hex << (int) c.handle << -// ", properties: " << setw(2) << setfill('0') << hex << (int) properties << -// ", valueHandle: 0x" << setw(4) << setfill('0') << hex << (int) valueHandle << -// ", uuid: " << uuid; + LOG_DEBUG("characteristic: handle: " << setw(2) << setfill('0') << hex << (int) c.handle << + ", properties: " << setw(2) << setfill('0') << hex << (int) properties << + ", valueHandle: 0x" << setw(4) << setfill('0') << hex << (int) valueHandle << + ", uuid: " << uuid); s->addCharacteristic(std::move(make_shared(s, c.handle, uuid, properties, valueHandle))); } diff --git a/ble/misc.cpp b/ble/misc.cpp index b26b99e..a8df2cb 100644 --- a/ble/misc.cpp +++ b/ble/misc.cpp @@ -1,11 +1,13 @@ -#include "ble/misc.h" - #include +#include #include +#include namespace trygvis { namespace bluetooth { +using namespace std; + std::ostream &operator<<(std::ostream &s, Uuid const &uuid) { s << uuid.str(); return s; @@ -14,13 +16,62 @@ std::ostream &operator<<(std::ostream &s, Uuid const &uuid) { std::string Uuid::str() const { char str[37]; - std::sprintf(str, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], - value[8], value[9], value[10], value[11], value[12], value[13], value[14], value[15]); + std::snprintf(str, sizeof(str), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], + value[8], value[9], value[10], value[11], value[12], value[13], value[14], value[15]); return {str}; } +o Uuid::fromString(const std::string &str) { + try { + if (str.size() == 4) { + unsigned long x = std::stoul(str, nullptr, 16); + + if (x > std::numeric_limits::max()) { + return {}; + } + + return {ShortUuid{static_cast(x)}.toLong()}; + } else if (str.size() == 36) { + uint8_t bytes[16]; + + unsigned i = 0; + for (unsigned byte = 0; byte < 16; byte++) { + switch (byte) { + case 4: + case 6: + case 8: + case 10: + i++; + break; + default: + break; + } + + auto tmp = str.substr(i, 2); + cout << "str=" << tmp << endl; + unsigned long x = std::stoul(tmp, nullptr, 16); + + if (x > std::numeric_limits::max()) { + return {}; + } + bytes[byte] = static_cast(x); + + i += 2; + } + + return {Uuid{bytes}}; + } else { + return {}; + } + } catch (std::invalid_argument &) { + return {}; + } catch (std::out_of_range &) { + return {}; + } +} + Uuid makeUuid(const Uuid &base, uint8_t a, uint8_t b) { uint8_t value[16]; memcpy(value, base.value, 16); @@ -29,5 +80,13 @@ Uuid makeUuid(const Uuid &base, uint8_t a, uint8_t b) { return Uuid{value}; } +std::string ShortUuid::str() const noexcept { + char str[sizeof("abcd")]; + + std::snprintf(str, sizeof(str), "%02x%02x", int(value >> 8), int(value & 0xff)); + + return {str}; +} + } // namespace bluetooth } // namespace trygvis -- cgit v1.2.3