Blame view
net/sntp.c
1.67 KB
ea287debe * Patch by Masami... |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * SNTP support driver * * Masami Komiya <mkomiya@sonare.it> 2005 * */ #include <common.h> #include <command.h> #include <net.h> #include <rtc.h> #include "sntp.h" |
49f3bdbba net: express the ... |
14 |
#define SNTP_TIMEOUT 10000UL |
ea287debe * Patch by Masami... |
15 16 17 18 |
static int SntpOurPort; static void |
6c3234a34 net: cosmetic: sn... |
19 |
SntpSend(void) |
ea287debe * Patch by Masami... |
20 21 22 23 |
{ struct sntp_pkt_t pkt; int pktlen = SNTP_PACKET_LEN; int sport; |
0ebf04c60 minor debug clean... |
24 25 |
debug("%s ", __func__); |
ea287debe * Patch by Masami... |
26 |
|
6c3234a34 net: cosmetic: sn... |
27 |
memset(&pkt, 0, sizeof(pkt)); |
ea287debe * Patch by Masami... |
28 29 30 31 |
pkt.li = NTP_LI_NOLEAP; pkt.vn = NTP_VERSION; pkt.mode = NTP_MODE_CLIENT; |
594c26f8a net: cosmetic: Un... |
32 |
memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE, |
6c3234a34 net: cosmetic: sn... |
33 |
(char *)&pkt, pktlen); |
ea287debe * Patch by Masami... |
34 35 36 |
SntpOurPort = 10000 + (get_timer(0) % 4096); sport = NTP_SERVICE_PORT; |
6c3234a34 net: cosmetic: sn... |
37 38 |
NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen); |
ea287debe * Patch by Masami... |
39 40 41 |
} static void |
6c3234a34 net: cosmetic: sn... |
42 |
SntpTimeout(void) |
ea287debe * Patch by Masami... |
43 |
{ |
6c3234a34 net: cosmetic: sn... |
44 45 |
puts("Timeout "); |
22f6e99d5 net: Refactor to ... |
46 |
net_set_state(NETLOOP_FAIL); |
ea287debe * Patch by Masami... |
47 48 49 50 |
return; } static void |
03eb129f8 NET: pass source ... |
51 52 |
SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src, unsigned len) |
ea287debe * Patch by Masami... |
53 |
{ |
414eec35e Fix problems with... |
54 |
struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
ea287debe * Patch by Masami... |
55 |
struct rtc_time tm; |
414eec35e Fix problems with... |
56 |
ulong seconds; |
ea287debe * Patch by Masami... |
57 |
|
0ebf04c60 minor debug clean... |
58 59 |
debug("%s ", __func__); |
ea287debe * Patch by Masami... |
60 |
|
6c3234a34 net: cosmetic: sn... |
61 62 |
if (dest != SntpOurPort) return; |
ea287debe * Patch by Masami... |
63 |
|
414eec35e Fix problems with... |
64 65 66 67 |
/* * As the RTC's used in U-Boot sepport second resolution only * we simply ignore the sub-second field. */ |
6c3234a34 net: cosmetic: sn... |
68 |
memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
ea287debe * Patch by Masami... |
69 |
|
414eec35e Fix problems with... |
70 |
to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm); |
643d1ab23 net/: Remove obso... |
71 |
#if defined(CONFIG_CMD_DATE) |
6c3234a34 net: cosmetic: sn... |
72 |
rtc_set(&tm); |
ea287debe * Patch by Masami... |
73 |
#endif |
6c3234a34 net: cosmetic: sn... |
74 75 |
printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d ", |
ea287debe * Patch by Masami... |
76 77 |
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
ea287debe * Patch by Masami... |
78 |
|
22f6e99d5 net: Refactor to ... |
79 |
net_set_state(NETLOOP_SUCCESS); |
ea287debe * Patch by Masami... |
80 81 82 |
} void |
6c3234a34 net: cosmetic: sn... |
83 |
SntpStart(void) |
ea287debe * Patch by Masami... |
84 |
{ |
0ebf04c60 minor debug clean... |
85 86 |
debug("%s ", __func__); |
ea287debe * Patch by Masami... |
87 |
|
6c3234a34 net: cosmetic: sn... |
88 |
NetSetTimeout(SNTP_TIMEOUT, SntpTimeout); |
ece223b52 net: Refactor to ... |
89 |
net_set_udp_handler(SntpHandler); |
6c3234a34 net: cosmetic: sn... |
90 |
memset(NetServerEther, 0, sizeof(NetServerEther)); |
ea287debe * Patch by Masami... |
91 |
|
6c3234a34 net: cosmetic: sn... |
92 |
SntpSend(); |
ea287debe * Patch by Masami... |
93 |
} |