summaryrefslogtreecommitdiffstats
path: root/src/plugins/canbus/virtualcan/virtualcanbackend.cpp
blob: ce1ca3e998743a8a2f5dde8bb05ecac22b6d4183 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/****************************************************************************
**
** Copyright (C) 2018 Andre Hartmann <aha_1980@gmx.de>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtSerialBus module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "virtualcanbackend.h"

#include <QtCore/qdatetime.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qregularexpression.h>

#include <QtNetwork/qtcpserver.h>
#include <QtNetwork/qtcpsocket.h>

QT_BEGIN_NAMESPACE

Q_DECLARE_LOGGING_CATEGORY(QT_CANBUS_PLUGINS_VIRTUALCAN)

enum {
    ServerDefaultTcpPort = 35468,
    VirtualChannels = 2
};

static const char RemoteRequestFlag    = 'R';
static const char ExtendedFormatFlag   = 'X';
static const char FlexibleDataRateFlag = 'F';
static const char BitRateSwitchFlag    = 'B';
static const char ErrorStateFlag       = 'E';
static const char LocalEchoFlag        = 'L';

VirtualCanServer::VirtualCanServer(QObject *parent)
    : QObject(parent)
{
    qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Server [%p] constructed.", this);
}

VirtualCanServer::~VirtualCanServer()
{
    qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Server [%p] destructed.", this);
}

void VirtualCanServer::start(quint16 port)
{
    // If there is already a server object, return immediately
    if (m_server) {
        qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN, "Server [%p] is already running.", this);
        return;
    }

    // Otherwise try to start a new server. If there is already
    // another server listen on the specified port, give up.
    m_server = new QTcpServer(this);
    if (!m_server->listen(QHostAddress::LocalHost, port)) {
        qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN,
               "Server [%p] could not be started, port %d is already in use.", this, port);
        m_server->deleteLater();
        m_server = nullptr;
        return;
    }

    // Server successfully started
    connect(m_server, &QTcpServer::newConnection, this, &VirtualCanServer::connected);
    qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN,
            "Server [%p] started and listening on port %d.", this, port);
    return;
}

void VirtualCanServer::connected()
{
    while (m_server->hasPendingConnections()) {
        qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN, "Server [%p] client connected.", this);
        QTcpSocket *next = m_server->nextPendingConnection();
        m_serverSockets.append(next);
        connect(next, &QIODevice::readyRead, this, &VirtualCanServer::readyRead);
        connect(next, &QTcpSocket::disconnected, this, &VirtualCanServer::disconnected);
    }
}

void VirtualCanServer::disconnected()
{
    qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN, "Server [%p] client disconnected.", this);

    auto socket = qobject_cast<QTcpSocket *>(sender());
    Q_ASSERT(socket);

    m_serverSockets.removeOne(socket);
    socket->deleteLater();
}

void VirtualCanServer::readyRead()
{
    auto readSocket = qobject_cast<QTcpSocket *>(sender());
    Q_ASSERT(readSocket);

    while (readSocket->canReadLine()) {
        const QByteArray command = readSocket->readLine().trimmed();
        qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN,
                "Server [%p] received: '%s'.", this, command.constData());

        if (command.startsWith("connect:")) {
            const QVariant interfaces = readSocket->property("interfaces");
            QStringList list = interfaces.toStringList();
            list.append(command.mid(int(strlen("connect:"))));
            readSocket->setProperty("interfaces", list);

        } else if (command.startsWith("disconnect:")) {
            const QVariant interfaces = readSocket->property("interfaces");
            QStringList list = interfaces.toStringList();
            list.removeAll(command.mid(int(strlen("disconnect:"))));
            readSocket->setProperty("interfaces", list);
            readSocket->disconnectFromHost();

        } else {
            const QByteArrayList commandList = command.split(':');
            Q_ASSERT(commandList.size() == 2);

            for (QTcpSocket *writeSocket : qAsConst(m_serverSockets)) {
                // Don't send the frame back to its origin
                if (writeSocket == readSocket)
                    continue;

                // Send frame to all clients registered to the same interface as sender
                const QVariant property = writeSocket->property("interfaces");
                if (!property.isValid())
                    continue;

                const QStringList propertyList = property.toStringList();
                if (propertyList.contains(commandList.first()))
                    writeSocket->write(commandList.last() + '\n');
            }
        }
    }
}

Q_GLOBAL_STATIC(VirtualCanServer, g_server)

VirtualCanBackend::VirtualCanBackend(const QString &interface, QObject *parent)
    : QCanBusDevice(parent)
{
    m_url = QUrl(interface);
    const QString canDevice = m_url.fileName();

    const QRegularExpression re(QStringLiteral("can(\\d)"));
    const QRegularExpressionMatch match = re.match(canDevice);

    if (Q_UNLIKELY(!match.hasMatch())) {
        qCWarning(QT_CANBUS_PLUGINS_VIRTUALCAN,
                "Invalid interface '%ls'.", qUtf16Printable(interface));
        setError(tr("Invalid interface '%1'.").arg(interface), QCanBusDevice::ConnectionError);
        return;
    }

    const uint channel = match.captured(1).toUInt();
    if (Q_UNLIKELY(channel >= VirtualChannels)) {
        qCWarning(QT_CANBUS_PLUGINS_VIRTUALCAN,
                "Invalid interface '%ls'.", qUtf16Printable(interface));
        setError(tr("Invalid interface '%1'.").arg(interface), QCanBusDevice::ConnectionError);
        return;
    }

    m_channel = channel;
}

VirtualCanBackend::~VirtualCanBackend()
{
    qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] socket destructed.", this);
}

bool VirtualCanBackend::open()
{
    setState(QCanBusDevice::ConnectingState);

    const QString host = m_url.host();
    const QHostAddress address = host.isEmpty() ? QHostAddress::LocalHost : QHostAddress(host);
    const quint16 port = static_cast<quint16>(m_url.port(ServerDefaultTcpPort));

    if (address.isLoopback())
        g_server->start(port);

    m_clientSocket = new QTcpSocket(this);
    m_clientSocket->connectToHost(address, port, QIODevice::ReadWrite);
    connect(m_clientSocket, &QAbstractSocket::connected, this, &VirtualCanBackend::clientConnected);
    connect(m_clientSocket, &QAbstractSocket::disconnected, this, &VirtualCanBackend::clientDisconnected);
    connect(m_clientSocket, &QIODevice::readyRead, this, &VirtualCanBackend::clientReadyRead);
    qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] socket created.", this);
    return true;
}

void VirtualCanBackend::close()
{
    qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] sends disconnect to server.", this);

    m_clientSocket->write("disconnect:can" + QByteArray::number(m_channel) + '\n');
}

void VirtualCanBackend::setConfigurationParameter(int key, const QVariant &value)
{
    if (key == QCanBusDevice::ReceiveOwnKey || key == QCanBusDevice::CanFdKey)
        QCanBusDevice::setConfigurationParameter(key, value);
}

/*
    Protocol format: All data is in ASCII, one CAN message per line,
    each line ends with line feed '\n'.

    Format:  "<CAN-Channel>:<Flags>#<CAN-ID>#<Data-Bytes>\n"
    Example: "can0:XF#123#123456\n"

    The first part is the destination CAN channel, "can0" or "can1",
    followed by the flags list:

    * R - Remote Request
    * X - Extended Frame Format
    * F - Flexible Data Rate Format
    * B - Bitrate Switch
    * E - Error State Indicator
    * L - Local Echo

    Afterwards the CAN-ID and the data follows, both separated by '#'.
*/

