summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@theqtcompany.com>2015-08-28 13:20:11 +0200
committerAlex Blasche <alexander.blasche@theqtcompany.com>2015-08-31 09:17:26 +0000
commitb47d4384f20dcb5a9331eb823e52986bd5db52d1 (patch)
treec1a17bf4120fc38be2e50cf5bf7d22f94b5cdb10
parent0982955739c6b98b9dbe63efb6575cbf01aee1ec (diff)
Adjust config param handling in socketcan
The configuration parameter can be applied before and after a connect. So far the example application always handled the parameters after a connect. The recent can example changes set the params before connect. This lead to the result that the new params were ignored. This change makes it possible to store the params until they are needed. In the case of socketcan the params can only set after the connect when the can bus socket becomes valid. Change-Id: I983fa7d41a63d8f806d0510b4c013d967f039d51 Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com> Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
-rw-r--r--src/plugins/canbus/socketcan/socketcanbackend.cpp75
-rw-r--r--src/plugins/canbus/socketcan/socketcanbackend.h1
2 files changed, 59 insertions, 17 deletions
diff --git a/src/plugins/canbus/socketcan/socketcanbackend.cpp b/src/plugins/canbus/socketcan/socketcanbackend.cpp
index c750ec6..789a80a 100644
--- a/src/plugins/canbus/socketcan/socketcanbackend.cpp
+++ b/src/plugins/canbus/socketcan/socketcanbackend.cpp
@@ -71,8 +71,6 @@ void SocketCanBackend::resetConfigurations()
QCanBusDevice::ReceiveOwnKey, false);
QCanBusDevice::setConfigurationParameter(
QCanBusDevice::ErrorFilterKey, QCanBusFrame::NoError);
- QCanBusDevice::setConfigurationParameter(
- QCanBusDevice::RawFilterKey, QList<QVariant>());
}
bool SocketCanBackend::open()
@@ -96,39 +94,57 @@ void SocketCanBackend::close()
setState(QCanBusDevice::UnconnectedState);
}
-void SocketCanBackend::setConfigurationParameter(int key, const QVariant &value)
+bool SocketCanBackend::applyConfigurationParameter(int key, const QVariant &value)
{
+ bool success = false;
- if (key == QCanBusDevice::LoopbackKey) {
+ switch (key) {
+ case QCanBusDevice::LoopbackKey:
+ {
const int loopback = value.toBool() ? 1 : 0;
if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback)) < 0) {
setError(qt_error_string(errno),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
- } else if (key == QCanBusDevice::ReceiveOwnKey) {
+ success = true;
+ break;
+ }
+ case QCanBusDevice::ReceiveOwnKey:
+ {
const int receiveOwnMessages = value.toBool() ? 1 : 0;
if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS,
&receiveOwnMessages, sizeof(receiveOwnMessages)) < 0) {
setError(qt_error_string(errno),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
- } else if (key == QCanBusDevice::ErrorFilterKey) {
+ success = true;
+ break;
+ }
+ case QCanBusDevice::ErrorFilterKey:
+ {
const int errorMask = value.value<QCanBusFrame::FrameErrors>();
if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
&errorMask, sizeof(errorMask)) < 0) {
setError(qt_error_string(errno),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
- } else if (key == QCanBusDevice::RawFilterKey) {
+ success = true;
+ break;
+ }
+ case QCanBusDevice::RawFilterKey:
+ {
const QList<QVariant> filterList = value.toList();
const int size = filterList.size();
- if (size == 0)
+ if (!size) {
setError(QStringLiteral("ERROR SocketCanBackend: \"CanFilter\" "
"QList<QVariant> empty or not valid"),
QCanBusDevice::CanBusError::ConfigurationError);
+ break;
+ }
+
can_filter filters[size];
for (int i = 0; i < size; i++) {
can_filter filter;
@@ -140,7 +156,7 @@ void SocketCanBackend::setConfigurationParameter(int key, const QVariant &value)
"FilterId key not found or value is not valid in index: ")
+ QString::number(i),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
filter.can_mask = filterHash.value("CanMask").toInt(&ok);
if (!ok) {
@@ -148,21 +164,27 @@ void SocketCanBackend::setConfigurationParameter(int key, const QVariant &value)
"CanMask key not found or value is not valid in index:")
+ QString::number(i),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
filters[i] = filter;
}
- if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, filters, sizeof(filters)) < 0)
+ if (setsockopt(canSocket, SOL_CAN_RAW, CAN_RAW_FILTER, filters, sizeof(filters)) < 0) {
setError(qt_error_string(errno),
QCanBusDevice::CanBusError::ConfigurationError);
- } else {
+ break;
+ }
+ success = true;
+ break;
+ }
+ default:
setError(QStringLiteral("SocketCanBackend: No such configuration as")
+ key + QStringLiteral("in SocketCanBackend"),
QCanBusDevice::CanBusError::ConfigurationError);
- return;
+ break;
}
- QCanBusDevice::setConfigurationParameter(key, value);
+ qDebug() << "applyConfiguration" << key << value << success;
+ return success;
}
bool SocketCanBackend::connectSocket()
@@ -206,9 +228,28 @@ bool SocketCanBackend::connectSocket()
connect(notifier.data(), &QSocketNotifier::activated,
this, &SocketCanBackend::readSocket);
+ //apply all stored configurations
+ foreach (int key, configurationKeys()) {
+ const QVariant param = configurationParameter(key);
+ bool success = applyConfigurationParameter(key, param);
+ if (!success) {
+ qWarning() << "Cannot apply parameter:" << key
+ << "with value:" << param;
+ }
+ }
+
return true;
}
+void SocketCanBackend::setConfigurationParameter(int key, const QVariant &value)
+{
+ // connected & params not applyable/invalid
+ if (canSocket != -1 && !applyConfigurationParameter(key, value))
+ return;
+
+ QCanBusDevice::setConfigurationParameter(key, value);
+}
+
bool SocketCanBackend::writeFrame(const QCanBusFrame &newData)
{
if (state() != ConnectedState)
diff --git a/src/plugins/canbus/socketcan/socketcanbackend.h b/src/plugins/canbus/socketcan/socketcanbackend.h
index 0c74027..f11c536 100644
--- a/src/plugins/canbus/socketcan/socketcanbackend.h
+++ b/src/plugins/canbus/socketcan/socketcanbackend.h
@@ -73,6 +73,7 @@ private Q_SLOTS:
private:
void resetConfigurations();
bool connectSocket();
+ bool applyConfigurationParameter(int key, const QVariant &value);
qint64 canSocket;
QPointer<QSocketNotifier> notifier;