sntp.h 5.5 KB

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