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