summaryrefslogtreecommitdiffstats
path: root/src/serialbus/qcanuniqueiddescription.cpp
blob: 3096b0517c5150f99e586a7f47b908f8727b822d (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qcanuniqueiddescription.h"
#include "qcanuniqueiddescription_p.h"

QT_BEGIN_NAMESPACE

/*!
    \class QCanUniqueIdDescription
    \inmodule QtSerialBus
    \since 6.5
    \preliminary

    \brief The QCanUniqueIdDescription class describes the rules for accessing
    a unique identifier in a \l QCanBusFrame.

    A unique identifier is used to distinguish different CAN bus frames and
    apply proper \l {QCanMessageDescription}s to encode or decode them.
    Different CAN protocols can use different parts of the CAN frame as a unique
    identifier (e.g. the DBC protocol uses the whole FrameId as a unique
    identifier).

    This class contains parameters to specify the unique identifier position
    within a CAN frame in a flexible way:

    \list
        \li The part of the frame which will be used to extract the unique
            identifier (FrameId or payload).
        \li The start bit of the unique identifier, relative to the selected
            part of the frame. The bits are counted starting from the LSB.
        \li The number of bits used to represent the unique identifier.
        \li The endian used to extract the value.
    \endlist

    Check the \l {Data Endianness Processing} section of the
    \l QCanSignalDescription documentation to see how the start bit value
    depends on the data endianness. The approach that is described there is
    also used for unique id description.

    The actual value of a unique identifier is represented by the
    \l QtCanBus::UniqueId type.

    \sa QCanMessageDescription
*/

/*!
    Creates an empty unique identifier description.
*/
QCanUniqueIdDescription::QCanUniqueIdDescription()
    : d(new QCanUniqueIdDescriptionPrivate)
{
}

/*!
    Creates a unique identifier description with the values copied from
    \a other.
*/
QCanUniqueIdDescription::QCanUniqueIdDescription(const QCanUniqueIdDescription &other)
    : d(other.d)
{
}

/*!
    \fn QCanUniqueIdDescription::QCanUniqueIdDescription(QCanUniqueIdDescription &&other) noexcept

    Creates a unique identifier description by moving from \a other.

    \note The moved-from QCanUniqueIdDescription object can only be destroyed or
    assigned to. The effect of calling other functions than the destructor or
    one of the assignment operators is undefined.
*/

/*!
    \fn QCanUniqueIdDescription::~QCanUniqueIdDescription()

    Destroys this unique identifier description.
*/

QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QCanUniqueIdDescriptionPrivate)

/*!
    Assigns the values from \a other to this unique identifier description.
*/
QCanUniqueIdDescription &QCanUniqueIdDescription::operator=(const QCanUniqueIdDescription &other)
{
    d = other.d;
    return *this;
}

/*!
    \fn QCanUniqueIdDescription &QCanUniqueIdDescription::operator=(QCanUniqueIdDescription &&other) noexcept

    Move-assigns the values from \a other to this unique identifier description.

    \note The moved-from QCanUniqueIdDescription object can only be destroyed or
    assigned to. The effect of calling other functions than the destructor or
    one of the assignment operators is undefined.
*/

/*!
    \fn bool QCanUniqueIdDescription::operator==(const QCanUniqueIdDescription &lhs, const QCanUniqueIdDescription &rhs)

    Returns \c true if all of the \a lhs object's values are the same as those
    of \a rhs. Otherwise returns \c false.
*/

/*!
    \fn bool QCanUniqueIdDescription::operator!=(const QCanUniqueIdDescription &lhs, const QCanUniqueIdDescription &rhs)

    Returns \c true if any of the \a lhs object's values are not the same as
    those of \a rhs. Otherwise returns \c false.
*/

/*!
    Returns \c true when this unique identifier description is valid and
    \c false otherwise.

    A valid unique identifier description \e must have a \l bitLength() which is
    greater than zero and does not exceed the number of bits of the
    \l QtCanBus::UniqueId type.

    \sa bitLength()
*/
bool QCanUniqueIdDescription::isValid() const
{
    static constexpr auto uidSize = sizeof(QtCanBus::UniqueId) * 8;
    return d->bitLength > 0 && d->bitLength <= uidSize;
}

/*!
    Returns the data source of the unique identifier.

    By default, \l {QtCanBus::}{FrameId} is used.

    \sa setSource(), QtCanBus::DataSource
*/
QtCanBus::DataSource QCanUniqueIdDescription::source() const
{
    return d->source;
}

/*!
    Sets the data source of the unique identifier to \a source.

    \sa source(), QtCanBus::DataSource
*/
void QCanUniqueIdDescription::setSource(QtCanBus::DataSource source)
{
    d.detach();
    d->source = source;
}

/*!
    Returns the start bit of the unique identifier in the \l source().

    \sa setStartBit(), bitLength(), setBitLength()
*/
quint16 QCanUniqueIdDescription::startBit() const
{
    return d->startBit;
}

/*!
    Sets the start bit of the unique identifier in the \l source() to \a bit.

    \sa startBit(), bitLength(), setBitLength()
*/
void QCanUniqueIdDescription::setStartBit(quint16 bit)
{
    d.detach();
    d->startBit = bit;
}

/*!
    Returns the bit length of the unique identifier.

    \sa setBitLength(), startBit(), setStartBit()
*/
quint8 QCanUniqueIdDescription::bitLength() const
{
    return d->bitLength;
}

/*!
    Sets the bit length of the unique identifier to \a length.

    \sa bitLength(), startBit(), setStartBit()
*/
void QCanUniqueIdDescription::setBitLength(quint8 length)
{
    d.detach();
    d->bitLength = length;
}

/*!
    Returns the data endian of the unique identifier.

    By default, \l {QSysInfo::}{LittleEndian} is used.

    \sa setEndian(), QSysInfo::Endian
*/
QSysInfo::Endian QCanUniqueIdDescription::endian() const
{
    return d->endian;
}

/*!
    Sets the data endian of the unique identifier to \a endian.

    \sa endian(), QSysInfo::Endian
*/
void QCanUniqueIdDescription::setEndian(QSysInfo::Endian endian)
{
    d.detach();
    d->endian = endian;
}

bool QCanUniqueIdDescription::equals(const QCanUniqueIdDescription &lhs, const QCanUniqueIdDescription &rhs)
{
    return lhs.d->source == rhs.d->source
            && lhs.d->endian == rhs.d->endian
            && lhs.d->startBit == rhs.d->startBit
            && lhs.d->bitLength == rhs.d->bitLength;
}

QCanUniqueIdDescriptionPrivate *QCanUniqueIdDescriptionPrivate::get(const QCanUniqueIdDescription &desc)
{
    return desc.d.data();
}

QT_END_NAMESPACE