4 * Masami Komiya <mkomiya@sonare.it> 2005
13 #if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_SNTP))
17 #define SNTP_TIMEOUT 10
19 static int SntpOurPort;
21 static void SntpSend(void){
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
27 printf("%s\n", __FUNCTION__);
30 memset(&pkt, 0, sizeof(pkt));
32 pkt.li = NTP_LI_NOLEAP;
34 pkt.mode = NTP_MODE_CLIENT;
36 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
38 SntpOurPort = 10000 + (get_timer(0) % 4096);
39 sport = NTP_SERVICE_PORT;
41 puts("Sending SNTP request...\n");
43 NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen);
46 static void SntpTimeout(void){
47 puts("## Error: timeout\n");
48 NetState = NETLOOP_FAIL;
53 static void SntpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len){
54 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
59 printf("%s\n", __FUNCTION__);
62 if(dest != SntpOurPort){
67 * As the RTC's used in U-Boot supports second resolution only
68 * we simply ignore the sub-second field.
70 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
72 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
74 #if (CONFIG_COMMANDS & CFG_CMD_DATE)
75 // TODO: find out how to use RTC on
78 printf("Got SNTP response: %4d-%02d-%02d %2d:%02d:%02d\n\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
79 NetState = NETLOOP_SUCCESS;
84 printf("%s\n", __FUNCTION__);
87 NetSetTimeout(SNTP_TIMEOUT * CFG_HZ, SntpTimeout);
88 NetSetHandler(SntpHandler);
90 memset(NetServerEther, 0, 6);
95 #endif /* CONFIG_COMMANDS & CFG_CMD_SNTP */