Explorar o código

Merge branch 'bugfix/esp_sntp_declare' into 'master'

lw-ip: Fix sntp custom options if sntp_get_system_time used

Closes FCS-710

See merge request espressif/esp-idf!14556
David Čermák %!s(int64=4) %!d(string=hai) anos
pai
achega
756cc4f8dc

+ 15 - 0
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);
+}

+ 4 - 34
components/lwip/port/esp32/include/lwipopts.h

@@ -33,9 +33,7 @@
 #define __LWIPOPTS_H__
 
 #include <stdlib.h>
-#include <time.h>
 #include <unistd.h>
-#include <sys/time.h>
 #include <sys/fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -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 */
 
@@ -1058,22 +1057,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"
@@ -1083,22 +1066,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
 

+ 55 - 0
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 <stdint.h>
+
+#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__