Skip to content

Commit 857ac99

Browse files
wui: Allow overriding the NTP server from the settings ini
The SNTP client is hardcoded to prusa3d.pool.ntp.org and there is no way to point it elsewhere, so a printer on a network without a route to the internet can never obtain a time. On printers without a backup battery this is every boot, which leaves timestamps at 1970 and breaks TLS certificate validation for Prusa Connect deployments using hostname/custom_cert from this same ini. Add an ntp key to the [network] section of prusa_printer_settings.ini, stored in the config store beside the hostname and applied after sntp_init(), which unconditionally resets server 0 to the compiled-in default. The setting is empty by default and loading an empty value reverts to the default, so behaviour is unchanged for anyone who does not set it. BFW-6184 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent e96ce2b commit 857ac99

8 files changed

Lines changed: 45 additions & 3 deletions

File tree

doc/prusa_printer_settings.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ hostname=prusa
55
# Printer support two dns servers. If variables are not empty, they will be used
66
# even is DHCP pr AUTO type is set.
77
dns4=192.168.0.1;192.168.0.2
8+
# Hostname or IP of the NTP server used for time synchronization. When not
9+
# set, the built-in default (prusa3d.pool.ntp.org) is used. Loading an
10+
# empty value reverts to the default. Max 30 chars.
11+
# A .local (mDNS) name cannot be used: the printer answers mDNS queries
12+
# about itself but does not resolve mDNS names. On a network with no DNS
13+
# server at all, an IP address is the only form that works.
14+
# ntp=192.168.0.1
815

916
[eth::ipv4]
1017
# Type could be DHCP, STATIC or OFF.

lib/WUI/netif_settings.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
#define TURN_FLAG_ON(flg) (flg &= ~LAN_FLAG_ONOFF_POS) // flip lan switch flg to ON
1616
#define TURN_FLAG_OFF(flg) (flg |= LAN_FLAG_ONOFF_POS) // flip lan switch flg to OFF
1717

18-
#define HOSTNAME_LEN 20 // ethernet hostname MAX length
19-
#define SSID_MAX_LEN 32 // https://en.wikipedia.org/wiki/Service_set_(802.11_network)#SSID
20-
#define WIFI_PSK_MAX 64
18+
#define HOSTNAME_LEN 20 // ethernet hostname MAX length
19+
#define SSID_MAX_LEN 32 // https://en.wikipedia.org/wiki/Service_set_(802.11_network)#SSID
20+
#define WIFI_PSK_MAX 64
21+
#define NTP_SERVER_LEN 30 // NTP server hostname MAX length
2122

