1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the QtSerialBus module.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "canbusutil.h"
38#include "sigtermhandler.h"
39
40#include <QCommandLineParser>
41#include <QCoreApplication>
42#include <QTextStream>
43#include <QScopedPointer>
44
45#include <signal.h>
46
47int main(int argc, char *argv[])
48{
49 QCoreApplication app(argc, argv);
50 QCoreApplication::setApplicationName(QStringLiteral("canbusutil"));
51 QCoreApplication::setApplicationVersion(QStringLiteral(QT_VERSION_STR));
52
53 QScopedPointer<SigTermHandler> s(SigTermHandler::instance());
54 if (signal(SIGINT, handler: SigTermHandler::handle) == SIG_ERR)
55 return -1;
56 QObject::connect(sender: s.data(), signal: &SigTermHandler::sigTermSignal, context: &app, slot: &QCoreApplication::quit);
57
58 QTextStream output(stdout);
59 CanBusUtil util(output, app);
60
61 QCommandLineParser parser;
62 parser.setApplicationDescription(CanBusUtil::tr(
63 s: "Sends arbitrary CAN bus frames.\n"
64 "If the -l option is set, all received CAN bus frames are dumped."));
65 parser.addHelpOption();
66 parser.addVersionOption();
67
68 parser.addPositionalArgument(QStringLiteral("plugin"),
69 description: CanBusUtil::tr(s: "Plugin name to use. See --list-plugins."));
70
71 parser.addPositionalArgument(QStringLiteral("device"),
72 description: CanBusUtil::tr(s: "Device to use."));
73
74 parser.addPositionalArgument(QStringLiteral("data"),
75 description: CanBusUtil::tr(
76 s: "Data to send if -l is not specified. Format:\n"
77 "\t\t<id>#{payload} (CAN 2.0 data frames),\n"
78 "\t\t<id>#Rxx (CAN 2.0 RTR frames with xx bytes data length),\n"
79 "\t\t<id>##[flags]{payload} (CAN FD data frames),\n"
80 "where {payload} has 0..8 (0..64 CAN FD) ASCII hex-value pairs, "
81 "and flags is one optional ASCII hex char for CAN FD flags: "
82 "1 = Bitrate Switch, 2 = Error State Indicator\n"
83 "e.g. 1#1a2b3c\n"), QStringLiteral("[data]"));
84
85 const QCommandLineOption listeningOption({"l", "listen"},
86 CanBusUtil::tr(s: "Start listening CAN data on device."));
87 parser.addOption(commandLineOption: listeningOption);
88
89 const QCommandLineOption listOption({"L", "list-plugins"},
90 CanBusUtil::tr(s: "List all available plugins."));
91 parser.addOption(commandLineOption: listOption);
92
93 const QCommandLineOption showTimeStampOption({"t", "timestamp"},
94 CanBusUtil::tr(s: "Show timestamp for each received CAN bus frame."));
95 parser.addOption(commandLineOption: showTimeStampOption);
96
97 const QCommandLineOption showFlagsOption({"i", "info"},
98 CanBusUtil::tr(s: "Show flags bitrate switch, error indicator, and local echo"
99 " for each received CAN bus frame."));
100 parser.addOption(commandLineOption: showFlagsOption);
101
102 const QCommandLineOption listDevicesOption({"d", "devices"},
103 CanBusUtil::tr(s: "Show available CAN bus devices for the given plugin."));
104 parser.addOption(commandLineOption: listDevicesOption);
105
106 const QCommandLineOption canFdOption({"f", "can-fd"},
107 CanBusUtil::tr(s: "Enable CAN FD functionality when listening."));
108 parser.addOption(commandLineOption: canFdOption);
109
110 const QCommandLineOption loopbackOption({"c", "local-loopback"},
111 CanBusUtil::tr(s: "Transmits all sent frames to other local applications."));
112 parser.addOption(commandLineOption: loopbackOption);
113
114 const QCommandLineOption receiveOwnOption({"o", "receive-own"},
115 CanBusUtil::tr(s: "Receive each sent frame on successful transmission."));
116 parser.addOption(commandLineOption: receiveOwnOption);
117
118 const QCommandLineOption bitrateOption({"b", "bitrate"},
119 CanBusUtil::tr(s: "Set the CAN bus bitrate to the given value."),
120 QStringLiteral("bitrate"));
121 parser.addOption(commandLineOption: bitrateOption);
122
123 const QCommandLineOption dataBitrateOption({"a", "data-bitrate"},
124 CanBusUtil::tr(s: "Set the CAN FD data bitrate to the given value."),
125 QStringLiteral("bitrate"));
126 parser.addOption(commandLineOption: dataBitrateOption);
127
128 parser.process(app);
129
130 if (parser.isSet(option: listOption))
131 return util.printPlugins();
132
133 QString data;
134 const QStringList args = parser.positionalArguments();
135
136 if (parser.isSet(option: canFdOption))
137 util.setConfigurationParameter(key: QCanBusDevice::CanFdKey, value: true);
138 if (parser.isSet(option: loopbackOption))
139 util.setConfigurationParameter(key: QCanBusDevice::LoopbackKey, value: true);
140 if (parser.isSet(option: receiveOwnOption))
141 util.setConfigurationParameter(key: QCanBusDevice::ReceiveOwnKey, value: true);
142 if (!parser.value(option: bitrateOption).isEmpty()) {
143 util.setConfigurationParameter(key: QCanBusDevice::BitRateKey,
144 value: parser.value(option: bitrateOption).toInt());
145 }
146 if (!parser.value(option: dataBitrateOption).isEmpty()) {
147 util.setConfigurationParameter(key: QCanBusDevice::DataBitRateKey,
148 value: parser.value(option: dataBitrateOption).toInt());
149 }
150
151 if (parser.isSet(option: listeningOption)) {
152 util.setShowTimeStamp(parser.isSet(option: showTimeStampOption));
153 util.setShowFlags(parser.isSet(option: showFlagsOption));
154 } else if (args.size() == 3) {
155 data = args.at(i: 2);
156 } else if (args.size() == 1 && parser.isSet(option: listDevicesOption)) {
157 return util.printDevices(pluginName: args.at(i: 0));
158 }
159
160 if (args.size() < 2 || args.size() > 3) {
161 output << CanBusUtil::tr(s: "Invalid number of arguments (%1 given).").arg(a: args.size());
162 output << Qt::endl << Qt::endl << parser.helpText();
163 return 1;
164 }
165
166 if (!util.start(pluginName: args.at(i: 0), deviceName: args.at(i: 1), data))
167 return -1;
168
169 return app.exec();
170}
171

source code of qtserialbus/src/tools/canbusutil/main.cpp