open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
ua_architecture.h
Go to the documentation of this file.
1/** This work is licensed under a Creative Commons CCZero 1.0 Universal License.
2 * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
3 *
4 * Copyright 2016-2017 (c) Julius Pfrommer, Fraunhofer IOSB
5 * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
6 */
7
8#ifdef UA_ARCHITECTURE_POSIX
9
10#ifndef PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
11#define PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_
12
14
15#include <errno.h>
16#include <arpa/inet.h>
17#include <netinet/in.h>
18#include <netdb.h>
19#include <sys/ioctl.h>
20#include <sys/select.h>
21#include <sys/types.h>
22#include <net/if.h>
23#include <poll.h>
24#ifdef UA_sleep_ms
25void UA_sleep_ms(unsigned long ms);
26#else
27# include <unistd.h>
28# define UA_sleep_ms(X) usleep(X * 1000)
29#endif
30
31#define OPTVAL_TYPE int
32
33#include <fcntl.h>
34#include <unistd.h> // read, write, close
35
36#ifdef __QNX__
37# include <sys/socket.h>
38#endif
39#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
40# include <sys/param.h>
41# if defined(BSD)
42# include<sys/socket.h>
43# endif
44#endif
45
46#include <netinet/tcp.h>
47
48/** unsigned int for windows and workaround to a glibc bug */
49/** Additionally if GNU_LIBRARY is not defined, it may be using
50 * musl libc (e.g. Docker Alpine) */
51#if defined(__OpenBSD__) || \
52 (defined(__GNU_LIBRARY__) && (__GNU_LIBRARY__ <= 6) && \
53 (__GLIBC__ <= 2) && (__GLIBC_MINOR__ < 16) || \
54 !defined(__GNU_LIBRARY__))
55# define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
56# define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
57#else
58# define UA_fd_set(fd, fds) FD_SET(fd, fds)
59# define UA_fd_isset(fd, fds) FD_ISSET(fd, fds)
60#endif
61
62#define UA_access access
63
64#define UA_IPV6 1
65#define UA_SOCKET int
66#define UA_INVALID_SOCKET -1
67#define UA_ERRNO errno
68#define UA_INTERRUPTED EINTR
69#define UA_AGAIN EAGAIN
70#define UA_EAGAIN EAGAIN
71#define UA_WOULDBLOCK EWOULDBLOCK
72#define UA_ERR_CONNECTION_PROGRESS EINPROGRESS
73
74#define UA_POLLIN POLLIN
75#define UA_POLLOUT POLLOUT
76
77#define UA_ENABLE_LOG_COLORS
78
79#define UA_poll poll
80#define UA_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
81 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
82#define UA_send send
83#define UA_recv recv
84#define UA_sendto sendto
85#define UA_recvfrom recvfrom
86#define UA_recvmsg recvmsg
87#define UA_htonl htonl
88#define UA_ntohl ntohl
89#define UA_close close
90#define UA_select select
91#define UA_shutdown shutdown
92#define UA_socket socket
93#define UA_bind bind
94#define UA_listen listen
95#define UA_accept accept
96#define UA_connect connect
97#define UA_getaddrinfo getaddrinfo
98#define UA_getsockopt getsockopt
99#define UA_setsockopt setsockopt
100#define UA_ioctl ioctl
101#define UA_freeaddrinfo freeaddrinfo
102#define UA_gethostname gethostname
103#define UA_getsockname getsockname
104#define UA_inet_pton inet_pton
105#if UA_IPV6
106# define UA_if_nametoindex if_nametoindex
107#endif
108
109/** Use the standard malloc */
110#include <stdlib.h>
111#ifndef UA_free
112# define UA_free free
113# define UA_malloc malloc
114# define UA_calloc calloc
115# define UA_realloc realloc
116#endif
117
118#include <stdio.h>
119#include <strings.h>
120#define UA_snprintf snprintf
121#define UA_strncasecmp strncasecmp
122
123#define UA_clean_errno(STR_FUN) (errno == 0 ? (char*) "None" : (STR_FUN)(errno))
124
125#define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
126 char *errno_str = UA_clean_errno(strerror); \
127 LOG; \
128 errno = 0; \
129}
130#define UA_LOG_SOCKET_ERRNO_GAI_WRAP(LOG) { \
131 const char *errno_str = UA_clean_errno(gai_strerror); \
132 LOG; \
133 errno = 0; \
134}
135
136#if UA_MULTITHREADING >= 100
137
138#include <pthread.h>
139
140typedef struct {
141 pthread_mutex_t mutex;
142 pthread_mutexattr_t mutexAttr;
143 int mutexCounter;
144} UA_Lock;
145
146static void
147UA_LOCK_INIT(UA_Lock *lock) {
148 pthread_mutexattr_init(&lock->mutexAttr);
149 pthread_mutexattr_settype(&lock->mutexAttr, PTHREAD_MUTEX_RECURSIVE);
150 pthread_mutex_init(&lock->mutex, &lock->mutexAttr);
151 lock->mutexCounter = 0;
152}
153
154static void
155UA_LOCK_DESTROY(UA_Lock *lock) {
156 pthread_mutex_destroy(&lock->mutex);
157 pthread_mutexattr_destroy(&lock->mutexAttr);
158}
159
160static void
161UA_LOCK(UA_Lock *lock) {
162 pthread_mutex_lock(&lock->mutex);
163 UA_assert(++(lock->mutexCounter) == 1);
164}
165
166static void
167UA_UNLOCK(UA_Lock *lock) {
168 UA_assert(--(lock->mutexCounter) == 0);
169 pthread_mutex_unlock(&lock->mutex);
170}
171
172static void
173UA_LOCK_ASSERT(UA_Lock *lock, int num) {
174 UA_assert(lock->mutexCounter == num);
175}
176#else
177#define UA_EMPTY_STATEMENT \
178 do { \
179 } while(0)
180#define UA_LOCK_INIT(lock) UA_EMPTY_STATEMENT
181#define UA_LOCK_DESTROY(lock) UA_EMPTY_STATEMENT
182#define UA_LOCK(lock) UA_EMPTY_STATEMENT
183#define UA_UNLOCK(lock) UA_EMPTY_STATEMENT
184#define UA_LOCK_ASSERT(lock, num) UA_EMPTY_STATEMENT
185#endif
186
188
189#if defined(__APPLE__) && defined(_SYS_QUEUE_H_)
190// in some compilers there's already a _SYS_QUEUE_H_ which is included first and doesn't
191// have all functions
192
193#undef SLIST_HEAD
194#undef SLIST_HEAD_INITIALIZER
195#undef SLIST_ENTRY
196#undef SLIST_FIRST
197#undef SLIST_END
198#undef SLIST_EMPTY
199#undef SLIST_NEXT
200#undef SLIST_FOREACH
201#undef SLIST_FOREACH_SAFE
202#undef SLIST_INIT
203#undef SLIST_INSERT_AFTER
204#undef SLIST_INSERT_HEAD
205#undef SLIST_REMOVE_AFTER
206#undef SLIST_REMOVE_HEAD
207#undef SLIST_REMOVE
208#undef LIST_HEAD
209#undef LIST_HEAD_INITIALIZER
210#undef LIST_ENTRY
211#undef LIST_FIRST
212#undef LIST_END
213#undef LIST_EMPTY
214#undef LIST_NEXT
215#undef LIST_FOREACH
216#undef LIST_FOREACH_SAFE
217#undef LIST_INIT
218#undef LIST_INSERT_AFTER
219#undef LIST_INSERT_BEFORE
220#undef LIST_INSERT_HEAD
221#undef LIST_REMOVE
222#undef LIST_REPLACE
223#undef SIMPLEQ_HEAD
224#undef SIMPLEQ_HEAD_INITIALIZER
225#undef SIMPLEQ_ENTRY
226#undef SIMPLEQ_FIRST
227#undef SIMPLEQ_END
228#undef SIMPLEQ_EMPTY
229#undef SIMPLEQ_NEXT
230#undef SIMPLEQ_FOREACH
231#undef SIMPLEQ_FOREACH_SAFE
232#undef SIMPLEQ_INIT
233#undef SIMPLEQ_INSERT_HEAD
234#undef SIMPLEQ_INSERT_TAIL
235#undef SIMPLEQ_INSERT_AFTER
236#undef SIMPLEQ_REMOVE_HEAD
237#undef SIMPLEQ_REMOVE_AFTER
238#undef XSIMPLEQ_HEAD
239#undef XSIMPLEQ_ENTRY
240#undef XSIMPLEQ_XOR
241#undef XSIMPLEQ_FIRST
242#undef XSIMPLEQ_END
243#undef XSIMPLEQ_EMPTY
244#undef XSIMPLEQ_NEXT
245#undef XSIMPLEQ_FOREACH
246#undef XSIMPLEQ_FOREACH_SAFE
247#undef XSIMPLEQ_INIT
248#undef XSIMPLEQ_INSERT_HEAD
249#undef XSIMPLEQ_INSERT_TAIL
250#undef XSIMPLEQ_INSERT_AFTER
251#undef XSIMPLEQ_REMOVE_HEAD
252#undef XSIMPLEQ_REMOVE_AFTER
253#undef TAILQ_HEAD
254#undef TAILQ_HEAD_INITIALIZER
255#undef TAILQ_ENTRY
256#undef TAILQ_FIRST
257#undef TAILQ_END
258#undef TAILQ_NEXT
259#undef TAILQ_LAST
260#undef TAILQ_PREV
261#undef TAILQ_EMPTY
262#undef TAILQ_FOREACH
263#undef TAILQ_FOREACH_SAFE
264#undef TAILQ_FOREACH_REVERSE
265#undef TAILQ_FOREACH_REVERSE_SAFE
266#undef TAILQ_INIT
267#undef TAILQ_INSERT_HEAD
268#undef TAILQ_INSERT_TAIL
269#undef TAILQ_INSERT_AFTER
270#undef TAILQ_INSERT_BEFORE
271#undef TAILQ_REMOVE
272#undef TAILQ_REPLACE
273#undef CIRCLEQ_HEAD
274#undef CIRCLEQ_HEAD_INITIALIZER
275#undef CIRCLEQ_ENTRY
276#undef CIRCLEQ_FIRST
277#undef CIRCLEQ_LAST
278#undef CIRCLEQ_END
279#undef CIRCLEQ_NEXT
280#undef CIRCLEQ_PREV
281#undef CIRCLEQ_EMPTY
282#undef CIRCLEQ_FOREACH
283#undef CIRCLEQ_FOREACH_SAFE
284#undef CIRCLEQ_FOREACH_REVERSE
285#undef CIRCLEQ_FOREACH_REVERSE_SAFE
286#undef CIRCLEQ_INIT
287#undef CIRCLEQ_INSERT_AFTER
288#undef CIRCLEQ_INSERT_BEFORE
289#undef CIRCLEQ_INSERT_HEAD
290#undef CIRCLEQ_INSERT_TAIL
291#undef CIRCLEQ_REMOVE
292#undef CIRCLEQ_REPLACE
293
294#undef _SYS_QUEUE_H_
295
296#endif /* defined(__APPLE__) && defined(_SYS_QUEUE_H_) */
297
298#endif /* PLUGINS_ARCH_POSIX_UA_ARCHITECTURE_H_ */
299
300#endif /* UA_ARCHITECTURE_POSIX */
#define UA_assert(ignore)
#define UA_sleep_ms(X)
This work is licensed under a Creative Commons CCZero 1.0 Universal License.
Definition ua_freeRTOS.h:17