diff --git a/components/jetkvm_c5_wifi/jetkvm_c5_wifi.c b/components/jetkvm_c5_wifi/jetkvm_c5_wifi.c index d50b4abc..796dc414 100644 --- a/components/jetkvm_c5_wifi/jetkvm_c5_wifi.c +++ b/components/jetkvm_c5_wifi/jetkvm_c5_wifi.c @@ -1333,6 +1333,15 @@ static void c5_wifi_event_handler(void *argument, esp_event_base_t event_base, } if (event_id == WIFI_EVENT_STA_CONNECTED) { __atomic_store_n(&s_station_associated, true, __ATOMIC_RELEASE); + /* A radio stop (a band switch, hotspot mode) removes the station + * from lwIP, which clears lwIP's default interface, and IDF does + * not set a default chosen by hand again at the next start. + * Without one, lwIP has no route off the local network. */ + const esp_err_t default_error = esp_netif_set_default_netif(s_station); + if (default_error != ESP_OK) { + ESP_LOGE(TAG, "Wi-Fi station is not the default interface: %s", + esp_err_to_name(default_error)); + } /* The station interface starts with lwIP's own choice for IPv6; * the ipv6_mode setting decides from here on. */ jetkvm_network_settings_link_up(); diff --git a/tools/integration/tests/wifi_radio_restart.test.mjs b/tools/integration/tests/wifi_radio_restart.test.mjs new file mode 100644 index 00000000..53bffea3 --- /dev/null +++ b/tools/integration/tests/wifi_radio_restart.test.mjs @@ -0,0 +1,75 @@ +import assert from 'node:assert/strict'; +import path from 'node:path'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; +import { benchFile, readBench } from '../../combined_load/runner/build.mjs'; +import { connect, until } from '../lib/mini.mjs'; + +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..'); +const bench = readBench(benchFile(root, process.env.INTEGRATION_BENCH ?? '')); +const skip = bench.JETKVM_SKU === 'jetkvm-mini-wireless' ? false : 'the Mini has no Wi-Fi'; +const PRODUCTION = 'https://api.jetkvm.com'; +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + +// The Mini leaves its network for the band change: the page's session ends. +async function backOnline(mini, seconds) { + await sleep(5000); + return until(async () => { + await mini.open(); + return mini.rpc('getWifiSettings'); + }, settings => !settings.applying && settings.connected, seconds); +} + +// Right after a join the Mini's other TLS clients connect again and can hold +// the TLS memory that a check needs. A Mini without a route to the server +// does not get one in the wait. +async function reachesProduction(mini, when) { + try { + await until(() => mini.rpc('getUpdateStatus', {}, 30_000), status => Boolean(status.remote?.systemVersion), 30); + } catch (error) { + assert.fail(`the Mini does not reach ${PRODUCTION} ${when}: ${error.message}`); + } +} + +// A stop of the Wi-Fi radio takes the station out of lwIP, and with it lwIP's +// default interface. The Mini then still reaches its own subnet but no +// address beyond it, so the update server is the one on the internet. Each +// band change stops the radio, also a change to the band that the Mini is on, +// which keeps the bench's network. +test('the Mini reaches the update server on the internet after a Wi-Fi band change restarts its radio', { timeout: 300_000, skip }, async t => { + const online = await fetch(`${PRODUCTION}/releases?deviceId=jetkvm-integration&sku=${bench.JETKVM_SKU}`, + { signal: AbortSignal.timeout(30_000) }).then(response => response.ok, () => false); + if (!online) { + t.skip(`needs the internet: this machine does not reach ${PRODUCTION}`); + return; + } + const mini = await connect(); + let original = null; + t.after(async () => { + try { + const settings = original && await backOnline(mini, 240); + if (settings && settings.band !== original) { + await mini.rpc('setWifiBand', { band: original }); + await backOnline(mini, 240); + } + } finally { + await mini.close(); + } + }); + await mini.rpc('setUpdateApiUrl', { url: PRODUCTION }); + await mini.rpc('setDevChannelState', { enabled: false }); + const before = await mini.rpc('getWifiSettings'); + assert.ok(before.connected && before.channel > 0, `the Mini is on a Wi-Fi network: ${JSON.stringify(before)}`); + await reachesProduction(mini, 'before the band change'); + + const band = before.band === 'auto' ? (before.channel > 14 ? '5ghz' : '2.4ghz') : 'auto'; + original = before.band; + const changedAt = Date.now(); + await mini.rpc('setWifiBand', { band }); + const after = await backOnline(mini, 240); + t.diagnostic(`band ${before.band} to ${band} on channel ${before.channel}; back on ${after.ssid}, channel ${after.channel}, ` + + `after ${Math.round((Date.now() - changedAt) / 1000)} s`); + assert.equal(after.apply_failed, false, 'the band change succeeded'); + assert.equal(after.band, band); + await reachesProduction(mini, 'after the band change'); +});