Ticket #1003: K36atpoff

File K36atpoff, 4.0 KB (added by mwester@…, 11 years ago)

/etc/rc0.d/K36atpoff (updated, and with debug statements)

Line 
1#!/bin/sh
2#
3# atpoff - issues the power-off command to the GTA01 GSM modem
4
5PATH=/bin:/usr/bin:/sbin:/usr/sbin
6
7timeout=3
8dlog="/debug.log"
9
10[ -f /etc/default/rcS ] && . /etc/default/rcS
11[ -f /etc/default/gsmd ] && . /etc/default/gsmd
12
13# Silently exit if this is not a GTA01 device
14[ -n "$GSM_DEV" ] || exit
15
16case "$1" in
17    start)
18        # Do nothing if starting; gsmd will do that.
19        ;;
20
21    stop)
22        echo -n "Stopping GSM modem:"
23        $0 execute_gsm_poweroff <"$GSM_DEV"
24        sync
25        sleep 1
26        stty -F "$GSM_DEV" -crtscts
27        [ -n "$GSM_POW" ] && echo "0" >$GSM_POW
28        stty -F "$GSM_DEV" sane
29        ;;
30
31    execute_gsm_poweroff)
32        rm -f "$dlog"
33        echo "atpoff: Stopping GSM modem" >$dlog
34
35        # First reset the serial port status, then reset the
36        # modem itself, and finally turn on flow control so
37        # we can talk to the modem - if you do this in the
38        # wrong order, you can lock up the kernel.
39        stty sane -echo -crtscts
40        [ -n "$GSM_POW" ] && echo "0" >$GSM_POW
41        sleep 1
42        [ -n "$GSM_POW" ] && echo "1" >$GSM_POW
43        stty -parenb -parodd cs8 -hupcl -cstopb cread clocal crtscts \
44                ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr \
45                igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel \
46                -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel \
47                nl0 cr0 tab0 bs0 vt0 ff0 -isig -icanon -iexten -echo \
48                -echoe -echok -echonl -noflsh -xcase -tostop -echoprt \
49                echoctl echoke min 1 time 1 -hup 115200 \
50                -hup tostop
51
52        # Look for the initialization message from the modem
53        i=8
54        resetseen=0
55        while [ $i -ne 0 ] ; do
56            read -t $timeout R
57            if [ $? -eq 0 ]; then
58                echo "([INFO ] reset-reply-text: \"$R\")"
59                echo "([INFO ] reset-reply-text: \"$R\")" >>$dlog
60                if [ "$R" = "AT-Command Interpreter ready" ] ; then
61                    echo "([GOOD ] reset-reply-ok)"
62                    echo "([GOOD ] reset-reply-ok)" >>$dlog
63                    resetseen=1
64                else
65                    # We must be still reading junk; continue searching
66                    resetseen=0
67                fi
68            else
69                if [ $resetseen -eq 1 ]; then
70                    echo "([GOOD ] reset-reply-timed-out: $i)"
71                    echo "([GOOD ] reset-reply-timed-out: $i)" >>$dlog
72                    i=0
73                else
74                    echo "([ERROR] reset-reply-timed-out: $i)"
75                    echo "([ERROR] reset-reply-timed-out: $i)" >>$dlog
76                    i=$((i - 1))
77                fi
78            fi
79        done
80
81        # Diag: Ensure that we can talk to the modem
82
83#       cmd="AT"
84        cmd="AT+CGMM"
85        echo "([INFO ] send-text: \"${cmd}\")"
86        echo "([INFO ] send-text: \"${cmd}\")" >>$dlog
87        echo -e "${cmd}\r" >$GSM_DEV
88
89        read -t $timeout R
90        [ $? -eq 0 -a "X$R" = "X${cmd}" ] && read -t $timeout R
91        if [ $? -eq 0 ]; then
92            echo "([INFO ] test-reply-text-1: \"$R\")"
93            echo "([INFO ] test-reply-text-1: \"$R\")" >>$dlog
94            read -t $timeout R
95            [ $? -eq 0 -a -z "$R" ] && read -t $timeout R
96            if [ $? -eq 0 ]; then
97                echo "([INFO ] test-reply-text-2: \"$R\")"
98                echo "([INFO ] test-reply-text-2: \"$R\")" >>$dlog
99                read -t $timeout R
100                [ $? -eq 0 -a -z "$R" ] && read -t $timeout R
101                if [ $? -eq 0 ]; then
102                    echo "([INFO ] test-reply-text-3: \"$R\")"
103                    echo "([INFO ] test-reply-text-3: \"$R\")" >>$dlog
104                else
105                    echo "([INFO ] test-reply-timed-out-3)"
106                    echo "([INFO ] test-reply-timed-out-3)" >>$dlog
107                fi
108            else
109                echo "([INFO ] test-reply-timed-out-2)"
110                echo "([INFO ] test-reply-timed-out-2)" >>$dlog
111            fi
112        else
113            echo "([INFO ] test-reply-timed-out-1)"
114            echo "([INFO ] test-reply-timed-out-1)" >>$dlog
115        fi
116
117        # Always send the command to power off
118        cmd="AT@POFF"
119        echo "([INFO ] send-text: \"${cmd}\")"
120        echo "([INFO ] send-text: \"${cmd}\")" >>$dlog
121        echo -e "${cmd}\r" >$GSM_DEV
122
123        # Confirm what we get back - should be the command issued
124        # (Note that if the real power-off command is issued, the
125        # read command seems to hang, or the tty hangs.
126#       read -t $timeout R
127#        if [ $? -eq 0 ]; then
128#           echo "([INFO ] power-off-reply-text: \"$R\")"
129#           echo "([INFO ] power-off-reply-text: \"$R\")" >>$dlog
130#        else
131#           echo "([ERROR] power-off-reply-timed-out)"
132#           echo "([ERROR] power-off-reply-timed-out)" >>$dlog
133#       fi
134
135        echo "exit" >>$dlog
136        ;;
137
138    *)
139        echo "Usage: /etc/init.d/atpoff {start|stop}"
140        exit 1
141        ;;
142esac
143
144exit 0