#!/bin/sh VERBOSE=no PIDS=`pidof -x "$0"` # make sure pidof sees us if [ -z "$PIDS" ]; then exec "$0" exit 1 fi GSM_DEVICE="/dev/ttySAC0" GSM_POWER="/sys/devices/platform/neo1973-pm-gsm.0/power_on" GSM_RESET="/sys/devices/platform/neo1973-pm-gsm.0/reset" GSM_COMMANDS="Z E0V1 +CFUN=1 +COPS=0" gsm_running () { if [ "$PIDS" != "$$" ]; then return 0 else return 1 fi } gsm_send () { cmd="$1" echo -ne "AT${cmd}\r" > "$GSM_DEVICE" } # this function might block forever gsm_wait () { ret="UNKNOWN" while read status; do case "$status" in OK*) ret="OK" ;; ERROR*) ret="ERROR" ;; *) ret="UNKNOWN" ;; esac if [ "x$ret" != "xUNKNOWN" ]; then break fi done < "$GSM_DEVICE" if [ "x$ret" != "xOK" ]; then return 1 fi return 0 } gsm_setup () { [ $VERBOSE == "yes" ] && echo -n "Powering up GSM modem..." echo 0 > "$GSM_POWER"; sleep 1 echo 1 > "$GSM_POWER"; sleep 1 echo 1 > "$GSM_RESET"; sleep 1 echo 0 > "$GSM_RESET"; sleep 4 [ $VERBOSE == "yes" ] && echo "done" stty -F "$GSM_DEVICE" cooked -opost -echo crtscts 115200 } gsm_wakeup () { [ $VERBOSE == "yes" ] && echo "Waking up GSM modem" # there is at most one OK/ERROR even if we send multiple commands gsm_send "" gsm_send "" gsm_send "" if gsm_wait; then [ $VERBOSE == "yes" ] && echo "GSM modem awake" return 0 else echo "failed to wake up GSM modem" return 1 fi } start () { gsm_setup if gsm_wakeup; then for cmd in $GSM_COMMANDS do [ $VERBOSE == "yes" ] && echo "Sending AT$cmd" gsm_send "$cmd" if ! gsm_wait; then echo "AT$cmd failed" break fi done fi } set -e case "$1" in start) if gsm_running; then echo "Another instance is already running" exit 1 fi start & ;; stop) if gsm_running; then PIDS=`pidof -x -o "$$" "$0"` echo -n "Stopping other instances..." kill $PIDS echo "done" fi ;; *) echo "Usage: $0 {start|stop}" exit 1 ;; esac