Remove all bashisms; drop grml-muttng
[grml-scripts-core.git] / usr_sbin / bt-audio
1 #!/bin/sh
2 # Filename:      bt-audio
3 # Purpose:       connect bluetooth audio device (e.g. headset) to local system
4 # Authors:       grml-team (grml.org), (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 . /etc/grml/lsb-functions
10 . /etc/grml/script-functions
11
12 check4root || exit 1
13
14 if ! modprobe -l | grep snd-bt-sco ; then
15    eerror "Sorry, could not find kernel module snd-bt-sco. Exiting." ; eend 1
16    ewarn "The kernel version you are using either does not support the snd-bt-sco module..."
17    ewarn "... yet or you should take a look at Debian package bluez-audio."
18    exit 1
19 fi
20
21 HCID_CONF='/etc/bluetooth/bt_headset.conf'
22 [ -n "$PIN" ] || PIN='0000'
23
24 case "$1" in
25   start)
26    einfo "Starting bluetooth support."
27     eindent
28
29     if ! [ -r "$HCID_CONF" ] ; then
30       ewarn "$HCID_CONF does not exist. Setting it up."
31       cat > $HCID_CONF <<EOF
32 # Bluetooth headset configuration file created by bt-audio.
33
34 # HCId options
35 options {
36         # Automatically initialize new devices
37         autoinit yes;
38
39         # Security Manager mode
40         #   none - Security manager disabled
41         #   auto - Use local PIN for incoming connections
42         #   user - Always ask user for a PIN
43         #
44         security auto;
45
46         # Pairing mode
47         #   none  - Pairing disabled
48         #   multi - Allow pairing with already paired devices
49         #   once  - Pair once and deny successive attempts
50         pairing multi;
51
52         # Default PIN code for incoming connections
53         passkey "0000";
54 }
55
56 # Default settings for HCI devices
57 device {
58         # Local device name
59         #   %d - device id
60         #   %h - host name
61         name "%h-%d";
62
63         # Local device class
64         class 0x3e0100;
65
66         # Default packet type
67         #pkt_type DH1,DM1,HV1;
68
69         # Inquiry and Page scan
70         iscan enable; pscan enable;
71
72         # Default link mode
73         #   none   - no specific policy
74         #   accept - always accept incoming connections
75         #   master - become master on incoming connections,
76         #            deny role switch on outgoing connections
77         lm accept;
78
79         # Default link policy
80         #   none    - no specific policy
81         #   rswitch - allow role switch
82         #   hold    - allow hold mode
83         #   sniff   - allow sniff mode
84         #   park    - allow park mode
85         lp rswitch,hold,sniff,park;
86 }
87 EOF
88     fi
89
90     if pgrep dbus-daemon 1>/dev/null; then
91       einfo "dbus already running." ; eend 0
92     else
93       einfo "Starting dbus."
94       /etc/init.d/dbus start 1>/dev/null ; eend $?
95     fi
96
97     if pgrep hcid 1>/dev/null; then
98       einfo "hicd already running." ; eend 0
99     else
100       einfo "Starting hcid."
101        HCIINFO=$(mktemp)
102        if /usr/sbin/hcid -f $HCID_CONF 1>$HCIINFO 2>&1 ; then
103          rm -f $HCIINFO
104          eend 0
105        else
106          eerror "hcid could not be started: `cat $HCIINFO`. Exiting."
107          rm -f $HCIINFO
108          eend 1
109          exit 1
110        fi
111     fi
112
113     einfo "Loading bluetooth modules:"
114     for module in bluetooth ohci1394 hci_usb snd-bt-sco ; do
115       eindent
116       einfo "$module" ; modprobe $module ; eend $?
117       eoutdent
118     done
119
120     einfo "Scanning for bluetooth audio device. Press the 'connect' button on the device!"
121     einfo "Scanning might take a while. Searching..."
122     SUCCESS=$(mktemp)
123     ERROR=$(mktemp)
124
125     if hcitool scan 1>${SUCCESS} 2>${ERROR} ; then
126      if ! grep -q ':' $SUCCESS ; then
127         eerror "Could not find any devices. Exiting." ; eend 1
128         exit 1
129      else
130      ID=$(grep '[[:alnum:]][[:alnum:]]:' $SUCCESS | awk '{print $1}')
131       if [ -n "$ID" ] ; then
132        einfo "Success: connected device ${ID}." ; eend 0
133        logger -t "bluez-connect" "connected human input device ${ID}"
134        btsco -v $ID 1>/dev/null &
135        # hcitool cc $ID
136       else
137        ewarn "Warning: searching for device succeded but no connection could be established."
138       fi
139      fi
140     else
141       eerror "Error: `cat $ERROR`" ; eend 1
142     fi
143
144     rm -f $SUCCESS $ERROR
145     ;;
146   stop)
147     HCIDAEMON=$(pgrep hcid)
148     if [ -n "$HCIDAEMON" ] ; then
149        einfo "Stopping hcid."
150        kill $HCIDAEMON
151        eend $? # workaround because start-stop-daemon does not work :-/
152     else
153        einfo "No running hcid found, nothing to stop."
154        eend 0
155     fi
156
157     ewarn "Will not stop dbus as it might be used by other services." ; eend 0
158
159     einfo "Disconnecting all human input devices."
160     logger -t "bluez-connect" "disconnected all human input devices"
161     hidd --killall ; eend $?
162     ;;
163   restart|force-reload)
164     $0 stop
165     sleep 1
166     $0 start
167     ;;
168   test)
169     if [ -r /usr/share/centericq/sms.wav ] ; then
170       einfo "Trying to play /usr/share/centericq/sms.wav on headset:"
171       echo -n "   "
172       aplay -B 1000000 -D plughw:Headset  /usr/share/centericq/sms.wav 1>/dev/null ; eend $?
173     else
174       eerror "/usr/share/centericq/sms.wav does not exist. Can not test sound." ; eend 1
175     fi
176     ;;
177   status)
178     einfo "$0 - checking status:"
179     eindent
180     if pgrep hcid 1>/dev/null ; then
181       einfo "hcid running." ; eend 0
182       if [ -d /proc/asound/Headset ] ; then
183         einfo "cat /proc/asound/Headset/*/info:"
184         cat /proc/asound/Headset/*/info ; eend $?
185       else
186         eerror "Directory /proc/asound/Headset does not exist." ; eend 1
187       fi
188     else
189       eerror "hcid not running." ; eend 1
190     fi
191     eoutdent
192     ;;
193   *)
194     echo "Usage: $0 {start|stop|status|restart|force-reload|test}"
195     exit 1
196     ;;
197 esac
198
199 eoutdent
200
201 exit 0
202
203 ## END OF FILE #################################################################