Ser2net: is there a simple go/nogo test for configured serial proxies?

No, I hadn't done that before ... I wasn't aware or realized that this had to be done ...
The script is now there, but unfortunately ser2net still does not start automatically.
There are no entries for "ser2net" in the output of either "ps" or "netstat -lpn".

root@OpenWrt:~# cat /etc/init.d/ser2net
#!/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 global 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_set_param file "$DYNAMICCFGFILE"
	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "ser2net"
}

It could be failing to start because the serial device does not yet exist when the script runs during boot. START=75 seems early for this sort of thing. Both serial and networking have to be up before ser2net can start.

1 Like

You can check if the config file in /tmp exists after boot. If not, the startup script hasn't run at all.

1 Like

Yes, the temporary conf file is available

#
# Following part is auto-generated from UCI settings in /etc/config/ser2net
#
CONTROLPORT:localhost,2000
DEFAULT:speed:115200
DEFAULT:databits:8
DEFAULT:stopbits:1
DEFAULT:parity:none
5000:raw:0:/dev/ttyS1:115200 8DATABITS NONE 1STOPBIT
5001:raw:0:/dev/ttyS2:57600 8DATABITS NONE 1STOPBIT

If you check the status after booting via the Autostart menu item in the System area of the user interface, you will find "ser2net" in the list.

In the list via /cgi-bin/luci/admin/status/processes, however, you will not find "ser2net" as a running process.

But, if you perform a manual restart via the user interface, the following entry appears in /cgi-bin/luci/admin/status/processes afterwards

  • 3532 root /usr/sbin/ser2net -n -c /tmp/ser2net.conf

In the meantime, I keep searching the internet for further clues. And here, too, I have noticed that the timing aspect is mentioned remarkably often in various forums - mostly RasPI.

  • Would you have a suggestion for a "safe" entry or value?

Hm, I don't recall having ever had problems with ser2net not starting. I configured it always via LuCI and that's it. AFAICT /dev/ttySx ports do not rely on loadable kernel modules, so they should be available from the start.

A procd_set_param respawn could be added to cause an automatic restart if the daemon crashes. I'm looking at gpsd which is a similar kind of service.

2 Likes

Automatic restart also sounds good for other reasons :hugs:

However, I entered START=94 and the first result is ser2net in the process list. I cannot yet say how sustainable it is.

I chose 94 because in the script for "done" the entry START=95 can be found.

Well, it was only one restart, but I'll try a few more to be sure... :wink: .

Now that a few days have passed and the NA502 gateway with the ser2net package no longer had any problems after changing the START parameter in the init script, this change can IMHO be considered a solution or valid workaround.

Original value

START=75

Changed value

START=94

Many thanks to all supporters and tipsters.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.