Christopher Wiley | 2f48d95 | 2013-02-22 09:51:47 -0800 | [diff] [blame] | 1 | #!/bin/dash |
| 2 | |
| 3 | . "$(dirname "$0")/mm.sh" |
| 4 | |
| 5 | # This is somewhat subtle, and quite disgusting. Here's the story: dbus-send |
| 6 | # --fixed emits arrays and tuples like: |
| 7 | # /0 something |
| 8 | # /1 anotherthing |
| 9 | # We happen to know that 0 is actually, say, signal strength, and 1 is carrier |
| 10 | # name, but we get the info back as a tuple. Fortunately, unpacktuple() to the |
| 11 | # rescue! This function generates and then runs a sed expression which |
| 12 | # transforms the indexes into proper keys automatically. For the above |
| 13 | # expression, we might use: |
| 14 | # unpacktuple SignalStrength Carrier |
| 15 | # And if we then did: |
| 16 | # dbus-blahblah GetSomething | unpacktuple SignalStrength Carrier |
| 17 | # We would end up with nicely-formatted output: |
| 18 | # SignalStrength: something |
| 19 | # Carrier: anotherthing |
| 20 | unpacktuple () { |
| 21 | local cmd='sed' |
| 22 | local argidx=0 |
| 23 | while test $# != 0; do |
| 24 | # Grab a variable name |
| 25 | local varname=$1 |
| 26 | shift |
| 27 | |
| 28 | # Generate an expression that turns the current index into that |
| 29 | # variable name, and append it. |
| 30 | cmd="$cmd -e s/^\\/${argidx}/$varname:/" |
| 31 | argidx=$((argidx+1)) |
| 32 | done |
| 33 | |
| 34 | # Indent the result. XXX: variable depth? Do we care? |
| 35 | $cmd -e 's/^/ /' |
| 36 | } |
| 37 | |
| 38 | stripindexes () { |
| 39 | sed -e 's/^\/[[:digit:]]\+\///' -e 's/[^[:space:]]*/\0:/' -e 's/^/ /' |
| 40 | } |
| 41 | |
| 42 | pcdma () { |
| 43 | local mm=$1 |
| 44 | local modem=$2 |
| 45 | echo "CDMA:" |
| 46 | echo -n " ESN: " |
| 47 | dbus $mm $modem $IMODEM_CDMA.GetEsn |
| 48 | |
| 49 | # GetRegistrationState is... 'gratuitously different' in that it |
| 50 | # returns a tuple at the top level instead of an array. As such, |
| 51 | # the unpack here is nasty. |
| 52 | echo " Registration:" |
| 53 | echo -n " CDMA-1x: " |
| 54 | dbus $mm $modem $IMODEM_CDMA.GetRegistrationState | head -n 1 |
| 55 | echo -n " EVDO: " |
| 56 | dbus $mm $modem $IMODEM_CDMA.GetRegistrationState | tail -n 1 |
| 57 | |
| 58 | echo " ServingSystem:" |
| 59 | dbus $mm $modem $IMODEM_CDMA.GetServingSystem \ |
| 60 | | unpacktuple BandClass Band ID |
| 61 | echo -n " SignalQuality: " |
| 62 | dbus $mm $modem $IMODEM_CDMA.GetSignalQuality |
| 63 | } |
| 64 | |
| 65 | pgsm () { |
| 66 | local mm=$1 |
| 67 | local modem=$2 |
| 68 | echo "GSM:" |
| 69 | echo -n " IMSI: " |
| 70 | dbus $mm $modem $IMODEM_GSM_CARD.GetImsi |
| 71 | echo " Registration:" |
| 72 | dbus $mm $modem $IMODEM_GSM_NETWORK.GetRegistrationInfo \ |
| 73 | | unpacktuple Status OperatorCode OperatorName |
| 74 | echo -n " SignalQuality: " |
| 75 | dbus $mm $modem $IMODEM_GSM_NETWORK.GetSignalQuality |
| 76 | } |
| 77 | |
| 78 | hidemdn () { |
| 79 | [ -z $devmode ] && sed -e '/mdn/s/ 001[0-9]*$/ partial/' |
| 80 | } |
| 81 | |
| 82 | hidemin () { |
| 83 | [ -z $devmode ] && sed -e '/min/s/ 001[0-9]*$/ partial/' |
| 84 | } |
| 85 | |
| 86 | pmodem () { |
| 87 | local mm=$1 |
| 88 | local modem=$2 |
| 89 | echo "" |
| 90 | echo "Modem $modem (manager $mm):" |
| 91 | echo " GetStatus:" |
| 92 | dbus $mm $modem $IMODEM_SIMPLE.GetStatus | stripindexes | hidemdn | hidemin |
| 93 | echo " GetInfo:" |
| 94 | dbus $mm $modem $IMODEM.GetInfo | unpacktuple Manufacturer Modem Version |
| 95 | echo " Props:" |
| 96 | modemprops $mm $modem | stripindexes |
| 97 | local mtype=$(modemprop $mm $modem Type) |
| 98 | if [ -z $mtype ]; then |
| 99 | echo "Modem type unknown; no further information available." |
| 100 | return |
| 101 | fi |
| 102 | if [ $mtype -eq $MTYPE_CDMA ]; then |
| 103 | pcdma $mm $modem | sed -e 's/^/ /' |
| 104 | fi |
| 105 | if [ $mtype -eq $MTYPE_GSM ]; then |
| 106 | pgsm $mm $modem | sed -e 's/^/ /' |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | usage () { |
| 111 | echo "Usage: $0 [-d]" |
| 112 | exit 0 |
| 113 | } |
| 114 | |
| 115 | if [ $# -gt 0 ]; then |
| 116 | case "$1" in |
| 117 | -d) |
| 118 | devmode=1 |
| 119 | ;; |
| 120 | *) |
| 121 | usage |
| 122 | ;; |
| 123 | esac |
| 124 | fi |
| 125 | |
| 126 | for mm in $(modemmanagers); do |
| 127 | for m in $(modems $mm); do |
| 128 | pmodem $mm $m |
| 129 | done |
| 130 | done |