Skip to content

Commit

Permalink
Add ntp server ip to settings menu and ntp via dhcp support. Prepare …
Browse files Browse the repository at this point in the history
…manual ntp server ip entry. Add M1702 gcode for setting ntp ip addr manually.
  • Loading branch information
bkerler committed Jan 13, 2024
1 parent 6f686bf commit f7585a7
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/gui/MItem_lan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ MI_IP4_GWAY::MI_IP4_GWAY()
MI_MAC_ADDR::MI_MAC_ADDR()
: WiInfo<MAC_LEN>(_(label), nullptr, is_enabled_t::yes, is_hidden_t::no) {
}

MI_NTP_IP4_ADDR::MI_NTP_IP4_ADDR()
: WiInfo<ADDR_LEN>(_(label), nullptr, is_enabled_t::yes, is_hidden_t::no) {
}

/*****************************************************************************/
// MI_NTP_VIA_DHCP
MI_NTP_VIA_DHCP::MI_NTP_VIA_DHCP()
: WI_ICON_SWITCH_OFF_ON_t(bool(config_store().lan_ntp_via_dhcp.get()), _(label), nullptr, is_enabled_t::yes, is_hidden_t::no) {}

void MI_NTP_VIA_DHCP::OnChange(size_t /*old_index*/) {
bool enabled = config_store().lan_ntp_via_dhcp.get();
config_store().lan_ntp_via_dhcp.set(!enabled);
}
15 changes: 15 additions & 0 deletions src/gui/MItem_lan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,18 @@ class MI_MAC_ADDR : public WiInfo<MAC_LEN> {
public:
MI_MAC_ADDR();
};

class MI_NTP_IP4_ADDR : public WiInfo<ADDR_LEN> {
static constexpr const char *const label = GuiDefaults::ScreenWidth > 240 ? N_("NTP IPv4 Address") : N_("NTP IP");

public:
MI_NTP_IP4_ADDR();
};

class MI_NTP_VIA_DHCP : public WI_ICON_SWITCH_OFF_ON_t {
constexpr static const char *const label = N_("NTP via DHCP");

public:
MI_NTP_VIA_DHCP();
virtual void OnChange(size_t old_index) override;
};
4 changes: 4 additions & 0 deletions src/gui/screen_menu_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "netdev.h"
#include "network_gui_tools.hpp"
#include <http_lifetime.h>
#include <sntp.h>

