A time interface class. This class replicates the normal time functions, but goes a couple of steps further. mbed library 82 and prior has a defective gmtime function. Also, this class enables access to setting the time, and adjusting the accuracy of the RTC.

Dependencies:   CalendarPage

Dependents:   CI-data-logger-server WattEye X10Svr SSDP_Server

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sun Jun 22 21:00:01 2014 +0000
Parent:
1:2ee90f546f54
Child:
3:49f36b489b64
Commit message:
Revised the TimeInterface class to use NTP as is, not to modify it.

Changed in this revision

TimeInterface.cpp Show annotated file Show diff for this revision Revisions of this file
TimeInterface.h Show annotated file Show diff for this revision Revisions of this file
--- a/TimeInterface.cpp	Sat Jun 14 12:34:53 2014 +0000
+++ b/TimeInterface.cpp	Sun Jun 22 21:00:01 2014 +0000
@@ -3,6 +3,20 @@
 
 #include "rtc_api.h"
 
+#define DEBUG "Time"
+#include <cstdio>
+#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
+#define DBG(x, ...)  std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...)  std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#define INFO(x, ...)
+#endif
+
 #ifdef WIN32
 // Fake it out for Win32 development and testing
 struct LPC {
@@ -25,6 +39,24 @@
 {
 }
 
+NTPResult TimeInterface::setTime(const char* host, uint16_t port, uint32_t timeout)
+{
+    NTPClient ntp;
+    NTPResult res;
+    int16_t tzomin = get_tzo_min();
+    INFO("setTime(%s, %d, %d) %d\r\n", host, port, timeout, tzomin);
+    res = ntp.setTime(host, port, timeout);
+    INFO("  ret: %d\r\n", res);
+    if (res == NTP_OK) {
+        // if the time was fetched successfully, then 
+        // let's save the time last set with the local tzo applied
+        // and this saves the last time set for later precision
+        // tuning.
+        set_time(std::time(NULL));
+    }
+    return res;
+}
+
 clock_t TimeInterface::clock(void)
 {
     return std::clock();
@@ -55,7 +87,7 @@
     return result;
 }
 
-char * TimeInterface::asctime(const struct tm_ex *timeptr)
+char * TimeInterface::asctime(const struct tm_ex * timeptr)
 {
     static const char wday_name[][4] = {
         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
@@ -130,10 +162,11 @@
 
 void TimeInterface::set_time(time_t t, int16_t tzo_min)
 {
-    time_t tval = t - tzo_min;
+    time_t tval = t - (tzo_min * 60);
     rtc_init();
     rtc_write(tval);
     LPC_RTC->GPREG1 = tval;
+    INFO("set_time(%s)", ctime(&tval));
 }
 
 void TimeInterface::set_tzo_min(int16_t tzo_min)
--- a/TimeInterface.h	Sat Jun 14 12:34:53 2014 +0000
+++ b/TimeInterface.h	Sun Jun 22 21:00:01 2014 +0000
@@ -3,6 +3,8 @@
 #define TIMEINTERFACE_H
 #include "mbed.h"
 
+#include "NTPClient.h"  // ver 4 Donatien Garnier
+
 // Special Registers and their usage:
 // GPREG0: 32 bits
 //      low word: time zone offset (-720 to +720)
@@ -180,9 +182,9 @@
     /// should be the UTC time, which then permits gmtime and 
     /// localtime to be used appropriately.
     ///
-    /// @param[in] t is the UTC time value to set the clock to. If the available 
+    /// @param[in] t should be the UTC time value to set the clock to. If the available 
     ///     time value is local time, the optional time zone offset can
-    ///     be provided.
+    ///     be provided so the system clock is UTC.
     /// @param[in] tzo is the optional time zone offset in minutes when it is in
     ///     the range of -720 to +720 (-12 hours to + 12 hours). Any
     ///     other value is illegal and no change will be made.
@@ -261,9 +263,19 @@
     ///
     bool adjust_sec(int32_t adjustSeconds);
 
-    
-    // ntp interface functions
-    
+    /// Set the clock from an internet source (blocking)
+    ///
+    /// This function is the interface to NTPClient.
+    /// Blocks until completion
+    ///
+    /// @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
+    /// @param[in] port port to use; defaults to 123
+    /// @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
+    /// @return 0 on success, NTP error code (<0) on failure
+    ///
+    NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT);
+
+    // ntp interface functions    
 private:
     char result[30];    // holds the converted to text time string
     time_t tresult;     // holds the converted time structure.