Network Services

Dependents:   PwrCond_mbed5

Fork of W5500Interface_K22F by Andrew McCartney

Committer:
dgriffin65
Date:
Thu Jun 15 20:29:03 2017 +0000
Revision:
15:14382459c8b7
Converted to a single library

Who changed what in which revision?

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