dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "test_env.h"
00004 
00005 namespace {
00006     const char *HTTP_SERVER_NAME = "utcnist.colorado.edu";
00007     const int HTTP_SERVER_PORT = 37;
00008     const float YEARS_TO_PASS = 114.0;
00009 }
00010 
00011 
00012 int main() {
00013     MBED_HOSTTEST_TIMEOUT(20);
00014     MBED_HOSTTEST_SELECT(default_auto);
00015     MBED_HOSTTEST_DESCRIPTION(NIST Internet Time Service);
00016     MBED_HOSTTEST_START("NET_2");
00017 
00018     bool result = false;
00019     EthernetInterface eth;
00020     eth.init(); //Use DHCP
00021     eth.connect();
00022     printf("UDP client IP Address is %s\n", eth.getIPAddress());
00023 
00024     UDPSocket sock;
00025     sock.init();
00026 
00027     Endpoint nist;
00028     nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00029 
00030     char out_buffer[] = "plop"; // Does not matter
00031     sock.sendTo(nist, out_buffer, sizeof(out_buffer));
00032 
00033     union {
00034         char in_buffer_tab[4];
00035         unsigned int in_buffer_uint;
00036     };
00037 
00038     const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab));
00039     if (n > 0) {
00040         result = true;
00041         const unsigned int timeRes = ntohl(in_buffer_uint);
00042         const float years = timeRes / 60.0 / 60.0 / 24.0 / 365.0;
00043         const float days = timeRes / 24.0 / 60.0 / 60.0;
00044         printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port());
00045         printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]");
00046         printf("UDP: %.2f days since 01/01/1900 00:00 GMT ... %s\r\n", days, timeRes > 0 ? "[OK]" : "[FAIL]");
00047         printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]");
00048 
00049         if (years < YEARS_TO_PASS) {
00050             result = false;
00051         }
00052     }
00053     sock.close();
00054     eth.disconnect();
00055     MBED_HOSTTEST_RESULT(result);
00056 }