summaryrefslogtreecommitdiff
path: root/gatouuid.h
blob: e0915122961a3833409dbc260bf010f2ed4b24a1 (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
#ifndef GATOUUID_H
#define GATOUUID_H

#include <QtCore/QDebug>
#include <QtCore/QtEndian>
#include <QtCore/QSharedDataPointer>
#include <QtCore/QString>
#include "libgato_global.h"

class GatoUUIDPrivate;

struct gatouint128
{
	quint8 data[16];
};

template<>
inline LIBGATO_EXPORT gatouint128 qFromLittleEndian<gatouint128>(const uchar *src)
{
	gatouint128 dest;
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
	for (int i = 0; i < 16; i++) {
		dest.data[i] = src[i];
	}
#else
	for (int i = 0; i < 16; i++) {
		dest.data[i] = src[15 - i];
	}
#endif
	return dest;
}

template<>
inline LIBGATO_EXPORT void qToLittleEndian<gatouint128>(gatouint128 src, uchar *dest)
{
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
	for (int i = 0; i < 16; i++) {
		dest[i] = src.data[i];
	}
#else
	for (int i = 0; i < 16; i++) {
		dest[i] = src.data[15 - i];
	}
#endif
}

class LIBGATO_EXPORT GatoUUID
{
	Q_GADGET
	Q_ENUMS(GattUuid)

public:
	enum GattUuid {
		GattGenericAccessProfile = 0x1800,
		GattGenericAttributeProfile = 0x1801,
		GattPrimaryService = 0x2800,
		GattSecondaryService = 0x2801,
		GattInclude = 0x2802,
		GattCharacteristic = 0x2803,
		GattCharacteristicExtendedProperties = 0x2900,
		GattCharacteristicUserDescription = 0x2901,
		GattClientCharacteristicConfiguration = 0x2902,
		GattServerCharacteristicConfiguration = 0x2903,
		GattCharacteristicFormat = 0x2904,
		GattCharacteristicAggregateFormat = 0x2905,
		GattDeviceName = 0x2A00,
		GattAppearance = 0x2A01,
		GattPeripheralPrivacyFlag = 0x2A02,
		GattReconnectionAddress = 0x2A03,
		GattPeripheralPreferredConnectionParameters = 0x2A04,
		GattServiceChanged = 0x2A05
	};

	GatoUUID();
	GatoUUID(GattUuid uuid);
	explicit GatoUUID(quint16 uuid);
	explicit GatoUUID(quint32 uuid);
	explicit GatoUUID(gatouint128 uuid);
	explicit GatoUUID(const QString &uuid);
	GatoUUID(const GatoUUID &o);
	~GatoUUID();

	bool isNull() const;

	quint16 toUInt16(bool *ok = 0) const;
	quint32 toUInt32(bool *ok = 0) const;
	gatouint128 toUInt128() const;
	QString toString() const;

	GatoUUID& operator=(const GatoUUID& o);
	friend bool operator==(const GatoUUID &a, const GatoUUID &b);
	friend bool operator!=(const GatoUUID &a, const GatoUUID &b);

private:
	QSharedDataPointer<GatoUUIDPrivate> d;
};

inline QDebug operator<<(QDebug debug, const GatoUUID &uuid)
{
    debug << uuid.toString();
    return debug;
}

LIBGATO_EXPORT QDataStream & operator<<(QDataStream &s, const gatouint128 &u);
LIBGATO_EXPORT QDataStream & operator>>(QDataStream &s, gatouint128 &u);

LIBGATO_EXPORT uint qHash(const GatoUUID &a);

#endif // GATOUUID_H