From f410aea2f72fb1205803b10e5cec4a322601a82b Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Wed, 27 Oct 2010 01:11:39 +0200 Subject: [PATCH] Isolate RTU and TCP headers and simplify Win32 detection --- configure.ac | 10 ++++------ src/Makefile.am | 3 ++- src/modbus-rtu-private.h | 17 +++++++++-------- src/modbus-rtu.c | 17 ++++++++--------- src/modbus-tcp.c | 43 +++++++++++++++++++++++++++++-------------- src/modbus-tcp.h | 9 +++++++++ src/modbus.h | 25 +------------------------ tests/bandwidth-server-many-up.c | 4 +++- 8 files changed, 65 insertions(+), 63 deletions(-) diff --git a/configure.ac b/configure.ac index 4d53728..9fe435f 100644 --- a/configure.ac +++ b/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 diff --git a/src/Makefile.am b/src/Makefile.am index 7ca577b..5ef1465 100644 --- a/src/Makefile.am +++ b/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 diff --git a/src/modbus-rtu-private.h b/src/modbus-rtu-private.h index 19e2381..363ce17 100644 --- a/src/modbus-rtu-private.h +++ b/src/modbus-rtu-private.h @@ -19,7 +19,12 @@ #define _MODBUS_RTU_PRIVATE_H_ #include + +#if defined(_WIN32) +#include +#else #include +#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 - +#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 diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index 976620f..98c6652 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -22,7 +22,6 @@ #include #include -#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; diff --git a/src/modbus-tcp.c b/src/modbus-tcp.c index 26fb0f3..af26b9b 100644 --- a/src/modbus-tcp.c +++ b/src/modbus-tcp.c @@ -15,29 +15,44 @@ * along with this program. If not, see . */ -#include "modbus.h" -#include "modbus-private.h" - #include #include #include #include #include - #include -#ifdef NATIVE_WIN32 -#include +#if defined(_WIN32) +# define OS_WIN32 +# include +# include +# define SHUT_RDWR 2 #else -#include -#include +# include +# include + +#if defined(OpenBSD) || (defined(__FreeBSD__) && __FreeBSD__ < 5) +# define OS_BSD +# include +#endif + +# include +# include +# include +# include +#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; } diff --git a/src/modbus-tcp.h b/src/modbus-tcp.h index 09fb1d4..1de1176 100644 --- a/src/modbus-tcp.h +++ b/src/modbus-tcp.h @@ -20,6 +20,15 @@ #include "modbus.h" +#if defined(_WIN32) && !defined(__CYGWIN__) +/* Win32 with MinGW, supplement to */ +#include +#define ECONNRESET WSAECONNRESET +#define ECONNREFUSED WSAECONNREFUSED +#define ETIMEDOUT WSAETIMEDOUT +#define ENOPROTOOPT WSAENOPROTOOPT +#endif + #define MODBUS_TCP_DEFAULT_PORT 502 #define MODBUS_TCP_SLAVE 0xFF diff --git a/src/modbus.h b/src/modbus.h index 48ae2b3..1e876b8 100644 --- a/src/modbus.h +++ b/src/modbus.h @@ -20,35 +20,12 @@ #include -/* 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 -#endif /* win32 and no cygwin */ - /* Add this for macros that defined unix flavor */ #if (defined(__unix__) || defined(unix)) && !defined(USG) #include #endif -#include -#ifndef NATIVE_WIN32 - -#include -#if defined(OpenBSD) || (defined(__FreeBSD__) && __FreeBSD__ < 5) -#include -#endif -#include -#include -#include -#include -#endif +#include #include #include "modbus-version.h" diff --git a/tests/bandwidth-server-many-up.c b/tests/bandwidth-server-many-up.c index 759a184..3a27d7e 100644 --- a/tests/bandwidth-server-many-up.c +++ b/tests/bandwidth-server-many-up.c @@ -24,8 +24,10 @@ #include -#ifdef NATIVE_WIN32 +#if defined(_WIN32) #include +#else +#include #endif #define NB_CONNECTION 5 -- libgit2 0.21.4