/* XS_NTP.c: Time Synchronization using NTP Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: Large Project: XS_NTP.c ..\lib\7188el.Lib ..\lib\tcpip32.lib ..\lib\FW_nnnnn.LIB Hordware: I-7188EX Active servers: http://www.eecis.udel.edu/~mills/ntp/clock1a.html This demo is used to synchronize the time of a computer client or server to another server or reference time source through UDP protocol, Type13, 14 (Timestamp Request and Timestamp Reply Message) The timestamp values are in milliseconds from midnight UT (Greenwich mean time) Orig Recv Xmit RecvReply |----------------------->| |------------------------->| |<----------------------- RTT -------------------------->| RTT(round-trip time), Recv=Xmit diff=RTT/2-(Recv-Orig)=(RecvReply-Orig)/2-(RecvReply-Xmit) diff (It's the time difference between the 7188E and Time server) Time server: time.stdtime.gov.tw 220.130.158.52 nets.org.sg 203.117.180.36 Active Timer servers: http://www.eecis.udel.edu/~mills/ntp/clock1a.html [Dec 19, 2008] by Liam [Aug,02,2011] by Nicholas */ #include #include #include "..\lib\7188e.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" #define BUFSIZE 1460 // read/write buffer size #define TaiwanTime 8 // GMT+ 8.00 #define SNTP_PORT 123 void ShowSocketStatus(void); char *PublicIP="nets.org.sg"; char IP_Address[20]; unsigned long ulIP; unsigned char GsIP[4]; int DN_to_IP(unsigned char *sDN,unsigned char *sIP); void DoUdpNTP(int skt); UDP_SOCKET UdpNTP={ //UDP client -1, //int socket; 0, //unsigned MyPort; "203.117.180.36", //char *RemoteIp; //nets.org.sg SNTP_PORT, //unsigned RemotePort; 1, //int fNonBlock; 0, //int fEnableBroadcast; 0, //int status; DoUdpNTP //void (*CallBackFun)(int skt); }; unsigned long ltime1; char *GetModuleName(void) /* User's program must support this function for UDP search */ { return "I-7188EX-MFW" ; } /************************************************************************/ char *GetAliasName(void) /* User's program must support this function for UDP search */ { return "test1" ; } #define DNS1 "10.0.0.103" #define DNS2 "10.0.0.1" unsigned char MyIp[4]; unsigned char NewMask[4]; unsigned char NewGateway[6]; void RequestsyncTime(void); void XS_UserInit(int argc,char *argv[]) { extern int bAcceptBroadcast; extern unsigned long ACKDELAY; // extern struct NETDATA netdata[]; extern long MAXTXTOUT; void UserLoop(void); int i, port; unsigned char cIP_Net[10]; InitLib(); for(i=1; i99 Putch(key); if(key=='\r') { //The terminal character of a string is '\r' (Press 'Enter' key) Command[idx]=0; Putch('\n'); if(Command[0]=='/') { // '/' denotes that the inputted string was a command if(Command[1]=='Q') { //Break the TCP connection } else { //unknow command Print("Unsupport command\r\n"); } } else { Print("[do nothing]\r\n"); } idx=0; } break; } } } void RequestsyncTime(void) { Print("Status=%d\r\n",UdpNTP.status); if(UdpNTP.status>0) synctime_send(UdpNTP.socket); } char buf[BUFSIZE]; void main(int argc, char *argv[]) { XS_main(argc, argv); /* call the XS library main function. */ } #define ESTABLISHED 1 /* state machine states, see handbook */ #define FINWAIT_1 2 #define FINWAIT_2 3 #define CLOSED_WAIT 4 #define TIMEWAIT 5 #define LAST_ACK 6 #define CLOSED 7 #define SYN_SENT 8 #define SYN_RECEIVED 9 #define LISTEN 16 void ShowSocketStatus(void) { int i; Puts("\r\n"); for(i=0; ih_addr_list); sprintf(sIP,"%d.%d.%d.%d",tmp[0],tmp[1],tmp[2],tmp[3]); return 0; //DN -> IP OK } else return -1;//DN -> IP Faliure } void DoUdpNTP(int skt) { struct sockaddr_in sockAddrWhoSent; synctime_recv(skt, TaiwanTime); }