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