1// Copyright (C) 2017 Ford Motor Company.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef PASSTHRUCAN_PASSTHRUCANIO_H
5#define PASSTHRUCAN_PASSTHRUCANIO_H
6
7#include "j2534passthru.h"
8
9#include <QtSerialBus/qcanbusdevice.h>
10#include <QtSerialBus/qcanbusframe.h>
11#include <QByteArray>
12#include <QList>
13#include <QMutex>
14#include <QObject>
15#include <QString>
16#include <QTimer>
17#include <QVariant>
18
19QT_BEGIN_NAMESPACE
20
21class PassThruCanIO : public QObject
22{
23 Q_OBJECT
24 Q_DISABLE_COPY(PassThruCanIO)
25public:
26 static const uint pollTimeout = 100; // ms
27
28 explicit PassThruCanIO(QObject *parent = nullptr);
29 virtual ~PassThruCanIO();
30
31 Q_INVOKABLE void open(const QString &library, const QByteArray &subDev, uint bitRate);
32 Q_INVOKABLE void close();
33 Q_INVOKABLE void applyConfig(QCanBusDevice::ConfigurationKey key, const QVariant &value);
34 Q_INVOKABLE void listen();
35
36 // Internally locked; safe to call directly from any thread.
37 bool enqueueMessage(const QCanBusFrame &frame);
38
39Q_SIGNALS:
40 void errorOccurred(const QString &description, QCanBusDevice::CanBusError error);
41 void messagesReceived(QList<QCanBusFrame> frames);
42 void messagesSent(qint64 count);
43 void openFinished(bool success);
44 void closeFinished();
45
46private:
47 bool setMessageFilters(const QList<QCanBusDevice::Filter> &filters);
48 bool setConfigValue(J2534::Config::Parameter param, ulong value);
49 void pollForMessages();
50 bool writeMessages();
51 void readMessages(bool writePending);
52
53 J2534::PassThru * m_passThru = nullptr;
54 J2534::PassThru::Handle m_deviceId = 0;
55 J2534::PassThru::Handle m_channelId = 0;
56 QTimer * m_idleNotifier = nullptr;
57 QList<J2534::Message> m_ioBuffer;
58 QMutex m_writeGuard;
59 QList<QCanBusFrame> m_writeQueue;
60};
61
62QT_END_NAMESPACE
63
64#endif // PASSTHRUCAN_PASSTHRUCANIO_H
65

source code of qtserialbus/src/plugins/canbus/passthrucan/passthrucanio.h