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