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