]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
net: extract function net_sntp_set_rtc() from sntp_handler()
authorJerome Forissier <jerome.forissier@linaro.org>
Wed, 25 Jun 2025 13:19:18 +0000 (15:19 +0200)
committerJerome Forissier <jerome.forissier@linaro.org>
Tue, 8 Jul 2025 09:07:37 +0000 (11:07 +0200)
Extract the code that sets the RTC clock from sntp_handler() in
net/sntp.c and make it a new function net_sntp_set_rtc() in
net/net-common.c. This will allow re-use with NET_LWIP.

According to [1] it is safe to assume that all devices have been
converted to DM_RTC so drop the useless code.

[1] https://lists.denx.de/pipermail/u-boot/2025-June/591376.html

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
include/net-common.h
net/net-common.c
net/sntp.c

index c04f86bdfccabd16ba0f21fbfa04df2d8532187c..d4512c3a4d19fc7c84b009b4945acd2fd99ead18 100644 (file)
@@ -574,4 +574,6 @@ extern struct wget_http_info default_wget_info;
 extern struct wget_http_info *wget_info;
 int wget_request(ulong dst_addr, char *uri, struct wget_http_info *info);
 
+void net_sntp_set_rtc(u32 seconds);
+
 #endif /* __NET_COMMON_H__ */
index e01b0da7d7b2268079084e4495b5fd6bc5023e29..b064557d5241e50436222c4ae2b49f1ca4fabd08 100644 (file)
@@ -1,5 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
+
+#include <dm/uclass.h>
 #include <net-common.h>
+#include <linux/time.h>
+#include <rtc.h>
 
 void copy_filename(char *dst, const char *src, int size)
 {
@@ -25,3 +29,22 @@ int wget_request(ulong dst_addr, char *uri, struct wget_http_info *info)
        wget_info = info ? info : &default_wget_info;
        return wget_do_request(dst_addr, uri);
 }
+
+void net_sntp_set_rtc(u32 seconds)
+{
+       struct rtc_time tm;
+       struct udevice *dev;
+       int ret;
+
+       rtc_to_tm(seconds, &tm);
+
+       ret = uclass_get_device(UCLASS_RTC, 0, &dev);
+       if (ret)
+               printf("SNTP: cannot find RTC: err=%d\n", ret);
+       else
+               dm_rtc_set(dev, &tm);
+
+       printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
+              tm.tm_year, tm.tm_mon, tm.tm_mday,
+              tm.tm_hour, tm.tm_min, tm.tm_sec);
+}
index 73d1d87d38b1aff2b1cf66a17076510969e32859..77cee0046bda67f742ac03ab6101ab65b84e5560 100644 (file)
@@ -57,8 +57,7 @@ static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                         unsigned src, unsigned len)
 {
        struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
-       struct rtc_time tm;
-       ulong seconds;
+       u32 seconds;
 
        debug("%s\n", __func__);
 
@@ -69,24 +68,8 @@ static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
         * As the RTC's used in U-Boot support second resolution only
         * we simply ignore the sub-second field.
         */
-       memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
-
-       rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
-#ifdef CONFIG_DM_RTC
-       struct udevice *dev;
-       int ret;
-
-       ret = uclass_get_device(UCLASS_RTC, 0, &dev);
-       if (ret)
-               printf("SNTP: cannot find RTC: err=%d\n", ret);
-       else
-               dm_rtc_set(dev, &tm);
-#elif defined(CONFIG_CMD_DATE)
-       rtc_set(&tm);
-#endif
-       printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
-              tm.tm_year, tm.tm_mon, tm.tm_mday,
-              tm.tm_hour, tm.tm_min, tm.tm_sec);
+       memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(seconds));
+       net_sntp_set_rtc(ntohl(seconds) - 2208988800UL + net_ntp_time_offset);
 
        net_set_state(NETLOOP_SUCCESS);
 }