. /usr/share/libubox/jshn.sh
LTE=" 1 2 3 4 5 7 8 12 13 14 17 18 19 20 25 26 28 29 30 32 34 38 39 40 41 42 43 46 48 66 71 "
SA=" 1 2 3 5 7 8 12 13 14 18 20 25 26 28 29 30 38 40 41 48 66 70 71 75 76 77 78 79 "
NSA=" 1 2 3 5 7 8 12 13 14 18 20 25 26 28 29 30 38 40 41 48 66 70 71 75 76 77 78 79 "

get_modem_bus()
{
    local modem_bus="1-1.2"
    node=$(uci -q get glmodem.global.usbnode)
    if [ -z "$node" ]; then
        build_in="$(cat /proc/gl-hw-info/build-in-modem 2>/dev/null)"
        [ -z "$build_in" -a "$1" = "usb" ] && build_in="$(cat /proc/gl-hw-info/usb-port 2>/dev/null)"
    else
        build_in=$node
    fi

    if [ "$(echo $build_in | grep -q ',';echo $?)" = "0" ]; then
        build_in_array="${build_in/,/ }"
		for modem in $build_in_array; do
		    if [ -z "$(echo $modem | grep '-')" ]; then
		        ls /sys/bus/pci/devices/* | grep -q "$modem"
		        [ "$(echo $?)" = "0" ] && modem_bus=$modem
		        break
		    else
		        ls /tmp/usbnode/* | grep -q "$modem"
		        [ "$(echo $?)" = "0" ] && modem_bus=$modem
		        break
		    fi
		done
    elif [ -z "$(echo $build_in | grep '-')" ]; then
        ls /sys/bus/pci/devices/* |grep -q "$build_in"
        [ "$(echo $?)" = "0" ] && modem_bus=$build_in
    else
        ls /tmp/usbnode/* | grep -q "$build_in"
        [ "$(echo $?)" = "0" ] && modem_bus=$build_in
    fi
    echo $modem_bus
}

get_modem_iface()
{
	local modem_iface=""
	local bus=$1
	if [ -n "$(echo $bus | grep ':')" -a -e "/proc/gl-hw-info/pcie-bus" ] && [[ $(cat /proc/gl-hw-info/pcie-bus 2>/dev/null) == *$bus* ]];then
		modem_iface="modem_$(echo ${bus%%:*})"
	elif [ -n "$(echo $bus | grep '-')" ]; then
		modem_iface="modem_$(echo $bus | sed 's/-/_/g' | sed 's/\./_/g')"
	fi
	echo $modem_iface
}

allow_bands()
{
	[ -f "/proc/gl-hw-info/build-in-modem" ] || return
	local mode="$1"
	local bands=`echo $2 | sed 's/ /:/g'`
	local bus=$(get_modem_bus)

	[ "$bands" = "" ] && return

	case $mode in
		*LTE*)
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"lte_band\",$bands\'
		;;
		SA*)
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_band\",$bands\'
		;;
		*NSA*)
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nsa_nr5g_band\",$bands\'
		;;
	esac
}

disable_bands()
{
	local mode="$1"
	local dis_bands="$2"

	local bands=$(eval echo \$$mode)
	for dis_band in $dis_bands
	do
		bands=`echo $bands | sed 's/\<'"$dis_band"'\>//'`
	done

	allow_bands "$mode" "$bands"
}

set_5G_mode()
{
	local lte_band="$1"
	local sa_band="$2"
	local nsa_band="$3"
	local bus=$(get_modem_bus)

	if [ "$lte_band" = "" -a "$sa_band" = "" -a "$nsa_band" = "" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",AUTO\'
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
	elif [ "$lte_band" = "" ] && [ "$sa_band" != "" -o "$nsa_band" != "" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",NR5G\'
		if [ "$sa_band" != "" -a "$nsa_band" = "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",2\'
		elif [ "$sa_band" = "" -a "$nsa_band" != "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",1\'
		elif [ "$sa_band" != "" -a "$nsa_band" != "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
		fi
	elif [ "$lte_band" != "" ] && [ "$sa_band" != "" -o "$nsa_band" != "" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",AUTO\'
		if [ "$sa_band" != "" -a "$nsa_band" = "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",2\'
		elif [ "$sa_band" = "" -a "$nsa_band" != "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",1\'
		elif [ "$sa_band" != "" -a "$nsa_band" != "" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
		fi
	elif [ "$lte_band" != "" ] && [ "$sa_band" = "" -o "$nsa_band" = "" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",LTE\'
	fi
}

set_5G_mode_mask()
{
	local lte_band="$1"
	local sa_band="$2"
	local nsa_band="$3"
	local bus=$(get_modem_bus)

	if [ "$lte_band" = "" -a "$sa_band" = "" -a "$nsa_band" = "" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",AUTO\'
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
	elif [ " $lte_band " = "$LTE" ] && [ " $sa_band " != "$SA" -o " $nsa_band " != "$NSA" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",NR5G\'
		if [ " $sa_band " != "$SA" -a " $nsa_band " = "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",2\'
		elif [ " $sa_band " = "$SA" -a " $nsa_band " != "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",1\'
		elif [ " $sa_band " != "$SA" -o " $nsa_band " != "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
		fi
	elif [ " $lte_band " != "$LTE" ] && [ " $sa_band " != "$SA" -o " $nsa_band " != "$NSA" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",AUTO\'
		if [ " $sa_band " != "$SA" -a " $nsa_band " = "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",2\'
		elif [ " $sa_band " = "$SA" -a " $nsa_band " != "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",1\'
		elif [ " $sa_band " != "$SA" -o " $nsa_band " != "$NSA" ];then
			eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"nr5g_disable_mode\",0\'
		fi
	elif [ " $lte_band " != "$LTE" ] && [ " $sa_band " = "$SA" -o " $nsa_band " = "$NSA" ];then
		eval gl_modem -B $bus AT \'AT+QNWPREFCFG=\"mode_pref\",LTE\'
	fi

}

handle_bands()
{
	local version=""
	local bus=$(get_modem_bus)
	for count in $(seq 1 10)
	do
		version=`gl_modem -B $bus AT ATI | grep Revision: | awk '{print $2}'`
		[ "$version" != "" ] && break
		sleep 1
	done

	local interface="$1"
	[ -n "$interface" ] || return
	local band_filter=`uci -q get network.$interface.band_filter_mode`
	local band_list=`uci -q get network.$interface.band_list`
	json_load "$band_list"
	json_get_values "lte" "LTE"
	case $version in
		*RM520NGL*)
			json_get_values "sa" "NR-SA"
			json_get_values "nsa" "NR-NSA"
			[ "$band_filter" = "0" ] && {
				set_5G_mode "$lte" "$sa" "$nsa"
				[ "$lte" != "" ] && allow_bands "LTE" "$lte"
				[ "$sa" != "" ] && allow_bands "SA" "$sa"
				[ "$nas" != "" ] && allow_bands "NSA" "$nsa"
			}
			[ "$band_filter" = "1" ] && {
				set_5G_mode_mask "$lte" "$sa" "$nsa"
				[ " $lte " != "$LTE" ] && disable_bands "LTE" "$lte"
				[ " $sa" != "$SA" ] && disable_bands "SA" "$sa"
				[ " $nsa" != "$NSA" ] && disable_bands "NSA" "$nsa"
			}
		;;
		*)
			[ "$band_filter" = "0" ] && {
				allow_bands "LTE" "$lte"
			}
			[ "$band_filter" = "1" ] && {
				disable_bands "LTE" "$lte"
			}
		;;
	esac	

}

modem_AT_set_band()
{
	local modem_iface=""
	local bus=$(get_modem_bus)
	if [ -f "/proc/gl-hw-info/build-in-modem" ];then
		modem_iface=$(get_modem_iface $bus)
	else
		return
	fi

	handle_bands $modem_iface
}

modem_AT_set_roaming()
{
	[ -f "/proc/gl-hw-info/build-in-modem" ] || return
	local bus=$(get_modem_bus)
	local modem_iface=$(get_modem_iface $bus)
	local roaming=`uci -q get network.$modem_iface.roaming`
	if [ "$roaming" = "0" ];then
		gl_modem -B $bus AT AT+QNWPREFCFG=\"roam_pref\",1
	else
		gl_modem -B $bus AT AT+QNWPREFCFG=\"roam_pref\",255
	fi

}

