Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into logging-config
Browse files Browse the repository at this point in the history
  • Loading branch information
kpr0th committed Dec 22, 2023
2 parents a3c3c0f + 68f9880 commit 99692a4
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Install test dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twisted pymodbus
pip install setuptools wheel twisted pyserial pymodbus
sudo apt-get install -y socat
- name: Run tests
Expand Down
26 changes: 6 additions & 20 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
/autom4te.cache
/config.cache
/config.h
/config.status
/.settings/
/src/.deps/
/src/*.o
/src/mbusd
/doc/mbusd.8
Makefile
/.settings
*.log
*.in~
systemd-units/[email protected]
*.conf
*.pyc
CMakeCache.txt
CMakeFiles
CPackConfig.cmake
CPackSourceConfig.cmake
cmake_install.cmake
.idea
*~
/.idea
/.vscode
.project
.cproject
build
.pydevproject
/build
build.sh
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
cmake_minimum_required(VERSION 3.2)

project(mbusd VERSION 0.5.1)
project(mbusd VERSION 0.5.2)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/extern_GPL)
include(CheckFunctionExists)
include(CheckLibraryExists)
include(CheckSymbolExists)
include(GNUInstallDirs)
include(FindUnixCommands)
include(FindSystemd)
Expand Down Expand Up @@ -41,6 +42,10 @@ check_function_exists(cfsetispeed HAVE_CFSETISPEED)
if(HAVE_CFSETSPEED AND HAVE_CFSETISPEED)
add_definitions(-DHAVE_CFSETSPEED)
endif()
check_symbol_exists(TIOCSRS485 sys/ioctl.h HAVE_TIOCRS485)
if(HAVE_TIOCRS485)
add_definitions(-DHAVE_TIOCRS485)
endif()
check_function_exists(time HAVE_TIME)
check_function_exists(localtime HAVE_LOCALTIME)
if(HAVE_TIME AND HAVE_LOCALTIME)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2002-2003, 2013-2022 Victor Antonovich ([email protected])
Copyright (c) 2002-2003, 2013-2023 Victor Antonovich ([email protected])
Copyright (c) 2011 Andrew Denysenko <[email protected]>
All rights reserved.

Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ can be altered in many ways, e.g. by using the following tools in the `build` di
Usage:
------

mbusd [-h] [-d] [-L logfile] [-v level] [-c cfgfile] [-p device] [-s speed] [-m mode]
[-t] [-y file] [-Y file] [-A address] [-P port] [-C maxconn] [-N retries]
mbusd [-h] [-d] [-L logfile] [-v level] [-c cfgfile]
[-p device] [-s speed] [-m mode] [-S]
[-t] [-r] [-y sysfsfile] [-Y sysfsfile]
[-A address] [-P port] [-C maxconn] [-N retries]
[-R pause] [-W wait] [-T timeout] [-b]

-h Usage help.
Expand All @@ -73,18 +75,19 @@ Usage:
Specifies serial port speed.
-m mode
Specifies serial port mode (like 8N1).
-S Enable RS-485 support for given serial port device (Linux only)
-t Enable RTS RS-485 data direction control using RTS, active transmit.
-r Enable RTS RS-485 data direction control using RTS, active receive.
-y file
Enable RS-485 direction data direction control by writing '1' to file
for transmitter enable and '0' to file for transmitter disable.
-Y file
Enable RS-485 direction data direction control by writing '0' to file
for transmitter enable and '1' to file for transmitter disable.
-A address
Specifies TCP server address to bind (default 0.0.0.0).
Specifies TCP server address to bind (default is 0.0.0.0).
-P port
Specifies TCP server port number (default 502).
-t
Enable RTS RS-485 data direction control (if not disabled while compile).
-y sysfsfile
Enable RS-485 direction data direction control by writing '1' to sysfsfile
for transmitter enable and '0' to file for transmitter disable
-Y sysfsfile
Enable RS-485 direction data direction control by writing '0' to sysfsfile
for transmitter enable and '1' to file for transmitter disable
Specifies TCP server port number (default is 502).
-C maxconn
Specifies maximum number of simultaneous TCP connections (default is 32).
-N retries
Expand All @@ -96,7 +99,7 @@ Usage:
-T timeout
Specifies connection timeout value in seconds (0 disables timeout, default is 60).
-b
Instructs **mbusd** to reply on a broadcast.
Instructs mbusd to reply on a broadcast.

Please note running **mbusd** on default Modbus TCP port (502) requires root privileges!

Expand Down
5 changes: 4 additions & 1 deletion conf/mbusd.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ speed = 9600
# Serial port mode
mode = 8n1

# RS-485 data direction control type (addc, rts, sysfs_0, sysfs_1)
# Enable RS-485 support for given serial port device (Linux only)
# enable_rs485 = no

# RS-485 data direction control type (addc, rts_0, rts/rts_1, sysfs_0, sysfs_1)
trx_control = addc

# Sysfs file to use to control data direction
Expand Down
15 changes: 10 additions & 5 deletions doc/mbusd.8.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ mbusd \- MODBUS/TCP to MODBUS/RTU gateway.
.RB [ -m
.IR mode ]
.RB [ -t ]
.RB [ -r ]
.RB [ -y
.IR file ]
.IR sysfsfile ]
.RB [ -Y
.IR file ]
.IR sysfsfile ]
.RB [ -A
.IR address ]
.RB [ -P
Expand Down Expand Up @@ -60,18 +61,22 @@ Specifies serial port device name.
Specifies serial port speed.
.IP "\fB-m \fImode\fR"
Specifies serial port mode (like 8N1).
.IP \fB-S\fR
Enable RS-485 support for given serial port device (Linux only).
.IP "\fB-A \fIaddress\fR"
Specifies TCP address to bind.
.IP "\fB-P \fIport\fR"
Specifies TCP port number.
.IP \fB-t\fR
Enable RTS RS-485 data direction control (if not disabled while compile).
Enable RTS RS-485 data direction control using RTS, active transmit.
.IP \fB-r\fR
Enable RTS RS-485 data direction control using RTS, active receive.
.IP "\fB-y \fIfile\fR"
Enable RS-485 direction data direction control by writing '1' to file
for transmitter enable and '0' to file for transmitter disable
for transmitter enable and '0' to file for transmitter disable.
.IP "\fB-Y \fIfile\fR"
Enable RS-485 direction data direction control by writing '0' to file
for transmitter enable and '1' to file for transmitter disable
for transmitter enable and '1' to file for transmitter disable.
.IP "\fB-C \fImaxconn\fR"
Specifies maximum number of simultaneous TCP connections.
.IP "\fB-N \fIretries\fR"
Expand Down
17 changes: 15 additions & 2 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ cfg_init(void)
strncpy(cfg.ttyport, DEFAULT_PORT, INTBUFSIZE);
cfg.ttyspeed = DEFAULT_SPEED;
strncpy(cfg.ttymode, DEFAULT_MODE, INTBUFSIZE);
#ifdef HAVE_TIOCRS485
cfg.rs485 = FALSE;
#endif
#ifdef TRXCTL
cfg.trxcntl = TRX_ADDC;
*cfg.trxcntl_file = '\0';
Expand Down Expand Up @@ -197,6 +200,12 @@ cfg_handle_param(char *name, char *value)
cfg.conntimeout = strtoul(value, NULL, 0);
if (cfg.conntimeout > MAX_CONNTIMEOUT)
return 0;
#ifdef HAVE_TIOCRS485
}
else if (CFG_NAME_MATCH("enable_rs485"))
{
cfg.rs485 = CFG_VALUE_BOOL();
#endif
#ifdef TRXCTL
}
else if (CFG_NAME_MATCH("trx_control"))
Expand All @@ -205,9 +214,13 @@ cfg_handle_param(char *name, char *value)
{
cfg.trxcntl = TRX_ADDC;
}
else if (CFG_VALUE_MATCH("rts"))
else if (CFG_VALUE_MATCH("rts") || CFG_VALUE_MATCH("rts_1"))
{
cfg.trxcntl = TRX_RTS_1;
}
else if (CFG_VALUE_MATCH("rts_0"))
{
cfg.trxcntl = TRX_RTS;
cfg.trxcntl = TRX_RTS_0;
}
else if (CFG_VALUE_MATCH("sysfs_0"))
{
Expand Down
6 changes: 6 additions & 0 deletions src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ typedef struct
int ttyspeed;
/* tty mode */
char ttymode[INTBUFSIZE + 1];
#ifdef HAVE_TIOCRS485
/* Linux RS-485 support use flag */
bool rs485;
#endif
#ifdef TRXCTL
/* trx control type (0 - ADDC, 1 - by RTS, 2 - by sysfs GPIO with 1 activating transmit, 3 - by sysfs GPIO with 0 activating transmit) */
int trxcntl;
/* trx control sysfs file */
char trxcntl_file[INTBUFSIZE + 1];
#endif
/* TCP server address */
char serveraddr[INTBUFSIZE + 1];
/* TCP server port number */
Expand Down
4 changes: 2 additions & 2 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
#ifdef TRXCTL
if (istty && cfg.trxcntl != TRX_ADDC)
{
tty_set_rts(d);
tty_set_tx(d);
tty_delay(35000000l/cfg.ttyspeed);
}
#endif
Expand All @@ -267,7 +267,7 @@ conn_write(int d, void *buf, size_t nbytes, int istty)
if (istty && cfg.trxcntl != TRX_ADDC )
{
tty_delay(DV(nbytes, tty.bpc, cfg.ttyspeed));
tty_clr_rts(d);
tty_set_rx(d);
}
#endif

