]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/net/helloworld/udpclient/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / net / helloworld / udpclient / main.cpp
1 #include "mbed.h"
2 #include "EthernetInterface.h"
3 #include "test_env.h"
4
5 namespace {
6     const char *HTTP_SERVER_NAME = "utcnist.colorado.edu";
7     const int HTTP_SERVER_PORT = 37;
8     const float YEARS_TO_PASS = 114.0;
9 }
10
11
12 int main() {
13     MBED_HOSTTEST_TIMEOUT(20);
14     MBED_HOSTTEST_SELECT(default_auto);
15     MBED_HOSTTEST_DESCRIPTION(NIST Internet Time Service);
16     MBED_HOSTTEST_START("NET_2");
17
18     bool result = false;
19     EthernetInterface eth;
20     eth.init(); //Use DHCP
21     eth.connect();
22     printf("UDP client IP Address is %s\n", eth.getIPAddress());
23
24     UDPSocket sock;
25     sock.init();
26
27     Endpoint nist;
28     nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT);
29
30     char out_buffer[] = "plop"; // Does not matter
31     sock.sendTo(nist, out_buffer, sizeof(out_buffer));
32
33     union {
34         char in_buffer_tab[4];
35         unsigned int in_buffer_uint;
36     };
37
38     const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab));
39     if (n > 0) {
40         result = true;
41         const unsigned int timeRes = ntohl(in_buffer_uint);
42         const float years = timeRes / 60.0 / 60.0 / 24.0 / 365.0;
43         const float days = timeRes / 24.0 / 60.0 / 60.0;
44         printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port());
45         printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]");
46         printf("UDP: %.2f days since 01/01/1900 00:00 GMT ... %s\r\n", days, timeRes > 0 ? "[OK]" : "[FAIL]");
47         printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]");
48
49         if (years < YEARS_TO_PASS) {
50             result = false;
51         }
52     }
53     sock.close();
54     eth.disconnect();
55     MBED_HOSTTEST_RESULT(result);
56 }