#!/bin/sh
#
# Copyright (c) 2019 Qualcomm Technologies, Inc.
#
# All Rights Reserved.
# Confidential and Proprietary - Qualcomm Technologies, Inc.


. /lib/functions.sh
. /lib/wifi_interface_helper.sh

#Gives simple usage of how script works and whats expecting to receive as input
usage() {
	cat <<EOF
Usage: $0 [DBS_SBS|DBS|dbs_sbs|dbs]
EOF
	exit 1
}

#Enables vaps up after hw mode has changed
wifi_hw_mode_continue() {
    local driver_hw_mode=""
    mode=`retrieve_current_hw_mode`

    if [ $mode -eq 1 ]; then
        driver_hw_mode="DBS"
    else
        driver_hw_mode="DBS_SBS"
    fi

    if [ $prev_hw_mode = $mode ]; then
        echo "Switch Incomplete! HW mode is $driver_hw_mode"
    else
        echo "HW mode was changed to $driver_hw_mode"
    fi

    if [ $mode -eq 1 ]; then #DBS
        wifi_vaps_change_state "enable" "wifi0"
    elif [ $mode -eq 4 ]; then #DBS_SBS
        wifi_vaps_change_state "enable" "wifi0"
        wifi_vaps_change_state "enable" "wifi2"
    fi

}

#Receives an state as a parameter for a radio
#It either disables or enables vaps depending on that given state
wifi_vaps_change_state() {
    local vaps_state=$1
    local device=$2

    config_get disabled "$device" disabled
    [ "$disabled" = "1" ] && {
            echo "'$device' is disabled"
            set disable
    }
    config_get iftype "$device" type

    if [ "$vaps_state" = "disable" ]; then
        eval "${vaps_state}_$iftype '$device' 1" || echo "$device($iftype): $vaps_state failed"
    else
        if eval "type ${vaps_state}_$iftype" 2>/dev/null >/dev/null; then
            eval "scan_$iftype '$device'"
            eval "${vaps_state}_$iftype '$device' 1 1" || echo "$device($iftype): $vaps_state failed"
        elif [ ! -f /lib/netifd/wireless/$iftype.sh ]; then
            echo "$device($iftype): Interface type not supported"
        fi
    fi
}

#Starting point where user input get checked, vaps get disabled and
#called to driver is made to make the hw mode switch
wifi_hw_mode() {
    local user_hwmode=$1
    if [ "$user_hwmode" != "DBS_SBS" ] && [ "$user_hwmode" != "DBS" ] && [ "$user_hwmode" != "dbs_sbs" ] && [ "$user_hwmode" != "dbs" ]; then
        echo "wrong input for hw mode"
        usage
        exit
    fi

    if [ "$user_hwmode" == "DBS_SBS" ] || [ "$user_hwmode" == "dbs_sbs" ]; then
        user_hwmode="DBS_SBS"
    else
        user_hwmode="DBS"
    fi


    dynamic_hw_mode=`grep "dynamic_hw_mode" /ini/internal/global_i.ini | grep -m1 -v "^[#]" | awk -F'=' '{print $2}'`
    if [ "$dynamic_hw_mode" != "1" ]; then
        echo "dynamic mode not on!"
        exit
    fi

    driver_hw_mode=`retrieve_current_hw_mode`
    prev_hw_mode=$driver_hw_mode
    if [ $driver_hw_mode -eq 1 ]; then
        driver_hw_mode="DBS"
    elif [ $driver_hw_mode -eq 4 ]; then
        driver_hw_mode="DBS_SBS"
    else
        echo "$driver_hw_mode is unsupported for dynamic change"
        exit
    fi

    if [ "$driver_hw_mode" = "$user_hwmode" ]; then
        echo "$1 is already current hw mode"
        exit
    fi

    if [ "$user_hwmode" = "DBS" ]; then
        user_hwmode=1
        wifi_vaps_change_state "disable" "wifi0"
        wifi_vaps_change_state "disable" "wifi2"
        echo "switching hw_mode to DBS..."
    elif [ "$user_hwmode" = "DBS_SBS" ]; then
        user_hwmode=4
        wifi_vaps_change_state "disable" "wifi0"
        echo "switching hw_mode to DBS_SBS..."
    fi


    eval "switch_hw_mode $user_hwmode"
    wifi_hw_mode_continue
}

DEVICES=
DRIVERS=
include /lib/wifi
scan_wifi

trap 'wifi_trap; exit' INT TERM ABRT QUIT ALRM
hw_mode=$1
prev_hw_mode=""


case "$1" in
	--help|help) usage;;
	*) wifi_hw_mode "$1";;
esac

