Repository navigation
esp32: Let network:start/1 run SNTP alone - #2487
gabrielmancini wants to merge 1 commit into
Conversation
|
AMP code: Review: ESP32 SNTP-only startupCommit: Recommendation: fix the false-success case before merging. The valid SNTP-only path uses the existing SNTP initializer, and explicit P2 — Reject SNTP-only startup when SNTP was not configuredThe new gate checks for the presence of 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 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 enabledP3 — The smoke test checks acceptance, not the SNTP lifecycle
For the smoke test, replace the fixed sleep with a synchronous 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 Scope caveat: externally owned Wi-Fi
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>
7b938f7 to
b38613a
Compare
|
Thanks, you're right about P2: the new gate accepted any
On the scope caveat: agreed. QEMU ( The failing jobs (Elixir master |
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/1sets SNTP up. A configuration with no STA, AP, ETH or PPP entry was refused withbadarg. Ansntpentry alone is now enough, andnetwork:stop/0stops it as before.Test:
test_sntp_aloneintest_ethstarts the network with onlysntp, checks the process stays up, and stops it. The esp32 QEMU suite passes locally, excepttest_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.