ntp.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void NtpSync(void){
  68. /* Ophalen van pool.ntp.org */
  69. isSyncing = true;
  70. httpGet("/gettimezone.php", parsetimezone);
  71. _daylight = 0;
  72. printf("Timezone is: %d", TIME_ZONE);
  73. NutDelay(100);
  74. //puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  75. timeserver = inet_addr("213.154.229.24");
  76. for (;;) {
  77. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  78. break;
  79. } else {
  80. NutSleep(400);
  81. puts("Fout bij het ontvangen van de tijd");
  82. }
  83. }
  84. //puts("Opgehaald.\n");
  85. ntp_datetime = localtime(&ntp_time);
  86. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  87. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  88. X12RtcSetClock(ntp_datetime);
  89. NtpWriteTimeToEeprom(*ntp_datetime);
  90. isSyncing = false;
  91. validTime = true;
  92. }
  93. void NtpWriteTimeToEeprom(tm time_struct){
  94. Eeprom_tm eeprom_tm_struct;
  95. eeprom_tm_struct.len = sizeof(eeprom_tm_struct);
  96. eeprom_tm_struct.tm_struct = time_struct;
  97. int success = NutNvMemSave(256, &eeprom_tm_struct, sizeof(eeprom_tm_struct));
  98. if (success == 0){ puts("NtpWriteTimeToEeprom: Time succesfully written to eeprom \n"); }
  99. NutDelay(100);
  100. }
  101. void setTimeZone(int timezone){
  102. TIME_ZONE = timezone;
  103. _timezone = -1*timezone * 3600;
  104. }
  105. int getTimeZone(){
  106. return TIME_ZONE;
  107. }