esp_sntp.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/ip.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. /// Aliases for esp_sntp prefixed API (inherently thread safe)
  36. #define esp_sntp_sync_time sntp_sync_time
  37. #define esp_sntp_set_sync_mode sntp_set_sync_mode
  38. #define esp_sntp_get_sync_mode sntp_get_sync_mode
  39. #define esp_sntp_get_sync_status sntp_get_sync_status
  40. #define esp_sntp_set_sync_status sntp_set_sync_status
  41. #define esp_sntp_set_time_sync_notification_cb sntp_set_time_sync_notification_cb
  42. #define esp_sntp_set_sync_interval sntp_set_sync_interval
  43. #define esp_sntp_get_sync_interval sntp_get_sync_interval
  44. #define esp_sntp_restart sntp_restart
  45. #ifndef SNTP_OPMODE_POLL
  46. #define SNTP_OPMODE_POLL ESP_SNTP_OPMODE_POLL
  47. #else
  48. #warning "Defined!"
  49. #endif /* SNTP_OPMODE_POLL */
  50. /// SNTP time update mode
  51. typedef enum {
  52. SNTP_SYNC_MODE_IMMED, /*!< Update system time immediately when receiving a response from the SNTP server. */
  53. 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. */
  54. } sntp_sync_mode_t;
  55. /// SNTP sync status
  56. typedef enum {
  57. SNTP_SYNC_STATUS_RESET, // Reset status.
  58. SNTP_SYNC_STATUS_COMPLETED, // Time is synchronized.
  59. SNTP_SYNC_STATUS_IN_PROGRESS, // Smooth time sync in progress.
  60. } sntp_sync_status_t;
  61. /// SNTP operating modes per lwip SNTP module
  62. typedef enum {
  63. ESP_SNTP_OPMODE_POLL,
  64. ESP_SNTP_OPMODE_LISTENONLY,
  65. } esp_sntp_operatingmode_t;
  66. /**
  67. * @brief SNTP callback function for notifying about time sync event
  68. *
  69. * @param tv Time received from SNTP server.
  70. */
  71. typedef void (*sntp_sync_time_cb_t) (struct timeval *tv);
  72. /**
  73. * @brief This function updates the system time.
  74. *
  75. * This is a weak-linked function. It is possible to replace all SNTP update functionality
  76. * by placing a sntp_sync_time() function in the app firmware source.
  77. * If the default implementation is used, calling sntp_set_sync_mode() allows
  78. * the time synchronization mode to be changed to instant or smooth.
  79. * If a callback function is registered via sntp_set_time_sync_notification_cb(),
  80. * it will be called following time synchronization.
  81. *
  82. * @param tv Time received from SNTP server.
  83. */
  84. void sntp_sync_time(struct timeval *tv);
  85. /**
  86. * @brief Set the sync mode
  87. *
  88. * Modes allowed: SNTP_SYNC_MODE_IMMED and SNTP_SYNC_MODE_SMOOTH.
  89. * @param sync_mode Sync mode.
  90. */
  91. void sntp_set_sync_mode(sntp_sync_mode_t sync_mode);
  92. /**
  93. * @brief Get set sync mode
  94. *
  95. * @return SNTP_SYNC_MODE_IMMED: Update time immediately.
  96. * SNTP_SYNC_MODE_SMOOTH: Smooth time updating.
  97. */
  98. sntp_sync_mode_t sntp_get_sync_mode(void);
  99. /**
  100. * @brief Get status of time sync
  101. *
  102. * After the update is completed, the status will be returned as SNTP_SYNC_STATUS_COMPLETED.
  103. * After that, the status will be reset to SNTP_SYNC_STATUS_RESET.
  104. * If the update operation is not completed yet, the status will be SNTP_SYNC_STATUS_RESET.
  105. * If a smooth mode was chosen and the synchronization is still continuing (adjtime works), then it will be SNTP_SYNC_STATUS_IN_PROGRESS.
  106. *
  107. * @return SNTP_SYNC_STATUS_RESET: Reset status.
  108. * SNTP_SYNC_STATUS_COMPLETED: Time is synchronized.
  109. * SNTP_SYNC_STATUS_IN_PROGRESS: Smooth time sync in progress.
  110. */
  111. sntp_sync_status_t sntp_get_sync_status(void);
  112. /**
  113. * @brief Set status of time sync
  114. *
  115. * @param sync_status status of time sync (see sntp_sync_status_t)
  116. */
  117. void sntp_set_sync_status(sntp_sync_status_t sync_status);
  118. /**
  119. * @brief Set a callback function for time synchronization notification
  120. *
  121. * @param callback a callback function
  122. */
  123. void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback);
  124. /**
  125. * @brief Set the sync interval of SNTP operation
  126. *
  127. * Note: SNTPv4 RFC 4330 enforces a minimum sync interval of 15 seconds.
  128. * This sync interval will be used in the next attempt update time throught SNTP.
  129. * To apply the new sync interval call the sntp_restart() function,
  130. * otherwise, it will be applied after the last interval expired.
  131. *
  132. * @param interval_ms The sync interval in ms. It cannot be lower than 15 seconds, otherwise 15 seconds will be set.
  133. */
  134. void sntp_set_sync_interval(uint32_t interval_ms);
  135. /**
  136. * @brief Get the sync interval of SNTP operation
  137. *
  138. * @return the sync interval
  139. */
  140. uint32_t sntp_get_sync_interval(void);
  141. /**
  142. * @brief Restart SNTP
  143. *
  144. * @return True - Restart
  145. * False - SNTP was not initialized yet
  146. */
  147. bool sntp_restart(void);
  148. /**
  149. * @brief Sets SNTP operating mode. The mode has to be set before init.
  150. *
  151. * @param operating_mode Desired operating mode
  152. */
  153. void esp_sntp_setoperatingmode(esp_sntp_operatingmode_t operating_mode);
  154. /**
  155. * @brief Init and start SNTP service
  156. */
  157. void esp_sntp_init(void);
  158. /**
  159. * @brief Stops SNTP service
  160. */
  161. void esp_sntp_stop(void);
  162. /**
  163. * @brief Sets SNTP server address
  164. *
  165. * @param idx Index of the server
  166. * @param addr IP address of the server
  167. */
  168. void esp_sntp_setserver(u8_t idx, const ip_addr_t *addr);
  169. /**
  170. * @brief Sets SNTP hostname
  171. * @param idx Index of the server
  172. * @param server Name of the server
  173. */
  174. void esp_sntp_setservername(u8_t idx, const char *server);
  175. /**
  176. * @brief Gets SNTP server name
  177. * @param idx Index of the server
  178. * @return Name of the server
  179. */
  180. const char *esp_sntp_getservername(u8_t idx);
  181. /**
  182. * @brief Get SNTP server IP
  183. * @param idx Index of the server
  184. * @return IP address of the server
  185. */
  186. const ip_addr_t* esp_sntp_getserver(u8_t idx);
  187. /**
  188. * @brief Checks if sntp is enabled
  189. * @return true if sntp module is enabled
  190. */
  191. bool esp_sntp_enabled(void);
  192. #if LWIP_DHCP_GET_NTP_SRV
  193. /**
  194. * @brief Enable acquiring SNTP server from DHCP
  195. * @param enable True for enabling SNTP from DHCP
  196. */
  197. void esp_sntp_servermode_dhcp(bool enable);
  198. #endif /* LWIP_DHCP_GET_NTP_SRV */
  199. #if !defined(ESP_LWIP_COMPONENT_BUILD) && !defined(ESP_NETIF_COMPONENT_BUILD)
  200. /**
  201. * @brief if not build within lwip, provide translating inlines,
  202. * that will warn about thread safety
  203. */
  204. static inline __attribute__((deprecated("use esp_sntp_setoperatingmode() instead")))
  205. void sntp_setoperatingmode(u8_t operating_mode)
  206. {
  207. esp_sntp_setoperatingmode((esp_sntp_operatingmode_t)operating_mode);
  208. }
  209. static inline __attribute__((deprecated("use esp_sntp_servermode_dhcp() instead")))
  210. void sntp_servermode_dhcp(int set_servers_from_dhcp)
  211. {
  212. #if LWIP_DHCP_GET_NTP_SRV
  213. esp_sntp_servermode_dhcp(set_servers_from_dhcp);
  214. #endif
  215. }
  216. static inline __attribute__((deprecated("use esp_sntp_setservername() instead")))
  217. void sntp_setservername(u8_t idx, const char *server)
  218. {
  219. esp_sntp_setservername(idx, server);
  220. }
  221. static inline __attribute__((deprecated("use esp_sntp_init() instead")))
  222. void sntp_init(void)
  223. {
  224. esp_sntp_init();
  225. }
  226. static inline __attribute__((deprecated("use esp_sntp_getservername() instead")))
  227. const char *sntp_getservername(u8_t idx)
  228. {
  229. return esp_sntp_getservername(idx);
  230. }
  231. static inline __attribute__((deprecated("use esp_sntp_getserver() instead")))
  232. const ip_addr_t* sntp_getserver(u8_t idx)
  233. {
  234. return esp_sntp_getserver(idx);
  235. }
  236. #endif /* ESP_LWIP_COMPONENT_BUILD */
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif // __ESP_SNTP_H__