Skip to content

esp32: Let network:start/1 run SNTP alone - #2487

Open
gabrielmancini wants to merge 1 commit into
atomvm:release-0.7from
iobee-io:network-sntp-alone
Open

gabrielmancini wants to merge 1 commit into
atomvm:release-0.7from
iobee-io:network-sntp-alone

Conversation

@gabrielmancini

Copy link
Copy Markdown

An interface that another driver or component brings up (a tunnel, a PPP stack outside the network driver) has an address and a default route without the network driver. Its clock still needs SNTP, and only network:start/1 sets SNTP up. A configuration with no STA, AP, ETH or PPP entry was refused with badarg. An sntp entry alone is now enough, and network:stop/0 stops it as before.

Test: test_sntp_alone in test_eth starts the network with only sntp, checks the process stays up, and stops it. The esp32 QEMU suite passes locally, except test_socket, which needs Internet.

We have run this on ESP32 nodes of an ESP-NOW mesh since mid-November: their clock comes from SNTP over the tunnel.

@petermm

petermm commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

AMP code:

Review: ESP32 SNTP-only startup

Commit: 7b938f7 — esp32: Let network:start/1 run SNTP alone

Recommendation: fix the false-success case before merging. The valid SNTP-only path uses the existing SNTP initializer, and explicit network:stop/0 still calls esp_sntp_stop(). One new startup-validation issue and one test gap remain.

P2 — Reject SNTP-only startup when SNTP was not configured

The new gate checks for the presence of sntp, not whether SNTP can be configured. For example, network:start([{sntp, []}]) now receives a successful driver startup acknowledgment: maybe_set_sntp() does nothing without a host, and start_network() replies ok anyway. An unconvertible host term has the same result. The singleton stays registered even though it started neither an interface nor SNTP. The parent commit rejected these configurations with driver badarg (the Erlang server reports it as {start_port_failed, badarg} after the asynchronous start).

Keep ignoring unusable optional SNTP settings when an interface was requested, but require the initializer to run when SNTP is the only service. Add regression cases for {sntp, []} and {sntp, [{host, invalid}]} that assert the driver rejection rather than relying on the initial {ok, Pid} from network:start/1.

diff --git a/src/platforms/esp32/components/avm_builtins/network_driver.c b/src/platforms/esp32/components/avm_builtins/network_driver.c
--- a/src/platforms/esp32/components/avm_builtins/network_driver.c
+++ b/src/platforms/esp32/components/avm_builtins/network_driver.c
@@ -1363,7 +1363,7 @@
     }
 }
 
-static void maybe_set_sntp(term sntp_config, GlobalContext *global)
+static bool maybe_set_sntp(term sntp_config, GlobalContext *global)
 {
     if (!term_is_invalid_term(sntp_config) && !term_is_invalid_term(interop_kv_get_value(sntp_config, host_atom, global))) {
         int ok;
@@ -1377,9 +1377,11 @@
             esp_sntp_setservername(0, host);
             sntp_set_time_sync_notification_cb(time_sync_notification_cb);
             esp_sntp_init();
             ESP_LOGI(TAG, "SNTP initialized with host set to %s", host);
+            return true;
         } else {
             ESP_LOGE(TAG, "Unable to locate sntp host in configuration");
         }
     }
+    return false;
 }
@@ -2227,8 +2229,9 @@
 #endif
     bool want_wifi = !term_is_invalid_term(sta_config) || !term_is_invalid_term(ap_config);
+    bool want_interface = want_wifi || !term_is_invalid_term(eth_config) || !term_is_invalid_term(ppp_config);
     // SNTP alone is for an interface another driver brought up.
