#!/bin/sh /etc/rc.common
# Copyright (C) 2010-2018 OpenWrt.org

START=40

SET_WLAN="iwpriv wlan1"
SET_WLAN_PARAM="$SET_WLAN set_mib"
IFCONFIG=ifconfig

wlan_power_ctrl() {
	local on=$1
	local name
	local power
	local pcie_device
	linux_version=`uname -r`
	local version_num
	version_num=`echo ${linux_version:0:1}`
	#echo $version_num
	if [ $version_num == "3" ]; then
	   echo "this is 3 version: $linux_version"
	else
	   echo "this is 5 version: $linux_version"
	fi
	#hwacc w 0xD401E0C8 0x000010c2
    sleep 3
	if [ $version_num == "3" ]; then
		for name in  $(ls /sys/devices | grep "rfkill"); do
			[ ! -e "/sys/devices/${name}/pwr_ctrl" ] && continue
			power=0
			$(cat /sys/devices/${name}/pwr_ctrl | grep -q on) && power=1
			[ "$power" == "$on" ] && continue
			echo $on > /sys/devices/${name}/pwr_ctrl
		done
	else
		for name in  $(ls /sys/devices/platform | grep "rfkill"); do
			[ ! -e "/sys/devices/platform/${name}/pwr_ctrl" ] && continue
			power=0
			$(cat /sys/devices/platform/${name}/pwr_ctrl | grep -q on) && power=1
			[ "$power" == "$on" ] && continue
			echo $on > /sys/devices/platform/${name}/pwr_ctrl
		done
	fi
	
}

wlan_ops_lock() {
	echo mwlan > /sys/power/wake_lock
}

wlan_ops_unlock() {
	echo mwlan > /sys/power/wake_unlock
}

wlan_ubus_event() {
	case $1 in
	start )
		ubus send mwlan.stat.action ' { "stat" : "start" } ' ;;
	up )
		ubus send mwlan.stat.action ' { "stat" : "up" } ' ;;
	down )
		ubus send mwlan.stat.action ' { "stat" : "down" } ' ;;
	* )
		echo "unknown option ($1)" ;;
	esac
}

wlan_set_ap_isolate() {
	__wlan_set_ap_isolate() {
		local _ifname _isolate _mode

		config_get _isolate $1 isolate
		config_get _ifname $1 ifname
		config_get _mode $1 mode

		if [ $_mode == "ap" ]; then
			if [ $_isolate == "1" ]; then
				iwpriv $_ifname set_mib block_relay=1
			else
				iwpriv $_ifname set_mib block_relay=0
			fi
		fi
	}

	config_load wireless
	config_foreach __wlan_set_ap_isolate wifi-iface
}

wlan_check_probe_result() {
	__wlan_check_probe_result() {
		local _ifname _device _disabled

		config_get _device $1 device
		config_get _disabled $_device disabled
		config_get _phyname $_device phy

        if [ $_disabled == "0" ]; then
		    if [ ! -d "/sys/kernel/debug/ieee80211/$_phyname" ]; then
			    echo "$_phyname is not present, retry power on" > /dev/kmsg
			    wlan_power_ctrl 0
				sleep 1
				wlan_power_ctrl 1
		    fi
		fi
	}

	config_load wireless
	config_foreach __wlan_check_probe_result wifi-iface
}

start() {
	#linux_version=`uname -r`
	#local version_num
	#version_num=`echo ${linux_version:0:1}`

	wlan_ops_lock
	uci set wireless.AP0_2G.log_level=0
	uci commit wireless

	wlan_ubus_event start
	wlan_power_ctrl 1
	wlan_check_probe_result
	/sbin/wifi
	#wlan_set_isolate
	wlan_ubus_event up
	#if [ $version_num != "3" ]; then
		wlan_ops_unlock
	#fi
}

boot() {
	#hwacc w 0xD4050018 0x00000001
	sleep 2
	wlan_check_wifi_exist() {
		local _ifname _device _disabled

		config_get _device $1 device
		config_get _disabled $_device disabled
		config_get _ifname $1 ifname

		if [ $_disabled == "1" ]; then
			mkdir -p /var/wifi_ok
			touch /var/wifi_ok/$_device
		else
			ifconfig -a | grep -q $_ifname && {
				mkdir -p /var/wifi_ok
				touch /var/wifi_ok/$_device
			}
		fi
	}

	start
	sleep 10 # this delay will be less after driver remove debug log
	config_load wireless
	config_foreach wlan_check_wifi_exist wifi-iface
}

stop() {
	#linux_version=`uname -r`
	#local version_num
	#version_num=`echo ${linux_version:0:1}`
	#if [ $version_num != "3" ]; then
		wlan_ops_lock
	#fi
	/sbin/wifi down
	sleep 1
	wlan_power_ctrl 0
	wlan_ubus_event down
	wlan_ops_unlock
}

restart() {
	local para_num=$#
	local reset=0
	#linux_version=`uname -r`
	#local version_num
	#version_num=`echo ${linux_version:0:1}`
	#if [ $version_num != "3" ]; then
		wlan_ops_lock
	#fi
	if [ $para_num != "0" ]; then
		reset=$1
	fi
	if [ $reset == "5803" ]; then
		echo "reset asr5803 wifi" > /dev/kmsg
		#/sbin/wifi down
		#sleep 1
		wlan_power_ctrl 0
		sleep 1
	fi

	wlan_power_ctrl 1
	if [ $reset == "5803" ]; then
		wlan_check_probe_result
	fi
	/sbin/wifi
	wlan_set_ap_isolate
	if [ $reset == "5803" ]; then
		sleep 10
	fi
	#if [ $version_num != "3" ]; then
		wlan_ops_unlock
	#fi
}

reload() {
	#linux_version=`uname -r`
	#local version_num
	#version_num=`echo ${linux_version:0:1}`
	#if [ $version_num != "3" ]; then
		wlan_ops_lock
	#fi
	wlan_power_ctrl 1
	/sbin/wifi reload
	#wlan_set_ap_isolate
	#if [ $version_num != "3" ]; then
		wlan_ops_unlock
	#fi
}
