ntp.c 3.2 KB

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