Blame view
net/sntp.c
2.05 KB
ea287debe * Patch by Masami... |
1 2 3 4 5 6 7 8 9 |
/* * SNTP support driver * * Masami Komiya <mkomiya@sonare.it> 2005 * */ #include <common.h> #include <command.h> |
17f0ac609 dm: net: rtc: Sup... |
10 |
#include <dm.h> |
ea287debe * Patch by Masami... |
11 12 13 14 |
#include <net.h> #include <rtc.h> #include "sntp.h" |
49f3bdbba net: express the ... |
15 |
#define SNTP_TIMEOUT 10000UL |
ea287debe * Patch by Masami... |
16 |
|
38ba25587 net: cosmetic: Cl... |
17 |
static int sntp_our_port; |
ea287debe * Patch by Masami... |
18 |
|
38ba25587 net: cosmetic: Cl... |
19 |
static void sntp_send(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; |
1203fccee net: cosmetic: Cl... |
32 33 |
memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, (char *)&pkt, pktlen); |
ea287debe * Patch by Masami... |
34 |
|
38ba25587 net: cosmetic: Cl... |
35 |
sntp_our_port = 10000 + (get_timer(0) % 4096); |
ea287debe * Patch by Masami... |
36 |
sport = NTP_SERVICE_PORT; |
1203fccee net: cosmetic: Cl... |
37 |
net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport, |
38ba25587 net: cosmetic: Cl... |
38 |
sntp_our_port, pktlen); |
ea287debe * Patch by Masami... |
39 |
} |
38ba25587 net: cosmetic: Cl... |
40 |
static void sntp_timeout_handler(void) |
ea287debe * Patch by Masami... |
41 |
{ |
6c3234a34 net: cosmetic: sn... |
42 43 |
puts("Timeout "); |
22f6e99d5 net: Refactor to ... |
44 |
net_set_state(NETLOOP_FAIL); |
ea287debe * Patch by Masami... |
45 46 |
return; } |
049a95a77 net: cosmetic: Ch... |
47 48 |
static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip, unsigned src, unsigned len) |
ea287debe * Patch by Masami... |
49 |
{ |
3c56fb828 net: Fix compile ... |
50 |
#ifdef CONFIG_TIMESTAMP |
414eec35e Fix problems with... |
51 |
struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
ea287debe * Patch by Masami... |
52 |
struct rtc_time tm; |
414eec35e Fix problems with... |
53 |
ulong seconds; |
3c56fb828 net: Fix compile ... |
54 |
#endif |
ea287debe * Patch by Masami... |
55 |
|
0ebf04c60 minor debug clean... |
56 57 |
debug("%s ", __func__); |
ea287debe * Patch by Masami... |
58 |
|
38ba25587 net: cosmetic: Cl... |
59 |
if (dest != sntp_our_port) |
6c3234a34 net: cosmetic: sn... |
60 |
return; |
ea287debe * Patch by Masami... |
61 |
|
3c56fb828 net: Fix compile ... |
62 |
#ifdef CONFIG_TIMESTAMP |
414eec35e Fix problems with... |
63 |
/* |
3c56fb828 net: Fix compile ... |
64 |
* As the RTC's used in U-Boot support second resolution only |
414eec35e Fix problems with... |
65 66 |
* we simply ignore the sub-second field. */ |
6c3234a34 net: cosmetic: sn... |
67 |
memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
ea287debe * Patch by Masami... |
68 |
|
9f9276c34 dm: rtc: Rename t... |
69 |
rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm); |
643d1ab23 net/: Remove obso... |
70 |
#if defined(CONFIG_CMD_DATE) |
17f0ac609 dm: net: rtc: Sup... |
71 72 73 74 75 76 77 78 79 80 81 |
# ifdef CONFIG_DM_RTC struct udevice *dev; int ret; ret = uclass_get_device(UCLASS_RTC, 0, &dev); if (ret) printf("SNTP: cannot find RTC: err=%d ", ret); else dm_rtc_set(dev, &tm); # else |
6c3234a34 net: cosmetic: sn... |
82 |
rtc_set(&tm); |
17f0ac609 dm: net: rtc: Sup... |
83 |
# endif |
ea287debe * Patch by Masami... |
84 |
#endif |
6c3234a34 net: cosmetic: sn... |
85 86 |
printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d ", |
38ba25587 net: cosmetic: Cl... |
87 88 |
tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
3c56fb828 net: Fix compile ... |
89 |
#endif |
ea287debe * Patch by Masami... |
90 |
|
22f6e99d5 net: Refactor to ... |
91 |
net_set_state(NETLOOP_SUCCESS); |
ea287debe * Patch by Masami... |
92 |
} |
38ba25587 net: cosmetic: Cl... |
93 |
void sntp_start(void) |
ea287debe * Patch by Masami... |
94 |
{ |
0ebf04c60 minor debug clean... |
95 96 |
debug("%s ", __func__); |
ea287debe * Patch by Masami... |
97 |
|
bc0571fc1 net: cosmetic: Fi... |
98 |
net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler); |
049a95a77 net: cosmetic: Ch... |
99 |
net_set_udp_handler(sntp_handler); |
0adb5b761 net: cosmetic: Na... |
100 |
memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
ea287debe * Patch by Masami... |
101 |
|
38ba25587 net: cosmetic: Cl... |
102 |
sntp_send(); |
ea287debe * Patch by Masami... |
103 |
} |