#!/bin/sh
#------------------------------------------------------------------------------
# Creation:     Hans Kraus                                         3.10.4
# Last Update:  $Id: get_umts_csq 18478 2010-06-16 10:31:05Z gdw $
#
#------------------------------------------------------------------------------
# Get signal strength and error rate out of the Adapter using sending 'AT+CSQ'
# The first value (before the comma) should be signal strength in dBm.
# The second one (after the comma) should be bit error rate in %.
# Both seem not to be what they should.
#------------------------------------------------------------------------------

case $1 in
  tty*)
    umts_ctrl=$1 ;;
  webif|rrd)
    [ -f /var/run/umts.ctrl ] && umts_ctrl=`cat /var/run/umts.ctrl`
    [ -z "$umts_ctrl" ] && return
    ;;
  *)
    echo "this is not a 'tty' interface"
    echo "usage: `basename $0` ttyS0|ttyS1|...|ttyUSB0|...|ttyACM0|...|ttyHS0|..."
    return ;;
esac

while [ -f /var/lock/umts.get_csq ] ; do
  sleep 1
done
>/var/lock/umts.get_csq

for j in $umts_ctrl ; do
  chat -e -t1 '' "AT+CSQ" OK >/dev/$j </dev/$j 2>/tmp/umts.csq
  sed -n '/\+CSQ:/p' /tmp/umts.csq | sed -n 's/.*\+CSQ:.*\([0-9,]*\).* /\1/g;$p'|grep ""
  [ $? = 0 ] && break
done

[ $1 = webif -o $1 = rrd ] && echo $umts_ctrl | sed -e "s/$j/ /;s/./$j &/">/var/run/umts.ctrl
if [ $1 = webif ] ; then
  >/tmp/umts.settings
  if [ ! "`grep -hE "^pcmcia:.00AF.(1AAF|0276)" /sys/bus/*/devices/*/modalias`" ] ; then
    chat -e -t2 '' "AT&V" OK >/dev/$j </dev/$j 2>/tmp/umts.settings
  fi
fi
rm -f /var/lock/umts.get_csq
