]> Git Repo - u-boot.git/blame - net/sntp.c
net: Fix error if some network features are disabled
[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>
17f0ac60 10#include <dm.h>
f7ae49fc 11#include <log.h>
ea287deb
WD
12#include <net.h>
13#include <rtc.h>
14
15#include "sntp.h"
16
49f3bdbb 17#define SNTP_TIMEOUT 10000UL
ea287deb 18
38ba2558 19static int sntp_our_port;
ea287deb 20
38ba2558 21static void sntp_send(void)
ea287deb
WD
22{
23 struct sntp_pkt_t pkt;
24 int pktlen = SNTP_PACKET_LEN;
25 int sport;
26
0ebf04c6 27 debug("%s\n", __func__);
ea287deb 28
6c3234a3 29 memset(&pkt, 0, sizeof(pkt));
ea287deb
WD
30
31 pkt.li = NTP_LI_NOLEAP;
32 pkt.vn = NTP_VERSION;
33 pkt.mode = NTP_MODE_CLIENT;
34
1203fcce
JH
35 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
36 (char *)&pkt, pktlen);
ea287deb 37
38ba2558 38 sntp_our_port = 10000 + (get_timer(0) % 4096);
ea287deb
WD
39 sport = NTP_SERVICE_PORT;
40
1203fcce 41 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
38ba2558 42 sntp_our_port, pktlen);
ea287deb
WD
43}
44
38ba2558 45static void sntp_timeout_handler(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{
3c56fb82 55#ifdef CONFIG_TIMESTAMP
414eec35 56 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 57 struct rtc_time tm;
414eec35 58 ulong seconds;
3c56fb82 59#endif
ea287deb 60
0ebf04c6 61 debug("%s\n", __func__);
ea287deb 62
38ba2558 63 if (dest != sntp_our_port)
6c3234a3 64 return;
ea287deb 65
3c56fb82 66#ifdef CONFIG_TIMESTAMP
414eec35 67 /*
3c56fb82 68 * As the RTC's used in U-Boot support second resolution only
414eec35
WD
69 * we simply ignore the sub-second field.
70 */
6c3234a3 71 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 72
9f9276c3 73 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
643d1ab2 74#if defined(CONFIG_CMD_DATE)
17f0ac60
SG
75# ifdef CONFIG_DM_RTC
76 struct udevice *dev;
77 int ret;
78
79 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
80 if (ret)
81 printf("SNTP: cannot find RTC: err=%d\n", ret);
82 else
83 dm_rtc_set(dev, &tm);
84# else
6c3234a3 85 rtc_set(&tm);
17f0ac60 86# endif
ea287deb 87#endif
6c3234a3 88 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
38ba2558
JH
89 tm.tm_year, tm.tm_mon, tm.tm_mday,
90 tm.tm_hour, tm.tm_min, tm.tm_sec);
3c56fb82 91#endif
ea287deb 92
22f6e99d 93 net_set_state(NETLOOP_SUCCESS);
ea287deb
WD
94}
95
38ba2558 96void sntp_start(void)
ea287deb 97{
0ebf04c6 98 debug("%s\n", __func__);
ea287deb 99
bc0571fc 100 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
049a95a7 101 net_set_udp_handler(sntp_handler);
0adb5b76 102 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
ea287deb 103
38ba2558 104 sntp_send();
ea287deb 105}
This page took 0.310682 seconds and 4 git commands to generate.