#!/bin/sh /etc/rc.common

START=20
STOP=90

USE_PROCD=1

init_switch() {
	setup_switch() { return 0; }

	include /lib/network
	setup_switch
	#Soleil used LANxLED1 for MT7531 port LEDs--start
        switch phy cl45 w 0 0x1f 0x21 0x8009
        switch phy cl45 w 0 0x1f 0x26 0xc007
        switch phy cl45 w 0 0x1f 0x27 0x003f
        #Soleil used LANxLED1 for MT7531 port LEDs--End
}

init_RTL8221() {
	[ -e /tmp/MODE_BR ] && [ -e /var/force_dhcp_server_started ] && {
	echo "Force DHCP mode Disable RTL8221 ..." > /var/tmp/wnc-phy-init-debug
	echo mode 101 1 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	echo dir 101 1 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	echo out 101 0 > /sys/devices/platform/10005000.pinctrl/mt_gpio
} || {
	echo "@@@@@@ (1) Reset the RTL8221 ..." > /var/tmp/wnc-phy-init-debug
	#devmem 0x10211390 w 0x11111111
	echo mode 101 1 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	#devmem 0x10211030 w 0x00000038
	echo dir 101 1 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	#devmem 0x10211130 w 0x00000000
	echo out 101 0 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	sleep 1
	#devmem 0x10211130 w 0x00000020
	echo out 101 1 > /sys/devices/platform/10005000.pinctrl/mt_gpio
	sleep 3
	/etc/init.d/RTL8221_patch.sh
	echo "@@@@@@ (13) disable RTL8221 AN ..." >> /var/tmp/wnc-phy-init-debug
	switch phy cl45 w 5 30 0x7588 0x2
	switch phy cl45 w 5 30 0x7589 0x71D0
	switch phy cl45 w 5 30 0x7587 0x3
	switch phy cl45 r 5 30 0x7587
	echo "@@@@@@ (14) Init LED0,LED1 ..." >> /var/tmp/wnc-phy-init-debug
	switch phy cl45 w 5 31 0xD032 0x20
	switch phy cl45 w 5 31 0xD034 0x26
	switch phy cl45 w 5 31 0xD036 0x26
	switch phy cl45 w 5 31 0xD040 0x3906
	echo "@@@@@@ (15) disable 10M support ... " >> /var/tmp/wnc-phy-init-debug
	switch phy cl45 w 5 7 0x0010 0x1d81
	#Soleil disabled the validation of a broadcast from MAC (a command with PHY address=0),20201224--start
	echo "@@@@@@ (16) disable the response to boardcast (phy addr=0) ... " >> /var/tmp/wnc-phy-init-debug
	switch phy cl45 w 5 31 0xA430 0x19a
	#Soleil disabled the validation of a broadcast from MAC (a command with PHY address=0),20201224--end
	echo "@@@@@@ (17) RTL8221 initialized! " >> /var/tmp/wnc-phy-init-debug
	}
}

start_service() {
	init_switch

	procd_open_instance
	procd_set_param command /sbin/netifd
	procd_set_param respawn
	procd_set_param watch network.interface
	[ -e /proc/sys/kernel/core_pattern ] && {
		procd_set_param limits core="unlimited"
	}
	procd_close_instance
	init_RTL8221
	#WNC-NMR0000-JOE-PENG-20200923-Implement section 4.4.21.2 jumbo frame, 4.4.22 pause frame, 4.4.23.1 EEE-start
	/usr/sbin/ether_tool restart_jumbo_frame > /dev/null
	/usr/sbin/ether_tool restart_pause_frame > /dev/null
	#WNC-NMR0000-JOE-PENG-20210121-Fix issue in QandA FromNEC 133 (DUT could not send IPv6 DAD NS packet for LAN interface)
	(sleep 8; /usr/sbin/ether_tool restart_eee > /dev/null)&
	#WNC-NMR0000-JOE-PENG-20200923-Implement section 4.4.21.2 jumbo frame, 4.4.22 pause frame, 4.4.23.1 EEE-end
}

reload_service() {
	init_switch
	ubus call network reload
	init_RTL8221
	#WNC-NMR0000-JOE-PENG-20200923-Implement section 4.4.21.2 jumbo frame, 4.4.22 pause frame, 4.4.23.1 EEE-start
	/usr/sbin/ether_tool restart_jumbo_frame > /dev/null
	/usr/sbin/ether_tool restart_pause_frame > /dev/null
	/usr/sbin/ether_tool restart_eee > /dev/null
	#WNC-NMR0000-JOE-PENG-20200923-Implement section 4.4.21.2 jumbo frame, 4.4.22 pause frame, 4.4.23.1 EEE-end
	/sbin/wifi reload_legacy

	bridge-utils-brctl setzoneisolate br-lan 1
	bridge-utils-brctl setguestisolate br-lan 1
	[ -e /tmp/MODE_BR ] && {
		bridge-utils-brctl setzone br-lan eth0.2 2
	}
}

stop_service() {
	/sbin/wifi down
	ifdown -a
	sleep 1
}

service_running() {
	ubus -t 120 wait_for network.interface
	/sbin/wifi reload_legacy

	bridge-utils-brctl setzoneisolate br-lan 1
	bridge-utils-brctl setguestisolate br-lan 1
	[ -e /tmp/MODE_BR ] && {
		bridge-utils-brctl setzone br-lan eth0.2 2
	}
}

validate_atm_bridge_section()
{
	uci_validate_section network "atm-bridge" "${1}" \
		'unit:uinteger:0' \
		'vci:range(32, 65535):35' \
		'vpi:range(0, 255):8' \
		'atmdev:uinteger:0' \
		'encaps:or("llc", "vc"):llc' \
		'payload:or("bridged", "routed"):bridged'
}

validate_route_section()
{
	uci_validate_section network route "${1}" \
		'interface:string' \
		'target:cidr4' \
		'netmask:netmask4' \
		'gateway:ip4addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_route6_section()
{
	uci_validate_section network route6 "${1}" \
		'interface:string' \
		'target:cidr6' \
		'gateway:ip6addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_rule_section()
{
	uci_validate_section network rule "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr4' \
		'dest:cidr4' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_rule6_section()
{
	uci_validate_section network rule6 "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr6' \
		'dest:cidr6' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_switch_section()
{
	uci_validate_section network switch "${1}" \
		'name:string' \
		'enable:bool' \
		'enable_vlan:bool' \
		'reset:bool'
}

validate_switch_vlan()
{
	uci_validate_section network switch_vlan "${1}" \
		'device:string' \
		'vlan:uinteger' \
		'ports:list(ports)'
}

service_triggers()
{
	procd_add_reload_trigger network wireless

	procd_open_validate
	validate_atm_bridge_section
	validate_route_section
	validate_route6_section
	validate_rule_section
	validate_rule6_section
	validate_switch_section
	validate_switch_vlan
	procd_close_validate
}

shutdown() {
	/sbin/wifi down
	ifdown -a
	sleep 1
}
