The Library of SNTP Client for W5500 SNTP is short for Simple Network Time Protocol.

Dependents:   SNTP_Ethernet_W5500 SNTP_Ethernet_W5500 SNTP_SHT15_WIZwizki-W7500 Nucleo_SNTP_Ethernet_W5500 ... more

Committer:
xeon011
Date:
Fri Dec 19 05:46:22 2014 +0000
Revision:
0:137fc24033c4
The Library of SNTP Client for W5500

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xeon011 0:137fc24033c4 1 /**
xeon011 0:137fc24033c4 2 * @author Raphael Kwon
xeon011 0:137fc24033c4 3 *
xeon011 0:137fc24033c4 4 * @section LICENSE
xeon011 0:137fc24033c4 5 *
xeon011 0:137fc24033c4 6 * Copyright (c) 2014 WIZnet Co., Ltd.
xeon011 0:137fc24033c4 7 *
xeon011 0:137fc24033c4 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
xeon011 0:137fc24033c4 9 * of this software and associated documentation files (the "Software"), to deal
xeon011 0:137fc24033c4 10 * in the Software without restriction, including without limitation the rights
xeon011 0:137fc24033c4 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
xeon011 0:137fc24033c4 12 * copies of the Software, and to permit persons to whom the Software is
xeon011 0:137fc24033c4 13 * furnished to do so, subject to the following conditions:
xeon011 0:137fc24033c4 14 *
xeon011 0:137fc24033c4 15 * The above copyright notice and this permission notice shall be included in
xeon011 0:137fc24033c4 16 * all copies or substantial portions of the Software.
xeon011 0:137fc24033c4 17 *
xeon011 0:137fc24033c4 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
xeon011 0:137fc24033c4 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
xeon011 0:137fc24033c4 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
xeon011 0:137fc24033c4 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
xeon011 0:137fc24033c4 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
xeon011 0:137fc24033c4 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
xeon011 0:137fc24033c4 24 * THE SOFTWARE.
xeon011 0:137fc24033c4 25 *
xeon011 0:137fc24033c4 26 * @section DESCRIPTION
xeon011 0:137fc24033c4 27 * Simple Network Time Protocol Client
xeon011 0:137fc24033c4 28 *
xeon011 0:137fc24033c4 29 */
xeon011 0:137fc24033c4 30
xeon011 0:137fc24033c4 31 #ifndef SNTPCLIENT_H
xeon011 0:137fc24033c4 32 #define SNTPCLIENT_H
xeon011 0:137fc24033c4 33
xeon011 0:137fc24033c4 34 #include "mbed.h"
xeon011 0:137fc24033c4 35 #include "UDPSocket.h"
xeon011 0:137fc24033c4 36
xeon011 0:137fc24033c4 37 /*
xeon011 0:137fc24033c4 38 * @brief Define it for Debug & Monitor DNS processing.
xeon011 0:137fc24033c4 39 * @note
xeon011 0:137fc24033c4 40 */
xeon011 0:137fc24033c4 41 //#define _SNTP_DEBUG_
xeon011 0:137fc24033c4 42
xeon011 0:137fc24033c4 43 #define MAX_SNTP_BUF_SIZE sizeof(ntpformat) ///< maximum size of DNS buffer. */
xeon011 0:137fc24033c4 44 #define ntp_port 123 //ntp server port number
xeon011 0:137fc24033c4 45 #define SECS_PERDAY 86400UL // seconds in a day = 60*60*24
xeon011 0:137fc24033c4 46 #define UTC_ADJ_HRS 9 // SEOUL : GMT+9
xeon011 0:137fc24033c4 47 #define EPOCH 1900 // NTP start year
xeon011 0:137fc24033c4 48
xeon011 0:137fc24033c4 49 /* for ntpclient */
xeon011 0:137fc24033c4 50 typedef signed char s_char;
xeon011 0:137fc24033c4 51 typedef unsigned long long tstamp;
xeon011 0:137fc24033c4 52 typedef unsigned int tdist;
xeon011 0:137fc24033c4 53
xeon011 0:137fc24033c4 54 typedef struct _ntpformat
xeon011 0:137fc24033c4 55 {
xeon011 0:137fc24033c4 56 uint8_t dstaddr[4]; /* destination (local) address */
xeon011 0:137fc24033c4 57 char version; /* version number */
xeon011 0:137fc24033c4 58 char leap; /* leap indicator */
xeon011 0:137fc24033c4 59 char mode; /* mode */
xeon011 0:137fc24033c4 60 char stratum; /* stratum */
xeon011 0:137fc24033c4 61 char poll; /* poll interval */
xeon011 0:137fc24033c4 62 s_char precision; /* precision */
xeon011 0:137fc24033c4 63 tdist rootdelay; /* root delay */
xeon011 0:137fc24033c4 64 tdist rootdisp; /* root dispersion */
xeon011 0:137fc24033c4 65 char refid; /* reference ID */
xeon011 0:137fc24033c4 66 tstamp reftime; /* reference time */
xeon011 0:137fc24033c4 67 tstamp org; /* origin timestamp */
xeon011 0:137fc24033c4 68 tstamp rec; /* receive timestamp */
xeon011 0:137fc24033c4 69 tstamp xmt; /* transmit timestamp */
xeon011 0:137fc24033c4 70 } ntpformat;
xeon011 0:137fc24033c4 71
xeon011 0:137fc24033c4 72 typedef struct _datetime
xeon011 0:137fc24033c4 73 {
xeon011 0:137fc24033c4 74 uint16_t yy;
xeon011 0:137fc24033c4 75 uint8_t mo;
xeon011 0:137fc24033c4 76 uint8_t dd;
xeon011 0:137fc24033c4 77 uint8_t hh;
xeon011 0:137fc24033c4 78 uint8_t mm;
xeon011 0:137fc24033c4 79 uint8_t ss;
xeon011 0:137fc24033c4 80 } datetime;
xeon011 0:137fc24033c4 81
xeon011 0:137fc24033c4 82 /** SNTPClient client Class.
xeon011 0:137fc24033c4 83 *
xeon011 0:137fc24033c4 84 * Example (ethernet network):
xeon011 0:137fc24033c4 85 * @code
xeon011 0:137fc24033c4 86 * #include "mbed.h"
xeon011 0:137fc24033c4 87 * #include "EthernetInterface.h"
xeon011 0:137fc24033c4 88 * #include "SNTPClient.h"
xeon011 0:137fc24033c4 89 *
xeon011 0:137fc24033c4 90 * int main() {
xeon011 0:137fc24033c4 91 * EthernetInterface eth;
xeon011 0:137fc24033c4 92 * eth.init(); //Use DHCP
xeon011 0:137fc24033c4 93 * eth.connect();
xeon011 0:137fc24033c4 94 * printf("IP Address is %s\n\r", eth.getIPAddress());
xeon011 0:137fc24033c4 95 *
xeon011 0:137fc24033c4 96 * SNTPClient ws("ws://sockets.mbed.org:443/ws/demo/rw");
xeon011 0:137fc24033c4 97 * ws.connect();
xeon011 0:137fc24033c4 98 *
xeon011 0:137fc24033c4 99 * while (1) {
xeon011 0:137fc24033c4 100 * int res = ws.send("SNTPClient Hello World!");
xeon011 0:137fc24033c4 101 *
xeon011 0:137fc24033c4 102 * if (ws.read(recv)) {
xeon011 0:137fc24033c4 103 * printf("rcv: %s\r\n", recv);
xeon011 0:137fc24033c4 104 * }
xeon011 0:137fc24033c4 105 *
xeon011 0:137fc24033c4 106 * wait(0.1);
xeon011 0:137fc24033c4 107 * }
xeon011 0:137fc24033c4 108 * }
xeon011 0:137fc24033c4 109 * @endcode
xeon011 0:137fc24033c4 110 */
xeon011 0:137fc24033c4 111
xeon011 0:137fc24033c4 112
xeon011 0:137fc24033c4 113 class SNTPClient
xeon011 0:137fc24033c4 114 {
xeon011 0:137fc24033c4 115 public:
xeon011 0:137fc24033c4 116 /**
xeon011 0:137fc24033c4 117 * Constructor
xeon011 0:137fc24033c4 118 *
xeon011 0:137fc24033c4 119 * @param url The SNTPClient host
xeon011 0:137fc24033c4 120 */
xeon011 0:137fc24033c4 121 SNTPClient(char * url, uint8_t time_zone);
xeon011 0:137fc24033c4 122
xeon011 0:137fc24033c4 123 /**
xeon011 0:137fc24033c4 124 * Connect to the SNTPClient url
xeon011 0:137fc24033c4 125 *
xeon011 0:137fc24033c4 126 *@return true if the connection is established, false otherwise
xeon011 0:137fc24033c4 127 */
xeon011 0:137fc24033c4 128 bool connect();
xeon011 0:137fc24033c4 129
xeon011 0:137fc24033c4 130 /**
xeon011 0:137fc24033c4 131 * Read a SNTPClient message
xeon011 0:137fc24033c4 132 *
xeon011 0:137fc24033c4 133 * @param message pointer to the string to be read (null if drop frame)
xeon011 0:137fc24033c4 134 *
xeon011 0:137fc24033c4 135 * @return true if a SNTPClient frame has been read
xeon011 0:137fc24033c4 136 */
xeon011 0:137fc24033c4 137 bool getTime(datetime *time);
xeon011 0:137fc24033c4 138
xeon011 0:137fc24033c4 139 /**
xeon011 0:137fc24033c4 140 * Close the SNTPClient connection
xeon011 0:137fc24033c4 141 *
xeon011 0:137fc24033c4 142 * @return true if the connection has been closed, false otherwise
xeon011 0:137fc24033c4 143 */
xeon011 0:137fc24033c4 144 bool close();
xeon011 0:137fc24033c4 145
xeon011 0:137fc24033c4 146 /*
xeon011 0:137fc24033c4 147 * Accessor: get host from the SNTPClient url
xeon011 0:137fc24033c4 148 *
xeon011 0:137fc24033c4 149 * @return host
xeon011 0:137fc24033c4 150 */
xeon011 0:137fc24033c4 151 char* getHost();
xeon011 0:137fc24033c4 152
xeon011 0:137fc24033c4 153 private:
xeon011 0:137fc24033c4 154
xeon011 0:137fc24033c4 155 uint16_t port;
xeon011 0:137fc24033c4 156 char host[32];
xeon011 0:137fc24033c4 157
xeon011 0:137fc24033c4 158 UDPSocket socket;
xeon011 0:137fc24033c4 159 Endpoint sntp_server;
xeon011 0:137fc24033c4 160
xeon011 0:137fc24033c4 161 ntpformat NTPformat;
xeon011 0:137fc24033c4 162 datetime Nowdatetime;
xeon011 0:137fc24033c4 163 uint8_t ntpmessage[48];
xeon011 0:137fc24033c4 164 uint8_t tz; // Time Zone
xeon011 0:137fc24033c4 165
xeon011 0:137fc24033c4 166 void get_seconds_from_ntp_server(uint8_t *buf, uint16_t idx);
xeon011 0:137fc24033c4 167 tstamp changedatetime_to_seconds(void);
xeon011 0:137fc24033c4 168 void calcdatetime(tstamp seconds);
xeon011 0:137fc24033c4 169 };
xeon011 0:137fc24033c4 170
xeon011 0:137fc24033c4 171 #endif