2223
typedef struct {
2324
uint8_t flag; // lan flags: pos0 = switch(ON=0, OFF=1), pos1 = type(DHCP=0, STATIC=1)
@@ -29,6 +30,7 @@ typedef struct {
2930

3031
typedef struct {
3132
char hostname[HOSTNAME_LEN + 1]; // ETH hostname: MAX 20 chars
33+
char ntp_server[NTP_SERVER_LEN + 1]; // user defined NTP server hostname or IP
3234
ip_addr_t dns1_ip4; // user defined DNS #1
3335
ip_addr_t dns2_ip4; // user defined DNS #2
3436
lan_t lan; // user defined LAN configurations

lib/WUI/sntp/sntp_client.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "sntp.h"
22
#include "sntp_client.h"
33
#include "netdev.h"
4+
#include "wui_api.h"
45
#include "tcpip.h"
56

67
#include <option/has_esp.h>
@@ -10,6 +11,13 @@ void sntp_client_init(void) {
1011
sntp_setoperatingmode(SNTP_OPMODE_POLL);
1112

1213
sntp_init();
14+
15+
// sntp_init() resets server 0 to the compiled-in default
16+
// (SNTP_SERVER_ADDRESS), so the override must be applied after it.
17+
const char *ntp_server = wui_get_ntp_server();
18+
if (ntp_server != NULL) {
19+
sntp_setservername(0, ntp_server);
20+
}
1321
}
1422

1523
void sntp_client_step(void) {

lib/WUI/wui_api.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ static int ini_handler_func(void *user, const char *section, const char *name, c
8181
if (ip4addr_aton(value, &tmp_config->lan.gw_ip4)) {
8282
tmp_config->var_mask |= ETHVAR_MSK(ETHVAR_LAN_GW_IP4);
8383
}
84+
} else if (ini_string_match(section, "network", name, "ntp")) {
85+
strlcpy(tmp_config->ntp_server, value, NTP_SERVER_LEN + 1);
86+
tmp_config->var_mask |= ETHVAR_MSK(ETHVAR_NTP_SERVER);
8487
} else if (ini_string_match(section, "network", name, "dns4")) {
8588

8689
if (NULL != strchr(value, ';')) {
@@ -177,6 +180,10 @@ void save_net_params(netif_config_t *ethconfig, [[maybe_unused]] ap_entry_t *ap,
177180
if (ethconfig->var_mask & ETHVAR_MSK(ETHVAR_HOSTNAME)) {
178181
store.hostname.set(ethconfig->hostname);
179182
}
183+
if (ethconfig->var_mask & ETHVAR_MSK(ETHVAR_NTP_SERVER)) {
184+
static_assert(NTP_SERVER_LEN == config_store_ns::ntp_server_size);
185+
store.ntp_server.set(ethconfig->ntp_server);
186+
}
180187

181188
#if HAS_ESP()
182189
if (ap != NULL) {
@@ -221,6 +228,7 @@ void load_net_params(netif_config_t *ethconfig, [[maybe_unused]] ap_entry_t *ap,
221228
}
222229

223230
strlcpy(ethconfig->hostname, store.hostname.get_c_str(), HOSTNAME_LEN + 1);
231+
strlcpy(ethconfig->ntp_server, store.ntp_server.get_c_str(), NTP_SERVER_LEN + 1);
224232

225233
#if HAS_ESP()
226234
if (ap != NULL) {
@@ -242,6 +250,11 @@ void get_MAC_address(mac_address_t *dest, uint32_t netdev_id) {
242250
}
243251
}
244252

253+
const char *wui_get_ntp_server(void) {
254+
const char *server = config_store().ntp_server.get_c_str();
255+
return (server[0] != '\0') ? server : NULL;
256+
}
257+
245258
void sntp_set_system_time(uint32_t sec) {
246259

247260
// RTC_TimeTypeDef has attributes like TimeFormat (AM/PM) and DayLightSaving, which we don't use

lib/WUI/wui_api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef enum {
4747
ETHVAR_TIMEZONE, // int8_t, timezone
4848
ETHVAR_DNS1_IP4, // ip_addr_t, dns1_ip4
4949
ETHVAR_DNS2_IP4, // ip_addr_t, dns2_ip4
50+
ETHVAR_NTP_SERVER, // char[30 + 1], ntp_server
5051
ETHVAR_MAC_ADDRESS, // is not included in ethconfig (used in stringifying for screen)
5152

5253
APVAR_SSID, // char[32 + 1], ap_entry_t::ssid
@@ -114,6 +115,13 @@ void get_MAC_address(mac_address_t *dest, uint32_t netdev_id);
114115
************************************************************************************************************/
115116
void sntp_set_system_time(uint32_t sec);
116117

118+
/*!**********************************************************************************************************
119+
* \brief Gets the user-configured NTP server, if any
120+
*
121+
* \return NTP server hostname or IP, or NULL when none is configured
122+
************************************************************************************************************/
123+
const char *wui_get_ntp_server(void);
124+
117125
/*!********************************************************************************
118126
* \brief Adds time in seconds to given timestamp
119127
*

src/persistent_stores/store_instances/config_store/constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ inline constexpr size_t sheets_num { 8 };
1717
inline constexpr float z_offset_uncalibrated { std::numeric_limits<float>::max() };
1818

1919
inline constexpr size_t lan_hostname_max_len { 20 };
20+
inline constexpr size_t ntp_server_size { 30 };
2021
inline constexpr size_t connect_host_size { 20 };
2122
inline constexpr size_t connect_proxy_size { 30 };
2223
inline constexpr size_t connect_token_size { 20 };

src/persistent_stores/store_instances/config_store/defaults.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace defaults {
5151
inline constexpr TestResult test_result_unknown { TestResult::unknown };
5252

5353
inline constexpr std::array<char, lan_hostname_max_len + 1> net_hostname { LAN_HOSTNAME_DEF };
54+
inline constexpr std::array<char, ntp_server_size + 1> ntp_server { "" };
5455
inline constexpr int8_t lan_timezone { 1 };
5556
inline constexpr time_tools::TimezoneOffsetMinutes timezone_minutes { time_tools::TimezoneOffsetMinutes::no_offset };
5657
inline constexpr time_tools::TimezoneOffsetSummerTime timezone_summer { time_tools::TimezoneOffsetSummerTime::no_summertime };

src/persistent_stores/store_instances/config_store/store_definition.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ struct CurrentStore
231231

232232
// General network settings
233233
StoreItem<std::array<char, lan_hostname_max_len + 1>, defaults::net_hostname, ItemFlag::network, journal::hash("Hostname")> hostname;
234+
// NTP server hostname or IP; when empty, the compiled-in default is used
235+
StoreItem<std::array<char, ntp_server_size + 1>, defaults::ntp_server, ItemFlag::network, journal::hash("NTP Server")> ntp_server;
234236

235237
StoreItem<SoundMode, defaults::sound_mode, ItemFlag::user_interface, journal::hash("Sound Mode")> sound_mode;
236238
StoreItem<uint8_t, defaults::sound_volume, ItemFlag::user_interface, journal::hash("Sound Volume")> sound_volume;

0 commit comments

Comments
 (0)