| 1 | // Copyright (C) 2022 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 "qcanuniqueiddescription.h" |
| 5 | #include "qcanuniqueiddescription_p.h" |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! |
| 10 | \class QCanUniqueIdDescription |
| 11 | \inmodule QtSerialBus |
| 12 | \since 6.5 |
| 13 | \preliminary |
| 14 | |
| 15 | \brief The QCanUniqueIdDescription class describes the rules for accessing |
| 16 | a unique identifier in a \l QCanBusFrame. |
| 17 | |
| 18 | A unique identifier is used to distinguish different CAN bus frames and |
| 19 | apply proper \l {QCanMessageDescription}s to encode or decode them. |
| 20 | Different CAN protocols can use different parts of the CAN frame as a unique |
| 21 | identifier (e.g. the DBC protocol uses the whole FrameId as a unique |
| 22 | identifier). |
| 23 | |
| 24 | This class contains parameters to specify the unique identifier position |
| 25 | within a CAN frame in a flexible way: |
| 26 | |
| 27 | \list |
| 28 | \li The part of the frame which will be used to extract the unique |
| 29 | identifier (FrameId or payload). |
| 30 | \li The start bit of the unique identifier, relative to the selected |
| 31 | part of the frame. The bits are counted starting from the LSB. |
| 32 | \li The number of bits used to represent the unique identifier. |
| 33 | \li The endian used to extract the value. |
| 34 | \endlist |
| 35 | |
| 36 | Check the \l {Data Endianness Processing} section of the |
| 37 | \l QCanSignalDescription documentation to see how the start bit value |
| 38 | depends on the data endianness. The approach that is described there is |
| 39 | also used for unique id description. |
| 40 | |
| 41 | The actual value of a unique identifier is represented by the |
| 42 | \l QtCanBus::UniqueId type. |
| 43 | |
| 44 | \sa QCanMessageDescription |
| 45 | */ |
| 46 | |
| 47 | /*! |
| 48 | Creates an empty unique identifier description. |
| 49 | */ |
| 50 | QCanUniqueIdDescription::QCanUniqueIdDescription() |
| 51 | : d(new QCanUniqueIdDescriptionPrivate) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | /*! |
| 56 | Creates a unique identifier description with the values copied from |
| 57 | \a other. |
| 58 | */ |
| 59 | QCanUniqueIdDescription::QCanUniqueIdDescription(const QCanUniqueIdDescription &other) |
| 60 | : d(other.d) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | /*! |
| 65 | \fn QCanUniqueIdDescription::QCanUniqueIdDescription(QCanUniqueIdDescription &&other) noexcept |
| 66 | |
| 67 | Creates a unique identifier description by moving from \a other. |
| 68 | |
| 69 | \note The moved-from QCanUniqueIdDescription object can only be destroyed or |
| 70 | assigned to. The effect of calling other functions than the destructor or |
| 71 | one of the assignment operators is undefined. |
| 72 | */ |
| 73 | |
| 74 | /*! |
| 75 | \fn QCanUniqueIdDescription::~QCanUniqueIdDescription() |
| 76 | |
| 77 | Destroys this unique identifier description. |
| 78 | */ |
| 79 | |
| 80 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QCanUniqueIdDescriptionPrivate) |
| 81 | |
| 82 | /*! |
| 83 | Assigns the values from \a other to this unique identifier description. |
| 84 | */ |
| 85 | QCanUniqueIdDescription &QCanUniqueIdDescription::operator=(const QCanUniqueIdDescription &other) |
| 86 | { |
| 87 | d = other.d; |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | /*! |
| 92 | \fn QCanUniqueIdDescription &QCanUniqueIdDescription::operator=(QCanUniqueIdDescription &&other) noexcept |
| 93 | |
| 94 | Move-assigns the values from \a other to this unique identifier description. |
| 95 | |
| 96 | \note The moved-from QCanUniqueIdDescription object can only be destroyed or |
| 97 | assigned to. The effect of calling other functions than the destructor or |
| 98 | one of the assignment operators is undefined. |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | Returns \c true when this unique identifier description is valid and |
| 103 | \c false otherwise. |
| 104 | |
| 105 | A valid unique identifier description \e must have a \l bitLength() which is |
| 106 | greater than zero and does not exceed the number of bits of the |
| 107 | \l QtCanBus::UniqueId type. |
| 108 | |
| 109 | \sa bitLength() |
| 110 | */ |
| 111 | bool QCanUniqueIdDescription::isValid() const |
| 112 | { |
| 113 | static constexpr auto uidSize = sizeof(QtCanBus::UniqueId) * 8; |
| 114 | return d->bitLength > 0 && d->bitLength <= uidSize; |
| 115 | } |
| 116 | |
| 117 | /*! |
| 118 | Returns the data source of the unique identifier. |
| 119 | |
| 120 | By default, \l {QtCanBus::}{FrameId} is used. |
| 121 | |
| 122 | \sa setSource(), QtCanBus::DataSource |
| 123 | */ |
| 124 | QtCanBus::DataSource QCanUniqueIdDescription::source() const |
| 125 | { |
| 126 | return d->source; |
| 127 | } |
| 128 | |
| 129 | /*! |
| 130 | Sets the data source of the unique identifier to \a source. |
| 131 | |
| 132 | \sa source(), QtCanBus::DataSource |
| 133 | */ |
| 134 | void QCanUniqueIdDescription::setSource(QtCanBus::DataSource source) |
| 135 | { |
| 136 | d.detach(); |
| 137 | d->source = source; |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | Returns the start bit of the unique identifier in the \l source(). |
| 142 | |
| 143 | \sa setStartBit(), bitLength(), setBitLength() |
| 144 | */ |
| 145 | quint16 QCanUniqueIdDescription::startBit() const |
| 146 | { |
| 147 | return d->startBit; |
| 148 | } |
| 149 | |
| 150 | /*! |
| 151 | Sets the start bit of the unique identifier in the \l source() to \a bit. |
| 152 | |
| 153 | \sa startBit(), bitLength(), setBitLength() |
| 154 | */ |
| 155 | void QCanUniqueIdDescription::setStartBit(quint16 bit) |
| 156 | { |
| 157 | d.detach(); |
| 158 | d->startBit = bit; |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | Returns the bit length of the unique identifier. |
| 163 | |
| 164 | \sa setBitLength(), startBit(), setStartBit() |
| 165 | */ |
| 166 | quint8 QCanUniqueIdDescription::bitLength() const |
| 167 | { |
| 168 | return d->bitLength; |
| 169 | } |
| 170 | |
| 171 | /*! |
| 172 | Sets the bit length of the unique identifier to \a length. |
| 173 | |
| 174 | \sa bitLength(), startBit(), setStartBit() |
| 175 | */ |
| 176 | void QCanUniqueIdDescription::setBitLength(quint8 length) |
| 177 | { |
| 178 | d.detach(); |
| 179 | d->bitLength = length; |
| 180 | } |
| 181 | |
| 182 | /*! |
| 183 | Returns the data endian of the unique identifier. |
| 184 | |
| 185 | By default, \l {QSysInfo::}{LittleEndian} is used. |
| 186 | |
| 187 | \sa setEndian(), QSysInfo::Endian |
| 188 | */ |
| 189 | QSysInfo::Endian QCanUniqueIdDescription::endian() const |
| 190 | { |
| 191 | return d->endian; |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | Sets the data endian of the unique identifier to \a endian. |
| 196 | |
| 197 | \sa endian(), QSysInfo::Endian |
| 198 | */ |
| 199 | void QCanUniqueIdDescription::setEndian(QSysInfo::Endian endian) |
| 200 | { |
| 201 | d.detach(); |
| 202 | d->endian = endian; |
| 203 | } |
| 204 | |
| 205 | QCanUniqueIdDescriptionPrivate *QCanUniqueIdDescriptionPrivate::get(const QCanUniqueIdDescription &desc) |
| 206 | { |
| 207 | return desc.d.data(); |
| 208 | } |
| 209 | |
| 210 | QT_END_NAMESPACE |
| 211 | |