ntp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #define TIME_ZONE 1
  20. #define LOG_MODULE LOG_NTP_MODULE
  21. typedef struct _Eeprom_tm {
  22. size_t len;
  23. tm tm_struct;
  24. } Eeprom_tm;
  25. bool isSyncing;
  26. bool validTime = false;
  27. time_t ntp_time = 0;
  28. tm *ntp_datetime;
  29. uint32_t timeserver = 0;
  30. void NtpInit(void) {
  31. puts("Func: NtpInit(void)");
  32. /* Timezone van nederland (gmt 1) */
  33. _timezone = -TIME_ZONE * 3600;
  34. NtpCheckValidTime();
  35. }
  36. bool NtpIsSyncing(void){
  37. return isSyncing;
  38. }
  39. void NtpCheckValidTime(void){
  40. Eeprom_tm eeprom_tm_struct;
  41. NutNvMemLoad(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
  42. if (eeprom_tm_struct.len != sizeof(eeprom_tm_struct)){
  43. // Size mismatch: There is no valid configuration present.
  44. puts("NtpCheckValidTime(): Size mismatch");
  45. validTime = false;
  46. return;
  47. }
  48. // Valid configuration available.
  49. puts("NtpCheckValidTime(): Valid config available");
  50. tm stored_tm = eeprom_tm_struct.tm_struct;
  51. // Check time is valid;
  52. tm current_tm;
  53. X12RtcGetClock(&current_tm);
  54. validTime = NtpCompareTime(current_tm, stored_tm);
  55. if (validTime){
  56. puts("NtpCheckValidTime(): Time was valid");
  57. }else {
  58. puts("NtpCheckValidTime(): Invalid time!");
  59. }
  60. }
  61. //Tests if t1 is after t2.
  62. bool NtpCompareTime(tm t1, tm t2){
  63. char debug[120];
  64. sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d",
  65. t1.tm_year+1900,
  66. t1.tm_mon+1,
  67. t1.tm_mday,
  68. t1.tm_hour,
  69. t1.tm_min,
  70. t1.tm_sec,
  71. t2.tm_year+1900,
  72. t2.tm_mon+1,
  73. t2.tm_mday,
  74. t2.tm_hour,
  75. t2.tm_min,
  76. t2.tm_sec
  77. );
  78. puts(debug);
  79. if (t1.tm_year > t2.tm_year)
  80. return true;
  81. if (t1.tm_mon > t2.tm_mon)
  82. return true;
  83. if (t1.tm_mday > t2.tm_mday)
  84. return true;
  85. if (t1.tm_hour > t2.tm_hour)
  86. return true;
  87. if (t1.tm_min > t2.tm_min)
  88. return true;
  89. if (t1.tm_sec > t2.tm_sec)
  90. return true;
  91. //else
  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. //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  101. timeserver = inet_addr("213.154.229.24");
  102. for (;;) {
  103. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  104. break;
  105. } else {
  106. NutSleep(400);
  107. puts("Fout bij het ontvangen van de tijd");
  108. }
  109. }
  110. //puts("Opgehaald.\n");
  111. ntp_datetime = localtime(&ntp_time);
  112. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  113. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  114. X12RtcSetClock(ntp_datetime);
  115. NtpWriteTimeToEeprom(*ntp_datetime);
  116. isSyncing = false;
  117. validTime = true;
  118. }
  119. void NtpWriteTimeToEeprom(tm time_struct){
  120. Eeprom_tm eeprom_tm_struct;
  121. eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
  122. eeprom_tm_struct.tm_struct = time_struct;
  123. int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
  124. if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom"); }
  125. NutDelay(100);
  126. }
  127. //unsigned long TmStructToEpoch(tm tm_struct){
  128. //
  129. //}