forked from chcbaram/QtWiznetCanBus
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQtWiznetCanBusPlugin.cpp
60 lines (47 loc) · 1.59 KB
/
QtWiznetCanBusPlugin.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
49
50
51
52
53
54
55
56
57
58
59
60
#include "WiznetCanBackend.h"
#include <QCanBusFactory>
#include <QObject>
#include <QStringList>
#include <limits>
#include <iostream>
class QtWiznetCanBusPlugin : public QObject, public QCanBusFactoryV2
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QCanBusFactory" FILE "plugin.json")
Q_INTERFACES(QCanBusFactoryV2)
public:
QList<QCanBusDeviceInfo> availableDevices(QString *errorMessage) const override
{
Q_UNUSED(errorMessage);
return WiznetCanBackend::interfaces();
}
QCanBusDevice* createDevice(const QString& interfaceName,
QString* errorMessage) const override
{
std::cout << "interfaceName " << interfaceName.toStdString().c_str() << std::endl;
auto tokens = interfaceName.split(QChar(':'));
if (tokens.size() != 2)
{
*errorMessage = "Invalid interface name format";
return nullptr;
}
bool ok;
QHostAddress remoteAddr(tokens[0]);
if (remoteAddr.isNull())
{
*errorMessage = "Invalid remote address format";
return nullptr;
}
const auto quint16max = std::numeric_limits<quint16>::max();
auto remotePort = tokens[1].toUInt(&ok);
if (!ok || remotePort > quint16max)
{
*errorMessage = "Invalid remote port format";
return nullptr;
}
auto localPort = remotePort + 1;
qDebug() << remoteAddr.toString();
return new WiznetCanBackend(localPort, remoteAddr, remotePort);
}
};
#include "QtWiznetCanBusPlugin.moc"