-
Notifications
You must be signed in to change notification settings - Fork 11
/
QtCannelloniCanBusPlugin.cpp
48 lines (44 loc) · 1.42 KB
/
QtCannelloniCanBusPlugin.cpp
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
#include "CannelloniCanBackend.h"
#include <QCanBusFactory>
#include <QObject>
#include <QStringList>
#include <limits>
class QtCannelloniCanBusPlugin : public QObject, public QCanBusFactory
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QCanBusFactory" FILE "plugin.json")
Q_INTERFACES(QCanBusFactory)
public:
QCanBusDevice* createDevice(const QString& interfaceName,
QString* errorMessage) const override
{
auto tokens = interfaceName.split(QChar(','));
if (tokens.size() != 3)
{
*errorMessage = "Invalid interface name format";
return nullptr;
}
bool ok;
const auto quint16max = std::numeric_limits<quint16>::max();
auto localPort = tokens[0].toUInt(&ok);
if (!ok || localPort > quint16max)
{
*errorMessage = "Invalid local port format";
return nullptr;
}
QHostAddress remoteAddr(tokens[1]);
if (remoteAddr.isNull())
{
*errorMessage = "Invalid remote address format";
return nullptr;
}
auto remotePort = tokens[2].toUInt(&ok);
if (!ok || remotePort > quint16max)
{
*errorMessage = "Invalid remote port format";
return nullptr;
}
return new CannelloniCanBackend(localPort, remoteAddr, remotePort);
}
};
#include "QtCannelloniCanBusPlugin.moc"