Quellcode durchsuchen

add an option to use external timing API for embedded targets;
clean up time handling

Julius Pfrommer vor 8 Jahren
Ursprung
Commit
2f1c8f8649
5 geänderte Dateien mit 26 neuen und 10 gelöschten Zeilen
  1. 3 0
      CMakeLists.txt
  2. 4 0
      doc/building.rst
  3. 3 1
      include/ua_config.h.in
  4. 15 6
      src/ua_types.c
  5. 1 3
      src/ua_util.h

+ 3 - 0
CMakeLists.txt

@@ -125,6 +125,9 @@ option(UA_ENABLE_GENERATE_NAMESPACE0 "Generate and load UA XML Namespace 0 defin
 option(UA_ENABLE_EMBEDDED_LIBC "Target has no libc, use internal definitions" OFF)
 mark_as_advanced(UA_ENABLE_EMBEDDED_LIBC)
 
+option(UA_ENABLE_EMBEDDED_CLOCK "Target has no standard time APIs (link custom implementation)" OFF)
+mark_as_advanced(UA_ENABLE_EMBEDDED_LIBC)
+
 option(UA_ENABLE_EXTERNAL_NAMESPACES "Enable namespace handling by an external component (experimental)" OFF)
 mark_as_advanced(UA_ENABLE_EXTERNAL_NAMESPACES)
 

+ 4 - 0
doc/building.rst

@@ -140,6 +140,10 @@ This group contains build options related to the supported OPC UA features.
    Enable multi-threading support (experimental)
 **UA_ENABLE_COVERAGE**
    Measure the coverage of unit tests
+**UA_ENABLE_EMBEDDED_LIBC**
+   Use a custom implementation of some libc functions that might be missing on embedded targets (e.g. string handling).
+**UA_ENABLE_EMBEDDED_CLOCK**
+   Do not implement the time-handling functions where no standard API can be used on embedded targets. Instead, link in a custom implementation.
 
 Some options are marked as advanced. The advanced options need to be toggled to
 be visible in the cmake GUIs.

+ 3 - 1
include/ua_config.h.in

@@ -32,11 +32,13 @@ extern "C" {
 #cmakedefine UA_ENABLE_METHODCALLS
 #cmakedefine UA_ENABLE_SUBSCRIPTIONS
 #cmakedefine UA_ENABLE_TYPENAMES
-#cmakedefine UA_ENABLE_EMBEDDED_LIBC
 #cmakedefine UA_ENABLE_GENERATE_NAMESPACE0
 #cmakedefine UA_ENABLE_EXTERNAL_NAMESPACES
 #cmakedefine UA_ENABLE_NODEMANAGEMENT
 
+#cmakedefine UA_ENABLE_EMBEDDED_LIBC
+#cmakedefine UA_ENABLE_EMBEDDED_CLOCK
+
 #cmakedefine UA_ENABLE_NONSTANDARD_UDP
 #cmakedefine UA_ENABLE_NONSTANDARD_STATELESS
 

+ 15 - 6
src/ua_types.c

@@ -51,8 +51,14 @@ UA_Boolean UA_String_equal(const UA_String *string1, const UA_String *string2) {
 }
 
 /* DateTime */
+
+/* Implement UA_DateTime_now and UA_DateTime_nowMonotonic outside of the
+   library. This becomes necessary on embedded targets when the POSIX/Windows
+   API is not available. */
+#ifndef UA_ENABLE_EMBEDDED_CLOCK
+
 UA_DateTime UA_DateTime_now(void) {
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(__MINGW32__)
     /* Windows filetime has the same definition as UA_DateTime */
     FILETIME ft;
     SYSTEMTIME st;
@@ -70,31 +76,34 @@ UA_DateTime UA_DateTime_now(void) {
 }
 
 UA_DateTime UA_DateTime_nowMonotonic(void) {
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(__MINGW32__)
     LARGE_INTEGER freq, ticks;
     QueryPerformanceFrequency(&freq);
     QueryPerformanceCounter(&ticks);
     UA_Double ticks2dt = UA_SEC_TO_DATETIME;
     ticks2dt /= freq.QuadPart;
     return (UA_DateTime)(ticks.QuadPart * ticks2dt);
-#elif defined(__APPLE__) || defined(__MACH__) // OS X does not have clock_gettime, use clock_get_time
+#elif defined(__APPLE__) || defined(__MACH__)
+    /* OS X does not have clock_gettime, use clock_get_time */
     clock_serv_t cclock;
     mach_timespec_t mts;
     host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
     clock_get_time(cclock, &mts);
     mach_port_deallocate(mach_task_self(), cclock);
     return (mts.tv_sec * UA_SEC_TO_DATETIME) + (mts.tv_nsec / 100);
-#else
+#elif defined(__CYGWIN__)
     struct timespec ts;
-#ifdef __CYGWIN__    
     clock_gettime(CLOCK_MONOTONIC, &ts);
+    return (ts.tv_sec * UA_SEC_TO_DATETIME) + (ts.tv_nsec / 100);
 #else
+    struct timespec ts;
     clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
-#endif
     return (ts.tv_sec * UA_SEC_TO_DATETIME) + (ts.tv_nsec / 100);
 #endif
 }
 
+#endif
+
 UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime t) {
     /* Calculating the the milli-, micro- and nanoseconds */
     UA_DateTimeStruct dateTimeStruct;

+ 1 - 3
src/ua_util.h

@@ -70,9 +70,7 @@
 #endif
 
 #include <time.h>
-#if defined(_WIN32) && !defined(__MINGW32__)
-int gettimeofday(struct timeval *tp, struct timezone *tzp);
-#else
+#if !defined(_WIN32) || defined(__MINGW32__)
 # include <sys/time.h>
 #endif