#!/bin/sh

. $gCommonDir/monitor_ctrl.sh
. $gCommonDir/module_err.sh

#==============================================
#=============== Local function ===============
#==============================================
Aiw344_l_WriteCfg()
{
    local data="$1"

    if [ ! -z $2 ] && [ "$2" = "append" ]; then
        echo "$1" >> $gModuleCfgFile
    else        
        echo "$1" > $gModuleCfgFile
    fi

    if [ $? -eq 0 ]; then
        return 0
    else
        return $MErr_WDEVNODE
    fi
}

Aiw344_l_Help()
{
    echo "Usage: $0 $gRunCmd $@"
}

Aiw344_l_ATCfunc()
{
    Dbg3 "Aiw344_$gRunCmd"

    Monitor_GetState
    local ret=$?
    if [ $ret -eq $M_RUN ]; then
        gRetStr=$(MErr_strCode2Msg $MErr_MTRRUN)
        return $ret
    fi

    ATCOMM "AT+CFUN=$1" "2"
    ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    return 0
}

#==============================================
#================ API function ================
#==============================================
Aiw344_iSetUsbMode()
{
    [ -z $1 ] && return $MErr_NULL
    
    if [ $1 -ne 29 ] && [ $1 -ne 17 ]; then
        #Msg "Unsupported usbmode $1"
        return $MErr_USBMODEARG
    fi

    ATCOMM "AT+GTUSBMODE=$1"
    local ret=$?
    if [ $ret -ne 0 ]; then
        #Msg "Set to usbmode failed."
        gRetStr="AT error!"
        return $ret
    fi

    return 0
}

Aiw344_iCheckSim()
{
    ATCOMM "AT+GTUSIM?"
    local ret=$?
    if [ $ret -eq 0 ]; then
        return 0
    fi

    gRetStr="AT error!"
    return $ret
}

Aiw344_iConnect()
{
    ATCOMM 'AT\$QCRMCALL=1,1,3' '2'
    local ret=$?
    if [ $ret -eq 0 ]; then
        return 0
    fi

    gRetStr="AT error!"
    return $ret
}

Aiw344_iDisConnect()
{
    ATCOMM 'AT\$QCRMCALL?' '1'
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    local status=`cat $gATLogFile | grep '$QCRMCALL:' | cut -d' ' -f1`
    [ -z "$status" ] && return $MErr_RMCALL
    
    ATCOMM 'AT\$QCRMCALL=0,1,3' '1'
    local ret=$?
    if [ $ret -eq 0 ]; then
        return 0
    fi

    gRetStr="AT error!"
    return $ret
}


#==============================================
#================ Command Set =================
#==============================================
Aiw344_Init()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    local ret=`grep "^Aiw344_[^a-z].*" ${gModuleDir}/${gModuleName}/${gModuleName}.sh | cut -d'_' -f2 | sed 's/()//g' > $gModuleListFile`
    ret=$? && [ $ret -ne 0 ] && return $MErr_WRITELIST

    ProbeSerial 6 $gCfgPortSet
    ret=$? && [ $ret -ne 0 ] && gRetStr="Probe serial failed!" && return $ret

    Aiw344_l_WriteCfg "gDevNode=$gDevNode"
    ret=$? && [ $ret -ne 0 ] && return $MErr_WDEVNODE

    return 0
}

Aiw344_GetInfo()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "ATI"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    Log_ModuleCmd "[ATI]"
    cat $gATLogFile | grep ":" >> $gATCmdDir/$gRunCmd.txt
    [ $? -ne 0 ] && return $MErr_WRITEINFO

    Log_ShowCmdResult

    return 0
}

Aiw344_GetSignal()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CSQ?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    local status=`cat $gATLogFile | grep "+CSQ:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PSIGNAL
    Log_ModuleCmd "[+CSQ]: $status"
    # Extract RSSI
    status=`echo $status | cut -d',' -f1`
    Log_KeyToCmd "$KEY_RSSI" "$status" title 
    
    ATCOMM "AT+CESQ"
    ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    status=`cat $gATLogFile | grep "+CESQ:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PSIGNAL
    
    Log_ModuleCmd "[+CESQ]: $status" "append"
    Log_ShowCmdResult

    return 0
}

Aiw344_GetOperator()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+COPS?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    local status=`cat $gATLogFile | grep "+COPS:" | cut -d':' -f2 | sed 's/^ //g' | sed 's/ /_/g'`
    [ -z $status ] && return $MErr_POPERATOR
    
    Log_ModuleCmd "[+COPS]: $status"
    Log_ShowCmdResult

    return 0
}

