|
|
@@ -3,6 +3,7 @@
|
|
|
//
|
|
|
#include <dev/board.h>
|
|
|
#include <dev/debug.h>
|
|
|
+#include <dev/nvmem.h>
|
|
|
|
|
|
#include <sys/timer.h>
|
|
|
|
|
|
@@ -23,19 +24,102 @@
|
|
|
#define TIME_ZONE 1
|
|
|
#define LOG_MODULE LOG_NTP_MODULE
|
|
|
|
|
|
+typedef struct _Eeprom_tm {
|
|
|
+ size_t len;
|
|
|
+ tm tm_struct;
|
|
|
+} Eeprom_tm;
|
|
|
+
|
|
|
+bool isSyncing;
|
|
|
+bool validTime = false;
|
|
|
time_t ntp_time = 0;
|
|
|
tm *ntp_datetime;
|
|
|
uint32_t timeserver = 0;
|
|
|
|
|
|
-void NtpInit() {
|
|
|
+void NtpInit(void) {
|
|
|
+ puts("Func: NtpInit(void)");
|
|
|
/* Timezone van nederland (gmt 1) */
|
|
|
_timezone = -TIME_ZONE * 3600;
|
|
|
- GetTime();
|
|
|
+ NtpCheckValidTime();
|
|
|
+}
|
|
|
+
|
|
|
+bool NtpIsSyncing(void){
|
|
|
+ return isSyncing;
|
|
|
+}
|
|
|
+
|
|
|
+void NtpCheckValidTime(void){
|
|
|
+
|
|
|
+ Eeprom_tm eeprom_tm_struct;
|
|
|
+
|
|
|
+ NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
|
|
|
+
|
|
|
+ if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
|
|
|
+ // Size mismatch: There is no valid configuration present.
|
|
|
+ puts("NtpCheckValidTime(): Size mismatch");
|
|
|
+ validTime = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Valid configuration available.
|
|
|
+ puts("NtpCheckValidTime(): Valid config available");
|
|
|
+ tm stored_tm = eeprom_tm_struct.tm_struct;
|
|
|
+
|
|
|
+ // Check time is valid;
|
|
|
+ tm current_tm;
|
|
|
+ X12RtcGetClock(¤t_tm);
|
|
|
+
|
|
|
+ validTime = NtpCompareTime(current_tm, stored_tm);
|
|
|
+ if (validTime){
|
|
|
+ puts("NtpCheckValidTime(): Time was valid");
|
|
|
+ }else {
|
|
|
+ puts("NtpCheckValidTime(): Invalid time!");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//Tests if t1 is after t2.
|
|
|
+bool NtpCompareTime(tm t1, tm t2){
|
|
|
+ char debug[120];
|
|
|
+ sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d",
|
|
|
+ t1.tm_year+1900,
|
|
|
+ t1.tm_mon+1,
|
|
|
+ t1.tm_mday,
|
|
|
+ t1.tm_hour,
|
|
|
+ t1.tm_min,
|
|
|
+ t1.tm_sec,
|
|
|
+
|
|
|
+ t2.tm_year+1900,
|
|
|
+ t2.tm_mon+1,
|
|
|
+ t2.tm_mday,
|
|
|
+ t2.tm_hour,
|
|
|
+ t2.tm_min,
|
|
|
+ t2.tm_sec
|
|
|
+ );
|
|
|
+ puts(debug);
|
|
|
+
|
|
|
+ if (t1.tm_year > t2.tm_year)
|
|
|
+ return true;
|
|
|
+ if (t1.tm_mon > t2.tm_mon)
|
|
|
+ return true;
|
|
|
+ if (t1.tm_mday > t2.tm_mday)
|
|
|
+ return true;
|
|
|
+ if (t1.tm_hour > t2.tm_hour)
|
|
|
+ return true;
|
|
|
+ if (t1.tm_min > t2.tm_min)
|
|
|
+ return true;
|
|
|
+ if (t1.tm_sec > t2.tm_sec)
|
|
|
+ return true;
|
|
|
+
|
|
|
+ //else
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+bool NtpTimeIsValid(void){
|
|
|
+ return validTime;
|
|
|
}
|
|
|
|
|
|
-void GetTime(){
|
|
|
+void NtpSync(void){
|
|
|
/* Ophalen van pool.ntp.org */
|
|
|
- puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
|
|
|
+ isSyncing = true;
|
|
|
+ //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
|
|
|
timeserver = inet_addr("213.154.229.24");
|
|
|
|
|
|
for (;;) {
|
|
|
@@ -46,7 +130,7 @@ void GetTime(){
|
|
|
puts("Fout bij het ontvangen van de tijd");
|
|
|
}
|
|
|
}
|
|
|
- puts("Opgehaald.\n");
|
|
|
+ //puts("Opgehaald.\n");
|
|
|
|
|
|
ntp_datetime = localtime(&ntp_time);
|
|
|
|
|
|
@@ -54,14 +138,24 @@ void GetTime(){
|
|
|
printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
|
|
|
|
|
|
X12RtcSetClock(ntp_datetime);
|
|
|
+ NtpWriteTimeToEeprom(*ntp_datetime);
|
|
|
|
|
|
+ isSyncing = false;
|
|
|
+ validTime = true;
|
|
|
+}
|
|
|
|
|
|
- NutDelay(100);
|
|
|
+void NtpWriteTimeToEeprom(tm time_struct){
|
|
|
+ Eeprom_tm eeprom_tm_struct;
|
|
|
+
|
|
|
+ eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
|
|
|
+ eeprom_tm_struct.tm_struct = time_struct;
|
|
|
|
|
|
+ int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
|
|
|
+ if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom"); }
|
|
|
|
|
|
- // for logging purposes
|
|
|
- tm gmt;
|
|
|
- X12RtcGetClock(&gmt);
|
|
|
- LogMsg_P(LOG_INFO, PSTR("RTC time [%02d:%02d:%02d]"), gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
|
|
|
+ NutDelay(100);
|
|
|
}
|
|
|
|
|
|
+//unsigned long TmStructToEpoch(tm tm_struct){
|
|
|
+//
|
|
|
+//}
|