From 912ece4c3dd486bcd62f0d0dfee760b9f01caac6 Mon Sep 17 00:00:00 2001
From: Philippe Reynes <philippe.reynes@softathome.com>
Date: Fri, 18 Sep 2020 14:13:02 +0200
Subject: [PATCH] sntp: use udp framework

This commits update the support of sntp to use
the framework udp. This change allows to remove
all the reference to sntp in the main network
file net/net.c.

Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
---
 cmd/Kconfig                 |  1 +
 cmd/net.c                   | 10 +++++++++-
 {net => include/net}/sntp.h |  3 ++-
 net/net.c                   | 31 +------------------------------
 net/sntp.c                  | 29 +++++++++++++++++++++++++++--
 5 files changed, 40 insertions(+), 34 deletions(-)
 rename {net => include/net}/sntp.h (94%)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 30c358d7e7..999b6cf239 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1645,6 +1645,7 @@ config CMD_CDP
 
 config CMD_SNTP
 	bool "sntp"
+	select PROT_UDP
 	help
 	  Synchronize RTC via network
 
diff --git a/cmd/net.c b/cmd/net.c
index 9bbcdbcfe0..beb2877dfd 100644
--- a/cmd/net.c
+++ b/cmd/net.c
@@ -13,6 +13,8 @@
 #include <env.h>
 #include <image.h>
 #include <net.h>
+#include <net/udp.h>
+#include <net/sntp.h>
 
 static int netboot_common(enum proto_t, struct cmd_tbl *, int, char * const []);
 
@@ -356,6 +358,12 @@ U_BOOT_CMD(
 #endif
 
 #if defined(CONFIG_CMD_SNTP)
+static struct udp_ops sntp_ops = {
+	.prereq = sntp_prereq,
+	.start = sntp_start,
+	.data = NULL,
+};
+
 int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	char *toff;
@@ -380,7 +388,7 @@ int do_sntp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 	else
 		net_ntp_time_offset = simple_strtol(toff, NULL, 10);
 
-	if (net_loop(SNTP) < 0) {
+	if (udp_loop(&sntp_ops) < 0) {
 		printf("SNTP failed: host %pI4 not responding\n",
 		       &net_ntp_server);
 		return CMD_RET_FAILURE;
diff --git a/net/sntp.h b/include/net/sntp.h
similarity index 94%
rename from net/sntp.h
rename to include/net/sntp.h
index d3cbfbc69a..30b44d1c06 100644
--- a/net/sntp.h
+++ b/include/net/sntp.h
@@ -52,6 +52,7 @@ struct sntp_pkt_t {
 	unsigned long long transmit_timestamp;
 } __attribute__((packed));
 
-void sntp_start(void);	/* Begin SNTP */
+int sntp_prereq(void *data);
+int sntp_start(void *data);	/* Begin SNTP */
 
 #endif /* __SNTP_H__ */
diff --git a/net/net.c b/net/net.c
index 1ce0eb57a7..197fde3568 100644
--- a/net/net.c
+++ b/net/net.c
@@ -72,12 +72,6 @@
  *	We want:	- load the boot file
  *	Next step:	none
  *
- * SNTP:
- *
- *	Prerequisites:	- own ethernet address
- *			- own IP address
- *	We want:	- network time
- *	Next step:	none
  *
  * WOL:
  *
@@ -119,9 +113,6 @@
 #include "nfs.h"
 #include "ping.h"
 #include "rarp.h"
-#if defined(CONFIG_CMD_SNTP)
-#include "sntp.h"
-#endif
 #if defined(CONFIG_CMD_WOL)
 #include "wol.h"
 #endif
@@ -185,13 +176,6 @@ u32 net_boot_file_size;
 /* Boot file size in blocks as reported by the DHCP server */
 u32 net_boot_file_expected_size_in_blocks;
 
-#if defined(CONFIG_CMD_SNTP)
-/* NTP server IP address */
-struct in_addr	net_ntp_server;
-/* offset time from UTC */
-int		net_ntp_time_offset;
-#endif
-
 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
 /* Receive packets */
 uchar *net_rx_packets[PKTBUFSRX];
@@ -521,11 +505,6 @@ restart:
 			nc_start();
 			break;
 #endif
-#if defined(CONFIG_CMD_SNTP)
-		case SNTP:
-			sntp_start();
-			break;
-#endif
 #if defined(CONFIG_CMD_DNS)
 		case DNS:
 			dns_start();
@@ -1352,14 +1331,6 @@ static int net_check_prereq(enum proto_t protocol)
 		}
 		goto common;
 #endif
-#if defined(CONFIG_CMD_SNTP)
-	case SNTP:
-		if (net_ntp_server.s_addr == 0) {
-			puts("*** ERROR: NTP server address not given\n");
-			return 1;
-		}
-		goto common;
-#endif
 #if defined(CONFIG_CMD_DNS)
 	case DNS:
 		if (net_dns_server.s_addr == 0) {
@@ -1385,7 +1356,7 @@ static int net_check_prereq(enum proto_t protocol)
 			puts("*** ERROR: `serverip' not set\n");
 			return 1;
 		}
-#if	defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP) || \
+#if	defined(CONFIG_CMD_PING) || \
 	defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
 common:
 #endif
diff --git a/net/sntp.c b/net/sntp.c
index 39d7664a22..d5d5671933 100644
--- a/net/sntp.c
+++ b/net/sntp.c
@@ -12,12 +12,17 @@
 #include <net.h>
 #include <rtc.h>
 
-#include "sntp.h"
+#include <net/sntp.h>
 
 #define SNTP_TIMEOUT 10000UL
 
 static int sntp_our_port;
 
+/* NTP server IP address */
+struct in_addr	net_ntp_server;
+/* offset time from UTC */
+int		net_ntp_time_offset;
+
 static void sntp_send(void)
 {
 	struct sntp_pkt_t pkt;
@@ -93,7 +98,25 @@ static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
 	net_set_state(NETLOOP_SUCCESS);
 }
 
-void sntp_start(void)
+/*
+ * SNTP:
+ *
+ *	Prerequisites:	- own ethernet address
+ *			- own IP address
+ *	We want:	- network time
+ *	Next step:	none
+ */
+int sntp_prereq(void *data)
+{
+	if (net_ntp_server.s_addr == 0) {
+		puts("*** ERROR: NTP server address not given\n");
+		return 1;
+	}
+
+	return 0;
+}
+
+int sntp_start(void *data)
 {
 	debug("%s\n", __func__);
 
@@ -102,4 +125,6 @@ void sntp_start(void)
 	memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
 
 	sntp_send();
+
+	return 0;
 }
-- 
2.39.5