bool VirtualCanBackend::writeFrame(const QCanBusFrame &frame)
{
    if (Q_UNLIKELY(state() != ConnectedState)) {
        qCWarning(QT_CANBUS_PLUGINS_VIRTUALCAN, "Error: Cannot write frame as client is not connected!");
        return false;
    }

    bool canFdEnabled = configurationParameter(QCanBusDevice::CanFdKey).toBool();
    if (Q_UNLIKELY(frame.hasFlexibleDataRateFormat() && !canFdEnabled)) {
        qCWarning(QT_CANBUS_PLUGINS_VIRTUALCAN,
                "Error: Cannot write CAN FD frame as CAN FD is not enabled!");
        return false;
    }

    QByteArray flags;
    if (frame.frameType() == QCanBusFrame::RemoteRequestFrame)
        flags.append(RemoteRequestFlag);
    if (frame.hasExtendedFrameFormat())
        flags.append(ExtendedFormatFlag);
    if (frame.hasFlexibleDataRateFormat())
        flags.append(FlexibleDataRateFlag);
    if (frame.hasBitrateSwitch())
        flags.append(BitRateSwitchFlag);
    if (frame.hasErrorStateIndicator())
        flags.append(ErrorStateFlag);
    if (frame.hasLocalEcho())
        flags.append(LocalEchoFlag);
    const QByteArray frameId = QByteArray::number(frame.frameId());
    const QByteArray command = "can" + QByteArray::number(m_channel)
            + ':' + frameId + '#' + flags + '#' + frame.payload().toHex() + '\n';
    m_clientSocket->write(command);

    if (configurationParameter(QCanBusDevice::ReceiveOwnKey).toBool()) {
        const qint64 timeStamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
        QCanBusFrame echoFrame = frame;
        echoFrame.setLocalEcho(true);
        echoFrame.setTimeStamp(QCanBusFrame::TimeStamp::fromMicroSeconds(timeStamp * 1000));
        enqueueReceivedFrames({echoFrame});
    }

    emit framesWritten(qint64(1));
    return true;
}

