#!/bin/sh

. /etc/wlan/wifi_conf

# === global parameter ===
g_sleep_time=60
g_thermal_onboard_file=/tmp/cache/wireless/thermal_board.conf
g_thermal_onchip_wifi0_file=/tmp/cache/wireless/thermal_wifi0.conf
g_thermal_onchip_wifi1_file=/tmp/cache/wireless/thermal_wifi1.conf
g_thermal_onchip_wifi2_file=/tmp/cache/wireless/thermal_wifi2.conf
g_onboard_sensor_name="onboard-ddr"
g_otpEnable="False"
g_debug_mode=
DEVICES="wl0 wl1"

[ ! -d "/tmp/cache/wireless" ] && /bin/mkdir -p /tmp/cache/wireless

debugecho() { [ "$g_debug_mode" = "1" ] && echo $@ > /dev/console; }
debugcat() { [ "$g_debug_mode" = "1" ] && cat $@ > /dev/console; }

# $1: sensor_name, $2: highestTemp, $3: lowestTemp,
# $4: otpEnable, $5: otpTriggerStatus, $6: otpTriggerCount, $7: otpTriggerLongestDuration
print_thermal_data()
{
	cat <<EOF
Name=$1
highestTemp=$2
lowestTemp=$3
otpEnable=$4
otpTriggerStatus=$5
otpTriggerCount=$6
otpTriggerLongestDuration=$7
EOF
}

is_under_cac() # $1: wifi-device
{
    [ -z "$(wl -i $1 dfs_status | grep ISM)" ] && return 0
    return 1
}

_get_onchip_temp_level() # $1(I): wifi-device, $2(O): temp, $3(O): level
{
	local _temp="Unknown"
	local _string=$(wl -i $1 phy_tempsense)

	if [ -n "$_string" ]; then
		_temp=$(echo "$_string" | awk '{print $1}')
	fi

	eval "$2=$_temp"
}

get_onchip_temp_level() # $1(I): wifi-device, $2(O): temp, $3(O): level
{
	local temp="Unknown"

	_get_onchip_temp_level $1 temp

	# temp=0 may mean :
	# 1. device under cac
	# 2. device just recover from cac within 2 secs, so we need to re-get temp
	# 3. the temp really be 0
	if [ "$temp" -eq 0 ]; then
        if is_under_cac "$1"; then
			temp="Unknown"
			level="Unknown"
		else
			sleep 2
			_get_onchip_temp_level $1 temp
        fi
	fi

	eval "$2=$temp"
}

update_thermal_onchip_data() # $1: wifi-device
{
    [ "$1" = "wl0" ] && dev="wifi0"
    [ "$1" = "wl1" ] && dev="wifi1"
	eval local thermal_onchip_file="\$g_thermal_onchip_${dev}_file"
	eval local chipName="BRCM6756_${dev}"
	local highestTemp lowestTemp
	local otpTriggerStatus=0 otpTriggerCount=0 otpTriggerLongestDuration=0
	local cur_temp cur_level
	local otpEnable

	get_onchip_temp_level $1 cur_temp

	[ -f $thermal_onchip_file ] && . $thermal_onchip_file

	if [ "$cur_temp" != "Unknown" ]; then
		highestTemp=${highestTemp:-$cur_temp}
		lowestTemp=${lowestTemp:-$cur_temp}
		[ "$cur_temp" -gt "$highestTemp" ] && highestTemp=$cur_temp
		[ "$cur_temp" -lt "$lowestTemp" ] && lowestTemp=$cur_temp
	fi
	if [ "${g_otpEnable}" = "1" ]; then
		[ "$otpTriggerStatus" = "0" ] && [ "$cur_level" = "1" -o "$cur_level" = "2" -o "$cur_level" = "3" ] && \
			otpTriggerCount=$((otpTriggerCount + 1))
		[ "$otpTriggerStatus" = "1" -o "$otpTriggerStatus" = "2" -o "$otpTriggerStatus" = "3" ] && \
			otpTriggerLongestDuration=$((otpTriggerLongestDuration + $g_sleep_time))
		otpTriggerStatus=$cur_level
	else
		otpTriggerCount=
		otpTriggerLongestDuration=
		otpTriggerStatus=Unknown
	fi

	print_thermal_data "$chipName" "$highestTemp" "$lowestTemp" "$g_otpEnable" \
		"$otpTriggerStatus" "$otpTriggerCount" "$otpTriggerLongestDuration" > "$thermal_onchip_file"

	debugcat $thermal_onchip_file
}

get_onboard_temp()
{
	#temp_i2c=`i2cget -y 0 0x48 0x00`
	temp_chip=`cat /sys/power/bpcm/select0 | awk '{print $2}'| awk -F '.' '{print $1}'`
	#t_tmp=$(($temp_i2c & 0x80 ))

	#if [ $t_tmp -gt 0  ]; then
	#	cur_temp=0
	#else
	#	cur_temp=$(( $temp_i2c & 0xff ))
	#fi

	#[ -z "$cur_temp" ] && echo "Unknown" || echo "$cur_temp"
	[ -z "$temp_chip" ] && echo "Unknown" || echo "$temp_chip"
}

get_onboard_level()
{
	cur_dc_wl0=$(wl -i wl0 dutycycle_cck 2>/dev/null | awk '{print $1}')
	cur_dc_wl1=$(wl -i wl1 dutycycle_cck 2>/dev/null | awk '{print $1}')
	if [ "$is_dual_band" = "0" ]; then
		cur_dc_wl2=$(wl -i wl2 dutycycle_cck 2>/dev/null | awk '{print $1}')
	fi

	for wlx in ${main_ifname_list}
	do
		eval now=\$cur_dc_$wlx
		if [ -n "$now" ]; then
			[ "$now" -eq "100" ] && echo "0" && return
			[ "$now" -lt "100" ] && echo "1" && return
		fi
	done
	echo "Unknown"
}

update_thermal_onboard_data()
{
	local highestTemp lowestTemp
	local otpTriggerStatus=0 otpTriggerCount=0 otpTriggerLongestDuration=0
	local cur_temp=$(get_onboard_temp)
	#local cur_level=$(get_onboard_level)

	[ -f $g_thermal_onboard_file ] && . $g_thermal_onboard_file

	if [ "$cur_temp" != "Unknown" ]; then
		highestTemp=${highestTemp:-$cur_temp}
		lowestTemp=${lowestTemp:-$cur_temp}
		[ $cur_temp -gt $highestTemp ] && highestTemp=$cur_temp
		[ $cur_temp -lt $lowestTemp ] && lowestTemp=$cur_temp
	fi
	if [ "${g_otpEnable}" = "True" ]; then
		[ "$otpTriggerStatus" = "0" ] && [ "$cur_level" = "1" ] && otpTriggerCount=$((otpTriggerCount + 1))
		[ "$otpTriggerStatus" = "1" ] && otpTriggerLongestDuration=$((otpTriggerLongestDuration + $g_sleep_time))
		otpTriggerStatus=$cur_level
	else
		otpTriggerCount=
		otpTriggerLongestDuration=
		otpTriggerStatus=Unknown
	fi

	print_thermal_data "$g_onboard_sensor_name" "$highestTemp" "$lowestTemp" "$g_otpEnable" \
		"$otpTriggerStatus" "$otpTriggerCount" "$otpTriggerLongestDuration" > "$g_thermal_onboard_file"

	debugcat $g_thermal_onboard_file
}

while : ; do
    for device in ${DEVICES}; do
		update_thermal_onchip_data $device
	done
	[ -n "$g_onboard_sensor_name" ] && update_thermal_onboard_data
	sleep $g_sleep_time
done