ScreenMenuNetwork::ScreenMenuNetwork()
: ScreenMenuNetwork__(_(label)) {
Expand All @@ -27,8 +28,11 @@ void ScreenMenuNetwork::refresh_address() {
netdev_get_ipv4_addresses(active_netdev, &ethconfig);
stringify_address_for_screen(str, sizeof(str), ethconfig, ETHVAR_MSK(ETHVAR_LAN_ADDR_IP4));
Item<MI_IP4_ADDR>().ChangeInformation(str);
const ip_addr_t *ntp_server = sntp_getserver(0);
Item<MI_NTP_IP4_ADDR>().ChangeInformation(ipaddr_ntoa(ntp_server));
} else {
Item<MI_IP4_ADDR>().ChangeInformation(UNKNOWN_ADDR);
Item<MI_NTP_IP4_ADDR>().ChangeInformation(UNKNOWN_ADDR);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/screen_menu_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using ScreenMenuNetwork__ = ScreenMenu<EFooter::Off, MI_RETURN, MI_PRUSALINK,
#if BUDDY_ENABLE_CONNECT()
MI_PRUSA_CONNECT,
#endif
MI_NET_INTERFACE_t, MI_IP4_ADDR, MI_MAC_ADDR, MI_METRICS_SETTINGS, MI_ETH_SETTINGS, MI_WIFI_SETTINGS>;
MI_NET_INTERFACE_t, MI_IP4_ADDR, MI_MAC_ADDR, MI_NTP_VIA_DHCP, MI_NTP_IP4_ADDR, MI_METRICS_SETTINGS, MI_ETH_SETTINGS, MI_WIFI_SETTINGS>;

class ScreenMenuNetwork : public ScreenMenuNetwork__ {
public:
Expand Down
1 change: 1 addition & 0 deletions src/marlin_stubs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target_sources(
G64.cpp
gcode.cpp
M123.cpp
M1702.cpp
M300.cpp
M330.cpp
M340.cpp
Expand Down
35 changes: 35 additions & 0 deletions src/marlin_stubs/M1702.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file
*/
#include "../../lib/Marlin/Marlin/src/gcode/gcode.h"
#include "../../lib/Marlin/Marlin/src/gcode/queue.h"
#include "PrusaGcodeSuite.hpp"
#include "selftest_esp.hpp"
#include <sntp_client.h>
/**
* Set ntp server ip
*
* Used for setting the ntp server ip manually
*
* ## Parameters
*
* - `I` - Set ntp ip address, for example: `I "192.168.0.1"`
*/
void PrusaGcodeSuite::M1702() {
char ip_address[config_store_ns::lan_hostname_max_len + 1] = { 0 };
if (parser.seen('I')) {
const char *ip_str = nullptr;
if ((ip_str = strstr(parser.string_arg, "\"")) != nullptr) {
++ip_str;
strncpy(ip_address, ip_str, sizeof(ip_address));
for (char *fn = ip_address; *fn; ++fn) {
if (*fn == '"') {
*fn = '\0';
break;
}
}
config_store().lan_ntp_server.set(ip_address);
sntp_client_static_init(ip_address);
}
}
}
1 change: 1 addition & 0 deletions src/marlin_stubs/PrusaGcodeSuite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void M1600(); ///< Menu change filament. Prusa STM32 platform specific
void M1601(); ///< Filament stuck detected, Prusa STM32 platform specific
void M1700(); ///< Preheat. Prusa STM32 platform specific
void M1701(); ///< Autoload. Prusa STM32 platform specific
void M1702(); ///< Set ntp server ip

#if HAS_TOOLCHANGER()
void P0(); ///< Tool park
Expand Down
4 changes: 4 additions & 0 deletions src/marlin_stubs/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ bool GcodeSuite::process_parsed_command_custom(bool no_ok) {
case 1701:
PrusaGcodeSuite::M1701();
break;
case 1702:
PrusaGcodeSuite::M1702();
break;
default:
processed = false;
break;
Expand Down Expand Up @@ -243,6 +246,7 @@ void __attribute__((weak)) PrusaGcodeSuite::M1600() { log_error(PRUSA_GCODE, "M1
void __attribute__((weak)) PrusaGcodeSuite::M1601() { log_error(PRUSA_GCODE, "M1601 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::M1700() { log_error(PRUSA_GCODE, "M1700 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::M1701() { log_error(PRUSA_GCODE, "M1701 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::M1702() { log_error(PRUSA_GCODE, "M1702 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::G64() { log_error(PRUSA_GCODE, "G64 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::G162() { log_error(PRUSA_GCODE, "G162 unsupported"); }
void __attribute__((weak)) PrusaGcodeSuite::G163() { log_error(PRUSA_GCODE, "G163 unsupported"); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ namespace defaults {
inline constexpr std::array<char, wifi_max_ssid_len + 1> wifi_ap_ssid { "" };
inline constexpr std::array<char, wifi_max_passwd_len + 1> wifi_ap_password { "" };

// default ntp server: ip of Czech CESNET NTP server tak.cesnet.cz, until user changable
inline constexpr std::array<char, lan_hostname_max_len + 1> ntp_server { "195.113.144.238" };

inline constexpr eSOUND_MODE sound_mode { eSOUND_MODE::_undef };
inline constexpr uint8_t sound_volume { 5 };
inline constexpr uint16_t language { 0xffff };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ struct CurrentStore : public journal::CurrentStoreConfig<journal::Backend, backe

StoreItem<int8_t, defaults::lan_timezone, journal::hash("LAN Timezone")> timezone; // hour difference from UTC

StoreItem<std::array<char, lan_hostname_max_len + 1>, defaults::ntp_server, journal::hash("NTP Server")> lan_ntp_server; // ntp server hostname
StoreItem<bool, defaults::bool_false, journal::hash("NTP via DHCP")> lan_ntp_via_dhcp; // use dhcp server for ntp

// WIFI settings
// wifi_flag & 1 -> On = 0/off = 1, lan_flag & 2 -> dhcp = 0/static = 1, wifi_flag & 0b1100 -> reserved, previously ap_sec_t security
StoreItem<uint8_t, defaults::uint8_t_zero, journal::hash("WIFI Flag")> wifi_flag;
Expand Down

0 comments on commit f7585a7

Please sign in to comment.