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