Example of HTTPServer with additional features: * SNTPClient, DST rules * Link status indication * Local or SDCard-based WebServer * RPC-able class * Static and Dynamic HTML page

Dependencies:   mbed

Committer:
iva2k
Date:
Fri Jan 08 00:28:14 2010 +0000
Revision:
1:ec4170739967
Parent:
0:886e4b3119ad
Child:
2:360fda42fefd

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iva2k 0:886e4b3119ad 1 /*
iva2k 0:886e4b3119ad 2 * SNTPClient.cpp
iva2k 1:ec4170739967 3 * SNTP (Simple NTP) client
iva2k 0:886e4b3119ad 4 * Written by iva2k
iva2k 0:886e4b3119ad 5 *
iva2k 1:ec4170739967 6 * Wrapper around LWIP/sntp for MBED, with DST rules.
iva2k 0:886e4b3119ad 7 * This implementation relies on:
iva2k 1:ec4170739967 8 * 1. LWIP (http://www.sics.se/~adam/lwip/) adopted for MBED
iva2k 0:886e4b3119ad 9 * http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk
iva2k 1:ec4170739967 10 * 2. LWIP's contributed SNTP client (sntp.c and sntp.h) from
iva2k 0:886e4b3119ad 11 * http://cvs.savannah.gnu.org/viewvc/contrib/apps/sntp/?root=lwip
iva2k 0:886e4b3119ad 12 * (used version 1.8)
iva2k 1:ec4170739967 13 *
iva2k 0:886e4b3119ad 14 * Changes needed in LWIP/sntp:
iva2k 1:ec4170739967 15 * - pointer typecast (line 594) - fixes mbed compiler problem
iva2k 1:ec4170739967 16 * - #include "sntp.h" moved after lwip/opt.h et.al - for re-defines.
iva2k 1:ec4170739967 17 * - pbuf_free(p) in sntp_send_request() (line 602) - fixes memory leak BUG.
iva2k 1:ec4170739967 18 * - changing sntp_dns_found() to delayed sntp_try_next_server() - to unblock if network is disconnected.
iva2k 1:ec4170739967 19 *
iva2k 0:886e4b3119ad 20 * Changes in MBED's LWIP:
iva2k 0:886e4b3119ad 21 * - modified lwipopts.h (this file is automatically included into sntp.c)
iva2k 1:ec4170739967 22 *
iva2k 0:886e4b3119ad 23 * Requirements:
iva2k 0:886e4b3119ad 24 * + direct RTC update from receiving the NTP time packet
iva2k 0:886e4b3119ad 25 * + optionally support more than one NTP server
iva2k 0:886e4b3119ad 26 * + the IP address of the NTP server(s) stored in a local file
iva2k 0:886e4b3119ad 27 * + timeout error recovery should the NTP server(s) not be available. No RTC update should the NTP access fail
iva2k 0:886e4b3119ad 28 * + allow for a UTC offset, also stored in the local file
iva2k 1:ec4170739967 29 * + DST correction
iva2k 0:886e4b3119ad 30 * + use DNS for NTP servers IP address resolution
iva2k 0:886e4b3119ad 31 * + periodic updates at specified time intervals
iva2k 1:ec4170739967 32 *
iva2k 0:886e4b3119ad 33 * TODO:
iva2k 1:ec4170739967 34 * . DST correction
iva2k 0:886e4b3119ad 35 * - record metrics (count of updates, failed tries?, correction more than epsilon, etc.?)
iva2k 0:886e4b3119ad 36 */
iva2k 1:ec4170739967 37
iva2k 0:886e4b3119ad 38 #include "mbed.h"
iva2k 0:886e4b3119ad 39 #include "lwip/opt.h"
iva2k 0:886e4b3119ad 40 #include "SNTPClient.h"
iva2k 0:886e4b3119ad 41
iva2k 0:886e4b3119ad 42 #define SNTP_EPSILON 10 // time difference noticed as big update (in seconds).
iva2k 0:886e4b3119ad 43
iva2k 1:ec4170739967 44 //#define SNTP_DST_TESTS // Define this to run DST algorithms tests
iva2k 1:ec4170739967 45
iva2k 1:ec4170739967 46 typedef struct DST_ZONE_DESCR {
iva2k 1:ec4170739967 47 tDST_ZONE zone;
iva2k 1:ec4170739967 48 const char *name;
iva2k 1:ec4170739967 49 int gmt;
iva2k 1:ec4170739967 50 int dst;
iva2k 1:ec4170739967 51 int hr1;
iva2k 1:ec4170739967 52 int wk1;
iva2k 1:ec4170739967 53 int wday1;
iva2k 1:ec4170739967 54 int mon1;
iva2k 1:ec4170739967 55 int hr2;
iva2k 1:ec4170739967 56 int wk2;
iva2k 1:ec4170739967 57 int wday2;
iva2k 1:ec4170739967 58 int mon2;
iva2k 1:ec4170739967 59 pFncDstCalc fnc;
iva2k 1:ec4170739967 60 } tDST_ZONE_DESR;
iva2k 1:ec4170739967 61
iva2k 1:ec4170739967 62 static tDST_ZONE_DESR gSntpDstZones[] = {
iva2k 1:ec4170739967 63 #define _(z, fnc, gmt, dst, hr1,wk1,wday1,mon1, hr2,wk2,wday2,mon2) \
iva2k 1:ec4170739967 64 { z, #z, gmt, dst, hr1,wk1,wday1,mon1, hr2,wk2,wday2,mon2, fnc },
iva2k 1:ec4170739967 65 #include "DstZones.h"
iva2k 1:ec4170739967 66 };
iva2k 1:ec4170739967 67 #define DST_ZONE_DESCR_CNT (sizeof(gSntpDstZones)/sizeof(gSntpDstZones[0]))
iva2k 1:ec4170739967 68
iva2k 1:ec4170739967 69 const char *SNTPDstZoneName(tDST_ZONE zone) {
iva2k 1:ec4170739967 70 if (zone >= DST_ZONE_DESCR_CNT)
iva2k 1:ec4170739967 71 return "";
iva2k 1:ec4170739967 72 return gSntpDstZones[zone].name;
iva2k 1:ec4170739967 73 }
iva2k 1:ec4170739967 74
iva2k 0:886e4b3119ad 75 #ifdef __cplusplus
iva2k 0:886e4b3119ad 76 extern "C" {
iva2k 0:886e4b3119ad 77 #endif
iva2k 1:ec4170739967 78 tDST_ZONE gSntpDstZone = DST_NONE; // DST zone - rule selector
iva2k 0:886e4b3119ad 79 unsigned int gSntpRecvTimeout_ms = 3000; // 3 sec; SNTP_RECV_TIMEOUT
iva2k 0:886e4b3119ad 80 unsigned int gSntpUpdateDelay_ms = 3600000; // 1 hour; SNTP_UPDATE_DELAY
iva2k 0:886e4b3119ad 81 signed int gSntpTimezone = 0*3600; // seconds from UTC to local time
iva2k 0:886e4b3119ad 82 signed int gSntpDST = 0*3600; // seconds from UTC to local time
iva2k 0:886e4b3119ad 83 bool gSntpRtcUtc = false; // true to keep RTC in UTC, false to keep in local time
iva2k 1:ec4170739967 84 unsigned int gSntpUpdates = 0; // Track number of all clock NTP updates
iva2k 1:ec4170739967 85 unsigned int gSntpUpdatesBig = 0; // Track number of significant clock NTP updates
iva2k 1:ec4170739967 86 bool gSntpRunning = false; // true if SNTP service is running
iva2k 1:ec4170739967 87 Timeout gSntpDelay; // For async calls.
iva2k 1:ec4170739967 88 Ticker gSntpTicker; // There is no RTC interrupt on MBED (yet). Use (more wastefull) timer.
iva2k 1:ec4170739967 89 time_t gSntpRtcTCR = 0; // Timer Capture Register equivalent (in RTC time - either local or UTC depending on gSntpRtcUtc)
iva2k 1:ec4170739967 90 signed int gSntpRtcTCRDST = 0; // New DST value for when RTC crosses gSntpRtcTCR
iva2k 1:ec4170739967 91
iva2k 1:ec4170739967 92 static void SNTPSetDSTEx(unsigned int dst, bool adjust_clock) {
iva2k 1:ec4170739967 93 if (adjust_clock && !gSntpRtcUtc && gSntpDST != dst) {
iva2k 1:ec4170739967 94 time_t seconds = time(NULL);
iva2k 1:ec4170739967 95 seconds -= gSntpDST; // Convert from old local time
iva2k 1:ec4170739967 96 seconds += dst; // Convert to new local time
iva2k 1:ec4170739967 97 set_time(seconds);
iva2k 1:ec4170739967 98 if (gSntpRtcTCR) {
iva2k 1:ec4170739967 99 // Adjust our alarm clock
iva2k 1:ec4170739967 100 gSntpRtcTCR -= gSntpDST;
iva2k 1:ec4170739967 101 gSntpRtcTCR += dst;
iva2k 1:ec4170739967 102 }
iva2k 1:ec4170739967 103 }
iva2k 1:ec4170739967 104 gSntpDST = dst;
iva2k 1:ec4170739967 105 }
iva2k 1:ec4170739967 106
iva2k 1:ec4170739967 107 #if 0
iva2k 1:ec4170739967 108 // Custom DST zone function example
iva2k 1:ec4170739967 109 // USA (since 2007)
iva2k 1:ec4170739967 110 // Calculate start or stop DST point for given year based on rules
iva2k 1:ec4170739967 111 // tz - Timezone
iva2k 1:ec4170739967 112 // year - current year. DST changes well away from year end, so does not matter what timezone.
iva2k 1:ec4170739967 113 // start - DST_START for start, DST_STOP for stop
iva2k 1:ec4170739967 114 // Returns DST point (in local time). DST_STOP time is in DST
iva2k 1:ec4170739967 115 static tDstPoint _sntp_dst_calc_custom(int tz, int year, tDST_START start) {
iva2k 1:ec4170739967 116 tDstPoint ret;
iva2k 1:ec4170739967 117 struct tm t;
iva2k 1:ec4170739967 118
iva2k 1:ec4170739967 119 // USA: 3600; 2, Second SUN, March; 2, First SUN, November
iva2k 1:ec4170739967 120 int dst = 3600; // DST shift (seconds)
iva2k 1:ec4170739967 121 int hour; // Hour of the change
iva2k 1:ec4170739967 122 int N; // Week in the month
iva2k 1:ec4170739967 123 int wday; // Day of the week
iva2k 1:ec4170739967 124 int month;
iva2k 1:ec4170739967 125 if (start == DST_START) {
iva2k 1:ec4170739967 126 hour = 2; // (0-23) Hour of the change
iva2k 1:ec4170739967 127 N = 2-1; // 2nd in the month
iva2k 1:ec4170739967 128 wday = 0; // Sunday
iva2k 1:ec4170739967 129 month = 2; // March
iva2k 1:ec4170739967 130 } else { // DST_STOP
iva2k 1:ec4170739967 131 hour = 2; // (0-23) Hour of the change
iva2k 1:ec4170739967 132 N = 1-1; // 1st in the month
iva2k 1:ec4170739967 133 wday = 0; // Sunday
iva2k 1:ec4170739967 134 month = 10; // November
iva2k 1:ec4170739967 135 }
iva2k 1:ec4170739967 136
iva2k 1:ec4170739967 137 t.tm_sec = 0; // 0-59
iva2k 1:ec4170739967 138 t.tm_min = 0; // 0-59
iva2k 1:ec4170739967 139 t.tm_hour = hour; // 0-23
iva2k 1:ec4170739967 140 t.tm_year = year - 1900; //
iva2k 1:ec4170739967 141 t.tm_mon = month; // 0-11
iva2k 1:ec4170739967 142 t.tm_mday = 1+N*7; // 1-31, first possible date for Nth given weekday
iva2k 1:ec4170739967 143 mktime(&t); // Calculate tm_wday
iva2k 1:ec4170739967 144 t.tm_mday += (wday-t.tm_wday+14)%7; // Shift to wday
iva2k 1:ec4170739967 145 ret.t = mktime(&t);
iva2k 1:ec4170739967 146 ret.dst = (start == DST_START) ? dst : 0;
iva2k 1:ec4170739967 147 ret.dstshift = dst;
iva2k 1:ec4170739967 148 return ret;
iva2k 1:ec4170739967 149 }
iva2k 1:ec4170739967 150 #endif
iva2k 1:ec4170739967 151
iva2k 1:ec4170739967 152 // Calculate start or stop DST point for given year based on rules for the DST zone
iva2k 1:ec4170739967 153 // tz - Timezone
iva2k 1:ec4170739967 154 // zone - DST zone
iva2k 1:ec4170739967 155 // year - current year. DST changes well away from year end, so does not matter timezone.
iva2k 1:ec4170739967 156 // start - DST_START for start, DST_STOP for stop
iva2k 1:ec4170739967 157 // Returns DST point (in standard local time). DST_STOP time is in NOT IN DST
iva2k 1:ec4170739967 158 static tDstPoint _sntp_dst_calc(int tz, tDST_ZONE zone, int year, tDST_START start) {
iva2k 1:ec4170739967 159 tDstPoint ret;
iva2k 1:ec4170739967 160
iva2k 1:ec4170739967 161 ret.t = 0;
iva2k 1:ec4170739967 162 ret.dst = 0;
iva2k 1:ec4170739967 163 ret.dstshift = 0;
iva2k 1:ec4170739967 164 if (zone == DST_NONE || zone >= DST_ZONE_DESCR_CNT)
iva2k 1:ec4170739967 165 return ret;
iva2k 1:ec4170739967 166
iva2k 1:ec4170739967 167 tDST_ZONE_DESR *descr = &gSntpDstZones[zone];
iva2k 1:ec4170739967 168 if (descr->fnc) {
iva2k 1:ec4170739967 169 // Use custom function
iva2k 1:ec4170739967 170 ret = descr->fnc(tz, year, start);
iva2k 1:ec4170739967 171 } else {
iva2k 1:ec4170739967 172 // Use rules
iva2k 1:ec4170739967 173 struct tm t;
iva2k 1:ec4170739967 174 t.tm_sec = 0; // 0-59
iva2k 1:ec4170739967 175 t.tm_min = 0; // 0-59
iva2k 1:ec4170739967 176 t.tm_hour = (start == DST_START) ? descr->hr1 : descr->hr2;
iva2k 1:ec4170739967 177 t.tm_year = year - 1900; //
iva2k 1:ec4170739967 178 t.tm_mon = (start == DST_START) ? descr->mon1 : descr->mon2; // 0-11
iva2k 1:ec4170739967 179 int wk =((start == DST_START) ? descr->wk1 : descr->wk2) -1;
iva2k 1:ec4170739967 180 int wday = (start == DST_START) ? descr->wday1: descr->wday2;
iva2k 1:ec4170739967 181 if (wk < 0) {
iva2k 1:ec4170739967 182 // For "Last in the month" - we go to next month, then move back by one week
iva2k 1:ec4170739967 183 t.tm_mon += 1; // 0-11
iva2k 1:ec4170739967 184 if (t.tm_mon > 11) {
iva2k 1:ec4170739967 185 t.tm_mon -= 12;
iva2k 1:ec4170739967 186 t.tm_year += 1;
iva2k 1:ec4170739967 187 }
iva2k 1:ec4170739967 188 t.tm_mday = 1+0*7; // 1-31, first possible date for Nth given weekday
iva2k 1:ec4170739967 189 } else {
iva2k 1:ec4170739967 190 t.tm_mday = 1+wk*7; // 1-31, first possible date for Nth given weekday
iva2k 1:ec4170739967 191 }
iva2k 1:ec4170739967 192 mktime(&t); // Calculate tm_wday
iva2k 1:ec4170739967 193 t.tm_mday += (wday-t.tm_wday+14)%7; // Shift to wday
iva2k 1:ec4170739967 194 ret.t = mktime(&t);
iva2k 1:ec4170739967 195 if (wk < 0) {
iva2k 1:ec4170739967 196 ret.t -= 7 * 24 * 3600;
iva2k 1:ec4170739967 197 }
iva2k 1:ec4170739967 198 if (descr->gmt) {
iva2k 1:ec4170739967 199 ret.t += tz;
iva2k 1:ec4170739967 200 }
iva2k 1:ec4170739967 201 ret.dst = (start == DST_START) ? descr->dst : 0;
iva2k 1:ec4170739967 202 ret.dstshift = descr->dst;
iva2k 1:ec4170739967 203 }
iva2k 1:ec4170739967 204 if (start == DST_STOP) {
iva2k 1:ec4170739967 205 ret.t -= ret.dstshift;
iva2k 1:ec4170739967 206 // this correction is due to the fact that rules are given in local time with DST,
iva2k 1:ec4170739967 207 // but calculations are made in standard local time (without DST adjustment)
iva2k 1:ec4170739967 208 }
iva2k 1:ec4170739967 209 return ret;
iva2k 1:ec4170739967 210 }
iva2k 1:ec4170739967 211
iva2k 1:ec4170739967 212 // Calculate desired DST point relative to now based on rules for the DST zone
iva2k 1:ec4170739967 213 // tz - timezone
iva2k 1:ec4170739967 214 // zone - DST zone
iva2k 1:ec4170739967 215 // now - current time (standard local time = excluding DST).
iva2k 1:ec4170739967 216 // index - 0 for immediate next, +1 for second next, -1 for immediate previous
iva2k 1:ec4170739967 217 // Returns DST point (in standard local time)
iva2k 1:ec4170739967 218 static tDstPoint _sntp_dst_point(int tz, tDST_ZONE zone, time_t now, int index) {
iva2k 1:ec4170739967 219 tDstPoint ret;
iva2k 1:ec4170739967 220 int year;
iva2k 1:ec4170739967 221 tDST_START type;
iva2k 1:ec4170739967 222 struct tm *pt = localtime(&now);
iva2k 1:ec4170739967 223
iva2k 1:ec4170739967 224 ret.t = 0;
iva2k 1:ec4170739967 225 ret.dst = 0;
iva2k 1:ec4170739967 226 ret.dstshift = 0;
iva2k 1:ec4170739967 227 if (zone == DST_NONE || zone >= DST_ZONE_DESCR_CNT)
iva2k 1:ec4170739967 228 return ret;
iva2k 1:ec4170739967 229
iva2k 1:ec4170739967 230 // Algorithm
iva2k 1:ec4170739967 231 // 1. Determine where now is in respect to current year DST points (find for index=0)
iva2k 1:ec4170739967 232 // 2. Use index to shift year and type
iva2k 1:ec4170739967 233 // 3. return the point
iva2k 1:ec4170739967 234 // This algorithm relies on DST start date being before DST stop date in the year.
iva2k 1:ec4170739967 235
iva2k 1:ec4170739967 236 year = pt->tm_year + 1900;
iva2k 1:ec4170739967 237 type = DST_START;
iva2k 1:ec4170739967 238 ret = _sntp_dst_calc(tz, zone, year, type);
iva2k 1:ec4170739967 239 if (now > ret.t) {
iva2k 1:ec4170739967 240 type = DST_STOP;
iva2k 1:ec4170739967 241 ret = _sntp_dst_calc(tz, zone, year, type);
iva2k 1:ec4170739967 242 if (now > ret.t) {
iva2k 1:ec4170739967 243 // It is next year's start point
iva2k 1:ec4170739967 244 type = DST_START;
iva2k 1:ec4170739967 245 year += 1;
iva2k 1:ec4170739967 246 }
iva2k 1:ec4170739967 247 }
iva2k 1:ec4170739967 248 // Now year and type are right for index=0
iva2k 1:ec4170739967 249
iva2k 1:ec4170739967 250 // Calculate where index points to - shift year and type
iva2k 1:ec4170739967 251 int norm = (index > 0) ? (int)(index/2) : (int)((index-1)/2);
iva2k 1:ec4170739967 252 year += norm;
iva2k 1:ec4170739967 253 index -= norm*2; // Now index is (0,1)
iva2k 1:ec4170739967 254 if (index) {
iva2k 1:ec4170739967 255 // Flip the type
iva2k 1:ec4170739967 256 type = (type == DST_START) ? DST_STOP : DST_START;
iva2k 1:ec4170739967 257 }
iva2k 1:ec4170739967 258
iva2k 1:ec4170739967 259 ret = _sntp_dst_calc(tz, zone, year, type);
iva2k 1:ec4170739967 260 return ret;
iva2k 1:ec4170739967 261
iva2k 1:ec4170739967 262 // struct tm t;
iva2k 1:ec4170739967 263 // t.tm_sec = 0; // 0-59
iva2k 1:ec4170739967 264 // t.tm_min = 0; // 0-59
iva2k 1:ec4170739967 265 // t.tm_hour = 0; // 0-23
iva2k 1:ec4170739967 266 // t.tm_mday = 0; // 1-31
iva2k 1:ec4170739967 267 // t.tm_mon = 0; // 0-11
iva2k 1:ec4170739967 268 // t.tm_year = 2005-1900; // year since 1900
iva2k 1:ec4170739967 269 // t.tm_wday = 0; // 0-6 Day of week (Sunday = 0)
iva2k 1:ec4170739967 270 // t.tm_yday = 0; // Day of year (0 - 365)
iva2k 1:ec4170739967 271 // t.tm_isdst = -1; // Nonzero = Daylight saving time
iva2k 1:ec4170739967 272 }
iva2k 1:ec4170739967 273
iva2k 1:ec4170739967 274 #if defined(LWIP_DEBUG)
iva2k 1:ec4170739967 275 // Print DST dates report
iva2k 1:ec4170739967 276 static void _sntp_dst_dates(int tz, tDST_ZONE zone, int year) {
iva2k 1:ec4170739967 277 char _buffer[64];
iva2k 1:ec4170739967 278 tDstPoint r;
iva2k 1:ec4170739967 279 printf("\r\nDST DATES for zone %d/%s:\r\n", (int)zone, SNTPDstZoneName(zone));
iva2k 1:ec4170739967 280
iva2k 1:ec4170739967 281 r = _sntp_dst_calc(tz, zone, year, DST_START);
iva2k 1:ec4170739967 282 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 283 printf(" %d DST BEGINS (+%04d sec) %s\r\n", year, r.dst, _buffer);
iva2k 1:ec4170739967 284
iva2k 1:ec4170739967 285 r = _sntp_dst_calc(tz, zone, year, DST_STOP);
iva2k 1:ec4170739967 286 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 287 printf(" %d DST ENDS (+%04d sec) %s\r\n", year, r.dst, _buffer);
iva2k 1:ec4170739967 288 }
iva2k 1:ec4170739967 289 #endif
iva2k 1:ec4170739967 290
iva2k 1:ec4170739967 291 #ifdef SNTP_DST_TESTS
iva2k 1:ec4170739967 292 static void test_sntp_dst_calc(int tz, tDST_ZONE zone) {
iva2k 1:ec4170739967 293 char _buffer[64];
iva2k 1:ec4170739967 294 tDstPoint r;
iva2k 1:ec4170739967 295 int year;
iva2k 1:ec4170739967 296
iva2k 1:ec4170739967 297 printf("\r\nTEST: _sntp_dst_calc(tz=%d, zone=%d/%s)\r\n", tz, (int)zone, SNTPDstZoneName(zone));
iva2k 1:ec4170739967 298
iva2k 1:ec4170739967 299 for (year = 1999; year < 2016; year++) {
iva2k 1:ec4170739967 300 r = _sntp_dst_calc(tz, zone, year, DST_START);
iva2k 1:ec4170739967 301 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 302 printf(" (0,%d,start) %s %d\r\n", year, _buffer, r.dst);
iva2k 1:ec4170739967 303
iva2k 1:ec4170739967 304 r = _sntp_dst_calc(tz, zone, year, DST_STOP);
iva2k 1:ec4170739967 305 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 306 printf(" (0,%d,stop ) %s %d\r\n", year, _buffer, r.dst);
iva2k 1:ec4170739967 307 }
iva2k 1:ec4170739967 308 }
iva2k 1:ec4170739967 309
iva2k 1:ec4170739967 310 static void test_sntp_dst_point(int index) {
iva2k 1:ec4170739967 311 char _buffer[64];
iva2k 1:ec4170739967 312 struct tm t;
iva2k 1:ec4170739967 313 tDstPoint r;
iva2k 1:ec4170739967 314 int tz=gSntpTimezone;
iva2k 1:ec4170739967 315 tDST_ZONE zone=gSntpDstZone;
iva2k 1:ec4170739967 316 int year = 2009, day, month;
iva2k 1:ec4170739967 317
iva2k 1:ec4170739967 318 int norm = (index > 0) ? (int)(index/2) : (int)((index-1)/2);
iva2k 1:ec4170739967 319 printf("\r\nTEST: _sntp_dst_point(%d) norm=%d\r\n", index, norm);
iva2k 1:ec4170739967 320
iva2k 1:ec4170739967 321 t.tm_sec = 0; // 0-59
iva2k 1:ec4170739967 322 t.tm_min = 0; // 0-59
iva2k 1:ec4170739967 323 t.tm_hour = 9; // 0-23
iva2k 1:ec4170739967 324 t.tm_year = year-1900;
iva2k 1:ec4170739967 325
iva2k 1:ec4170739967 326 day = 1;
iva2k 1:ec4170739967 327 for (month = 0; month < 2; month++) {
iva2k 1:ec4170739967 328 t.tm_mon = month;
iva2k 1:ec4170739967 329 t.tm_mday = day;
iva2k 1:ec4170739967 330 t.tm_year = year-1900;
iva2k 1:ec4170739967 331 r = _sntp_dst_point(tz, zone, mktime(&t), index);
iva2k 1:ec4170739967 332 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 333 printf(" (0,%02d/%02d/%d,%d) %s %d\r\n", month+1, day, year, index, _buffer, r.dst);
iva2k 1:ec4170739967 334 }
iva2k 1:ec4170739967 335
iva2k 1:ec4170739967 336 for (day = 1; day < 32; day++) {
iva2k 1:ec4170739967 337 t.tm_mon = month;
iva2k 1:ec4170739967 338 t.tm_mday = day;
iva2k 1:ec4170739967 339 t.tm_year = year-1900;
iva2k 1:ec4170739967 340 r = _sntp_dst_point(tz, zone, mktime(&t), index);
iva2k 1:ec4170739967 341 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 342 printf(" (0,%02d/%02d/%d,%d) %s %d\r\n", month+1, day, year, index, _buffer, r.dst);
iva2k 1:ec4170739967 343 }
iva2k 1:ec4170739967 344
iva2k 1:ec4170739967 345 day = 30;
iva2k 1:ec4170739967 346 for (; month < 10; month++) {
iva2k 1:ec4170739967 347 t.tm_mon = month;
iva2k 1:ec4170739967 348 t.tm_mday = day;
iva2k 1:ec4170739967 349 t.tm_year = year-1900;
iva2k 1:ec4170739967 350 r = _sntp_dst_point(tz, zone, mktime(&t), index);
iva2k 1:ec4170739967 351 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 352 printf(" (0,%02d/%02d/%d,%d) %s %d\r\n", month+1, day, year, index, _buffer, r.dst);
iva2k 1:ec4170739967 353 }
iva2k 1:ec4170739967 354
iva2k 1:ec4170739967 355 for (day = 1; day < 32; day++) {
iva2k 1:ec4170739967 356 t.tm_mon = month;
iva2k 1:ec4170739967 357 t.tm_mday = day;
iva2k 1:ec4170739967 358 t.tm_year = year-1900;
iva2k 1:ec4170739967 359 r = _sntp_dst_point(tz, zone, mktime(&t), index);
iva2k 1:ec4170739967 360 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&r.t));
iva2k 1:ec4170739967 361 printf(" (0,%02d/%02d/%d,%d) %s %d\r\n", month+1, day, year, index, _buffer, r.dst);
iva2k 1:ec4170739967 362 }
iva2k 1:ec4170739967 363
iva2k 1:ec4170739967 364 }
iva2k 1:ec4170739967 365 #endif // SNTP_DST_TESTS
iva2k 1:ec4170739967 366
iva2k 1:ec4170739967 367 // Set current DST
iva2k 1:ec4170739967 368 static void _sntp_dst_now(void) {
iva2k 1:ec4170739967 369 time_t now = time(NULL);
iva2k 1:ec4170739967 370 // Convert to standart local time (no DST)
iva2k 1:ec4170739967 371 now = gSntpRtcUtc ? (now + gSntpTimezone) : (now - gSntpDST);
iva2k 1:ec4170739967 372
iva2k 1:ec4170739967 373 // Check DST setting for now
iva2k 1:ec4170739967 374 tDstPoint dst = _sntp_dst_point(gSntpTimezone, gSntpDstZone, now, -1);
iva2k 1:ec4170739967 375
iva2k 1:ec4170739967 376 #ifdef LWIP_DEBUG
iva2k 1:ec4170739967 377 char _buffer[64];
iva2k 1:ec4170739967 378 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&dst.t));
iva2k 1:ec4170739967 379 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
iva2k 1:ec4170739967 380 "DEBUG: _sntp_dst_now(%d) based on last DST change on (%d) %s to (+%04d sec)\r\n",
iva2k 1:ec4170739967 381 now, dst.t, _buffer, dst.dst));
iva2k 1:ec4170739967 382 #endif
iva2k 1:ec4170739967 383
iva2k 1:ec4170739967 384 // Change RTC
iva2k 1:ec4170739967 385 SNTPSetDSTEx(dst.dst, true);
iva2k 1:ec4170739967 386 }
iva2k 1:ec4170739967 387
iva2k 1:ec4170739967 388 // Plan for next DST change
iva2k 1:ec4170739967 389 static void _sntp_dst_schedule(void) {
iva2k 1:ec4170739967 390 time_t now = time(NULL);
iva2k 1:ec4170739967 391 // Convert to standart local time (no DST)
iva2k 1:ec4170739967 392 now = gSntpRtcUtc ? (now + gSntpTimezone) : (now - gSntpDST);
iva2k 1:ec4170739967 393
iva2k 1:ec4170739967 394 // Check next DST change point
iva2k 1:ec4170739967 395 tDstPoint dst = _sntp_dst_point(gSntpTimezone, gSntpDstZone, now, 0);
iva2k 1:ec4170739967 396
iva2k 1:ec4170739967 397 if (dst.t) {
iva2k 1:ec4170739967 398
iva2k 1:ec4170739967 399 #ifdef LWIP_DEBUG
iva2k 1:ec4170739967 400 char _buffer[64];
iva2k 1:ec4170739967 401 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&dst.t));
iva2k 1:ec4170739967 402 #endif
iva2k 1:ec4170739967 403
iva2k 1:ec4170739967 404 // Set our alarm clock
iva2k 1:ec4170739967 405 // Convert from standard local to UTC time or local time
iva2k 1:ec4170739967 406 dst.t = gSntpRtcUtc ? (dst.t - gSntpTimezone) : (dst.t + gSntpDST);
iva2k 1:ec4170739967 407
iva2k 1:ec4170739967 408 #ifdef LWIP_DEBUG
iva2k 1:ec4170739967 409 if (time(NULL) > dst.t) {
iva2k 1:ec4170739967 410 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
iva2k 1:ec4170739967 411 "DEBUG: _sntp_dst_schedule() ASSERTION FAILED !(%d<%d) trying to schedule for the past: %s (+%04d sec)\r\n",
iva2k 1:ec4170739967 412 time(NULL), dst.t, _buffer, dst.dst));
iva2k 1:ec4170739967 413 } else {
iva2k 1:ec4170739967 414 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
iva2k 1:ec4170739967 415 "DEBUG: _sntp_dst_schedule() scheduled in %d seconds, set for %s (+%04d sec)\r\n",
iva2k 1:ec4170739967 416 dst.t-time(NULL), _buffer, dst.dst));
iva2k 1:ec4170739967 417 }
iva2k 1:ec4170739967 418 #endif
iva2k 1:ec4170739967 419 }
iva2k 1:ec4170739967 420 gSntpRtcTCR = dst.t;
iva2k 1:ec4170739967 421 gSntpRtcTCRDST = dst.dst;
iva2k 1:ec4170739967 422 }
iva2k 1:ec4170739967 423
iva2k 1:ec4170739967 424 // RTC ISR - called upon each RTC tick
iva2k 1:ec4170739967 425 static void _sntp_isr(void) {
iva2k 1:ec4170739967 426 time_t seconds = time(NULL);
iva2k 1:ec4170739967 427 if (gSntpRtcTCR && seconds > gSntpRtcTCR ) {
iva2k 1:ec4170739967 428 // DST change has arrived
iva2k 1:ec4170739967 429 gSntpRtcTCR = 0;
iva2k 1:ec4170739967 430 // Disable ISR and avoid extra calcs in SNTPSetDSTEx()
iva2k 1:ec4170739967 431
iva2k 1:ec4170739967 432 //if (gSntpRtcTCRDST != gSntpDST) {
iva2k 1:ec4170739967 433 // Change to/from DST
iva2k 1:ec4170739967 434 SNTPSetDSTEx(gSntpRtcTCRDST, true);
iva2k 1:ec4170739967 435 gSntpRtcTCRDST = 0;
iva2k 1:ec4170739967 436
iva2k 1:ec4170739967 437 // Schedule callback to plan for next DST change (take it out of ISR context)
iva2k 1:ec4170739967 438 gSntpDelay.attach(_sntp_dst_schedule, 1.0);
iva2k 1:ec4170739967 439
iva2k 1:ec4170739967 440 #ifdef LWIP_DEBUG
iva2k 1:ec4170739967 441 char _buffer[64];
iva2k 1:ec4170739967 442 if (gSntpRtcUtc) {
iva2k 1:ec4170739967 443 seconds += gSntpTimezone + gSntpDST; // Convert to local time
iva2k 1:ec4170739967 444 }
iva2k 1:ec4170739967 445 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&seconds));
iva2k 1:ec4170739967 446 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
iva2k 1:ec4170739967 447 "DEBUG: _sntp_isr() changed DST. datetime=%s\r\n", _buffer));
iva2k 1:ec4170739967 448 #endif
iva2k 1:ec4170739967 449
iva2k 1:ec4170739967 450 //}
iva2k 1:ec4170739967 451
iva2k 1:ec4170739967 452 }
iva2k 1:ec4170739967 453 }
iva2k 0:886e4b3119ad 454
iva2k 0:886e4b3119ad 455 // NTP Callback - timestamp from NTP server arrives here
iva2k 0:886e4b3119ad 456 void SntpClientSet(time_t seconds) {
iva2k 0:886e4b3119ad 457 time_t old_seconds = time(NULL);
iva2k 0:886e4b3119ad 458 if (!gSntpRtcUtc) {
iva2k 0:886e4b3119ad 459 seconds += gSntpTimezone + gSntpDST; // Convert to local time
iva2k 0:886e4b3119ad 460 }
iva2k 0:886e4b3119ad 461 set_time(seconds);
iva2k 1:ec4170739967 462
iva2k 1:ec4170739967 463 // Special tasks for the very first NTP updates
iva2k 1:ec4170739967 464 if (!gSntpUpdates) {
iva2k 1:ec4170739967 465
iva2k 1:ec4170739967 466 #if defined(LWIP_DEBUG)
iva2k 1:ec4170739967 467 // Report DST dates for the zone
iva2k 1:ec4170739967 468 struct tm *pt = localtime(&seconds);
iva2k 1:ec4170739967 469 _sntp_dst_dates(gSntpTimezone, gSntpDstZone, pt->tm_year+1900);
iva2k 1:ec4170739967 470 #endif
iva2k 1:ec4170739967 471
iva2k 1:ec4170739967 472 // DST scheduler
iva2k 1:ec4170739967 473 _sntp_dst_now();
iva2k 1:ec4170739967 474 _sntp_dst_schedule();
iva2k 1:ec4170739967 475
iva2k 1:ec4170739967 476 // Enable RTC ISR
iva2k 1:ec4170739967 477 gSntpTicker.attach(_sntp_isr, 1.0);
iva2k 1:ec4170739967 478 }
iva2k 1:ec4170739967 479
iva2k 0:886e4b3119ad 480 if (gSntpUpdates && abs((int)(old_seconds - seconds)) > SNTP_EPSILON) {
iva2k 0:886e4b3119ad 481 printf("SNTP settime() corrected big difference: (%d) seconds, more than %d. Big updates count=%d.\r\n",
iva2k 0:886e4b3119ad 482 old_seconds-seconds, SNTP_EPSILON, gSntpUpdatesBig);
iva2k 0:886e4b3119ad 483 gSntpUpdatesBig ++;
iva2k 0:886e4b3119ad 484 }
iva2k 1:ec4170739967 485
iva2k 0:886e4b3119ad 486 gSntpUpdates++;
iva2k 0:886e4b3119ad 487
iva2k 0:886e4b3119ad 488 if (1) {
iva2k 0:886e4b3119ad 489 char _buffer[64];
iva2k 0:886e4b3119ad 490 if (gSntpRtcUtc) {
iva2k 0:886e4b3119ad 491 seconds += gSntpTimezone + gSntpDST; // Convert to local time
iva2k 0:886e4b3119ad 492 }
iva2k 0:886e4b3119ad 493 strftime(_buffer, sizeof(_buffer), "%A %m/%d/%Y %H:%M:%S", localtime(&seconds));
iva2k 0:886e4b3119ad 494 printf("SNTP settime() #%d seconds=%d TZ=%0.1f datetime=%s\r\n",
iva2k 0:886e4b3119ad 495 gSntpUpdates, seconds, gSntpTimezone/3600.0, _buffer);
iva2k 0:886e4b3119ad 496 }
iva2k 0:886e4b3119ad 497 }
iva2k 0:886e4b3119ad 498 #ifdef __cplusplus
iva2k 0:886e4b3119ad 499 };
iva2k 0:886e4b3119ad 500 #endif
iva2k 0:886e4b3119ad 501
iva2k 0:886e4b3119ad 502 #ifdef __cplusplus
iva2k 0:886e4b3119ad 503 extern "C" {
iva2k 0:886e4b3119ad 504 #endif
iva2k 0:886e4b3119ad 505
iva2k 0:886e4b3119ad 506 #include "lwip/def.h"
iva2k 0:886e4b3119ad 507 #include "lwip/pbuf.h"
iva2k 0:886e4b3119ad 508 #include "lwip/sys.h"
iva2k 0:886e4b3119ad 509 #include "lwip/stats.h"
iva2k 0:886e4b3119ad 510 #include "netif/etharp.h"
iva2k 0:886e4b3119ad 511 #include "string.h"
iva2k 0:886e4b3119ad 512
iva2k 0:886e4b3119ad 513 #ifdef __cplusplus
iva2k 0:886e4b3119ad 514 };
iva2k 0:886e4b3119ad 515 #endif
iva2k 0:886e4b3119ad 516
iva2k 0:886e4b3119ad 517 // Accomodating sntp timeout functions
iva2k 0:886e4b3119ad 518 #if NO_SYS
iva2k 0:886e4b3119ad 519 #include "sntp.h"
iva2k 0:886e4b3119ad 520 extern void sntp_request(void *arg); // this is dirty hack around "static" function. Some linkers may revolt!
iva2k 0:886e4b3119ad 521 extern void sntp_try_next_server(void *arg);
iva2k 0:886e4b3119ad 522 extern char* sntp_server_addresses[];
iva2k 0:886e4b3119ad 523 extern u8_t sntp_current_server;
iva2k 0:886e4b3119ad 524 extern u8_t sntp_num_servers;
iva2k 0:886e4b3119ad 525 static void (*sntp_addresses_free)(void*) = NULL;
iva2k 0:886e4b3119ad 526 static Timeout _sntp_timer1;
iva2k 0:886e4b3119ad 527 static Timeout _sntp_timer2;
iva2k 0:886e4b3119ad 528 void sntp_sys_timeout(u32_t timeout_ms, void (*func)(void *arg), void *arg) {
iva2k 0:886e4b3119ad 529 // all we really need to track is only 2 functions: sntp_request, sntp_try_next_server
iva2k 0:886e4b3119ad 530 Timeout *t = NULL;
iva2k 0:886e4b3119ad 531 if (func == &sntp_request) {
iva2k 0:886e4b3119ad 532 t = &_sntp_timer1;
iva2k 1:ec4170739967 533 LWIP_DEBUGF(SNTP_DEBUG_STATE, ("DEBUG: IN sntp_sys_timeout(), func=sntp_request\r\n"));
iva2k 0:886e4b3119ad 534 } else if (func == &sntp_try_next_server) {
iva2k 0:886e4b3119ad 535 t = &_sntp_timer2;
iva2k 1:ec4170739967 536 LWIP_DEBUGF(SNTP_DEBUG_STATE, ("DEBUG: IN sntp_sys_timeout(), func=sntp_try_next_server\r\n"));
iva2k 0:886e4b3119ad 537 }
iva2k 0:886e4b3119ad 538 if (t) {
iva2k 0:886e4b3119ad 539 t->detach();
iva2k 0:886e4b3119ad 540 t->attach((void(*)(void))func, 0.001*timeout_ms);
iva2k 0:886e4b3119ad 541 // Another shortcut - we have no arg to pass, so just typecast the func.
iva2k 0:886e4b3119ad 542 }
iva2k 0:886e4b3119ad 543 }
iva2k 0:886e4b3119ad 544
iva2k 0:886e4b3119ad 545 void sntp_sys_untimeout(void (*func)(void *arg), void *arg) {
iva2k 0:886e4b3119ad 546 Timeout *t = NULL;
iva2k 0:886e4b3119ad 547 if (func == &sntp_request) {
iva2k 0:886e4b3119ad 548 t = &_sntp_timer1;
iva2k 1:ec4170739967 549 LWIP_DEBUGF(SNTP_DEBUG_STATE, ("DEBUG: IN sntp_sys_untimeout(), func=sntp_request\r\n"));
iva2k 0:886e4b3119ad 550 } else if (func == &sntp_try_next_server) {
iva2k 0:886e4b3119ad 551 t = &_sntp_timer2;
iva2k 1:ec4170739967 552 LWIP_DEBUGF(SNTP_DEBUG_STATE, ("DEBUG: IN sntp_sys_untimeout(), func=sntp_try_next_server\r\n"));
iva2k 0:886e4b3119ad 553 }
iva2k 0:886e4b3119ad 554 if (t) {
iva2k 0:886e4b3119ad 555 t->detach();
iva2k 0:886e4b3119ad 556 }
iva2k 0:886e4b3119ad 557 }
iva2k 0:886e4b3119ad 558
iva2k 0:886e4b3119ad 559 #else // NO_SYS
iva2k 0:886e4b3119ad 560 #error "I don't know how to compile LWIP/SNTP with NO_SYS=0"
iva2k 0:886e4b3119ad 561 #endif // NO_SYS
iva2k 0:886e4b3119ad 562
iva2k 1:ec4170739967 563 // Can be called when already running
iva2k 1:ec4170739967 564 void SNTPSetDstZone(tDST_ZONE zone) {
iva2k 1:ec4170739967 565 if (zone >= DST_ZONE_DESCR_CNT)
iva2k 1:ec4170739967 566 return; // ERR_INVALID_ARG
iva2k 1:ec4170739967 567
iva2k 1:ec4170739967 568 gSntpDstZone = zone;
iva2k 1:ec4170739967 569
iva2k 1:ec4170739967 570 if (gSntpRunning) {
iva2k 1:ec4170739967 571 // DST scheduler
iva2k 1:ec4170739967 572 _sntp_dst_now();
iva2k 1:ec4170739967 573 _sntp_dst_schedule();
iva2k 1:ec4170739967 574 }
iva2k 1:ec4170739967 575 }
iva2k 1:ec4170739967 576
iva2k 0:886e4b3119ad 577 void SNTPSetRecvTimeout(unsigned int val_ms) { gSntpRecvTimeout_ms = val_ms; }
iva2k 0:886e4b3119ad 578 void SNTPSetUpdateDelay(unsigned int val_ms) { gSntpUpdateDelay_ms = val_ms; }
iva2k 0:886e4b3119ad 579 void SNTPSetTimezone(float hours_from_utc, bool adjust_clock) {
iva2k 0:886e4b3119ad 580 if (adjust_clock && !gSntpRtcUtc) {
iva2k 0:886e4b3119ad 581 time_t seconds = time(NULL);
iva2k 0:886e4b3119ad 582 seconds -= gSntpTimezone; // Convert from old local time
iva2k 0:886e4b3119ad 583 seconds += hours_from_utc * 3600; // Convert to new local time
iva2k 0:886e4b3119ad 584 set_time(seconds);
iva2k 1:ec4170739967 585 if (gSntpRtcTCR) {
iva2k 1:ec4170739967 586 // Adjust our alarm clock
iva2k 1:ec4170739967 587 gSntpRtcTCR -= gSntpTimezone;
iva2k 1:ec4170739967 588 gSntpRtcTCR += hours_from_utc * 3600;
iva2k 1:ec4170739967 589 }
iva2k 0:886e4b3119ad 590 }
iva2k 0:886e4b3119ad 591 gSntpTimezone = hours_from_utc * 3600;
iva2k 0:886e4b3119ad 592 }
iva2k 0:886e4b3119ad 593 void SNTPSetDST(float hours_from_utc, bool adjust_clock) {
iva2k 1:ec4170739967 594 SNTPSetDSTEx(hours_from_utc * 3600, adjust_clock);
iva2k 0:886e4b3119ad 595 }
iva2k 0:886e4b3119ad 596
iva2k 0:886e4b3119ad 597 static int sntp_num_servers_alloc = 0;
iva2k 0:886e4b3119ad 598 static void _SNTPClrAddresses(void) {
iva2k 0:886e4b3119ad 599 if (!sntp_num_servers_alloc) sntp_num_servers_alloc = sntp_num_servers;
iva2k 0:886e4b3119ad 600 // Here we save the original size of the sntp_server_addresses[] array.
iva2k 1:ec4170739967 601
iva2k 0:886e4b3119ad 602 if (sntp_addresses_free) {
iva2k 0:886e4b3119ad 603 for (int i=0; i<sntp_num_servers; i++) {
iva2k 0:886e4b3119ad 604 sntp_addresses_free(sntp_server_addresses[i]);
iva2k 0:886e4b3119ad 605 }
iva2k 0:886e4b3119ad 606 }
iva2k 0:886e4b3119ad 607 sntp_current_server = 0;
iva2k 0:886e4b3119ad 608 sntp_num_servers = 0;
iva2k 0:886e4b3119ad 609 sntp_addresses_free = NULL;
iva2k 0:886e4b3119ad 610 }
iva2k 0:886e4b3119ad 611 static int _SNTPAddAddress(const char* server_address) {
iva2k 0:886e4b3119ad 612 if (sntp_num_servers+1 > sntp_num_servers_alloc)
iva2k 0:886e4b3119ad 613 return -1;
iva2k 0:886e4b3119ad 614 sntp_server_addresses[sntp_num_servers] = (char*)malloc(strlen(server_address)+1);
iva2k 0:886e4b3119ad 615 if (sntp_server_addresses[sntp_num_servers] == NULL) return -1; // Out of memory
iva2k 0:886e4b3119ad 616 strcpy(sntp_server_addresses[sntp_num_servers], server_address);
iva2k 0:886e4b3119ad 617 sntp_num_servers++;
iva2k 0:886e4b3119ad 618 return 0;
iva2k 0:886e4b3119ad 619 }
iva2k 0:886e4b3119ad 620
iva2k 0:886e4b3119ad 621 // Override default servers list.
iva2k 0:886e4b3119ad 622 // For no-copy, pass pointer to free() in p_free.
iva2k 0:886e4b3119ad 623 // Returns: actual number of servers set (limited by allocated buffer size)
iva2k 0:886e4b3119ad 624 // WARNING! There is no interlock to ensure that SNTP service does not read its data while we are updating it.
iva2k 0:886e4b3119ad 625 // This function is intended to be called only before SNTPClientInit().
iva2k 0:886e4b3119ad 626 int SNTPSetAddresses(const char* server_addresses[], int count, void (*p_free)(void*)) {
iva2k 0:886e4b3119ad 627 // In order to use sntp.c as-is, we hack into its static variables.
iva2k 0:886e4b3119ad 628 // Not all compilers/linkers will support that.
iva2k 0:886e4b3119ad 629
iva2k 0:886e4b3119ad 630 _SNTPClrAddresses();
iva2k 0:886e4b3119ad 631 if (count > sntp_num_servers_alloc)
iva2k 0:886e4b3119ad 632 count = sntp_num_servers_alloc;
iva2k 0:886e4b3119ad 633 for (int i=0; i<count; i++) {
iva2k 0:886e4b3119ad 634 if (p_free) {
iva2k 0:886e4b3119ad 635 sntp_server_addresses[i] = (char *)server_addresses[i];
iva2k 0:886e4b3119ad 636 } else {
iva2k 0:886e4b3119ad 637 _SNTPAddAddress(server_addresses[i]);
iva2k 0:886e4b3119ad 638 }
iva2k 0:886e4b3119ad 639 }
iva2k 0:886e4b3119ad 640 sntp_num_servers = count;
iva2k 0:886e4b3119ad 641 sntp_current_server = 0;
iva2k 0:886e4b3119ad 642 sntp_addresses_free = p_free ? p_free : &free;
iva2k 0:886e4b3119ad 643 return count;
iva2k 0:886e4b3119ad 644 }
iva2k 0:886e4b3119ad 645
iva2k 0:886e4b3119ad 646 // Trim whitespace/CRLFs from both ends of a given string
iva2k 0:886e4b3119ad 647 char * str_cleanup(char *in) {
iva2k 0:886e4b3119ad 648 char * out = in;
iva2k 0:886e4b3119ad 649 // Trim leading spaces and CR/LF
iva2k 0:886e4b3119ad 650 while (*out == ' ' || *out == '\t' || *out == '\r' || *out == '\n')
iva2k 0:886e4b3119ad 651 out ++;
iva2k 0:886e4b3119ad 652 // Trim trailing spaces and CR/LF
iva2k 0:886e4b3119ad 653 int len = strlen(out)-1;
iva2k 0:886e4b3119ad 654 while (out[len] == ' ' || out[len] == '\t' || out[len] == '\r' || out[len] == '\n') {
iva2k 0:886e4b3119ad 655 out[len] = '\0';
iva2k 0:886e4b3119ad 656 len--;
iva2k 0:886e4b3119ad 657 }
iva2k 1:ec4170739967 658 return out;
iva2k 0:886e4b3119ad 659 }
iva2k 0:886e4b3119ad 660
iva2k 0:886e4b3119ad 661 #ifndef CRLF
iva2k 0:886e4b3119ad 662 #define CRLF "\r\n"
iva2k 0:886e4b3119ad 663 #endif
iva2k 0:886e4b3119ad 664 void SNTPWriteIniFile(FILE * f) {
iva2k 0:886e4b3119ad 665 fprintf(f, "# SNTP Configuration file" CRLF);
iva2k 0:886e4b3119ad 666 fprintf(f, CRLF "[Servers]" CRLF);
iva2k 0:886e4b3119ad 667 for (int i=0; i<sntp_num_servers; i++) {
iva2k 0:886e4b3119ad 668 fprintf(f, "%s" CRLF, sntp_server_addresses[i]);
iva2k 0:886e4b3119ad 669 }
iva2k 0:886e4b3119ad 670 fprintf(f, CRLF "[Global]" CRLF);
iva2k 0:886e4b3119ad 671 fprintf(f, "RtcUtc=%d" CRLF, (int)gSntpRtcUtc);
iva2k 0:886e4b3119ad 672 fprintf(f, "Timezone=%0.1f" CRLF, gSntpTimezone / 3600.0);
iva2k 1:ec4170739967 673 fprintf(f, "DstZone=%d" CRLF, gSntpDstZone);
iva2k 1:ec4170739967 674 fprintf(f, "# %s" CRLF, SNTPDstZoneName(gSntpDstZone));
iva2k 0:886e4b3119ad 675 fprintf(f, "UpdateDelay=%d" CRLF, gSntpUpdateDelay_ms);
iva2k 0:886e4b3119ad 676 fprintf(f, "RecvTimeout=%d" CRLF, gSntpRecvTimeout_ms);
iva2k 0:886e4b3119ad 677 fprintf(f, CRLF "##END" CRLF);
iva2k 0:886e4b3119ad 678 }
iva2k 0:886e4b3119ad 679
iva2k 0:886e4b3119ad 680 // Simple ini file parser for SNTP configuration (Case-sensitive!)
iva2k 0:886e4b3119ad 681 // This function is intended to be called only before SNTPClientInit().
iva2k 0:886e4b3119ad 682 // Returns: 0 if OK, errno otherwise
iva2k 0:886e4b3119ad 683 enum {
iva2k 0:886e4b3119ad 684 SECT_NONE,
iva2k 0:886e4b3119ad 685 SECT_SERVERS,
iva2k 0:886e4b3119ad 686 SECT_GLOBAL,
iva2k 0:886e4b3119ad 687 };
iva2k 0:886e4b3119ad 688 int SNTPReadIniFile(const char* filename) {
iva2k 0:886e4b3119ad 689 FILE *f;
iva2k 0:886e4b3119ad 690 char buf[512];
iva2k 0:886e4b3119ad 691 bool addresses_cleared = false;
iva2k 0:886e4b3119ad 692
iva2k 0:886e4b3119ad 693 f = fopen(filename, "r");
iva2k 0:886e4b3119ad 694 if (!f)
iva2k 0:886e4b3119ad 695 return -1; // errno not used?
iva2k 0:886e4b3119ad 696
iva2k 0:886e4b3119ad 697 char *buf1, *buf2;
iva2k 0:886e4b3119ad 698 int section=SECT_NONE;
iva2k 0:886e4b3119ad 699 int line = 0;
iva2k 0:886e4b3119ad 700 while (fgets(buf, sizeof(buf)/sizeof(buf[0]), f)) {
iva2k 0:886e4b3119ad 701 line++;
iva2k 0:886e4b3119ad 702 buf1 = str_cleanup(buf);
iva2k 0:886e4b3119ad 703 if (*buf1 == '#' || *buf1 == '\0')
iva2k 0:886e4b3119ad 704 continue; // Comment line or empty line - skip
iva2k 0:886e4b3119ad 705 if (*buf1 == '[') {
iva2k 0:886e4b3119ad 706 // New section
iva2k 0:886e4b3119ad 707 if (0 == strncmp(buf1,"[Servers]", sizeof("[Servers]")-1)) {
iva2k 0:886e4b3119ad 708 section=SECT_SERVERS;
iva2k 0:886e4b3119ad 709 if (!addresses_cleared) {
iva2k 0:886e4b3119ad 710 // Clear addresses only once.
iva2k 1:ec4170739967 711 _SNTPClrAddresses();
iva2k 0:886e4b3119ad 712 addresses_cleared = true;
iva2k 0:886e4b3119ad 713 }
iva2k 0:886e4b3119ad 714 } else if (0 == strncmp(buf1,"[Global]", sizeof("[Global]")-1)) {
iva2k 0:886e4b3119ad 715 section=SECT_GLOBAL;
iva2k 0:886e4b3119ad 716 } else {
iva2k 0:886e4b3119ad 717 section=SECT_NONE;
iva2k 0:886e4b3119ad 718 fprintf(stderr, "File \"%s\", line %d - section \"%s\" is not understood.\r\n", filename, line, buf1);
iva2k 0:886e4b3119ad 719 }
iva2k 0:886e4b3119ad 720 } else {
iva2k 0:886e4b3119ad 721 // Section values
iva2k 0:886e4b3119ad 722 switch (section) {
iva2k 0:886e4b3119ad 723 case SECT_SERVERS:
iva2k 0:886e4b3119ad 724 if (_SNTPAddAddress(buf1)) {
iva2k 0:886e4b3119ad 725 fprintf(stderr, "File \"%s\", line %d - cannot add server \"%s\" - exceeded allocated slots.\r\n", filename, line, buf1);
iva2k 0:886e4b3119ad 726 }
iva2k 0:886e4b3119ad 727 break;
iva2k 0:886e4b3119ad 728 case SECT_GLOBAL:
iva2k 0:886e4b3119ad 729 buf2 = strchr(buf1, '=');
iva2k 0:886e4b3119ad 730 if (buf2) {
iva2k 0:886e4b3119ad 731 *buf2++ = '\0'; // Now buf1 has variable name, buf2 has value
iva2k 0:886e4b3119ad 732 buf2 = str_cleanup(buf2);
iva2k 0:886e4b3119ad 733 if (0 == strncmp(buf1, "Timezone", sizeof("Timezone")-1)) {
iva2k 0:886e4b3119ad 734 gSntpTimezone = strtod(buf2, &buf2) * 3600;
iva2k 0:886e4b3119ad 735 } else if (0 == strncmp(buf1, "UpdateDelay", sizeof("UpdateDelay")-1)) {
iva2k 0:886e4b3119ad 736 gSntpUpdateDelay_ms = strtoul(buf2, &buf2, 10);
iva2k 0:886e4b3119ad 737 } else if (0 == strncmp(buf1, "RecvTimeout", sizeof("RecvTimeout")-1)) {
iva2k 0:886e4b3119ad 738 gSntpRecvTimeout_ms = strtoul(buf2, &buf2, 10);
iva2k 0:886e4b3119ad 739 } else if (0 == strncmp(buf1, "RtcUtc", sizeof("RtcUtc")-1)) {
iva2k 0:886e4b3119ad 740 gSntpRtcUtc = (bool)strtol(buf2, &buf2, 10);
iva2k 1:ec4170739967 741 } else if (0 == strncmp(buf1, "DstZone", sizeof("DstZone")-1)) {
iva2k 1:ec4170739967 742 // FIXME: It would be nice to allow human-readable string here.
iva2k 1:ec4170739967 743 gSntpDstZone = (tDST_ZONE)strtol(buf2, &buf2, 10);
iva2k 0:886e4b3119ad 744 } else {
iva2k 1:ec4170739967 745 fprintf(stderr, "File \"%s\", line %d - unrecognized variable \"%s\" in section [Global]\r\n", filename, line, buf1);
iva2k 0:886e4b3119ad 746 }
iva2k 0:886e4b3119ad 747 } else {
iva2k 0:886e4b3119ad 748 fprintf(stderr, "File \"%s\", line %d - unrecognized statement in section [Global]: %s\r\n", filename, line, buf1);
iva2k 0:886e4b3119ad 749 }
iva2k 0:886e4b3119ad 750 break;
iva2k 0:886e4b3119ad 751 default:
iva2k 0:886e4b3119ad 752 fprintf(stderr, "File \"%s\", line %d - unrecognized statement / no section: %s\r\n", filename, line, buf1);
iva2k 0:886e4b3119ad 753 }
iva2k 0:886e4b3119ad 754 }
iva2k 0:886e4b3119ad 755 }
iva2k 0:886e4b3119ad 756 fclose(f);
iva2k 0:886e4b3119ad 757 printf("SNTP configuration read from file \"%s\", %d lines.\r\n", filename, line);
iva2k 0:886e4b3119ad 758 return 0;
iva2k 0:886e4b3119ad 759 }
iva2k 0:886e4b3119ad 760
iva2k 0:886e4b3119ad 761 // Start the SNTP client
iva2k 0:886e4b3119ad 762 void SNTPClientInit(void) {
iva2k 1:ec4170739967 763
iva2k 1:ec4170739967 764 #ifdef SNTP_DST_TESTS
iva2k 1:ec4170739967 765 // Test our DST algorithms
iva2k 1:ec4170739967 766 test_sntp_dst_calc(gSntpTimezone, gSntpDstZone);
iva2k 1:ec4170739967 767 test_sntp_dst_point(-2);
iva2k 1:ec4170739967 768 test_sntp_dst_point(-1);
iva2k 1:ec4170739967 769 test_sntp_dst_point(0);
iva2k 1:ec4170739967 770 test_sntp_dst_point(1);
iva2k 1:ec4170739967 771 test_sntp_dst_point(2);
iva2k 1:ec4170739967 772 #endif
iva2k 1:ec4170739967 773
iva2k 0:886e4b3119ad 774 // SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds
iva2k 0:886e4b3119ad 775 if (gSntpUpdateDelay_ms < 15000) {
iva2k 0:886e4b3119ad 776 gSntpUpdateDelay_ms = 15000;
iva2k 0:886e4b3119ad 777 }
iva2k 1:ec4170739967 778
iva2k 1:ec4170739967 779 gSntpRunning = true;
iva2k 0:886e4b3119ad 780
iva2k 0:886e4b3119ad 781 // Just call this to start SNTP client:
iva2k 0:886e4b3119ad 782 sntp_init();
iva2k 1:ec4170739967 783
iva2k 1:ec4170739967 784 // // Enable RTC ISR
iva2k 1:ec4170739967 785 // gSntpTicker.attach(_sntp_isr, 1);
iva2k 1:ec4170739967 786 // We do it from first NTP response
iva2k 0:886e4b3119ad 787 }
iva2k 0:886e4b3119ad 788
iva2k 0:886e4b3119ad 789 // Use instead of system time()
iva2k 0:886e4b3119ad 790 // Returns local time
iva2k 0:886e4b3119ad 791 time_t SNTPTime(void) {
iva2k 0:886e4b3119ad 792 time_t seconds = time(NULL);
iva2k 0:886e4b3119ad 793 if (gSntpRtcUtc) {
iva2k 0:886e4b3119ad 794 seconds += gSntpTimezone + gSntpDST; // Convert to local time
iva2k 0:886e4b3119ad 795 }
iva2k 0:886e4b3119ad 796 return seconds;
iva2k 0:886e4b3119ad 797 }
iva2k 0:886e4b3119ad 798
iva2k 0:886e4b3119ad 799 // Use instead of system set_time()
iva2k 0:886e4b3119ad 800 // seconds - local time
iva2k 0:886e4b3119ad 801 void SNTPSetTime(time_t seconds) {
iva2k 0:886e4b3119ad 802 if (gSntpRtcUtc) {
iva2k 0:886e4b3119ad 803 seconds -= gSntpTimezone + gSntpDST; // Convert from local time
iva2k 0:886e4b3119ad 804 }
iva2k 0:886e4b3119ad 805 set_time(seconds);
iva2k 0:886e4b3119ad 806 }
iva2k 0:886e4b3119ad 807
iva2k 0:886e4b3119ad 808 // Use instead of system time()
iva2k 0:886e4b3119ad 809 // Returns UTC time
iva2k 0:886e4b3119ad 810 time_t SNTPTimeUTC(void) {
iva2k 0:886e4b3119ad 811 time_t seconds = time(NULL);
iva2k 0:886e4b3119ad 812 if (!gSntpRtcUtc) {
iva2k 0:886e4b3119ad 813 seconds -= gSntpTimezone + gSntpDST; // Convert from local time
iva2k 0:886e4b3119ad 814 }
iva2k 0:886e4b3119ad 815 return seconds;
iva2k 0:886e4b3119ad 816 }
iva2k 0:886e4b3119ad 817
iva2k 0:886e4b3119ad 818 // Use instead of system set_time()
iva2k 0:886e4b3119ad 819 // seconds - UTC time
iva2k 0:886e4b3119ad 820 void SNTPSetTimeUTC(time_t seconds) {
iva2k 0:886e4b3119ad 821 if (!gSntpRtcUtc) {
iva2k 0:886e4b3119ad 822 seconds += gSntpTimezone + gSntpDST; // Convert to local time
iva2k 0:886e4b3119ad 823 }
iva2k 0:886e4b3119ad 824 set_time(seconds);
iva2k 0:886e4b3119ad 825 }
iva2k 0:886e4b3119ad 826
iva2k 0:886e4b3119ad 827 //END