Sfoglia il codice sorgente

Build GPIO configuration choices from active board whitelist

SybexXx 3 giorni fa
parent
commit
87bae6ea3b
1 ha cambiato i file con 97 aggiunte e 16 eliminazioni
  1. 97 16
      sd-card/html/edit_config_template.html

+ 97 - 16
sd-card/html/edit_config_template.html

@@ -3378,24 +3378,105 @@ openDescription();
 LoadConfigNeu();
 
 /*
- * Fail closed in the UI while the active board profile exposes no user pins.
- * The firmware performs the authoritative validation independently.
+ * The board profile is the single source of truth for GPIO choices.
+ * Hide legacy hard-coded rows first; only rows present in the firmware
+ * whitelist may become visible and editable.
  */
-(function lockUnprofiledGpioUi() {
+(function loadBoardGpioCapabilities() {
     var categoryCheckbox = document.getElementById("Category_GPIO_enabled");
-    if (categoryCheckbox) {
-        categoryCheckbox.checked = false;
-        categoryCheckbox.disabled = true;
-    }
-    if (typeof category !== "undefined" && category.GPIO) {
-        category.GPIO.enabled = false;
-    }
-    document.querySelectorAll(".GPIO_item input, .GPIO_item select, .GPIO_item textarea").forEach(function (element) {
-        element.disabled = true;
-    });
-    document.querySelectorAll(".GPIO_item").forEach(function (element) {
-        element.style.opacity = "0.55";
-    });
+    var notice = document.getElementById("GPIO_profile_notice");
+    var knownPins = [0, 1, 3, 4, 12, 13];
+    var capability = {
+        input: 1, output: 2, pullUp: 4, pullDown: 8, interrupt: 16, pwm: 32
+    };
+
+    function showUnavailable(message) {
+        if (notice) {
+            notice.style.display = "";
+            notice.cells[0].innerHTML = "<b>GPIO configuration unavailable.</b><br>" + message;
+        }
+        if (categoryCheckbox) {
+            categoryCheckbox.checked = false;
+            categoryCheckbox.disabled = true;
+        }
+        if (typeof category !== "undefined" && category.GPIO) {
+            category.GPIO.enabled = false;
+        }
+        knownPins.forEach(function (pin) {
+            document.querySelectorAll(".GPIO_IO" + pin).forEach(function (row) {
+                row.style.display = "none";
+                row.querySelectorAll("input, select, textarea").forEach(function (element) {
+                    element.disabled = true;
+                });
+            });
+        });
+    }
+
+    showUnavailable("Loading the active board profile…");
+
+    fetch("/GPIO?capabilities=1", { method: "GET", cache: "no-store" })
+        .then(function (response) {
+            if (!response.ok) throw new Error("HTTP " + response.status);
+            return response.json();
+        })
+        .then(function (profile) {
+            if (!profile || !Array.isArray(profile.pins) ||
+                !Number.isInteger(profile.maxChannels) || profile.maxChannels < 1) {
+                throw new Error("Invalid board GPIO profile response");
+            }
+
+            var available = profile.pins.filter(function (pin) {
+                return Number.isInteger(pin.gpio) && knownPins.indexOf(pin.gpio) !== -1 &&
+                    Number.isInteger(pin.capabilities);
+            }).slice(0, Math.min(4, profile.maxChannels));
+
+            if (notice) notice.style.display = available.length ? "none" : "";
+            if (!available.length) {
+                showUnavailable("The active profile (" + profile.profile +
+                    ") does not currently whitelist any GPIO exposed by this configuration page.");
+                return;
+            }
+
+            knownPins.forEach(function (pin) {
+                var allowed = available.find(function (item) { return item.gpio === pin; });
+                document.querySelectorAll(".GPIO_IO" + pin).forEach(function (row) {
+                    row.style.display = allowed ? "" : "none";
+                    row.querySelectorAll("input, select, textarea").forEach(function (element) {
+                        element.disabled = !allowed;
+                    });
+                });
+                if (!allowed) return;
+
+                var modes = document.getElementById("GPIO_IO" + pin + "_value1");
+                if (modes) {
+                    Array.from(modes.options).forEach(function (option) {
+                        var modeAllowed =
+                            (option.value === "input" && (allowed.capabilities & capability.input)) ||
+                            (option.value === "input-pullup" &&
+                                (allowed.capabilities & capability.input) && (allowed.capabilities & capability.pullUp)) ||
+                            (option.value === "input-pulldown" &&
+                                (allowed.capabilities & capability.input) && (allowed.capabilities & capability.pullDown)) ||
+                            (option.value === "output" && (allowed.capabilities & capability.output)) ||
+                            (option.value === "output-pwm" && (allowed.capabilities & capability.output) &&
+                                (allowed.capabilities & capability.pwm));
+                        option.disabled = !modeAllowed;
+                    });
+                }
+                var interrupts = document.getElementById("GPIO_IO" + pin + "_value2");
+                if (interrupts) {
+                    interrupts.disabled = !(allowed.capabilities & capability.interrupt);
+                }
+            });
+
+            if (categoryCheckbox) categoryCheckbox.disabled = false;
+            if (typeof category !== "undefined" && category.GPIO) {
+                category.GPIO.enabled = false;
+            }
+        })
+        .catch(function (error) {
+            showUnavailable("Could not read the firmware board profile (" + error.message +
+                "). GPIO stays locked for safety.");
+        });
 })();
 
 </script>