Browse Source

Cleanup of freeRTOS and LwIP

Jose Cabral 5 years ago
parent
commit
17e2143530
3 changed files with 24 additions and 34 deletions
  1. 5 3
      arch/common/ua_freeRTOS.h
  2. 11 9
      arch/common/ua_lwip.h
  3. 8 22
      arch/freertosLWIP/ua_clock.c

+ 5 - 3
arch/common/ua_freeRTOS.h

@@ -14,8 +14,6 @@
 # undef BYTE_ORDER
 #endif
 
-#include <unistd.h> // read, write, close
-
 #define UA_sleep_ms(X) vTaskDelay(pdMS_TO_TICKS(X))
 
 #ifdef OPEN62541_FEERTOS_USE_OWN_MEM
@@ -30,7 +28,11 @@
 # define UA_realloc realloc
 #endif
 
-#define UA_access access
+#ifdef UA_ENABLE_DISCOVERY
+# ifndef UA_fileExists
+#  define UA_fileExists(X) (0) //file managing is not part of freeRTOS. If the system provides it, please define it before
+# endif // UA_fileExists
+#endif
 
 // No log colors on freeRTOS
 // #define UA_ENABLE_LOG_COLORS

+ 11 - 9
arch/common/ua_lwip.h

@@ -7,18 +7,20 @@
 #ifndef ARCH_COMMON_LWIP62541_H_
 #define ARCH_COMMON_LWIP62541_H_
 
-#define LWIP_TIMEVAL_PRIVATE 0
-#define LWIP_POSIX_SOCKETS_IO_NAMES 0
-#ifdef LWIP_COMPAT_SOCKETS
-#undef LWIP_COMPAT_SOCKETS
-#endif
-#define LWIP_COMPAT_SOCKETS 0
-
+/*
+ * Needed flags to be set before including this file. Normally done in lwipopts.h
+ * #define LWIP_COMPAT_SOCKETS 0 // Don't do name define-transformation in networking function names.
+ * #define LWIP_SOCKET 1 // Enable Socket API (normally already set)
+ * #define LWIP_DNS 1 // enable the lwip_getaddrinfo function, struct addrinfo and more.
+ * #define SO_REUSE 1 // Allows to set the socket as reusable
+ * #define LWIP_TIMEVAL_PRIVATE 0 // This is optional. Set this flag if you get a compilation error about redefinition of struct timeval
+ *
+ * Why not define these here? This stack is used as middleware so other code might use this header file with other flags (specially LWIP_COMPAT_SOCKETS)
+ */
 #include <lwip/tcpip.h>
 #include <lwip/netdb.h>
 #include <lwip/init.h>
 #include <lwip/sockets.h>
-#define sockaddr_storage sockaddr
 
 #define OPTVAL_TYPE int
 
@@ -72,7 +74,7 @@ unsigned int lwip_if_nametoindex(const char *ifname);
 # endif
 #endif
 
-int gethostname_lwip(char* name, size_t len);
+int gethostname_lwip(char* name, size_t len); //gethostname is not present in LwIP. We implement here a dummy. See ../freertosLWIP/ua_architecture_functions.c
 
 #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
 

+ 8 - 22
arch/freertosLWIP/ua_clock.c

@@ -9,37 +9,23 @@
 #ifdef UA_ARCHITECTURE_FREERTOSLWIP
 
 #include "ua_types.h"
-#include <time.h>
-#include <sys/time.h>
-
 #include <task.h>
 
+/* The current time in UTC time */
 UA_DateTime UA_DateTime_now(void) {
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    return (tv.tv_sec * UA_DATETIME_SEC) + (tv.tv_usec * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
+  UA_DateTime microSeconds = xTaskGetTickCount() * (1000000 / configTICK_RATE_HZ);
+  return ((microSeconds / 1000000) * UA_DATETIME_SEC) + ((microSeconds % 1000000) * UA_DATETIME_USEC) + UA_DATETIME_UNIX_EPOCH;
 }
 
-/* Credit to https://stackoverflow.com/questions/13804095/get-the-time-zone-gmt-offset-in-c */
+/* Offset between local time and UTC time */
 UA_Int64 UA_DateTime_localTimeUtcOffset(void) {
-    time_t gmt, rawtime = time(NULL);
-    struct tm *ptm;
-    struct tm gbuf;
-    ptm = gmtime_r(&rawtime, &gbuf);
-    // Request that mktime() looksup dst in timezone database
-    ptm->tm_isdst = -1;
-    gmt = mktime(ptm);
-    return (UA_Int64) (difftime(rawtime, gmt) * UA_DATETIME_SEC);
+  return 0; //TODO: this is set to zero since UA_DateTime_now() is just the local time, and not UTC.
 }
 
+/* CPU clock invariant to system time changes. Use only to measure durations,
+ * not absolute time. */
 UA_DateTime UA_DateTime_nowMonotonic(void) {
-    portTickType TaskTime = xTaskGetTickCount();
-    UA_DateTimeStruct UATime;
-    UATime.milliSec = (UA_UInt16) TaskTime;
-    struct timespec ts;
-    ts.tv_sec = UATime.milliSec/1000;
-    ts.tv_nsec = (UATime.milliSec % 1000)* 1000000;
-    return (ts.tv_sec * UA_DATETIME_SEC) + (ts.tv_nsec / 100);
+  return (xTaskGetTickCount() * 1000 / configTICK_RATE_HZ) * UA_DATETIME_MSEC;
 }
 
 #endif /* UA_ARCHITECTURE_FREERTOSLWIP */