Browse Source

Isolate RTU and TCP headers and simplify Win32 detection

- move termios header in RTU backend
- move TCP headers in TCP backend
- use native serial on Win32 for Cygwin
- avoid too many defines
Stéphane Raimbault 14 years ago
parent
commit
f410aea2f7
8 changed files with 65 additions and 63 deletions
  1. 4 6
      configure.ac
  2. 2 1
      src/Makefile.am
  3. 9 8
      src/modbus-rtu-private.h
  4. 8 9
      src/modbus-rtu.c
  5. 29 14
      src/modbus-tcp.c
  6. 9 0
      src/modbus-tcp.h
  7. 1 24
      src/modbus.h
  8. 3 1
      tests/bandwidth-server-many-up.c

+ 4 - 6
configure.ac

@@ -47,15 +47,13 @@ LIBMODBUS_LT_VERSION_INFO=$LIBMODBUS_LD_CURRENT:$LIBMODBUS_LD_REVISION:$LIBMODBU
 AC_SUBST(LIBMODBUS_LT_VERSION_INFO)
 
 # Check whether we are building for Win32
-build_win32="false"
-case "${host}" in
+os_win32="false"
+case "${target}" in
       *mingw32)
-		AC_DEFINE([BUILD_WIN32], [], [Build libmodbus for Win32])
-		build_win32="true"
+		os_win32="true"
 	;;
 esac
-
-AM_CONDITIONAL(BUILD_WIN32, test "$build_win32" = "true")
+AM_CONDITIONAL(OS_WIN32, test "$os_win32" = "true")
 
 # Checks for programs.
 AC_PROG_CC

+ 2 - 1
src/Makefile.am

@@ -11,7 +11,8 @@ libmodbus_la_SOURCES = \
         modbus-tcp.h \
         modbus-tcp-private.h \
         modbus-version.h
-if BUILD_WIN32
+
+if OS_WIN32
 libmodbus_la_LIBADD = -lwsock32
 endif
 

+ 9 - 8
src/modbus-rtu-private.h

@@ -19,7 +19,12 @@
 #define _MODBUS_RTU_PRIVATE_H_
 
 #include <stdint.h>
+
+#if defined(_WIN32)
+#include <windows.h>
+#else
 #include <termios.h>
+#endif
 
 #define _MODBUS_RTU_HEADER_LENGTH      1
 #define _MODBUS_RTU_PRESET_REQ_LENGTH  6
@@ -27,11 +32,7 @@
 
 #define _MODBUS_RTU_CHECKSUM_LENGTH    2
 
