#!/bin/bash

[ -f /opt/huawei/snas/script/inspect_mml/CheckItems ] && source /opt/huawei/snas/script/inspect_mml/CheckItems
[ -f /opt/huawei/snas/script/inspect_mml/inspect_comm.sh ] && source /opt/huawei/snas/script/inspect_mml/inspect_comm.sh
[ -f /opt/huawei/snas/upd/upd_fw/rhfw/snas_rhbmc_fw_upd_utility.sh ] && source /opt/huawei/snas/upd/upd_fw/rhfw/snas_rhbmc_fw_upd_utility.sh

#2288 V5 X86，5288 V5 X86，TianShan 5280 V2，TianShan 2280 V2
function IsTSV2Node()
{
    local productName="$1"
    
    # PRODUCT_NAME in source /opt/node_type_recognize.sh
    if [ "$productName"X = "TaiShan 5280 V2"X -o "$productName"X = "TaiShan 2280 V2"X ]; then
        return 0
    else
        return 1
    fi
}

function IsNeedCheckNode()
{
    local iRetRHV5=""
    local iRetTS=""
    local productName=""
    
    productName="${PRODUCT_NAME}"
    # IsRHV5Node in source /opt/node_type_recognize.sh
    IsRHV5Node
    iRetRHV5=$?
    
    IsTSV2Node "$productName"
    iRetTS=$?
    if [ $iRetRHV5 -eq 0 -o $iRetTS -eq 0 ]; then
        return 0
    else
        return 1
    fi
}

function CheckBMCDataAcquisitionSwitch()
{
    local iRet=""
    local isPass=0
    local isOn="off"
    local curInspectNum="323"
    local curInspectFun="$( GetInspectType $curInspectNum )"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    >${resultFile}

    IsNeedCheckNode
    iRet=$?
    if [ $iRet -ne 0 ]; then
        isPass=3
        echo "BMC Version:--||Data Acquisition Switch:-- " >>${resultFile} 2>&1
        echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
        LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
        return
    fi

    #GetRHBmcFwVer in source /opt/huawei/snas/upd/upd_fw/rhfw/snas_rhbmc_fw_upd_utility.sh
    BmcVer=$(GetRHBmcFwVer)
    iRet=$?
    if [ $iRet -ne 0 ];then
        isPass=1
        echo "[ERR]INFO:can not get to the BMC firmware version!" >>${resultFile} 2>&1 
        echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
        LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
        return
    fi

    if [[ "${BmcVer}" > "3.04" || "${BmcVer}" = "3.04" ]] && [[ "${BmcVer}" < "3.54" ]];then
        isOpen=$(ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x36 0x72 0x00 0x01 0xff 0x00 0x00 0xff 0xff 0x79 0x01 0x00 0x01 0x00 | awk '{printf $9}')
        if [ "$isOpen" == "" ];then
            isPass=1
            echo "[ERR]INFO:can not get BMC data acquisition switch status! " >>${resultFile} 2>&1 
            echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
            LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
            return
        fi
        if  [[ "${isOpen}" = "01" ]]; then
            isPass=1
            isOn="on"
            echo "[ERR]INFO:The BMC firmware version (${BmcVer}) is in the [3.04, 3.55) interval and the data collection switch is turned on! " >>${resultFile} 2>&1 
        else
            echo "BMC Version:${BmcVer}||Data Acquisition Switch:${isOn} " >>${resultFile} 2>&1
        fi
    else
        echo "BMC Version:${BmcVer}||Data Acquisition Switch:-- " >>${resultFile} 2>&1
    fi
    
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
    LOG "[$FUNCNAME]${curInspectFun}_Pass ${isPass}"
}

CheckBMCDataAcquisitionSwitch

