1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtBluetooth module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qbluetoothaddress.h"
41#include "qbluetoothaddress_p.h"
42
43#ifndef QT_NO_DEBUG_STREAM
44#include <QDebug>
45#endif
46
47QT_BEGIN_NAMESPACE
48
49/*!
50 \class QBluetoothAddress
51 \inmodule QtBluetooth
52 \brief The QBluetoothAddress class assigns an address to the Bluetooth
53 device.
54 \since 5.2
55
56 This class holds a Bluetooth address in a platform- and protocol-independent manner.
57*/
58
59/*!
60 \fn inline bool QBluetoothAddress::operator!=(const QBluetoothAddress &other) const
61
62
63 Compares this Bluetooth address with \a other.
64
65 Returns true if the Bluetooth addresses are not equal, otherwise returns false.
66*/
67
68static void registerQBluetoothAddressMetaType()
69{
70 static bool initDone = false;
71 if (!initDone) {
72 qRegisterMetaType<QBluetoothAddress>();
73 initDone = true;
74 }
75}
76
77/*!
78 Constructs an null Bluetooth address.
79*/
80QBluetoothAddress::QBluetoothAddress() :
81 d_ptr(new QBluetoothAddressPrivate)
82{
83 registerQBluetoothAddressMetaType();
84}
85
86/*!
87 Constructs a new Bluetooth address and assigns \a address to it.
88*/
89QBluetoothAddress::QBluetoothAddress(quint64 address) :
90 d_ptr(new QBluetoothAddressPrivate)
91{
92 registerQBluetoothAddressMetaType();
93
94 Q_D(QBluetoothAddress);
95 d->m_address = address;
96}
97
98/*!
99 Constructs a new Bluetooth address and assigns \a address to it.
100
101 The format of \a address can be either XX:XX:XX:XX:XX:XX or XXXXXXXXXXXX,
102 where X is a hexadecimal digit. Case is not important.
103*/
104QBluetoothAddress::QBluetoothAddress(const QString &address) :
105 d_ptr(new QBluetoothAddressPrivate)
106{
107 registerQBluetoothAddressMetaType();
108
109 Q_D(QBluetoothAddress);
110
111 QString a = address;
112
113 if (a.length() == 17)
114 a.remove(c: QLatin1Char(':'));
115
116 if (a.length() == 12) {
117 bool ok;
118 d->m_address = a.toULongLong(ok: &ok, base: 16);
119 if (!ok)
120 clear();
121 } else {
122 d->m_address = 0;
123 }
124}
125
126/*!
127 Constructs a new Bluetooth address which is a copy of \a other.
128*/
129QBluetoothAddress::QBluetoothAddress(const QBluetoothAddress &other) :
130 d_ptr(new QBluetoothAddressPrivate)
131{
132 *this = other;
133}
134
135/*!
136 Destroys the QBluetoothAddress.
137*/
138QBluetoothAddress::~QBluetoothAddress()
139{
140 delete d_ptr;
141}
142
143/*!
144 Assigns \a other to this Bluetooth address.
145*/
146QBluetoothAddress &QBluetoothAddress::operator=(const QBluetoothAddress &other)
147{
148 Q_D(QBluetoothAddress);
149
150 d->m_address = other.d_func()->m_address;
151
152 return *this;
153}
154
155/*!
156 Sets the Bluetooth address to 00:00:00:00:00:00.
157*/
158void QBluetoothAddress::clear()
159{
160 Q_D(QBluetoothAddress);
161 d->m_address = 0;
162}
163
164/*!
165 Returns true if the Bluetooth address is null, otherwise returns false.
166*/
167bool QBluetoothAddress::isNull() const
168{
169 Q_D(const QBluetoothAddress);
170 return d->m_address == 0;
171}
172
173/*!
174 Returns true if the Bluetooth address is less than \a other, otherwise
175 returns false.
176*/
177bool QBluetoothAddress::operator<(const QBluetoothAddress &other) const
178{
179 Q_D(const QBluetoothAddress);
180 return d->m_address < other.d_func()->m_address;
181}
182
183/*!
184 Compares this Bluetooth address to \a other.
185
186 Returns true if the two Bluetooth addresses are equal, otherwise returns false.
187*/
188bool QBluetoothAddress::operator==(const QBluetoothAddress &other) const
189{
190 Q_D(const QBluetoothAddress);
191 return d->m_address == other.d_func()->m_address;
192}
193
194/*!
195 Returns this Bluetooth address as a quint64.
196*/
197quint64 QBluetoothAddress::toUInt64() const
198{
199 Q_D(const QBluetoothAddress);
200 return d->m_address;
201}
202
203/*!
204 Returns the Bluetooth address as a string of the form, XX:XX:XX:XX:XX:XX.
205*/
206QString QBluetoothAddress::toString() const
207{
208 QString s(QStringLiteral("%1:%2:%3:%4:%5:%6"));
209 Q_D(const QBluetoothAddress);
210
211 for (int i = 5; i >= 0; --i) {
212 const quint8 a = (d->m_address >> (i*8)) & 0xff;
213 s = s.arg(a, fieldWidth: 2, base: 16, fillChar: QLatin1Char('0'));
214 }
215
216 return s.toUpper();
217}
218
219QBluetoothAddressPrivate::QBluetoothAddressPrivate()
220{
221 m_address = 0;
222}
223
224#ifndef QT_NO_DEBUG_STREAM
225QDebug operator<<(QDebug debug, const QBluetoothAddress &address)
226{
227 debug << address.toString();
228 return debug;
229}
230#endif
231
232QT_END_NAMESPACE
233

source code of qtconnectivity/src/bluetooth/qbluetoothaddress.cpp