From bb0eecee0ec7f7a8a885d76efebc9890b2da2682 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 28 Jul 2021 17:03:50 +0200 Subject: [PATCH] lwip: Fix sntp custom options if sntp_get_system_time used --- components/lwip/apps/sntp/sntp.c | 15 +++++ components/lwip/port/esp32/include/lwipopts.h | 38 ++----------- .../esp32/include/sntp/sntp_get_set_time.h | 55 +++++++++++++++++++ 3 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 components/lwip/port/esp32/include/sntp/sntp_get_set_time.h diff --git a/components/lwip/apps/sntp/sntp.c b/components/lwip/apps/sntp/sntp.c index d6ff2993cf..677574d200 100644 --- a/components/lwip/apps/sntp/sntp.c +++ b/components/lwip/apps/sntp/sntp.c @@ -117,3 +117,18 @@ bool sntp_restart(void) } return false; } + +void sntp_set_system_time(uint32_t sec, uint32_t us) +{ + struct timeval tv = { .tv_sec = sec, .tv_usec = us }; + sntp_sync_time(&tv); +} + +void sntp_get_system_time(uint32_t *sec, uint32_t *us) +{ + struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; + gettimeofday(&tv, NULL); + *(sec) = tv.tv_sec; + *(us) = tv.tv_usec; + sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); +} diff --git a/components/lwip/port/esp32/include/lwipopts.h b/components/lwip/port/esp32/include/lwipopts.h index 98a85fc2d9..dcd0c18d3e 100644 --- a/components/lwip/port/esp32/include/lwipopts.h +++ b/components/lwip/port/esp32/include/lwipopts.h @@ -33,9 +33,7 @@ #define __LWIPOPTS_H__ #include -#include #include -#include #include #include #include @@ -44,6 +42,7 @@ #include "esp_system.h" #include "sdkconfig.h" #include "netif/dhcp_state.h" +#include "sntp/sntp_get_set_time.h" /* Enable all Espressif-only options */ @@ -1027,22 +1026,6 @@ * 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" @@ -1052,22 +1035,9 @@ LWIP_FORWARD_DECLARE_C_CXX uint32_t sntp_get_sync_interval(void); // It disables a check of SNTP_UPDATE_DELAY it is done in sntp_set_sync_interval #define SNTP_SUPPRESS_DELAY_CHECK -#define SNTP_UPDATE_DELAY (sntp_get_sync_interval()) - -#define SNTP_SET_SYSTEM_TIME_US(sec, us) \ - do { \ - struct timeval tv = { .tv_sec = sec, .tv_usec = us }; \ - sntp_sync_time(&tv); \ - } while (0); - -#define SNTP_GET_SYSTEM_TIME(sec, us) \ - do { \ - struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; \ - gettimeofday(&tv, NULL); \ - (sec) = tv.tv_sec; \ - (us) = tv.tv_usec; \ - sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); \ - } while (0); +#define SNTP_UPDATE_DELAY (sntp_get_sync_interval()) +#define SNTP_SET_SYSTEM_TIME_US(sec, us) (sntp_set_system_time(sec, us)) +#define SNTP_GET_SYSTEM_TIME(sec, us) (sntp_get_system_time(&(sec), &(us))) #define SOC_SEND_LOG //printf diff --git a/components/lwip/port/esp32/include/sntp/sntp_get_set_time.h b/components/lwip/port/esp32/include/sntp/sntp_get_set_time.h new file mode 100644 index 0000000000..1991620804 --- /dev/null +++ b/components/lwip/port/esp32/include/sntp/sntp_get_set_time.h @@ -0,0 +1,55 @@ +// Copyright 2021 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. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. + +#ifndef __SNTP_GET_SET_TIME_H__ +#define __SNTP_GET_SET_TIME_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/* + * Declarations of functions used in lwipopts.h to redefine + * default sntp macros, such as: + * - SNTP_UPDATE_DELAY() + * - SNTP_SET_SYSTEM_TIME_US() + * - SNTP_GET_SYSTEM_TIME() + */ + +/* + * @brief Get the sync interval of SNTP operation + * Full definition is provided in IDF's layer in esp_sntp.c + */ +uint32_t sntp_get_sync_interval(void); + +/** + * @brief system time setter used in the sntp module + * @note The lwip sntp uses u32_t types for sec and us arguments + */ +void sntp_set_system_time(uint32_t sec, uint32_t us); + +/** + * @brief system time getter used in the sntp module + * @note The lwip sntp uses u32_t types for sec and us arguments + */ +void sntp_get_system_time(uint32_t* sec, uint32_t* us); + +#ifdef __cplusplus +} +#endif + +#endif //__SNTP_GET_SET_TIME_H__