#
# Get the state of PS attachment
#
Aiw344_GetPSState()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CGATT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    local status=`cat $gATLogFile | grep "+CGATT:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PPSSTATE
    
    Log_ModuleCmd "[+CGATT]: $status"
    Log_ShowCmdResult

    return 0
}

Aiw344_GetBand()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+GTACT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    local status=`cat $gATLogFile | grep "+GTACT:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PBAND
    
    Log_ModuleCmd "[+GTACT]: $status"
    Log_ShowCmdResult
    
    return 0
}

#
# Get Radio Access Technology
#
Aiw344_GetRat()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+GTRAT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    local status=`cat $gATLogFile | grep "+GTRAT:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PRAT
    
    Log_ModuleCmd "[+GTRAT]: $status"
    Log_ShowCmdResult

    return 0
}

#
# Get Current Cell Information
#
Aiw344_GetCellInfo()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+GTCCINFO?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    Log_ModuleCmd "[AT+GTCCINFO?]"
    cat $gATLogFile | grep -v 'OK' | grep -v '+GTCCINFO' >> $gATCmdDir/$gRunCmd.txt
    [ $? -ne 0 ] && return $MErr_PCELLINFO

    Log_ShowCmdResult

    return 0
}

Aiw344_GetApn()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CGDCONT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    local status=`cat $gATLogFile | grep "CGDCONT:" | grep '"IP"' | cut -d',' -f3 | sed 's/\"//g'`
    [ -z $status ] && return $MErr_PAPN
    
    Log_ModuleCmd "[+CGDCONT]: $status"
    Log_KeyToCmd "$KEY_APN" "$status" title 
    Log_ShowCmdResult

    return 0
}

Aiw344_GetIP()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+GTUSBMODE?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    local mode=`cat $gATLogFile | grep "+GTUSBMODE:" | cut -d' ' -f2`
    [ -z $mode ] && return $MErr_PIPUSBMODE
    
    #
    # Get IP with RmNet or MBIM mode 
    #
    ATCOMM "AT+CGDCONT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    local status=`cat $gATLogFile | grep "CGDCONT:" | grep '"IP"' | cut -d',' -f4 | sed 's/\"//g'`
    [ -z $status ] && return $MErr_PIP

    #
    # Get IP with MBIM mode only if not get IP above
    #
    if [ $mode -eq 29 ] && [ "$status" = "0.0.0.0" ]; then    
        status=`cat $gATLogFile | grep '+CGDCONT: 4' | cut -d',' -f4 | sed 's/\"//g'`
    fi
    [ -z $status ] && return $MErr_PIP
    
    Log_ModuleCmd "[+CGDCONT]: $status"
    Log_KeyToCmd "$KEY_IP" "$status" title 
    Log_ShowCmdResult

    return 0
}

Aiw344_GetPin()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    ATCOMM "AT+CPIN?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
   
    local status=`cat $gATLogFile | grep "CPIN:" | cut -d' ' -f2 | sed 's/\r//g'`
    [ -z $status ] && return $MErr_PPIN
    
    Log_ModuleCmd "[+CPIN]: $status"
    Log_KeyToCmd "$KEY_PIN" "$status" title 
    Log_ShowCmdResult

    return 0
}

Aiw344_GetPinLock()
{
    Dbg3 "Aiw344_$gRunCmd"
   
    ATCOMM 'AT+CLCK=\"SC\",2'
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret 
    
    local status=`cat $gATLogFile | grep "+CLCK:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PPINLOCK
    
    Log_ModuleCmd "[+CLCK]: $status"
    Log_ShowCmdResult
    
    return $status
}

#
# Get usbmode
# RMNET: 17, MBIM: 29
#
Aiw344_GetUsbMode()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+GTUSBMODE?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    local status=`cat $gATLogFile | grep "+GTUSBMODE:" | cut -d' ' -f2`
    [ -z $status ] && return $MErr_PUSBMODE
    
    Log_ModuleCmd "[+GTUSBMODE]: $status"
    Log_KeyToCmd "$KEY_USBMODE" "$status" title 
    Log_ShowCmdResult
    
    return 0
}

#
# Get registration status
#
Aiw344_GetRegStatus()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CEREG?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    local status=`cat $gATLogFile | grep "+CEREG:" | cut -d' ' -f 2`
    [ -z $status ] && return $MErr_PREGSTATUS
    
    Log_ModuleCmd "[+CEREG]: $status"
    status=`echo $status | cut -d',' -f 2`
    Log_KeyToCmd "$KEY_REGS" "$status" title 
    Log_ShowCmdResult

    return 0
}

Aiw344_GetAuth()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+MGAUTH?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    Log_ModuleCmd "[AT+MGAUTH?]"
    cat $gATLogFile | grep -v 'OK' | grep -v 'AT+MGAUTH?' >> $gATCmdDir/$gRunCmd.txt
    
    [ $? -ne 0 ] && return $MErr_AUTHINFO

    Log_ShowCmdResult
    
    return 0
}

