dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
main.cpp
00001 #if !FEATURE_IPV4 00002 #error [NOT_SUPPORTED] IPV4 not supported for this target 00003 #endif 00004 00005 #include "mbed.h" 00006 #include "EthernetInterface.h" 00007 #include "UDPSocket.h" 00008 #include "greentea-client/test_env.h" 00009 00010 namespace { 00011 const char *HTTP_SERVER_NAME = "pool.ntp.org"; 00012 const int HTTP_SERVER_PORT = 123; 00013 } 00014 00015 00016 int main() { 00017 GREENTEA_SETUP(60, "default_auto"); 00018 00019 bool result = false; 00020 const time_t TIME1970 = 2208988800L; 00021 int ntp_send_values[12] = {0}; 00022 int ntp_recv_values[12] = {0}; 00023 00024 EthernetInterface eth; 00025 eth.connect(); 00026 printf("UDP client IP Address is %s\n", eth.get_ip_address()); 00027 00028 UDPSocket sock; 00029 sock.open(ð); 00030 sock.set_timeout(15000); 00031 00032 SocketAddress nist(ð, HTTP_SERVER_NAME, HTTP_SERVER_PORT); 00033 00034 printf("UDP: NIST server %s address: %s on port %d\r\n", HTTP_SERVER_NAME, nist.get_ip_address(), nist.get_port()); 00035 00036 memset(ntp_send_values, 0x00, sizeof(ntp_send_values)); 00037 ntp_send_values[0] = '\x1b'; 00038 00039 while(1) { 00040 memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values)); 00041 00042 int ret_send = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values)); 00043 printf("UDP: Sent %d Bytes to NTP server \n", ret_send); 00044 00045 SocketAddress source; 00046 const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values)); 00047 00048 printf("UDP: Recved from NTP server %d Bytes \n", n); 00049 00050 if (n > 0 && strcmp(source.get_ip_address(), nist.get_ip_address()) == 0) { 00051 result = true; 00052 00053 printf("UDP: Values returned by NTP server: \n"); 00054 for (size_t i=0; i < sizeof(ntp_recv_values) / sizeof(ntp_recv_values[0]); ++i) { 00055 printf("\t[%02d] 0x%X", i, ntohl(ntp_recv_values[i])); 00056 00057 if (i == 10) { 00058 time_t timestamp = ntohl(ntp_recv_values[i]) - TIME1970; 00059 printf("\tNTP timestamp is %s", ctime(×tamp)); 00060 } else { 00061 printf("\n"); 00062 } 00063 } 00064 00065 break; 00066 } 00067 00068 printf("Failed to receive data, retrying in 5 seconds...\n"); 00069 wait(5); 00070 } 00071 00072 sock.close(); 00073 eth.disconnect(); 00074 GREENTEA_TESTSUITE_RESULT(result); 00075 }
Generated on Tue Jul 12 2022 11:01:53 by
1.7.2
