]>
Commit | Line | Data |
---|---|---|
ea287deb WD |
1 | /* |
2 | * SNTP support driver | |
3 | * | |
4 | * Masami Komiya <[email protected]> 2005 | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <common.h> | |
9 | #include <command.h> | |
10 | #include <net.h> | |
11 | #include <rtc.h> | |
12 | ||
13 | #include "sntp.h" | |
14 | ||
49f3bdbb | 15 | #define SNTP_TIMEOUT 10000UL |
ea287deb WD |
16 | |
17 | static int SntpOurPort; | |
18 | ||
19 | static void | |
6c3234a3 | 20 | SntpSend(void) |
ea287deb WD |
21 | { |
22 | struct sntp_pkt_t pkt; | |
23 | int pktlen = SNTP_PACKET_LEN; | |
24 | int sport; | |
25 | ||
0ebf04c6 | 26 | debug("%s\n", __func__); |
ea287deb | 27 | |
6c3234a3 | 28 | memset(&pkt, 0, sizeof(pkt)); |
ea287deb WD |
29 | |
30 | pkt.li = NTP_LI_NOLEAP; | |
31 | pkt.vn = NTP_VERSION; | |
32 | pkt.mode = NTP_MODE_CLIENT; | |
33 | ||
594c26f8 | 34 | memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, |
6c3234a3 | 35 | (char *)&pkt, pktlen); |
ea287deb WD |
36 | |
37 | SntpOurPort = 10000 + (get_timer(0) % 4096); | |
38 | sport = NTP_SERVICE_PORT; | |
39 | ||
6c3234a3 JH |
40 | NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, |
41 | pktlen); | |
ea287deb WD |
42 | } |
43 | ||
44 | static void | |
6c3234a3 | 45 | SntpTimeout(void) |
ea287deb | 46 | { |
6c3234a3 | 47 | puts("Timeout\n"); |
ea287deb WD |
48 | NetState = NETLOOP_FAIL; |
49 | return; | |
50 | } | |
51 | ||
52 | static void | |
03eb129f LC |
53 | SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, |
54 | unsigned len) | |
ea287deb | 55 | { |
414eec35 | 56 | struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
ea287deb | 57 | struct rtc_time tm; |
414eec35 | 58 | ulong seconds; |
ea287deb | 59 | |
0ebf04c6 | 60 | debug("%s\n", __func__); |
ea287deb | 61 | |
6c3234a3 JH |
62 | if (dest != SntpOurPort) |
63 | return; | |
ea287deb | 64 | |
414eec35 WD |
65 | /* |
66 | * As the RTC's used in U-Boot sepport second resolution only | |
67 | * we simply ignore the sub-second field. | |
68 | */ | |
6c3234a3 | 69 | memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
ea287deb | 70 | |
414eec35 | 71 | to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); |
643d1ab2 | 72 | #if defined(CONFIG_CMD_DATE) |
6c3234a3 | 73 | rtc_set(&tm); |
ea287deb | 74 | #endif |
6c3234a3 | 75 | printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", |
ea287deb WD |
76 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
77 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
ea287deb WD |
78 | |
79 | NetState = NETLOOP_SUCCESS; | |
80 | } | |
81 | ||
82 | void | |
6c3234a3 | 83 | SntpStart(void) |
ea287deb | 84 | { |
0ebf04c6 | 85 | debug("%s\n", __func__); |
ea287deb | 86 | |
6c3234a3 | 87 | NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); |
ea287deb | 88 | NetSetHandler(SntpHandler); |
6c3234a3 | 89 | memset(NetServerEther, 0, sizeof(NetServerEther)); |
ea287deb | 90 | |
6c3234a3 | 91 | SntpSend(); |
ea287deb | 92 | } |