OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 # This is a wrapper around a lot of different DBus calls, written in sh; this is |
| 7 # meant to replace parts of flimflam-test, and also to be callable from crosh. |
| 8 |
| 9 . /usr/lib/mm.sh |
| 10 |
| 11 # Generates a small snippet of code to take a single argument out of our |
| 12 # parameter list, and complain (and exit) if it's not present. Used in other |
| 13 # places like: $(needarg foo), which binds foo to $1. |
| 14 needarg () { |
| 15 # We need to echo eval here because the part of bash that understands |
| 16 # variable assignments has already run by the time we substitute in the |
| 17 # text of $(needarg foo) - i.e., bash will try to execute 'foo="$1"' as |
| 18 # a *command*, which it isn't. The eval forces bash to reparse the code |
| 19 # before executing it. |
| 20 echo eval "$1=\"\$1\"; |
| 21 [ -z \"\$$1\" ] && echo 'Missing arg: $1' && usage; |
| 22 shift" |
| 23 } |
| 24 |
| 25 # Generates a small snippet of code to take a matching flag argument |
| 26 # and value out of our parameter list if present. If not, assign the |
| 27 # default value to $1. |
| 28 # Used in other places like: $(arg_or_default foo bar) |
| 29 # which binds foo to "bar", unless $1 is "-foo" in which case foo is |
| 30 # bound to $2. |
| 31 arg_or_default () { |
| 32 echo eval "[ x\"\$1\" = x\"-$1\" ] && $1=\"\$2\" && shift 2; |
| 33 [ -z \"\$$1\" ] && $1=\"$2\"" |
| 34 } |
| 35 |
| 36 # Generates a small snippet of code to take a single argument out of our |
| 37 # parameter list. If it's not present, prompt the user and accept a |
| 38 # blank input as if the users chose the default specified as $2. |
| 39 arg_or_prompt () { |
| 40 echo eval "$1=\"\$1\"; |
| 41 [ -n \"\$$1\" ] && shift ; |
| 42 [ -z \"\$$1\" ] && read -p \"$1 [$2]: \" $1 ; |
| 43 [ -z \"\$$1\" ] && $1=\"$2\";" |
| 44 } |
| 45 |
| 46 # Require a key in a csv list of key value pairs |
| 47 # $1 - comma separated list of keys and values |
| 48 # $2 - required key |
| 49 # If the key is not found in the argument list, then prompt the user |
| 50 # for a value for key, and return $key,$value appended to $1 |
| 51 require () { |
| 52 local value |
| 53 local args=$1 |
| 54 local key=$2 |
| 55 if [ -z "$args" -o -n "${args##*$2*}" ] ; then |
| 56 read -p "$key: " value |
| 57 if [ -n "$args" ] ; then |
| 58 args="$args," |
| 59 fi |
| 60 args="$args$key,$value" |
| 61 fi |
| 62 echo "$args" |
| 63 } |
| 64 |
| 65 # Wait for a modem to reset itself and come back with a new dbus name |
| 66 # args: |
| 67 # $1 - dbus name of the modem before reset |
| 68 # $2 - timeout in seconds |
| 69 wait_for_modem_reset () { |
| 70 local modem |
| 71 local oldmodem=$1 |
| 72 local timeout=$2 |
| 73 local status="Timed out" |
| 74 echo -n "Waiting..." |
| 75 while test $timeout -gt 0 ; do |
| 76 modem=$(modems | head -1) |
| 77 if [ -n "$modem" -a "$modem" != "$oldmodem" ] ; then |
| 78 status="Done" |
| 79 break |
| 80 fi |
| 81 sleep 1 |
| 82 timeout=$((timeout - 1)) |
| 83 echo -n "." |
| 84 done |
| 85 echo $status |
| 86 } |
| 87 |
| 88 # Wait for a modem to reset itself and come back with a new dbus name, |
| 89 # or for the activation state to change from "1" |
| 90 # MM_MODEM_ACTIVATION_STATE_ACTIVATING to something else |
| 91 # |
| 92 # args: |
| 93 # $1 - dbus name of the modem before activation |
| 94 # $2 - activation state of modem before activation call |
| 95 # $3 - timeout in seconds |
| 96 wait_for_activation_change () { |
| 97 local modem |
| 98 local oldmodem=$1 |
| 99 local oldstate=$2 |
| 100 local timeout=$3 |
| 101 local status="Timed out" |
| 102 local activation_state |
| 103 echo -n "Waiting..." |
| 104 while test $timeout -gt 0 ; do |
| 105 modem=$(modems | head -1) |
| 106 if [ -n "$modem" -a "$modem" != "$oldmodem" ] ; then |
| 107 status="Done" |
| 108 break |
| 109 fi |
| 110 activation_state=$(modem_activation_state $modem) |
| 111 if [ $activation_state != 1 ]; then |
| 112 status="Done" |
| 113 break |
| 114 fi |
| 115 sleep 1 |
| 116 timeout=$((timeout - 1)) |
| 117 echo -n "." |
| 118 done |
| 119 activation_state=$(modem_activation_state $modem) |
| 120 if [ -z "$activation_state" -o $activation_state -le $oldstate ] ; then |
| 121 status="Failed" |
| 122 fi |
| 123 echo $status |
| 124 } |
| 125 |
| 126 activate_manual () { |
| 127 $(arg_or_default modem $(modems | head -1)) |
| 128 [ -z "$modem" ] && echo "no modem found" && exit 1 |
| 129 args=$(echo "$@" | sed -e 's/ /,/g') |
| 130 args=$(require "$args" min) |
| 131 args=$(require "$args" mdn) |
| 132 args=$(require "$args" spc) |
| 133 args=$(require "$args" system_id) |
| 134 local oldstate=$(modem_activation_state $modem) |
| 135 dbus "$modem" $IMODEM_CDMA.ActivateManualDebug \ |
| 136 "dict:string:string:$args" |
| 137 wait_for_activation_change $modem $oldstate 40 |
| 138 } |
| 139 |
| 140 activate () { |
| 141 $(arg_or_default modem $(modems | head -1)) |
| 142 [ -z "$modem" ] && echo "no modem found" && exit 1 |
| 143 # Work around braindead crosh quoting semantics (i.e., there are none, |
| 144 # arguments are tokenized on spaces). Sigh. |
| 145 carrier=$(echo "$@") |
| 146 # TODO(jglasgow): verify that we can pass an empty string |
| 147 local oldstate=$(modem_activation_state $modem) |
| 148 local status=$(dbus "$modem" $IMODEM_CDMA.Activate "string:$carrier") |
| 149 if [ $status != 0 ] ; then |
| 150 echo "Error $status while activating" |
| 151 else |
| 152 wait_for_activation_change $modem $oldstate 40 |
| 153 fi |
| 154 } |
| 155 |
| 156 connect () { |
| 157 $(arg_or_default modem $(modems | head -1)) |
| 158 [ -z "$modem" ] && echo "no modem found" && exit 1 |
| 159 # Work around braindead quoting again... |
| 160 args=$(echo "$@") |
| 161 [ -z "$args" ] && args='ignored' |
| 162 dbus "$modem" $IMODEM.Connect "string:$args" |
| 163 } |
| 164 |
| 165 factory_reset () { |
| 166 $(arg_or_default modem $(modems | head -1)) |
| 167 [ -z "$modem" ] && echo "no modem found" && exit 1 |
| 168 $(arg_or_prompt spc 000000) |
| 169 dbus "$modem" $IMODEM.FactoryReset "string:$spc" |
| 170 wait_for_modem_reset $modem 40 |
| 171 } |
| 172 |
| 173 status () { |
| 174 /usr/share/userfeedback/scripts/mm-status |
| 175 } |
| 176 |
| 177 update_prl () { |
| 178 local modem |
| 179 local mdn |
| 180 local min |
| 181 $(arg_or_default modem $(modems | head -1)) |
| 182 [ -z "$modem" ] && echo "no modem found" && exit 1 |
| 183 $(needarg prlfile) |
| 184 mdn=$(dbus $modem $IMODEM_SIMPLE.GetStatus | awk '/\/mdn/ {print $2}') |
| 185 min=$(dbus $modem $IMODEM_SIMPLE.GetStatus | awk '/\/min/ {print $2}') |
| 186 if [ -z "$mdn" -o -z "$min" ]; then |
| 187 echo "Cannot update PRL: MDN/MIN are unknown" |
| 188 exit 1 |
| 189 fi |
| 190 if [ ! -r "$prlfile" ]; then |
| 191 echo "Cannot read PRL file \"$prlfile\"" |
| 192 exit 1 |
| 193 fi |
| 194 # Because cromo runs as a different user than this script, |
| 195 # it may not have access to the specified file, even when |
| 196 # the script does. A specific example is the Downloads folder, |
| 197 # which only the "chronos" user has access to. To work |
| 198 # around this problem, we copy the PRL file to /tmp. |
| 199 if ! echo "$prlfile" | grep -q '^/tmp/'; then |
| 200 prlbase=${prlfile##*/} |
| 201 cp $prlfile /tmp/$prlbase |
| 202 if [ $? -ne 0 ]; then |
| 203 echo "Cannot copy PRL file \"$prlfile\" to temp director
y" |
| 204 exit 1 |
| 205 fi |
| 206 prlfile=/tmp/$prlbase |
| 207 fi |
| 208 local oldstate=$(modem_activation_state $modem) |
| 209 args="mdn,$mdn,min,$min,prlfile,$prlfile" |
| 210 dbus "$modem" $IMODEM_CDMA.ActivateManualDebug \ |
| 211 "dict:string:string:$args" |
| 212 wait_for_modem_reset $modem 40 |
| 213 } |
| 214 |
| 215 usage () { |
| 216 echo "Usage: $0 <command> [args...]" |
| 217 echo " activate [-modem <modem>] [<carrier>] Activate modem" |
| 218 echo " activate-manual [-modem <modem>] [args...] Activate modem manu
ally" |
| 219 echo " connect [-modem <modem>] [phone number] Connect modem" |
| 220 echo " factory-reset [-modem <modem>] [<spc>] Factory-reset the m
odem" |
| 221 echo " status Display modem statu
s" |
| 222 echo " update-prl [-modem <modem>] <prl-file-name> Install a PRL file" |
| 223 exit 0 |
| 224 } |
| 225 |
| 226 $(needarg cmd) |
| 227 case "$cmd" in |
| 228 activate) |
| 229 activate "$@" |
| 230 ;; |
| 231 activate-manual) |
| 232 activate_manual "$@" |
| 233 ;; |
| 234 connect) |
| 235 connect "$@" |
| 236 ;; |
| 237 factory-reset) |
| 238 factory_reset "$@" |
| 239 ;; |
| 240 status) |
| 241 status "$@" |
| 242 ;; |
| 243 update-prl) |
| 244 update_prl "$@" |
| 245 ;; |
| 246 *) |
| 247 usage |
| 248 ;; |
| 249 esac |
OLD | NEW |