bt-audio: check for presence of snd-bt-sco
[grml-scripts.git] / usr_sbin / bt-hid
1 #!/bin/sh
2 # Filename:      bt-hid
3 # Purpose:       connect human input device via bluetooth 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: Die Aug 08 19:58:41 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 case "$1" in
18   start)
19    einfo "Starting bluetooth support."
20     eindent
21
22     if pgrep dbus-daemon 1>/dev/null; then
23       einfo "dbus already running." ; eend 0
24     else
25       einfo "Starting dbus."
26       /etc/init.d/dbus start 1>/dev/null ; eend $?
27     fi
28
29     if pgrep sdpd 1>/dev/null; then
30       einfo "Main bluetooth daemons seem to be already running." ; eend 0
31     else
32       einfo "Starting main bluetooth support."
33       /etc/init.d/bluetooth start 1>/dev/null ; eend $?
34     fi
35
36     if pgrep hcid 1>/dev/null; then
37       einfo "hcid already running."
38     else
39       einfo "Starting hcid."
40        HCIINFO=$(mktemp)
41        if start-stop-daemon --start --exec /usr/sbin/hcid 1>$HCIINFO 2>&1 ; then
42          rm -f $HCIINFO
43          eend 0
44        else
45          eerror "hcid could not be started: `cat $HCIINFO`. Exiting."
46          rm -f $HCIINFO
47          eend 1
48          exit 1
49        fi
50     fi
51      einfo "Loading bluetooth modules:"
52      for module in bluetooth ohci1394 hci_usb ; do
53        eindent
54        einfo "$module" ; modprobe $module ; eend $?
55        eoutdent
56      done
57
58      einfo "Scanning for human input device(s). Press the 'connect' button on the device!"
59      einfo "Scanning might take a while. Searching..."
60      SUCCESS=$(mktemp)
61      ERROR=$(mktemp)
62      if hidd --search 1>${SUCCESS} 2>${ERROR} ; then
63       if ! grep -q 'Connecting to device' $SUCCESS ; then
64          eerror "Could not find any devices. Exiting." ; eend 1
65          exit 1
66       else
67        ID=$(awk '/Connecting to device/ {print $4}' $SUCCESS)
68        if [ -n "$ID" ] ; then
69         einfo "Success: connected device ${ID}." ; eend 0
70         logger -t "bluez-connect" "connected human input device ${ID}"
71        else
72         ewarn "Warning: searching for device succeded but no connection could be established."
73        fi
74       fi
75      else
76        eerror "Error: `cat $ERROR`" ; eend 1
77      fi
78      rm -f $SUCCESS $ERROR
79     ;;
80   stop)
81     einfo "Stopping hcid."
82     killall hcid ; eend $? # workaround because start-stop-daemon does not work :-/
83     einfo "Disconnecting all human input devices."
84     logger -t "bluez-connect" "disconnected all human input devices"
85     hidd --killall ; eend $?
86     ;;
87   restart|force-reload)
88     $0 stop
89     sleep 1
90     $0 start
91     ;;
92   status)
93     local INFO="$(hidd --show)"
94     einfo "$0 - checking status:"
95     eindent
96     if pgrep hcid 1>/dev/null ; then
97       einfo "hcid running." ; eend 0
98     else
99       eerror "hcid not running." ; eend 1
100     fi
101     if [ -n "$INFO" ] ; then
102       einfo "$INFO" ; eend 0
103     else
104       eerror "No devices connected." ; eend 1
105     fi
106     eoutdent
107     ;;
108   *)
109     echo "Usage: $0 {start|stop|status|restart|force-reload}"
110     exit 1
111     ;;
112 esac
113
114 eoutdent
115
116 exit 0
117
118 ## END OF FILE #################################################################