6e4599cd72a3de98a64236bc337e6e2a2bf9c26e
[grml-network.git] / sbin / modemlink
1 #!/bin/bash +x
2 # Filename:      modemlink
3 # Purpose:       start device drivers and set /dev/modem link in GRML
4 # Authors:       (c) Klaus Knopper Mar 2004, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin"
10 export PATH
11
12 # XDIALOG_HIGH_DIALOG_COMPAT=1
13 # export XDIALOG_HIGH_DIALOG_COMPAT
14
15 if [ $UID != 0 ] ; then
16    echo Error: become root before starting $0 >& 2
17    exit 100
18 fi
19
20 TMP=$(mktemp)
21
22 bailout(){
23   rm -f "$TMP"
24   exit $1
25 }
26
27 DIALOG="dialog"
28 # [ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"
29
30 trap bailout 1 2 3 15
31
32 # LANGUAGE
33 [ -r /etc/default/locale ] && . /etc/default/locale
34
35 # Language-dependent Messages
36 case "$LANGUAGE" in
37 de*|at*|ch*)
38 TITLE1="Art der Verbindung"
39 MESSAGE1="Bitte die gewuenschten Verbindungsart auswaehlen:"
40 SERIAL="Serielle Schnittstelle (z.B. Modem)"
41 WINMODEM="Nach nicht-unterstuetzten \"WinModems\" suchen"
42 SIR="Standard SIR (oder USB)"
43 USB="USB Schnittstelle (z.B. Modem)"
44 IRDA="Infrarot Schnittstelle (z.B. fuer Handy/PDA)"
45 BLUETOOTH="Bluetooth Funkadapter (z.B. fuer Handy/PDA)"
46 TITLE_DONGLES="IRDA Adapter-Typ"
47 MESSAGE_DONGLES="Bitte den verwendeten IRDA-Adaptertyp auswaehlen:"
48 TITLE_TEST="Adaptertest - Strg-C zum Beenden des Tests"
49 TITLE_SRVFAIL="FEHLER"
50 MESSAGE_SRVFAIL="Konnte Daemon fuer Geraet nicht starten. Wahrscheinlich wurde die falsche Schnittstelle ausgewaehlt." 
51 AUTOSEARCH="ALLE Schnittstellen scannen"
52 TITLE_BLUES="Bluetooth-Geraete"
53 MESSAGE_BLUES="Folgende Bluetooth-Geraete befinden sich in der Umgebung. Bitte eins auswaehlen:"
54 TITLE_PIN="PIN"
55 MESSAGE_PIN="Bitte geben Sie eine 4-stellige PIN zur Authentfizierung der Bluetooth-Geraete ein:"
56 TITLE_GAUGE="Device-Scan..."
57 TITLE_LINK="/dev/modem Link erzeugen..."
58 MESSAGE_LINK="Moechten Sie den Symlink /dev/modem auf das neu konfigurierte Geraet setzen? Dies ist notwendig, wenn Sie das Geraet z.B. als Modem fuer kppp oder GPRS benutzen moechten).
59
60 Verlinken von /dev/modem -> "
61 TITLE_RETRY="FEHLER"
62 MESSAGE_RETRY="Das scheint nicht geklappt zu haben. Noch einmal versuchen?"
63 TITLE_BTID="Keine Bluetooth Geraete in Reichweite"
64 MESSAGE_BTID="Auf die Sendeanfrage hat kein Bluetooth-Geraet geantwortet. Bitte geben Sie die Adresse eines entfernten Bluetooth-Geraetes ein."
65 ;;
66 *)
67 TITLE1="Connection Type"
68 MESSAGE1="Please select type of connection:"
69 WINMODEM="Scan for unsupported \"WinModems\""
70 SERIAL="Serial connector (i.e. Modem)"
71 SIR="Standard SIR (or USB)"
72 USB="USB connector (i.e. Modem)"
73 IRDA="Infrared wireless device (i.e. cellphone/PDA)"
74 BLUETOOTH="Bluetooth wireless device (i.e. cellphone/PDA)"
75 TITLE_DONGLES="IRDA Adapter type"
76 MESSAGE_DONGLES="Please select type of IRDA adapter:"
77 TITLE_TEST="Testing adapter - Hit Ctrl-C to end test."
78 TITLE_SRVFAIL="ERROR"
79 MESSAGE_SRVFAIL="Failed to start device daemon. Probably the selected port is wrong."
80 AUTOSEARCH="scan ALL devices"
81 TITLE_BLUES="Bluetooth Devices"
82 MESSAGE_BLUES="These bluetooth devices exist nearby. Please select:"
83 TITLE_PIN="PIN"
84 MESSAGE_PIN="Please chose a 4-digit PIN number for authention of bluetooth devices:"
85 TITLE_GAUGE="Scanning devices..."
86 TITLE_LINK="Create /dev/modem link..."
87 MESSAGE_LINK="Do you want to set the symlink /dev/modem to point to the newly configured device? This is necessary if you want o use the device as modem for kppp, as an example, or for using GPRS).
88
89 Create link /dev/modem -> "
90 TITLE_RETRY="ERROR"
91 MESSAGE_RETRY="This seems to have failed. Do you want to try again?"
92 TITLE_BTID="No bluetooth devices in range"
93 MESSAGE_BTID="No bluetooth devices responded to our scan. Please enter the address of a remote bluetooth device."
94 ;;
95 esac
96
97 # Shortcut description selected
98 TYPES=(serial "$SERIAL" on \
99 usb "$USB" off \
100 irda "$IRDA" off \
101 bluetooth "$BLUETOOTH" off \
102 winmodem "$WINMODEM" off)
103
104 type scanmodem >/dev/null 2>&1 || { unset TYPES[14]; unset TYPES[13]; unset TYPES[12]; }
105 type rfcomm >/dev/null 2>&1 || { unset TYPES[11]; unset TYPES[10]; unset TYPES[9]; }
106 type irdadump >/dev/null 2>&1 || { unset TYPES[8]; unset TYPES[7]; unset TYPES[6]; }
107
108 # man irattach
109 DONGLES=(auto "$SIR" on \
110 act200l "ACTiSYS Ir-200L dongles" off \
111 actisys "ACTiSYS IR-220L dongle" off \
112 actisys+ "ACTiSYS IR-220L+ dongle" off \
113 airport "Airport dongle" off \
114 ep7211 "IR port driver for the Cirrus Logic EP7211 processor (ARM based)" off \
115 esi "Extended Systems JetEye PC ESI-9680" off \
116 girbil "Greenwich GIrBIL dongle" off \
117 litelink "Parallax LiteLink dongle & Extended Systems JetEye PC ESI-9680B" off \
118 old_belkin "Belkin (old) SmartBeam dongle or any dongle only capable of 9600 bauds" off \
119 ma600 "Mobile Action ma600 dongles" off \
120 mcp2120 "Dongles based on the MCP2120 (Microchip)" off \
121 tekram "Tekram IrMate IR-210B dongle" off)
122
123 AUTODEVICE=(auto "$AUTOSEARCH" off)
124
125 SERIALDEVICES=(ttyS0 "(COM1:)" off \
126 ttyS1 "(COM2:)" off \
127 ttyS2 "(COM3:)" off \
128 ttyS3 "(COM4:)" off \
129 ttyS4 "(COM5:)" off \
130 ttyS5 "(COM6:)" off \
131 ttyS6 "(COM7:)" off \
132 ttyS7 "(COM8:)" off \
133 ttyS8 "(COM9:)" off \
134 ttyS9 "(COM10:)" off \
135 ttyS10 "(COM11:)" off \
136 ttyS11 "(COM12:)" off \
137 ttyS12 "(COM13:)" off \
138 ttyS13 "(COM14:)" off \
139 ttyS14 "(COM15:)" off \
140 ttyS15 "(COM16:)" off)
141
142 USBDEVICES=(ttyACM0 "USB Modem 1" off \
143 ttyACM1 "USB Modem 2" off \
144 ttyACM2 "USB Modem 3" off \
145 ttyACM3 "USB Modem 4" off \
146 ttyUSB0 "USB Serial 0" off \
147 ttyUSB1 "USB Serial 1" off \
148 ttyUSB2 "USB Serial 2" off \
149 ttyUSB3 "USB Serial 3" off)
150
151 num=${#COMMANDS[@]}
152
153 #for ((i=0; i<$num; i++)); do
154 ## Remove non-existing desktops/descriptions from list
155 #d0="$(($i * 3))"
156 #d1="$(($i * 3 + 1))"
157 #d2="$(($i * 3 + 2))"
158 #if type "${COMMANDS[$i]}" >/dev/null 2>&1; then
159 #[ "$DESKTOP" = "${DESKTOPS[$(($i * 3))]}" ] && DESKTOPS[$(($i * 3 + 2))]="on" || DESKTOPS[$(($i * 3 + 2))]="off"
160 #else
161 #unset DESKTOPS[$d0]
162 #unset DESKTOPS[$d1]
163 #unset DESKTOPS[$d2]
164 #unset COMMANDS[$i]
165 #fi
166 #done
167
168 gauge(){
169 rm -f "$TMP.done"
170 status=0
171 while [ ! -e "$TMP.done" ]; do echo "$status" ; status="`expr \( 100 - $status \) / 4 + $status`"; sleep 2; done | $DIALOG --title "$TITLE_GAUGE" --gauge "$1" 8 75 0
172 }
173
174 # Stop status bar
175 killgauge(){
176 touch "$TMP.done" ; wait ; rm -f "$TMP.done"
177 }
178
179 conntype(){
180 rm -f "$TMP"
181 $DIALOG --clear --title "$TITLE1" --radiolist "$MESSAGE1" 18 75 9 "${TYPES[@]}" 2>"$TMP" || bailout 1
182 read TYPE <"$TMP"
183 TYPE="${TYPE#\"}"; TYPE="${TYPE%\"}"
184 rm -f "$TMP"
185 return 0
186 }
187
188 startservice(){
189 case "$TYPE" in
190 serial)
191 rm -f "$TMP"
192 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${SERIALDEVICES[@]}" 2>"$TMP" || bailout 1
193 read DEVICE <"$TMP"
194 DEVICE="${DEVICE#\"}"; DEVICE="${DEVICE%\"}"
195 rm -f "$TMP"
196 ;;
197 winmodem)
198 [ -n "$DISPLAY" ] && xterm -e bash -c "scanmodem; read -p 'Enter to quit.'" || { scanmodem; read -p 'Enter to quit.'; }
199 return 1
200 ;;
201 usb)
202 rm -f "$TMP"
203 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${USBDEVICES[@]}" 2>"$TMP" || bailout 1
204 read DEVICE <"$TMP"
205 DEVICE="${DEVICE#\"}"; DEVICE="${DEVICE%\"}"
206 rm -f "$TMP"
207 ;;
208 irda)
209 killall irattach 2>/dev/null
210 modprobe irda 2>/dev/null
211 modprobe ircomm-tty 2>/dev/null
212 rm -f "$TMP"
213 $DIALOG --clear --title "$TITLE_RAWDEVICE" --radiolist "$MESSAGE_RAWDEVICE" 18 75 9 "${AUTODEVICE[@]}" "${SERIALDEVICES[@]}" "${USBDEVICES[@]}" 2>"$TMP" || bailout 1
214 read RAWDEVICE <"$TMP"
215 RAWDEVICE="${RAWDEVICE#\"}"; RAWDEVICE="${RAWDEVICE%\"}"
216 rm -f "$TMP"
217 $DIALOG --clear --title "$TITLE_DONGLES" --radiolist "$MESSAGE_DONGLES" 18 75 9 "${DONGLES[@]}" 2>"$TMP" || bailout 1
218 read DONGLE <"$TMP"
219 DONGLE="${DONGLE#\"}"; DONGLE="${DONGLE%\"}"
220 rm -f "$TMP"
221 case "$DONGLE" in auto) DONGLE="" ;; *) DONGLE="-d $DONGLE" ;; esac
222 if [ "$RAWDEVICE" = "auto" ]; then
223 gauge "Device-Scan..." &
224 for i in /dev/ttyUSB* /dev/ttyS*; do
225 RAWDEVICE="${i##/dev/}"
226 irattach "/dev/$RAWDEVICE" $DONGLE -s
227 usleep 125000
228 done
229 killgauge
230 sleep 2
231 RAWDEVICE="$(ps auxw | awk '/irattach/{print $12}' | head -1)"
232 RAWDEVICE="${RAWDEVICE##/dev/}"
233 else
234 irattach "/dev/$RAWDEVICE" $DONGLE -s
235 sleep 2
236 fi
237 if ifconfig irda0 >/dev/null 2>&1; then
238 true
239 else
240 $DIALOG --clear --title "$TITLE_SRVFAIL" --msgbox "$MESSAGE_SRVFAIL" 18 75
241 DEVICE=""
242 return 1
243 fi
244 DEVICE="ircomm0"
245 rm -f /etc/irda.conf
246 echo "#irda.conf Version: 1.0
247 IRDADEV=/dev/$RAWDEVICE" >/etc/irda.conf
248 [ -n "$DONGLE" ] && echo "DONGLE=\"${DONGLE##-d }\"" >>/etc/irda.conf
249 echo "DISCOVERY=-s
250 ENABLE=yes" >>/etc/irda.conf
251 [ -x /etc/init.d/irda ] && for i in 2 3 5; do ln -sf /etc/init.d/irda /etc/rc$i.d/S99irda; done
252 ;;
253 bluetooth)
254 killall hcid 2>/dev/null
255 killall rfcomm 2>/dev/null
256 hcid || return 1
257 count=0
258 gauge "Device-Scan..." &
259 while read HW NAME; do
260 d0="$(($count * 3))"
261 d1="$(($count * 3 + 1))"
262 d2="$(($count * 3 + 2))"
263 BLUES[$d0]="$HW"; BLUES[$d1]="$NAME"; BLUES[$d2]="off"
264 count=$(($count + 1))
265 done <<EOT
266 $(hcitool scan 2>/dev/null | awk '/[0-9]:[0-9]/{print $0}')
267 EOT
268 killgauge
269 if [ -n "${BLUES[0]}" ]; then
270 $DIALOG --clear --title "$TITLE_BLUES" --radiolist "$MESSAGE_BLUES" 18 75 9 "${BLUES[@]}" 2>"$TMP" || bailout 1
271 else
272 $DIALOG --title "$TITLE_BTID" --inputbox "$MESSAGE_BTID" 15 60 "00:00:00:00:00:00" 2>"$TMP" || bailout 1
273 fi
274 read RAWDEVICE <"$TMP"
275 RAWDEVICE="${RAWDEVICE#\"}"; RAWDEVICE="${RAWDEVICE%\"}"
276 rm -f "$TMP"
277 read PIN < /etc/bluetooth/pin  2>/dev/null
278 rm -f "$TMP"
279 if [ -n "$DISPLAY" ]; then
280 $DIALOG --title "$TITLE_PIN" --inputbox "$MESSAGE_PIN" 8 65 "$PIN" 2>"$TMP" || bailout 1
281 fi
282 read PIN <"$TMP" 2>/dev/null
283 rm -f "$TMP"
284 rm -f /etc/bluetooth/pin
285 echo "$PIN" >/etc/bluetooth/pin
286 rm -f /etc/bluetooth/rfcomm.conf
287 cat >/etc/bluetooth/rfcomm.conf <<EOT
288 #
289 # RFCOMM configuration file.
290 #
291 # Created by modemlink-grml on $(date)
292 #
293
294 rfcomm0 {
295  bind yes;
296 # Bluetooth address of the device
297  device $RAWDEVICE;
298 # RFCOMM channel for the connection
299 #       channel 1;
300 # Description of the connection
301  comment "Selected Bluetooth device";
302 }
303 EOT
304 rfcomm bind all
305 [ -x /etc/init.d/bluez-utils ] && for i in 2 3 5; do ln -sf /etc/init.d/bluez-utils /etc/rc$i.d/S99bluez-utils; done
306 DEVICE=rfcomm0
307 ;;
308 esac
309 return 0
310 }
311
312 # MAIN
313
314 while true; do
315 conntype
316 startservice
317 if [ "$?" = "0" -a -n "$DEVICE" ]; then
318 $DIALOG --title "$TITLE_LINK" --yesno "$MESSAGE_LINK $DEVICE ?" 15 65 || bailout 1
319 rm -f /dev/modem
320 ln -sf /dev/"$DEVICE" /dev/modem
321 break
322 else
323 $DIALOG --clear --title "$TITLE_RETRY" --yesno "$MESSAGE_RETRY" 18 75 || bailout 1
324 fi
325 done
326
327 bailout 0
328
329 ## END OF FILE #################################################################