Aiw344_GetCurrentMode()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CFUN?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    local status=`cat $gATLogFile | grep "+CFUN:" | cut -d' ' -f 2`
    [ -z $status ] && return $MErr_PCURMODE
    
    Log_ModuleCmd "[+CFUN?]: $status"
    status=`echo $status | cut -d',' -f 1 | sed 's/^ //g'`

    local mode="Other"
    case $status in
        0)
        mode="Station-Off"
        ;;
        
        1)
        mode="Normal"
        ;;
        
        4)
        mode="AirPlane"
        ;;

        *)
        mode="Other($status)"
        ;;
    esac

    Log_KeyToCmd "$KEY_CURMODE" "$mode" title
    Log_ModuleCmd "$mode" "append"
    Log_ShowCmdResult

    return 0
}

Aiw344_GetImsi()
{
    Dbg3 "Aiw344_$gRunCmd"

    ATCOMM "AT+CIMI?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    local status=`cat $gATLogFile | grep "+CIMI:" | cut -d' ' -f2 | sed 's/^ //g'`
    [ -z $status ] && return $MErr_PIMSI

    Log_ModuleCmd "[+CIMI]: $status"
    Log_ShowCmdResult

    return 0
}

Aiw344_SetPin()
{
    Dbg3 "Aiw344_$gRunCmd: $@, \$\#: $#"

    # Help information with this command
    [ $# -le 0 ] && Aiw344_l_Help '<pin_string>' && return 0

    [ -z $1 ] && return $MErr_NULL
    [ ${#1} -gt 32 ] && return $MErr_PINLEN

    local pin=$1
    ATCOMM "AT+CPIN=$pin"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    return 0
}

Aiw344_SetApn()
{
    Dbg3 "Aiw344_$gRunCmd: $@"
    
    # Help information with this command
    [ $# -le 0 ] && Aiw344_l_Help '<apn_string>' && return 0

    [ -z $1 ] && return $MErr_NULL
    [ ${#1} -gt 62 ] && return $MErr_APNLEN

    local apn="$1"
    local tmp='AT+CGDCONT=1,\"ip\",\"internet\"'
    tmp=`echo "$tmp" | sed -e "s/internet/$apn/g"`
    
    ATCOMM $tmp
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    return 0
}

Aiw344_SetPinUnlock()
{
    Dbg3 "Aiw344_$gRunCmd: $@"
    
    # Help information with this command
    [ $# -le 0 ] && Aiw344_l_Help '<pin_code_string>' && return 0
    
    [ -z $1 ] && return $MErr_NULL
    [ ${#1} -gt 32 ]  &&  return $MErr_ULPINLEN
 
    local pin="$1"
    local tmp='AT+CLCK=\"SC\",0,\"pin_code\"'
    tmp=`echo "$tmp" | sed -e "s/pin_code/$pin/g"`

    ATCOMM $tmp
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    
    return 0
}

Aiw344_SetPinLock()
{
    Dbg3 "Aiw344_$gRunCmd: $@"

    # Help information with this command
    [ $# -le 0 ] && Aiw344_l_Help '<pin_code_string>' && return 0

    [ -z $1 ] && return $MErr_NULL
    [ ${#1} -gt 32 ]  &&  return $MErr_ULPINLEN

    local pin="$1"
    local tmp='AT+CLCK=\"SC\",1,\"pin_code\"'
    tmp=`echo "$tmp" | sed -e "s/pin_code/$pin/g"`

    ATCOMM $tmp
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    return 0
}

Aiw344_SetAuth()
{
    Dbg3 "Aiw344_$gRunCmd: $@"

    # Help information with this command
    [ $# -lt 3 ] && Aiw344_l_Help '<username_string> <password_string> <auth 0:None, 1:PAP, 2:CHAP, 3:PAP and CHAP>' && return 0

    if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
        return $MErr_NULL
    fi

    if [ ${#1} -gt 64 ] || [ ${#2} -gt 64 ]; then
        return $MErr_AUTHLEN
    fi

    if [ $3 -lt 0 ] || [ $3 -gt 3 ] 2> /dev/null; then
        return $MErr_AUTHTYPE
    fi

    # Check cid
    ATCOMM "AT+CGDCONT?"
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret
    local cid=`cat $gATLogFile | grep "CGDCONT:" | grep '"IP"' | cut -d' ' -f2 | cut -d',' -f1 | sed 's/\"//g'`
    if [ -z $cid ] || [ ! $cid -ge 0 ]; then
        return $MErr_AUTHCID
    fi
    
    local username="$1"
    local password="$2"
    local auth_type="$3"
    local tmp=''
    if [ "$username" != "0" ] &&  [ "$password" != "0" ]; then
        tmp='AT+MGAUTH=$cid,$auth_type,\"$username\",\"$password\"'
        tmp=`echo "$tmp" | sed -e "s/\\$cid/$cid/g"`
        tmp=`echo "$tmp" | sed -e "s/\\$auth_type/$auth_type/g"`
        tmp=`echo "$tmp" | sed -e "s/\\$username/$username/g"`
        tmp=`echo "$tmp" | sed -e "s/\\$password/$password/g"`
    else
        tmp="AT+MGAUTH=$cid"
    fi

    ATCOMM $tmp
    local ret=$? && [ $ret -ne 0 ] && gRetStr="AT error!" && return $ret

    return 0
}

Aiw344_SetNormalMode()
{
    Aiw344_l_ATCfunc 1
    local ret=$?
    
    return $ret
}

Aiw344_SetAirPlaneMode()
{
    Aiw344_l_ATCfunc 4
    local ret=$?
    
    return $ret
}

Aiw344_SetStationOffMode()
{
    Aiw344_l_ATCfunc 0
    local ret=$?
    
    return $ret
}

Aiw344_Reset()
{
    Aiw344_l_ATCfunc 15
    local ret=$?
   
    [ $ret -eq 0 ] && Msg "Please wait for $gCfgResetDelay seconds for module rebooting."

    return $ret
}

Aiw344_EnableMonitor()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    Monitor_GetState
    local ret=$?
    if [ $ret -eq $M_RUN ]; then
        gRetStr=$(MErr_strCode2Msg $MErr_MTRRUN)
        return $ret
    fi

    Monitor_Enable
    ret=$? && [ $ret -ne 0 ] && gRetStr=$(MErr_strCode2Msg $MErr_MTRENABLE) && return $ret
    
    return 0
}

Aiw344_DisableMonitor()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    Monitor_GetState
    local ret=$?
    if [ $ret -eq $M_RUN ]; then
        gRetStr=$(MErr_strCode2Msg $MErr_MTRRUN)
        return $ret
    fi

    Monitor_Disable
    ret=$? && [ $ret -ne 0 ] && gRetStr=$(MErr_strCode2Msg $MErr_MTRDISABLE) && return $ret  
    
    return 0
}

Aiw344_StopMonitor()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    Monitor_GetState
    local ret=$?
    if [ $ret -eq $M_RUN ]; then
        gRetStr=$(MErr_strCode2Msg $MErr_MTRRUN)
        return $ret
    fi

    Monitor_Stop
    ret=$? && [ $ret -ne 0 ] && gRetStr=$(MErr_strCode2Msg $MErr_MTRSTOP) && return $ret
    
    return 0
}

Aiw344_GetMonitorState()
{
    Dbg3 "Aiw344_$gRunCmd"

    Monitor_GetCtrlState
    local ret=$?

    case $ret in
        $M_INIT)
        ctrl_s='Disable(Init)'
        ;;
        
        $M_ENABLE)
        ctrl_s='Enable'
        ;;
        
        $M_DISABLE)
        ctrl_s='Disable'
        ;;
        
        $M_STOP)
        ctrl_s='Stop'
        ;;
        
        *)
        ctrl_s="Unknow (Ctrl:$ret)"
        ;;
    esac

    #Monitor_GetState
    #ret=$?
    #case $ret in
    #    $M_INIT)
    #    monitor_state='Init'
    #    ;;

    #    $M_RUN)
    #    monitor_state='Run'
    #    ;;

    #    $M_SLEEP)
    #    monitor_state='Sleep'
    #    ;;

    #    *)
    #    monitor_state="Unknow (Monitor state:$ret)"
    #    ;;
    #esac

    Echo "$ctrl_s"
    #Echo "Control: $ctrl_s"
    #Echo "Monitor state: $monitor_state"
    
    return 0
}

Aiw344_SetLogLevel()
{
    Dbg3 "Aiw344_$gRunCmd"
    
    # Help information with this command
    [ $# -le 0 ] && Aiw344_l_Help "<$L_LEVEL0, $L_LEVEL1 or $L_LEVEL2>" && return 0
    
    if [ $1 -ne $L_LEVEL0 ] && [ $1 -ne $L_LEVEL1 ] && [ $1 -ne $L_LEVEL2 ]; then
        return $MErr_LOGARGUMENT
    fi

    Log_SetLogLevel $1
    local ret=$?
    if [ $ret -ne 0 ]; then
        gRetStr=$(MErr_strCode2Msg $MErr_SETLOG)
        return $ret
    fi

    return 0
}
