summaryrefslogtreecommitdiff
path: root/scripts/telit_radio_upgrade.sh
blob: 607376826ac5b730640480a4685da9bc575714ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/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 <firmware_file>"
  exitnow $ERROR_BAD_ARGS
fi

function do_upgrade() {
  local _RET=1
    telit_flash_uxfp >&2
    _RET=$?
}

do_upgrade