summaryrefslogtreecommitdiff
path: root/gatouuid.cpp
blob: 91b9e327875161a3634b39ffcd0ecefa9497dbef (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  libgato - A GATT/ATT library for use with Bluez
 *
 *  Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
 *  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/QDebug>
#include <QtCore/QHash>

#include "gatouuid.h"

// Bluetooth style UUIDs are like
// {XXXXXXXX-0000-1000-8000-00805F9B34FB}

static const quint16 baseuuid_data2 = 0x0000;
static const quint16 baseuuid_data3 = 0x1000;
static const quint8 baseuuid_data4[8] = {0x80, 0x00,
                                         0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};

// Used in QUuid constructor, because it wants bytes as args instead of byte array
#define BASEUUID_DATA4_VALUES baseuuid_data4[0], baseuuid_data4[1], baseuuid_data4[2], baseuuid_data4[3], \
                              baseuuid_data4[4], baseuuid_data4[5], baseuuid_data4[6], baseuuid_data4[7]

GatoUUID::GatoUUID()
{

}

GatoUUID::GatoUUID(GattUuid uuid)
    : QUuid(uuid, baseuuid_data2, baseuuid_data3, BASEUUID_DATA4_VALUES)
{
}

GatoUUID::GatoUUID(quint16 uuid)
    : QUuid(uuid, baseuuid_data2, baseuuid_data3, BASEUUID_DATA4_VALUES)
{

}

GatoUUID::GatoUUID(quint32 uuid)
    : QUuid(uuid, baseuuid_data2, baseuuid_data3, BASEUUID_DATA4_VALUES)
{
}

GatoUUID::GatoUUID(gatouint128 uuid)
    : QUuid()
{
	quint32 tmp32;
	memcpy(&tmp32, &uuid.data[0], 4);
	data1 = qFromBigEndian<quint32>(tmp32);

	quint16 tmp16;
	memcpy(&tmp16, &uuid.data[4], 2);
	data2 = qFromBigEndian<quint16>(tmp16);

	memcpy(&tmp16, &uuid.data[6], 2);
	data3 = qFromBigEndian<quint16>(tmp16);

	memcpy(data4, &uuid.data[8], 8);
}

GatoUUID::GatoUUID(const QString &uuid)
    : QUuid(uuid)
{

}

GatoUUID::GatoUUID(const GatoUUID &o)
	: QUuid(o)
{

}

GatoUUID::GatoUUID(const QUuid &uuid)
    : QUuid(uuid)
{

}

GatoUUID::~GatoUUID()
{
}

int GatoUUID::minimumSize() const
{
	if (isNull()) {
		return 0;
	}

	if (data2 == baseuuid_data2 && data3 == baseuuid_data3 &&
	        memcmp(data4, baseuuid_data4, sizeof(baseuuid_data4)) == 0) {
		if (data1 & 0xFFFF0000) {
			return 4;
		} else {
			return 2;
		}
	}

	return 16;
}

quint16 GatoUUID::toUInt16(bool *ok) const
{
	if (minimumSize() <= 2) {
		if (ok) *ok = true;
		return data1;
	} else {
		if (ok) *ok = false;
		return 0;
	}
}

quint32 GatoUUID::toUInt32(bool *ok) const
{
	if (minimumSize() <= 4) {
		if (ok) *ok = true;
		return data1;
	} else {
		if (ok) *ok = false;
		return 0;
	}
}

gatouint128 GatoUUID::toUInt128() const
{
	gatouint128 uuid;

	quint32 tmp32 = qToBigEndian<quint32>(data1);
	memcpy(&uuid.data[0], &tmp32, 4);

	quint16 tmp16 = qToBigEndian<quint16>(data2);
	memcpy(&uuid.data[4], &tmp16, 2);

	tmp16 = qToBigEndian<quint16>(data3);
	memcpy(&uuid.data[6], &tmp16, 2);

	memcpy(&uuid.data[8], data4, 8);

	return uuid;
}

QDebug operator<<(QDebug debug, const GatoUUID &uuid)
{
	quint16 uuid16;
	quint32 uuid32;
	bool ok = false;

	uuid16 = uuid.toUInt16(&ok);
	if (ok) {
		debug.nospace() << "0x" << hex << uuid16 << dec;
		return debug.space();
	}

	uuid32 = uuid.toUInt32(&ok);
	if (ok) {
		debug.nospace() << "0x" << hex << uuid32 << dec;
		return debug.space();
	}

	debug.nospace() << uuid.toString().toLatin1().constData();
    return debug.space();
}

QDataStream & operator<<(QDataStream &s, const gatouint128 &u)
{
	if (static_cast<QSysInfo::Endian>(s.byteOrder()) == QSysInfo::ByteOrder) {
		for (int i = 0; i < 16; i++) {
			s << u.data[i];
		}
	} else {
		for (int i = 15; i >= 0; i--) {
			s << u.data[i];
		}
	}
	return s;
}

QDataStream & operator>>(QDataStream &s, gatouint128 &u)
{
	if (static_cast<QSysInfo::Endian>(s.byteOrder()) == QSysInfo::ByteOrder) {
		for (int i = 0; i < 16; i++) {
			s >> u.data[i];
		}
	} else {
		for (int i = 15; i >= 0; i--) {
			s >> u.data[i];
		}
	}
	return s;
}

uint qHash(const GatoUUID &a)
{
	gatouint128 u128 = a.toUInt128();

	uint h = u128.data[0] << 24 | u128.data[1] << 16 | u128.data[2] << 8 | u128.data[3] << 0;
	h ^= u128.data[4] << 24 | u128.data[5] << 16 | u128.data[6] << 8 | u128.data[7] << 0;
	h ^= u128.data[8] << 24 | u128.data[9] << 16 | u128.data[10] << 8 | u128.data[11] << 0;
	h ^= u128.data[12] << 24 | u128.data[13] << 16 | u128.data[14] << 8 | u128.data[15] << 0;

	return h;
}