diff mbox series

[1/3] net: extract function net_sntp_set_rtc() from sntp_handler()

Message ID 20250521151516.16729-2-jerome.forissier@linaro.org
State Superseded
Headers show
Series sntp for NET_LWIP | expand

Commit Message

Jerome Forissier May 21, 2025, 3:14 p.m. UTC
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.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
---

 include/net-common.h |  2 ++
 net/net-common.c     | 28 ++++++++++++++++++++++++++++
 net/sntp.c           | 23 +++--------------------
 3 files changed, 33 insertions(+), 20 deletions(-)

Comments

Heinrich Schuchardt June 2, 2025, 9:13 p.m. UTC | #1
Am 21. Mai 2025 17:14:41 MESZ schrieb Jerome Forissier <jerome.forissier@linaro.org>:
>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.
>
>Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
>---
>
> include/net-common.h |  2 ++
> net/net-common.c     | 28 ++++++++++++++++++++++++++++
> net/sntp.c           | 23 +++--------------------
> 3 files changed, 33 insertions(+), 20 deletions(-)
>
>diff --git a/include/net-common.h b/include/net-common.h
>index e536968a92b..a021bf503ff 100644
>--- a/include/net-common.h
>+++ b/include/net-common.h
>@@ -586,4 +586,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__ */
>diff --git a/net/net-common.c b/net/net-common.c
>index e01b0da7d7b..30d04323d4a 100644
>--- a/net/net-common.c
>+++ b/net/net-common.c
>@@ -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,27 @@ 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;
>+#ifdef CONFIG_DM_RTC

Are there really RTC drivers that have not been converted to the driver model. If yes, shouldn't we drop these?

Best regards

Heinrich

>+	struct udevice *dev;
>+	int ret;
>+#endif
>+
>+	rtc_to_tm(seconds, &tm);
>+
>+#ifdef CONFIG_DM_RTC
>+	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);
>+}
>diff --git a/net/sntp.c b/net/sntp.c
>index 73d1d87d38b..77cee0046bd 100644
>--- a/net/sntp.c
>+++ b/net/sntp.c
>@@ -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);
> }
Tom Rini June 3, 2025, 2:05 p.m. UTC | #2
On Mon, Jun 02, 2025 at 11:13:39PM +0200, Heinrich Schuchardt wrote:
> Am 21. Mai 2025 17:14:41 MESZ schrieb Jerome Forissier <jerome.forissier@linaro.org>:
> >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.
> >
> >Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
> >---
> >
> > include/net-common.h |  2 ++
> > net/net-common.c     | 28 ++++++++++++++++++++++++++++
> > net/sntp.c           | 23 +++--------------------
> > 3 files changed, 33 insertions(+), 20 deletions(-)
> >
> >diff --git a/include/net-common.h b/include/net-common.h
> >index e536968a92b..a021bf503ff 100644
> >--- a/include/net-common.h
> >+++ b/include/net-common.h
> >@@ -586,4 +586,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__ */
> >diff --git a/net/net-common.c b/net/net-common.c
> >index e01b0da7d7b..30d04323d4a 100644
> >--- a/net/net-common.c
> >+++ b/net/net-common.c
> >@@ -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,27 @@ 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;
> >+#ifdef CONFIG_DM_RTC
> 
> Are there really RTC drivers that have not been converted to the driver model. If yes, shouldn't we drop these?
 
It looks like all RTC drivers are converted (and there's no 'RTC' config
option). I think just openpiton_riscv64_spl needs to be converted (or
just config options switched) so that SPL_RTC can go away as a symbol
and SPL_DM_RTC (and SPL_DM) is required if we need an RTC in SPL.
diff mbox series

Patch

diff --git a/include/net-common.h b/include/net-common.h
index e536968a92b..a021bf503ff 100644
--- a/include/net-common.h
+++ b/include/net-common.h
@@ -586,4 +586,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__ */
diff --git a/net/net-common.c b/net/net-common.c
index e01b0da7d7b..30d04323d4a 100644
--- a/net/net-common.c
+++ b/net/net-common.c
@@ -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,27 @@  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;
+#ifdef CONFIG_DM_RTC
+	struct udevice *dev;
+	int ret;
+#endif
+
+	rtc_to_tm(seconds, &tm);
+
+#ifdef CONFIG_DM_RTC
+	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);
+}
diff --git a/net/sntp.c b/net/sntp.c
index 73d1d87d38b..77cee0046bd 100644
--- a/net/sntp.c
+++ b/net/sntp.c
@@ -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);
 }