<commit_before>#!/bin/sh
#
# If not stated otherwise in this file or this component's Licenses.txt file the
# following copyright and licenses apply:
#
# Copyright 2016 RDK Management
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
. /etc/include.properties
. /etc/device.properties

if [ -z $LOG_PATH ]; then
    LOG_PATH="/opt/logs/"
fi

#input file to get the peripheral image versions
peripheral_json_file="$RAMDISK_PATH/rc-proxy-params.json"
CURL_TLS_TIMEOUT=30
#notifications to be send to Control Manager only atleast one download is successful
send_iarm_event=false
#holds the firmwares that are successfully downloaded
iarmevent_firmware_filenames=""

CURRENT_PERIPHERAL_VERSION="/tmp/current_peripheral_versions.txt"

if [ ! -f /etc/os-release ]; then
    IARM_EVENT_BINARY_LOCATION=/usr/local/bin
else
    IARM_EVENT_BINARY_LOCATION=/usr/bin
fi

#retrieves the current firmware info of peripheral devices
getRemoteInfo()
{
    peripheral_data=""
    currentversions=""
    if [ -f $peripheral_json_file ]; then
        while read line
        do
            product_check=`echo $line | grep -c 'Product'`
            if [ $product_check -ne 0 ]; then
                peripheral_product=`echo $line | awk -F '"Product":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_fw_version=`echo $line | awk -F '"FwVer":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_audio_version=`echo $line | awk -F '"AudioVer":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_data="$peripheral_data&remCtrl$peripheral_product=$peripheral_fw_version"
                currentversions="${currentversions},${peripheral_product}_firmware_${peripheral_fw_version}.tgz,${peripheral_product}_audio_${peripheral_audio_version}.tgz"
            fi
        done < $peripheral_json_file
    fi
    echo $currentversions > $CURRENT_PERIPHERAL_VERSION
    echo $peripheral_data
}

#sends notification to Control Manager with downloaded firmwares and their location 
sendNotification()
{
    if [ -f $IARM_EVENT_BINARY_LOCATION/IARM_event_sender ]; then
        $IARM_EVENT_BINARY_LOCATION/IARM_event_sender "PeripheralUpgradeEvent" "$DIFW_PATH" "$iarmevent_firmware_filenames"
    else
        echo "Missing the binary $IARM_EVENT_BINARY_LOCATION/IARM_event_sender"
    fi
}

#created the signed URL for codebig communication
getCodebigSignedURL()
{
    URL="$upgrade_location/$firmware_version.tgz"
    domainName=`echo $URL | awk -F/ '{print $3}'`
    URL=`echo $URL | sed -e "s|.*$domainName||g"`
    SIGN_CMD="configparamgen 1 \"$URL\""
    eval $SIGN_CMD > /tmp/.signedRequest
    cbSignedURL=`cat /tmp/.signedRequest`
    rm -f /tmp/.signedRequest
    cbSignedURL=$(sed 's|stb_cdl%2F|stb_cdl/|g' <<< $cbSignedURL)
    echo $cbSignedURL
}

#Function to download the cloud firmware, uses TLS for the connection, added TLS fallback too.
#Common for both Direct communication and Codebig communication
sendTLSRequestForImageDowmload()
{
    CURL_PARAMETER=""
    URL="$upgrade_location/$firmware_version.tgz"
        
    # check whether communication is codebig. if codebig, then gets the signed URL and authorization header
    if [ "$CODEBIG" == "true" ]; then
        cbURL=$(getCodebigSignedURL)  
        authorizationHeader=`echo $cbURL | sed -e "s|&|\", |g" -e "s|=|=\"|g" -e "s|.*oauth_consumer_key|oauth_consumer_key|g"`
        authorizationHeader="Authorization: OAuth realm=\"\", $authorizationHeader\""
            
        # getting codebig URL and authorisation header
        URL=`echo $cbURL | sed -e "s|&oauth_consumer_key.*||g"`
        CURL_PARAMETER="-H '$authorizationHeader'"
    fi
            
    cmd="curl $TLS --connect-timeout $CURL_TLS_TIMEOUT -w '%{http_code}\n' $CURL_PARAMETER -fgLo $DIFW_PATH/$firmware_version.tgz \"$URL\"" 
    echo "Download URL: $cmd"
    eval $cmd > $HTTP_CODE
    TLSRet=$?
        
    case $TLSRet in
        35|51|53|54|58|59|60|64|66|77|80|82|83|90|91)
            echo "HTTPS $TLS failed to connect to SSR server with curl error code $TLSRet" >> $LOG_PATH/tlsError.log
        ;;
    esac
    echo "Curl download return code : $TLSRet"
}

