Skip to content

Apply vendor board configuration only when the board type is known - #1268

Merged
gskjold merged 1 commit into
mainfrom
fix/vendor-board-config-return-value
Sep 17, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/vendor-board-config-return-value

Conversation

@gskjold

@gskjold gskjold commented Sep 10, 2026

Copy link
Copy Markdown
Member

Reported by Egil, who noticed it while documenting board types in the wiki.

The bug

handleSave() discarded the return value of HwTools::applyBoardConfig(), leaving success hardcoded to true, so if(success) and both success ? … : … were dead branches:

bool success = true;
...
config->clearGpio(*gpioConfig);
hw->applyBoardConfig(boardType, *gpioConfig, meterConfig, hanPin);
if(success) {
    config->setGpioConfig(*gpioConfig);
    sys.boardType = success ? boardType : 0xFF;
    sys.vendorConfigured = success;

This regressed in 67e6a51 ("Force board profiles on boot"), which lifted the per-board switch out of handleSave() into applyBoardConfig(). The original switch (from ab7128c) had a default: success = false; arm, and the extraction dropped the wiring — applyBoardConfig() returns bool and falls through to return false for an unknown board type, but the caller ignored it.

Because clearGpio() runs before the call, an unmatched board type persisted a wiped GPIO config together with vendorConfigured = true: apPin, ledPin, ledPinRed/Green/Blue and vccPin all stayed 0xFF. The LED and AP button go dead, and with no apPin the emergency factory reset is unreachable. The device is not bricked — the web UI still works and /vendor can be used to pick the right type — but nothing pointed the user there.

Via the GUI this is unlikely, since the select preselects the current board type and filters on chip. But the empty <option value={-1}> is selected when the stored board type is not among that chip's options, the select has no required, and the endpoint accepted any value from a hand-made POST.

Fix

The profile is applied to a local GpioConfig, and only copied into the live one on success — so a rejected board type leaves neither EEPROM nor the running configuration touched. (Without the local copy, a rejected POST would leave the in-memory GPIO config wiped for hw->setup(&sys, gpioConfig) at the end of the handler, even with EEPROM intact.)

Three related issues along the same path

clear() discarded the HAN pin. clear() deliberately preserves boardType and vendorConfigured, but then called clearMeter(), which resets rxPin, txPin and rxPinPullup — the three fields applyBoardConfig() sets, dictated by that same board type. Board profiles are only re-applied on boot for boardType < 20, so generic boards lost their HAN pin on every factory reset (button, web and MQTT) and whenever "Clear all other configuration" was used on the vendor page. clear() now carries those three pins across.

Fixed centrally rather than by widening the < 20 guard in setup() — forcing profiles on boot for generic boards would stomp user GPIO customisations, which is what that guard exists to protect. getMeterConfig() does its own config-version check and returns cleared values on a virgin EEPROM, so first-boot behaviour is unchanged, and it never calls back into clear(), so there is no recursion (unlike getSystemConfig()). Note the setup branch at AmsWebServer.cpp:1139 already hand-rolled this same save/restore around its own clear() call; that is now redundant but left alone.

Unsigned compare in the reset button guard. setup() tested if(gpioConfig.apPin >= 0), always true for a uint8_t. With no AP pin configured, boot spent five seconds walking a hold sequence that can never complete (the third check needs a HIGH read on an invalid pin), flashing green and red LEDs on the way. Now tests != 0xFF, matching the two other apPin call sites.

Vendor page hid the rejection. handleSubmit navigated to / or /setup regardless of res.success. Since getSysinfo() is only re-polled under specific conditions, the vndcfg === false redirect in App.svelte would not necessarily bring the user back until a reload. It now stays on the page and reports the failure. The string is hardcoded English to match the rest of this route, which has no translations — no Localazy change needed.

Verification

All six environments build: esp8266dev, esp32dev, esp32s2dev, esp32s3dev, esp32c3dev, esp32solodev. ui/dist rebuilt (only index.js changed). esp8266 flash 98.1% → 98.2%, +128 bytes.

pio test -e native was not run — nothing here touches src/decoder/.

Left alone, worth a follow-up

  • AmsConfiguration.cpp:533 has the identical unsigned compare in setGpioConfig() — if(config.apPin >= 0) pinMode(...). Harmless (invalid-pin pinMode is a no-op) but wrong for the same reason.
  • VendorRoute.svelte:30 reads s.boardType, but the sysinfo field is s.board (as used everywhere else in that file), so s.if.eth is always false after a vendor save.
  • A vendor POST with vr=true and a rejected board type still runs config->clear() at the end of handleSave(), wiping configuration while correctly reporting failure. Restructuring that is more invasive than this fix warrants — the clear() must stay last, since it writes EEPROM_CLEARED_INDICATOR and must not be followed by save() (see f1089fa).
  • CLAUDE.md lists Node 19.x as a prerequisite, but the current Vite refuses to run on it (requires Node.js version 20.19+ or 22.12+). Built with v22.22.2.

🤖 Generated with Claude Code

handleSave() discarded the return value of HwTools::applyBoardConfig(),
leaving "success" hardcoded to true, so if(success) and both
success ? ... : ... were dead branches. This regressed in 67e6a51, which
lifted the per-board switch out of handleSave() into applyBoardConfig();
the original switch had a "default: success = false" arm that was lost in
the move.

Because clearGpio() runs before the call, an unmatched board type
persisted a wiped GPIO config together with vendorConfigured = true:
apPin, ledPin, ledPinRed/Green/Blue and vccPin all stayed 0xFF, leaving
the LED and AP button dead and the emergency factory reset unreachable.
The GUI preselects the current board type and filters on chip, but the
empty option is selected when the stored type is not among the chip's
options, and the endpoint accepted any value from a hand-made POST.

The profile is now applied to a local GpioConfig that is only copied into
the live one on success, so a rejected board type leaves neither EEPROM
nor the running configuration touched.

Three related issues along the same path:

- clear() preserves boardType and vendorConfigured, but clearMeter() then
  reset rxPin, txPin and rxPinPullup, which are dictated by that same
  board type. Board profiles are only re-applied on boot for boardType
  < 20, so generic boards lost their HAN pin on every factory reset and
  whenever "Clear all other configuration" was used. clear() now carries
  those three pins across.

- The reset button guard in setup() tested apPin >= 0, always true for a
  uint8_t. With no AP pin configured, boot spent five seconds walking a
  hold sequence that can never complete on an invalid pin, flashing the
  green and red LEDs on the way. Now tests != 0xFF, as the two other
  apPin call sites already do.

- The vendor page navigated away regardless of the response, so a
  rejected board type was invisible. sysinfo is not polled unconditionally,
  so the vndcfg redirect did not necessarily bring the user back. It now
  stays put and reports the failure.

Reported by Egil.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: a6e575b

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit 2f54e3f into main Sep 17, 2026
8 checks passed
@gskjold
gskjold deleted the fix/vendor-board-config-return-value branch September 17, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant