#!/bin/sh
#version=1.0
#2022-4-14 by gaga
if [ "$2" = "" ];then
        echo "Usage:$0 [conf] [mode]"
        exit 0
fi
source $1 #加载配置文件数据

#删除以下三行
#MS_CHECK_HEADER="ms_check:"
#MS_ERROR_HEADER="ms_error:"
#MS_SUCCESS_HEADER="ms_success"

#统一log输出模板
print_log()
{
        if [ "$1" == "check" ];then
                echo "ms_check: $2"
        fi
        if [ "$1" == "error" ];then                                              
                echo "ms_error: $2"                                             
        fi
        if [ "$1" == "success" ];then                                              
                echo "ms_success: $2"                                             
        fi
        if [ "$1" == "info" ];then
                echo "ms_info: $2"
        fi
}

#提前用ubus call查询设备基础信息，其中cellular需要多查1次
SUMMARY=`ubus call yruo_status get '{"base":"summary"}'`
CELLULAR=`ubus call yruo_status get '{"base":"yruo_celluar"}'`
cellular_version=`echo $CELLULAR | awk -F '"version": ' '{print $2}' |awk -F ',' '{print $1}'`
if [ "$cellular_version" == "" ];then
	sleep 5
	CELLULAR=`ubus call yruo_status get '{"base":"yruo_celluar"}'`
fi
#初始化加密芯片参数
PINFO_PRODUCTID=0
PINFO_CELLULAR=""
PINFO_HWVER=""
PINFO_OEM=""
PINFO_PN1=""
DINFO_MODEL=""
DINFO_CELLULAR_VERSION=""
DINFO_CELLULAR_MODEL=""

switch_pinfo_to_cellular_name()
{
	#根据加密芯片转化蜂窝模块型号
	case $PINFO_CELLULAR in
	"L00J0")
	PINFO_CELLULAR_NAME="EC25J"
	;;
	"L00AF")
	PINFO_CELLULAR_NAME="EC25AF"
	;;
	"L04AF")
	PINFO_CELLULAR_NAME="EC25AFX"
	;; 
	"L00AU")                                                    
	PINFO_CELLULAR_NAME="EC25AU"
	;; 
	"L04AU")                                                    
	PINFO_CELLULAR_NAME="EC25AUX"
	;; 
	"L00E0")
	PINFO_CELLULAR_NAME="EC25E"
	;; 
	"L04EU")
	PINFO_CELLULAR_NAME="EC25EUX"
	;; 
	"L01CE")                                                    
	PINFO_CELLULAR_NAME="EC20CE"
	;;
	"L06CN")
	PINFO_CELLULAR_NAME="EC200SCN"
	;; 
	"L01CN")
	PINFO_CELLULAR_NAME="EC200CN"
	;; 
	 "L02G0")
	PINFO_CELLULAR_NAME="EG25G"
	;; 
	"500GL")
	PINFO_CELLULAR_NAME="RM500QGL"
	;; 
	"552CN")
	PINFO_CELLULAR_NAME="FM650CN"
	;; 
	*)
	PINFO_CELLULAR_NAME="None"
	;; 
	esac
}

get_urtool()
{
	#转化加密芯片数值
	PINFO_PRODUCTID=`urtool -g  | grep "product" | awk -F ': ' '{print $2}'`
	PINFO_PN1=`urtool -g  | grep "sonboardsn" | awk -F ': ' '{print $2}'`
	PINFO_CELLULAR=${PINFO_PN1:0:5}
	switch_pinfo_to_cellular_name
	PINFO_PN2=`urtool -g  | grep "reserved        :" | awk -F ': ' '{print $2}'`
	PINFO_HWVER=`urtool -g  | grep "hwver" | awk -F ': ' '{print $2}'`
	PINFO_OEM=`urtool -g  | grep "oem" | awk -F ': ' '{print $2}'`
	DINFO_MODEL=`echo $SUMMARY | awk -F '"model": ' '{print $2}' |awk -F ',' '{print $1}' |awk -F '"' '{print $2}'`
}

check_pinfo()
{
	#检查加密芯片数据
	print_log info "check pinfo...."
	if [ "$PINFO_PN1" != "$C_SONBOARDSN" ];then
		print_log error "check pinfo sonboardsn $PINFO_PN1 != $C_SONBOARDSN"
		echo "check_SONBOARDSN:$PINFO_PN1 != $C_SONBOARDSN" >> result
	else
		print_log success "check SONBOARDSN"
		echo "check_SONBOARDSN:success" >> result
	fi
	if [ "$PINFO_PN2" != "$C_RESERVED" ];then                                                               
		print_log error "check pinfo RESERVED $PINFO_PN2 != $C_RESERVED"
		echo "check_RESERVED:$PINFO_PN2 != $C_RESERVED" >> result
	else
		print_log success "check RESERVED"
		echo "check_RESERVED:success" >> result
	fi
	if [ "$PINFO_OEM" != "$C_OEM" ];then
		print_log error "check pinfo OEM $PINFO_OEM != $C_OEM"
		echo "check_OEM:$PINFO_OEM != $C_OEM" >> result
	else
		print_log success "check OEMid"
		echo "check_OEM:success" >> result
	fi
	if [ "$PINFO_PRODUCTID" == "$C_PRODUCT" ];then
		echo "check_PRODUCT:success" >> result
		print_log success "check PRODUCT"
	else
		print_log error "check product $PINFO_PRODUCTID != $C_PRODUCT"
		echo "check_PRODUCT:product $PINFO_PRODUCTID != $C_PRODUCT" >> result
	fi
}

check_raminfo()
{
	#检查ram
	print_log info "check raminfo...."
	ramsize=`echo $SUMMARY | awk -F '"memory_total": ' '{print $2}' | awk -F ',' '{print $1}'`
	if [ "$ramsize" = "$C_RAM" ];then
			echo "check_RAM:success" >> result
			print_log success "check RAM"
	else
			print_log error "check raminfo $ramsize != $C_RAM"
			echo "check_RAM:raminfo $ramsize != $C_RAM" >> result
	fi
}

check_flashinfo()
{
	#检查flash挂载
	print_log info "check flashinfo...."
	flash_total=`echo $SUMMARY | awk -F '"flash_total": ' '{print $2}' | awk -F ',' '{print $1}'`
	if [ "$flash_total" = "$C_FLASH" ];then
			echo "check_FLASH:success" >> result
			print_log success "check FLASH"
	else
			print_log error "check flashinfo $flash_total != $C_FLASH"
			echo "check_FLASH:flashinfo $flash_total != $C_FLASH" >> result
	fi
}

check_cellular_module()
{
	#检查蜂窝版本号
	print_log info "check cellular module...."
	version=`echo $CELLULAR | awk -F '"version": ' '{print $2}' |awk -F ',' '{print $1}'`
	result=`echo $version |grep $PINFO_CELLULAR_NAME`
	if [ "$result" != "" ];then
			echo "check_CELLULAR:success" >> result
			print_log success "check CELLULAR MODULE"
	else
			print_log error "check cellular module $PINFO_CELLULAR_NAME not in $version ,hw != sw"
			echo "check_CELLULAR:module $PINFO_CELLULAR_NAME not in $version ,hw != sw" >> result
	fi

	#检查当前使用卡槽并返回
	sim=`echo $CELLULAR | awk -F '"cur_sim": ' '{print $2}' |awk -F ',' '{print $1}' |awk -F '"' '{print $2}'`
	if [ "$sim" == "SIM1" ];then
		return 1
	else
		return 2
	fi
}

check_ping_cellular_network()
{
	#检查蜂窝ping
	print_log info "check ping cellular SIM$1...."
	ping=`ping -I cellular0 -c $C_CELLULAR_PING_COUNT $C_CELLULAR_PING_IP`
	check_network=`echo "$ping" |grep "transmitted" | wc -l`
	if [ $check_network == 1 ];then
		ping=`echo $ping | awk -F 'received, ' '{print $2}'| awk -F '%' '{print $1}'`
	else
        ping=100
	fi	
	
	if [ "$ping" -lt "$C_CELLULAR_PING_THRESHOLD" ];then
        	echo "check_CELLULAR_SIM$1_PING:success $ping% < $C_CELLULAR_PING_THRESHOLD%" >> result
		print_log success "check CELLULAR SIM$1 PING"
	else
		print_log error "check SIM$1 packet loss $ping% > $C_CELLULAR_PING_THRESHOLD%"
		echo "check_CELLULAR_SIM$1_PING:packet loss $ping% > $C_CELLULAR_PING_THRESHOLD%" >> result
	fi
}

check_cellular_signal()
{        
	#检查蜂窝信号值
	print_log info "check cellular SIM$1 signal...."
	signal=`echo $CELLULAR | awk -F '"signal": ' '{print $2}' |awk -F ',' '{print $1}' |awk -F '-' '{print $2}' | awk -F 'd' '{print $1}'`
	if [ "-$signal" -gt "$C_CELLULAR_SIGNAL" ];then
		echo "check_CELLULAR_SIM$1_SIGNAL:success -$signal > $C_CELLULAR_SIGNAL" >> result
			print_log success "check CELLULAR SIM$1 SIGNAL"
	else                     
			print_log error "check cellular SIM$1 signal -$signal < $C_CELLULAR_SIGNAL"
			echo "check_CELLULAR_SIM$1_SIGNAL: -$signal < $C_CELLULAR_SIGNAL" >> result
	fi
}

version()
{
	#打印一些设备基础信息，供生产人员排查
	firmware_version=`echo $SUMMARY | awk -F '"firmware_ver": ' '{print $2}' | awk -F '"' '{print $2}'`
	hardware_version=`echo $SUMMARY | awk -F '"hardware_ver": ' '{print $2}' | awk -F '"' '{print $2}'`
	cellular_version=`echo $CELLULAR | awk -F '"version": ' '{print $2}' |awk -F ',' '{print $1}'`
	imei=`echo $CELLULAR | awk -F '"imei": ' '{print $2}' |awk -F ',' '{print $1}' |awk -F ' ' '{print $1}' | awk -F '"' '{print $2}'`
	echo "check_FIRMWARE:success-$firmware_version" >> result
	echo "check_HARDWARE:success-$hardware_version" >> result
	echo "check_CELLULAR_VERSION:success-$cellular_version" >> result
	echo "check_IMEI:success-$imei" >> result
	echo "check_MODEL:success-$DINFO_MODEL" >> result
}

check_gps_module()
{
	print_log info "check gps module...."
	sleep 5
	latitude=`ubus call yruo_industrial_gps_status get | grep latitude | awk -F ': ' '{print $2}' | awk -F '"' '{print $2}'`
	longitude=`ubus call yruo_industrial_gps_status get | grep longitude | awk -F ': ' '{print $2}' | awk -F '"' '{print $2}'`
	status=`ubus call yruo_industrial_gps_status get | grep '"status' | awk -F ': ' '{print $2}' | awk -F '"' '{print $2}'`
	latitude_result=`echo $latitude |grep '24.'`
	longitude_result=`echo $longitude |grep '118.'`
	if [ "$status" == "53" ] && [ "$latitude_result" != "" ] && [ "$longitude_result" != "" ];then
		echo "check_GPS:success" >> result
		print_log success "check GPS SUCCESS==latitude:$latitude  longitude:$longitude"
	else
		print_log error "check gps module failed!" 
		echo "check_GPS:failed==status:$status  latitude:$latitude  longitude:$longitude" >> result
	fi
}

check_wifi_module_connect()
{
	#根据设备配置选择开启wifi
	#print_log info "check wifi start...."
	
	if [ "$C_ifWIFI_24G" == "1" ] && [ "$1" == "24" ];then
		ip=`ifconfig eth0 | grep Bcast | sed 's/:/ /g' | awk '{print $3}' | awk -F '.' '{print $4}'`
		#ubus call yruo_wifi set '{"base":"yruo_wifi","index":"1","value":{"enabled":1,"proto":1,"mode":1,"ssid_sta":"wifi-2.4","bssid_sta":"","ip":"'$C_24G_IP'.'$ip'","mask":"255.255.255.0","gate":"'$C_24G_IP'.1"}}'
		ubus call yruo_wifi set '{"base":"yruo_wifi","index":"1","value":{"enabled":1,"proto":2,"mode":1,"ssid_sta":"wifi-2.4","bssid_sta":""}}'
		ubus call yruo_wifi set '{"base":"yruo_wifi","index":"2","value":{"enabled":0}}'
		ubus call yruo_apply apply
		sleep 40
	fi
	if [ "$C_ifWIFI_5G" == "1" ] && [ "$1" == "5" ];then
		ip=`ifconfig eth0 | grep Bcast | sed 's/:/ /g' | awk '{print $3}' | awk -F '.' '{print $4}'`
		ubus call yruo_wifi set '{"base":"yruo_wifi","index":"2","value":{"enabled":1,"proto":2,"mode":1,"ssid_sta":"wifi-5","bssid_sta":""}}'
		ubus call yruo_wifi set '{"base":"yruo_wifi","index":"1","value":{"enabled":0}}'
		ubus call yruo_apply apply
		sleep 40
	fi
	
}

check_wifi_ping()
{
	#根据设备配置测试wifi的ping
	print_log info "check wifi ping..."
	if [ "$C_ifWIFI_24G" == "1" ] && [ "$1" == "24" ];then
		ping=`ping -I wlan0 -c $C_24G_PING_COUNT $C_24G_PING_IP`                                                                                         
		check_network=`echo "$ping" |grep "transmitted" | wc -l`                                                                                                       
		if [ $check_network == 1 ];then                                                                                                                                
			ping=`echo $ping | awk -F 'received, ' '{print $2}'| awk -F '%' '{print $1}'`                                                                          
		else                                                                                                                                                           
			ping=100                                                                                                                                                       
		fi                                                                                                                                                             
																																								   
		if [ "$ping" -lt "$C_24G_PING_THRESHOLD" ];then                                                                                                           
			echo "check_WIFI_24G_PING:success $ping% < $C_24G_PING_THRESHOLD%" >> result                                                                
			print_log success "check WIFI 2.4G PING"                                                                                                          
		else                                                                                                                                                           
			print_log error "check WIFI 2.4G packet loss $ping% > $C_24G_PING_THRESHOLD%"                                                                         
			echo "check_WIFI_2.4G_PING:packet loss $ping% > $C_24G_PING_THRESHOLD%" >> result                                                               
		fi
	fi	
        if [ "$C_ifWIFI_5G" == "1" ] && [ "$1" == "5" ];then                                                                                                                            
			ping=`ping -I ath1 -c $C_5G_PING_COUNT $C_5G_PING_IP`                                                                                              
			check_network=`echo "$ping" |grep "transmitted" | wc -l`                                                                                               
			if [ $check_network == 1 ];then                                                                                                                        
				ping=`echo $ping | awk -F 'received, ' '{print $2}'| awk -F '%' '{print $1}'`                                                                  
			else                                                                                                                                                   
					ping=100                                                                                                                                       
			fi                                                                                                                                                     
																																								   
			if [ "$ping" -lt "$C_5G_PING_THRESHOLD" ];then                                                                                                       
				echo "check_WIFI_5G_PING:success $ping% < $C_5G_PING_THRESHOLD%" >> result                                                                 
				print_log success "check WIFI 5G PING"                                                                                                       
			else                                                                                                                                                   
				print_log error "check WIFI 5G packet loss $ping% > $C_5G_PING_THRESHOLD%"                                                                 
				echo "check_WIFI_5G_PING:packet loss $ping% > $C_5G_PING_THRESHOLD%" >> result                                                             
			fi                                                                                                                                                     
        fi
}

get_urtool

#./UR75V2_autotest.sh test.conf check(check模式会测试加密芯片是否正确；test模式会进行实际功能测试)
#全检工具会先用check模式，通过后才进行功能测试。
if [ "$2" == "check" ];then
	echo "[RESULT]" > result
	#print_detail_info
	check_wifi_module_connect 24
	check_pinfo
	version
	check_raminfo
	check_flashinfo
	if [ "$C_ifGPS" == "1" ];then
		#提前开启GPS功能，注意如果GPS本来就开启呢？
		echo -e "enable\ncon t\ngps config\nenable startup\nshutdown\nno shutdown" | vtysh
	fi
	echo "MILESIGHT=CHECK DONE" >> result #工具根据该内容判断测试结束
else
	if [ "$C_ifCELLULAR" == "1" ];then
		check_cellular_module
		simA=$?
		simB=`expr 3 - $simA`
	fi
	check_wifi_ping 24
	if [ "$C_ifCELLULAR_PING" == "1" ] && [ "$C_ifCELLULAR" == "1" ];then
		check_ping_cellular_network $simA
		check_cellular_signal $simA
		if [ "$C_SIM_COUNT" == "2" ];then
			#有两个卡槽，则检测另一个SIM的信号值
			echo -e "enable\ncon t\nlink-backup wwan$simB\nno shutdown\nend\ncon t\nlink-backup wwan$simA\nshutdown" | vtysh
			CELLULAR=`ubus call yruo_status get '{"base":"yruo_celluar"}'`
			check_cellular_signal $simB
		fi
	fi
	check_wifi_module_connect 5
	#sleep 30
	if [ "$C_ifGPS" == "1" ];then
		check_gps_module
	fi
	check_wifi_ping 5
	echo "MILESIGHT=TEST DONE" #工具根据该内容判断测试结束
	echo "MILESIGHT=RESULT DONE" >> result #工具根据该内容判断测试结束
fi