Diffstat (limited to 'noncore/settings/networksettings2/opietooth2/OTUUID.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/opietooth2/OTUUID.cpp | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/opietooth2/OTUUID.cpp b/noncore/settings/networksettings2/opietooth2/OTUUID.cpp new file mode 100644 index 0000000..4b05a7a --- a/dev/null +++ b/noncore/settings/networksettings2/opietooth2/OTUUID.cpp | |||
@@ -0,0 +1,162 @@ | |||
1 | //-*-c++-*- | ||
2 | /*************************************************************************** | ||
3 | * Copyright (C) 2003 by Fred Schaettgen * | ||
4 | * kdebluetooth@schaettgen.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | #include <assert.h> | ||
13 | #include <qregexp.h> | ||
14 | #include <opie2/odebug.h> | ||
15 | |||
16 | #include <OTUUID.h> | ||
17 | |||
18 | using namespace Opietooth2; | ||
19 | |||
20 | OTUUID::OTUUID(QString s) { | ||
21 | fromString(s); | ||
22 | } | ||
23 | |||
24 | OTUUID::OTUUID(uint64_t l, uint64_t h) { | ||
25 | this->hi = h; | ||
26 | this->lo = l; | ||
27 | } | ||
28 | |||
29 | OTUUID::OTUUID( const OTUUID & O ) { | ||
30 | this->hi = O.hi; | ||
31 | this->lo = O.lo; | ||
32 | } | ||
33 | |||
34 | OTUUID & OTUUID::operator =( const OTUUID & O ) { | ||
35 | hi = O.hi; | ||
36 | lo = O.lo; | ||
37 | return *this; | ||
38 | } | ||
39 | |||
40 | bool OTUUID::fromString(QString s) { | ||
41 | |||
42 | // Strip leading "0x" | ||
43 | if (s.startsWith("0x")) | ||
44 | { | ||
45 | s = s.right(s.length()-2); | ||
46 | } | ||
47 | // Remove separators | ||
48 | s.replace( QRegExp(":"), "" ); | ||
49 | |||
50 | bool bOk = false; | ||
51 | if( s.length() == 4 || s.length() == 8) { | ||
52 | |||
53 | uint32_t u32 = s.toUInt(&bOk, 16); | ||
54 | setUUID32(u32); | ||
55 | return bOk; | ||
56 | |||
57 | } else if (s.length() == 32) { | ||
58 | |||
59 | #if (QT_VERSION >= 0x030200) | ||
60 | uint64_t u64hi = s.left(16).toULongLong(&bOk, 16); | ||
61 | #else | ||
62 | uint64_t u64hi = s.left(16).toULong(&bOk, 16); | ||
63 | #endif | ||
64 | if (!bOk) | ||
65 | return false; | ||
66 | |||
67 | #if (QT_VERSION >= 0x030200) | ||
68 | uint64_t u64lo = s.right(16).toULongLong(&bOk, 16); | ||
69 | #else | ||
70 | uint64_t u64lo = s.right(16).toULong(&bOk, 16); | ||
71 | #endif | ||
72 | if (!bOk) | ||
73 | return false; | ||
74 | hi = u64hi; | ||
75 | lo = u64lo; | ||
76 | return true; | ||
77 | |||
78 | } else { | ||
79 | hi = 0; | ||
80 | lo = 0; | ||
81 | return true; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | void OTUUID::setUUID32(uint32_t v) { | ||
86 | |||
87 | hi = uint64_t(0x1000) | (uint64_t(v) << 32); | ||
88 | // see BT 1.1 Core spec p2.7.1 | ||
89 | lo = (uint64_t(0x80000080) << 32) | uint64_t(0x5F9B34FB); | ||
90 | |||
91 | } | ||
92 | |||
93 | void OTUUID::setUUID128(uint64_t _hi, uint64_t _lo) { | ||
94 | hi = _hi; | ||
95 | lo = _lo; | ||
96 | } | ||
97 | |||
98 | QString OTUUID::toString() const { | ||
99 | QString ret; | ||
100 | uint32_t v1 = uint32_t(hi >> 32); | ||
101 | uint32_t v2 = uint32_t(hi & 0xFFFFFFFF); | ||
102 | uint32_t v3 = uint32_t(lo >> 32); | ||
103 | uint32_t v4 = uint32_t(lo & 0xFFFFFFFF); | ||
104 | ret.sprintf("0x%08lx:%08lx:%08lx:%08lx", | ||
105 | (unsigned long)v1, (unsigned long)v2, | ||
106 | (unsigned long)v3, (unsigned long)v4); | ||
107 | |||
108 | return ret; | ||
109 | } | ||
110 | |||
111 | OTUUID::operator QString() const { | ||
112 | return toString(); | ||
113 | } | ||
114 | |||
115 | OTUUID::operator ::uuid_t() const { | ||
116 | |||
117 | //kdDebug() << "uuid_t() -> " << QString(*this) << endl; | ||
118 | ::uuid_t ret; | ||
119 | if ((lo == (uint64_t(0x80000080) << 32) | uint64_t(0x5F9B34FB)) && | ||
120 | ((hi&0xFFFFFFFF) == 0x1000)) { | ||
121 | uint32_t uuid32val = uint32_t(hi >> 32); | ||
122 | if (uuid32val > 0xFFFF) { | ||
123 | ret.type = SDP_UUID16; | ||
124 | ret.value.uuid16 = uint16_t(uuid32val); | ||
125 | } else { | ||
126 | ret.type = SDP_UUID32; | ||
127 | ret.value.uuid32 = uuid32val; | ||
128 | } | ||
129 | } else { | ||
130 | ret.value.uuid128.data[0] = (lo >> 0) && 0xFF; | ||
131 | ret.value.uuid128.data[1] = (lo >> 8) && 0xFF; | ||
132 | ret.value.uuid128.data[2] = (lo >> 16) && 0xFF; | ||
133 | ret.value.uuid128.data[3] = (lo >> 24) && 0xFF; | ||
134 | ret.value.uuid128.data[4] = (lo >> 32) && 0xFF; | ||
135 | ret.value.uuid128.data[5] = (lo >> 40) && 0xFF; | ||
136 | ret.value.uuid128.data[6] = (lo >> 48) && 0xFF; | ||
137 | ret.value.uuid128.data[7] = (lo >> 56) && 0xFF; | ||
138 | |||
139 | ret.value.uuid128.data[8] = (hi >> 0) && 0xFF; | ||
140 | ret.value.uuid128.data[9] = (hi >> 8) && 0xFF; | ||
141 | ret.value.uuid128.data[10] = (hi >> 16) && 0xFF; | ||
142 | ret.value.uuid128.data[11] = (hi >> 24) && 0xFF; | ||
143 | ret.value.uuid128.data[12] = (hi >> 32) && 0xFF; | ||
144 | ret.value.uuid128.data[13] = (hi >> 40) && 0xFF; | ||
145 | ret.value.uuid128.data[14] = (hi >> 48) && 0xFF; | ||
146 | ret.value.uuid128.data[15] = (hi >> 56) && 0xFF; | ||
147 | |||
148 | ret.type = SDP_UUID128; | ||
149 | } | ||
150 | return ret; | ||
151 | } | ||
152 | |||
153 | bool OTUUID::operator<(const OTUUID & other) const { | ||
154 | if (hi != other.hi) | ||
155 | return hi < other.hi; | ||
156 | |||
157 | return lo<other.lo; | ||
158 | } | ||
159 | |||
160 | bool OTUUID::operator ==(const OTUUID& u) const { | ||
161 | return (u.hi == hi) && (u.lo == lo); | ||
162 | } | ||