summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-19 22:59:08 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-19 22:59:08 +0100
commitf4da422087f77ce9a549ff73250910e5ae217ff0 (patch)
tree239aa732304bc77f6767aad7e17a9c46d8740b1a
parent364430e417600c68133673254b58b4d35608777a (diff)
downloadBobinkQtOpcUa-f4da422087f77ce9a549ff73250910e5ae217ff0.tar.gz
BobinkQtOpcUa-f4da422087f77ce9a549ff73250910e5ae217ff0.zip
Refactor BobinkClient and clean up comments
- Format BobinkAuth.cpp to match GNU style used elsewhere - Replace brittle error name array with QMetaEnum in BobinkClient - Move QOpcUaPkiConfiguration include from header to .cpp - Replace m_discoveryInterval member with file-scope constant - Add doc comments to SecurityMode and SecurityPolicy enums - Comment endpoint selection strategy in handleEndpointsReceived - Add missing QML ids to connectionPage and serverListView
-rw-r--r--demo/Main.qml2
-rw-r--r--src/BobinkAuth.cpp110
-rw-r--r--src/BobinkClient.cpp31
-rw-r--r--src/BobinkClient.h4
4 files changed, 84 insertions, 63 deletions
diff --git a/demo/Main.qml b/demo/Main.qml
index dae415f..9d3e168 100644
--- a/demo/Main.qml
+++ b/demo/Main.qml
@@ -76,6 +76,7 @@ ApplicationWindow {
Layout.fillHeight: true
initialItem: Page {
+ id: connectionPage
Component.onCompleted: {
Bobink.discoveryUrl = discoveryUrlField.text;
Bobink.startDiscovery();
@@ -146,6 +147,7 @@ ApplicationWindow {
}
ListView {
+ id: serverListView
Layout.fillWidth: true
Layout.preferredHeight: 100
clip: true
diff --git a/src/BobinkAuth.cpp b/src/BobinkAuth.cpp
index 03ae2cf..132e4be 100644
--- a/src/BobinkAuth.cpp
+++ b/src/BobinkAuth.cpp
@@ -4,74 +4,98 @@
*/
#include "BobinkAuth.h"
-BobinkAuth::BobinkAuth(QObject *parent)
- : QObject(parent)
+BobinkAuth::BobinkAuth (QObject *parent) : QObject (parent) {}
+
+BobinkAuth::AuthMode
+BobinkAuth::mode () const
{
+ return m_mode;
}
-BobinkAuth::AuthMode BobinkAuth::mode() const { return m_mode; }
-
-void BobinkAuth::setMode(AuthMode mode)
+void
+BobinkAuth::setMode (AuthMode mode)
{
- if (m_mode == mode)
- return;
- m_mode = mode;
- emit modeChanged();
+ if (m_mode == mode)
+ return;
+ m_mode = mode;
+ emit modeChanged ();
}
-QString BobinkAuth::username() const { return m_username; }
+QString
+BobinkAuth::username () const
+{
+ return m_username;
+}
-void BobinkAuth::setUsername(const QString &username)
+void
+BobinkAuth::setUsername (const QString &username)
{
- if (m_username == username)
- return;
- m_username = username;
- emit usernameChanged();
+ if (m_username == username)
+ return;
+ m_username = username;
+ emit usernameChanged ();
}
-QString BobinkAuth::password() const { return m_password; }
+QString
+BobinkAuth::password () const
+{
+ return m_password;
+}
-void BobinkAuth::setPassword(const QString &password)
+void
+BobinkAuth::setPassword (const QString &password)
{
- if (m_password == password)
- return;
- m_password = password;
- emit passwordChanged();
+ if (m_password == password)
+ return;
+ m_password = password;
+ emit passwordChanged ();
}
-QString BobinkAuth::certPath() const { return m_certPath; }
+QString
+BobinkAuth::certPath () const
+{
+ return m_certPath;
+}
-void BobinkAuth::setCertPath(const QString &path)
+void
+BobinkAuth::setCertPath (const QString &path)
{
- if (m_certPath == path)
- return;
- m_certPath = path;
- emit certPathChanged();
+ if (m_certPath == path)
+ return;
+ m_certPath = path;
+ emit certPathChanged ();
}
-QString BobinkAuth::keyPath() const { return m_keyPath; }
+QString
+BobinkAuth::keyPath () const
+{
+ return m_keyPath;
+}
-void BobinkAuth::setKeyPath(const QString &path)
+void
+BobinkAuth::setKeyPath (const QString &path)
{
- if (m_keyPath == path)
- return;
- m_keyPath = path;
- emit keyPathChanged();
+ if (m_keyPath == path)
+ return;
+ m_keyPath = path;
+ emit keyPathChanged ();
}
-QOpcUaAuthenticationInformation BobinkAuth::toAuthenticationInformation() const
+QOpcUaAuthenticationInformation
+BobinkAuth::toAuthenticationInformation () const
{
- QOpcUaAuthenticationInformation info;
- switch (m_mode) {
+ QOpcUaAuthenticationInformation info;
+ switch (m_mode)
+ {
case Anonymous:
- info.setAnonymousAuthentication();
- break;
+ info.setAnonymousAuthentication ();
+ break;
case UserPass:
- info.setUsernameAuthentication(m_username, m_password);
- break;
+ info.setUsernameAuthentication (m_username, m_password);
+ break;
case Certificate:
- info.setCertificateAuthentication(m_certPath, m_keyPath);
- break;
+ info.setCertificateAuthentication (m_certPath, m_keyPath);
+ break;
}
- return info;
+ return info;
}
diff --git a/src/BobinkClient.cpp b/src/BobinkClient.cpp
index 6d1c608..7355a0c 100644
--- a/src/BobinkClient.cpp
+++ b/src/BobinkClient.cpp
@@ -6,11 +6,15 @@
#include "BobinkAuth.h"
#include <QDir>
+#include <QMetaEnum>
+#include <QOpcUaPkiConfiguration>
#include <QOpcUaUserTokenPolicy>
#include <QStandardPaths>
using namespace Qt::Literals::StringLiterals;
+static constexpr qint32 DISCOVERY_INTERVAL = 30000; // ms
+
static QString
defaultPkiDir ()
{
@@ -87,23 +91,13 @@ BobinkClient::setupClient ()
connect (m_client, &QOpcUaClient::errorChanged, this,
[this] (QOpcUaClient::ClientError error)
{
- static const QLatin1StringView names[] = {
- "NoError"_L1,
- "InvalidUrl"_L1,
- "AccessDenied"_L1,
- "ConnectionError"_L1,
- "UnknownError"_L1,
- "UnsupportedAuthenticationInformation"_L1,
- "InvalidAuthenticationInformation"_L1,
- "InvalidEndpointDescription"_L1,
- "NoMatchingUserIdentityTokenFound"_L1,
- "UnsupportedSecurityPolicy"_L1,
- "InvalidPki"_L1,
- };
- qint32 idx = static_cast<qint32> (error);
- if (idx > 0 && idx <= 10)
- emit connectionError (
- QStringLiteral ("Client error: %1").arg (names[idx]));
+ if (error == QOpcUaClient::NoError)
+ return;
+ auto me = QMetaEnum::fromType<QOpcUaClient::ClientError> ();
+ const char *key = me.valueToKey (static_cast<int> (error));
+ emit connectionError (
+ QStringLiteral ("Client error: %1")
+ .arg (key ? QLatin1StringView (key) : "Unknown"_L1));
});
}
@@ -290,6 +284,7 @@ BobinkClient::handleEndpointsReceived (
return;
}
+ // Pick the endpoint with the highest securityLevel (server-assigned score).
QOpcUaEndpointDescription best = endpoints.first ();
for (const auto &ep : endpoints)
{
@@ -381,7 +376,7 @@ BobinkClient::startDiscovery ()
return;
doDiscovery ();
- m_discoveryTimer.start (m_discoveryInterval);
+ m_discoveryTimer.start (DISCOVERY_INTERVAL);
if (!m_discovering)
{
diff --git a/src/BobinkClient.h b/src/BobinkClient.h
index 2b41e05..c566a67 100644
--- a/src/BobinkClient.h
+++ b/src/BobinkClient.h
@@ -15,7 +15,6 @@
#include <QOpcUaClient>
#include <QOpcUaEndpointDescription>
#include <QOpcUaErrorState>
-#include <QOpcUaPkiConfiguration>
#include <QOpcUaProvider>
#include <QQmlEngine>
#include <QTimer>
@@ -48,6 +47,7 @@ class BobinkClient : public QObject
QString keyFile READ keyFile WRITE setKeyFile NOTIFY keyFileChanged)
public:
+ /// OPC UA message security mode (values match MessageSecurityMode).
enum SecurityMode
{
SignAndEncrypt = 3,
@@ -56,6 +56,7 @@ public:
};
Q_ENUM (SecurityMode)
+ /// OPC UA security policy for encryption algorithms.
enum SecurityPolicy
{
Basic256Sha256,
@@ -175,7 +176,6 @@ private:
/* -- Discovery -- */
QString m_discoveryUrl;
- qint32 m_discoveryInterval = 30000; // ms
QTimer m_discoveryTimer;
bool m_discovering = false;
QList<QOpcUaApplicationDescription> m_discoveredServers;