Expand Down
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <netdb.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdbool.h>
#ifdef HAVE_LIBUTIL
# include <libutil.h>
#endif
Expand Down
57 changes: 38 additions & 19 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,52 +114,60 @@ void
usage(char *exename)
{
cfg_init();
printf("%s-%s Copyright (C) 2002-2003, 2011, 2013-2022 Victor Antonovich <[email protected]>, "
printf("%s-%s Copyright (C) 2002-2003, 2011, 2013-2023 Victor Antonovich <[email protected]>, "
"Andrew Denysenko <[email protected]>\n\n"
"Usage: %s [-h] [-d] "
#ifdef LOG
"[-L logfile] [-v level] "
#endif
"[-c cfgfile] \n"
" [-p device] [-s speed] [-m mode]\n"
" [-p device] [-s speed] [-m mode]"
#ifdef HAVE_TIOCRS485
" [-S]"
#endif
"\n"
#ifdef TRXCTL
" [-t] [-y sysfsfile] [-Y sysfsfile]\n"
" [-t] [-r] [-y sysfsfile] [-Y sysfsfile]\n"
#endif
" [-A address] [-P port] [-C maxconn] [-N retries]\n"
" [-R pause] [-W wait] [-T timeout] [-b]\n\n"
"Options:\n"
" -h : this help\n"
" -d : don't daemonize\n"
#ifdef LOG
" -L logfile : set log file name (default %s%s, \n"
" -L logfile : set log file name (default is %s%s, \n"
" '-' for logging to STDOUT only)\n"
#ifdef DEBUG
" -v level : set log level (0-9, default %d, 0 - errors only)\n"
" -v level : set log level (0-9, default is %d, 0 - errors only)\n"
#else
" -v level : set log level (0-2, default %d, 0 - errors only)\n"
#endif
#endif
" -c cfgfile : read configuration from cfgfile\n"
" -p device : set serial port device name (default %s)\n"
" -s speed : set serial port speed (default %d)\n"
" -m mode : set serial port mode (default %s)\n"
" -A address : set TCP server address to bind (default %s)\n"
" -P port : set TCP server port number (default %d)\n"
" -p device : set serial port device name (default is %s)\n"
" -s speed : set serial port speed (default is %d)\n"
" -m mode : set serial port mode (default is %s)\n"
#ifdef HAVE_TIOCRS485
" -S : enable Linux RS-485 support for given serial port device\n"
#endif
" -A address : set TCP server address to bind (default is %s)\n"
" -P port : set TCP server port number (default is %d)\n"
#ifdef TRXCTL
" -t : enable RTS RS-485 data direction control using RTS\n"
" -t : enable RTS RS-485 data direction control using RTS, active transmit\n"
" -r : enable RTS RS-485 data direction control using RTS, active receive\n"
" -y : enable RTS RS-485 data direction control using sysfs file, active transmit\n"
" -Y : enable RTS RS-485 data direction control using sysfs file, active receive\n"
#endif
" -C maxconn : set maximum number of simultaneous TCP connections\n"
" (1-%d, default %d)\n"
" (1-%d, default is %d)\n"
" -N retries : set maximum number of request retries\n"
" (0-%d, default %d, 0 - without retries)\n"
" (0-%d, default is %d, 0 disables retrying)\n"
" -R pause : set pause between requests in milliseconds\n"
" (1-%d, default %lu)\n"
" (1-%d, default is %lu)\n"
" -W wait : set response wait time in milliseconds\n"
" (1-%d, default %lu)\n"
" (1-%d, default is %lu)\n"
" -T timeout : set connection timeout value in seconds\n"
" (0-%d, default %d, 0 - no timeout)\n"
" (0-%d, default is %d, 0 disables timeout)\n"
" -b : enable reply on broadcast"
"\n", PACKAGE, VERSION, exename,
#ifdef LOG
Expand Down Expand Up @@ -197,6 +205,9 @@ main(int argc, char *argv[])
#ifdef TRXCTL
"ty:Y:"
#endif
#ifdef HAVE_TIOCRS485
"S"
#endif
#ifdef LOG
"v:L:"
#endif
Expand All @@ -223,16 +234,24 @@ main(int argc, char *argv[])
break;
#ifdef TRXCTL
case 't':
cfg.trxcntl = TRX_RTS;
cfg.trxcntl = TRX_RTS_1;
break;
case 'r':
cfg.trxcntl = TRX_RTS_0;
break;
case 'y':
cfg.trxcntl = TRX_SYSFS_1;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
break;
case 'Y':
cfg.trxcntl = TRX_SYSFS_0;
strncpy(cfg.trxcntl_file, optarg, INTBUFSIZE);
break;
break;
#endif
#ifdef HAVE_TIOCRS485
case 'S':
cfg.rs485 = TRUE;
break;
#endif
#ifdef LOG
case 'v':
Expand Down
Loading

0 comments on commit 99692a4

Please sign in to comment.