Pārlūkot izejas kodu

move system-dependent clock functions into a plugin

Julius Pfrommer 8 gadi atpakaļ
vecāks
revīzija
1f98aa2e01
4 mainītis faili ar 60 papildinājumiem un 58 dzēšanām
  1. 1 3
      CMakeLists.txt
  2. 0 2
      doc/building.rst
  3. 59 0
      plugins/ua_clock.c
  4. 0 53
      src/ua_types.c

+ 1 - 3
CMakeLists.txt

@@ -124,9 +124,6 @@ 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_CLOCK)
-
 option(UA_ENABLE_EXTERNAL_NAMESPACES "Enable namespace handling by an external component (experimental)" OFF)
 mark_as_advanced(UA_ENABLE_EXTERNAL_NAMESPACES)
 
@@ -223,6 +220,7 @@ set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
                 ${PROJECT_SOURCE_DIR}/src/server/ua_services_subscription.c
                 ${PROJECT_SOURCE_DIR}/src/client/ua_client_highlevel_subscriptions.c
                 # plugins and dependencies
+                ${PROJECT_SOURCE_DIR}/plugins/ua_clock.c
                 ${PROJECT_SOURCE_DIR}/plugins/networklayer_tcp.c
                 ${PROJECT_SOURCE_DIR}/plugins/logger_stdout.c
                 ${PROJECT_SOURCE_DIR}/plugins/ua_config_standard.c

+ 0 - 2
doc/building.rst

@@ -142,8 +142,6 @@ This group contains build options related to the supported OPC UA features.
    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.

+ 59 - 0
plugins/ua_clock.c

@@ -0,0 +1,59 @@
+/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
+ * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
+
+#include "ua_types.h"
+
+#include <time.h>
+#if !defined(_WIN32) || defined(__MINGW32__)
+# include <sys/time.h>
+#endif
+
+#if defined(__APPLE__) || defined(__MACH__)
+# include <mach/clock.h>
+# include <mach/mach.h>
+#endif
+
+UA_DateTime UA_DateTime_now(void) {
+#if defined(_WIN32) && !defined(__MINGW32__)
+    /* Windows filetime has the same definition as UA_DateTime */
+    FILETIME ft;
+    SYSTEMTIME st;
+    GetSystemTime(&st);
+    SystemTimeToFileTime(&st, &ft);
+    ULARGE_INTEGER ul;
+    ul.LowPart = ft.dwLowDateTime;
+    ul.HighPart = ft.dwHighDateTime;
+    return (UA_DateTime)ul.QuadPart;
+#else
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    return (tv.tv_sec * UA_SEC_TO_DATETIME) + (tv.tv_usec * UA_USEC_TO_DATETIME) + UA_DATETIME_UNIX_EPOCH;
+#endif
+}
+
+UA_DateTime UA_DateTime_nowMonotonic(void) {
+#if defined(_WIN32)
+    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 */
+    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);
+#elif defined(__CYGWIN__) || !defined(CLOCK_MONOTONIC_RAW)
+    struct timespec ts;
+    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);
+    return (ts.tv_sec * UA_SEC_TO_DATETIME) + (ts.tv_nsec / 100);
+#endif
+}

+ 0 - 53
src/ua_types.c

@@ -51,59 +51,6 @@ 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) {
-#if defined(_WIN32) && !defined(__MINGW32__)
-    /* Windows filetime has the same definition as UA_DateTime */
-    FILETIME ft;
-    SYSTEMTIME st;
-    GetSystemTime(&st);
-    SystemTimeToFileTime(&st, &ft);
-    ULARGE_INTEGER ul;
-    ul.LowPart = ft.dwLowDateTime;
-    ul.HighPart = ft.dwHighDateTime;
-    return (UA_DateTime)ul.QuadPart;
-#else
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    return (tv.tv_sec * UA_SEC_TO_DATETIME) + (tv.tv_usec * UA_USEC_TO_DATETIME) + UA_DATETIME_UNIX_EPOCH;
-#endif
-}
-
-UA_DateTime UA_DateTime_nowMonotonic(void) {
-#if defined(_WIN32)
-    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 */
-    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);
-#elif defined(__CYGWIN__) || !defined(CLOCK_MONOTONIC_RAW)
-    struct timespec ts;
-    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);
-    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;