blob: 2432eb161b7dad983869fc7196fe4cc507b13770 [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This is a wrapper around a lot of different DBus calls, written in sh; this is
# meant to replace parts of flimflam-test, and also to be callable from crosh.
. /usr/lib/mm.sh
. /usr/lib/connectivity-common.sh
# Wait for a modem to reset itself and come back with a new dbus name
# args:
# $1 - dbus name of the modem before reset
# $2 - timeout in seconds
wait_for_modem_reset () {
local modem
local oldmodem=$1
local timeout=$2
local status="Timed out"
echo -n "Waiting..."
while test $timeout -gt 0 ; do
modem=$(modems | head -1)
if [ -n "$modem" -a "$modem" != "$oldmodem" ] ; then
status="Done"
break
fi
sleep 1
timeout=$((timeout - 1))
echo -n "."
done
echo $status
}
# Wait for a modem to reset itself and come back with a new dbus name,
# or for the activation state to change from "1"
# MM_MODEM_ACTIVATION_STATE_ACTIVATING to something else
#
# args:
# $1 - dbus name of the modem before activation
# $2 - activation state of modem before activation call
# $3 - timeout in seconds
wait_for_activation_change () {
local modem
local oldmodem=$1
local oldstate=$2
local timeout=$3
local status="Timed out"
local activation_state
echo -n "Waiting..."
while test $timeout -gt 0 ; do
modem=$(modems | head -1)
if [ -n "$modem" -a "$modem" != "$oldmodem" ] ; then
status="Done"
break
fi
activation_state=$(modem_activation_state $modem)
if [ $activation_state != 1 ]; then
status="Done"
break
fi
sleep 1
timeout=$((timeout - 1))
echo -n "."
done
activation_state=$(modem_activation_state $modem)
if [ -z "$activation_state" -o $activation_state -le $oldstate ] ; then
status="Failed"
fi
echo $status
}
activate_manual () {
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
args=$(echo "$@" | sed -e 's/ /,/g')
args=$(require "$args" min)
args=$(require "$args" mdn)
args=$(require "$args" spc)
args=$(require "$args" system_id)
local oldstate=$(modem_activation_state $modem)
dbus "$modem" $IMODEM_CDMA.ActivateManualDebug \
"dict:string:string:$args"
wait_for_activation_change $modem $oldstate 40
}
activate () {
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
# Work around braindead crosh quoting semantics (i.e., there are none,
# arguments are tokenized on spaces). Sigh.
carrier=$(echo "$@")
# TODO(jglasgow): verify that we can pass an empty string
local oldstate=$(modem_activation_state $modem)
local status=$(dbus "$modem" $IMODEM_CDMA.Activate "string:$carrier")
if [ $status != 0 ] ; then
echo "Error $status while activating"
else
wait_for_activation_change $modem $oldstate 40
fi
}
connect () {
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
# Work around braindead quoting again...
args=$(echo "$@")
[ -z "$args" ] && args='ignored'
dbus "$modem" $IMODEM.Connect "string:$args"
}
factory_reset () {
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
$(arg_or_prompt spc 000000)
dbus "$modem" $IMODEM.FactoryReset "string:$spc"
wait_for_modem_reset $modem 40
}
status () {
/usr/share/userfeedback/scripts/mm-status
}
update_prl () {
local modem
local mdn
local min
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
$(needarg prlfile)
mdn=$(dbus $modem $IMODEM_SIMPLE.GetStatus | awk '/\/mdn/ {print $2}')
min=$(dbus $modem $IMODEM_SIMPLE.GetStatus | awk '/\/min/ {print $2}')
if [ -z "$mdn" -o -z "$min" ]; then
echo "Cannot update PRL: MDN/MIN are unknown"
exit 1
fi
if [ ! -r "$prlfile" ]; then
echo "Cannot read PRL file \"$prlfile\""
exit 1
fi
# Because cromo runs as a different user than this script,
# it may not have access to the specified file, even when
# the script does. A specific example is the Downloads folder,
# which only the "chronos" user has access to. To work
# around this problem, we copy the PRL file to /tmp.
local tmpfile=$(mktemp --tmpdir prl.XXXXXXXXXX)
trap "rm -f $tmpfile" 1 2 13 15
cp $prlfile $tmpfile
if [ $? -ne 0 ]; then
echo "Cannot copy \"$prlfile\" to \"$tmpfile\" directory"
rm -f $tmpfile
exit 1
fi
chmod 644 $tmpfile
local oldstate=$(modem_activation_state $modem)
args="mdn,$mdn,min,$min,prlfile,$tmpfile"
dbus "$modem" $IMODEM_CDMA.ActivateManualDebug \
"dict:string:string:$args"
wait_for_modem_reset $modem 40
rm -f $tmpfile
}
set_carrier () {
local modem
local mm
mm=$(modemmanagers | grep chromium)
[ -z "$mm" ] && echo "no compatible modem found" && exit 1
$(arg_or_default modem $(modems | head -1))
[ -z "$modem" ] && echo "no modem found" && exit 1
if [ $# -eq 0 ]; then echo 'Missing arg: carrier' && usage; fi
carrier="$*"
echo Setting carrier to "$carrier". This could take up to two minutes.
echo Using modem "$modem"
dbus-send \
--reply-timeout=120000 \
--system \
--print-reply \
--dest=$DEST \
"$modem" \
org.chromium.ModemManager.Modem.Gobi.SetCarrier \
"string:$carrier"
}
usage () {
echo "Usage: $0 <command> [args...]"
echo " activate [-modem <modem>] [<carrier>] Activate modem"
echo " activate-manual [-modem <modem>] [args...] Activate modem manually"
echo " connect [-modem <modem>] [phone number] Connect modem"
echo " factory-reset [-modem <modem>] [<spc>] Factory-reset the modem"
echo " status Display modem status"
echo " update-prl [-modem <modem>] <prl-file-name> Install a PRL file"
echo " set-carrier [-modem <modem>] <carrier-name> Set modem carrier firmware"
exit 0
}
$(needarg cmd)
case "$cmd" in
activate)
activate "$@"
;;
activate-manual)
activate_manual "$@"
;;
connect)
connect "$@"
;;
factory-reset)
factory_reset "$@"
;;
status)
status "$@"
;;
update-prl)
update_prl "$@"
;;
set-carrier)
set_carrier "$@"
;;
*)
usage
;;
esac