]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/sntp.c
net: cosmetic: Clean up RARP variables and functions
[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
6c3234a3 20SntpSend(void)
ea287deb
WD
21{
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
24 int sport;
25
0ebf04c6 26 debug("%s\n", __func__);
ea287deb 27
6c3234a3 28 memset(&pkt, 0, sizeof(pkt));
ea287deb
WD
29
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
1203fcce
JH
34 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
35 (char *)&pkt, pktlen);
ea287deb
WD
36
37 SntpOurPort = 10000 + (get_timer(0) % 4096);
38 sport = NTP_SERVICE_PORT;
39
1203fcce
JH
40 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
41 SntpOurPort, pktlen);
ea287deb
WD
42}
43
44static void
6c3234a3 45SntpTimeout(void)
ea287deb 46{
6c3234a3 47 puts("Timeout\n");
22f6e99d 48 net_set_state(NETLOOP_FAIL);
ea287deb
WD
49 return;
50}
51
049a95a7
JH
52static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
53 unsigned src, unsigned len)
ea287deb 54{
414eec35 55 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 56 struct rtc_time tm;
414eec35 57 ulong seconds;
ea287deb 58
0ebf04c6 59 debug("%s\n", __func__);
ea287deb 60
6c3234a3
JH
61 if (dest != SntpOurPort)
62 return;
ea287deb 63
414eec35
WD
64 /*
65 * As the RTC's used in U-Boot sepport second resolution only
66 * we simply ignore the sub-second field.
67 */
6c3234a3 68 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 69
414eec35 70 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
643d1ab2 71#if defined(CONFIG_CMD_DATE)
6c3234a3 72 rtc_set(&tm);
ea287deb 73#endif
6c3234a3 74 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
ea287deb
WD
75 tm.tm_year, tm.tm_mon, tm.tm_mday,
76 tm.tm_hour, tm.tm_min, tm.tm_sec);
ea287deb 77
22f6e99d 78 net_set_state(NETLOOP_SUCCESS);
ea287deb
WD
79}
80
81void
6c3234a3 82SntpStart(void)
ea287deb 83{
0ebf04c6 84 debug("%s\n", __func__);
ea287deb 85
6c3234a3 86 NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
049a95a7 87 net_set_udp_handler(sntp_handler);
0adb5b76 88 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
ea287deb 89
6c3234a3 90 SntpSend();
ea287deb 91}