-    bool want_sntp = interop_kv_get_value_default(config, sntp_atom, term_invalid_term(), ctx->global) != term_invalid_term();
-    if (UNLIKELY(!want_wifi && term_is_invalid_term(eth_config) && term_is_invalid_term(ppp_config) && !want_sntp)) {
+    term sntp_config = interop_kv_get_value(config, sntp_atom, ctx->global);
+    if (UNLIKELY(!want_interface && term_is_invalid_term(sntp_config))) {
         ESP_LOGE(TAG, "Expected STA, AP, ETH, PPP or SNTP configuration but got none");
         term error = port_create_error_tuple(ctx, BADARG_ATOM);
         port_send_reply(ctx, pid, ref, error);
@@ -2306,7 +2309,11 @@
     //
     // Set up simple NTP, if configured
     //
-    maybe_set_sntp(interop_kv_get_value(config, sntp_atom, ctx->global), ctx->global);
+    if (!maybe_set_sntp(sntp_config, ctx->global) && !want_interface) {
+        term error = port_create_error_tuple(ctx, BADARG_ATOM);
+        port_send_reply(ctx, pid, ref, error);
+        goto cleanup;
+    }
 
     //
     // Set the DHCP hostname, if STA mode is enabled

P3 — The smoke test checks acceptance, not the SNTP lifecycle

test_sntp_alone/0 runs after the prior case stops Ethernet. Its C wrapper does not bring up an external interface. The test would pass with either esp_sntp_init() or esp_sntp_stop() removed, provided the driver still acknowledged startup and closed the port on stop.

For the smoke test, replace the fixed sleep with a synchronous sta_status/0 call. The server handles it only after handle_continue/2 receives the driver startup reply. This checks acceptance without waiting a second; it does not verify time sync.

diff --git a/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl b/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
--- a/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
+++ b/src/platforms/esp32/test/main/test_erl_sources/test_eth.erl
@@ -68,15 +68,9 @@
 % SNTP alone, for an interface another driver brought up: started, not
 % refused as a config with no network.
 test_sntp_alone() ->
-    {ok, Pid} = network:start([{sntp, [{host, <<"pool.ntp.org">>}]}]),
-    Monitor = monitor(process, Pid),
-    receive
-        {'DOWN', Monitor, process, Pid, Reason} -> error({sntp_alone, Reason})
-    after 1000 -> ok
-    end,
-    demonitor(Monitor, [flush]),
-    ok = network:stop(),
-    flush().
+    {ok, _Pid} = network:start([{sntp, [{host, <<"pool.ntp.org">>}]}]),
+    inactive = network:sta_status(),
+    ok = network:stop().
 
 test_static_ip() ->
     IpInfo = {{10, 0, 2, 42}, {255, 255, 255, 0}, {0, 0, 0, 0}},

For lifecycle coverage, reuse the C-owned Ethernet fixture. Assert a synchronized callback against a controlled NTP endpoint and that SNTP is disabled after explicit stop, before VM destruction (which also stops SNTP).

Scope caveat: externally owned Wi-Fi

stop_network() looks up and destroys default Wi-Fi netifs without checking that it created them. The parent already reached this teardown through terminate/2 after failed startup, so this is not a new teardown regression. If “an interface another driver brought up” includes externally managed default Wi-Fi, qualify that claim or address ownership separately.

Review based on source tracing and an independent oracle pass. No ESP32 runtime tests were run.

An interface another driver or component brings up has an address and a
default route without the network driver, but its clock still needs
SNTP, which only network:start/1 sets up. With no STA, AP, ETH or PPP
the configuration was refused with badarg; an sntp entry alone is now
enough, and network:stop/0 stops it as before. Alone, it must start:
with no host, or a host that is not a string, the driver still replies
badarg rather than acknowledging a start that runs nothing.

Signed-off-by: Gabriel Mancini <gabriel.mancini@gmail.com>
@gabrielmancini

Copy link
Copy Markdown
Author

Thanks, you're right about P2: the new gate accepted any sntp entry, and with no usable host it acknowledged a start that ran nothing. Amended into the commit:

  • P2: your change, taken as given. maybe_set_sntp/2 returns whether SNTP was started. When SNTP is the only service and it does not start, the driver replies badarg. With an interface, an unusable SNTP setting is still ignored, as before.
  • Regression cases: test_sntp_alone_badarg/0 starts [{sntp, []}] and [{sntp, [{host, invalid}]}] and waits for the server to go down with {start_port_failed, badarg}. That checks the driver's rejection, not the initial {ok, Pid}.
  • P3: the smoke test now calls network:sta_status/0 instead of sleeping for a second, as you suggested.
  • Placement: I had also put test_sntp_alone/0 between test_static_ip/0 and its comment. It now sits after expect_badarg/1.
  • Wording: the comment in the driver now says "an interface brought up outside this driver, from C or by another port driver" instead of "another driver".

On the scope caveat: agreed. stop_network() destroying default Wi-Fi netifs it did not create is an ownership question of its own. I've narrowed the claim rather than address it here.

QEMU (esp32, ESP-IDF v5.5.5, no JIT): test_eth passes with both new cases (the log shows Unable to locate sntp host in configuration for the invalid host). test_socket fails on this machine with or without the change; it needs outside network from QEMU, and the non-JIT esp32 jobs here pass it.

The failing jobs (Elixir master Code.ensure_loaded?/1, wasm -Wunsequenced-volatile in mbedTLS, ESP32 JIT test_ssl with spi_flash_mmap(INST) failed, macOS test_net_kernel timeout) fail the same way on #2488 and #2489.

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.

2 participants