Hiroshi Yamaguchi / Mbed 2 deprecated SimpleSocketExamples

Dependencies:   EthernetInterface SimpleSocket mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ntpclient2.cpp Source File

ntpclient2.cpp

00001 #include "SimpleSocket.h"
00002 
00003 typedef unsigned long long Time64;
00004 
00005 Time64 toTime64(char buf[]) {
00006     Time64 time64 = 0;
00007     for (int i = 0; i < 8; i++) {
00008         time64 = (time64 << 8) | buf[i];
00009     }
00010     return time64;
00011 }
00012 
00013 Time64 toTime64(int usecs) {
00014     return Time64(double(usecs) * (1ULL << 32) / 1000000);
00015 }
00016 
00017 void printTime64(char *title, Time64 time64) {
00018     unsigned long seconds = (unsigned long) (time64 >> 32);
00019     unsigned long subsecs = (unsigned long) (time64 & 0xFFFFFFFFULL);
00020     char buf[16];
00021 
00022     time_t jstime = time_t(seconds - 2208988800UL) + 9 * 3600;
00023     strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
00024     printf("%s: %s", title, buf);
00025     sprintf(buf, "%f\n", (double) subsecs / (1ULL << 32));
00026     printf("%s", &buf[1]);
00027 }
00028 
00029 void ntpclient2() {
00030     char *NTP_SERVER = "pool.ntp.org";
00031     char buf[48] = {0x23};// 00100011 LI(0), Version(4), Mode(3: Client)
00032     Timer timer;
00033     Time64 adjustedTime = 0;
00034 
00035     for (int count = 0; count < 5; count++) {
00036         buf[0] = 0x23;
00037         DatagramSocket datagram;
00038         datagram.write(buf, sizeof(buf));
00039         timer.reset();
00040         datagram.send(NTP_SERVER, 123);
00041         timer.start();
00042         if (datagram.receive() > 0) {
00043             int turnaround = timer.read_us();
00044             if (datagram.read(buf, sizeof(buf))) {
00045                 Time64 receivedTime = toTime64(&buf[32]);
00046                 Time64 transferTime = toTime64(&buf[40]);
00047                 adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;
00048                 timer.reset();
00049                 timer.start();
00050                 printTime64("transfer", transferTime);
00051                 printTime64("adjusted", adjustedTime);
00052                 printf("\n");
00053             }
00054         } else {
00055             wait(5);
00056         }
00057     }
00058 
00059     float subsecs = (double) (adjustedTime & 0xFFFFFFFFULL) / (1ULL << 32);
00060     wait(1 - subsecs);
00061     set_time((size_t) ((adjustedTime >> 32) - 2208988800UL) + 1);
00062 
00063     time_t jstime = time(NULL) + 9 * 3600;
00064     strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
00065     printf("RTC delta = %6d, %s\n\n", timer.read_us(), buf);
00066 
00067     for (int count = 0; count < 20; count++) {
00068         buf[0] = 0x23;
00069         DatagramSocket datagram;
00070         datagram.write(buf, sizeof(buf));
00071         timer.reset();
00072         datagram.send(NTP_SERVER, 123);
00073         timer.start();
00074         if (datagram.receive() > 0) {
00075             int turnaround = timer.read_us();
00076             if (datagram.read(buf, sizeof(buf))) {
00077                 Time64 receivedTime = toTime64(&buf[32]);
00078                 Time64 transferTime = toTime64(&buf[40]);
00079                 adjustedTime = toTime64(turnaround / 2) + receivedTime / 2 + transferTime / 2;  
00080                 printTime64("adjusted", adjustedTime);
00081                 time_t jstime = time(NULL) + 9 * 3600;
00082                 strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
00083                 printf("     RTC: %s\n\n", buf);
00084             }
00085         } else {
00086             wait(5);
00087         }
00088     }
00089 
00090 }