From e4ce4247239e040566ad955c7f564b708a147c95 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 21 Sep 2026 10:04:17 +0700 Subject: [PATCH 1/2] feat: converge File Home on IEDScout workflow --- .github/workflows/ied-simulator-qt.yml | 6 + apps/ied_simulator/qml/FileWorkspace.qml | 321 ++++++++++++++---- apps/ied_simulator/qml/Main.qml | 38 ++- .../src/ProductHardeningController.cpp | 136 +++++++- .../src/ProductHardeningController.hpp | 21 +- .../src/ied_product_hardening_qa.cpp | 86 ++++- apps/ied_simulator/test_file_home_workflow.py | 44 +++ 7 files changed, 564 insertions(+), 88 deletions(-) create mode 100644 apps/ied_simulator/test_file_home_workflow.py diff --git a/.github/workflows/ied-simulator-qt.yml b/.github/workflows/ied-simulator-qt.yml index 3362ae142..bf09ec6ab 100644 --- a/.github/workflows/ied-simulator-qt.yml +++ b/.github/workflows/ied-simulator-qt.yml @@ -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 diff --git a/apps/ied_simulator/qml/FileWorkspace.qml b/apps/ied_simulator/qml/FileWorkspace.qml index 06f08d5df..d7184b2b3 100644 --- a/apps/ied_simulator/qml/FileWorkspace.qml +++ b/apps/ied_simulator/qml/FileWorkspace.qml @@ -11,10 +11,12 @@ Item { required property var workspace required property var hardening required property var browserSession + required property var context property int viewIndex: 0 signal browserRequested() + signal discoverRequested(string host, int port) signal endpointRequested(string host, int port) signal simulatorRequested() signal snifferRequested() @@ -25,28 +27,84 @@ Item { return slash >= 0 ? normalized.substring(slash + 1) : normalized } - Component.onCompleted: { - if (workspace.loaded) - root.viewIndex = 1 - } - - Connections { - target: workspace - function onWorkspaceChanged() { - if (workspace.loaded) - root.viewIndex = 1 - } - } - FileDialog { id: openDialog - title: "Open IEC 61850 engineering resource" + title: "Open IEC 61850 engineering model" fileMode: FileDialog.OpenFile nameFilters: ["IEC 61850 engineering files (*.scl *.cid *.scd *.iid *.icd)", "All files (*)"] onAccepted: { - if (workspace.openFile(selectedFile)) { - root.viewIndex = 1 + if (workspace.openFile(selectedFile)) root.browserRequested() + } + } + + Dialog { + id: discoverDialog + modal: true + title: "Discover IED" + standardButtons: Dialog.NoButton + width: 430 + x: Math.max(12, Math.round((root.width - width) / 2)) + y: Math.max(12, Math.round((root.height - height) / 2)) + onOpened: { + discoverHost.text = browserSession.host + discoverPort.value = browserSession.port + discoverHost.forceActiveFocus() + discoverHost.selectAll() + } + + contentItem: ColumnLayout { + spacing: 10 + + Label { + Layout.fillWidth: true + text: "Build the IED engineering model directly from MMS discovery, then open it in the same Browser used by Open SCL." + color: root.theme.textSoft + font.pixelSize: 9 + wrapMode: Text.WordWrap + } + + Label { text: "IED IP / hostname"; color: root.theme.text; font.pixelSize: 9 } + TextField { + id: discoverHost + Layout.fillWidth: true + placeholderText: "192.168.1.100" + selectByMouse: true + } + + Label { text: "MMS TCP port"; color: root.theme.text; font.pixelSize: 9 } + SpinBox { + id: discoverPort + from: 1 + to: 65535 + value: 102 + editable: true + } + + Label { + Layout.fillWidth: true + text: "Discovery is explicit. Recent IED entries never auto-connect on startup." + color: root.theme.muted + font.pixelSize: 8 + wrapMode: Text.WordWrap + } + + RowLayout { + Layout.fillWidth: true + Item { Layout.fillWidth: true } + Button { text: "Cancel"; onClicked: discoverDialog.close() } + ActionButton { + theme: root.theme + text: "Discover IED" + primary: true + enabled: discoverHost.text.trim().length > 0 + onClicked: { + const host = discoverHost.text.trim() + const port = discoverPort.value + discoverDialog.close() + root.discoverRequested(host, port) + } + } } } } @@ -87,10 +145,9 @@ Item { } Button { - text: workspace.loaded ? "Current resource" : "Resource" + text: "Configuration" checkable: true checked: root.viewIndex === 1 - enabled: workspace.loaded implicitHeight: 28 onClicked: root.viewIndex = 1 } @@ -106,27 +163,29 @@ Item { ActionButton { theme: root.theme text: "Open SCL" - primary: !workspace.loaded + primary: true enabled: !workspace.busy && !browserSession.configurationLocked onClicked: openDialog.open() } + ActionButton { + theme: root.theme + text: "Discover IED" + enabled: !browserSession.configurationLocked + onClicked: discoverDialog.open() + } + Item { Layout.fillWidth: true } Label { - visible: workspace.loaded - Layout.maximumWidth: 420 - text: workspace.sourceName + " · " + workspace.editionText - color: root.theme.textSoft + Layout.maximumWidth: 430 + text: context.loaded + ? context.iedName + " · " + context.authority + : "No active IED engineering context" + color: context.loaded ? root.theme.textSoft : root.theme.muted font.pixelSize: 9 elide: Text.ElideMiddle } - - Label { - text: workspace.busy ? workspace.stateText : "" - color: root.theme.muted - font.pixelSize: 8 - } } } @@ -153,7 +212,7 @@ Item { } Label { Layout.fillWidth: true - text: "Open an engineering resource, continue with an online IED, start the simulator, or inspect network traffic." + text: "Start from an engineering file or a live IED. Both paths converge into one persistent IED Browser model." color: root.theme.textSoft font.pixelSize: 9 wrapMode: Text.WordWrap @@ -161,22 +220,22 @@ Item { GridLayout { Layout.fillWidth: true - columns: root.width >= 1120 ? 4 : 2 + columns: root.width >= 1260 ? 5 : root.width >= 760 ? 3 : 2 columnSpacing: 8 rowSpacing: 8 SurfaceCard { Layout.fillWidth: true - Layout.preferredHeight: 104 + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent anchors.margins: 10 spacing: 3 - Label { text: "Engineering file"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { text: "Open SCL"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } Label { Layout.fillWidth: true - text: "Open SCL / CID / SCD / IID / ICD and continue in the unified IED Browser." + text: "Open SCL / CID / SCD / IID / ICD, browse offline, then go Online from the same Browser." color: root.theme.muted font.pixelSize: 8 wrapMode: Text.WordWrap @@ -194,58 +253,63 @@ Item { SurfaceCard { Layout.fillWidth: true - Layout.preferredHeight: 104 + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent anchors.margins: 10 spacing: 3 - Label { text: "Online IED"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { text: "Discover IED"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } Label { Layout.fillWidth: true - text: "Continue to the unified IED Browser with one logical endpoint." + text: "Connect to one MMS endpoint, discover its structure, and build the canonical IED model." color: root.theme.muted font.pixelSize: 8 wrapMode: Text.WordWrap } Item { Layout.fillHeight: true } - ActionButton { theme: root.theme; text: "IED Browser"; onClicked: root.browserRequested() } + ActionButton { + theme: root.theme + text: "Discover IED" + enabled: !browserSession.configurationLocked + onClicked: discoverDialog.open() + } } } SurfaceCard { Layout.fillWidth: true - Layout.preferredHeight: 104 + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent anchors.margins: 10 spacing: 3 - Label { text: "Simulation"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { text: "Simulate IED"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } Label { Layout.fillWidth: true - text: "Create or stimulate an IEC 61850 IED in the dedicated simulator workspace." + text: "Open the independent IEC 61850 simulator and commissioning workspace." color: root.theme.muted font.pixelSize: 8 wrapMode: Text.WordWrap } Item { Layout.fillHeight: true } - ActionButton { theme: root.theme; text: "IED Simulator"; onClicked: root.simulatorRequested() } + ActionButton { theme: root.theme; text: "Simulate IED"; onClicked: root.simulatorRequested() } } } SurfaceCard { Layout.fillWidth: true - Layout.preferredHeight: 104 + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent anchors.margins: 10 spacing: 3 - Label { text: "Network observation"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { text: "Sniffer"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } Label { Layout.fillWidth: true - text: "Open the independent Sniffer workspace for capture and decode workflows." + text: "Open passive GOOSE capture, decode, supervision, and PCAP workflows." color: root.theme.muted font.pixelSize: 8 wrapMode: Text.WordWrap @@ -254,6 +318,27 @@ Item { ActionButton { theme: root.theme; text: "Sniffer"; onClicked: root.snifferRequested() } } } + + SurfaceCard { + Layout.fillWidth: true + Layout.preferredHeight: 112 + theme: root.theme + ColumnLayout { + anchors.fill: parent + anchors.margins: 10 + spacing: 3 + Label { text: "Configuration"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { + Layout.fillWidth: true + text: "Review application state, Browser layout persistence, and raw-Ethernet runtime readiness." + color: root.theme.muted + font.pixelSize: 8 + wrapMode: Text.WordWrap + } + Item { Layout.fillHeight: true } + ActionButton { theme: root.theme; text: "Configuration"; onClicked: root.viewIndex = 1 } + } + } } RowLayout { @@ -264,7 +349,7 @@ Item { SurfaceCard { Layout.fillWidth: true Layout.fillHeight: true - Layout.minimumHeight: 260 + Layout.minimumHeight: 270 theme: root.theme ColumnLayout { @@ -275,7 +360,7 @@ Item { RowLayout { Layout.fillWidth: true Label { - text: "Recent engineering resources" + text: "Recently opened SCL files" color: root.theme.text font.pixelSize: 10 font.weight: Font.DemiBold @@ -292,13 +377,12 @@ Item { Label { visible: hardening.recentResources.length === 0 Layout.fillWidth: true - text: "No recent engineering resources yet." + text: "No recently opened engineering files." color: root.theme.muted font.pixelSize: 9 } ListView { - id: resourceList Layout.fillWidth: true Layout.fillHeight: true clip: true @@ -359,10 +443,8 @@ Item { && !workspace.busy && !browserSession.configurationLocked onClicked: { - if (workspace.openFile(hardening.recentResourceUrl(index))) { - root.viewIndex = 1 + if (workspace.openFile(hardening.recentResourceUrl(index))) root.browserRequested() - } } } } @@ -371,9 +453,9 @@ Item { } SurfaceCard { - Layout.preferredWidth: Math.max(330, root.width * 0.32) + Layout.fillWidth: true Layout.fillHeight: true - Layout.minimumHeight: 260 + Layout.minimumHeight: 270 theme: root.theme ColumnLayout { @@ -384,46 +466,45 @@ Item { RowLayout { Layout.fillWidth: true Label { - text: "Recent online endpoints" + text: "Recently discovered IEDs" color: root.theme.text font.pixelSize: 10 font.weight: Font.DemiBold } Item { Layout.fillWidth: true } Button { - visible: hardening.recentEndpoints.length > 0 + visible: hardening.recentDiscoveredIeds.length > 0 text: "Clear" flat: true - onClicked: hardening.clearRecentEndpoints() + onClicked: hardening.clearRecentDiscoveredIeds() } } Label { - visible: hardening.recentEndpoints.length === 0 + visible: hardening.recentDiscoveredIeds.length === 0 Layout.fillWidth: true - text: "No recent MMS endpoints yet. Automatic reconnect remains disabled." + text: "No discovered IED history yet." color: root.theme.muted font.pixelSize: 9 - wrapMode: Text.WordWrap } ListView { - id: endpointList + id: discoveredIedList Layout.fillWidth: true Layout.fillHeight: true clip: true reuseItems: true spacing: 2 - model: hardening.recentEndpoints + model: hardening.recentDiscoveredIeds ScrollBar.vertical: ScrollBar {} delegate: Rectangle { required property int index required property string modelData width: ListView.view.width - height: 38 + height: 44 radius: 4 - color: endpointMouse.containsMouse ? root.theme.surfaceRaised : root.theme.surfaceSoft + color: discoveredMouse.containsMouse ? root.theme.surfaceRaised : root.theme.surfaceSoft border.width: 1 border.color: root.theme.lineSoft @@ -448,18 +529,20 @@ Item { } MouseArea { - id: endpointMouse + id: discoveredMouse anchors.fill: parent hoverEnabled: true - onClicked: root.endpointRequested(hardening.recentHost(index), - hardening.recentPort(index)) + enabled: !browserSession.configurationLocked + onClicked: root.endpointRequested( + hardening.recentDiscoveredIedHost(index), + hardening.recentDiscoveredIedPort(index)) } } } Label { Layout.fillWidth: true - text: "Selecting an endpoint restores address context only; it never auto-connects." + text: "Selecting a recent IED restores address context only. Discovery or Online connection remains explicit." color: root.theme.muted font.pixelSize: 7 wrapMode: Text.WordWrap @@ -472,9 +555,105 @@ Item { } } - SclWorkspace { - theme: root.theme - workspace: root.workspace + ScrollView { + contentWidth: availableWidth + + ColumnLayout { + width: Math.max(0, parent.width - 28) + x: 14 + spacing: 10 + + Item { Layout.preferredHeight: 4 } + RowLayout { + Layout.fillWidth: true + Label { + text: "Configuration" + color: root.theme.text + font.pixelSize: 14 + font.weight: Font.DemiBold + } + Item { Layout.fillWidth: true } + Button { text: "Back to Home"; onClicked: root.viewIndex = 0 } + } + + SurfaceCard { + Layout.fillWidth: true + theme: root.theme + ColumnLayout { + anchors.fill: parent + anchors.margins: 12 + spacing: 6 + Label { text: "Application state"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { + Layout.fillWidth: true + text: hardening.settingsStatus + color: hardening.settingsHealthy ? root.theme.textSoft : root.theme.amber + font.pixelSize: 9 + wrapMode: Text.WordWrap + } + Label { + text: "Automatic reconnect on startup: disabled" + color: root.theme.textSoft + font.pixelSize: 9 + } + Label { + Layout.fillWidth: true + text: "State file: " + hardening.statePath + color: root.theme.muted + font.pixelSize: 8 + elide: Text.ElideMiddle + } + } + } + + SurfaceCard { + Layout.fillWidth: true + theme: root.theme + ColumnLayout { + anchors.fill: parent + anchors.margins: 12 + spacing: 6 + RowLayout { + Layout.fillWidth: true + Label { text: "Raw Ethernet readiness"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Item { Layout.fillWidth: true } + Button { text: "Refresh"; onClicked: hardening.refreshRuntimeReadiness() } + } + Label { + Layout.fillWidth: true + text: hardening.npcapStatus + color: hardening.rawEthernetReady ? root.theme.textSoft : root.theme.amber + font.pixelSize: 9 + wrapMode: Text.WordWrap + } + } + } + + SurfaceCard { + Layout.fillWidth: true + theme: root.theme + ColumnLayout { + anchors.fill: parent + anchors.margins: 12 + spacing: 6 + Label { text: "Browser layout"; color: root.theme.text; font.pixelSize: 10; font.weight: Font.DemiBold } + Label { + text: "Navigation width: " + hardening.browserNavigationWidth + " px" + color: root.theme.textSoft + font.pixelSize: 9 + } + Label { + Layout.fillWidth: true + text: "The IED Browser splitter persists safely between sessions. Protocol runtime settings are not duplicated here." + color: root.theme.muted + font.pixelSize: 8 + wrapMode: Text.WordWrap + } + } + } + + Item { Layout.preferredHeight: 8 } + } } } } diff --git a/apps/ied_simulator/qml/Main.qml b/apps/ied_simulator/qml/Main.qml index fa23f709e..87c170d72 100644 --- a/apps/ied_simulator/qml/Main.qml +++ b/apps/ied_simulator/qml/Main.qml @@ -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() { @@ -209,20 +224,20 @@ 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 } } ToolTip.visible: hovered - ToolTip.text: "Recent MMS endpoints · restored without auto-connect" + ToolTip.text: "Recently discovered IEDs · address restored without auto-connect" } Label { @@ -281,11 +296,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 } diff --git a/apps/ied_simulator/src/ProductHardeningController.cpp b/apps/ied_simulator/src/ProductHardeningController.cpp index 8296b0c47..2f017fb5a 100644 --- a/apps/ied_simulator/src/ProductHardeningController.cpp +++ b/apps/ied_simulator/src/ProductHardeningController.cpp @@ -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 {}; @@ -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 @@ -109,6 +125,13 @@ QStringList ProductHardeningController::recentResources() const { return result; } +QStringList ProductHardeningController::recentDiscoveredIeds() const { + QStringList result; + result.reserve(static_cast(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; } @@ -130,6 +153,7 @@ void ProductHardeningController::resetDefaults() { browserNavigationWidth_ = defaultBrowserNavigationWidth; recent_.clear(); recentResources_.clear(); + recentDiscoveredIeds_.clear(); } void ProductHardeningController::setStateFault(const QString& message) { @@ -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) { @@ -220,7 +245,9 @@ bool ProductHardeningController::loadState() { } std::vector 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.")); @@ -244,8 +271,43 @@ bool ProductHardeningController::loadState() { } } - int browserNavigationWidth = defaultBrowserNavigationWidth; + std::vector 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(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) { @@ -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) { @@ -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."); } @@ -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(); @@ -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_); @@ -395,6 +472,59 @@ int ProductHardeningController::recentPort(const int index) const { return recent_[static_cast(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(maximumRecentDiscoveredIeds)) { + recentDiscoveredIeds_.resize(static_cast(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(recentDiscoveredIeds_.size())) return {}; + return recentDiscoveredIeds_[static_cast(index)].iedName; +} + +QString ProductHardeningController::recentDiscoveredIedHost(const int index) const { + if (index < 0 || index >= static_cast(recentDiscoveredIeds_.size())) return {}; + return recentDiscoveredIeds_[static_cast(index)].endpoint.host; +} + +int ProductHardeningController::recentDiscoveredIedPort(const int index) const { + if (index < 0 || index >= static_cast(recentDiscoveredIeds_.size())) return 0; + return recentDiscoveredIeds_[static_cast(index)].endpoint.port; +} + bool ProductHardeningController::rememberResource(const QString& pathValue) { const auto path = normalizedResourcePath(pathValue); if (path.isEmpty()) { diff --git a/apps/ied_simulator/src/ProductHardeningController.hpp b/apps/ied_simulator/src/ProductHardeningController.hpp index 2bb0e0351..38c263a84 100644 --- a/apps/ied_simulator/src/ProductHardeningController.hpp +++ b/apps/ied_simulator/src/ProductHardeningController.hpp @@ -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) @@ -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_; } @@ -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; @@ -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; @@ -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(); @@ -103,6 +121,7 @@ class ProductHardeningController : public QObject { int browserNavigationWidth_{defaultBrowserNavigationWidth}; std::vector recent_; std::vector recentResources_; + std::vector recentDiscoveredIeds_; bool settingsHealthy_{true}; QString settingsStatus_{QStringLiteral("Product state ready.")}; bool npcapAvailable_{}; diff --git a/apps/ied_simulator/src/ied_product_hardening_qa.cpp b/apps/ied_simulator/src/ied_product_hardening_qa.cpp index 4343ded9f..7a71f30a1 100644 --- a/apps/ied_simulator/src/ied_product_hardening_qa.cpp +++ b/apps/ied_simulator/src/ied_product_hardening_qa.cpp @@ -40,6 +40,7 @@ int main(int argc, char** argv) { ProductHardeningController state(path); if (!state.settingsHealthy() || state.workspaceIndex() != 0 || !state.recentEndpoints().isEmpty() || !state.recentResources().isEmpty() || + !state.recentDiscoveredIeds().isEmpty() || state.browserNavigationWidth() != 320 || state.automaticReconnectOnStartup()) { std::cerr << "Safe default product state contract failed.\n"; return 3; @@ -88,10 +89,40 @@ int main(int argc, char** argv) { return 9; } + for (int index = 1; index <= 10; ++index) { + if (!state.rememberDiscoveredIed( + QStringLiteral("IED_%1").arg(index), + QStringLiteral("198.51.100.%1").arg(index), + 102)) { + std::cerr << "Could not persist discovered IED history.\n"; + return 46; + } + } + if (state.recentDiscoveredIeds().size() != 8 || + state.recentDiscoveredIedName(0) != QStringLiteral("IED_10") || + state.recentDiscoveredIedHost(0) != QStringLiteral("198.51.100.10") || + state.recentDiscoveredIedPort(0) != 102 || + state.recentDiscoveredIeds().front() != QStringLiteral("IED_10 (198.51.100.10:102)")) { + std::cerr << "Discovered IED identity/order contract failed.\n"; + return 47; + } + const auto discoveredCount = state.recentDiscoveredIeds().size(); + if (!state.rememberDiscoveredIed( + QStringLiteral("IED_9_RENAMED"), + QStringLiteral("198.51.100.9"), + 102) || + state.recentDiscoveredIeds().size() != discoveredCount || + state.recentDiscoveredIedName(0) != QStringLiteral("IED_9_RENAMED")) { + std::cerr << "Discovered IED endpoint dedupe contract failed.\n"; + return 48; + } + state.setWorkspaceIndex(99); state.setBrowserNavigationWidth(900); if (state.workspaceIndex() != 2 || state.browserNavigationWidth() != 388 || state.rememberEndpoint(QString{}, 102) || state.rememberEndpoint(QStringLiteral("relay.local"), 0) || + state.rememberDiscoveredIed(QStringLiteral("IED_BAD"), QString{}, 102) || + state.rememberDiscoveredIed(QStringLiteral("IED_BAD"), QStringLiteral("relay.local"), 0) || state.rememberResource(QStringLiteral("relative-model.scd")) || state.rememberResource(QStringLiteral("https://example.invalid/model.scd"))) { std::cerr << "Invalid workspace/endpoint/resource did not fail closed.\n"; @@ -104,6 +135,8 @@ int main(int argc, char** argv) { if (!restored.settingsHealthy() || restored.workspaceIndex() != 2 || restored.browserNavigationWidth() != 388 || restored.recentEndpoints().size() != 8 || restored.lastHost() != QStringLiteral("192.0.2.9") || restored.lastPort() != 109 || restored.recentResources().size() != 8 || + restored.recentDiscoveredIeds().size() != 8 || + restored.recentDiscoveredIedName(0) != QStringLiteral("IED_9_RENAMED") || restored.recentResourcePath(0) != absolutePath(temp.filePath(QStringLiteral("model-9.scd"))) || restored.automaticReconnectOnStartup()) { std::cerr << "Restart persistence contract failed.\n"; @@ -143,6 +176,7 @@ int main(int argc, char** argv) { if (!migrated.settingsHealthy() || migrated.workspaceIndex() != 3 || migrated.lastHost() != QStringLiteral("legacy.local") || !migrated.recentResources().isEmpty() || + migrated.recentDiscoveredIeds().size() != 1 || !migrated.settingsStatus().contains(QStringLiteral("migrated"), Qt::CaseInsensitive)) { std::cerr << "Legacy seven-workspace state migration failed.\n"; return 21; @@ -152,11 +186,11 @@ int main(int argc, char** argv) { if (!migratedFile.open(QIODevice::ReadOnly)) return 22; const auto migratedDocument = QJsonDocument::fromJson(migratedFile.readAll()); if (!migratedDocument.isObject() || - migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 4 || + migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 5 || migratedDocument.object().value(QStringLiteral("workspaceIndex")).toInt(-1) != 3 || !migratedDocument.object().value(QStringLiteral("recentResources")).isArray() || migratedDocument.object().value(QStringLiteral("browserNavigationWidth")).toInt(-1) != 320) { - std::cerr << "Migrated legacy state was not rewritten as schema 4.\n"; + std::cerr << "Migrated legacy state was not rewritten as schema 5.\n"; return 23; } } @@ -173,6 +207,7 @@ int main(int argc, char** argv) { if (!migrated.settingsHealthy() || migrated.workspaceIndex() != 1 || migrated.lastHost() != QStringLiteral("p1.local") || !migrated.recentResources().isEmpty() || + migrated.recentDiscoveredIeds().size() != 1 || !migrated.settingsStatus().contains(QStringLiteral("migrated"), Qt::CaseInsensitive)) { std::cerr << "P1 schema-2 state migration failed.\n"; return 25; @@ -182,10 +217,10 @@ int main(int argc, char** argv) { if (!migratedFile.open(QIODevice::ReadOnly)) return 26; const auto migratedDocument = QJsonDocument::fromJson(migratedFile.readAll()); if (!migratedDocument.isObject() || - migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 4 || + migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 5 || !migratedDocument.object().value(QStringLiteral("recentResources")).isArray() || migratedDocument.object().value(QStringLiteral("browserNavigationWidth")).toInt(-1) != 320) { - std::cerr << "P1 state was not rewritten as schema 4.\n"; + std::cerr << "P1 state was not rewritten as schema 5.\n"; return 27; } } @@ -203,6 +238,7 @@ int main(int argc, char** argv) { if (!migrated.settingsHealthy() || migrated.workspaceIndex() != 1 || migrated.browserNavigationWidth() != 320 || migrated.lastHost() != QStringLiteral("p3.local") || + migrated.recentDiscoveredIeds().size() != 1 || !migrated.settingsStatus().contains(QStringLiteral("migrated"), Qt::CaseInsensitive)) { std::cerr << "P3 schema-3 Browser layout migration failed.\n"; return 29; @@ -212,13 +248,44 @@ int main(int argc, char** argv) { if (!migratedFile.open(QIODevice::ReadOnly)) return 30; const auto migratedDocument = QJsonDocument::fromJson(migratedFile.readAll()); if (!migratedDocument.isObject() || - migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 4 || + migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 5 || migratedDocument.object().value(QStringLiteral("browserNavigationWidth")).toInt(-1) != 320) { - std::cerr << "P3 state was not rewritten as schema 4.\n"; + std::cerr << "P3 state was not rewritten as schema 5.\n"; return 31; } } + { + QJsonObject p4; + p4.insert(QStringLiteral("schema"), 4); + p4.insert(QStringLiteral("workspaceIndex"), 1); + p4.insert(QStringLiteral("browserNavigationWidth"), 360); + p4.insert(QStringLiteral("recentEndpoints"), + QJsonArray{endpoint(QStringLiteral("p4.local"), 102)}); + p4.insert(QStringLiteral("recentResources"), QJsonArray{}); + if (!writeJson(path, p4)) return 49; + + ProductHardeningController migrated(path); + if (!migrated.settingsHealthy() || + migrated.browserNavigationWidth() != 360 || + migrated.recentDiscoveredIeds().size() != 1 || + migrated.recentDiscoveredIedHost(0) != QStringLiteral("p4.local") || + !migrated.settingsStatus().contains(QStringLiteral("migrated"), Qt::CaseInsensitive)) { + std::cerr << "P4 schema-4 discovered IED history migration failed.\n"; + return 50; + } + + QFile migratedFile(path); + if (!migratedFile.open(QIODevice::ReadOnly)) return 51; + const auto migratedDocument = QJsonDocument::fromJson(migratedFile.readAll()); + if (!migratedDocument.isObject() || + migratedDocument.object().value(QStringLiteral("schema")).toInt(-1) != 5 || + !migratedDocument.object().value(QStringLiteral("recentDiscoveredIeds")).isArray()) { + std::cerr << "P4 state was not rewritten as schema 5.\n"; + return 52; + } + } + { QFile corrupt(path); if (!corrupt.open(QIODevice::WriteOnly | QIODevice::Truncate) || corrupt.write("{not-json") < 0 || @@ -236,7 +303,8 @@ int main(int argc, char** argv) { ProductHardeningController damaged(path); if (damaged.settingsHealthy() || damaged.workspaceIndex() != 0 || - !damaged.recentEndpoints().isEmpty() || !damaged.recentResources().isEmpty()) { + !damaged.recentEndpoints().isEmpty() || !damaged.recentResources().isEmpty() || + !damaged.recentDiscoveredIeds().isEmpty()) { std::cerr << "Corrupt state was not ignored fail-closed.\n"; return 31; } @@ -313,12 +381,15 @@ int main(int argc, char** argv) { << " workspace_restore=2" << " endpoint_recent=8" << " resource_recent=8" + << " discovered_ied_recent=8" + << " discovered_ied_identity=pass" << " endpoint_dedupe=pass" << " resource_dedupe=pass" << " bounded_recent=8" << " legacy_workspace_migration=pass" << " p1_schema_migration=pass" << " p3_layout_migration=pass" + << " p4_discovered_ied_migration=pass" << " browser_splitter_persistence=pass" << " crash_recovery=pass" << " auto_reconnect=false" @@ -329,6 +400,7 @@ int main(int argc, char** argv) { << " invalid_workspace=rejected" << " invalid_endpoint=rejected" << " invalid_resource=rejected" + << " invalid_discovered_ied=rejected" << " unbounded_endpoint_recent=rejected" << " unbounded_resource_recent=rejected\n"; return 0; diff --git a/apps/ied_simulator/test_file_home_workflow.py b/apps/ied_simulator/test_file_home_workflow.py new file mode 100644 index 000000000..a95f5b25c --- /dev/null +++ b/apps/ied_simulator/test_file_home_workflow.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +from pathlib import Path +import sys + +if len(sys.argv) != 3: + raise SystemExit("usage: test_file_home_workflow.py FileWorkspace.qml Main.qml") + +file_qml = Path(sys.argv[1]).read_text(encoding="utf-8") +main_qml = Path(sys.argv[2]).read_text(encoding="utf-8") + +required = [ + "Open SCL", + "Discover IED", + "Simulate IED", + "Sniffer", + "Configuration", + "Recently opened SCL files", + "Recently discovered IEDs", + "signal discoverRequested(string host, int port)", + "recentDiscoveredIeds", +] +missing = [token for token in required if token not in file_qml] +if missing: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL missing=" + ",".join(missing)) + +if "SclWorkspace {" in file_qml: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL File tab still owns separate SCL workspace UI") + +recent_start = file_qml.index("Recently discovered IEDs") +recent_end = file_qml.index("Selecting a recent IED", recent_start) +recent_section = file_qml[recent_start:recent_end] +if "endpointRequested(" not in recent_section: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL recent IED does not restore endpoint context") +if "discoverRequested(" in recent_section or "discoverAndConnect(" in recent_section: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL recent IED auto-connects") + +if "onDiscoverRequested" not in main_qml or "browserSession.discoverAndConnect()" not in main_qml: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL explicit Discover IED routing missing") +if 'browserSession.trustedSclPath = ""' not in main_qml: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL discovery/recent IED does not bypass stale trusted SCL") +if "rememberDiscoveredIed(" not in main_qml: + raise SystemExit("FILE_HOME_WORKFLOW_FAIL discovered identity is not persisted") + +print("FILE_HOME_WORKFLOW_PASS primary_actions=5 recent_scl=pass recent_discovered_ied=pass explicit_discovery=pass auto_reconnect=false") From b2c546fd64b8f39561066c79cf31fae4f2a27493 Mon Sep 17 00:00:00 2001 From: Ari Sulistiono Date: Mon, 21 Sep 2026 10:05:08 +0700 Subject: [PATCH 2/2] fix: make recent IED routing explicitly live --- apps/ied_simulator/qml/FileWorkspace.qml | 3 +++ apps/ied_simulator/qml/Main.qml | 1 + 2 files changed, 4 insertions(+) diff --git a/apps/ied_simulator/qml/FileWorkspace.qml b/apps/ied_simulator/qml/FileWorkspace.qml index d7184b2b3..1790fd11f 100644 --- a/apps/ied_simulator/qml/FileWorkspace.qml +++ b/apps/ied_simulator/qml/FileWorkspace.qml @@ -578,6 +578,7 @@ Item { SurfaceCard { Layout.fillWidth: true + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent @@ -608,6 +609,7 @@ Item { SurfaceCard { Layout.fillWidth: true + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent @@ -631,6 +633,7 @@ Item { SurfaceCard { Layout.fillWidth: true + Layout.preferredHeight: 112 theme: root.theme ColumnLayout { anchors.fill: parent diff --git a/apps/ied_simulator/qml/Main.qml b/apps/ied_simulator/qml/Main.qml index 87c170d72..6f65dfa90 100644 --- a/apps/ied_simulator/qml/Main.qml +++ b/apps/ied_simulator/qml/Main.qml @@ -234,6 +234,7 @@ ApplicationWindow { if (recentHost.length > 0 && recentPort > 0) { browserSession.host = recentHost browserSession.port = recentPort + browserSession.trustedSclPath = "" } } ToolTip.visible: hovered