Fix cleanup and diff.
[nethome.git] / bin / line9k
1 #! /bin/bash
2 #
3 #       $Id$
4 #       http://www.jankratochvil.net/project/line9k/
5 #       Modem / Nokia 9110 incoming GSM data gate
6 #
7 #       /etc/ppp/chap-secrets:
8 #               Secrets for authentication using CHAP
9 #               client          server  secret          IP addresses
10 #               username1a      line9k  secret1a        192.168.90.11
11 #               username1b      line9k  secret1b        192.168.90.11
12 #               username2       linecr  secret2         192.168.90.12
13 #
14 #       /etc/inittab:
15 #               l9:2345:respawn:/usr/local/sbin/line9k line9k
16 #               lc:2345:respawn:/usr/local/sbin/line9k linecr
17 #
18 #       Charger scheme:
19 #                             1N4148
20 #               RS232 DTR------|>|------     --------------+--------------     ----AC conn.
21 #                                       |   |              |              |   |
22 #                                       )  /               |   1N4148     )  /
23 #                                       ) /                 ----|>|----   ) /
24 #                              680R     ) --                           |  ) --
25 #                             ______    |   |                          |  |   |
26 #               RS232 GND----|______|---     ----PS/2 +5V     PS/2 GND-+--     ----AC conn.
27 #                                    RELSIA05-500                       RAS-0515
28 #
29 #       Manual charge control:
30 #               /usr/local/sbin/line9k charge on        # Force charging on
31 #               /usr/local/sbin/line9k charge off       # Automatic charging
32 #               /usr/local/sbin/line9k charge           # Query charging force
33
34
35 device=$1
36
37 # Local IP address of setup pointtopoint interfaces
38 LOCAL_IP=192.168.90.10
39
40 # Nameserver for remote clients:
41 NS=192.168.90.10
42
43 # Percent of battery where charging starts
44 charge_force_le=50;
45
46 # Charger control port
47 charge_ttyS=/dev/ttyS0
48
49 # Time in seconds to charge the battery after reaching 100% state
50 max_limit_time=$[3*60*60];
51
52 case $device in
53         line9k)
54                 PORT=/dev/ttyS4 # 9110
55                 BAUD=19200
56                 # Local IP address of setup pointtopoint interfaces
57                 REMOTE_IP=192.168.90.11
58                 RINGS=1
59                 OPTIONS="-crtscts xonxoff asyncmap a0000"
60                 # "server" field in /etc/ppp/chap-secrets
61                 AUTH_NAME=line9k
62                 CHARGE=true
63                 ;;
64         linecr)
65                 PORT=/dev/ttyS3 # Courier
66                 BAUD=57600
67                 REMOTE_IP=192.168.90.12
68                 RINGS=7
69                 OPTIONS="crtscts asyncmap 0"
70                 AUTH_NAME=linecr
71                 CHARGE=false
72                 ;;
73
74 ##############################################################################
75 # Configuration section ends here.
76 ##############################################################################
77
78         charge)
79                 function chargestate
80                 {
81                         echo -n "Charging force: "
82                         if test -f $charge_ttyS.charge;then
83                                 echo "YES"
84                         else
85                                 echo "NO"
86                                 fi
87                 }
88                 chargestate
89                 if [ "$2" = on -o "$2" = yes ];then
90                         echo "CHANGED: YES"
91                         touch $charge_ttyS.charge
92                         baud=9600
93                 elif [ "$2" = off -o "$2" = no ];then
94                         echo "CHANGED: NO"
95                         rm -f $charge_ttyS.charge
96                         baud=0
97                         fi
98                 stty -F $charge_ttyS -hup clocal $baud 2>/dev/null
99                 [ -n "$2" ] && chargestate
100                 exit 0
101                 ;;
102         *)
103                 echo "Unknown device: $device"
104                 exit 1
105         esac
106
107 RING1='"RING" "\c"'
108 RINGSTR=""
109 while [ $RINGS -gt 0 ];do
110         # FIXME: timeout is not reset after stray ring(s)
111         RINGSTR="$RINGSTR $RING1"
112         RINGS=$[$RINGS-1]
113         done
114 LOGGER="logger -i -t $device"
115
116 # Use single AT/OK after ATZ to set terminal speed for USRobotics Courier
117 exec pppd "$PORT" "$BAUD" modem lock $OPTIONS $LOCAL_IP:$REMOTE_IP ms-dns "$NS" \
118         connect '
119                 charge_val_last=-1;
120                 baud_last=0;
121                 limit_date_last=0;
122                 aborts='\''
123                         ABORT       "ERROR"
124                         ABORT       "BUSY"
125                         ABORT       "NO\sANSWER"
126                         ABORT       "NO\sCARRIER"
127                         ABORT       "NO\sDIAL\sTONE"
128                         ABORT       "VOICE"
129                         '\'';
130                 rc=3;
131                 while test $rc -eq 3; do
132                 chat                          \
133                         TIMEOUT     1         \
134                         ""          "ATZ"     \
135                         "OK-ATZ-OK" "AT"      \
136                         ABORT       "+CBC: 0,0"   \
137                         ABORT       "+CBC: 0,25"  \
138                         ABORT       "+CBC: 0,50"  \
139                         ABORT       "+CBC: 0,75"  \
140                         ABORT       "+CBC: 0,100" \
141                         "OK"                  \
142                                 '"`if $CHARGE;then echo "AT+CBC" NOTREACHED;fi`"' \
143                                     "\c"      \
144                         ;
145                 rc=$?; test $rc -eq 0 -o $rc -ge 4 || exit $rc;
146                 if '$CHARGE';then
147                         charge_arr=(0 25 50 75 100);
148                         charge_val=${charge_arr[$[$rc-4]]};
149                         charge_force_le='$charge_force_le';
150                         charge_limit_gt=99;
151                         max_limit_time='$max_limit_time';
152                         baud=$baud_last;
153                         charge_ttyS='$charge_ttyS';
154                         if test -f $charge_ttyS.charge;then
155                                 baud=nocontrol;
156                         else
157                                 if test $baud = nocontrol;then
158                                         baud=0;
159                                         fi;
160                                 if test $charge_val -le $charge_force_le;then baud=9600;fi;
161                                 if test $charge_val -gt $charge_limit_gt;then
162                                         limit_date=$[`date +%s`];
163                                         if test $limit_date_last -eq 0;then
164                                                 limit_date_last=$limit_date;
165                                                 if test $baud -ne 0;then
166                                                         '"$LOGGER"' "CHARGE state: full-stage charging entered";
167                                                         fi;
168                                         else
169                                                 if test $baud -ne 0 -a $[$limit_date-$limit_date_last] -gt $max_limit_time;then
170                                                         '"$LOGGER"' "CHARGE state: full-stage charging terminated";
171                                                         baud=0;
172                                                         fi;
173                                                 fi;
174                                 else
175                                         limit_date_last=0;
176                                         fi;
177                                 fi;
178                         if test $charge_val_last -ne $charge_val -o $baud_last '!=' $baud;then
179                                 '"$LOGGER"' "CHARGE state: charge=$charge_val; baud=$baud";
180                                 fi;
181                         charge_val_last=$charge_val;
182                         baud_last=$baud;
183                         if test $baud '!=' nocontrol;then
184                                 stty -F $charge_ttyS -hup clocal $baud 2>/dev/null;
185                                 fi;
186                         fi;
187                 chat                          \
188                         $aborts               \
189                         TIMEOUT     590       \
190                         '"$RINGSTR"'          \
191                         ;
192                 rc=$?; test $rc -eq 0 -o $rc -eq 3 || exit $rc;
193                 done
194                 chat -v $aborts               \
195                         TIMEOUT     40        \
196                         REPORT      "CONNECT" \
197                         ""          "ATA"     \
198                         "CONNECT"   "\c"      \
199                         "^M"        "\c"      \
200                         ;
201                 rc=$?; test $rc -eq 0 || exit $rc;
202                 baud=9600;
203                 stty -F $charge_ttyS -hup clocal $baud 2>/dev/null;
204                 ' \
205         -ccp nodefaultroute lcp-echo-interval 30 lcp-echo-failure 4 lcp-max-configure 30 -detach debug nologfd kdebug 255 \
206         require-chap -pap auth name $AUTH_NAME