Przeglądaj źródła

move POSIX definitions out of the public header

Julius Pfrommer 7 lat temu
rodzic
commit
67dbb4cf18

+ 14 - 3
examples/client_connect_loop.c

@@ -1,6 +1,19 @@
 /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
 
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+
 /**
  * Client disconnect handling
  * --------------------------
@@ -8,9 +21,7 @@
  * is shut down while the client is connected. You just need to call connect
  * again and the client will automatically reconnect.
  *
- * This example is very similar to the tutorial_client_firststeps.c
- */
-
+ * This example is very similar to the tutorial_client_firststeps.c. */
 
 #include "open62541.h"
 #include <signal.h>

+ 36 - 84
include/ua_config.h.in

@@ -10,8 +10,8 @@ extern "C" {
 #endif
 
 /**
- * Library Version
- * --------------- */
+ * open62541 Version
+ * ----------------- */
 #define UA_OPEN62541_VER_MAJOR ${OPEN62541_VER_MAJOR}
 #define UA_OPEN62541_VER_MINOR ${OPEN62541_VER_MINOR}
 #define UA_OPEN62541_VER_PATCH ${OPEN62541_VER_PATCH}
@@ -19,17 +19,16 @@ extern "C" {
 #define UA_OPEN62541_VER_COMMIT "${OPEN62541_VER_COMMIT}"
 
 /**
- * Options
- * ------- */
+ * Feature Options
+ * ---------------
+ * Changing the feature options has no effect on a pre-compiled library. */
 #define UA_LOGLEVEL ${UA_LOGLEVEL}
 #cmakedefine UA_ENABLE_METHODCALLS
 #cmakedefine UA_ENABLE_NODEMANAGEMENT
 #cmakedefine UA_ENABLE_SUBSCRIPTIONS
 #cmakedefine UA_ENABLE_MULTITHREADING
 
-/**
- * Advanced Options
- * ---------------- */
+/* Advanced Options */
 #cmakedefine UA_ENABLE_STATUSCODE_DESCRIPTIONS
 #cmakedefine UA_ENABLE_TYPENAMES
 #cmakedefine UA_ENABLE_EMBEDDED_LIBC
@@ -42,54 +41,36 @@ extern "C" {
 #cmakedefine UA_DEBUG_DUMP_PKGS
 
 /**
- * Standard Includes
- * ----------------- */
-#if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
-# define _XOPEN_SOURCE 600
-#endif
-#ifndef _DEFAULT_SOURCE
-# define _DEFAULT_SOURCE
-#endif
-// On older systems we need to define _BSD_SOURCE
-// _DEFAULT_SOURCE is an alias for that
-#ifndef _BSD_SOURCE
-# define _BSD_SOURCE
-#endif
-
-// Disable deprecation warnings for sprintf, strncpy, strerror
-#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
-# define _CRT_SECURE_NO_WARNINGS
-#endif
-
-#include <stddef.h>
-
-/* Include stdint.h and stdbool.h or workaround for older Visual Studios */
-#if !defined(_MSC_VER) || _MSC_VER >= 1600
-# include <stdint.h>
-# include <stdbool.h> /* C99 Boolean */
-#else
-# include "ms_stdint.h"
-#if !(defined(__bool_true_false_are_defined))
-#define bool short
-#define true 1
-#define false 0
-#define __bool_true_false_are_defined
-#endif
-#endif
-
-/**
- * Standard Macros
- * --------------- */
-
-/* Assert */
+ * Assertions
+ * ---------- */
 #include <assert.h>
 #define UA_assert(ignore) assert(ignore)
 
+/* Outputs an error message at compile time if the assert fails.
+ * Example usage:
+ * UA_STATIC_ASSERT(sizeof(long)==7, use_another_compiler_luke)
+ * See: https://stackoverflow.com/a/4815532/869402 */
+#if defined(__cplusplus) && __cplusplus >= 201103L /* C++11 or above */
+# define UA_STATIC_ASSERT(cond,msg) static_assert(cond, #msg)
+#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or above */
+# define UA_STATIC_ASSERT(cond,msg) _Static_assert(cond, #msg)
+#elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) /* GCC, Clang, MSC */
+# define UA_CTASTR2(pre,post) pre ## post
+# define UA_CTASTR(pre,post) UA_CTASTR2(pre,post)
+# define UA_STATIC_ASSERT(cond,msg)                             \
+    typedef struct {                                            \
+        int UA_CTASTR(static_assertion_failed_,msg) : !!(cond); \
+    } UA_CTASTR(static_assertion_failed_,__COUNTER__)
+#else /* Everybody else */
+# define UA_STATIC_ASSERT(cond,msg) typedef char static_assertion_##msg[(cond)?1:-1]
+#endif
+
 /**
  * Memory Management
- * ---------------
- * The default malloc implementation from ``stdlib.h`` is used internally.
- * Override if required. */
+ * -----------------
+ * The default is to use the malloc implementation from ``stdlib.h``. Override
+ * if required. Changing the settings has no effect on a pre-compiled
+ * library. */
 #include <stdlib.h>
 #if defined(_WIN32) && !defined(__clang__)
 # include <malloc.h>
@@ -116,6 +97,7 @@ extern "C" {
  * order to export symbols for a DLL. Define ``UA_DYNAMIC_LINKING`` only to
  * import symbols from a DLL.*/
 #cmakedefine UA_DYNAMIC_LINKING
+
 #if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
 # ifdef UA_DYNAMIC_LINKING_EXPORT /* export dll */
 #  ifdef __GNUC__
@@ -185,8 +167,8 @@ extern "C" {
 #endif
 
 /**
- * String Manipulation
- * -------------------
+ * Basic Libc Definitions
+ * ------------------------
  * The header ``string.h`` is defined in the C-standard. If no libc is provided
  * (e.g. on some embedded target), use the following definitions and the
  * implementation in ``/deps/libc_string.c`` */
@@ -200,8 +182,8 @@ extern "C" {
 #endif
 
 /**
- * Binary Encoding Overlays
- * ------------------------
+ * Detect Binary Overlaying for Encoding
+ * -------------------------------------
  * Integers and floating point numbers are transmitted in little-endian (IEEE 754
  * for floating point) encoding. If the target architecture uses the same
  * format, numeral datatypes can be memcpy'd (overlayed) on the binary stream.
@@ -276,36 +258,6 @@ extern "C" {
 # define UA_BINARY_OVERLAYABLE_FLOAT 0
 #endif
 
-/**
- * Utility Definitions
- * ^^^^^^^^^^^^^^^^^^^ */
-/* Outputs an error message at compile time if the assert fails.
- * Example usage:
- * UA_STATIC_ASSERT(sizeof(long)==7, use_another_compiler_luke)
- * See: https://stackoverflow.com/a/4815532/869402 */
-#if defined(__cplusplus) && __cplusplus >= 201103L /* C++11 or above */
-# define UA_STATIC_ASSERT(cond,msg) static_assert(cond, #msg)
-#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or above */
-# define UA_STATIC_ASSERT(cond,msg) _Static_assert(cond, #msg)
-#elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) /* GCC, Clang, MSC */
-# define UA_CTASTR2(pre,post) pre ## post
-# define UA_CTASTR(pre,post) UA_CTASTR2(pre,post)
-# define UA_STATIC_ASSERT(cond,msg)                             \
-    typedef struct {                                            \
-        int UA_CTASTR(static_assertion_failed_,msg) : !!(cond); \
-    } UA_CTASTR(static_assertion_failed_,__COUNTER__)
-#else /* Everybody else */
-# define UA_STATIC_ASSERT(cond,msg) typedef char static_assertion_##msg[(cond)?1:-1]
-#endif
-
-/* To generate smaller binaries, descriptive strings can be excluded from the
- * datatype description */
-#ifdef UA_ENABLE_TYPENAMES
-# define UA_TYPENAME(name) name,
-#else
-# define UA_TYPENAME(name)
-#endif
-
 #ifdef __cplusplus
 } // extern "C"
 #endif

+ 26 - 2
include/ua_types.h

@@ -9,6 +9,22 @@
 extern "C" {
 #endif
 
+#include <stddef.h>
+
+/* Include stdint.h and stdbool.h or workaround for older Visual Studios */
+#if !defined(_MSC_VER) || _MSC_VER >= 1600
+# include <stdint.h>
+# include <stdbool.h> /* C99 Boolean */
+#else
+# include "ms_stdint.h"
+# if !defined(__bool_true_false_are_defined)
+#  define bool short
+#  define true 1
+#  define false 0
+#  define __bool_true_false_are_defined
+# endif
+#endif
+
 #include "ua_config.h"
 #include "ua_constants.h"
 
@@ -734,8 +750,7 @@ typedef struct UA_DiagnosticInfo {
  *   memory for the data type itself.
  *
  * Specializations, such as ``UA_Int32_new()`` are derived from the generic
- * type operations as static inline functions.
- */
+ * type operations as static inline functions. */
 
 typedef struct {
 #ifdef UA_ENABLE_TYPENAMES
@@ -775,10 +790,19 @@ struct UA_DataType {
     UA_DataTypeMember *members;
 };
 
+/* The following is used to exclude type names in the definition of UA_DataType
+ * structures if the feature is disabled. */
+#ifdef UA_ENABLE_TYPENAMES
+# define UA_TYPENAME(name) name,
+#else
+# define UA_TYPENAME(name)
+#endif
+
 /**
  * Builtin data types can be accessed as UA_TYPES[UA_TYPES_XXX], where XXX is
  * the name of the data type. If only the NodeId of a type is known, use the
  * following method to retrieve the data type description. */
+
 /* Returns the data type description for the type's identifier or NULL if no
  * matching data type was found. */
 const UA_DataType UA_EXPORT *

+ 14 - 1
plugins/ua_clock.c

@@ -1,7 +1,18 @@
 /* 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"
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
 
 #include <time.h>
 #ifdef _WIN32
@@ -27,6 +38,8 @@
 # include <mach/mach.h>
 #endif
 
+#include "ua_types.h"
+
 UA_DateTime UA_DateTime_now(void) {
 #if defined(_WIN32)
     /* Windows filetime has the same definition as UA_DateTime */

+ 19 - 1
plugins/ua_network_tcp.c

@@ -1,8 +1,26 @@
 /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
 
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+
+/* Disable some security warnings on MSVC */
+#ifdef _MSC_VER
+# define _CRT_SECURE_NO_WARNINGS
+#endif
+
+/* Assume that Windows versions are newer than Windows XP */
 #if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
-/* Assume the target is newer than Windows XP */
 # undef WINVER
 # undef _WIN32_WINDOWS
 # undef _WIN32_WINNT

+ 13 - 0
src/server/ua_mdns.c

@@ -2,6 +2,19 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+
 #include "ua_server_internal.h"
 #include "ua_mdns_internal.h"
 #include "ua_util.h"

+ 13 - 0
src/server/ua_services_discovery_multicast.c

@@ -2,6 +2,19 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+
 #include "ua_server_internal.h"
 #include "ua_services.h"
 #include "ua_mdns_internal.h"

+ 28 - 21
src/ua_types_encoding_binary.c

@@ -7,20 +7,9 @@
 #include "ua_types_generated.h"
 #include "ua_types_generated_handling.h"
 
-/* Type Encoding
- * -------------
- * This file contains encoding functions for the builtin data types and generic
- * functions that operate on all types and arrays. This requires the type
- * description from a UA_DataType structure. Note that some internal (static)
- * deocidng functions may abort and leave the type in an inconsistent state. But
- * this is always handled in UA_decodeBinary, where the error is caught and the
- * type cleaned up.
- *
- * Breaking a message into chunks is integrated with the encoding. When the end
- * of a buffer is reached, a callback is executed that sends the current buffer
- * as a chunk and exchanges the encoding buffer "underneath" the ongoing
- * encoding. This enables fast sending of large messages as spurious copying is
- * avoided. */
+/**
+ * Throw a warning if numeric types cannot be overlayed
+ * ---------------------------------------------------- */
 
 #if defined(__clang__)
 # pragma GCC diagnostic push
@@ -28,19 +17,35 @@
 #endif
 
 #ifndef UA_BINARY_OVERLAYABLE_INTEGER
-# warning Integer endianness could not be detected to be little endian. Use slow generic encoding.
+# warning Integer endianness could not be detected to be little endian. \
+    Use slow generic encoding.
 #endif
 
-/* There is no robust way to detect float endianness in clang. This warning can be removed
- * if the target is known to be little endian with floats in the IEEE 754 format. */
+/* There is no robust way to detect float endianness in clang. This warning can
+ * be removed if the target is known to be little endian with floats in the IEEE
+ * 754 format. */
 #ifndef UA_BINARY_OVERLAYABLE_FLOAT
-# warning Float endianness could not be detected to be little endian in the IEEE 754 format. Use slow generic encoding.
+# warning Float endianness could not be detected to be little endian in the IEEE \
+    754 format. Use slow generic encoding.
 #endif
 
 #if defined(__clang__)
 # pragma GCC diagnostic pop
 #endif
 
+/**
+ * Type Encoding and Decoding
+ * --------------------------
+ * The following methods contain encoding and decoding functions for the builtin
+ * data types and generic functions that operate on all types and arrays. This
+ * requires the type description from a UA_DataType structure.
+ *
+ * Breaking a message into chunks is integrated with the encoding. When the end
+ * of a buffer is reached, a callback is executed that sends the current buffer
+ * as a chunk and exchanges the encoding buffer "underneath" the ongoing
+ * encoding. This enables fast sending of large messages as spurious copying is
+ * avoided. */
+
 /* Jumptables for de-/encoding and computing the buffer length. The methods in
  * the decoding jumptable do not all clean up their allocated memory when an
  * error occurs. So a final _deleteMembers needs to be called before returning
@@ -1524,9 +1529,11 @@ UA_decodeBinary(const UA_ByteString *src, size_t *offset, void *dst,
     return ret;
 }
 
-/******************/
-/* CalcSizeBinary */
-/******************/
+/**
+ * Compute the Message Size
+ * ------------------------
+ * The following methods are used to compute the length of a datum in binary
+ * encoding. */
 
 static size_t
 Array_calcSizeBinary(const void *src, size_t length, const UA_DataType *type) {

+ 18 - 0
tools/amalgamate.py

@@ -53,6 +53,24 @@ if is_c:
 # define MDNSD_DYNAMIC_LINKING
 #endif
 
+/* Enable POSIX features */
+#ifndef _XOPEN_SOURCE
+# define _XOPEN_SOURCE 600
+#endif
+#ifndef _DEFAULT_SOURCE
+# define _DEFAULT_SOURCE
+#endif
+/* On older systems we need to define _BSD_SOURCE.
+ * _DEFAULT_SOURCE is an alias for that. */
+#ifndef _BSD_SOURCE
+# define _BSD_SOURCE
+#endif
+
+/* Disable security warnings for BSD sockets on MSVC */
+#ifdef _MSC_VER
+# define _CRT_SECURE_NO_WARNINGS
+#endif
+
 #include "%s.h"
 ''' % outname)
 else: