ntp.c 3.4 KB

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