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