Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ied-simulator-qt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ jobs:
QT_QPA_PLATFORM: offscreen
run: ./build-ied-simulator-qt/ied_product_hardening_qa

- name: IEDScout-style File/Home workflow contract
run: >-
python3 apps/ied_simulator/test_file_home_workflow.py
apps/ied_simulator/qml/FileWorkspace.qml
apps/ied_simulator/qml/Main.qml

- name: IED Browser session ownership gates
env:
QT_QPA_PLATFORM: offscreen
Expand Down
324 changes: 253 additions & 71 deletions apps/ied_simulator/qml/FileWorkspace.qml

Large diffs are not rendered by default.

39 changes: 33 additions & 6 deletions apps/ied_simulator/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ ApplicationWindow {
}
}

Connections {
target: iedContext
function onContextChanged() {
if (iedContext.loaded
&& iedContext.authorityKey === "live-discovery"
&& iedContext.endpointHost.length > 0
&& iedContext.endpointPort > 0) {
hardening.rememberDiscoveredIed(
iedContext.iedName,
iedContext.endpointHost,
iedContext.endpointPort)
}
}
}

Connections {
target: sclWorkspace
function onWorkspaceChanged() {
Expand Down Expand Up @@ -209,20 +224,21 @@ ApplicationWindow {

ComboBox {
id: recentConnectionPicker
visible: root.workspaceIndex === 1 && hardening.recentEndpoints.length > 0
Layout.preferredWidth: 160
model: hardening.recentEndpoints
visible: root.workspaceIndex === 1 && hardening.recentDiscoveredIeds.length > 0
Layout.preferredWidth: 220
model: hardening.recentDiscoveredIeds
enabled: !browserSession.configurationLocked
onActivated: {
const recentHost = hardening.recentHost(currentIndex)
const recentPort = hardening.recentPort(currentIndex)
const recentHost = hardening.recentDiscoveredIedHost(currentIndex)
const recentPort = hardening.recentDiscoveredIedPort(currentIndex)
if (recentHost.length > 0 && recentPort > 0) {
browserSession.host = recentHost
browserSession.port = recentPort
browserSession.trustedSclPath = ""
}
}
ToolTip.visible: hovered
ToolTip.text: "Recent MMS endpoints · restored without auto-connect"
ToolTip.text: "Recently discovered IEDs · address restored without auto-connect"
}

Label {
Expand Down Expand Up @@ -281,11 +297,22 @@ ApplicationWindow {
workspace: sclWorkspace
hardening: hardening
browserSession: browserSession
context: iedContext
onBrowserRequested: root.workspaceIndex = 1
onDiscoverRequested: function(host, port) {
if (!browserSession.configurationLocked) {
browserSession.host = host
browserSession.port = port
browserSession.trustedSclPath = ""
root.workspaceIndex = 1
browserSession.discoverAndConnect()
}
}
onEndpointRequested: function(host, port) {
if (!browserSession.configurationLocked) {
browserSession.host = host
browserSession.port = port
browserSession.trustedSclPath = ""
}
root.workspaceIndex = 1
}
Expand Down
136 changes: 133 additions & 3 deletions apps/ied_simulator/src/ProductHardeningController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ QString ProductHardeningController::normalizedHost(const QString& value) {
return host;
}

QString ProductHardeningController::normalizedIedName(const QString& value) {
const auto name = value.trimmed();
if (name.isEmpty() || name.size() > maximumIedNameCharacters) return {};
for (const auto character : name) {
if (character.category() == QChar::Other_Control) return {};
}
return name;
}

QString ProductHardeningController::normalizedResourcePath(const QString& value) {
const auto candidate = value.trimmed();
if (candidate.isEmpty() || candidate.size() > maximumResourcePathCharacters) return {};
Expand Down Expand Up @@ -77,6 +86,13 @@ QString ProductHardeningController::endpointLabel(const Endpoint& endpoint) {
return QStringLiteral("%1:%2").arg(host).arg(endpoint.port);
}

QString ProductHardeningController::discoveredIedLabel(const DiscoveredIed& ied) {
const auto endpoint = endpointLabel(ied.endpoint);
return ied.iedName.isEmpty()
? endpoint
: QStringLiteral("%1 (%2)").arg(ied.iedName, endpoint);
}

int ProductHardeningController::migrateLegacyWorkspaceIndex(const int value) noexcept {
switch (value) {
case 0: // IED Connection
Expand Down Expand Up @@ -109,6 +125,13 @@ QStringList ProductHardeningController::recentResources() const {
return result;
}

QStringList ProductHardeningController::recentDiscoveredIeds() const {
QStringList result;
result.reserve(static_cast<qsizetype>(recentDiscoveredIeds_.size()));
for (const auto& ied : recentDiscoveredIeds_) result.push_back(discoveredIedLabel(ied));
return result;
}

QString ProductHardeningController::lastHost() const {
return recent_.empty() ? QString{} : recent_.front().host;
}
Expand All @@ -130,6 +153,7 @@ void ProductHardeningController::resetDefaults() {
browserNavigationWidth_ = defaultBrowserNavigationWidth;
recent_.clear();
recentResources_.clear();
recentDiscoveredIeds_.clear();
}

void ProductHardeningController::setStateFault(const QString& message) {
Expand Down Expand Up @@ -169,6 +193,7 @@ bool ProductHardeningController::loadState() {
const auto object = document.object();
const auto schema = object.value(QStringLiteral("schema")).toInt(-1);
if (schema != stateSchemaVersion &&
schema != browserLayoutStateSchemaVersion &&
schema != fileHomeStateSchemaVersion &&
schema != previousStateSchemaVersion &&
schema != legacyStateSchemaVersion) {
Expand Down Expand Up @@ -220,7 +245,9 @@ bool ProductHardeningController::loadState() {
}

std::vector<QString> loadedResources;
if (schema == stateSchemaVersion || schema == fileHomeStateSchemaVersion) {
if (schema == stateSchemaVersion ||
schema == browserLayoutStateSchemaVersion ||
schema == fileHomeStateSchemaVersion) {
const auto resources = object.value(QStringLiteral("recentResources"));
if (!resources.isArray() || resources.toArray().size() > maximumRecentResources) {
setStateFault(QStringLiteral("Persisted state ignored: recent resource list is invalid or unbounded."));
Expand All @@ -244,8 +271,43 @@ bool ProductHardeningController::loadState() {
}
}

int browserNavigationWidth = defaultBrowserNavigationWidth;
std::vector<DiscoveredIed> loadedDiscoveredIeds;
if (schema == stateSchemaVersion) {
const auto discovered = object.value(QStringLiteral("recentDiscoveredIeds"));
if (!discovered.isArray() || discovered.toArray().size() > maximumRecentDiscoveredIeds) {
setStateFault(QStringLiteral("Persisted state ignored: discovered IED history is invalid or unbounded."));
emit stateChanged();
return false;
}
loadedDiscoveredIeds.reserve(static_cast<std::size_t>(discovered.toArray().size()));
for (const auto& value : discovered.toArray()) {
if (!value.isObject()) {
setStateFault(QStringLiteral("Persisted state ignored: malformed discovered IED history."));
emit stateChanged();
return false;
}
const auto item = value.toObject();
const auto rawName = item.value(QStringLiteral("iedName")).toString().trimmed();
const auto iedName = normalizedIedName(rawName);
const auto host = normalizedHost(item.value(QStringLiteral("host")).toString());
const auto port = item.value(QStringLiteral("port")).toInt(0);
if ((!rawName.isEmpty() && iedName.isEmpty()) ||
host.isEmpty() || port < 1 || port > 65'535) {
setStateFault(QStringLiteral("Persisted state ignored: invalid discovered IED history entry."));
emit stateChanged();
return false;
}
loadedDiscoveredIeds.push_back({iedName, {host, port}});
}
} else {
loadedDiscoveredIeds.reserve(loaded.size());
for (const auto& endpoint : loaded) {
loadedDiscoveredIeds.push_back({{}, endpoint});
}
}

int browserNavigationWidth = defaultBrowserNavigationWidth;
if (schema == stateSchemaVersion || schema == browserLayoutStateSchemaVersion) {
browserNavigationWidth = object.value(QStringLiteral("browserNavigationWidth")).toInt(-1);
if (browserNavigationWidth < minimumBrowserNavigationWidth ||
browserNavigationWidth > maximumBrowserNavigationWidth) {
Expand All @@ -259,6 +321,7 @@ bool ProductHardeningController::loadState() {
browserNavigationWidth_ = browserNavigationWidth;
recent_ = std::move(loaded);
recentResources_ = std::move(loadedResources);
recentDiscoveredIeds_ = std::move(loadedDiscoveredIeds);
settingsHealthy_ = true;

if (schema == legacyStateSchemaVersion) {
Expand All @@ -267,6 +330,8 @@ bool ProductHardeningController::loadState() {
settingsStatus_ = QStringLiteral("Product state migrated to File/Home engineering-resource history.");
} else if (schema == fileHomeStateSchemaVersion) {
settingsStatus_ = QStringLiteral("Product state migrated to persistent Browser layout state.");
} else if (schema == browserLayoutStateSchemaVersion) {
settingsStatus_ = QStringLiteral("Product state migrated to identity-aware discovered IED history.");
} else {
settingsStatus_ = QStringLiteral("Persisted product state restored safely.");
}
Expand All @@ -280,7 +345,9 @@ bool ProductHardeningController::loadState() {
? QStringLiteral("Legacy product state migrated to the four-workspace File/Home schema.")
: schema == previousStateSchemaVersion
? QStringLiteral("Product state migrated to File/Home engineering-resource history.")
: QStringLiteral("Product state migrated to persistent Browser layout state.");
: schema == fileHomeStateSchemaVersion
? QStringLiteral("Product state migrated to persistent Browser layout state.")
: QStringLiteral("Product state migrated to identity-aware discovered IED history.");
}

emit stateChanged();
Expand All @@ -305,12 +372,22 @@ bool ProductHardeningController::persistState() {
QJsonArray resources;
for (const auto& path : recentResources_) resources.append(path);

QJsonArray discoveredIeds;
for (const auto& ied : recentDiscoveredIeds_) {
QJsonObject item;
item.insert(QStringLiteral("iedName"), ied.iedName);
item.insert(QStringLiteral("host"), ied.endpoint.host);
item.insert(QStringLiteral("port"), ied.endpoint.port);
discoveredIeds.append(item);
}

QJsonObject object;
object.insert(QStringLiteral("schema"), stateSchemaVersion);
object.insert(QStringLiteral("workspaceIndex"), workspaceIndex_);
object.insert(QStringLiteral("browserNavigationWidth"), browserNavigationWidth_);
object.insert(QStringLiteral("recentEndpoints"), recent);
object.insert(QStringLiteral("recentResources"), resources);
object.insert(QStringLiteral("recentDiscoveredIeds"), discoveredIeds);
const auto payload = QJsonDocument(object).toJson(QJsonDocument::Compact);

QSaveFile output(statePath_);
Expand Down Expand Up @@ -395,6 +472,59 @@ int ProductHardeningController::recentPort(const int index) const {
return recent_[static_cast<std::size_t>(index)].port;
}

bool ProductHardeningController::rememberDiscoveredIed(
const QString& iedNameValue,
const QString& hostValue,
const int port) {
const auto rawName = iedNameValue.trimmed();
const auto iedName = normalizedIedName(rawName);
const auto host = normalizedHost(hostValue);
if ((!rawName.isEmpty() && iedName.isEmpty()) ||
host.isEmpty() || port < 1 || port > 65'535) {
settingsStatus_ = QStringLiteral("Rejected invalid discovered IED history entry.");
emit stateChanged();
return false;
}

const auto duplicate = std::find_if(
recentDiscoveredIeds_.begin(),
recentDiscoveredIeds_.end(),
[&](const DiscoveredIed& ied) {
return ied.endpoint.port == port &&
ied.endpoint.host.compare(host, Qt::CaseInsensitive) == 0;
});
if (duplicate != recentDiscoveredIeds_.end()) recentDiscoveredIeds_.erase(duplicate);
recentDiscoveredIeds_.insert(recentDiscoveredIeds_.begin(), {iedName, {host, port}});
if (recentDiscoveredIeds_.size() > static_cast<std::size_t>(maximumRecentDiscoveredIeds)) {
recentDiscoveredIeds_.resize(static_cast<std::size_t>(maximumRecentDiscoveredIeds));
}
const auto persisted = persistState();
emit stateChanged();
return persisted;
}

void ProductHardeningController::clearRecentDiscoveredIeds() {
if (recentDiscoveredIeds_.empty()) return;
recentDiscoveredIeds_.clear();
(void)persistState();
emit stateChanged();
}

QString ProductHardeningController::recentDiscoveredIedName(const int index) const {
if (index < 0 || index >= static_cast<int>(recentDiscoveredIeds_.size())) return {};
return recentDiscoveredIeds_[static_cast<std::size_t>(index)].iedName;
}

QString ProductHardeningController::recentDiscoveredIedHost(const int index) const {
if (index < 0 || index >= static_cast<int>(recentDiscoveredIeds_.size())) return {};
return recentDiscoveredIeds_[static_cast<std::size_t>(index)].endpoint.host;
}

int ProductHardeningController::recentDiscoveredIedPort(const int index) const {
if (index < 0 || index >= static_cast<int>(recentDiscoveredIeds_.size())) return 0;
return recentDiscoveredIeds_[static_cast<std::size_t>(index)].endpoint.port;
}

bool ProductHardeningController::rememberResource(const QString& pathValue) {
const auto path = normalizedResourcePath(pathValue);
if (path.isEmpty()) {
Expand Down
21 changes: 20 additions & 1 deletion apps/ied_simulator/src/ProductHardeningController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ProductHardeningController : public QObject {
Q_PROPERTY(int browserNavigationWidth READ browserNavigationWidth WRITE setBrowserNavigationWidth NOTIFY stateChanged)
Q_PROPERTY(QStringList recentEndpoints READ recentEndpoints NOTIFY stateChanged)
Q_PROPERTY(QStringList recentResources READ recentResources NOTIFY stateChanged)
Q_PROPERTY(QStringList recentDiscoveredIeds READ recentDiscoveredIeds NOTIFY stateChanged)
Q_PROPERTY(QString lastHost READ lastHost NOTIFY stateChanged)
Q_PROPERTY(int lastPort READ lastPort NOTIFY stateChanged)
Q_PROPERTY(QString statePath READ statePath CONSTANT)
Expand All @@ -39,6 +40,7 @@ class ProductHardeningController : public QObject {

[[nodiscard]] QStringList recentEndpoints() const;
[[nodiscard]] QStringList recentResources() const;
[[nodiscard]] QStringList recentDiscoveredIeds() const;
[[nodiscard]] QString lastHost() const;
[[nodiscard]] int lastPort() const noexcept;
[[nodiscard]] QString statePath() const { return statePath_; }
Expand All @@ -55,6 +57,12 @@ class ProductHardeningController : public QObject {
Q_INVOKABLE QString recentHost(int index) const;
Q_INVOKABLE int recentPort(int index) const;

Q_INVOKABLE bool rememberDiscoveredIed(const QString& iedName, const QString& host, int port);
Q_INVOKABLE void clearRecentDiscoveredIeds();
Q_INVOKABLE QString recentDiscoveredIedName(int index) const;
Q_INVOKABLE QString recentDiscoveredIedHost(int index) const;
Q_INVOKABLE int recentDiscoveredIedPort(int index) const;

Q_INVOKABLE bool rememberResource(const QString& path);
Q_INVOKABLE void clearRecentResources();
Q_INVOKABLE QString recentResourcePath(int index) const;
Expand All @@ -74,13 +82,21 @@ class ProductHardeningController : public QObject {
int port{102};
};

struct DiscoveredIed final {
QString iedName;
Endpoint endpoint;
};

static constexpr int legacyStateSchemaVersion = 1;
static constexpr int previousStateSchemaVersion = 2;
static constexpr int fileHomeStateSchemaVersion = 3;
static constexpr int stateSchemaVersion = 4;
static constexpr int browserLayoutStateSchemaVersion = 4;
static constexpr int stateSchemaVersion = 5;
static constexpr int maximumRecentEndpoints = 8;
static constexpr int maximumRecentResources = 8;
static constexpr int maximumRecentDiscoveredIeds = 8;
static constexpr int maximumResourcePathCharacters = 4096;
static constexpr int maximumIedNameCharacters = 255;
static constexpr int minimumWorkspaceIndex = 0;
static constexpr int maximumWorkspaceIndex = 3;
static constexpr int legacyMaximumWorkspaceIndex = 6;
Expand All @@ -90,8 +106,10 @@ class ProductHardeningController : public QObject {

[[nodiscard]] static QString defaultStatePath();
[[nodiscard]] static QString normalizedHost(const QString& value);
[[nodiscard]] static QString normalizedIedName(const QString& value);
[[nodiscard]] static QString normalizedResourcePath(const QString& value);
[[nodiscard]] static QString endpointLabel(const Endpoint& endpoint);
[[nodiscard]] static QString discoveredIedLabel(const DiscoveredIed& ied);
[[nodiscard]] static int migrateLegacyWorkspaceIndex(int value) noexcept;
[[nodiscard]] bool loadState();
[[nodiscard]] bool persistState();
Expand All @@ -103,6 +121,7 @@ class ProductHardeningController : public QObject {
int browserNavigationWidth_{defaultBrowserNavigationWidth};
std::vector<Endpoint> recent_;
std::vector<QString> recentResources_;
std::vector<DiscoveredIed> recentDiscoveredIeds_;
bool settingsHealthy_{true};
QString settingsStatus_{QStringLiteral("Product state ready.")};
bool npcapAvailable_{};
Expand Down
Loading
Loading