esp_sntp.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_SNTP_H__
  7. #define __ESP_SNTP_H__
  8. #include "lwip/err.h"
  9. #include "lwip/apps/sntp.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /*
  14. * The time update takes place in the sntp_sync_time() function.
  15. * The user has the ability to redefine this function in order
  16. * to re-define its functionality. This function has two time update modes,
  17. * which can be set via the sntp_set_sync_mode() function.
  18. * Two available modes are as follows:
  19. * - the first is an immediate update when receiving time from the sntp server,
  20. * - the second is a smooth time update (if the time error is no more than 35 minutes,
  21. * and an immediate update if the error is more than 35 minutes).
  22. *
  23. * To receive notification of time synchronization,
  24. * you can use the callback function or get the synchronization status
  25. * via the sntp_get_sync_status() function.
  26. *
  27. * To determine the time synchronization time on the device, you can use:
  28. * 1) sntp_set_time_sync_notification_cb() function to set the callback function,
  29. * which is convenient to use to receive notification of the update time.
  30. * 2) sntp_get_sync_status() function for getting time synchronization status.
  31. * After the time synchronization is completed, the status will be
  32. * SNTP_SYNC_STATUS_COMPLETED, after, it will be reseted to SNTP_SYNC_STATUS_RESET
  33. * to wait for the next sync cycle.
  34. */
  35. /// SNTP time update mode
  36. typedef enum {
  37. SNTP_SYNC_MODE_IMMED, /*!< Update system time immediately when receiving a response from the SNTP server. */
  38. SNTP_SYNC_MODE_SMOOTH, /*!< Smooth time updating. Time error is gradually reduced using adjtime function. If the difference between SNTP response time and system time is large (more than 35 minutes) then update immediately. */
  39. } sntp_sync_mode_t;
  40. /// SNTP sync status
  41. typedef enum {
  42. SNTP_SYNC_STATUS_RESET, // Reset status.
  43. SNTP_SYNC_STATUS_COMPLETED, // Time is synchronized.
  44. SNTP_SYNC_STATUS_IN_PROGRESS, // Smooth time sync in progress.
  45. } sntp_sync_status_t;
  46. /**
  47. * @brief SNTP callback function for notifying about time sync event
  48. *
  49. * @param tv Time received from SNTP server.
  50. */
  51. typedef void (*sntp_sync_time_cb_t) (struct timeval *tv);
  52. /**
  53. * @brief This function updates the system time.
  54. *
  55. * This is a weak-linked function. It is possible to replace all SNTP update functionality
  56. * by placing a sntp_sync_time() function in the app firmware source.
  57. * If the default implementation is used, calling sntp_set_sync_mode() allows
  58. * the time synchronization mode to be changed to instant or smooth.
  59. * If a callback function is registered via sntp_set_time_sync_notification_cb(),
  60. * it will be called following time synchronization.
  61. *
  62. * @param tv Time received from SNTP server.
  63. */
  64. void sntp_sync_time(struct timeval *tv);
  65. /**
  66. * @brief Set the sync mode
  67. *
  68. * Modes allowed: SNTP_SYNC_MODE_IMMED and SNTP_SYNC_MODE_SMOOTH.
  69. * @param sync_mode Sync mode.
  70. */
  71. void sntp_set_sync_mode(sntp_sync_mode_t sync_mode);
  72. /**
  73. * @brief Get set sync mode
  74. *
  75. * @return SNTP_SYNC_MODE_IMMED: Update time immediately.
  76. * SNTP_SYNC_MODE_SMOOTH: Smooth time updating.
  77. */
  78. sntp_sync_mode_t sntp_get_sync_mode(void);
  79. /**
  80. * @brief Get status of time sync
  81. *
  82. * After the update is completed, the status will be returned as SNTP_SYNC_STATUS_COMPLETED.
  83. * After that, the status will be reset to SNTP_SYNC_STATUS_RESET.
  84. * If the update operation is not completed yet, the status will be SNTP_SYNC_STATUS_RESET.
  85. * If a smooth mode was chosen and the synchronization is still continuing (adjtime works), then it will be SNTP_SYNC_STATUS_IN_PROGRESS.
  86. *
  87. * @return SNTP_SYNC_STATUS_RESET: Reset status.
  88. * SNTP_SYNC_STATUS_COMPLETED: Time is synchronized.
  89. * SNTP_SYNC_STATUS_IN_PROGRESS: Smooth time sync in progress.
  90. */
  91. sntp_sync_status_t sntp_get_sync_status(void);
  92. /**
  93. * @brief Set status of time sync
  94. *
  95. * @param sync_status status of time sync (see sntp_sync_status_t)
  96. */
  97. void sntp_set_sync_status(sntp_sync_status_t sync_status);
  98. /**
  99. * @brief Set a callback function for time synchronization notification
  100. *
  101. * @param callback a callback function
  102. */
  103. void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback);
  104. /**
  105. * @brief Set the sync interval of SNTP operation
  106. *
  107. * Note: SNTPv4 RFC 4330 enforces a minimum sync interval of 15 seconds.
  108. * This sync interval will be used in the next attempt update time throught SNTP.
  109. * To apply the new sync interval call the sntp_restart() function,
  110. * otherwise, it will be applied after the last interval expired.
  111. *
  112. * @param interval_ms The sync interval in ms. It cannot be lower than 15 seconds, otherwise 15 seconds will be set.
  113. */
  114. void sntp_set_sync_interval(uint32_t interval_ms);
  115. /**
  116. * @brief Get the sync interval of SNTP operation
  117. *
  118. * @return the sync interval
  119. */
  120. uint32_t sntp_get_sync_interval(void);
  121. /**
  122. * @brief Restart SNTP
  123. *
  124. * @return True - Restart
  125. * False - SNTP was not initialized yet
  126. */
  127. bool sntp_restart(void);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif // __ESP_SNTP_H__