ntp.c 3.1 KB

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