mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 05:34:32 +02:00
docs: CN translation for system_time.rst and migration-guides: 5.1/networking.rst
This commit is contained in:
committed by
David Cermak
parent
a71fa82177
commit
9d80d981b5
@@ -134,7 +134,7 @@ The lwIP SNTP library could work in one of the following sync modes:
|
||||
|
||||
If you want to choose the :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH` mode, please set the :cpp:member:`esp_sntp_config::smooth` to ``true`` in the SNTP configuration struct. Otherwise (and by default) the :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` mode will be used.
|
||||
|
||||
For setting a callback function when time gets synchronized, use the :cpp:member:`esp_sntp_config::sync_cb` field in the configuration struct.
|
||||
For setting a callback function that is called when time gets synchronized, use the :cpp:member:`esp_sntp_config::sync_cb` field in the configuration struct.
|
||||
|
||||
An application with this initialization code will periodically synchronize the time. The time synchronization period is determined by :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` (the default value is one hour). To modify the variable, set :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` in project configuration.
|
||||
|
||||
@@ -142,11 +142,11 @@ A code example that demonstrates the implementation of time synchronization base
|
||||
|
||||
Note that it's also possible to use lwIP API directly, but care must be taken to thread safety. Here we list the thread-safe APIs:
|
||||
|
||||
- :cpp:func:`sntp_set_time_sync_notification_cb()`: Can be used to set a callback function that will notify of the time synchronization process.
|
||||
- :cpp:func:`sntp_get_sync_status()` and :cpp:func:`sntp_set_sync_status()`: Can be used to get/set time synchronization status.
|
||||
- :cpp:func:`sntp_set_sync_mode` can be used to set the synchronization mode
|
||||
- :cpp:func:`esp_sntp_setoperatingmode` sets the preferred operating mode :cpp:enumerator:`ESP_SNTP_OPMODE_POLL` and :cpp:func:`esp_sntp_init` initializes SNTP modeule
|
||||
- :cpp:func:`esp_sntp_setservername` configures one SNTP server
|
||||
- :cpp:func:`sntp_set_time_sync_notification_cb` can be used to set a callback function that will notify of the time synchronization process.
|
||||
- :cpp:func:`sntp_get_sync_status` and :cpp:func:`sntp_set_sync_status` can be used to get/set time synchronization status.
|
||||
- :cpp:func:`sntp_set_sync_mode` can be used to set the synchronization mode.
|
||||
- :cpp:func:`esp_sntp_setoperatingmode` sets the preferred operating mode.:cpp:enumerator:`ESP_SNTP_OPMODE_POLL` and :cpp:func:`esp_sntp_init` initializes SNTP module.
|
||||
- :cpp:func:`esp_sntp_setservername` configures one SNTP server.
|
||||
|
||||
|
||||
Timezones
|
||||
|
@@ -108,28 +108,46 @@ SNTP 时间同步
|
||||
|
||||
要设置当前时间,可以使用 POSIX 函数 ``settimeofday()`` 和 ``adjtime()``。lwIP 中的 SNTP 库会在收到 NTP 服务器的响应报文后,调用这两个函数以更新当前的系统时间。当然,用户可以在 lwIP SNTP 库之外独立地使用这两个函数。
|
||||
|
||||
在 lwIP SNTP 库内部调用的函数依赖于系统时间的同步模式。可使用函数 :cpp:func:`sntp_set_sync_mode` 来设置下列同步模式之一。
|
||||
包括 SNTP 函数在内的一些 lwIP API 并非线程安全,因此建议在与 SNTP 模块交互时使用 :doc:`esp_netif component <../network/esp_netif>`。
|
||||
|
||||
- :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` (默认):使用函数 ``settimeofday()`` 后,收到 SNTP 服务器响应时立即更新系统时间。
|
||||
- :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH`:使用函数 ``adjtime()`` 后,通过逐渐减小时间误差,平滑地更新时间。如果 SNTP 响应报文中的时间与当前系统时间相差大于 35 分钟,则会通过 ``settimeofday()`` 立即更新系统时间。
|
||||
|
||||
lwIP SNTP 库提供了 API 函数,用于设置某个事件的回调函数。您可能需要使用以下函数:
|
||||
|
||||
- :cpp:func:`sntp_set_time_sync_notification_cb()`:用于设置回调函数,通知时间同步的过程。
|
||||
- :cpp:func:`sntp_get_sync_status()` 和 :cpp:func:`sntp_set_sync_status()`:用于获取或设置时间同步状态。
|
||||
|
||||
通过 SNTP 开始时间同步,只需调用以下三个函数:
|
||||
要初始化特定的 SNTP 服务器并启动 SNTP 服务,只需创建有特定服务器名称的默认 SNTP 服务器配置,然后调用 :cpp:func:`esp_netif_sntp_init()` 注册该服务器并启动 SNTP 服务。
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_setservername(0, "pool.ntp.org");
|
||||
sntp_init();
|
||||
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
|
||||
esp_netif_sntp_init(&config);
|
||||
|
||||
一旦收到 SNTP 服务器的响应,此代码会自动执行时间同步。有时等待时间同步很有意义,调用 :cpp:func:`esp_netif_sntp_sync_wait()` 可实现此目的:
|
||||
|
||||
.. code-block:: c
|
||||
|
||||
if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(10000)) != ESP_OK) {
|
||||
printf("Failed to update system time within 10s timeout");
|
||||
}
|
||||
|
||||
要配置多个 NTP 服务器(或使用更高级的设置,例如 DHCP 提供的 NTP 服务器),请参考 :doc:`esp_netif <../network/esp_netif>` 文档 :ref:`esp_netif-sntp-api` 中的详细说明。
|
||||
|
||||
lwIP SNTP 库可在下列任一同步模式下工作:
|
||||
|
||||
- :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` (默认):使用 ``settimeofday()``,收到 SNTP 服务器响应后立即更新系统时间。
|
||||
- :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH`:使用函数 ``adjtime()`` 逐渐减少时间误差以平滑更新时间。如果 SNTP 响应时间和系统时间之差超过 35 分钟,请立即使用 ``settimeofday()`` 更新系统时间。
|
||||
|
||||
如要选择 :cpp:enumerator:`SNTP_SYNC_MODE_SMOOTH` 模式,请将 SNTP 配置结构体中的 :cpp:member:`esp_sntp_config::smooth` 设置为 ``true``,否则将默认使用 :cpp:enumerator:`SNTP_SYNC_MODE_IMMED` 模式。
|
||||
|
||||
设置时间同步时的回调函数,请使用配置结构体中的 :cpp:member:`esp_sntp_config::sync_cb` 字段。
|
||||
|
||||
添加此初始化代码后,应用程序将定期同步时间。时间同步周期由 :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY` 设置(默认为一小时)。如需修改,请在项目配置中设置 :ref:`CONFIG_LWIP_SNTP_UPDATE_DELAY`。
|
||||
|
||||
如需查看示例代码,请前往 :example:`protocols/sntp` 目录。该目录下的示例展示了如何基于 lwIP SNTP 库实现时间同步。
|
||||
|
||||
您也可以直接使用 lwIP API,但请务必注意线程安全。线程安全的 API 如下:
|
||||
|
||||
- :cpp:func:`sntp_set_time_sync_notification_cb` 用于设置通知时间同步过程的回调函数。
|
||||
- :cpp:func:`sntp_get_sync_status` 和 :cpp:func:`sntp_set_sync_status` 用于获取/设置时间同步状态。
|
||||
- :cpp:func:`sntp_set_sync_mode` 用于设置同步模式。
|
||||
- :cpp:func:`esp_sntp_setoperatingmode` 用于设置首选操作模式。:cpp:enumerator:`ESP_SNTP_OPMODE_POLL` 和 :cpp:func:`esp_sntp_init` 可初始化 SNTP 模块。
|
||||
- :cpp:func:`esp_sntp_setservername` 用于配置特定 SNTP 服务器。
|
||||
|
||||
|
||||
时区
|
||||
------
|
||||
|
@@ -6,4 +6,4 @@
|
||||
SNTP
|
||||
*****
|
||||
|
||||
请参考章节 :ref:`esp_netif-sntp-api` 了解更多详情。
|
||||
SNTP 模块现在提供线程安全的 API 用于访问 lwIP 功能。建议使用 :doc:`ESP_NETIF </api-reference/network/esp_netif>` API。了解更多信息,请参考章节 :ref:`esp_netif-sntp-api`。
|
||||
|
Reference in New Issue
Block a user