Skip to content

Commit

Permalink
fix(esp_netif): Prevent running esp_netif_sntp_init() multiple times
Browse files Browse the repository at this point in the history
Closes #12854
  • Loading branch information
david-cermak committed Feb 7, 2024
1 parent 37f0447 commit feef14b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions components/esp_netif/lwip/esp_netif_sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void esp_netif_sntp_renew_servers(void *handler_args, esp_event_base_t base, int
esp_err_t esp_netif_sntp_init(const esp_sntp_config_t * config)
{
esp_err_t ret = ESP_OK;
ESP_RETURN_ON_FALSE(s_storage == NULL, ESP_ERR_INVALID_STATE, TAG, "esp_netif_sntp already initialized");
s_storage = calloc(1, sizeof(sntp_storage_t) + // allocate space for servers only if we are supposed to refresh the settings
(config->renew_servers_after_new_IP ? config->num_of_servers * sizeof(char*) : 0));
ESP_GOTO_ON_FALSE(s_storage != NULL, ESP_ERR_NO_MEM, err, TAG, "Failed to allocate SNTP storage");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -48,7 +48,15 @@ TEST(esp_netif, init_and_destroy_sntp)
{
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("127.0.0.1");
config.start = false;
esp_netif_sntp_init(&config);
TEST_ESP_OK(esp_netif_sntp_init(&config));
// Cannot initialize multiple times
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_sntp_init(&config));
// Try again to see that the state didn't change
TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_netif_sntp_init(&config));
esp_netif_sntp_deinit();

// Can initialize again once it's destroyed
TEST_ESP_OK(esp_netif_sntp_init(&config));
esp_netif_sntp_deinit();
}

Expand Down
2 changes: 1 addition & 1 deletion docs/en/api-reference/network/esp_netif.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ You can find a brief introduction to SNTP in general, its initialization code, a

This section provides more details about specific use cases of the SNTP service, with statically configured servers, or use the DHCP-provided servers, or both. The workflow is usually very simple:

1) Initialize and configure the service using :cpp:func:`esp_netif_sntp_init()`.
1) Initialize and configure the service using :cpp:func:`esp_netif_sntp_init()`. This operations can only be called once (unless the SNTP service has been destroyed by :cpp:func:`esp_netif_sntp_deinit()`)
2) Start the service via :cpp:func:`esp_netif_sntp_start()`. This step is not needed if we auto-started the service in the previous step (default). It is useful to start the service explicitly after connecting if we want to use the DHCP-obtained NTP servers. Please note, this option needs to be enabled before connecting, but the SNTP service should be started after.
3) Wait for the system time to synchronize using :cpp:func:`esp_netif_sntp_sync_wait()` (only if needed).
4) Stop and destroy the service using :cpp:func:`esp_netif_sntp_deinit()`.
Expand Down
2 changes: 1 addition & 1 deletion docs/zh_CN/api-reference/network/esp_netif.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ SNTP 的简要介绍、初始化代码和基本模式请参阅 :doc:`系统时

本节介绍了使用 SNTP 服务特定用例的详细信息,包括静态配置的服务器、使用 DHCP 提供的服务器或两者兼备的情况,操作流程如下:

1) 调用 :cpp:func:`esp_netif_sntp_init()` 初始化服务并完成配置。
1) 调用 :cpp:func:`esp_netif_sntp_init()` 初始化服务并完成配置。此操作只能执行一次(除非已调用 :cpp:func:`esp_netif_sntp_deinit()` 销毁 SNTP 服务)。
2) 调用 :cpp:func:`esp_netif_sntp_start()` 启动服务。如果在前一步中已经默认启动了服务,则不需要此步骤。如果需使用通过 DHCP 获取的 NTP 服务器,推荐在完成连接后显式启动该服务。注意,应在连接前启用通过 DHCP 获取的 NTP 服务器选项,并在连接后再启用 SNTP 服务。
3) 需要时,可调用 :cpp:func:`esp_netif_sntp_sync_wait()` 等待系统时间同步。
4) 调用 :cpp:func:`esp_netif_sntp_deinit()` 停止并销毁服务。
Expand Down

0 comments on commit feef14b

Please sign in to comment.