summaryrefslogtreecommitdiff
path: root/helpers.cpp
blob: c77c054011bdc29a2efd5af612c65dd08700a7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 *  libgato - A GATT/ATT library for use with Bluez
 *
 *  Copyright (C) 2013 Javier S. Pedro <maemo@javispedro.com>
 *
 *  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 <QtCore/QDataStream>
#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()) {
	case 2:
		return GatoUUID(read_le<quint16>(ba.constData()));
	case 4:
		return GatoUUID(read_le<quint32>(ba.constData()));
	case 16:
		// For some reason, Bluetooth UUIDs use "reversed big endian" order.
		return GatoUUID(QUuid::fromRfc4122(reverse(ba)));
	default:
		return GatoUUID();
	}
}

QByteArray gatouuid_to_bytearray(const GatoUUID &uuid, bool use_uuid16, bool use_uuid32)
{
	if (use_uuid16) {
		quint16 uuid16 = uuid.toUInt16(&use_uuid16);
		if (use_uuid16) {
			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) {
			QByteArray bytes(sizeof(quint32), Qt::Uninitialized);
			write_le(uuid32, bytes.data());
			return bytes;
		}
	}

	return reverse(uuid.toRfc4122());
}

void write_gatouuid(QDataStream &s, const GatoUUID &uuid, bool use_uuid16, bool use_uuid32)
{
	QByteArray bytes = gatouuid_to_bytearray(uuid, use_uuid16, use_uuid32);
	s.writeRawData(bytes.constData(), bytes.size());
}