ntp.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <arpa/inet.h>
  8. #include <net/route.h>
  9. #include <pro/dhcp.h>
  10. #include <pro/sntp.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "log.h"
  17. #include "ntp.h"
  18. #define TIME_ZONE 1
  19. #define LOG_MODULE LOG_NTP_MODULE
  20. bool isSyncing;
  21. time_t ntp_time = 0;
  22. tm *ntp_datetime;
  23. uint32_t timeserver = 0;
  24. void NtpInit() {
  25. /* Timezone van nederland (gmt 1) */
  26. _timezone = -TIME_ZONE * 3600;
  27. GetTime();
  28. }
  29. bool NtpIsSyncing(){
  30. return isSyncing;
  31. }
  32. void GetTime(){
  33. /* Ophalen van pool.ntp.org */
  34. isSyncing = true;
  35. puts("Tijd ophalen van pool.ntp.org (213.154.229.24)");
  36. timeserver = inet_addr("213.154.229.24");
  37. for (;;) {
  38. if (NutSNTPGetTime(&timeserver, &ntp_time) == 0) {
  39. break;
  40. } else {
  41. NutSleep(400);
  42. puts("Fout bij het ontvangen van de tijd");
  43. }
  44. }
  45. puts("Opgehaald.\n");
  46. ntp_datetime = localtime(&ntp_time);
  47. printf("NTP time is: %02d:%02d:%02d\n", ntp_datetime->tm_hour, ntp_datetime->tm_min, ntp_datetime->tm_sec);
  48. printf("NTP date is: %02d.%02d.%02d\n\n", ntp_datetime->tm_mday, (ntp_datetime->tm_mon + 1), (ntp_datetime->tm_year + 1900));
  49. X12RtcSetClock(ntp_datetime);
  50. isSyncing = false;
  51. }