From e0677f561465615d84ca81b48955dbf8e169e02e Mon Sep 17 00:00:00 2001 From: Javier Date: Tue, 20 May 2014 01:47:23 +0200 Subject: remove gatouint128, simplify gatouuid encoding --- helpers.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'helpers.cpp') diff --git a/helpers.cpp b/helpers.cpp index b62b464..ceb4454 100644 --- a/helpers.cpp +++ b/helpers.cpp @@ -20,6 +20,16 @@ #include "helpers.h" +static QByteArray reverse(const QByteArray &ba) +{ + int size = ba.size(); + QByteArray r(size, Qt::Uninitialized); + for (int i = 0; i < size; i++) { + r[i] = ba[size - i - 1]; + } + return r; +} + GatoUUID bytearray_to_gatouuid(const QByteArray &ba) { switch (ba.size()) { @@ -28,7 +38,8 @@ GatoUUID bytearray_to_gatouuid(const QByteArray &ba) case 4: return GatoUUID(read_le(ba.constData())); case 16: - return GatoUUID(read_le(ba.constData())); + // For some reason, Bluetooth UUIDs use "reversed big endian" order. + return GatoUUID(QUuid::fromRfc4122(reverse(ba))); default: return GatoUUID(); } @@ -36,29 +47,22 @@ GatoUUID bytearray_to_gatouuid(const QByteArray &ba) QByteArray gatouuid_to_bytearray(const GatoUUID &uuid, bool use_uuid16, bool use_uuid32) { - QByteArray ba; - if (use_uuid16) { quint16 uuid16 = uuid.toUInt16(&use_uuid16); if (use_uuid16) { - ba.resize(sizeof(quint16)); - write_le(uuid16, ba.data()); - return ba; + QByteArray bytes(sizeof(quint16), Qt::Uninitialized); + write_le(uuid16, bytes.data()); + return bytes; } } if (use_uuid32) { quint32 uuid32 = uuid.toUInt32(&use_uuid32); if (use_uuid32) { - ba.resize(sizeof(quint32)); - write_le(uuid32, ba.data()); - return ba; + QByteArray bytes(sizeof(quint32), Qt::Uninitialized); + write_le(uuid32, bytes.data()); + return bytes; } } - gatouint128 uuid128 = uuid.toUInt128(); - ba.resize(sizeof(gatouint128)); - Q_ASSERT(ba.size() == 16); - write_le(uuid128, ba.data()); - - return ba; + return reverse(uuid.toRfc4122()); } -- cgit v1.2.3