eunkyoung kim / SNTPClinet

Dependents:   SSD1306_smart_watch smart_watch smart_watch

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SNTPClient.h Source File

SNTPClient.h

00001 /**
00002 * @author Raphael Kwon
00003 *
00004 * @section LICENSE
00005 *
00006 * Copyright (c) 2014 WIZnet Co., Ltd.
00007 *
00008 * Permission is hereby granted, free of charge, to any person obtaining a copy
00009 * of this software and associated documentation files (the "Software"), to deal
00010 * in the Software without restriction, including without limitation the rights
00011 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 * copies of the Software, and to permit persons to whom the Software is
00013 * furnished to do so, subject to the following conditions:
00014 *
00015 * The above copyright notice and this permission notice shall be included in
00016 * all copies or substantial portions of the Software.
00017 *
00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024 * THE SOFTWARE.
00025 *
00026 * @section DESCRIPTION
00027 *    Simple Network Time Protocol Client
00028 *
00029 */
00030 
00031 #ifndef SNTPCLIENT_H
00032 #define SNTPCLIENT_H
00033 
00034 #include "mbed.h"
00035 #include "UDPSocket.h"
00036 
00037 /*
00038  * @brief Define it for Debug & Monitor DNS processing.
00039  * @note 
00040  */
00041 //#define _SNTP_DEBUG_
00042 
00043 #define MAX_SNTP_BUF_SIZE   sizeof(ntpformat)       ///< maximum size of DNS buffer. */
00044 #define ntp_port        123                     //ntp server port number
00045 #define SECS_PERDAY     86400UL                 // seconds in a day = 60*60*24
00046 #define UTC_ADJ_HRS     9                       // SEOUL : GMT+9
00047 #define EPOCH           1900                    // NTP start year
00048 
00049 /* for ntpclient */
00050 typedef signed char s_char;
00051 typedef unsigned long long tstamp;
00052 typedef unsigned int tdist;
00053 
00054 typedef struct _ntpformat
00055 {
00056     uint8_t  dstaddr[4];        /* destination (local) address */
00057     char    version;        /* version number */
00058     char    leap;           /* leap indicator */
00059     char    mode;           /* mode */
00060     char    stratum;        /* stratum */
00061     char    poll;           /* poll interval */
00062     s_char  precision;      /* precision */
00063     tdist   rootdelay;      /* root delay */
00064     tdist   rootdisp;       /* root dispersion */
00065     char    refid;          /* reference ID */
00066     tstamp  reftime;        /* reference time */
00067     tstamp  org;            /* origin timestamp */
00068     tstamp  rec;            /* receive timestamp */
00069     tstamp  xmt;            /* transmit timestamp */
00070 } ntpformat;
00071 typedef struct _datetime
00072 {
00073     int yy;
00074     int mo;
00075     int dd;
00076     int hh;
00077     int mm;
00078     int ss;
00079 } datetime;
00080 /*
00081 typedef struct _datetime
00082 {
00083     uint16_t yy;
00084     uint8_t mo;
00085     uint8_t dd;
00086     uint8_t hh;
00087     uint8_t mm;
00088     uint8_t ss;
00089 } datetime;
00090 */
00091 /** SNTPClient client Class.
00092  *
00093  * Example (ethernet network):
00094  * @code
00095  * #include "mbed.h"
00096  * #include "EthernetInterface.h"
00097  * #include "SNTPClient.h"
00098  *
00099  * int main() {
00100  *    EthernetInterface eth;
00101  *    eth.init(); //Use DHCP
00102  *    eth.connect();
00103  *    printf("IP Address is %s\n\r", eth.getIPAddress());
00104  *   
00105  *    SNTPClient ws("ws://sockets.mbed.org:443/ws/demo/rw");
00106  *    ws.connect();
00107  *   
00108  *    while (1) {
00109  *        int res = ws.send("SNTPClient Hello World!");
00110  *
00111  *        if (ws.read(recv)) {
00112  *            printf("rcv: %s\r\n", recv);
00113  *        }
00114  *
00115  *        wait(0.1);
00116  *    }
00117  * }
00118  * @endcode
00119  */
00120 
00121 
00122 class SNTPClient
00123 {
00124     public:
00125         /**
00126         * Constructor
00127         *
00128         * @param url The SNTPClient host
00129         */
00130         SNTPClient(char * url, uint8_t time_zone);
00131 
00132         /**
00133         * Connect to the SNTPClient url
00134         *
00135         *@return true if the connection is established, false otherwise
00136         */
00137         bool connect();
00138 
00139         /**
00140         * Read a SNTPClient message
00141         *
00142         * @param message pointer to the string to be read (null if drop frame)
00143         *
00144         * @return true if a SNTPClient frame has been read
00145         */
00146         bool getTime(datetime *time);
00147 
00148         /**
00149         * Close the SNTPClient connection
00150         *
00151         * @return true if the connection has been closed, false otherwise
00152         */
00153         bool close();
00154 
00155         /*
00156         * Accessor: get host from the SNTPClient url
00157         *
00158         * @return host
00159         */
00160         char* getHost();
00161 
00162     private:
00163 
00164         uint16_t port;
00165         char host[32];
00166 
00167         UDPSocket socket;
00168         Endpoint sntp_server;
00169 
00170         ntpformat NTPformat;
00171         datetime Nowdatetime;
00172         uint8_t ntpmessage[48];
00173         uint8_t tz; // Time Zone
00174 
00175         void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx);
00176         tstamp changedatetime_to_seconds(void);
00177         void calcdatetime(tstamp seconds);
00178 };
00179 
00180 #endif