wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SNTP support driver |
| 3 | * |
| 4 | * Masami Komiya <mkomiya@sonare.it> 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 | |
| 15 | #if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_SNTP)) |
| 16 | |
| 17 | #define SNTP_TIMEOUT 10 |
| 18 | |
| 19 | static int SntpOurPort; |
| 20 | |
| 21 | static void |
| 22 | SntpSend (void) |
| 23 | { |
| 24 | struct sntp_pkt_t pkt; |
| 25 | int pktlen = SNTP_PACKET_LEN; |
| 26 | int sport; |
| 27 | |
| 28 | debug ("%s\n", __FUNCTION__); |
| 29 | |
| 30 | memset (&pkt, 0, sizeof(pkt)); |
| 31 | |
| 32 | pkt.li = NTP_LI_NOLEAP; |
| 33 | pkt.vn = NTP_VERSION; |
| 34 | pkt.mode = NTP_MODE_CLIENT; |
| 35 | |
| 36 | memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen); |
| 37 | |
| 38 | SntpOurPort = 10000 + (get_timer(0) % 4096); |
| 39 | sport = NTP_SERVICE_PORT; |
| 40 | |
| 41 | NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen); |
| 42 | } |
| 43 | |
| 44 | static void |
| 45 | SntpTimeout (void) |
| 46 | { |
| 47 | puts ("Timeout\n"); |
| 48 | NetState = NETLOOP_FAIL; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | SntpHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len) |
| 54 | { |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 55 | struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 56 | struct rtc_time tm; |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 57 | ulong seconds; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 58 | |
| 59 | debug ("%s\n", __FUNCTION__); |
| 60 | |
| 61 | if (dest != SntpOurPort) return; |
| 62 | |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 63 | /* |
| 64 | * As the RTC's used in U-Boot sepport second resolution only |
| 65 | * we simply ignore the sub-second field. |
| 66 | */ |
| 67 | memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 68 | |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 69 | to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 70 | #if (CONFIG_COMMANDS & CFG_CMD_DATE) |
| 71 | rtc_set (&tm); |
| 72 | #endif |
| 73 | printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", |
| 74 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 75 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 76 | |
| 77 | NetState = NETLOOP_SUCCESS; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | SntpStart (void) |
| 82 | { |
| 83 | debug ("%s\n", __FUNCTION__); |
| 84 | |
| 85 | NetSetTimeout (SNTP_TIMEOUT * CFG_HZ, SntpTimeout); |
| 86 | NetSetHandler(SntpHandler); |
| 87 | memset (NetServerEther, 0, 6); |
| 88 | |
| 89 | SntpSend (); |
| 90 | } |
| 91 | |
| 92 | #endif /* CONFIG_COMMANDS & CFG_CMD_SNTP */ |