<commit_before>#!/bin/bash
# Copyright (C) 2017 Kubos Corporation
#
# 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.
#
# Flash files to ISIS OBC board
#

##########################################################################
# Scrape transfer status from minicom log. Looks like this:
# "Bytes Sent: 693248/1769379 BPS:8343 ETA 02:08"
##########################################################################
progress() {
    while sleep 1; do
        line=$(grep -m 1 -o -e "Bytes Sent.*\e" -e "Bytes Sent.*     " flash.log)
        printf "\033[2K${line}\r"
    done   
}

##########################################################################
# Minicom doesn't allow any pass-through arguments, so instead we need to 
# generate a script for it to run.
#
# Globals:
#   password
#   is_upgrade
# Arguments:
#   path of file to flash
#   path to flash to
# Returns:
#   none
##########################################################################
create_send_script() {
    local path=$1
    local name=$(basename ${path})
    echo "Sending ${name} to $2 on board..."
    cat > send.tmp <<-EOF
verbose on
send root
expect {
    "Password:" break
    timeout 1 break
}
send ${password}
expect {
    "~ #" break
    timeout 5 goto end
}
timeout 3600
send "mkdir -p $2"
send "cd $2"
send "rm -f ${name}"
if ${is_app} = 1 send "rm -f /home/etc/init.d/S*${app_name}"
send "rz -w 8192"
! sz -w 8192 ${path}
if ${is_upgrade} = 1 send "fw_setenv kubos_updatefile ${name}"
send "exit"
end:
! killall minicom -q
EOF
}

##########################################################################
# Generate a default init script for the new application. 
# File name will be 'S{run_level}{app_name}'
# ex. S90MyProj
#
# Globals:
#   app_name
#   init_script
# Arguments:
#   none
# Returns:
#   none
##########################################################################
create_init_script() {

    echo "Creating init script"

    # Delete any previous versions of the init script to avoid clutter
    rm -f S*${app_name}

    cat > ${init_script} <<-EOF
#!/bin/sh

NAME=${app_name}
PROG=/home/usr/bin/\${NAME}
PID=/var/run/\${NAME}.pid

case "\$1" in
    start)
    echo "Starting \${NAME}: "
    start-stop-daemon -S -q -m -b -p \${PID} --exec \${PROG}
    rc=\$?
    if [ \${rc} -eq 0 ]
    then
        echo "OK"
    else
        echo "FAIL" >&2
    fi
    ;;
    stop)
    echo "Stopping \${NAME}: "
    start-stop-daemon -K -q -p \${PID}
    rc=\$?
    if [ \${rc} -eq 0 ]
    then
        echo "OK"
    else
        echo "FAIL" >&2
    fi
    ;;
    restart)
    "\$0" stop
    "\$0" start
    ;;
    *)
    echo "Usage: \$0 {start|stop|restart}"
    ;;
esac

exit \${rc}
EOF

    chmod 0755 ${init_script}
}

##########################################################################
# Call the minicom transfer script to flash the file
#
# Globals:
#   none
# Arguments:
#   none
# Returns:
#   0 - Transfer successful
#   1 - Transfer failed
##########################################################################
send_file() {
    # Run the transfer script
    minicom kubos -o -S send.tmp > flash.log
    
    local retval=1
    
    # Check transfer result
    if grep -q incomplete flash.log; then
        echo "Transfer Failed" 1>&2
    elif grep -q complete flash.log; then
        echo "Transfer Successful"
        retval=0
    elif grep -q incorrect flash.log; then
        echo "Transfer Failed: Invalid password" 1>&2
    else
        echo "Transfer Failed: Connection failed" 1>&2
    fi
    
    return "${retval}"
}

