ntp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 20152016-TI2.3A6, 2016.
  3. *
  4. * Project : 20152016-TI2.3a6-Internet Radio
  5. * Module : NTP
  6. * File name : ntp.c
  7. * Revision : 1.1
  8. * Creation Date : 2016
  9. *
  10. * Description : This module syncs the time from a network time
  11. * server using the NTP-protocol.
  12. */
  13. #include <dev/board.h>
  14. #include <dev/debug.h>
  15. #include <dev/nvmem.h>
  16. #include <sys/timer.h>
  17. #include <arpa/inet.h>
  18. #include <net/route.h>
  19. #include <pro/dhcp.h>
  20. #include <pro/sntp.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <io.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include "eeprom.h"
  27. #include "log.h"
  28. #include "ntp.h"
  29. #include "typedefs.h"
  30. #define LOG_MODULE LOG_NTP_MODULE
  31. extern bool isSyncing = false;
  32. extern bool validTime = false;
  33. time_t ntp_time = 0;
  34. tm *ntp_datetime;
  35. int TIME_ZONE = 1;
  36. void NtpInit(void) {
  37. puts("Func: NtpInit(void)");
  38. /* Timezone van nederland (gmt 1) */
  39. _timezone = -TIME_ZONE * 3600;
  40. NtpCheckValidTime();
  41. }
  42. bool NtpIsSyncing(void){
  43. return isSyncing;
  44. }
  45. void NtpCheckValidTime(void){
  46. puts("Func: NtpCheckValidTime(void)");
  47. TCache *cache;
  48. if (EepromGetCache(cache) == false){
  49. puts("NtpCheckValidTime(): No cache available");
  50. validTime = false;
  51. return;
  52. }
  53. // Cache is present
  54. puts("NtpCheckValidTime(): Cache is available");
  55. // Check time is valid;
  56. tm current_tm;
  57. X12RtcGetClock(&current_tm);
  58. tm stored_tm = cache->last_sync;
  59. validTime = NtpCompareTime(current_tm, stored_tm);
  60. if (validTime){
  61. puts("NtpCheckValidTime(): Time was valid \n");
  62. }else {
  63. puts("NtpCheckValidTime(): Invalid time! \n");
  64. }
  65. }
  66. //Tests if t1 is after t2.
  67. bool NtpCompareTime(tm t1, tm t2){
  68. char debug[120];
  69. sprintf(&debug, "Comparing two times\nt1=%04d-%02d-%02d+%02d:%02d:%02d\nt2=%04d-%02d-%02d+%02d:%02d:%02d \n",
  70. t1.tm_year+1900,
  71. t1.tm_mon+1,
  72. t1.tm_mday,
  73. t1.tm_hour,
  74. t1.tm_min,
  75. t1.tm_sec,
  76. t2.tm_year+1900,
  77. t2.tm_mon+1,
  78. t2.tm_mday,
  79. t2.tm_hour,
  80. t2.tm_min,
  81. t2.tm_sec
  82. );
  83. puts(debug);
  84. if (t1.tm_year > t2.tm_year){
  85. return true;
  86. }
  87. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  88. return true;
  89. }
  90. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  91. return true;
  92. }
  93. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
  94. return true;
  95. }
  96. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
  97. return true;
  98. }
  99. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool NtpTimeIsValid(void){
  105. return validTime;
  106. }
  107. void NtpSync(void){
  108. /* Ophalen van pool.ntp.org */
  109. isSyncing = true;
  110. //_timezone = -getTimeZone() * 3600;
  111. puts("NtpSync(): Timezone fetched. ");
  112. NutDelay(100);
  113. puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  114. uint32_t timeserver = inet_addr("213.154.229.24");
  115. for (;;) {
  116. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  117. break;
  118. } else {
  119. puts("Fout bij het ontvangen van de tijd");
  120. NutSleep(1000);
  121. }
  122. }
  123. ntp_datetime = localtime(&ntp_time);
  124. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  125. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  126. X12RtcSetClock(ntp_datetime);
  127. NtpWriteTimeToEeprom(*ntp_datetime);
  128. isSyncing = false;
  129. validTime = true;
  130. NutSleep(100);
  131. }
  132. void NtpWriteTimeToEeprom(tm time_struct){
  133. TCache cache;
  134. cache.last_sync = time_struct;
  135. EepromSetCache(&cache);
  136. }