#!/bin/bash # # ck3g - controlla rete operatore e intensità di segnale per un modem GSM/3G/UMTS/HSDPA # richiede che il modem supporti il set di comando AT GSM # Copyright (C) 2008 Mario Pascucci # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or # visit the URL http://www.fsf.org/ # # # Changelog # * 2 settembre 2008 # Prima versione rilasciata e funzionante. # USO: # va lanciato da terminale o con una icona "launcher" da desktop # richiede che l'utente abbia i diritti di scrittura sul device del modem # tipicamente basta assegnare all'utente il gruppo "uucp". ####### # PROBLEMI # # # variabili globali # device standard della maggior parte dei modem USB dev3g=/dev/ttyACM0 # risposte del modem mdmreply="" # funzione di invio comandi al modem # il device rappresentante il modem deve essere aperto sul # file handle n. 8 # $1 -> stringa da inviare al modem # ritorna 0 se tutto ok, 1 se qualcosa va storto, la stringa di risposta in $mdmreply function mdmsend { mdmreply="" echo -ne $1>&8 2>/dev/null res=$? if [ "$res" -ne 0 ]; then mdmreply="Errore in scrittura modem: $res" return 1 fi for ((;;)); do read -u 8 -t 2 res=$? # errore in lettura, timeout o device chiuso if [ "$res" -ne 0 ]; then mdmreply="$mdmreply Errore in lettura modem: $res" return 1 fi # comando eseguito con successo if [ -n "$REPLY" ]; then if [ "$REPLY" == "OK" ]; then return 0 fi fi mdmreply="$mdmreply \n$REPLY" done } # funzione di apertura modem # $1=(stringa) pathname copmpleto del device del modem # viene sempre aperto sul file handle 8 function mdmopen { # apertura exec 8<>$1 res=$? if [ $res -ne 0 ]; then echo "Impossibile aprire il modem" exit 1 fi } # funzione di chiusura modem # viene sempre usato il file handle 8 function mdmclose { # apertura exec 8<&- res=$? if [ $res -ne 0 ]; then echo "Impossibile chiudere il modem" exit 1 fi } unset serlist # elenco delle seriali presenti # crea un array con tutte le seriali trovate function listaSeriali { lista=`ls -1 /dev/ttyS* /dev/ttyUSB* /dev/ttyACM* 2>/dev/null` if [ -z "$lista" ]; then return 0 fi set $lista i=0 while [ -n "$1" ]; do serlist[$i]=$1 shift i=$((i + 1)) done return $i } # visualizza un menu per la shell # richiede function shellMenu { local num num=$1 for ((;;)); do for ((i=0;i<$num;i++)); do echo $i - ${serlist[$i]} done echo -n "Scegli la seriale (0-$((num-1)), r per ripetere): " read r=$REPLY if [ -n "$r" -a "$r" != "r" -a "$r" != "R" ]; then if [ "$r" -lt 0 -o "$r" -ge $num ]; then continue fi return $r fi done } ######################################################### # # Programma principale # ######################################################## # controlla se esiste Zenity # e se c'è un display X collegato (rudimentale, ma efficace) if [ -f `which zenity` -a -n "$DISPLAY" ]; then GUI=1 else GUI=0 fi # elenca le seriali listaSeriali num=$? if [ "$num" -le 0 ]; then if [ "$GUI" -eq 0 ]; then echo "Nessuna seriale trovata, non posso continuare." else zenity --error --text="Nessuna seriale trovata, non posso continuare." fi exit 1 fi # menu per scegliere a quale seriale è connesso il modem if [ "$GUI" -eq 0 ]; then shellMenu $num dev3g=${serlist[$?]} else lista=`for((i=0;i<$num;i++));do echo -n "$i ${serlist[$i]} ";done` sel=`zenity --title="Scelta del device del modem" --list --text="Seleziona la seriale del modem" --column=n. --column="Nome device" $lista` if [ "$?" -ne 0 ]; then # premuto "annulla" exit 1 fi dev3g=${serlist[$sel]} fi # verifica stato modem mdmopen $dev3g mdmsend "ate0\r" res=$? if [ "$res" -ne 0 ]; then if [ "$GUI" -eq 0 ]; then echo "Non riesco a leggere il livello di segnale dal modem, esco." else zenity --error --text="Non riesco a leggere il livello di segnale dal modem, esco." fi exit 1 fi # lettura livello di segnale mdmsend "at+csq\r" level=`echo -e "$mdmreply" | grep "+CSQ" | cut -d : -f 2 | cut -d , -f 1 | tr -d ' '` if [ "$GUI" -eq 0 ]; then echo Livello segnale: $level su 31 fi mdmsend "at+cops?\r" op=`echo -e "$mdmreply" | grep "+COPS" | cut -d , -f 3 | tr -d '"'` if [ "$GUI" -eq 0 ]; then echo Operatore: $op else # se si vuole si può usare la variante con un cursore a scala, basta togliere # il commento alla riga successiva e commentare le due seguenti zenity --title="Stato del modem" --scale --text="Operatore: $op, livello segnale:" --value=$level --min-value=0 --max-value=31 --step=1 > /dev/null perc=$(($level * 100 / 31)) zenity --title="Stato del modem" --progress --text="Operatore: $op, livello segnale $perc% ($level su 31):" --percentage=$perc > /dev/null fi mdmclose