1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtSerialBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QtSerialBus/qcanbusframe.h>
38
39#include <QtCore/qdatastream.h>
40#include <QtTest/qtest.h>
41
42class tst_QCanBusFrame : public QObject
43{
44 Q_OBJECT
45public:
46 explicit tst_QCanBusFrame();
47
48private slots:
49 void constructors();
50 void id();
51 void payload();
52 void timeStamp();
53 void bitRateSwitch();
54 void errorStateIndicator();
55 void localEcho();
56
57 void tst_isValid_data();
58 void tst_isValid();
59 void tst_isValidSize_data();
60 void tst_isValidSize();
61
62 void tst_toString_data();
63 void tst_toString();
64
65 void streaming_data();
66 void streaming();
67
68 void tst_error();
69};
70
71tst_QCanBusFrame::tst_QCanBusFrame()
72{
73}
74
75void tst_QCanBusFrame::constructors()
76{
77 QCanBusFrame frame1;
78 QCanBusFrame frame2(123, "tst");
79 QCanBusFrame frame3(123456, "tst");
80 QCanBusFrame frame4(1234, "tst tst tst");
81 QCanBusFrame::TimeStamp timeStamp1;
82 QCanBusFrame::TimeStamp timeStamp2(5, 5);
83
84 QVERIFY(frame1.payload().isEmpty());
85 QVERIFY(!frame1.frameId());
86 QVERIFY(!frame1.hasFlexibleDataRateFormat());
87 QVERIFY(!frame1.hasExtendedFrameFormat());
88 QCOMPARE(frame1.frameType(), QCanBusFrame::DataFrame);
89
90 QVERIFY(!frame2.payload().isEmpty());
91 QVERIFY(frame2.frameId());
92 QVERIFY(!frame2.hasFlexibleDataRateFormat());
93 QVERIFY(!frame2.hasExtendedFrameFormat());
94 QCOMPARE(frame2.frameType(), QCanBusFrame::DataFrame);
95
96 QVERIFY(!frame3.payload().isEmpty());
97 QVERIFY(frame3.frameId());
98 QVERIFY(!frame3.hasFlexibleDataRateFormat());
99 QVERIFY(frame3.hasExtendedFrameFormat());
100 QCOMPARE(frame3.frameType(), QCanBusFrame::DataFrame);
101
102 QVERIFY(!frame4.payload().isEmpty());
103 QVERIFY(frame4.frameId());
104 QVERIFY(frame4.hasFlexibleDataRateFormat());
105 QVERIFY(!frame4.hasExtendedFrameFormat());
106 QCOMPARE(frame4.frameType(), QCanBusFrame::DataFrame);
107
108 QVERIFY(!timeStamp1.seconds());
109 QVERIFY(!timeStamp1.microSeconds());
110
111 QVERIFY(timeStamp2.seconds());
112 QVERIFY(timeStamp2.microSeconds());
113}
114
115void tst_QCanBusFrame::id()
116{
117 QCanBusFrame frame;
118 QVERIFY(!frame.frameId());
119 frame.setExtendedFrameFormat(false);
120 frame.setFrameId(2047u);
121 QCOMPARE(frame.frameId(), 2047u);
122 QVERIFY(frame.isValid());
123 QVERIFY(!frame.hasExtendedFrameFormat());
124 // id > 2^11 -> extended format
125 frame.setExtendedFrameFormat(false);
126 frame.setFrameId(2048u);
127 QCOMPARE(frame.frameId(), 2048u);
128 QVERIFY(frame.isValid());
129 QVERIFY(frame.hasExtendedFrameFormat());
130 // id < 2^11 -> no extended format
131 frame.setExtendedFrameFormat(false);
132 frame.setFrameId(512u);
133 QCOMPARE(frame.frameId(), 512u);
134 QVERIFY(frame.isValid());
135 QVERIFY(!frame.hasExtendedFrameFormat());
136 // id < 2^11 -> keep extended format
137 frame.setExtendedFrameFormat(true);
138 frame.setFrameId(512u);
139 QCOMPARE(frame.frameId(), 512u);
140 QVERIFY(frame.isValid());
141 QVERIFY(frame.hasExtendedFrameFormat());
142 // id >= 2^29 -> invalid
143 frame.setExtendedFrameFormat(false);
144 frame.setFrameId(536870912u);
145 QCOMPARE(frame.frameId(), 0u);
146 QVERIFY(!frame.isValid());
147 QVERIFY(!frame.hasExtendedFrameFormat());
148}
149
150void tst_QCanBusFrame::payload()
151{
152 QCanBusFrame frame;
153 QVERIFY(frame.payload().isEmpty());
154 frame.setPayload("test");
155 QCOMPARE(frame.payload().data(), "test");
156 QVERIFY(!frame.hasFlexibleDataRateFormat());
157 // setting long payload should automatically set hasFlexibleDataRateFormat()
158 frame.setPayload("testtesttest");
159 QCOMPARE(frame.payload().data(), "testtesttest");
160 QVERIFY(frame.hasFlexibleDataRateFormat());
161 // setting short payload should not change hasFlexibleDataRateFormat()
162 frame.setPayload("test");
163 QCOMPARE(frame.payload().data(), "test");
164 QVERIFY(frame.hasFlexibleDataRateFormat());
165}
166
167void tst_QCanBusFrame::timeStamp()
168{
169 QCanBusFrame frame;
170 QCanBusFrame::TimeStamp timeStamp = frame.timeStamp();
171 QVERIFY(!timeStamp.seconds());
172 QVERIFY(!timeStamp.microSeconds());
173
174 // fromMicroSeconds: no microsecond overflow
175 timeStamp = QCanBusFrame::TimeStamp::fromMicroSeconds(usec: 999999);
176 QCOMPARE(timeStamp.seconds(), 0);
177 QCOMPARE(timeStamp.microSeconds(), 999999);
178
179 // fromMicroSeconds: microsecond overflow
180 timeStamp = QCanBusFrame::TimeStamp::fromMicroSeconds(usec: 1000000);
181 QCOMPARE(timeStamp.seconds(), 1);
182 QCOMPARE(timeStamp.microSeconds(), 0);
183
184 // fromMicroSeconds: microsecond overflow
185 timeStamp = QCanBusFrame::TimeStamp::fromMicroSeconds(usec: 2000001);
186 QCOMPARE(timeStamp.seconds(), 2);
187 QCOMPARE(timeStamp.microSeconds(), 1);
188}
189
190void tst_QCanBusFrame::bitRateSwitch()
191{
192 QCanBusFrame frame(QCanBusFrame::DataFrame);
193 QVERIFY(!frame.hasBitrateSwitch());
194
195 // set CAN FD does not set BRS
196 frame.setFlexibleDataRateFormat(true);
197 QVERIFY(frame.hasFlexibleDataRateFormat());
198 QVERIFY(!frame.hasBitrateSwitch());
199
200 // set BRS keeps CAN FD
201 frame.setBitrateSwitch(true);
202 QVERIFY(frame.hasFlexibleDataRateFormat());
203 QVERIFY(frame.hasBitrateSwitch());
204
205 // clear BRS keeps CAN FD
206 frame.setBitrateSwitch(false);
207 QVERIFY(frame.hasFlexibleDataRateFormat());
208 QVERIFY(!frame.hasBitrateSwitch());
209
210 // clear CAN FD
211 frame.setFlexibleDataRateFormat(false);
212 QVERIFY(!frame.hasFlexibleDataRateFormat());
213 QVERIFY(!frame.hasBitrateSwitch());
214
215 // set BRS sets CAN FD
216 frame.setBitrateSwitch(true);
217 QVERIFY(frame.hasFlexibleDataRateFormat());
218 QVERIFY(frame.hasBitrateSwitch());
219
220 // clear CAN FD clears BRS
221 frame.setFlexibleDataRateFormat(false);
222 QVERIFY(!frame.hasFlexibleDataRateFormat());
223 QVERIFY(!frame.hasBitrateSwitch());
224
225 // default constructed CAN FD frame has no BRS
226 const QCanBusFrame frame2(0x123, QByteArray(10, 0x55));
227 QVERIFY(frame2.hasFlexibleDataRateFormat());
228 QVERIFY(!frame2.hasBitrateSwitch());
229}
230
231void tst_QCanBusFrame::errorStateIndicator()
232{
233 QCanBusFrame frame(QCanBusFrame::DataFrame);
234 QVERIFY(!frame.hasErrorStateIndicator());
235
236 // set CAN FD does not set ESI
237 frame.setFlexibleDataRateFormat(true);
238 QVERIFY(frame.hasFlexibleDataRateFormat());
239 QVERIFY(!frame.hasErrorStateIndicator());
240
241 // set ESI keeps CAN FD
242 frame.setErrorStateIndicator(true);
243 QVERIFY(frame.hasFlexibleDataRateFormat());
244 QVERIFY(frame.hasErrorStateIndicator());
245
246 // clear ESI keeps CAN FD
247 frame.setErrorStateIndicator(false);
248 QVERIFY(frame.hasFlexibleDataRateFormat());
249 QVERIFY(!frame.hasErrorStateIndicator());
250
251 // clear CAN FD
252 frame.setFlexibleDataRateFormat(false);
253 QVERIFY(!frame.hasFlexibleDataRateFormat());
254 QVERIFY(!frame.hasErrorStateIndicator());
255
256 // set ESI sets CAN FD
257 frame.setErrorStateIndicator(true);
258 QVERIFY(frame.hasFlexibleDataRateFormat());
259 QVERIFY(frame.hasErrorStateIndicator());
260
261 // clear CAN FD clears ESI
262 frame.setFlexibleDataRateFormat(false);
263 QVERIFY(!frame.hasFlexibleDataRateFormat());
264 QVERIFY(!frame.hasErrorStateIndicator());
265
266 // default constructed CAN FD frame has no ESI
267 const QCanBusFrame frame2(0x123, QByteArray(10, 0x55));
268 QVERIFY(frame2.hasFlexibleDataRateFormat());
269 QVERIFY(!frame2.hasErrorStateIndicator());
270}
271
272void tst_QCanBusFrame::localEcho()
273{
274 QCanBusFrame frame(QCanBusFrame::DataFrame);
275 QVERIFY(!frame.hasLocalEcho());
276
277 frame.setLocalEcho(true);
278 QVERIFY(frame.hasLocalEcho());
279
280 frame.setLocalEcho(false);
281 QVERIFY(!frame.hasLocalEcho());
282
283 const QCanBusFrame frame2(0x123, QByteArray());
284 QVERIFY(!frame2.hasLocalEcho());
285}
286
287void tst_QCanBusFrame::tst_isValid_data()
288{
289 QTest::addColumn<QCanBusFrame::FrameType>(name: "frameType");
290 QTest::addColumn<bool>(name: "isValid");
291 QTest::addColumn<QByteArray>(name: "payload");
292 QTest::addColumn<uint>(name: "id");
293 QTest::addColumn<bool>(name: "extended");
294 QTest::addColumn<bool>(name: "flexibleData");
295
296 QTest::newRow(dataTag: "invalid frame")
297 << QCanBusFrame::InvalidFrame << false
298 << QByteArray() << 0u << false << false;
299 QTest::newRow(dataTag: "data frame")
300 << QCanBusFrame::DataFrame << true
301 << QByteArray() << 0u << false << false;
302 QTest::newRow(dataTag: "error frame")
303 << QCanBusFrame::ErrorFrame << true
304 << QByteArray() << 0u << false << false;
305 QTest::newRow(dataTag: "remote request frame")
306 << QCanBusFrame::RemoteRequestFrame << true
307 << QByteArray() << 0u << false << false;
308 QTest::newRow(dataTag: "remote request CAN FD frame")
309 << QCanBusFrame::RemoteRequestFrame << false
310 << QByteArray() << 0u << false << true;
311 QTest::newRow(dataTag: "unknown frame")
312 << QCanBusFrame::UnknownFrame << true
313 << QByteArray() << 0u << false << false;
314 QTest::newRow(dataTag: "data frame CAN max payload")
315 << QCanBusFrame::DataFrame << true
316 << QByteArray(8, 0) << 0u << false << false;
317 QTest::newRow(dataTag: "data frame CAN FD max payload")
318 << QCanBusFrame::DataFrame << true
319 << QByteArray(64, 0) << 0u << false << true;
320 QTest::newRow(dataTag: "data frame CAN too much payload")
321 << QCanBusFrame::DataFrame << false
322 << QByteArray(65, 0) << 0u << false << false;
323 QTest::newRow(dataTag: "data frame short id")
324 << QCanBusFrame::DataFrame << true
325 << QByteArray(8, 0) << (1u << 11) - 1 << false << false;
326 QTest::newRow(dataTag: "data frame long id")
327 << QCanBusFrame::DataFrame << true
328 << QByteArray(8, 0) << (1u << 11) << true << false;
329 QTest::newRow(dataTag: "data frame bad long id")
330 << QCanBusFrame::DataFrame << false
331 << QByteArray(8, 0) << (1u << 11) << false << false;
332 QTest::newRow(dataTag: "data frame CAN too long payload")
333 << QCanBusFrame::DataFrame << false
334 << QByteArray(9, 0) << 512u << false << false;
335 QTest::newRow(dataTag: "data frame CAN FD long payload")
336 << QCanBusFrame::DataFrame << true
337 << QByteArray(12, 0) << 512u << false << true;
338 QTest::newRow(dataTag: "data frame CAN FD too long payload")
339 << QCanBusFrame::DataFrame << false
340 << QByteArray(65, 0) << 512u << false << true;
341 }
342
343void tst_QCanBusFrame::tst_isValid()
344{
345 QFETCH(QCanBusFrame::FrameType, frameType);
346 QFETCH(bool, isValid);
347 QFETCH(QByteArray, payload);
348 QFETCH(uint, id);
349 QFETCH(bool, extended);
350 QFETCH(bool, flexibleData);
351
352 QCanBusFrame frame(frameType);
353 frame.setPayload(payload);
354 frame.setFrameId(id);
355 frame.setExtendedFrameFormat(extended);
356 frame.setFlexibleDataRateFormat(flexibleData);
357 QCOMPARE(frame.isValid(), isValid);
358 QCOMPARE(frame.frameType(), frameType);
359 QCOMPARE(frame.payload(), payload);
360 QCOMPARE(frame.frameId(), id);
361 QCOMPARE(frame.hasExtendedFrameFormat(), extended);
362 QCOMPARE(frame.hasFlexibleDataRateFormat(), flexibleData);
363
364 frame.setFrameType(QCanBusFrame::InvalidFrame);
365 QCOMPARE(frame.isValid(), false);
366 QCOMPARE(QCanBusFrame::InvalidFrame, frame.frameType());
367}
368
369void tst_QCanBusFrame::tst_isValidSize_data()
370{
371 QTest::addColumn<int>(name: "payloadLength");
372 QTest::addColumn<bool>(name: "isFlexibleDataRate");
373 QTest::addColumn<bool>(name: "isValid");
374
375 QTest::newRow(dataTag: "2.0-0") << 0 << false << true;
376 QTest::newRow(dataTag: "2.0-8") << 8 << false << true;
377 QTest::newRow(dataTag: "2.0-9") << 9 << false << false;
378
379 QTest::newRow(dataTag: "FD-0") << 0 << true << true;
380 QTest::newRow(dataTag: "FD-8") << 8 << true << true;
381 QTest::newRow(dataTag: "FD-9") << 9 << true << false;
382 QTest::newRow(dataTag: "FD-11") << 11 << true << false;
383 QTest::newRow(dataTag: "FD-12") << 12 << true << true;
384 QTest::newRow(dataTag: "FD-13") << 13 << true << false;
385 QTest::newRow(dataTag: "FD-16") << 16 << true << true;
386
387 QTest::newRow(dataTag: "FD-20") << 20 << true << true;
388 QTest::newRow(dataTag: "FD-24") << 24 << true << true;
389 QTest::newRow(dataTag: "FD-32") << 32 << true << true;
390 QTest::newRow(dataTag: "FD-48") << 48 << true << true;
391
392 QTest::newRow(dataTag: "FD-63") << 63 << true << false;
393 QTest::newRow(dataTag: "FD-64") << 64 << true << true;
394 QTest::newRow(dataTag: "FD-65") << 65 << true << false;
395}
396
397void tst_QCanBusFrame::tst_isValidSize()
398{
399 QFETCH(int, payloadLength);
400 QFETCH(bool, isFlexibleDataRate);
401 QFETCH(bool, isValid);
402
403 QCanBusFrame frame(0, QByteArray(payloadLength, ' '));
404 frame.setFlexibleDataRateFormat(isFlexibleDataRate);
405
406 QCOMPARE(frame.isValid(), isValid);
407}
408
409void tst_QCanBusFrame::tst_toString_data()
410{
411 QTest::addColumn<QCanBusFrame::FrameType>(name: "frameType");
412 QTest::addColumn<quint32>(name: "id");
413 QTest::addColumn<bool>(name: "extended");
414 QTest::addColumn<QByteArray>(name: "payload");
415 QTest::addColumn<QString>(name: "expected");
416
417 QTest::newRow(dataTag: "invalid frame")
418 << QCanBusFrame::InvalidFrame << 0x0u << false
419 << QByteArray()
420 << "(Invalid)";
421 QTest::newRow(dataTag: "error frame")
422 << QCanBusFrame::ErrorFrame << 0x0u << false
423 << QByteArray()
424 << "(Error)";
425 QTest::newRow(dataTag: "unknown frame")
426 << QCanBusFrame::UnknownFrame << 0x0u << false
427 << QByteArray()
428 << "(Unknown)";
429 QTest::newRow(dataTag: "remote request frame")
430 << QCanBusFrame::RemoteRequestFrame << 0x123u << false
431 << QByteArray::fromHex(hexEncoded: "01") // fake data to get a DLC > 0
432 << QString(" 123 [1] Remote Request");
433 QTest::newRow(dataTag: "data frame min std id")
434 << QCanBusFrame::DataFrame << 0x0u << false
435 << QByteArray()
436 << QString(" 000 [0]");
437 QTest::newRow(dataTag: "data frame max std id")
438 << QCanBusFrame::DataFrame << 0x7FFu << false
439 << QByteArray()
440 << QString(" 7FF [0]");
441 QTest::newRow(dataTag: "data frame min ext id")
442 << QCanBusFrame::DataFrame << 0x0u << true
443 << QByteArray()
444 << QString("00000000 [0]");
445 QTest::newRow(dataTag: "data frame max ext id")
446 << QCanBusFrame::DataFrame << 0x1FFFFFFFu << true
447 << QByteArray()
448 << QString("1FFFFFFF [0]");
449 QTest::newRow(dataTag: "data frame minimal size")
450 << QCanBusFrame::DataFrame << 0x7FFu << false
451 << QByteArray::fromHex(hexEncoded: "01")
452 << QString(" 7FF [1] 01");
453 QTest::newRow(dataTag: "data frame maximal size")
454 << QCanBusFrame::DataFrame << 0x1FFFFFFFu << true
455 << QByteArray::fromHex(hexEncoded: "0123456789ABCDEF")
456 << QString("1FFFFFFF [8] 01 23 45 67 89 AB CD EF");
457 QTest::newRow(dataTag: "short data frame FD")
458 << QCanBusFrame::DataFrame << 0x123u << false
459 << QByteArray::fromHex(hexEncoded: "001122334455667788")
460 << QString(" 123 [09] 00 11 22 33 44 55 66 77 88");
461 QTest::newRow(dataTag: "long data frame FD")
462 << QCanBusFrame::DataFrame << 0x123u << false
463 << QByteArray::fromHex(hexEncoded: "00112233445566778899")
464 << QString(" 123 [10] 00 11 22 33 44 55 66 77 88 99");
465}
466
467void tst_QCanBusFrame::tst_toString()
468{
469 QFETCH(QCanBusFrame::FrameType, frameType);
470 QFETCH(quint32, id);
471 QFETCH(bool, extended);
472 QFETCH(QByteArray, payload);
473 QFETCH(QString, expected);
474 QCanBusFrame frame;
475 frame.setFrameType(frameType);
476 frame.setFrameId(id);
477 frame.setExtendedFrameFormat(extended);
478 frame.setPayload(payload);
479
480 const QString result = frame.toString();
481
482 QCOMPARE(result, expected);
483}
484
485void tst_QCanBusFrame::streaming_data()
486{
487 QTest::addColumn<quint32>(name: "frameId");
488 QTest::addColumn<QByteArray>(name: "payload");
489 QTest::addColumn<qint64>(name: "seconds");
490 QTest::addColumn<qint64>(name: "microSeconds");
491 QTest::addColumn<bool>(name: "isExtended");
492 QTest::addColumn<bool>(name: "isFlexibleDataRate");
493 QTest::addColumn<bool>(name: "isBitrateSwitch");
494 QTest::addColumn<bool>(name: "isErrorStateIndicator");
495 QTest::addColumn<bool>(name: "isLocalEcho");
496 QTest::addColumn<QCanBusFrame::FrameType>(name: "frameType");
497
498
499 QTest::newRow(dataTag: "emptyFrame") << quint32(0) << QByteArray()
500 << qint64(0) << qint64(0)
501 << false << false << false << false << false
502 << QCanBusFrame::DataFrame;
503 QTest::newRow(dataTag: "fullFrame1") << quint32(123) << QByteArray("abcde1")
504 << qint64(456) << qint64(784)
505 << true << false << false << false << false
506 << QCanBusFrame::DataFrame;
507 QTest::newRow(dataTag: "fullFrame2") << quint32(123) << QByteArray("abcde2")
508 << qint64(457) << qint64(785)
509 << false << false << false << false << false
510 << QCanBusFrame::DataFrame;
511 QTest::newRow(dataTag: "fullFrameFD") << quint32(123) << QByteArray("abcdfd")
512 << qint64(457) << qint64(785)
513 << false << true << false << false << false
514 << QCanBusFrame::DataFrame;
515 QTest::newRow(dataTag: "fullFrameBRS") << quint32(123) << QByteArray("abcdfd")
516 << qint64(457) << qint64(785)
517 << false << true << true << false << false
518 << QCanBusFrame::DataFrame;
519 QTest::newRow(dataTag: "fullFrameESI") << quint32(123) << QByteArray("abcdfd")
520 << qint64(457) << qint64(785)
521 << false << true << false << true << false
522 << QCanBusFrame::DataFrame;
523 QTest::newRow(dataTag: "echoFrame") << quint32(123) << QByteArray("abcde7")
524 << qint64(888) << qint64(777)
525 << false << false << false << false << true
526 << QCanBusFrame::DataFrame;
527 QTest::newRow(dataTag: "fullFrame3") << quint32(123) << QByteArray("abcde3")
528 << qint64(458) << qint64(786)
529 << true << false << false << false << false
530 << QCanBusFrame::RemoteRequestFrame;
531 QTest::newRow(dataTag: "fullFrame4") << quint32(123) << QByteArray("abcde4")
532 << qint64(459) << qint64(787)
533 << false << false << false << false << false
534 << QCanBusFrame::RemoteRequestFrame;
535 QTest::newRow(dataTag: "fullFrame5") << quint32(123) << QByteArray("abcde5")
536 << qint64(460) << qint64(789)
537 << true << false << false << false << false
538 << QCanBusFrame::ErrorFrame;
539 QTest::newRow(dataTag: "fullFrame6") << quint32(123) << QByteArray("abcde6")
540 << qint64(453) << qint64(788)
541 << false << false << false << false << false
542 << QCanBusFrame::ErrorFrame;
543}
544
545void tst_QCanBusFrame::streaming()
546{
547 QFETCH(quint32, frameId);
548 QFETCH(QByteArray, payload);
549 QFETCH(qint64, seconds);
550 QFETCH(qint64, microSeconds);
551 QFETCH(bool, isExtended);
552 QFETCH(bool, isFlexibleDataRate);
553 QFETCH(bool, isBitrateSwitch);
554 QFETCH(bool, isErrorStateIndicator);
555 QFETCH(bool, isLocalEcho);
556 QFETCH(QCanBusFrame::FrameType, frameType);
557
558 QCanBusFrame originalFrame(frameId, payload);
559 const QCanBusFrame::TimeStamp originalStamp(seconds, microSeconds);
560 originalFrame.setTimeStamp(originalStamp);
561
562 originalFrame.setExtendedFrameFormat(isExtended);
563 originalFrame.setFlexibleDataRateFormat(isFlexibleDataRate);
564 originalFrame.setBitrateSwitch(isBitrateSwitch);
565 originalFrame.setErrorStateIndicator(isErrorStateIndicator);
566 originalFrame.setLocalEcho(isLocalEcho);
567 originalFrame.setFrameType(frameType);
568
569 QByteArray buffer;
570 QDataStream out(&buffer, QIODevice::WriteOnly);
571 out << originalFrame;
572
573 QDataStream in(buffer);
574 QCanBusFrame restoredFrame;
575 in >> restoredFrame;
576 const QCanBusFrame::TimeStamp restoredStamp(restoredFrame.timeStamp());
577
578 QCOMPARE(restoredFrame.frameId(), originalFrame.frameId());
579 QCOMPARE(restoredFrame.payload(), originalFrame.payload());
580
581 QCOMPARE(restoredStamp.seconds(), originalStamp.seconds());
582 QCOMPARE(restoredStamp.microSeconds(), originalStamp.microSeconds());
583
584 QCOMPARE(restoredFrame.frameType(), originalFrame.frameType());
585 QCOMPARE(restoredFrame.hasExtendedFrameFormat(),
586 originalFrame.hasExtendedFrameFormat());
587 QCOMPARE(restoredFrame.hasFlexibleDataRateFormat(),
588 originalFrame.hasFlexibleDataRateFormat());
589 QCOMPARE(restoredFrame.hasBitrateSwitch(),
590 originalFrame.hasBitrateSwitch());
591 QCOMPARE(restoredFrame.hasErrorStateIndicator(),
592 originalFrame.hasErrorStateIndicator());
593 QCOMPARE(restoredFrame.hasLocalEcho(),
594 originalFrame.hasLocalEcho());
595}
596
597void tst_QCanBusFrame::tst_error()
598{
599 QCanBusFrame frame(1, QByteArray());
600 QCOMPARE(frame.frameType(), QCanBusFrame::DataFrame);
601 QCOMPARE(frame.frameId(), 1u);
602 QCOMPARE(frame.error(), QCanBusFrame::NoError);
603
604 //set error -> should be ignored since still DataFrame
605 frame.setError(QCanBusFrame::AnyError);
606 QCOMPARE(frame.frameType(), QCanBusFrame::DataFrame);
607 QCOMPARE(frame.frameId(), 1u);
608 QCOMPARE(frame.error(), QCanBusFrame::NoError);
609
610 frame.setFrameType(QCanBusFrame::ErrorFrame);
611 QCOMPARE(frame.frameType(), QCanBusFrame::ErrorFrame);
612 QCOMPARE(frame.frameId(), 0u); //id of Error frame always 0
613 QCOMPARE(frame.error(), QCanBusFrame::TransmissionTimeoutError);
614
615 frame.setError(QCanBusFrame::FrameErrors(QCanBusFrame::ControllerError | QCanBusFrame::ProtocolViolationError));
616 QCOMPARE(frame.frameType(), QCanBusFrame::ErrorFrame);
617 QCOMPARE(frame.frameId(), 0u); //id of Error frame always 0
618 QCOMPARE(frame.error(),
619 QCanBusFrame::ControllerError|QCanBusFrame::ProtocolViolationError);
620
621 frame.setFrameType(QCanBusFrame::RemoteRequestFrame);
622 QCOMPARE(frame.frameType(), QCanBusFrame::RemoteRequestFrame);
623 QCOMPARE(frame.frameId(), uint(QCanBusFrame::ControllerError | QCanBusFrame::ProtocolViolationError));
624 QCOMPARE(frame.error(), QCanBusFrame::NoError);
625}
626
627QTEST_MAIN(tst_QCanBusFrame)
628
629#include "tst_qcanbusframe.moc"
630

source code of qtserialbus/tests/auto/qcanbusframe/tst_qcanbusframe.cpp