summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2019-07-15 15:44:50 +0200
committerAndre Hartmann <aha_1980@gmx.de>2019-07-24 10:57:36 +0200
commit9f5b8ba590d424f82498fca5f2cc8594b1757ff0 (patch)
tree5489ac4be297bb4576f37d5d0c401529e5398297
parente39d9396428c3d078313b50fa1ff65662096debc (diff)
SocketCAN: Allow specifying the protocol to use
So far, only CAN_RAW was allowed, but there might be cases where other protocols are useful too. The default value is still CAN_RAW, so all existing applications will continue working. [ChangeLog][SocketCAN] Added the configuration parameter QCanBusDevice::ProtocolKey to use another protocol inside the protocol family PF_CAN. Fixes: QTBUG-75204 Change-Id: I8323ab8d77a569f51d3cd8c296b433352a3f5609 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/plugins/canbus/socketcan/socketcanbackend.cpp12
-rw-r--r--src/plugins/canbus/socketcan/socketcanbackend.h1
-rw-r--r--src/serialbus/doc/src/socketcan.qdoc4
-rw-r--r--src/serialbus/qcanbusdevice.cpp3
-rw-r--r--src/serialbus/qcanbusdevice.h1
5 files changed, 20 insertions, 1 deletions
diff --git a/src/plugins/canbus/socketcan/socketcanbackend.cpp b/src/plugins/canbus/socketcan/socketcanbackend.cpp
index 27fa326..2ed1310 100644
--- a/src/plugins/canbus/socketcan/socketcanbackend.cpp
+++ b/src/plugins/canbus/socketcan/socketcanbackend.cpp
@@ -389,7 +389,7 @@ bool SocketCanBackend::connectSocket()
{
struct ifreq interface;
- if (Q_UNLIKELY((canSocket = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, CAN_RAW)) < 0)) {
+ if (Q_UNLIKELY((canSocket = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, protocol)) < 0)) {
setError(qt_error_string(errno),
QCanBusDevice::CanBusError::ConnectionError);
return false;
@@ -464,6 +464,16 @@ void SocketCanBackend::setConfigurationParameter(int key, const QVariant &value)
return;
}
}
+ } else if (key == QCanBusDevice::ProtocolKey) {
+ bool ok = false;
+ const int newProtocol = value.toInt(&ok);
+ if (Q_UNLIKELY(!ok || (newProtocol < 0))) {
+ const QString errorString = tr("Cannot set protocol to value %1.").arg(value.toString());
+ setError(errorString, QCanBusDevice::ConfigurationError);
+ qCWarning(QT_CANBUS_PLUGINS_SOCKETCAN, "%ls", qUtf16Printable(errorString));
+ return;
+ }
+ protocol = newProtocol;
}
// connected & params not applyable/invalid
if (canSocket != -1 && !applyConfigurationParameter(key, value))
diff --git a/src/plugins/canbus/socketcan/socketcanbackend.h b/src/plugins/canbus/socketcan/socketcanbackend.h
index 199401e..0497244 100644
--- a/src/plugins/canbus/socketcan/socketcanbackend.h
+++ b/src/plugins/canbus/socketcan/socketcanbackend.h
@@ -87,6 +87,7 @@ private:
bool hasBusStatus() const;
QCanBusDevice::CanBusStatus busStatus() const;
+ int protocol = CAN_RAW;
canfd_frame m_frame;
sockaddr_can m_address;
msghdr m_msg;
diff --git a/src/serialbus/doc/src/socketcan.qdoc b/src/serialbus/doc/src/socketcan.qdoc
index 58ffa17..e578969 100644
--- a/src/serialbus/doc/src/socketcan.qdoc
+++ b/src/serialbus/doc/src/socketcan.qdoc
@@ -196,6 +196,10 @@
\li This configuration is not supported by the socketcan plugin. However
it is possible to set the data rate when configuring the CAN network interface
using the \c {ip link} command.
+ \row
+ \li QCanBusDevice::ProtocolKey
+ \li Allows to use another protocol inside the protocol family PF_CAN. The default
+ value for this configuration option is CAN_RAW (1).
\endtable
For example:
diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp
index e2188ae..af004d4 100644
--- a/src/serialbus/qcanbusdevice.cpp
+++ b/src/serialbus/qcanbusdevice.cpp
@@ -118,6 +118,9 @@ Q_LOGGING_CATEGORY(QT_CANBUS, "qt.canbus")
after the arbitration phase at the nominal bitrate is finished.
This enum value was introduced in Qt 5.9.
See also \c QCanBusDevice::BitRateKey
+ \value ProtocolKey This key allows to specify another protocol. For now, this
+ parameter can only be set and used in the SocketCAN plugin.
+ This enum value was introduced in Qt 5.14.
\value UserKey This key defines the range where custom keys start. Its most
common purpose is to permit platform-specific configuration
options.
diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h
index 01a5e8c..8457b08 100644
--- a/src/serialbus/qcanbusdevice.h
+++ b/src/serialbus/qcanbusdevice.h
@@ -89,6 +89,7 @@ public:
BitRateKey,
CanFdKey,
DataBitRateKey,
+ ProtocolKey,
UserKey = 30
};
Q_ENUM(ConfigurationKey)