#3 retries available for each image download
downloadPeripheralFirmware()
{
    retries=0
    while [ $retries -ne 3 ]
    do
        echo "Trying to download $firmware_version.tgz, retries : $retries"
        sendTLSRequestForImageDowmload

        if [ $TLSRet -eq 0 ]; then
            echo "Downloading $firmware_version.tgz is successful"
            send_iarm_event=true
            if [ "$iarmevent_firmware_filenames" == "" ]; then
                iarmevent_firmware_filenames=$firmware_version".tgz"
            else
                iarmevent_firmware_filenames=$iarmevent_firmware_filenames","$firmware_version".tgz"
            fi
            break
        else
            echo "Downloading $firmware_version.tgz failed"
        fi
        sleep 30
        retries=`expr $retries + 1`        
    done
}

#parses the input and trigger download for each firmware,
#Also switches to Codebig if Direct fails
getPeripheralFirmwares()
{
    echo "Going to download peripheral firmwares"
    upgrade_location=$1
    peripheral_firmwares=$2
    TLS="--tlsv1.2"
    CODEBIG=$3
    count=1
    currentversions=`cat $CURRENT_PERIPHERAL_VERSION`
    firmware_version=`echo $peripheral_firmwares | cut -d "," -f1`
    
    echo "Deleting the old firmwares if any"
    rm $DIFW_PATH/*_firmware_*.tgz
    rm $DIFW_PATH/*_audio_*.tgz
    while [ "$firmware_version" != "" ]
    do
        trigger_download=0
        peripheral_device_type=`echo $firmware_version | cut -d "_" -f1`
        peripheral_version_type=`echo $firmware_version | cut -d "_" -f2`

        if [ "$currentversions" != "" ]; then<commit_msg>DELIA-25768 : Creating DIFW folder before peripheraldownload if it doesnt exist<commit_after>#!/bin/sh
#
# If not stated otherwise in this file or this component's Licenses.txt file the
# following copyright and licenses apply:
#
# Copyright 2016 RDK Management
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
. /etc/include.properties
. /etc/device.properties

if [ -z $LOG_PATH ]; then
    LOG_PATH="/opt/logs/"
fi

#input file to get the peripheral image versions
peripheral_json_file="$RAMDISK_PATH/rc-proxy-params.json"
CURL_TLS_TIMEOUT=30
#notifications to be send to Control Manager only atleast one download is successful
send_iarm_event=false
#holds the firmwares that are successfully downloaded
iarmevent_firmware_filenames=""

CURRENT_PERIPHERAL_VERSION="/tmp/current_peripheral_versions.txt"

if [ ! -f /etc/os-release ]; then
    IARM_EVENT_BINARY_LOCATION=/usr/local/bin
else
    IARM_EVENT_BINARY_LOCATION=/usr/bin
fi

#retrieves the current firmware info of peripheral devices
getRemoteInfo()
{
    peripheral_data=""
    currentversions=""
    if [ -f $peripheral_json_file ]; then
        while read line
        do
            product_check=`echo $line | grep -c 'Product'`
            if [ $product_check -ne 0 ]; then
                peripheral_product=`echo $line | awk -F '"Product":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_fw_version=`echo $line | awk -F '"FwVer":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_audio_version=`echo $line | awk -F '"AudioVer":"' '{print $NF}' | awk -F '"' '{print $1}'`
                peripheral_data="$peripheral_data&remCtrl$peripheral_product=$peripheral_fw_version"
                currentversions="${currentversions},${peripheral_product}_firmware_${peripheral_fw_version}.tgz,${peripheral_product}_audio_${peripheral_audio_version}.tgz"
            fi
        done < $peripheral_json_file
    fi
    echo $currentversions > $CURRENT_PERIPHERAL_VERSION
    echo $peripheral_data
}

#sends notification to Control Manager with downloaded firmwares and their location 
sendNotification()
{
    if [ -f $IARM_EVENT_BINARY_LOCATION/IARM_event_sender ]; then
        $IARM_EVENT_BINARY_LOCATION/IARM_event_sender "PeripheralUpgradeEvent" "$DIFW_PATH" "$iarmevent_firmware_filenames"
    else
        echo "Missing the binary $IARM_EVENT_BINARY_LOCATION/IARM_event_sender"
    fi
}

#created the signed URL for codebig communication
getCodebigSignedURL()
{
    URL="$upgrade_location/$firmware_version.tgz"
    domainName=`echo $URL | awk -F/ '{print $3}'`
    URL=`echo $URL | sed -e "s|.*$domainName||g"`
    SIGN_CMD="configparamgen 1 \"$URL\""
    eval $SIGN_CMD > /tmp/.signedRequest
    cbSignedURL=`cat /tmp/.signedRequest`
    rm -f /tmp/.signedRequest
    cbSignedURL=$(sed 's|stb_cdl%2F|stb_cdl/|g' <<< $cbSignedURL)
    echo $cbSignedURL
}

#Function to download the cloud firmware, uses TLS for the connection, added TLS fallback too.
#Common for both Direct communication and Codebig communication
sendTLSRequestForImageDowmload()
{
    CURL_PARAMETER=""
    URL="$upgrade_location/$firmware_version.tgz"
        
    # check whether communication is codebig. if codebig, then gets the signed URL and authorization header
    if [ "$CODEBIG" == "true" ]; then
        cbURL=$(getCodebigSignedURL)  
        authorizationHeader=`echo $cbURL | sed -e "s|&|\", |g" -e "s|=|=\"|g" -e "s|.*oauth_consumer_key|oauth_consumer_key|g"`
        authorizationHeader="Authorization: OAuth realm=\"\", $authorizationHeader\""
            
        # getting codebig URL and authorisation header
        URL=`echo $cbURL | sed -e "s|&oauth_consumer_key.*||g"`
        CURL_PARAMETER="-H '$authorizationHeader'"
    fi
            
    cmd="curl $TLS --connect-timeout $CURL_TLS_TIMEOUT -w '%{http_code}\n' $CURL_PARAMETER -fgLo $DIFW_PATH/$firmware_version.tgz \"$URL\"" 
    echo "Download URL: $cmd"
    eval $cmd > $HTTP_CODE
    TLSRet=$?
        
    case $TLSRet in
        35|51|53|54|58|59|60|64|66|77|80|82|83|90|91)
            echo "HTTPS $TLS failed to connect to SSR server with curl error code $TLSRet" >> $LOG_PATH/tlsError.log
        ;;
    esac
    echo "Curl download return code : $TLSRet"
}

#3 retries available for each image download
downloadPeripheralFirmware()
{
    retries=0
    while [ $retries -ne 3 ]
    do
        echo "Trying to download $firmware_version.tgz, retries : $retries"
        sendTLSRequestForImageDowmload

        if [ $TLSRet -eq 0 ]; then
            echo "Downloading $firmware_version.tgz is successful"
            send_iarm_event=true
            if [ "$iarmevent_firmware_filenames" == "" ]; then
                iarmevent_firmware_filenames=$firmware_version".tgz"
            else
                iarmevent_firmware_filenames=$iarmevent_firmware_filenames","$firmware_version".tgz"
            fi
            break
        else
            echo "Downloading $firmware_version.tgz failed"
        fi
        sleep 30
        retries=`expr $retries + 1`        
    done
}

#parses the input and trigger download for each firmware,
#Also switches to Codebig if Direct fails
getPeripheralFirmwares()
{
    echo "Going to download peripheral firmwares"
    upgrade_location=$1
    peripheral_firmwares=$2
    TLS="--tlsv1.2"
    CODEBIG=$3
    count=1
    currentversions=`cat $CURRENT_PERIPHERAL_VERSION`
    firmware_version=`echo $peripheral_firmwares | cut -d "," -f1`
    
    if [ ! -d $DIFW_PATH ]; then
        mkdir -p $DIFW_PATH
    fi
    
    echo "Deleting the old firmwares if any"
    rm $DIFW_PATH/*_firmware_*.tgz
    rm $DIFW_PATH/*_audio_*.tgz
    while [ "$firmware_version" != "" ]
    do
        trigger_download=0
        peripheral_device_type=`echo $firmware_version | cut -d "_" -f1`
        peripheral_version_type=`echo $firmware_version | cut -d "_" -f2`

        if [ "$currentversions" != "" ]; then
{'fraction_non_alphanumeric': 0.10712872853865021, 'fraction_numerical': 0.01052298441332384, 'mean_word_length': 3.3022464261402313, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 29, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5268210', 'n_tokens_mistral': 4180, 'n_tokens_neox': 3940, 'n_words': 1056}