]> Git Repo - J-u-boot.git/blame - net/sntp.c
dm: rtc: Convert 'date' command to support driver model
[J-u-boot.git] / net / sntp.c
CommitLineData
ea287deb
WD
1/*
2 * SNTP support driver
3 *
4 * Masami Komiya <[email protected]> 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 16
38ba2558 17static int sntp_our_port;
ea287deb 18
38ba2558 19static void sntp_send(void)
ea287deb
WD
20{
21 struct sntp_pkt_t pkt;
22 int pktlen = SNTP_PACKET_LEN;
23 int sport;
24
0ebf04c6 25 debug("%s\n", __func__);
ea287deb 26
6c3234a3 27 memset(&pkt, 0, sizeof(pkt));
ea287deb
WD
28
29 pkt.li = NTP_LI_NOLEAP;
30 pkt.vn = NTP_VERSION;
31 pkt.mode = NTP_MODE_CLIENT;
32
1203fcce
JH
33 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
34 (char *)&pkt, pktlen);
ea287deb 35
38ba2558 36 sntp_our_port = 10000 + (get_timer(0) % 4096);
ea287deb
WD
37 sport = NTP_SERVICE_PORT;
38
1203fcce 39 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
38ba2558 40 sntp_our_port, pktlen);
ea287deb
WD
41}
42
38ba2558 43static void sntp_timeout_handler(void)
ea287deb 44{
6c3234a3 45 puts("Timeout\n");
22f6e99d 46 net_set_state(NETLOOP_FAIL);
ea287deb
WD
47 return;
48}
49
049a95a7
JH
50static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
51 unsigned src, unsigned len)
ea287deb 52{
3c56fb82 53#ifdef CONFIG_TIMESTAMP
414eec35 54 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 55 struct rtc_time tm;
414eec35 56 ulong seconds;
3c56fb82 57#endif
ea287deb 58
0ebf04c6 59 debug("%s\n", __func__);
ea287deb 60
38ba2558 61 if (dest != sntp_our_port)
6c3234a3 62 return;
ea287deb 63
3c56fb82 64#ifdef CONFIG_TIMESTAMP
414eec35 65 /*
3c56fb82 66 * As the RTC's used in U-Boot support second resolution only
414eec35
WD
67 * we simply ignore the sub-second field.
68 */
6c3234a3 69 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 70
9f9276c3 71 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
643d1ab2 72#if defined(CONFIG_CMD_DATE)
6c3234a3 73 rtc_set(&tm);
ea287deb 74#endif
6c3234a3 75 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
38ba2558
JH
76 tm.tm_year, tm.tm_mon, tm.tm_mday,
77 tm.tm_hour, tm.tm_min, tm.tm_sec);
3c56fb82 78#endif
ea287deb 79
22f6e99d 80 net_set_state(NETLOOP_SUCCESS);
ea287deb
WD
81}
82
38ba2558 83void sntp_start(void)
ea287deb 84{
0ebf04c6 85 debug("%s\n", __func__);
ea287deb 86
bc0571fc 87 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
049a95a7 88 net_set_udp_handler(sntp_handler);
0adb5b76 89 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
ea287deb 90
38ba2558 91 sntp_send();
ea287deb 92}
This page took 0.254065 seconds and 4 git commands to generate.