]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/sntp.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[people/ms/u-boot.git] / net / sntp.c
CommitLineData
ea287deb
WD
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
49f3bdbb 15#define SNTP_TIMEOUT 10000UL
ea287deb
WD
16
17static int SntpOurPort;
18
19static void
20SntpSend (void)
21{
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
24 int sport;
25
0ebf04c6 26 debug("%s\n", __func__);
ea287deb
WD
27
28 memset (&pkt, 0, sizeof(pkt));
29
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
34 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
35
36 SntpOurPort = 10000 + (get_timer(0) % 4096);
37 sport = NTP_SERVICE_PORT;
38
39 NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen);
40}
41
42static void
43SntpTimeout (void)
44{
45 puts ("Timeout\n");
46 NetState = NETLOOP_FAIL;
47 return;
48}
49
50static void
03eb129f
LC
51SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
52 unsigned len)
ea287deb 53{
414eec35 54 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 55 struct rtc_time tm;
414eec35 56 ulong seconds;
ea287deb 57
0ebf04c6 58 debug("%s\n", __func__);
ea287deb
WD
59
60 if (dest != SntpOurPort) return;
61
414eec35
WD
62 /*
63 * As the RTC's used in U-Boot sepport second resolution only
64 * we simply ignore the sub-second field.
65 */
66 memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 67
414eec35 68 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
643d1ab2 69#if defined(CONFIG_CMD_DATE)
ea287deb
WD
70 rtc_set (&tm);
71#endif
72 printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
73 tm.tm_year, tm.tm_mon, tm.tm_mday,
74 tm.tm_hour, tm.tm_min, tm.tm_sec);
ea287deb
WD
75
76 NetState = NETLOOP_SUCCESS;
77}
78
79void
80SntpStart (void)
81{
0ebf04c6 82 debug("%s\n", __func__);
ea287deb 83
49f3bdbb 84 NetSetTimeout (SNTP_TIMEOUT, SntpTimeout);
ea287deb
WD
85 NetSetHandler(SntpHandler);
86 memset (NetServerEther, 0, 6);
87
88 SntpSend ();
89}