ntp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #define LOG_MODULE LOG_NTP_MODULE
  22. bool isSyncing = false;
  23. bool validTime = false;
  24. time_t ntp_time = 0;
  25. tm *ntp_datetime;
  26. int TIME_ZONE = 1;
  27. void NtpInit(void) {
  28. puts("Func: NtpInit(void)");
  29. /* Timezone van nederland (gmt 1) */
  30. _timezone = -TIME_ZONE * 3600;
  31. NtpCheckValidTime();
  32. }
  33. bool NtpIsSyncing(void){
  34. return isSyncing;
  35. }
  36. void NtpCheckValidTime(void){
  37. TCache *cache;
  38. if (EepromGetCache(cache) == false){
  39. puts("NtpCheckValidTime(): No cache available");
  40. validTime = false;
  41. return;
  42. }
  43. // Cache is present
  44. puts("NtpCheckValidTime(): Cache is available");
  45. // Check time is valid;
  46. tm current_tm;
  47. X12RtcGetClock(&current_tm);
  48. tm stored_tm = cache->last_sync;
  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. }
  56. //Tests if t1 is after t2.
  57. bool NtpCompareTime(tm t1, tm t2){
  58. char debug[120];
  59. sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d \n",
  60. t1.tm_year+1900,
  61. t1.tm_mon+1,
  62. t1.tm_mday,
  63. t1.tm_hour,
  64. t1.tm_min,
  65. t1.tm_sec,
  66. t2.tm_year+1900,
  67. t2.tm_mon+1,
  68. t2.tm_mday,
  69. t2.tm_hour,
  70. t2.tm_min,
  71. t2.tm_sec
  72. );
  73. puts(debug);
  74. if (t1.tm_year > t2.tm_year){
  75. return true;
  76. }
  77. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  78. return true;
  79. }
  80. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  81. return true;
  82. }
  83. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
  84. return true;
  85. }
  86. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
  87. return true;
  88. }
  89. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool NtpTimeIsValid(void){
  95. return validTime;
  96. }
  97. void NtpSync(void){
  98. /* Ophalen van pool.ntp.org */
  99. isSyncing = true;
  100. _timezone = -getTimeZone() * 3600;
  101. puts("NtpSync(): Timezone fetched. ");
  102. printf(TIME_ZONE);
  103. NutDelay(100);
  104. //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  105. uint32_t timeserver = inet_addr("213.154.229.24");
  106. for (;;) {
  107. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  108. break;
  109. } else {
  110. NutSleep(400);
  111. puts("Fout bij het ontvangen van de tijd");
  112. }
  113. }
  114. //puts("Opgehaald.\n");
  115. ntp_datetime = localtime(&ntp_time);
  116. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  117. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  118. X12RtcSetClock(ntp_datetime);
  119. NtpWriteTimeToEeprom(*ntp_datetime);
  120. isSyncing = false;
  121. validTime = true;
  122. }
  123. void NtpWriteTimeToEeprom(tm time_struct){
  124. TCache cache;
  125. cache.last_sync = time_struct;
  126. EepromSetCache(&cache);
  127. }