-#ifdef NATIVE_WIN32
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
+#if defined(_WIN32)
 /* WIN32: struct containing serial handle and a receive buffer */
 #define PY_BUF_SIZE 512
 struct win32_ser {
@@ -42,7 +43,7 @@ struct win32_ser {
 	/* Received chars */
 	DWORD n_bytes;
 };
-#endif /* NATIVE_WIN32 */
+#endif /* _WIN32 */
 
 typedef struct _modbus_rtu {
     /* Device: "/dev/ttyS0", "/dev/ttyUSB0" or "/dev/tty.USA19*" on Mac OS X for
@@ -50,7 +51,7 @@ typedef struct _modbus_rtu {
        as the directory+file name was bigger than 19 bytes. Making it 67 bytes
        for now, but OS X does support 256 byte file names. May become a problem
        in the future. */
-#ifdef __APPLE_CC__
+#if defined(__APPLE_CC__)
     char device[64];
 #else
     char device[16];
@@ -63,7 +64,7 @@ typedef struct _modbus_rtu {
     uint8_t stop_bit;
     /* Parity: 'N', 'O', 'E' */
     char parity;
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     struct win32_ser w_ser;
     DCB old_dcb;
 #else

+ 8 - 9
src/modbus-rtu.c

@@ -22,7 +22,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "modbus.h"
 #include "modbus-private.h"
 
 #include "modbus-rtu.h"
@@ -156,7 +155,7 @@ int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
     return req_length;
 }
 
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
 /* This simple implementation is sort of a substitute of the select() call, working
  * this way: the win32_ser_select() call tries to read some data from the serial port,
  * setting the timeout as the select() call would. Data read is stored into the
@@ -233,7 +232,7 @@ static int win32_ser_read(struct win32_ser *ws, uint8_t *p_msg, unsigned int max
 
 ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
 {
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
     DWORD n_bytes = 0;
     return (WriteFile(ctx_rtu->w_ser.fd, req, req_length, &n_bytes, NULL)) ? n_bytes : -1;
@@ -244,7 +243,7 @@ ssize_t _modbus_rtu_send(modbus_t *ctx, const uint8_t *req, int req_length)
 
 ssize_t _modbus_rtu_recv(modbus_t *ctx, uint8_t *rsp, int rsp_length)
 {
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     return win32_ser_read(&((modbus_rtu_t *)ctx->backend_data)->w_ser, rsp, rsp_length);
 #else
     return read(ctx->s, rsp, rsp_length);
@@ -283,7 +282,7 @@ int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,
 /* Sets up a serial port for RTU communications */
 static int _modbus_rtu_connect(modbus_t *ctx)
 {
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     DCB dcb;
 #else
     struct termios tios;
@@ -297,7 +296,7 @@ static int _modbus_rtu_connect(modbus_t *ctx)
                ctx_rtu->data_bit, ctx_rtu->stop_bit);
     }
 
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     /* Some references here:
      * http://msdn.microsoft.com/en-us/library/aa450602.aspx
      */
@@ -681,7 +680,7 @@ void _modbus_rtu_close(modbus_t *ctx)
     /* Closes the file descriptor in RTU mode */
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
 
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     /* Revert settings */
     if (!SetCommState(ctx_rtu->w_ser.fd, &ctx_rtu->old_dcb))
         fprintf(stderr, "ERROR Couldn't revert to configuration (LastError %d)\n",
@@ -698,7 +697,7 @@ void _modbus_rtu_close(modbus_t *ctx)
 
 int _modbus_rtu_flush(modbus_t *ctx)
 {
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     modbus_rtu_t *ctx_rtu = ctx->backend_data;
     ctx_rtu->w_ser.n_bytes = 0;
     return ( FlushFileBuffers(ctx_rtu->w_ser.fd) == FALSE );
@@ -730,7 +729,7 @@ int _modbus_rtu_accept(modbus_t *ctx, int *socket)
 int _modbus_rtu_select(modbus_t *ctx, fd_set *rfds, struct timeval *tv, int msg_length_computed, int msg_length)
 {
     int s_rc;
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
     s_rc = win32_ser_select(&(((modbus_rtu_t*)ctx->backend_data)->w_ser), msg_length_computed, tv);
     if (s_rc == 0) {
         errno = ETIMEDOUT;

+ 29 - 14
src/modbus-tcp.c

@@ -15,29 +15,44 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "modbus.h"
-#include "modbus-private.h"
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
-
 #include <sys/types.h>
 
-#ifdef NATIVE_WIN32
-#include <ws2tcpip.h>
+#if defined(_WIN32)
+# define OS_WIN32
+# include <winsock2.h>
+# include <ws2tcpip.h>
+# define SHUT_RDWR 2
 #else
-#include <sys/socket.h>
-#include <sys/ioctl.h>
+# include <sys/socket.h>
+# include <sys/ioctl.h>
+
+#if defined(OpenBSD) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
+# define OS_BSD
+# include <netinet/in_systm.h>
+#endif
+
+# include <netinet/in.h>
+# include <netinet/ip.h>
+# include <netinet/tcp.h>
+# include <arpa/inet.h>
+#endif
+
+#if !defined(MSG_NOSIGNAL)
+#define MSG_NOSIGNAL 0
 #endif
 
+#include "modbus-private.h"
+
 #include "modbus-tcp.h"
 #include "modbus-tcp-private.h"
 
-#ifdef NATIVE_WIN32
-static int _modbus_tcp_init_win32()
+#ifdef OS_WIN32
+static int _modbus_tcp_init_win32(void)
 {
     // Initialise Win32 Socket API
     WORD wVersionRequested;
@@ -174,7 +189,7 @@ static int _modbus_tcp_connect(modbus_t *ctx)
     struct sockaddr_in addr;
     modbus_tcp_t *ctx_tcp = ctx->backend_data;
 
-#ifdef NATIVE_WIN32
+#ifdef OS_WIN32
     if (_modbus_tcp_init_win32() == -1) {
         return -1;
     }
@@ -195,7 +210,7 @@ static int _modbus_tcp_connect(modbus_t *ctx)
         return -1;
     }
 
-#ifndef NATIVE_WIN32
+#ifndef OS_WIN32
     /**
      * Cygwin defines IPTOS_LOWDELAY but can't handle that flag so it's
      * necessary to workaround that problem.
@@ -241,7 +256,7 @@ int _modbus_tcp_flush(modbus_t *ctx)
     do {
         /* Extract the garbage from the socket */
         char devnull[MODBUS_TCP_MAX_ADU_LENGTH];
-#ifndef NATIVE_WIN32
+#ifndef OS_WIN32
         rc = recv(ctx->s, devnull, MODBUS_TCP_MAX_ADU_LENGTH, MSG_DONTWAIT);
 #else
         /* On Win32, it's a bit more complicated to not wait */
@@ -275,7 +290,7 @@ int _modbus_tcp_listen(modbus_t *ctx, int nb_connection)
     struct sockaddr_in addr;
     modbus_tcp_t *ctx_tcp = ctx->backend_data;
 
-#ifdef NATIVE_WIN32
+#ifdef OS_WIN32
     if (_modbus_tcp_init_win32() == -1) {
         return -1;
     }

+ 9 - 0
src/modbus-tcp.h

@@ -20,6 +20,15 @@
 
 #include "modbus.h"
 
+#if defined(_WIN32) && !defined(__CYGWIN__)
+/* Win32 with MinGW, supplement to <errno.h> */
+#include <winsock2.h>
+#define ECONNRESET   WSAECONNRESET
+#define ECONNREFUSED WSAECONNREFUSED
+#define ETIMEDOUT    WSAETIMEDOUT
+#define ENOPROTOOPT  WSAENOPROTOOPT
+#endif
+
 #define MODBUS_TCP_DEFAULT_PORT   502
 #define MODBUS_TCP_SLAVE         0xFF
 

+ 1 - 24
src/modbus.h

@@ -20,35 +20,12 @@
 
 #include <config.h>
 
-/* If win32 and no cygwin, suppose it's MinGW or any other native windows compiler. */
-#if defined(WIN32) && !defined(__CYGWIN__)
-#define NATIVE_WIN32
-#define MSG_NOSIGNAL 0
-#define ECONNRESET   WSAECONNRESET
-#define ECONNREFUSED WSAECONNREFUSED
-#define ETIMEDOUT    WSAETIMEDOUT
-#define ENOPROTOOPT  WSAENOPROTOOPT
-#define SHUT_RDWR    2
-#include <winsock2.h>
-#endif /* win32 and no cygwin */
-
 /* Add this for macros that defined unix flavor */
 #if (defined(__unix__) || defined(unix)) && !defined(USG)
 #include <sys/param.h>
 #endif
-#include <stdint.h>
-#ifndef NATIVE_WIN32
-
-#include <termios.h>
 
-#if defined(OpenBSD) || (defined(__FreeBSD__) && __FreeBSD__ < 5)
-#include <netinet/in_systm.h>
-#endif
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/tcp.h>
-#include <arpa/inet.h>
-#endif
+#include <stdint.h>
 #include <sys/time.h>
 
 #include "modbus-version.h"

+ 3 - 1
tests/bandwidth-server-many-up.c

@@ -24,8 +24,10 @@
 
 #include <modbus.h>
 
-#ifdef NATIVE_WIN32
+#if defined(_WIN32)
 #include <ws2tcpip.h>
+#else
+#include <netinet/in.h>
 #endif
 
 #define NB_CONNECTION    5