esp_netif_sntp.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "esp_err.h"
  11. #include "esp_netif_types.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * @defgroup ESP_NETIF_SNTP_API ESP-NETIF SNTP API
  17. * @brief SNTP API for underlying TCP/IP stack
  18. *
  19. */
  20. /** @addtogroup ESP_NETIF_SNTP_API
  21. * @{
  22. */
  23. /**
  24. * @brief Time sync notification function
  25. */
  26. typedef void (*esp_sntp_time_cb_t)(struct timeval *tv);
  27. /**
  28. * @brief Utility macro for providing multiple servers in parentheses
  29. */
  30. #define ESP_SNTP_SERVER_LIST(...) { __VA_ARGS__ }
  31. /**
  32. * @brief Default configuration to init SNTP with multiple servers
  33. * @param servers_in_list Number of servers in the list
  34. * @param list_of_servers List of servers (use ESP_SNTP_SERVER_LIST(...))
  35. *
  36. */
  37. #define ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(servers_in_list, list_of_servers) { \
  38. .smooth_sync = false, \
  39. .server_from_dhcp = false, \
  40. .wait_for_sync = true, \
  41. .start = true, \
  42. .sync_cb = NULL, \
  43. .renew_servers_after_new_IP = false, \
  44. .ip_event_to_renew = IP_EVENT_STA_GOT_IP, \
  45. .index_of_first_server = 0, \
  46. .num_of_servers = (servers_in_list), \
  47. .servers = list_of_servers, \
  48. }
  49. /**
  50. * @brief Default configuration with a single server
  51. */
  52. #define ESP_NETIF_SNTP_DEFAULT_CONFIG(server) \
  53. ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(1, {server})
  54. /**
  55. * @brief SNTP configuration struct
  56. */
  57. typedef struct esp_sntp_config {
  58. bool smooth_sync; ///< set to true if smooth sync required
  59. bool server_from_dhcp; ///< set to true to request NTP server config from DHCP
  60. bool wait_for_sync; ///< if true, we create a semaphore to signal time sync event
  61. bool start; ///< set to true to automatically start the SNTP service
  62. esp_sntp_time_cb_t sync_cb; ///< optionally sets callback function on time sync event
  63. bool renew_servers_after_new_IP; ///< this is used to refresh server list if NTP provided by DHCP (which cleans other pre-configured servers)
  64. ip_event_t ip_event_to_renew; ///< set the IP event id on which we refresh server list (if renew_servers_after_new_IP=true)
  65. size_t index_of_first_server; ///< refresh server list after this server (if renew_servers_after_new_IP=true)
  66. size_t num_of_servers; ///< number of preconfigured NTP servers
  67. const char* servers[CONFIG_LWIP_SNTP_MAX_SERVERS]; ///< list of servers
  68. } esp_sntp_config_t;
  69. /**
  70. * @brief Initialize SNTP with supplied config struct
  71. * @param config Config struct
  72. * @return ESP_OK on success
  73. */
  74. esp_err_t esp_netif_sntp_init(const esp_sntp_config_t * config);
  75. /**
  76. * @brief Start SNTP service
  77. * if it wasn't started during init (config.start = false)
  78. * or restart it if already started
  79. * @return ESP_OK on success
  80. */
  81. esp_err_t esp_netif_sntp_start(void);
  82. /**
  83. * @brief Deinitialize esp_netif SNTP module
  84. */
  85. void esp_netif_sntp_deinit(void);
  86. /**
  87. * @brief Wait for time sync event
  88. * @param tout Specified timeout in RTOS ticks
  89. * @return ESP_TIMEOUT if sync event didn't came withing the timeout
  90. * ESP_ERR_NOT_FINISHED if the sync event came, but we're in smooth update mode and still in progress (SNTP_SYNC_STATUS_IN_PROGRESS)
  91. * ESP_OK if time sync'ed
  92. */
  93. esp_err_t esp_netif_sntp_sync_wait(TickType_t tout);
  94. /**
  95. * @}
  96. */
  97. #ifdef __cplusplus
  98. }
  99. #endif