From 47ada94baa424e56d2ded256fddc91e6aa4d3090 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Tue, 3 Dec 2013 03:05:47 +0100 Subject: initial import --- helpers.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 helpers.cpp (limited to 'helpers.cpp') diff --git a/helpers.cpp b/helpers.cpp new file mode 100644 index 0000000..b62b464 --- /dev/null +++ b/helpers.cpp @@ -0,0 +1,64 @@ +/* + * libgato - A GATT/ATT library for use with Bluez + * + * Copyright (C) 2013 Javier S. Pedro + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "helpers.h" + +GatoUUID bytearray_to_gatouuid(const QByteArray &ba) +{ + switch (ba.size()) { + case 2: + return GatoUUID(read_le(ba.constData())); + case 4: + return GatoUUID(read_le(ba.constData())); + case 16: + return GatoUUID(read_le(ba.constData())); + default: + return GatoUUID(); + } +} + +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; + } + } + if (use_uuid32) { + quint32 uuid32 = uuid.toUInt32(&use_uuid32); + if (use_uuid32) { + ba.resize(sizeof(quint32)); + write_le(uuid32, ba.data()); + return ba; + } + } + + gatouint128 uuid128 = uuid.toUInt128(); + ba.resize(sizeof(gatouint128)); + Q_ASSERT(ba.size() == 16); + write_le(uuid128, ba.data()); + + return ba; +} -- cgit v1.2.3