ntp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <dev/nvmem.h>
  7. #include <sys/timer.h>
  8. #include <arpa/inet.h>
  9. #include <net/route.h>
  10. #include <pro/dhcp.h>
  11. #include <pro/sntp.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <io.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include "log.h"
  18. #include "ntp.h"
  19. #include "eeprom.h"
  20. #include "typedefs.h"
  21. int TIME_ZONE = 1;
  22. #define LOG_MODULE LOG_NTP_MODULE
  23. bool isSyncing;
  24. bool validTime = false;
  25. time_t ntp_time = 0;
  26. tm *ntp_datetime;
  27. uint32_t timeserver = 0;
  28. void NtpInit(void) {
  29. puts("Func: NtpInit(void)");
  30. /* Timezone van nederland (gmt 1) */
  31. _timezone = -TIME_ZONE * 3600;
  32. NtpCheckValidTime();
  33. }
  34. bool NtpIsSyncing(void){
  35. return isSyncing;
  36. }
  37. void NtpCheckValidTime(void){
  38. TCache *cache;
  39. if (EepromGetCache(cache) == false){
  40. puts("NtpCheckValidTime(): No cache available");
  41. validTime = false;
  42. return;
  43. }
  44. // Cache is present
  45. puts("NtpCheckValidTime(): Cache is available");
  46. // Check time is valid;
  47. tm current_tm;
  48. X12RtcGetClock(&current_tm);
  49. validTime = NtpCompareTime(current_tm, stored_tm);
  50. if (validTime){
  51. puts("NtpCheckValidTime(): Time was valid \n");
  52. }else {
  53. puts("NtpCheckValidTime(): Invalid time! \n");
  54. }
  55. // Eeprom_tm eeprom_tm_struct;
  56. //
  57. // NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
  58. //
  59. // if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
  60. // // Size mismatch: There is no valid configuration present.
  61. // puts("NtpCheckValidTime(): Size mismatch");
  62. // validTime = false;
  63. // return;
  64. // }
  65. //
  66. // // Valid configuration available.
  67. // puts("NtpCheckValidTime(): Valid config available");
  68. // tm stored_tm = eeprom_tm_struct.tm_struct;
  69. //
  70. // // Check time is valid;
  71. // tm current_tm;
  72. // X12RtcGetClock(&current_tm);
  73. //
  74. // validTime = NtpCompareTime(current_tm, stored_tm);
  75. // if (validTime){
  76. // puts("NtpCheckValidTime(): Time was valid");
  77. // }else {
  78. // puts("NtpCheckValidTime(): Invalid time!");
  79. // }
  80. }
  81. //Tests if t1 is after t2.
  82. bool NtpCompareTime(tm t1, tm t2){
  83. char debug[120];
  84. sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d \n",
  85. t1.tm_year+1900,
  86. t1.tm_mon+1,
  87. t1.tm_mday,
  88. t1.tm_hour,
  89. t1.tm_min,
  90. t1.tm_sec,
  91. t2.tm_year+1900,
  92. t2.tm_mon+1,
  93. t2.tm_mday,
  94. t2.tm_hour,
  95. t2.tm_min,
  96. t2.tm_sec
  97. );
  98. puts(debug);
  99. if (t1.tm_year > t2.tm_year)
  100. return true;
  101. if (t1.tm_mon > t2.tm_mon)
  102. return true;
  103. if (t1.tm_mday > t2.tm_mday)
  104. return true;
  105. if (t1.tm_hour > t2.tm_hour)
  106. return true;
  107. if (t1.tm_min > t2.tm_min)
  108. return true;
  109. if (t1.tm_sec > t2.tm_sec)
  110. return true;
  111. //else
  112. return false;
  113. }
  114. bool NtpTimeIsValid(void){
  115. return validTime;
  116. }
  117. void NtpSync(void){
  118. /* Ophalen van pool.ntp.org */
  119. isSyncing = true;
  120. _timezone = -getTimeZone() * 3600;
  121. puts("NtpSync(): Timezone fetched. ");
  122. printf(TIME_ZONE);
  123. NutDelay(100);
  124. //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  125. timeserver = inet_addr("213.154.229.24");
  126. for (;;) {
  127. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  128. break;
  129. } else {
  130. NutSleep(400);
  131. puts("Fout bij het ontvangen van de tijd");
  132. }
  133. }
  134. //puts("Opgehaald.\n");
  135. ntp_datetime = localtime(&ntp_time);
  136. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  137. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  138. X12RtcSetClock(ntp_datetime);
  139. NtpWriteTimeToEeprom(*ntp_datetime);
  140. isSyncing = false;
  141. validTime = true;
  142. }
  143. void NtpWriteTimeToEeprom(tm time_struct){
  144. TCache cache;
  145. cache.last_sync = time_struct;
  146. EepromSetCache(&cache);
  147. }