Skip to content

Keep a WiFi Disable across power cycles on the Elite II / C64 Ultimate - #1042

Open
JC-000 wants to merge 17 commits into
GideonZ:masterfrom
JC-000:fix-589/r1
Open

JC-000 wants to merge 17 commits into
GideonZ:masterfrom
JC-000:fix-589/r1

Conversation

@JC-000

@JC-000 JC-000 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Makes "Disable" stick on the Elite II / C64 Ultimate. A WiFi Disable now survives a power cycle, so a disabled unit no longer starts its radio at mains-in. Fixes #589.

This PR was stacked on #1041, which is merged. The diff is the commits on top of it (6cfdc7b36..b93a12d34).

Summary

  • One stored setting. On the Elite II / C64 Ultimate, "WiFi Enabled" is now a stored enum item, the same one the Ultimate 64 already has. It replaces the two actions Enable and Disable. The menu's Enable and Disable, and the Yes of "WiFi is disabled. Enable it?", all go through NetworkLWIP_WiFi::set_enabled() from WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041. It sets the item, its change hook turns the radio on or off, and it saves only when the value changed.
  • The module keeps a copy. u64ctrl stores the setting in its NVS and reads it before it starts the radio. A unit stored as Disabled never starts its radio after mains-in, before the Ultimate is running.
    • The copy is updated when the Ultimate sends CMD_WIFI_DISABLE or CMD_WIFI_ENABLE.
    • A write that fails is retried: at once and then every 2 s while Disabled or idle without a timeout, otherwise at the reconnect pace; up to 5 writes.
    • A module that restarts on its own (crash, watchdog, brownout) and starts its radio tells the Ultimate first. While the setting is Disabled the Ultimate asks for the radio off again, so a stale copy cannot leave the radio on until the next boot.
  • The Ultimate's setting wins, through the WiFi task. There is no push at module detection any more. The Elite II / C64 Ultimate take WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041's path for the U64 and U2+L:
    • The WiFi task applies the setting once it has found the module, and on every change.
    • It drops the module's link events until EVENT_DISABLED confirms a radio off.
    • A radio off that is never confirmed is asked for again at each 10 s deadline. After 3 waits the user is told, the link is held down, and the radio off is asked for every 60 s.
    • A radio on that fails is asked for again in the same way.
    • A stale copy (module swapped, write given up) is therefore corrected by the first request after boot, and that request is confirmed.
  • Recovery is unchanged. Network recovery (Recover a U64-II over Wi-Fi from a signed manifest #1012) still brings the radio up through Autoconnect while the module is Disabled, and leaves the stored setting alone. A Disable that arrives while a recovery runs is answered ESP_ERR_INVALID_STATE: the copy is stored, and the radio stays on for the download. The Ultimate then waits for nothing and says nothing.

The default is Enabled, so nothing changes for a unit that never used Disable. No new RPC. The Wake on Wi-Fi notices that were part of the first version of this PR will be in their own PR.

Why

#589 reports that Disable is not persisted on the C64 Ultimate, and the maintainer agreed it is a bug. The off state lived only in the module's RAM, and the Ultimate had nothing stored. So after a mains cycle the module associated on its own while the Ultimate was still starting.

#1041 gave the U64 and U2+L one switch with a stored setting. The Elite II / C64 Ultimate module already had the radio switch, but not the storage. This change gives it the same setting, kept where it is needed before the Ultimate runs. The plan was discussed on #589.

Alternatives considered

  • Pushing the setting at module detection (the first version of this PR). The answer was ignored and nothing waited for the radio off, so a rejected or lost Disable left the row Disabled with the radio associated. A change of the setting during detection could also leave the radio opposite to the setting. It is replaced by WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041's task path, which serialises the requests and confirms the radio off.
  • A query call that returns the module's copy ({enabled, stored, write_failed}), so that the Ultimate could detect a mismatch. Not needed: the Ultimate applies its value once it has found the module and waits for the confirmation, and the commands are idempotent. A query would add an RPC number and a handler in the module for no decision that the confirmed request does not already make.
  • Keeping the Enable and Disable actions beside the setting. They would be a second way to set the same thing. set_enabled() covers every call, so the actions, cfg_enable / cfg_disable, and their item numbers CFG_WIFI_ENA / CFG_WIFI_DIS are removed.
  • Storing the setting only in the Ultimate, and having the module wait for it at boot (as raw_u64 and raw_c3 do: radio off until told). That leaves a window with the radio off at every boot of an Enabled unit. It also changes the module's start for an older Ultimate that never sends Enable. Reading its own copy keeps an old Ultimate working: the module starts the radio unless it was told Disabled, and an old Ultimate's Enable (or Autoconnect) starts it again.
  • Making recovery clear the stored Disabled. Recovery is a repair action, not a user choice, so WiFi stays off after the next boot, as the user left it.

Technical details, per commit

No line endings were changed: git diff --stat equals git diff --ignore-cr-at-eol --stat for every commit.

  1. 3ab1e55 Store WiFi Enabled on the Elite II / C64 Ultimate: a Disable survives mains-out.
    • Ultimate side (network_esp32.cc/.h, wifi.cc). The Elite II row becomes the U64's: CFG_WIFI_ENABLE, "WiFi Enabled", Enabled by default. Its change hook and set_enabled() are no longer U64 != 2 only.
    • Module (u64ctrl/main).
      • power_get_wifi_enabled() / power_set_wifi_enabled() keep the setting under the NVS key "wifion", next to the Wake on Wi-Fi key.
      • wifi_start_at_boot() replaces the esp_wifi_start() in wifi_init(). With the copy at Disabled, it leaves the radio stopped and sets disabled_g.
      • connector_boot_state() starts the connector in Disabled in that case, otherwise in LastAP.
      • CMD_WIFI_DISABLE stores 0 first. When the radio is off already, it confirms (EVENT_DISABLED) without stopping it again.
      • CMD_WIFI_ENABLE stores 1, then starts the radio if Disabled.
      • enable_wifi_if_needed(), the Autoconnect path that recovery uses, does not touch the copy.
  2. e4e41f5 Review fixes.
    • The netif's own input and output slots are recorded at init and again at STA_START. A Disabled boot has no netif yet, so before this a later Enable and a machine-off wrote NULL into it.
    • A Disable during a recovery only stores the copy.
  3. 96d7b1a Send the Elite II / C64 Ultimate radio requests from the WiFi task, and confirm a radio off.
  4. 23c6c64 Retry a failed write of the stored WiFi Enabled in the control module. The value whose write failed is kept (store_pending) and written again: at once and then every 2 s while Disabled or idle without a timeout, otherwise at the reconnect pace; up to 5 writes, with one warning. The idle waits are bounded while a write is due. A newer request replaces the pending value. A comment in enable_wifi_if_needed says why a recovery's Autoconnect leaves the radio on with Disabled stored.
  5. (Reverted by 39796ff; moves to its own PR.) 3d07467 Tell the user that Wake on Wi-Fi needs WiFi enabled. The notice is posted from the "WiFi Enabled" hook and from U64Config::setWakeOnWifi.
  6. 93c4f29 Clear the control module's Disabled flag before the event of a radio start. A connection query answered in between sent EVENT_DISABLED after the start's EVENT_DISCONNECTED, and the Ultimate ended in Disabled with the radio on.
  7. 136e9c7 (test only): the control module's refusal of a radio off, at runtime and at a later ask.
  8. dafb1c6 (test only): the timed write retries, and a newer Disable over a failed Enable.
  9. (Reverted by 39796ff; moves to its own PR.) 4587750 Tell of Wake on Wi-Fi also for a Disabled from a reset or load of the settings.
  10. (Reverted by 39796ff; moves to its own PR.) 22b783f Document that Wake On Wi-Fi needs WiFi Enabled, and tell it only once the setting is stored. This adds the help line under Power Settings.
  11. a4ee999 Write an Enable to the control module's copy also when the copy cannot be read. A read error gave the default, Enabled, so an Enable wrote nothing over a stored Disabled.
  12. 98c347c (test only): the legacy models fix1 and fix7 now change what they say they change.
  13. 39796ff Move the Wake on Wi-Fi notices and help text out of this PR. One new commit that reverts items 5, 9 and 10. No history was rewritten.
  14. 54731c2 Tell the Ultimate when the Elite II / C64 Ultimate control module restarts, and act on it.
  • Module. setup_modem() maps the reset reason (panic, watchdog, brownout) to self_restart_g. connector_boot_state() sends EVENT_RESTARTED before any link event when it starts the radio after such a restart. After a power-up, or with the radio left off, it sends nothing. The connector now starts once the UART is on its pins, so that the event is not lost.
  • Ultimate side (wifi.cc). The EVENT_RESTARTED case is no longer U64 != 2 only. While Disabled it asks for the radio off again and drops the link events until that is confirmed, as at boot.
  • Tests: two module cases in module_persist_test.cpp, and the two EVENT_RESTARTED cases of switch_task_test.cpp now also run for U64 == 2.
  1. b93a12d Answer a control module restart by the setting, not by the state the WiFi task is in. After an Enable the task's state stays Disabled until the module's radio events arrive, and a restart event in that gap was answered with a radio off although the setting said Enabled. The handler now asks for the radio off only while the setting is Disabled. WifiSwitchTask.module_restarted_while_an_enable_is_on_its_way fails without the change and passes with it.

Identity. u64ctrl's IDENT_STRING and IDENT_MINOR are bumped together, with a changelog line. The number is a placeholder for the maintainer. raw_u64 and raw_c3 are not touched beyond #1041.

Tests

Host tests

At this head (b93a12d34), every host test directory passes, run one by one on macOS and in an Ubuntu 24.04 container. Each suite has failing-before models (-red).

Case Suite Red model
(a) A stored Disabled survives a simulated boot: the radio is not started at boot nor by the connector, and the connector waits in Disabled. A stored Enabled still starts it. software/wifi/tests, u64ctrl: module_persist_test.cpp module 13
(b) Menu Disable and Enable, and the Yes of the prompt, set and save the setting: one write, none when unchanged. software/io/wifi/tests: persist_task_test.cpp, disabled_task_test.cpp legacy/fix7
(c) The setting is applied once the module is found. A radio off that is rejected, not sent, or never carried out never lets the link up; after 3 waits the user is told, then it is asked for at the slow pace. A change made while the module is being told wins. persist_task_test.cpp, switch_task_test.cpp fix18, fix21, fix10, fix7
(d) A recovery refuses the radio off at boot, at runtime, or at a later ask: nothing is waited for, nothing is said, and the link follows the module. Recovery's Autoconnect from a stored Disabled starts the radio and leaves the copy. both fix19, fix23, fix24; module 13, 15
(e) A failed NVS write is retried: once, twice, three times (after the timed waits), or given up after 5. A newer request overwrites a pending write. An Enable is written when the copy cannot be read. module; target/pc/linux/powerstate (the real power_state.c, with an NVS stub that can fail reads and writes) module 16, 16b, 16c
(f) The Disabled flag is cleared before the event of a radio start. module module 17
(g) A module restart of its own with the radio started tells the Ultimate first (EVENT_RESTARTED before any link event); with the radio off, or after a power-up, it tells nothing. While Disabled the Ultimate asks for the radio off again; while the setting is Enabled it does not, also in the gap after an Enable. module: module_persist_test.cpp; switch_task_test.cpp see items 14 and 15
(h) Disabled boot, then Enable, then machine-off: the netif slots are recorded and put back (not NULL). A machine-off before any radio start writes no NULL. wifi_init() starts the radio only through wifi_start_at_boot(). module module 14; a Makefile source check

Counts:

Suite Passed
io/wifi (U64=1 / U2+L / U64=2) 105 / 105 / 108
wifi_module_test raw_u64 61, raw_c3 61, u64ctrl 60; radio 7
powerstate 61 checks

Every model fails as required: 69 task-side and 50 module-side runs.

As in #1041, the -red targets run against a sed model of the code before the change, not against master, because the suites cut functions out of files that only exist in the new form.

Firmware builds

These all built with no errors at 98c347c: u64ii (with the u64ctrl blob), u64, raw_c3, and the U2+L and U2+ applications. For the hardware runs below the C64 Ultimate image was built at bf7e862; CI builds every product at the current head.

Hardware

All on a C64 Ultimate on 2026-11-10 and 2026-11-11, times in UTC.

C64 Ultimate only. The Elite II was not tested. A Wi-Fi sniffer on the AP's channel counted the seconds in which the unit sent or received anything, and a ping of its WiFi address and a read of the setting and the WiFi row ran alongside. The C64 Ultimate's own software "Power Cycle" only switches the main regulators and the control module stays powered, so each leg uses a real mains-out: software power-off (PUT /v1/machine:poweroff), the mains plug out and back in, power on with the toggle. The sniffer's silence only counts because it saw the unit's traffic in the Enabled leg.

Test Image Result
RED: Disabled stored, power-off, mains-out, power on master firmware (584250385, no stored copy) The radio came up by itself: Disable at 16:19:33Z, REST back at 16:21:04Z, Link Up at the first read at 16:21:15Z, with ping and sniffer frames. The Disable did not survive.
Disabled stored, power-off, mains-out, power on bf7e862fd PASS: row and setting Disabled at +21 / +58 / +94 s after the REST API came back (16:33:06, 16:33:43, 16:34:19Z), ping 0/177, 0 s with unit traffic in the 180 s from before the mains-out to 95 s after power-on
Enabled stored, power-off, mains-out, power on, nobody touches the menu bf7e862fd PASS: associated again about 12 s after power returned (sniffer), ping answered from 14 s (16:36:58Z), REST back at 16:37:00Z, the row read Link Up with an IP at 16:37:07Z, the setting read Enabled
Disabled stored, mains-out, power on (2026-11-11) head b93a12d34 + #835 PASS: row Disabled before and after; 0 s with unit traffic over about 190 s after power-on (beacons from other networks seen throughout); 0 ping replies on WiFi, Ethernet answering
Enabled stored, mains-out, power on (2026-11-11) head b93a12d34 + #835 PASS: row back to Link Up with no flap; 103 ping replies and 0 timeouts after power-on; unit frames on the sniffer in every second after power-on

The images were flashed through the updater with the configuration reset (the head image with a bench-only updater that forces the module flash). The module's version on the row read as the new one after the update and as the old one before it.

Host tests only, no hardware row:

  • The module restart fix (54731c25d). A mains-out never makes the module restart on its own, and no crash, watchdog or brownout was induced. In setup_modem() the new start order (connector after the pin switch) and the mapping of the reset reason to "restarted on its own" have no host test either.
  • The EVENT_RESTARTED handler fix (b93a12d34). It is in the 2026-11-11 image, but a mains-out does not exercise it.

Not tested on hardware: the Elite II.

Caveats

  • An old Ultimate after this module. A module that was Disabled starts no radio at boot, even when the Ultimate that follows is an older one that never sends Enable; its Autoconnect or Enable starts it. That is intended, but a user who swaps to an old Ultimate sees WiFi stay off until they enable it there.
  • A failed NVS write is retried up to 5 times. If every try fails, the copy stays stale until the Ultimate applies its setting at the next boot. A mains-out between the Ultimate's save and the module's write has the same effect.
  • Machine-off. With Enabled stored, the control module stays associated while the machine is off (unchanged). With Disabled stored it sends nothing.
  • Scans. With the radio not started at boot, a scan is refused until an Enable or Autoconnect.
  • A lost copy. A copy that is lost, erased (the module erases its NVS on a version change) or unreadable reads as Enabled. Such a unit transmits at that boot until the Ultimate's Disable is confirmed.
  • A module that never answers costs the WiFi task its 10 s reply timeout at each ask (see WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041).

Not tested on hardware

  • Network recovery with WiFi Disabled: there is no recovery trigger on the bench. It is covered by host tests.
  • The Elite II. I have no Elite II, only a C64 Ultimate and an Ultimate 64 Elite (the first model); the hardware rows come from the C64 Ultimate.
  • The EVENT_RESTARTED restart path: host tests only, because a mains-out never restarts the module by itself.
  • The new start order in setup_modem() and the reset-reason mapping have no host test either.

Written with Claude Opus 5.5 in Claude Code.

@JC-000

JC-000 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto #1041's current head. #1041 gained commit dd4c881 (a Disable ends a pending pick before the module confirms) after this PR was opened. The two commits here are unchanged apart from the rebase. The hardware run above already used both together, and host tests pass on the rebased branch.

Written with Claude Opus 5.5 in Claude Code.

@chrisgleissner

Copy link
Copy Markdown
Collaborator

@JC-000 Nice one! Thanks for this fix. It will take me a few days to review.

@chrisgleissner chrisgleissner added network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. labels Oct 31, 2026
@chrisgleissner

chrisgleissner commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

@JC-000 Thank you for this PR, and for keeping the persistence part small and well covered by host tests.

Result: changes requested. Reviewed head a2cb321ab (base 0854973d6). This PR contains the commits of #1041, so every change I asked for there applies here too: see #1041 (comment) (items 1 to 8). Those changes should be made on #1041 and carried into this PR. The inline comments here are only for #1042's own commits.

What I reviewed

  • The two feature commits of this PR (power_state.c/.h, the wifi_modem.c changes in u64ctrl, the boot handshake and setting handling for U64 == 2 in wifi.cc and network_esp32.cc) and how they interact with WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041's commits, on both the Ultimate and the module side. Two reviewers worked independently from the code.
  • Host tests: software/io/wifi/tests (all three product variants), software/wifi/tests (raw_u64 49, raw_c3 49, u64ctrl 41, radio 7) and target/pc/linux/powerstate (55 checks) pass, and the -red models fail as intended. make host_tests stops at the filemanager SIGSEGV that also occurs on dd4c88195 and 03c70e6e4.
  • C64 Ultimate hardware (exact head application over JTAG, u64ctrl V1.23 programmed and verified): menu Disable and Enable work. After Disable, a graceful shutdown (PUT /v1/machine:poweroff) and 10 s with the dedicated outlet off, WiFi stayed unreachable in 31 of 31 probes with V1.23, while with the WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041 module (V1.22) it was reachable in 59 of 59. JTAG loading is volatile, so the boot after the power cycle ran the flashed application, not this PR. The result shows that the module keeps the setting across power loss; it does not show a cold boot of the exact PR application. U64 Elite and U2+L: the Disable behaviour of WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041 also holds at this head.

Changes requested (this PR's own commits)

  1. A failed stored-Disabled push at boot is dropped (wifi.cc:313, U64 == 2). The result of wifi_disable() is ignored and no confirmation deadline exists on this target. After a rejected or untransmitted push, the application accepts the module's Connected status and shows Disabled while the radio stays associated. Host reproduction in the inline comment. Check the result, retry a bounded number of times, and do not accept Connected as success while the push is unconfirmed.
  2. A failed NVS write is ignored (u64ctrl/main/wifi_modem.c:833, and the Enable branch). The radio goes off and EVENT_DISABLED is sent, while the stored value stays Enabled, so the radio comes up again at the next boot. No test has a failing write: the power_state stub always succeeds. Keep the result, retry from the connector and add a failing-write test.

Please check

  1. Window at module detection (wifi.cc:294-315). state = eWifi_AppDetected is set before the stored setting is read. A setting change in between runs the hook, which sends its request, and the task then sends the older value it had read, so the radio ends up opposite to the setting. The window is a few milliseconds. Set the state after the push block, or read the setting immediately before sending. Reasoned from the code.
  2. Wake on WiFi with a stored Disabled. The watcher listens on the station interface, which does not exist while the radio is stopped. With this PR a Disable survives a power loss, so a user with Wake on WiFi on and WiFi off has a wake that never works, with no indication. Either document that Wake on WiFi needs WiFi Enabled, or refuse to arm it while the radio is disabled.

Suggestions

  • After a recovery run that used Autoconnect, the radio is on while the stored value says Disabled, until reboot. This is right for a recovery that must reach the network. A short comment in enable_wifi_if_needed would help the next reader.
  • Add the failing-write case to power_state_test.c and module_persist_test.cpp (item 2).

Not tested

Cold boot of the exact PR application on the C64 Ultimate, the Ultimate 64 Elite II, RF capture, Wake on WiFi with the new netif hooks, and recovery-button Autoconnect with a stored Disabled setting. One earlier persistence attempt left the C64 Ultimate off after the outlet was restored; a second outlet cycle started it. I did not find the cause and do not attribute it to this PR.

Comment thread software/io/wifi/wifi.cc Outdated
if (netstack->enabled_in_config()) {
wifi_enable();
} else {
wifi_disable();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 Thank you for this PR. My review is in two parts: inline comments on the lines concerned, and one summary comment. Everything from #1041's commits that is in this PR applies here as well; those comments are on #1041 and I have not repeated them. Required change for #1042:

At boot the Ultimate pushes its stored setting to the control module (wifi_enable() or wifi_disable()). This is what repairs a module whose NVS disagrees with the setting (module swap, failed NVS write). The result is ignored. If wifi_disable() cannot be sent or the module rejects the enqueue, there is no confirmation deadline on U64 == 2, because RadioOff and its retry are U64 != 2 only. Initialisation continues, wifi_is_connected reports the module as connected and the Connected event is accepted. The application shows WiFi Enabled = Disabled while the radio stays associated, with no retry and no message.

Host reproduction (U64=2, stored Disabled, the module answers the disable with an error, link up, then a Connected event): fails with "failed stored-Disabled push must be retried" and "stored Disabled must not accept connected module after failed push". It needs a failed command plus a setting/module mismatch. A normal boot is not affected.

Change: check the return value. On failure retry a bounded number of times, and otherwise leave a visible failure state. Do not treat a Connected event as success while a Disable push is unconfirmed.

Comment thread software/u64ctrl/main/wifi_modem.c Outdated
switch(cmd->command) {
case CMD_WIFI_DISABLE:
// The setting is kept first: it is what the next boot goes by, whatever the radio does now
power_set_wifi_enabled(0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required change (also the Enable branch further down).

The result of power_set_wifi_enabled() is ignored. write_u8 logs a failure, but the code continues: the radio goes off, EVENT_DISABLED is sent, and the stored value stays Enabled. After the next mains-out the radio starts at boot (wifi_start_at_boot) until the Ultimate pushes its setting. The window is short because the Ultimate pushes its setting when it detects the module, but see my comment on wifi.cc line 313: that push can fail as well. Neither the power_state host test stub nor module_persist_test.cpp has a failing-write case, so this path is not tested.

Change: keep the result. On failure retry from the connector (for example at its next wake) and log it once. Add a host test with a failing write that asserts the radio state and the retry.

JC-000 and others added 12 commits November 6, 2026 08:50
… mains-out

On U64 == 2 the menu's Enable and Disable were actions with no stored state, and the module's
off state lived in RAM only, so a Disabled unit started its radio at every mains-in.

WiFi Enabled is now the same stored setting as on the Ultimate 64 (the one switch,
NetworkLWIP_WiFi::set_enabled), which replaces the two actions. u64ctrl keeps a copy in its NVS
and reads it before it starts the radio; the Ultimate passes its setting when it finds the
module, and that wins. Recovery's Autoconnect still starts the radio and leaves the copy alone.
u64ctrl's identity is bumped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Record the netif's input and output slots when the radio first starts (a Disabled boot has no
netif yet, so a later Enable and a machine-off wrote NULL into the netif); a Disable during a
recovery only stores the copy; tests for wifi_init's use of wifi_start_at_boot, the netif
slots, the recovery case and the stored copy's defaults in power_state.c.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd confirm a radio off

On U64 == 2 the module was told the stored setting at its detection, with the answer ignored,
and nothing waited for the radio off: after a rejected or lost Disable the module's Connected
was taken, and the row showed Disabled while the radio stayed associated. A change of the
setting between that read and the request could also leave the radio opposite to the setting.

The Elite II and C64 Ultimate now take the path of the U64 and U2+L (GideonZ#1041): the WiFi task
applies the setting when it has found the module and on every change (EVENT_RADIO_SETTING),
drops the module's link events until EVENT_DISABLED confirms a radio off, asks again at the
deadlines, holds the link down when the asks run out, and asks again for a radio on that
failed. The push at detection and CFG_WIFI_ENA/DIS are gone. The control module is never
held off.

u64ctrl answers a Disable during a recovery with ESP_ERR_INVALID_STATE (the radio stays on
for the download, the setting is stored); the Ultimate then waits for nothing, says nothing,
and the link follows the module, also when the refusal comes at a later ask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
u64ctrl ignored the result of power_set_wifi_enabled(): with a failed NVS write the radio went
off and EVENT_DISABLED was sent while the stored value stayed Enabled, so the radio came up
again at the next boot (and the same for an Enable).

The value whose write failed is now kept (store_pending) and written again while the
connector idles: at once, then every 2 s, up to 5 writes in all, with one warning. The idle
waits that have no bound (Connected, Disabled, a user Disconnect) are bounded while a write
is due. A newer Enable or Disable replaces the pending value.

A comment in enable_wifi_if_needed says why a recovery's Autoconnect leaves the radio on with
Disabled stored. Host tests: a failing write in power_state_test.c (the NVS stub can fail
nvs_set_u8) and in module_persist_test.cpp. u64ctrl's identity notes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… C64 Ultimate

Wake on Wi-Fi listens on the station interface, which a radio that is off does not have. With
a Disable that now survives a power cycle, a user with Wake on Wi-Fi on and WiFi Disabled had
a wake that never came, and nothing said so.

Setting Wake On Wi-Fi while WiFi is Disabled, and disabling WiFi while Wake On Wi-Fi is on,
now post "Wake on Wi-Fi needs WiFi enabled". Neither is refused: the setting is kept and works
once WiFi is enabled again. The settings are documented outside this repository, so there is
no doc line here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…start

The control module's Enable and enable_wifi_if_needed() sent EVENT_DISCONNECTED and then
cleared disabled_g. The dispatcher answers the Ultimate's connection query on its own task,
at a higher priority (wifi_send_connected_event): a query that landed in between sent
EVENT_DISABLED after the start's EVENT_DISCONNECTED, and the Ultimate ended in Disabled
with the radio on and the setting Enabled. The WiFi task asks for the connection state a
few ms after its boot Enable, so the window was reachable. The flag is now cleared first.

raw_u64 and raw_c3 clear it first already, and answer the query from their connector, in
order with the Enable. Host test: an Enable and an Autoconnect from a stored Disabled send
no EVENT_DISCONNECTED while disabled_g is set (red 17 models the old order). u64ctrl's identity
notes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…asked again

Only the boot's refusal (a recovery runs) had a host test: the handling of a refused Disable
from the menu or the REST API, and of a refusal at a later ask, could be removed with every
case still passing. Two U64 == 2 cases now cover them: a runtime Disable refused waits for
nothing, says nothing and keeps the link; a radio off queued and then refused when asked
again ends the wait after one deadline, without a message, and the link follows the
module. legacy/fix23 and fix24 model the two without their refusal handling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er a failed Enable

Every failing-write case failed once, so the retry at the top of the connector's next pass
always stored the value, and the 2 s bounded waits (Connected and Disabled) could be removed
with the suite still passing. The new case lets the write fail twice and three times, in
Disabled and in Connected, and requires the copy to be stored after the bounded waits (red
16b and 16c model each wait without its bound). The pending-write case also runs the other
way round: a stored Disable, an Enable whose write fails, then a Disable, which wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… settings

The notice "Wake on Wi-Fi needs WiFi enabled" came from the change hook of "WiFi Enabled"
only, which a reset or load of the settings does not run: a loaded Disabled with Wake on
Wi-Fi on stopped the wake without a word. The effectuate that applies such a change now
tells it too, when the WiFi task had applied Enabled and no change waits for it, so neither
at boot nor a second time for a change whose hook told already. Host test (U64 == 2), and
legacy/fix25 models the effectuate without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the setting is stored

The help under Power Settings now says that Wake On Wi-Fi needs WiFi Enabled. setWakeOnWifi
posted that notice before it wrote the setting to the control module, so a failed write
showed it and then "Wake on Wi-Fi not stored."; the notice now follows a write that worked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t be read

power_set_wifi_enabled() returned early when power_get_wifi_enabled() read the value it was
to write. A read error gives the default, Enabled, so an Enable then wrote nothing over a
stored Disabled, and the radio stayed off at the next boot. write_u8() compares with what
is stored itself and writes only a change, so the early return goes, and with it the log
line of a change and the "not read" line that every push printed on a module that never
stored the setting. The NVS stub can fail a read; power_state_test.c covers the case.
u64ctrl's identity notes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Patterns of both no longer matched the code once the Elite II / C64 Ultimate took the radio
requests into the WiFi task: fix7's boot-push lines (and its wifi_disable() line, which hit
only the U64 restart path) matched nothing, so its model still told the module the setting
at detection; fix1's radio-off test at the start, its boot radio on, and its RadioOff and
RadioOn of the menu matched nothing either. Both now take the boot's radio off and radio on
out, and fix1 stops and starts the application for Disable and Enable again. Both still
fail, as required.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. This PR is rebased onto #1041's new head, 6cfdc7b36, so every #1041 change in my reply there is carried here. That includes the behaviour that applies to all products: a radio off that is never confirmed keeps the link down and is asked for every 60 s, and a failed radio on is asked for again. This reply covers only this PR's own items, for the Elite II, the C64 Ultimate and u64ctrl.

The commit messages were cleaned, so the PR is force-pushed. The two commits you reviewed are now 3ab1e55fd and e4e41f5b9. They are adjusted only where they meet #1041's changes: the "WiFi Enabled" hook, the test harness, and the module version. The fixes follow them: 6cfdc7b36..98c347c4a, 12 commits including those two.

At 98c347c4a every host test passes:

Suite Result
io/wifi 105 / 105 / 108 (U64, U2+L, U64 == 2)
modules raw_u64 61, raw_c3 61, u64ctrl 60, radio 7
powerstate 61 checks

Every legacy model fails, as it must. In the test column, run and model mean the same as in my reply on #1041, with RED taken against a2cb321ab.

Changes requested

1. The stored-Disabled push at boot (96d7b1a70)

  • The Elite II and C64 Ultimate now take WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041's path. The WiFi task applies the setting once it has found the module and on every change (EVENT_RADIO_SETTING). Until EVENT_DISABLED confirms a radio off, it drops the module's link events.
  • The push at detection is gone, and with it CFG_WIFI_ENA/CFG_WIFI_DIS.
  • A radio off that is never confirmed now gets the same handling here as on the U64 (wifi.cc:470-497): it is asked for again at each deadline. After 3 waits the user is told, the link is held down, the module's frames are turned off, and the radio off is asked for every 60 s. With Disabled stored, the link is never taken.
  • During a recovery, u64ctrl refuses a radio off (ESP_ERR_INVALID_STATE), because the download needs the radio. The task then waits for nothing, says nothing, and the link follows the module.

Tests (all are models):

Test Models
WifiPersistTask.an_unconfirmed_radio_off_does_not_let_the_link_up (U64 == 2: a push that was never sent, one that was rejected, one that was queued but never carried out) fix21, fix18, fix7
a_runtime_disable_refused_for_a_recovery_waits_for_nothing fix19, fix23
a_refusal_when_asked_again_ends_the_wait fix19, fix24

2. A failed NVS write (23c6c6437, dafb1c6cb, a4ee9994e)

  • u64ctrl keeps a value whose write failed (store_pending) and writes it again while the connector idles: at once, then every 2 s, up to 5 writes in all, with one warning.
  • While a write is due, the idle waits are bounded. A newer Enable or Disable replaces the pending value.
  • power_set_wifi_enabled() no longer returns early when the stored copy cannot be read. A read error gave the default, Enabled, so an Enable wrote nothing over a stored Disabled.

Tests:

  • models LEGACY_16, 16b and 16c (u64ctrl): Persist.a_failed_write_of_a_disable_is_retried, a_failed_write_of_an_enable_is_retried, a_failed_write_while_connected_is_retried, a_write_that_fails_twice_or_three_times_is_retried_after_a_wait, a_write_that_keeps_failing_is_given_up and a_newer_request_overwrites_a_pending_write.
  • run: the power_state_test case "An Enable while the stored Disabled cannot be read" fails at a2cb321ab.

Please check

3. The window at module detection. It is closed by the same task path. radio_setting_due is cleared before the setting is read, so a change that comes after the read sets the flag again and is applied on the next pass.

Test, models fix10 and fix7: WifiPersistTask.a_change_while_the_module_is_told_wins. It flips the setting right after the task read it, both ways, and requires the last request to match the final setting.

4. Wake on Wi-Fi with Disabled stored (3d0746737, 45877502e, 22b783fa5)

We tell the user rather than refuse: the setting is kept and works once WiFi is enabled again. "Wake on Wi-Fi needs WiFi enabled" is shown in three cases:

  • Wake on Wi-Fi is set while WiFi is Disabled (once the setting is stored);
  • WiFi is disabled while Wake on Wi-Fi is on;
  • a reset or load of the settings sets Disabled while Wake on Wi-Fi is on.

The help under Power Settings now reads "It needs WiFi Enabled (WiFi settings)." (u64_config.cc:3071).

Tests:

  • model fix20: WifiPersistTask.disable_with_wake_on_wifi_tells_the_user;
  • models fix20 and fix25: a_disabled_loaded_with_wake_on_wifi_tells_the_user;
  • the u64_config.cc half was checked on hardware (below).

Suggestions

Suggestion Done
A comment in enable_wifi_if_needed on the recovery's Autoconnect 23c6c6437 (u64ctrl/main/wifi_modem.c:843-845).
A failing-write case in power_state_test.c and module_persist_test.cpp 23c6c6437, dafb1c6cb, a4ee9994e. The NVS stub can now fail both writes and reads.

Also changed

u64ctrl clears its Disabled flag before the event of a radio start (93c4f2990). The dispatcher answers a connection query on its own task. A query that arrived between the event and the clear sent EVENT_DISABLED after the start's EVENT_DISCONNECTED, and the Ultimate ended in Disabled with the radio on. The WiFi task sends that query a few ms after its boot Enable, so the window could be hit.

Test, model LEGACY_17: Persist.a_radio_start_clears_disabled_before_its_event.

Hardware (C64 Ultimate)

Setup. The image carried this PR as it stood before #1041's last two changes (frames off after the give-up, and the actions that ask while a radio off waits). The rest of the tree is the same as 98c347c4a. The C64 Ultimate rows in my reply on #1041 ran on this image too, so they also exercised this PR's Elite II / C64 Ultimate path.

Method. "Silent" is measured with a Wi-Fi sniffer on the AP's channel (seconds in which the device's MAC sent or received anything) and a ping of the WiFi address.

Test Result
Disabled stored; software power-off (PUT /v1/machine:poweroff); mains out ≥ 10 s; power on PASS. Row and setting read Disabled at +20 / +60 / +120 s. Ping 0/150. 0 tx / 0 rx seconds in the 161 s from power-on.
Disabled stored; power-off by holding the toggle; mains out; power on PASS. Disabled at each read. 0 tx / 0 rx seconds over 285 s (before, during and after the off).
Enabled stored; power-off; mains out; power on PASS. Link up, ping 150/150, no user action needed.
The setting is applied at boot, not taken from the module PASS. The row reads Disabled from the first read; no frame after power-on, so the link never came up.
Help line under Power Settings PASS. Present, under the Wake On Wi-Fi paragraph.
Notice when Wake on Wi-Fi is set while WiFi is Disabled PASS 3/3. Shown within 0.6 s; the setting is kept.
Notice when WiFi is disabled while Wake on Wi-Fi is on PASS
No notice where it does not apply (four combinations) PASS

Mains-out: 3/3.

Not run on hardware. The third notice case (a load of a stored Disabled with Wake on Wi-Fi on) stays host-tested (a_disabled_loaded_with_wake_on_wifi_tells_the_user). So does the recovery-button Autoconnect with Disabled stored, on the module side (Persist.recovery_autoconnect_starts_the_radio_and_leaves_the_copy). We have no Elite II.

Observation, not a change. With Enabled stored, the control module stays associated while the machine is off, which is what Wake on Wi-Fi needs. With Disabled stored, it sent nothing while off.

Module version. The u64ctrl version is now above every work-in-progress build, including the one from this PR's first version on your C64 Ultimate, so the updater installs it.

The make host_tests crash. It is the master filemanager bug described on #1041, not part of this PR.

Written with Claude Opus 5.5 in Claude Code.

Conflicts: took master's deletion of the nine legacy/*.sed models GideonZ#1042 had modified;
software/wifi/tests/Makefile keeps master's structure (no legacy/red targets) plus GideonZ#1042's
additions (FUNCS_MODEM entries, WIFI_STORE_ extraction, wifi_init check).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@chrisgleissner chrisgleissner left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JC-000 thanks for this. I checked the behaviour against the source: an old config has no CFG_WIFI_ENABLE, so it loads as Enabled and an upgrade does not turn WiFi off. Only u64ctrl changes, so the U64, U2+ and U2+L are unaffected. The host tests extract the production functions, and they fail when I make boot ignore the stored copy or make set_enabled skip the save.

Requesting changes on three points:

  1. Hardware at head. The hardware run in the description is a work-in-progress build and the Elite II was not tested. Please run the head on the Elite II and the C64 Ultimate: set Disable, power cycle, confirm the radio stays off from power-on; set Enable, power cycle, confirm it comes up without user action.
  2. Scope. The Wake on Wi-Fi notices and help text (commits 3d07467, 4587750, 22b783f, in u64_config.cc, u64_config.h and wifi.h) are not needed for #589. Please move them to their own PR.
  3. Module restart while Disabled. On the Elite II, in the remaining #if U64 != 2 block of wifi.cc, a module that restarts on its own is not re-synchronised, so with a stale copy the radio can come back on while the setting is Disabled until the next boot. Please fix it or list it with the other caveats.

@chrisgleissner
chrisgleissner self-requested a review November 10, 2026 12:51
JC-000 and others added 4 commits November 10, 2026 09:56
They are not needed for GideonZ#589. They go to their own PR. This reverts 3d07467,
4587750 and 22b783f.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…tarts, and act on it

A control module that restarted on its own (a crash, a watchdog, a brownout) read its own
copy of WiFi Enabled and, if that was stale Enabled, started the radio while the Ultimate's
setting was Disabled. Nothing told the Ultimate, so the radio stayed on until the next
boot: the EVENT_RESTARTED handling in wifi.cc was U64 != 2 only.

The control module now sends EVENT_RESTARTED before any link event when it starts its radio
after a restart of its own, and the Ultimate handles it on every product: while Disabled it
asks for the radio off again and drops the link events until that is confirmed. The
connector now starts once the UART is on its pins, so that the event is not lost. The
module's identity is 1.35, above every open work-in-progress change.

Host tests: Persist.a_restart_of_its_own_with_the_radio_started_tells_the_ultimate and
a_restart_of_its_own_with_the_radio_off_tells_nothing (module); the two EVENT_RESTARTED
cases of switch_task_test.cpp now run for U64=2 as well.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…WiFi task is in

After an Enable the task's state stays Disabled until the module's radio events arrive.
A restart event that came in that gap was answered with a radio off, although the setting
said Enabled. The handler now asks for the radio off only while the setting is Disabled.

Host test WifiSwitchTask.module_restarted_while_an_enable_is_on_its_way fails without the
change (a radio off follows the Enable) and passes with it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Where each point stands:

1. Hardware at head. Run on a C64 Ultimate. I have no Elite II, so it was not tested there.

The C64 Ultimate's own software "Power Cycle" only switches the main regulators; the control module stays powered and
never re-reads its stored copy. So each leg below uses a real mains-out: the machine is switched off by software, the
mains plug is pulled and put back, and the machine is switched on with the toggle. "Silent" is measured with a Wi-Fi
sniffer on the AP's channel (seconds in which the C64 Ultimate's MAC sent or received anything), plus a ping of its WiFi
address and a read of the setting and the WiFi row.

Leg Build Result
Before the change: Disable, mains-out a work-in-progress build without this change The radio comes up by itself: Link Up, ping answers and sniffer frames within about 15 s of power returning. The Disable did not survive.
Disable, mains-out, power on this PR's head (the build below) PASS. The setting and the row read Disabled at +21 / +58 / +94 s after the REST API came back. Ping 0/177. 0 seconds with C64 Ultimate traffic over the 180 s from before the mains-out to 95 s after power-on.
Enable, mains-out, power on, nobody touches the menu this PR's head PASS. The radio was associated again about 12 s after power returned (sniffer), pings answered from 14 s, the row read Link Up with an IP at +7 s after the REST API came back, the setting read Enabled.

The silent measure only counts because the same sniffer saw the unit's traffic in the Enabled legs (positive control).
The module's own version read as the new one on the row after the update, and as the old one before it.

The build was flashed through the updater with the configuration reset; it was built from the PR branch as it was before the
last commit below, which only changes a host-tested handler (see 3).

2. Scope. The three Wake on Wi-Fi commits are reverted on this branch in one new commit (no history rewrite). They will
be in their own PR, which targets master. Until this PR has merged, that PR will show this PR's changes as well, because
the notice reads the stored "WiFi Enabled" setting that this PR adds to the Elite II / C64 Ultimate. It is not opened yet.

3. Module restart while Disabled. Fixed in code, but only host-tested: a mains-out never makes the module restart on its own, and I did not
induce a crash, a watchdog or a brownout, so there is no hardware row for it.

  • The control module now sends EVENT_RESTARTED before any link event when it starts its radio after a restart of its own
    (crash, watchdog, brownout). The connector now starts after the UART pins are switched, so the event is not lost.
  • wifi.cc handles EVENT_RESTARTED on every product, not only U64 != 2. It asks for the radio off again when the
    setting is Disabled, and drops the link events until that is confirmed, as at boot. It decides by the setting, not by
    the WiFi task's state: after an Enable the state stays Disabled until the module's radio events arrive, and a restart
    event in that gap used to be answered with a radio off although the setting said Enabled.
  • Tests that fail without the change: the two EVENT_RESTARTED cases of switch_task_test.cpp, now also built for the Elite II /
    C64 Ultimate (without the wifi.cc change the radio off is not asked for again and the link comes up);
    WifiSwitchTask.module_restarted_while_an_enable_is_on_its_way; module cases
    Persist.a_restart_of_its_own_with_the_radio_started_tells_the_ultimate and a_restart_of_its_own_with_the_radio_off_tells_nothing.
  • Not covered by a host test: setup_modem() is not among the functions the tests extract, so the new start order (connector after the pin switch)
    and the mapping of the reset reason to "restarted on its own" have no test. Both are a few lines.
  • The module's version is raised again so that the updater installs it.

Your 5 November comments.

  • The unchecked result of the Disable push in wifi.cc: the push at module detection is gone. The WiFi task applies the
    setting, keeps the link down until the radio off is confirmed, asks again at each deadline, and tells the user after three
    unconfirmed waits (tests in disabled_task_test.cpp, switch_task_test.cpp).
  • The unchecked power_set_wifi_enabled() result in wifi_modem.c (Disable and Enable): the result is kept, a failure is logged
    once, and the write is retried, up to 5 writes in all. In Disabled, and while the connector idles with no timeout, the retry is at once and
    then every 2 s; while it waits for a reconnect the retry follows the reconnect pace instead. Host cases with a failing write assert the radio
    state and the retries (power_state_test.c, module_persist_test.cpp).
  • Everything from WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041 applies here: this PR rests on WiFi: Disable turns the radio off, plus the connect and AP-list fixes from #1031 #1041's task path.

Written with Claude Opus and Claude Sonnet in Claude Code.

@chrisgleissner

chrisgleissner commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, scope is now clean and the restart handling is in. Remaining before approval: (1) the hardware rows ran the build before the last commit, which changes the EVENT_RESTARTED handler; please repeat the Disable and Enable mains-out legs on the head. (2) The Elite II has no hardware result; if nobody has one, please say so in the description. (3) The restart path has no hardware row, only host tests; please list that with the other caveats. This is a larger change than the other WiFi fixes, so it should go after #1041 and the small items.

@JC-000

JC-000 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I added the missing caveats to the description: there is no Elite II result (I only have a C64 Ultimate and an Elite I), and the restart path has host tests only. I will repeat the Disable and Enable mains-out legs on the head and post the rows here. Understood on ordering: this goes after #1041 and the small items.

Written with Claude Opus and Claude Sonnet in Claude Code.

@JC-000

JC-000 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

I repeated the C64 Ultimate Disable and Enable mains-out legs on the current head (b93a12d), built with #835 and a bench-only updater that forces the module flash.

Leg Result
Disable, mains out Row Disabled before and after. 0 C64 Ultimate frames on a channel 1 sniffer over about 190 s after power-on (beacons from other networks seen throughout); 0 ping replies on WiFi, Ethernet answering.
Enable, mains out Row back to Link Up with no flap; 103 ping replies and 0 timeouts after power-on; C64 Ultimate frames in every second after power-on.

The rows are added to the hardware table in the description.

Written with Claude Opus and Claude Sonnet in Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Existing behavior is incorrect, broken, or regressed; includes supported documentation defects. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wi‑Fi Radio is always ON, Wi‑Fi Disable not Persisted

2 participants