##########################################################################
# Main Script
##########################################################################
main() {
    start=$(date +%s)
    
    this_dir=$(cd "`dirname "$0"`"; pwd)
    file=$1
    name=$(basename $1)
    is_upgrade=0
    is_app=0
    
    password=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["password"]')
    dest_dir=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["destDir"]')
    init=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["initAtBoot"]')
    run_level=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["runLevel"]')
    app_name=$(cat ../../module.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["name"]')
    
    init_script="S${run_level}${app_name}"
    
    unamestr=`uname`
    
    if [[ "${unamestr}" =~ "Linux" ]]; then
        device=`lsusb -d '0403:'`
    fi
    
    if [[ "${device}" =~ "6001" ]]; then
        echo "Compatible FTDI device found"
        progress &
        progress_pid=$!
        disown
    else
        echo "No compatible FTDI device found" 1>&2
        exit 0
    fi
    
    if [[ "${password}" = "<PASSWORD>" ]]; then
        echo "Using default password"
    fi
    
    if [[ "${name}" = *.itb ]]; then
        path="/upgrade"
        is_upgrade=1
    elif [[ "${name}" != "${app_name}" ]]; then
        path="${dest_dir}"
    else
        path="/home/usr/bin"
        is_app=1
    fi
    
    # Send the file
    create_send_script ${file} ${path}<commit_msg>Adding run_level validation<commit_after>#!/bin/bash
# Copyright (C) 2017 Kubos Corporation
#
# 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.
#
# Flash files to ISIS OBC board
#

##########################################################################
# Scrape transfer status from minicom log. Looks like this:
# "Bytes Sent: 693248/1769379 BPS:8343 ETA 02:08"
##########################################################################
progress() {
    while sleep 1; do
        line=$(grep -m 1 -o -e "Bytes Sent.*\e" -e "Bytes Sent.*     " flash.log)
        printf "\033[2K${line}\r"
    done   
}

##########################################################################
# Minicom doesn't allow any pass-through arguments, so instead we need to 
# generate a script for it to run.
#
# Globals:
#   password
#   is_upgrade
# Arguments:
#   path of file to flash
#   path to flash to
# Returns:
#   none
##########################################################################
create_send_script() {
    local path=$1
    local name=$(basename ${path})
    echo "Sending ${name} to $2 on board..."
    cat > send.tmp <<-EOF
verbose on
send root
expect {
    "Password:" break
    timeout 1 break
}
send ${password}
expect {
    "~ #" break
    timeout 5 goto end
}
timeout 3600
send "mkdir -p $2"
send "cd $2"
send "rm -f ${name}"
if ${is_app} = 1 send "rm -f /home/etc/init.d/S*${app_name}"
send "rz -w 8192"
! sz -w 8192 ${path}
if ${is_upgrade} = 1 send "fw_setenv kubos_updatefile ${name}"
send "exit"
end:
! killall minicom -q
EOF
}

##########################################################################
# Generate a default init script for the new application. 
# File name will be 'S{run_level}{app_name}'
# ex. S90MyProj
#
# Globals:
#   app_name
#   init_script
# Arguments:
#   none
# Returns:
#   none
##########################################################################
create_init_script() {

    echo "Creating init script"

    # Delete any previous versions of the init script to avoid clutter
    rm -f S*${app_name}

    cat > ${init_script} <<-EOF
#!/bin/sh

NAME=${app_name}
PROG=/home/usr/bin/\${NAME}
PID=/var/run/\${NAME}.pid

case "\$1" in
    start)
    echo "Starting \${NAME}: "
    start-stop-daemon -S -q -m -b -p \${PID} --exec \${PROG}
    rc=\$?
    if [ \${rc} -eq 0 ]
    then
        echo "OK"
    else
        echo "FAIL" >&2
    fi
    ;;
    stop)
    echo "Stopping \${NAME}: "
    start-stop-daemon -K -q -p \${PID}
    rc=\$?
    if [ \${rc} -eq 0 ]
    then
        echo "OK"
    else
        echo "FAIL" >&2
    fi
    ;;
    restart)
    "\$0" stop
    "\$0" start
    ;;
    *)
    echo "Usage: \$0 {start|stop|restart}"
    ;;
esac

exit \${rc}
EOF

    chmod 0755 ${init_script}
}

##########################################################################
# Call the minicom transfer script to flash the file
#
# Globals:
#   none
# Arguments:
#   none
# Returns:
#   0 - Transfer successful
#   1 - Transfer failed
##########################################################################
send_file() {
    # Run the transfer script
    minicom kubos -o -S send.tmp > flash.log
    
    local retval=1
    
    # Check transfer result
    if grep -q incomplete flash.log; then
        echo "Transfer Failed" 1>&2
    elif grep -q complete flash.log; then
        echo "Transfer Successful"
        retval=0
    elif grep -q incorrect flash.log; then
        echo "Transfer Failed: Invalid password" 1>&2
    else
        echo "Transfer Failed: Connection failed" 1>&2
    fi
    
    return "${retval}"
}

##########################################################################
# Main Script
##########################################################################
main() {
    start=$(date +%s)
    
    this_dir=$(cd "`dirname "$0"`"; pwd)
    file=$1
    name=$(basename $1)
    is_upgrade=0
    is_app=0
    
    password=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["password"]')
    dest_dir=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["destDir"]')
    init=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["initAtBoot"]')
    run_level=$(cat yotta_config.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["system"]["runLevel"]')
    app_name=$(cat ../../module.json | python -c 'import sys,json; x=json.load(sys.stdin); print x["name"]')
    
    init_script="S${run_level}${app_name}"
    
    unamestr=`uname`
    
    if [[ "${unamestr}" =~ "Linux" ]]; then
        device=`lsusb -d '0403:'`
    fi
    
    if [[ "${device}" =~ "6001" ]]; then
        echo "Compatible FTDI device found"
        progress &
        progress_pid=$!
        disown
    else
        echo "No compatible FTDI device found" 1>&2
        exit 0
    fi
    
    if [[ "${password}" = "<PASSWORD>" ]]; then
        echo "Using default password"
    fi
    
    if [[ "${name}" = *.itb ]]; then
        path="/upgrade"
        is_upgrade=1
    elif [[ "${name}" != "${app_name}" ]]; then
        path="${dest_dir}"
    else
        path="/home/usr/bin"
        is_app=1
        if [[ "${run_level}" > 99 || "${run_level}" < 10 ]]; then
            echo "Run level of ${run_level} outside of range (10-99). Setting to default."
            run_level=50
        fi
    fi
    
    # Send the file
    create_send_script ${file} ${path}
{'fraction_non_alphanumeric': 0.25305425032037593, 'fraction_numerical': 0.01742844938060658, 'mean_word_length': 3.049117952265652, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7655096', 'n_tokens_mistral': 3815, 'n_tokens_neox': 3601, 'n_words': 1119}