#!/bin/bash ###################################################################################### # Definition of global constants. ###################################################################################### # "Firmware check"-compatible error codes ERROR_BAD_ARGS=91 COMPONENT_NAME="radio_fw_upgrade" ###################################################################################### function loginfo() { logger -s -p info -t $COMPONENT_NAME "$@" 1>&2 ; } function logwarn() { logger -s -p warning -t $COMPONENT_NAME "$@" 1>&2 ; } function logerror() { logger -s -p error -t $COMPONENT_NAME "$@" 1>&2 ; } ###################################################################################### ###################################################################################### # Print current radio firmware upgrade status and exit the application immediately. # Globals: # COMPONENT_NAME - name of the component. # Arguments: # $1 - exit code for this application. # Returns: # None (exits the application) ###################################################################################### function exitnow() { loginfo "Radio firmware upgrade procedure exited with exit code [$1]" #next_state "$UPGRADE_STATE_FINISHED" exit "$1" } ###################################################################################### # Parse command-line arguments for this application. # Globals: # FW_IMAGE_PATH - write-only, path to the upgrade image. # Arguments: # $@ - all command-line arguments of this application. # Returns: # 0 on success, 1 otherwise ###################################################################################### function parse_arguments() { if [ $# != 1 ]; then logerror "Invalid number of arguments" return 1 fi if [ ! -f "$1" ]; then logerror "Firmware file [$1] does not exist" return 1 fi FW_IMAGE_PATH="$1" return 0 } ###################################################################################### # Determine TTY flash port for Telit radios. # Logic ported from telit_flash. # Globals: # TELIT_FLASH_TTY - write-only, TTY port for flashing Telit radios # Arguments: # None # Returns: # 0 on success, 1 otherwise ###################################################################################### function select_telit_flash_tty() { loginfo "Setting up for LoRa USB card checks" # Check for LoRa USB cards. Depending on how many there are inserted, use # ttyUSB0 or ttyUSB1 or ttyUSB2 local port1=/sys/devices/platform/mts-io/ap1 local port2=/sys/devices/platform/mts-io/ap2 local hwversion="MTAC-LORA-1.0" local lora_usb_count=0 local usbtty loginfo "Checking for LoRa USB cards in AP1 and AP2" if [ -d $port1 ] && [[ $(cat $port1/hw-version) = $hwversion ]]; then loginfo "Found USB LoRa card on AP1" lora_usb_count=$((lora_usb_count + 1)) loginfo "incremented lora_usb_count" fi loginfo "Past check on AP1" if [ -d $port2 ] && [[ $(cat $port2/hw-version) = $hwversion ]]; then loginfo "Found USB LoRa card on AP2" lora_usb_count=$((lora_usb_count + 1)) fi loginfo "Past check on AP2" loginfo "lora_usb_count = $lora_usb_count" case $lora_usb_count in 0) usbtty="/dev/ttyUSB0" ;; 1) usbtty="/dev/ttyUSB1" ;; 2) usbtty="/dev/ttyUSB2" ;; *) # maybe this should just bail here? usbtty="/dev/ttyUSB0" ;; esac; loginfo "usbtty = $usbtty" TELIT_FLASH_TTY="$usbtty" return 0 } ###################################################################################### # Flash telit device using uxfp utility. # Logic ported from l4e1_flash. # Globals: # FW_IMAGE_PATH - read-only, path to the firmware image. # TELIT_FLASH_TTY - read-only, TTY port for flashing Telit radios. # UXFP_OPTIONS - read-only, options for uxfp flashing utility. # Arguments: # None # Returns: # 0 on success, 1 otherwise ###################################################################################### function telit_flash_uxfp() { if ! select_telit_flash_tty; then logerror "Flashing radio modem failed" return 1 fi # cycle power to get module into boot mode # this needs to be run in the background so the flashing can start # as soon as the module is in boot mode loginfo "Resetting radio modem" ## INFO: -1: ignore backoff timers in radio-reset echo -1 >/sys/devices/platform/mts-io/radio-reset & # start flashing loginfo "Starting flashing radio modem" ## INFO: We must catch up to the interface ttyUSBx (flashloader converter) before disconnected local COUNTER=0 local MAX=120 while [ $COUNTER -lt $MAX ]; do loginfo "Flash Attempt $COUNTER" # Start uxfp and log to its output to syslog if [ -c "$TELIT_FLASH_TTY" ] && uxfp --file "$FW_IMAGE_PATH" --port "$TELIT_FLASH_TTY" --debug; then loginfo "Flashing radio modem successful, restarting telit module" echo -1 >/sys/devices/platform/mts-io/radio-reset loginfo "Done" return 0 fi COUNTER=$((COUNTER+1)) sleep 0.1 done logerror "Flashing radio modem failed" return 1 } ###################################################################################### # MAIN ###################################################################################### if ! parse_arguments "$@"; then echo "I am here" logerror "Usage: $0 " exitnow $ERROR_BAD_ARGS fi function do_upgrade() { local _RET=1 telit_flash_uxfp >&2 _RET=$? } do_upgrade