1// Copyright (C) 2018 Andre Hartmann <aha_1980@gmx.de>
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 VIRTUALCANBACKEND_H
5#define VIRTUALCANBACKEND_H
6
7#include <QtSerialBus/qcanbusdevice.h>
8#include <QtSerialBus/qcanbusdeviceinfo.h>
9#include <QtSerialBus/qcanbusframe.h>
10
11#include <QtCore/qlist.h>
12#include <QtCore/qurl.h>
13#include <QtCore/qvariant.h>
14
15QT_BEGIN_NAMESPACE
16
17class QTcpServer;
18class QTcpSocket;
19
20class VirtualCanServer : public QObject
21{
22 Q_OBJECT
23 Q_DISABLE_COPY(VirtualCanServer)
24
25public:
26 explicit VirtualCanServer(QObject *parent = nullptr);
27 ~VirtualCanServer() override;
28
29 void start(quint16 port);
30
31private:
32 void connected();
33 void disconnected();
34 void readyRead();
35
36 QTcpServer *m_server = nullptr;
37 QList<QTcpSocket *> m_serverSockets;
38};
39
40class VirtualCanBackend : public QCanBusDevice
41{
42 Q_OBJECT
43 Q_DISABLE_COPY(VirtualCanBackend)
44
45public:
46 explicit VirtualCanBackend(const QString &interface, QObject *parent = nullptr);
47 ~VirtualCanBackend() override;
48
49 bool open() override;
50 void close() override;
51
52 void setConfigurationParameter(ConfigurationKey key, const QVariant &value) override;
53
54 bool writeFrame(const QCanBusFrame &frame) override;
55
56 QString interpretErrorFrame(const QCanBusFrame &errorFrame) override;
57
58 static QList<QCanBusDeviceInfo> interfaces();
59
60 QCanBusDeviceInfo deviceInfo() const override;
61
62private:
63 static QCanBusDeviceInfo virtualCanDeviceInfo(uint channel);
64
65 void clientConnected();
66 void clientDisconnected();
67 void clientReadyRead();
68
69 QUrl m_url;
70 uint m_channel = 0;
71 QTcpSocket *m_clientSocket = nullptr;
72};
73
74QT_END_NAMESPACE
75
76#endif // VIRTUALCANBACKEND_H
77

source code of qtserialbus/src/plugins/canbus/virtualcan/virtualcanbackend.h