From 1dd54f5673835dbd508f767aa62f98c1da0807f8 Mon Sep 17 00:00:00 2001 From: Michael Heimpold Date: Sat, 30 Dec 2017 23:30:06 +0100 Subject: [PATCH] ser2net: extend dynamic config generation (refs #5302) Signed-off-by: Michael Heimpold --- net/ser2net/Makefile | 11 +- net/ser2net/files/etc/config/ser2net | 8 -- net/ser2net/files/etc/init.d/ser2net | 46 ------- net/ser2net/files/ser2net.conf | 70 ++++++++++ net/ser2net/files/ser2net.config | 67 ++++++++++ net/ser2net/files/ser2net.init | 186 +++++++++++++++++++++++++++ 6 files changed, 330 insertions(+), 58 deletions(-) delete mode 100644 net/ser2net/files/etc/config/ser2net delete mode 100644 net/ser2net/files/etc/init.d/ser2net create mode 100644 net/ser2net/files/ser2net.conf create mode 100644 net/ser2net/files/ser2net.config create mode 100644 net/ser2net/files/ser2net.init diff --git a/net/ser2net/Makefile b/net/ser2net/Makefile index 9e61bef57..c1dad9f3e 100644 --- a/net/ser2net/Makefile +++ b/net/ser2net/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=ser2net PKG_VERSION:=3.5 -PKG_RELEASE:=1 +PKG_RELEASE:=2 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=@SF/ser2net @@ -43,13 +43,16 @@ endef define Package/ser2net/install $(INSTALL_DIR) $(1)/usr/sbin - $(CP) $(PKG_INSTALL_DIR)/usr/sbin/ser2net $(1)/usr/sbin/ + $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/ser2net $(1)/usr/sbin/ + $(INSTALL_DIR) $(1)/etc $(INSTALL_CONF) $(PKG_BUILD_DIR)/ser2net.conf $(1)/etc/ + $(INSTALL_DIR) $(1)/etc/config - $(INSTALL_CONF) ./files/etc/config/ser2net $(1)/etc/config/ser2net + $(INSTALL_CONF) ./files/ser2net.config $(1)/etc/config/ser2net + $(INSTALL_DIR) $(1)/etc/init.d - $(INSTALL_BIN) ./files/etc/init.d/ser2net $(1)/etc/init.d/ser2net + $(INSTALL_BIN) ./files/ser2net.init $(1)/etc/init.d/ser2net endef $(eval $(call BuildPackage,ser2net)) diff --git a/net/ser2net/files/etc/config/ser2net b/net/ser2net/files/etc/config/ser2net deleted file mode 100644 index 227f0521c..000000000 --- a/net/ser2net/files/etc/config/ser2net +++ /dev/null @@ -1,8 +0,0 @@ -config 'proxy' - option 'enabled' '0' - option 'port' '8000' - option 'protocol' 'raw' - option 'timeout' '30' - option 'device' '/dev/ttyUSB0' - option 'options' '9600 1STOPBIT 8DATABITS' - diff --git a/net/ser2net/files/etc/init.d/ser2net b/net/ser2net/files/etc/init.d/ser2net deleted file mode 100644 index e5ef383ee..000000000 --- a/net/ser2net/files/etc/init.d/ser2net +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh /etc/rc.common -# Copyright (C) 2017 OpenWrt.org - -START=75 -STOP=10 -CONFIGFILE="/tmp/ser2net.conf" - -USE_PROCD=1 -PROG=/usr/sbin/ser2net - -start_service() { - config_load 'ser2net' - - local enabled - config_get_bool enabled config 'enabled' '0' - [ "$enabled" -gt 0 ] || return 1 - - ser2net_create_config config || return 1 - procd_open_instance - procd_set_param command "$PROG" -n -c "$CONFIGFILE" - procd_close_instance -} - -ser2net_create_config() { - local cfg=$1 - local port - local device - - config_get port $cfg port - config_get device $cfg device - [ -z "$port" -o -t "$device" ] && return 1 - - local protocol - local timeout - local options - - config_get protocol $cfg protocol - config_get timeout $cfg timeout - config_get options $cfg options - - if [ -z "$options" ]; then - echo "$port":"$protocol":"$timeout":"$device" >> "$CONFIGFILE - else - echo "$port":"$protocol":"$timeout":"$device":"$options" >> "$CONFIGFILE" - fi -} diff --git a/net/ser2net/files/ser2net.conf b/net/ser2net/files/ser2net.conf new file mode 100644 index 000000000..2c9605bb4 --- /dev/null +++ b/net/ser2net/files/ser2net.conf @@ -0,0 +1,70 @@ +# +# This is a minimal example configuration file for ser2net. For a version with +# detailed comments and all possible configuration directives, please visit: +# https://github.com/cminyard/ser2net/blob/master/ser2net.conf +# +# On OpenWrt/LEDE systems, this configuration serves as a base configuration. +# During boot of the system, the UCI configuration file /etc/config/ser2net is +# parsed and converted to additional configuration lines which are _appended_ +# to this file. The ser2net daemon is then started with the combined +# configuration file /tmp/ser2net.conf. +# +# A basic service configuration line has the following format: +# :::: +# network port +# Name or number of the port to accept connections +# from for this device. A port number may be of the form +# [ipv4,|ipv6,][tcp,|udp,][host,]port, such as +# 127.0.0.1,2000 or ipv4,tcp,localhost,2000. If the host is +# specified, it will only bind to the IP address +# specified. Otherwise it will bind to all the ports on the +# machine. If ipv4 or ipv6 is specified, it will only bind +# to that network type. +# +# state +# Either raw or rawlp or telnet or off. off disables +# the port from accepting connections. It can be +# turned on later from the control port. raw enables +# the port and transfers all data as-is between the +# port and the long. rawlp enables the port and +# transfers all input data to device, device is open +# without any termios setting. It allow to use +# /dev/lpX devices and printers connected to them. +# telnet enables the port and runs the telnet proto- +# col on the port to set up telnet parameters. This +# is most useful for using telnet. +# +# timeout +# The time (in seconds) before the port will be dis- +# connected if there is no activity on it. A zero +# value disables this function. +# +# device +# The name of the device to connect to. This +# must be in the form of /dev/. +# +# options +# Sets operational parameters for the serial port. +# For a serial device (not IPMI SOL): +# Options 300, 1200, 2400, 4800, 9600, 19200, 38400, +# 57600, 115200 set the various baud rates. EVEN, +# ODD, NONE (MARK and SPACE if supported) set the parity. +# Note that MARK and SPACE are not available on all systems +# or hardware, if it is not supported then it will be +# silently set to ODD or EVEN parity. +# 1STOPBIT, 2STOPBITS set +# the number of stop bits. 5DATABITS, 6DATABITS, +# 7DATABITS, 8DATABITS set the number of data bits. +# [-]XONXOFF turns on (- off) XON/XOFF support. +# [-]RTSCTS turns on (- off) hardware flow control, +# [-]LOCAL turns off (- on) monitoring of the modem lines, +# and [-]HANGUP_WHEN_DONE turns on (- off) lowering the +# modem control lines when the connection is done. +# [-]NOBREAK disables automatic setting of the break +# setting of the serial port. +# +# The "[-]remctl" option allow remote control (ala RFC +# 2217) of serial-port configuration. +# +# Example: +# 5000:telnet:0:/dev/ttyAPP0:115200 8DATABITS NONE 1STOPBIT -XONXOFF -LOCAL -RTSCTS remctl diff --git a/net/ser2net/files/ser2net.config b/net/ser2net/files/ser2net.config new file mode 100644 index 000000000..1e9fcaa83 --- /dev/null +++ b/net/ser2net/files/ser2net.config @@ -0,0 +1,67 @@ +config global + option enabled 1 + +config controlport + option enabled 0 + option host localhost + option port 2000 + +config default + option speed 115200 + option databits 8 + option parity 'none' + option stopbits 1 + option rtscts false + option local false + option remctl true + +#config led rx +# option driver sysfs +# option device 'duckbill:green:rs485' +# option duration 20 +# option state 1 + +#config led tx +# option driver sysfs +# option device 'duckbill:red:rs485' +# option duration 20 +# option state 1 + +config proxy + option enabled 0 + option port 5000 + option protocol telnet + option timeout 0 + option device '/dev/ttyAPP0' + option baudrate 115200 + option databits 8 + option parity 'none' + option stopbits 1 +# option led_tx 'tx' +# option led_rx 'rx' + option rtscts false + option local false + option xonxoff false + list options '' + +config proxy + option enabled 0 + option port 5001 + option protocol telnet + option timeout 0 + option device '/dev/ttyUSB0' + option baudrate 115200 + option databits 8 + option parity 'none' + option stopbits 1 + +config proxy + option enabled 0 + option port 5002 + option protocol raw + option timeout 0 + option device '/dev/ttyUSB1' + option baudrate 9600 + option databits 8 + option parity 'none' + option stopbits 1 diff --git a/net/ser2net/files/ser2net.init b/net/ser2net/files/ser2net.init new file mode 100644 index 000000000..59516bee5 --- /dev/null +++ b/net/ser2net/files/ser2net.init @@ -0,0 +1,186 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2017 Michael Heimpold + +START=75 +STOP=10 + +USE_PROCD=1 +PROG=/usr/sbin/ser2net + +STATICCFGFILE="/etc/ser2net.conf" +DYNAMICCFGFILE="/tmp/ser2net.conf" + +list_cb_append() { + local var="$2" + local value="$1" + local sep="${3:- }" + + eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\"" +} + +append_bool() { + local var="$1" + local key="$2" + local val="$3" + local uc="$4" + local s="" + + [ "$uc" -eq 1 ] && key=`echo "$key" | tr '[a-z]' '[A-Z]'` + [ "$val" -eq 0 ] && s="-" + + append "$var" "$s$key" +} + +ser2net_default() { + local cfg="$1" + local key val + + for key in speed baudrate databits stopbits parity chardelay_scale chardelay_min; do + config_get val "$cfg" "$key" + [ -n "$val" ] || continue + + case "$key" in + baudrate) key="speed" ;; + hangup_when_done) ;; + telnet_brk_on_sync) ;; + deassert_CTS_DCD_DSR_on_connect) ;; + *) key=`echo "$key" | tr '_' '-'` + esac + + echo "DEFAULT:$key:$val" + done + + for key in chardelay deassert_CTS_DCD_DSR_on_connect hangup_when_done kickolduser \ + local nobreak remctl rtscts telnet_brk_on_sync xonxoff; do + config_get_bool val "$cfg" "$key" + [ -n "$val" ] || continue + [ "$val" -eq 0 ] && val="false" || val="true" + echo "DEFAULT:$key:$val" + done + + echo +} + +ser2net_controlport() { + local cfg="$1" + local enabled host port + + config_get_bool enabled "$cfg" enabled 0 + [ "$enabled" -eq 0 ] && return 0 + + config_get host "$cfg" host + config_get port "$cfg" port + + echo -e "CONTROLPORT:${host:+$host,}$port\n" +} + +ser2net_led() { + local cfg="$1" + local driver device state duration + + config_get driver "$cfg" driver sysfs + config_get device "$cfg" device + config_get state "$cfg" state 1 + config_get duration "$cfg" duration 20 + + echo -e "LED:$cfg:$driver:device=$device state=$state duration=$duration\n" +} + +ser2net_proxy() { + local cfg="$1" + local enabled port protocol timeout device baudrate databits parity stopbits + local led_tx led_rx key boolval options + + config_get_bool enabled "$cfg" enabled 0 + [ "$enabled" -eq 0 ] && return 0 + + config_get port "$cfg" port + [ "$port" -le 0 -o "$port" -gt 65535 ] && return 1 + + config_get protocol "$cfg" protocol + case "$protocol" in + raw|rawlp|telnet|off) ;; + *) return 1 + esac + + config_get timeout "$cfg" timeout 0 + config_get device "$cfg" device + [ -z "$device" ] && return 1 + + config_get baudrate "$cfg" baudrate + [ -n "$baudrate" ] && append options "$baudrate" + + config_get databits "$cfg" databits + if [ -n "$databits" ]; then + [ "$databits" -lt 5 -o "$databits" -gt 8 ] && return 1 + append options "${databits}DATABITS" + fi + + config_get parity "$cfg" parity + parity=`echo "$parity" | tr '[a-z]' '[A-Z]'` + case "$parity" in + EVEN|ODD|NONE|MARK|SPACE) append options "$parity" ;; + "") ;; + *) return 1 + esac + + config_get stopbits "$cfg" stopbits + case "$stopbits" in + 1) append options "${stopbits}STOPBIT" ;; + 2) append options "${stopbits}STOPBITS" ;; + "") ;; + *) return 1 + esac + + config_get led_tx "$cfg" led_tx + [ -n "$led_tx" ] && append options "led-tx=$led_tx" + + config_get led_rx "$cfg" led_rx + [ -n "$led_rx" ] && append options "led-rx=$led_rx" + + for key in rtscts local xonxoff nobreak hangup_when_done; do + config_get_bool boolval "$cfg" "$key" + [ -n "$boolval" ] || continue + append_bool options "$key" "$boolval" 1 + done + + for key in chardelay telnet_brk_on_sync kickolduser remctl; do + config_get_bool boolval "$cfg" "$key" + [ -n "$boolval" ] || continue + append_bool options "$key" "$boolval" 0 + done + + config_list_foreach "$cfg" options list_cb_append options + + if [ "`echo "$device" | sed 's/://g'`" != "$device" ]; then + echo "DEVICE:$cfg:$device" + device="$cfg" + fi + + echo -e "$port:$protocol:$timeout:$device:$options\n" +} + +start_service() { + local enabled + + config_load ser2net + + config_get_bool enabled ser2net enabled 0 + [ "$enabled" -gt 0 ] || return 0 + + cat "$STATICCFGFILE" - 2>/dev/null <<-EOF > "$DYNAMICCFGFILE" + + # + # Following part is auto-generated from UCI settings in /etc/config/ser2net + # + EOF + + config_foreach ser2net_controlport controlport >> "$DYNAMICCFGFILE" + config_foreach ser2net_default default >> "$DYNAMICCFGFILE" + config_foreach ser2net_led led >> "$DYNAMICCFGFILE" + config_foreach ser2net_proxy proxy >> "$DYNAMICCFGFILE" + + procd_open_instance + procd_set_param command "$PROG" -n -c "$DYNAMICCFGFILE" + procd_close_instance +}