ua_architecture.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 2018 (c) Stephan Kantelberg
  5. */
  6. #ifdef UA_ARCHITECTURE_WEC7
  7. #ifndef PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_
  8. #define PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_
  9. #include <open62541/architecture_base.h>
  10. #ifndef _BSD_SOURCE
  11. # define _BSD_SOURCE
  12. #endif
  13. /* Disable some security warnings on MSVC */
  14. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  15. # define _CRT_SECURE_NO_WARNINGS
  16. #endif
  17. #include "stdint.h"
  18. #ifdef UNDER_CE
  19. #define MAX_STRERROR 31
  20. static char *errorStrings[]= {"Error 0","","No such file or directory","","","","","Arg list too long",
  21. "Exec format error","Bad file number","","","Not enough core","Permission denied","","",
  22. "","File exists","Cross-device link","","","","Invalid argument","","Too many open files",
  23. "","","","No space left on device","","","","","Math argument","Result too large","",
  24. "Resource deadlock would occur", "Unknown error under wince"};
  25. char *strerror(int errnum);
  26. #endif
  27. #include <stdlib.h>
  28. #if defined(_WIN32)
  29. # include <malloc.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <winsock2.h>
  34. #include <windows.h>
  35. #include <ws2tcpip.h>
  36. #ifdef _MSC_VER
  37. # ifndef UNDER_CE
  38. # include <io.h> //access
  39. # define UA_access _access
  40. # endif
  41. #else
  42. # include <unistd.h> //access and tests
  43. # define UA_access access
  44. #endif
  45. #define ssize_t int
  46. #define OPTVAL_TYPE char
  47. #ifndef UA_sleep_ms
  48. # define UA_sleep_ms(X) Sleep(X)
  49. #endif
  50. // Windows does not support ansi colors
  51. // #define UA_ENABLE_LOG_COLORS
  52. #if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  53. # define UA_SOCKET int
  54. # define UA_INVALID_SOCKET -1
  55. #else
  56. # define UA_SOCKET SOCKET
  57. # define UA_INVALID_SOCKET INVALID_SOCKET
  58. #endif
  59. #define UA_ERRNO WSAGetLastError()
  60. #define UA_INTERRUPTED WSAEINTR
  61. #define UA_AGAIN WSAEWOULDBLOCK
  62. #define UA_EAGAIN EAGAIN
  63. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  64. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  65. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  66. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  67. #ifdef UNDER_CE
  68. #define UA_ERRNO WSAGetLastError()
  69. #endif
  70. #define UA_getnameinfo getnameinfo
  71. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  72. #define UA_recv recv
  73. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  74. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  75. #define UA_htonl htonl
  76. #define UA_ntohl ntohl
  77. #define UA_close closesocket
  78. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  79. #define UA_shutdown shutdown
  80. #define UA_socket socket
  81. #define UA_bind bind
  82. #define UA_listen listen
  83. #define UA_accept accept
  84. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  85. #define UA_getaddrinfo getaddrinfo
  86. #define UA_getsockopt getsockopt
  87. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  88. #define UA_freeaddrinfo freeaddrinfo
  89. #define UA_gethostname gethostname
  90. #define UA_getsockname getsockname
  91. #define UA_inet_pton InetPton
  92. #ifdef maxStringLength //defined in mingw64
  93. # undef maxStringLength
  94. #endif
  95. #ifndef UA_free
  96. #define UA_free free
  97. #endif
  98. #ifndef UA_malloc
  99. #define UA_malloc malloc
  100. #endif
  101. #ifndef UA_calloc
  102. #define UA_calloc calloc
  103. #endif
  104. #ifndef UA_realloc
  105. #define UA_realloc realloc
  106. #endif
  107. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  108. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  109. LPVOID errno_str = NULL; \
  110. FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  111. NULL, WSAGetLastError(), \
  112. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  113. (LPWSTR)&errno_str, \
  114. 0, NULL); \
  115. LOG; \
  116. LocalFree(errno_str); \
  117. }
  118. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  119. #if UA_MULTITHREADING >= 100
  120. #error Multithreading unsupported
  121. #else
  122. #define UA_LOCK_TYPE_NAME
  123. #define UA_LOCK_TYPE(mutexName)
  124. #define UA_LOCK_TYPE_POINTER(mutexName)
  125. #define UA_LOCK_INIT(mutexName)
  126. #define UA_LOCK_DESTROY(mutexName)
  127. #define UA_LOCK(mutexName)
  128. #define UA_UNLOCK(mutexName)
  129. #endif
  130. #include <open62541/architecture_functions.h>
  131. /* Fix redefinition of SLIST_ENTRY on mingw winnt.h */
  132. #if !defined(_SYS_QUEUE_H_) && defined(SLIST_ENTRY)
  133. # undef SLIST_ENTRY
  134. #endif
  135. #endif /* PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_ */
  136. #endif /* UA_ARCHITECTURE_WEC7 */