| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qbluetoothaddress.h" |
| 5 | |
| 6 | #ifndef QT_NO_DEBUG_STREAM |
| 7 | #include <QDebug> |
| 8 | #endif |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | static_assert(QT6_ONLY(!)std::is_trivially_copyable_v<QBluetoothAddress>, |
| 13 | "Must stay this way until Qt 7 because of BC reasons." ); |
| 14 | |
| 15 | QT_IMPL_METATYPE_EXTERN(QBluetoothAddress) |
| 16 | |
| 17 | /*! |
| 18 | \class QBluetoothAddress |
| 19 | \inmodule QtBluetooth |
| 20 | \brief The QBluetoothAddress class assigns an address to the Bluetooth |
| 21 | device. |
| 22 | \since 5.2 |
| 23 | |
| 24 | This class holds a Bluetooth address in a platform- and protocol-independent manner. |
| 25 | */ |
| 26 | |
| 27 | void registerQBluetoothAddress() |
| 28 | { |
| 29 | qRegisterMetaType<QBluetoothAddress>(); |
| 30 | } |
| 31 | Q_CONSTRUCTOR_FUNCTION(registerQBluetoothAddress) |
| 32 | |
| 33 | /*! |
| 34 | \fn QBluetoothAddress::QBluetoothAddress() |
| 35 | |
| 36 | Constructs a null Bluetooth Address. |
| 37 | */ |
| 38 | |
| 39 | /*! |
| 40 | \fn QBluetoothAddress::QBluetoothAddress(quint64 address) |
| 41 | |
| 42 | Constructs a new Bluetooth address and assigns \a address to it. |
| 43 | */ |
| 44 | |
| 45 | /*! |
| 46 | Constructs a new Bluetooth address and assigns \a address to it. |
| 47 | |
| 48 | The format of \a address can be either XX:XX:XX:XX:XX:XX or XXXXXXXXXXXX, |
| 49 | where X is a hexadecimal digit. Case is not important. |
| 50 | */ |
| 51 | QBluetoothAddress::QBluetoothAddress(const QString &address) |
| 52 | { |
| 53 | QString a = address; |
| 54 | |
| 55 | if (a.size() == 17) |
| 56 | a.remove(c: QLatin1Char(':')); |
| 57 | |
| 58 | if (a.size() == 12) { |
| 59 | bool ok; |
| 60 | m_address = a.toULongLong(ok: &ok, base: 16); |
| 61 | if (!ok) |
| 62 | clear(); |
| 63 | } else { |
| 64 | m_address = 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | \fn QBluetoothAddress::qHash(const QBluetoothAddress &key, size_t seed) |
| 70 | \since 6.6 |
| 71 | |
| 72 | Returns the hash value for the \a key, using \a seed to seed the |
| 73 | calculation. |
| 74 | */ |
| 75 | |
| 76 | /*! |
| 77 | \fn void QBluetoothAddress::clear() |
| 78 | |
| 79 | Sets the Bluetooth address to 00:00:00:00:00:00. |
| 80 | */ |
| 81 | |
| 82 | /*! |
| 83 | \fn bool QBluetoothAddress::isNull() const |
| 84 | |
| 85 | Returns true if the Bluetooth address is null, otherwise returns false. |
| 86 | */ |
| 87 | |
| 88 | /*! |
| 89 | \fn quint64 QBluetoothAddress::toUInt64() const |
| 90 | |
| 91 | Returns this Bluetooth address as a quint64. |
| 92 | */ |
| 93 | |
| 94 | /*! |
| 95 | Returns the Bluetooth address as a string of the form, XX:XX:XX:XX:XX:XX. |
| 96 | */ |
| 97 | QString QBluetoothAddress::toString() const |
| 98 | { |
| 99 | QString s(QStringLiteral("%1:%2:%3:%4:%5:%6" )); |
| 100 | |
| 101 | for (int i = 5; i >= 0; --i) { |
| 102 | const quint8 a = (m_address >> (i*8)) & 0xff; |
| 103 | s = s.arg(a, fieldWidth: 2, base: 16, fillChar: QLatin1Char('0')); |
| 104 | } |
| 105 | |
| 106 | return s.toUpper(); |
| 107 | } |
| 108 | |
| 109 | /*! |
| 110 | \fn bool QBluetoothAddress::operator<(const QBluetoothAddress &a, |
| 111 | const QBluetoothAddress &b) |
| 112 | \brief Returns true if the Bluetooth address \a a is less than \a b, otherwise |
| 113 | returns false. |
| 114 | */ |
| 115 | |
| 116 | /*! |
| 117 | \fn bool QBluetoothAddress::operator==(const QBluetoothAddress &a, |
| 118 | const QBluetoothAddress &b) |
| 119 | \brief Returns \c true if the two Bluetooth addresses \a a and \a b are equal, |
| 120 | otherwise returns \c false. |
| 121 | */ |
| 122 | |
| 123 | /*! |
| 124 | \fn bool QBluetoothAddress::operator!=(const QBluetoothAddress &a, |
| 125 | const QBluetoothAddress &b) |
| 126 | \brief Returns \c true if the two Bluetooth addresses \a a and \a b are not equal, |
| 127 | otherwise returns \c false. |
| 128 | */ |
| 129 | |
| 130 | #ifndef QT_NO_DEBUG_STREAM |
| 131 | QDebug QBluetoothAddress::streamingOperator(QDebug debug, const QBluetoothAddress &address) |
| 132 | { |
| 133 | debug << address.toString(); |
| 134 | return debug; |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | QT_END_NAMESPACE |
| 139 | |