From 76f6dd6214ca583b1a94c7c553ccac739a27f6d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20=C4=8Cerm=C3=A1k?= Date: Wed, 13 Jan 2021 10:28:34 +0800 Subject: [PATCH] lwip: Moved default SNTP API to esp_sntp.h and make sntp.h in port folders of lwip component obsoleted --- components/lwip/include/apps/esp_sntp.h | 138 ++++++++++++++++- components/lwip/include/apps/sntp/sntp.h | 139 +----------------- components/lwip/port/esp32/include/lwipopts.h | 18 ++- .../esp_netif_loopback_mock.c | 9 +- docs/doxygen/Doxyfile_common | 2 +- docs/en/api-reference/system/system_time.rst | 2 +- 6 files changed, 169 insertions(+), 139 deletions(-) diff --git a/components/lwip/include/apps/esp_sntp.h b/components/lwip/include/apps/esp_sntp.h index 020f0cf36a..9e9912a8c1 100644 --- a/components/lwip/include/apps/esp_sntp.h +++ b/components/lwip/include/apps/esp_sntp.h @@ -17,6 +17,142 @@ #include "lwip/err.h" #include "lwip/apps/sntp.h" -#include "sntp.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * The time update takes place in the sntp_sync_time() function. + * The user has the ability to redefine this function in order + * to re-define its functionality. This function has two time update modes, + * which can be set via the sntp_set_sync_mode() function. + * Two modes are available: + * - the first is an immediate update when receiving time from the sntp server, + * - the second is a smooth time update (if the time error is no more than 35 minutes, + * and an immediate update if the error is more than 35 minutes). + * + * To receive notification of time synchronization, + * you can use the callback function or get the synchronization status + * via the sntp_get_sync_status() function. + * + * To determine the time synchronization time on the device, you can use: + * 1) sntp_set_time_sync_notification_cb() function to set the callback function, + * which is convenient to use to receive notification of the update time. + * 2) sntp_get_sync_status() function for getting time synchronization status. + * After the time synchronization is completed, the status will be + * SNTP_SYNC_STATUS_COMPLETED, after, it will be reseted to SNTP_SYNC_STATUS_RESET + * to wait for the next sync cycle. + */ + +/// SNTP time update mode +typedef enum { + SNTP_SYNC_MODE_IMMED, /*!< Update system time immediately when receiving a response from the SNTP server. */ + 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. */ +} sntp_sync_mode_t; + +/// SNTP sync status +typedef enum { + SNTP_SYNC_STATUS_RESET, // Reset status. + SNTP_SYNC_STATUS_COMPLETED, // Time is synchronized. + SNTP_SYNC_STATUS_IN_PROGRESS, // Smooth time sync in progress. +} sntp_sync_status_t; + +/** + * @brief SNTP callback function for notifying about time sync event + * + * @param tv Time received from SNTP server. + */ +typedef void (*sntp_sync_time_cb_t) (struct timeval *tv); + +/** + * @brief This function updates the system time. + * + * This is a weak-linked function. It is possible to replace all SNTP update functionality + * by placing a sntp_sync_time() function in the app firmware source. + * If the default implementation is used, calling sntp_set_sync_mode() allows + * the time synchronization mode to be changed to instant or smooth. + * If a callback function is registered via sntp_set_time_sync_notification_cb(), + * it will be called following time synchronization. + * + * @param tv Time received from SNTP server. + */ +void sntp_sync_time(struct timeval *tv); + +/** + * @brief Set the sync mode + * + * Allowable two mode: SNTP_SYNC_MODE_IMMED and SNTP_SYNC_MODE_SMOOTH. + * @param sync_mode Sync mode. + */ +void sntp_set_sync_mode(sntp_sync_mode_t sync_mode); + +/** + * @brief Get set sync mode + * + * @return SNTP_SYNC_MODE_IMMED: Update time immediately. + * SNTP_SYNC_MODE_SMOOTH: Smooth time updating. + */ +sntp_sync_mode_t sntp_get_sync_mode(void); + +/** + * @brief Get status of time sync + * + * After the update is completed, the status will be returned as SNTP_SYNC_STATUS_COMPLETED. + * After that, the status will be reset to SNTP_SYNC_STATUS_RESET. + * If the update operation is not completed yet, the status will be SNTP_SYNC_STATUS_RESET. + * If a smooth mode was chosen and the synchronization is still continuing (adjtime works), then it will be SNTP_SYNC_STATUS_IN_PROGRESS. + * + * @return SNTP_SYNC_STATUS_RESET: Reset status. + * SNTP_SYNC_STATUS_COMPLETED: Time is synchronized. + * SNTP_SYNC_STATUS_IN_PROGRESS: Smooth time sync in progress. + */ +sntp_sync_status_t sntp_get_sync_status(void); + +/** + * @brief Set status of time sync + * + * @param sync_status status of time sync (see sntp_sync_status_t) + */ +void sntp_set_sync_status(sntp_sync_status_t sync_status); + +/** + * @brief Set a callback function for time synchronization notification + * + * @param callback a callback function + */ +void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback); + +/** + * @brief Set the sync interval of SNTP operation + * + * Note: SNTPv4 RFC 4330 enforces a minimum sync interval of 15 seconds. + * This sync interval will be used in the next attempt update time throught SNTP. + * To apply the new sync interval call the sntp_restart() function, + * otherwise, it will be applied after the last interval expired. + * + * @param interval_ms The sync interval in ms. It cannot be lower than 15 seconds, otherwise 15 seconds will be set. + */ +void sntp_set_sync_interval(uint32_t interval_ms); + +/** + * @brief Get the sync interval of SNTP operation + * + * @return the sync interval + */ +uint32_t sntp_get_sync_interval(void); + +/** + * @brief Restart SNTP + * + * @return True - Restart + * False - SNTP was not initialized yet + */ +bool sntp_restart(void); + +#ifdef __cplusplus +} +#endif #endif // __ESP_SNTP_H__ diff --git a/components/lwip/include/apps/sntp/sntp.h b/components/lwip/include/apps/sntp/sntp.h index b4c0939f88..616fd55460 100644 --- a/components/lwip/include/apps/sntp/sntp.h +++ b/components/lwip/include/apps/sntp/sntp.h @@ -1,4 +1,4 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,140 +15,13 @@ #ifndef __SNTP_H__ #define __SNTP_H__ -#ifdef __cplusplus -extern "C" { -#endif - /* - * The time update takes place in the sntp_sync_time() function. - * The user has the ability to redefine this function in order - * to re-define its functionality. This function has two time update modes, - * which can be set via the sntp_set_sync_mode() function. - * Two modes are available: - * - the first is an immediate update when receiving time from the sntp server, - * - the second is a smooth time update (if the time error is no more than 35 minutes, - * and an immediate update if the error is more than 35 minutes). - * - * To receive notification of time synchronization, - * you can use the callback function or get the synchronization status - * via the sntp_get_sync_status() function. - * - * To determine the time synchronization time on the device, you can use: - * 1) sntp_set_time_sync_notification_cb() function to set the callback function, - * which is convenient to use to receive notification of the update time. - * 2) sntp_get_sync_status() function for getting time synchronization status. - * After the time synchronization is completed, the status will be - * SNTP_SYNC_STATUS_COMPLETED, after, it will be reseted to SNTP_SYNC_STATUS_RESET - * to wait for the next sync cycle. + * This header is provided only for compatibility reasons for existing + * applications which directly include "sntp.h" from the IDF default paths. + * and will be removed in IDF v5.0. + * It is recommended to use "esp_sntp.h" from IDF's lwip port folder */ -/// SNTP time update mode -typedef enum { - SNTP_SYNC_MODE_IMMED, /*!< Update system time immediately when receiving a response from the SNTP server. */ - 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. */ -} sntp_sync_mode_t; - -/// SNTP sync status -typedef enum { - SNTP_SYNC_STATUS_RESET, // Reset status. - SNTP_SYNC_STATUS_COMPLETED, // Time is synchronized. - SNTP_SYNC_STATUS_IN_PROGRESS, // Smooth time sync in progress. -} sntp_sync_status_t; - -/** - * @brief SNTP callback function for notifying about time sync event - * - * @param tv Time received from SNTP server. - */ -typedef void (*sntp_sync_time_cb_t) (struct timeval *tv); - -/** - * @brief This function updates the system time. - * - * This is a weak-linked function. It is possible to replace all SNTP update functionality - * by placing a sntp_sync_time() function in the app firmware source. - * If the default implementation is used, calling sntp_set_sync_mode() allows - * the time synchronization mode to be changed to instant or smooth. - * If a callback function is registered via sntp_set_time_sync_notification_cb(), - * it will be called following time synchronization. - * - * @param tv Time received from SNTP server. - */ -void sntp_sync_time(struct timeval *tv); - -/** - * @brief Set the sync mode - * - * Allowable two mode: SNTP_SYNC_MODE_IMMED and SNTP_SYNC_MODE_SMOOTH. - * @param sync_mode Sync mode. - */ -void sntp_set_sync_mode(sntp_sync_mode_t sync_mode); - -/** - * @brief Get set sync mode - * - * @return SNTP_SYNC_MODE_IMMED: Update time immediately. - * SNTP_SYNC_MODE_SMOOTH: Smooth time updating. - */ -sntp_sync_mode_t sntp_get_sync_mode(void); - -/** - * @brief Get status of time sync - * - * After the update is completed, the status will be returned as SNTP_SYNC_STATUS_COMPLETED. - * After that, the status will be reset to SNTP_SYNC_STATUS_RESET. - * If the update operation is not completed yet, the status will be SNTP_SYNC_STATUS_RESET. - * If a smooth mode was chosen and the synchronization is still continuing (adjtime works), then it will be SNTP_SYNC_STATUS_IN_PROGRESS. - * - * @return SNTP_SYNC_STATUS_RESET: Reset status. - * SNTP_SYNC_STATUS_COMPLETED: Time is synchronized. - * SNTP_SYNC_STATUS_IN_PROGRESS: Smooth time sync in progress. - */ -sntp_sync_status_t sntp_get_sync_status(void); - -/** - * @brief Set status of time sync - * - * @param sync_status status of time sync (see sntp_sync_status_t) - */ -void sntp_set_sync_status(sntp_sync_status_t sync_status); - -/** - * @brief Set a callback function for time synchronization notification - * - * @param callback a callback function - */ -void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback); - -/** - * @brief Set the sync interval of SNTP operation - * - * Note: SNTPv4 RFC 4330 enforces a minimum sync interval of 15 seconds. - * This sync interval will be used in the next attempt update time throught SNTP. - * To apply the new sync interval call the sntp_restart() function, - * otherwise, it will be applied after the last interval expired. - * - * @param interval_ms The sync interval in ms. It cannot be lower than 15 seconds, otherwise 15 seconds will be set. - */ -void sntp_set_sync_interval(uint32_t interval_ms); - -/** - * @brief Get the sync interval of SNTP operation - * - * @return the sync interval - */ -uint32_t sntp_get_sync_interval(void); - -/** - * @brief Restart SNTP - * - * @return True - Restart - * False - SNTP was not initialized yet - */ -bool sntp_restart(void); - -#ifdef __cplusplus -} -#endif +#include "esp_sntp.h" #endif // __SNTP_H__ diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 3480ef0cbd..c364bab7f6 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -43,7 +43,6 @@ #include "esp_task.h" #include "esp_system.h" #include "sdkconfig.h" -#include "sntp.h" #include "netif/dhcp_state.h" /* Enable all Espressif-only options */ @@ -1002,6 +1001,23 @@ /* * SNTP update delay - in milliseconds */ + +/* + * Forward declarations of weak definitions from lwip's sntp.c which could + * be redefined by user application. This is needed to provide custom definition + * of the below macros in lwip's sntp.c. + * Full declaration is provided in IDF's port layer in esp_sntp.h + */ +#ifdef __cplusplus +#define LWIP_FORWARD_DECLARE_C_CXX extern "C" +#else +#define LWIP_FORWARD_DECLARE_C_CXX +#endif + +LWIP_FORWARD_DECLARE_C_CXX void sntp_sync_time(struct timeval *tv); + +LWIP_FORWARD_DECLARE_C_CXX uint32_t sntp_get_sync_interval(void); + /** Set this to 1 to support DNS names (or IP address strings) to set sntp servers * One server address/name can be defined as default if SNTP_SERVER_DNS == 1: * \#define SNTP_SERVER_ADDRESS "pool.ntp.org" diff --git a/components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c b/components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c index ba7bbccb3c..57c04b2164 100644 --- a/components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c +++ b/components/mdns/test_afl_fuzz_host/esp_netif_loopback_mock.c @@ -11,8 +11,13 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +#include "esp32_compat.h" -#include "esp_netif_lwip_internal.h" +// mock the types to decouple from lwip +typedef void * esp_netif_t; +typedef enum { DHCP_MOCK } esp_netif_dhcp_status_t; +typedef tcpip_adapter_ip_info_t esp_netif_ip_info_t; +typedef ip6_addr_t esp_ip6_addr_t; esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info) { @@ -26,7 +31,7 @@ esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_stat const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) { - return esp_netif->if_key; + return NULL; } esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6) diff --git a/docs/doxygen/Doxyfile_common b/docs/doxygen/Doxyfile_common index ce10817178..3994f802be 100644 --- a/docs/doxygen/Doxyfile_common +++ b/docs/doxygen/Doxyfile_common @@ -152,7 +152,7 @@ INPUT = \ ## ICMP-ECHO $(IDF_PATH)/components/lwip/include/apps/ping/ping_sock.h \ ## SNTP - $(IDF_PATH)/components/lwip/include/apps/sntp/sntp.h \ + $(IDF_PATH)/components/lwip/include/apps/esp_sntp.h \ ## mDNS $(IDF_PATH)/components/mdns/include/mdns.h \ $(IDF_PATH)/components/esp_http_client/include/esp_http_client.h \ diff --git a/docs/en/api-reference/system/system_time.rst b/docs/en/api-reference/system/system_time.rst index 839958f434..0bccadc2ac 100644 --- a/docs/en/api-reference/system/system_time.rst +++ b/docs/en/api-reference/system/system_time.rst @@ -133,4 +133,4 @@ Once these steps are completed, call the standard C library function ``localtime API Reference ------------- -.. include-build-file:: inc/sntp.inc +.. include-build-file:: inc/esp_sntp.inc