#!/bin/sh /etc/rc.common
#
# Copyright (C) 2014-2020, Lantronix, Inc. All Rights Reserved.
#           7535 Irvine Center Drive, Suite 100
#           Irvine, CA 92618 USA

# Permission to use, copy, modify this software for any
# purpose is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.

# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#


START=99

print_usage() {
	echo "Invalid argument please provide serial port number"
} 

kill_serial() {
	if [ "$#" -ne 1 ]; then
		print_usage
		return 0
	fi
	port=$1
	ps | egrep -i "serial /dev/ttyS$port" | grep -v grep | awk '{print $1}' | xargs kill -9 2>/dev/null
}

boot() {
	[ -f /proc/device-tree/lantronix/ftm_mode ] && {
		ftm_mode=`cat /proc/device-tree/lantronix/ftm_mode`
		[ $ftm_mode != "0" ] && return
	}
	total_ports=`cat /proc/device-tree/lantronix/hwcfg | tr -d -c 0-9 | cut -c 3-`
	port=1
	while [ $port -le $total_ports ]
	do
		o_port=$([ "$port" == "1" ] && echo "" || echo $(( $port - 1 )))
		mode="`uci get serial$o_port.@configuration[0].mode`"
		if [[ "$mode" == "transparent" ]] || [[ "$mode" == "manageddevice" ]] || [[ "$mode" == "none" ]] || [[ "$mode" == "modbus" ]] || [[ "$mode" == "terminal" ]]
		then
			/usr/sbin/serial.sh $port 2>/dev/null &
			sleep 6
			rm -f /tmp/luci_message_tunnel*
			rm -f /tmp/luci_message_serial*
		fi
		true $(( port++ ))
	done
	return 0
}

start() {
	if [ "$#" -ne 1 ]; then
		print_usage
		return 0
	fi
	port=$1
	[ -f /proc/device-tree/lantronix/ftm_mode ] && {
		ftm_mode=`cat /proc/device-tree/lantronix/ftm_mode`
		[ $ftm_mode != "0" ] && return
	}
	o_port=$([ "$port" == "1" ] && echo "" || echo $(( $port - 1 )))
	if [ $port -gt 16 ]; then
	    # kill socat in master
	    /usr/sbin/serial_socat.sh kill_socat "$port" 2>/dev/null
	    kill_serial $port
	    # Send Serial config file to slave
	    state=`uci get serial$o_port.Line.State`
	    if [ "$state" == "1" ]; then
		/usr/sbin/slave_communication.sh copy_files_to_slave "/etc/config/serial$o_port" "/etc/config/serial$o_port" 2>/dev/null
		/usr/sbin/serial_socat.sh serial_routine "$port"
	    fi
	else
	   kill_serial $port
	fi

	/usr/sbin/serial.sh $port 2>/dev/null
	tunnel_mode="`uci get serial$o_port.Line.Interface`"
	if [[ "$tunnel_mode" == "RS232" ]]
	then
		`ftm setRS232_$port`
	elif [[ "$tunnel_mode" == "RS485 Half-Duplex" && $port -lt 5 ]]
	then
		`ftm setRS485half_$port`
	elif [[ $port -lt 5 ]]
	then
		`ftm setRS485full_$port`
	fi
}

stop() {
	if [ "$#" -ne 1 ]; then
		print_usage
		return 0
	fi
	kill_serial $1
	return 0
}

reload() {
	if [ "$#" -ne 1 ]; then
		print_usage
		return 0
	fi
	port=$1
	o_port=$([ "$port" == "1" ] && echo "" || echo $(( $port - 1 )))
	mode="`uci get serial$o_port.@configuration[0].mode`"
	if [[ "$mode" != "modmaster" ]] && [[ "$mode" != "dlms" ]]
	then
		kill_serial $port
		/usr/sbin/serial.sh $port 2>/dev/null
	fi
}

restart() {
	if [ "$#" -ne 1 ]; then
		print_usage
		return 0
	fi
	reload $1
}
