3 # Copyright (c) 2005 Maksim Yevmenkin <m_evmenkin@yahoo.com>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # KEYWORD: nojail nostart
37 start_cmd="bluetooth_start"
38 stop_cmd="bluetooth_stop"
39 required_modules="ng_bluetooth ng_hci ng_l2cap ng_btsocket"
41 ##############################################################################
42 # Read and parse Bluetooth device configuration file
43 ##############################################################################
47 local _err _file _line _namespace
53 if [ ! -e $_file ]; then
57 if [ ! -f $_file -o ! -r $_file ]; then
58 err 1 "Bluetooth configuration file $_file is not a file or not readable"
69 if [ -z "$_line" ]; then
74 if expr "$_line" : "[a-zA-Z0-9_]*=" > /dev/null 2>&1; then
75 eval "${_namespace}${_line}"
77 warn "Unable to parse line \"$_line\" in $_file"
87 ##############################################################################
88 # Setup Bluetooth stack. Create and connect nodes
89 ##############################################################################
91 bluetooth_setup_stack()
99 ngctl mkpeer ${dev}: hci ${hook} drv \
100 > /dev/null 2>&1 || return 1
102 ngctl name ${dev}:${hook} ${dev}hci \
103 > /dev/null 2>&1 || return 1
105 ngctl msg ${dev}hci: set_debug ${bluetooth_device_hci_debug_level} \
106 > /dev/null 2>&1 || return 1
109 ngctl mkpeer ${dev}hci: l2cap acl hci \
110 > /dev/null 2>&1 || return 1
112 ngctl name ${dev}hci:acl ${dev}l2cap \
113 > /dev/null 2>&1 || return 1
115 ngctl msg ${dev}l2cap: set_debug ${bluetooth_device_l2cap_debug_level} \
116 > /dev/null 2>&1 || return 1
118 # Connect HCI node to the Bluetooth sockets layer
119 ngctl connect ${dev}hci: btsock_hci_raw: raw ${dev}raw \
120 > /dev/null 2>&1 || return 1
122 # Connect L2CAP node to Bluetooth sockets layer
123 ngctl connect ${dev}l2cap: btsock_l2c_raw: ctl ${dev}ctl \
124 > /dev/null 2>&1 || return 1
126 ngctl connect ${dev}l2cap: btsock_l2c: l2c ${dev}l2c \
127 > /dev/null 2>&1 || return 1
129 # Initilalize HCI node
130 ${hccontrol} -n ${dev}hci reset \
131 > /dev/null 2>&1 || return 1
133 ${hccontrol} -n ${dev}hci read_bd_addr \
134 > /dev/null 2>&1 || return 1
136 ${hccontrol} -n ${dev}hci read_local_supported_features \
137 > /dev/null 2>&1 || return 1
139 ${hccontrol} -n ${dev}hci read_buffer_size \
140 > /dev/null 2>&1 || return 1
142 if checkyesno bluetooth_device_discoverable; then
143 if checkyesno bluetooth_device_connectable; then
144 ${hccontrol} -n ${dev}hci write_scan_enable 3 \
145 > /dev/null 2>&1 || return 1
147 ${hccontrol} -n ${dev}hci write_scan_enable 1 \
148 > /dev/null 2>&1 || return 1
151 if checkyesno bluetooth_device_connectable; then
152 ${hccontrol} -n ${dev}hci write_scan_enable 2 \
153 > /dev/null 2>&1 || return 1
155 ${hccontrol} -n ${dev}hci write_scan_enable 0 \
156 > /dev/null 2>&1 || return 1
161 ${hccontrol} -n ${dev}hci write_class_of_device ${bluetooth_device_class} \
162 > /dev/null 2>&1 || return 1
164 if checkyesno bluetooth_device_authentication_enable; then
165 ${hccontrol} -n ${dev}hci write_authentication_enable 1 \
166 > /dev/null 2>&1 || return 1
168 ${hccontrol} -n ${dev}hci write_authentication_enable 0 \
169 > /dev/null 2>&1 || return 1
172 case "${bluetooth_device_encryption_mode}" in
174 ${hccontrol} -n ${dev}hci write_encryption_mode 0 \
175 > /dev/null 2>&1 || return 1
179 ${hccontrol} -n ${dev}hci write_encryption_mode 1 \
180 > /dev/null 2>&1 || return 1
184 ${hccontrol} -n ${dev}hci write_encryption_mode 2 \
185 > /dev/null 2>&1 || return 1
189 warn "Unsupported encryption mode ${bluetooth_device_encryption_mode} for device ${dev}"
194 if checkyesno bluetooth_device_role_switch; then
195 ${hccontrol} -n ${dev}hci write_node_role_switch 1 \
196 > /dev/null 2>&1 || return 1
198 ${hccontrol} -n ${dev}hci write_node_role_switch 0 \
199 > /dev/null 2>&1 || return 1
202 ${hccontrol} -n ${dev}hci change_local_name "${bluetooth_device_local_name}" \
203 > /dev/null 2>&1 || return 1
205 ${hccontrol} -n ${dev}hci initialize \
206 > /dev/null 2>&1 || return 1
211 ##############################################################################
212 # Shutdown Bluetooth stack. Destroy all nodes
213 ##############################################################################
215 bluetooth_shutdown_stack()
219 ngctl shutdown ${dev}hci: > /dev/null 2>&1
220 ngctl shutdown ${dev}l2cap: > /dev/null 2>&1
225 ##############################################################################
227 ##############################################################################
235 # Try to figure out device type by looking at device name
237 # uartX - serial/UART Bluetooth device
239 load_kld ng_h4 || return 1
243 # Obtain unit number from device.
244 unit=`expr ${dev} : 'uart\([0-9]\{1,\}\)'`
245 if [ -z "${unit}" ]; then
246 err 1 "Unable to get uart unit number: ${dev}"
249 ${hcseriald} -f /dev/cuau${unit} -n ${dev}
250 sleep 1 # wait a little bit
252 if [ ! -f "/var/run/hcseriald.${dev}.pid" ]; then
253 err 1 "Unable to start hcseriald on ${dev}"
257 # 3Com Bluetooth Adapter 3CRWB60-A
261 # Obtain unit number from device.
262 unit=`expr ${dev} : 'btccc\([0-9]\{1,\}\)'`
263 if [ -z "${unit}" ]; then
264 err 1 "Unable to get bt3c unit number: ${dev}"
268 # USB Bluetooth adapters
272 # Obtain unit number from device.
273 unit=`expr ${dev} : 'ubt\([0-9]\{1,\}\)'`
274 if [ -z "${unit}" ]; then
275 err 1 "Unable to get ubt unit number: ${dev}"
281 err 1 "Unsupported device: ${dev}"
285 # Be backward compatible and setup reasonable defaults
286 bluetooth_device_authentication_enable="0"
287 bluetooth_device_class="ff:01:0c"
288 bluetooth_device_connectable="1"
289 bluetooth_device_discoverable="1"
290 bluetooth_device_encryption_mode="0"
291 bluetooth_device_hci_debug_level="3"
292 bluetooth_device_l2cap_debug_level="3"
293 bluetooth_device_local_name="`/usr/bin/uname -n` (${dev})"
294 bluetooth_device_role_switch="1"
296 # Load default device configuration parameters
297 _file="/etc/defaults/bluetooth.device.conf"
299 if ! bluetooth_read_conf $_file bluetooth_device_ ; then
300 err 1 "Unable to read default Bluetooth configuration from $_file"
303 # Load device specific overrides
304 _file="/etc/bluetooth/$dev.conf"
306 if ! bluetooth_read_conf $_file bluetooth_device_ ; then
307 err 1 "Unable to read Bluetooth device configuration from $_file"
311 if ! bluetooth_setup_stack ${dev} ${hook} ; then
312 bluetooth_shutdown_stack $dev
313 err 1 "Unable to setup Bluetooth stack for device ${dev}"
319 ##############################################################################
321 ##############################################################################
327 # Try to figure out device type by looking at device name
329 # uartX - serial/UART Bluetooth device
331 if [ -f "/var/run/hcseriald.${dev}.pid" ]; then
332 kill `cat /var/run/hcseriald.${dev}.pid`
333 sleep 1 # wait a little bit
337 # 3Com Bluetooth Adapter 3CRWB60-A
341 # USB Bluetooth adapters
347 err 1 "Unsupported device: ${dev}"
351 bluetooth_shutdown_stack ${dev}
356 ##############################################################################
358 ##############################################################################
361 hccontrol="${bluetooth_hccontrol:-/usr/sbin/hccontrol}"
362 hcseriald="${bluetooth_hcseriald:-/usr/sbin/hcseriald}"