esp_sntp.h 5.5 KB

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