aboutsummaryrefslogtreecommitdiff
path: root/ble
diff options
context:
space:
mode:
Diffstat (limited to 'ble')
-rw-r--r--ble/LinuxBluetooth.cpp21
-rw-r--r--ble/misc.cpp69
2 files changed, 76 insertions, 14 deletions
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<DefaultBluetoothGattCharacteristic>(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 <ble/misc.h>
+#include <sstream>
#include <string>
+#include <ble/Bluetooth.h>
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> Uuid::fromString(const std::string &str) {
+ try {
+ if (str.size() == 4) {
+ unsigned long x = std::stoul(str, nullptr, 16);
+
+ if (x > std::numeric_limits<uint16_t>::max()) {
+ return {};
+ }
+
+ return {ShortUuid{static_cast<uint16_t>(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<uint8_t>::max()) {
+ return {};
+ }
+ bytes[byte] = static_cast<uint8_t>(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