QString VirtualCanBackend::interpretErrorFrame(const QCanBusFrame &errorFrame)
{
    Q_UNUSED(errorFrame);
    return QString();
}

QList<QCanBusDeviceInfo> VirtualCanBackend::interfaces()
{
    QList<QCanBusDeviceInfo> result;

    for (int channel = 0; channel < VirtualChannels; ++channel) {
        result.append(std::move(createDeviceInfo(
                                    QStringLiteral("can%1").arg(channel), QString(),
                                    QStringLiteral("Qt Virtual CAN bus"), channel,
                                    true, true)));
    }

    return result;
}

void VirtualCanBackend::clientConnected()
{
    qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] socket connected.", this);
    m_clientSocket->write("connect:can" + QByteArray::number(m_channel) + '\n');

    setState(QCanBusDevice::ConnectedState);
}

void VirtualCanBackend::clientDisconnected()
{
    qCInfo(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] socket disconnected.", this);

    setState(UnconnectedState);
}

void VirtualCanBackend::clientReadyRead()
{
    while (m_clientSocket->canReadLine()) {
        const QByteArray answer = m_clientSocket->readLine().trimmed();
        qCDebug(QT_CANBUS_PLUGINS_VIRTUALCAN, "Client [%p] received: '%s'.",
                this, answer.constData());

        if (answer.startsWith("disconnect:can" + QByteArray::number(m_channel))) {
            m_clientSocket->disconnectFromHost();
            continue;
        }

        const QByteArrayList list = answer.split('#');
        Q_ASSERT(list.size() == 3);

        const quint32 id = list.at(0).toUInt();
        const QByteArray flags = list.at(1);
        const QByteArray data = QByteArray::fromHex(list.at(2));
        const qint64 timeStamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
        QCanBusFrame frame(id, data);
        frame.setTimeStamp(QCanBusFrame::TimeStamp::fromMicroSeconds(timeStamp * 1000));
        if (flags.contains(RemoteRequestFlag))
            frame.setFrameType(QCanBusFrame::RemoteRequestFrame);
        frame.setExtendedFrameFormat(flags.contains(ExtendedFormatFlag));
        frame.setFlexibleDataRateFormat(flags.contains(FlexibleDataRateFlag));
        frame.setBitrateSwitch(flags.contains(BitRateSwitchFlag));
        frame.setErrorStateIndicator(flags.contains(ErrorStateFlag));
        frame.setLocalEcho(flags.contains(LocalEchoFlag));
        enqueueReceivedFrames({frame});
    }
}

QT_END_NAMESPACE