Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 16 Oct 2005 02:44:32 +0300
From:      Panagiotis Astithas <past@ebs.gr>
To:        freebsd-bluetooth@freebsd.org
Subject:   [RFC] rc.d integration for the bluetooth subsystem
Message-ID:  <43519460.1090605@ebs.gr>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------050801090108070509050005
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi all,

I've been playing with integrating the bluetooth subsystem into our rc.d 
  infrastructure and I'd like to submit the results of my efforts for 
review. My testing has been centered around my USB bluetooth dongle and 
I know that it works, but I suppose other bluetooth devices should work 
as well. I have taken the existing rc.bluetooth script (that is not 
installed by default) and converted it to rc.d, renaming it as 
'bluetooth'. I also added a couple of other scripts for hcsecd and sdpd, 
that are started from bluetooth, but can also function independently, id 
so desired. Finally I have added a devd configuration file that makes 
sure the bluetooth script gets started on insertion and removal of the 
USB dongle.

With these changes, when I plug in my USB bluetooth dongle, all the 
necessary initialization happens behind the scenes and I can start using 
my bluetooth peripherals right away. When I unplug the dongle all the 
services stop and the necessary shutdown operations are performed on the 
bluetooth stack. There is still some work left, like specifying 
different flags for sdpd & hcsecd, but the defaults work fine.

In order to test this stuff you have to:

- copy bluetooth, hcsecd and sdpd into /etc/rc.d
- copy ubt.conf into /usr/local/etc/devd (creating it if necessary), or 
add its contents to /etc/devd.conf
- add a line in /etc/rc.conf with:
   bluetooth_enable="YES"

In an eventual merge into the base system the devd configuration should 
be merged into /etc/devd.conf and /etc/defaults/rc.conf should contain 
the following line instead:
   bluetooth_enable="NO"

I'd appreciate any comments you may have.


Cheers,

Panagiotis

--------------050801090108070509050005
Content-Type: text/plain;
 name="bluetooth"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="bluetooth"

#!/bin/sh

# PROVIDE: bluetooth
# REQUIRE: DAEMON
# KEYWORD: nojail nostart shutdown

# Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id$
# $FreeBSD$
#

. /etc/rc.subr

name="bluetooth"
rcvar=`set_rcvar`
start_cmd="bluetooth_start"
stop_cmd="bluetooth_stop"

logger="/usr/bin/logger -i -s -p user.err"
kldstat="/sbin/kldstat"
kldload="/sbin/kldload"
sysctl="/sbin/sysctl"
ngctl="/usr/sbin/ngctl"
hcseriald="/usr/sbin/hcseriald"
hccontrol="/usr/sbin/hccontrol"
hci_debug_level=3
l2cap_debug_level=3

##############################################################################
# Display usage info
##############################################################################

usage()
{
	script=$1
	shift

	echo "Usage:"
	echo "  ${script} start device"
	echo "or"
	echo "  ${script} stop device"
} # usage

##############################################################################
# Verify and load if required kernel module
##############################################################################

load_module()
{
	module=$1
	shift

	${kldstat} -n ${module} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		${kldload} ${module} > /dev/null 2>&1
		if [ $? -ne 0 ]; then
			${logger} "Failed to load ${module}"
			exit 1
		fi
	fi
} # load_module

##############################################################################
# Setup Bluetooth stack. Create and connect nodes
##############################################################################

setup_stack()
{
	hook="$2"

	# Setup HCI 
	${ngctl} mkpeer ${dev}: hci ${hook} drv              || exit 1
	${ngctl} name ${dev}:${hook} ${dev}hci               || exit 1
	${ngctl} msg ${dev}hci: set_debug ${hci_debug_level} || exit 1

	# Setup L2CAP
	${ngctl} mkpeer ${dev}hci: l2cap acl hci                 || exit 1
	${ngctl} name ${dev}hci:acl ${dev}l2cap                  || exit 1
	${ngctl} msg ${dev}l2cap: set_debug ${l2cap_debug_level} || exit 1

	# Connect HCI node to the Bluetooth sockets layer
	${ngctl} connect ${dev}hci: btsock_hci_raw: raw ${dev}raw || exit 1

	# Connect L2CAP node to Bluetooth sockets layer
	${ngctl} connect ${dev}l2cap: btsock_l2c_raw: ctl ${dev}ctl || exit 1
	${ngctl} connect ${dev}l2cap: btsock_l2c: l2c ${dev}l2c     || exit 1

	# Initilalize HCI node
	${hccontrol} -n ${dev}hci reset                          || exit 1
#	${hccontrol} -n ${dev}hci read_local_name                || exit 1
	${hccontrol} -n ${dev}hci read_bd_addr                   || exit 1
	${hccontrol} -n ${dev}hci read_local_supported_features  || exit 1
	${hccontrol} -n ${dev}hci read_buffer_size               || exit 1
	${hccontrol} -n ${dev}hci write_scan_enable 3            || exit 1
#	${hccontrol} -n ${dev}hci read_scan_enable               || exit 1

	# We need to write class of device here because of Service Class
	# Filtering. For more info on Class_Of_Device field see
	# http://www.bluetooth.org/assigned-numbers/baseband.htm

	${hccontrol} -n ${dev}hci write_class_of_device ff:01:0c || exit 1

	# Enable this if you have trouble with accepting incomming connections.
	# By default we will try to perform role switch and become Master on
	# incomming connection (if local device supports it). Some devices
	# do not support role switch and thus incomming connection from such
	# devices will fail.

	${hccontrol} -n ${dev}hci write_node_role_switch 0       || exit 1

	${hccontrol} -n ${dev}hci change_local_name "`uname -n` (${dev})" || exit 1
	${hccontrol} -n ${dev}hci initialize                     || exit 1
} # setup_stack

##############################################################################
# Shutdown Bluetooth stack. Destroy all nodes
##############################################################################

shutdown_stack()
{
	${ngctl} shutdown ${dev}hci: > /dev/null 2>&1
	${ngctl} shutdown ${dev}l2cap: > /dev/null 2>&1
	echo -n ""
} # shutdown_stack


bluetooth_start()
{

	# Load required kernel modules
	load_module	ng_bluetooth
	load_module	ng_hci
	load_module	ng_l2cap
	load_module	ng_btsocket

	$0 stop ${dev} || exit 1

	# Try to figure out device type by looking at device name
	case ${dev} in
		# sioX - serial/UART Bluetooth device
		sio*)
			hook="hook"

			load_module	ng_h4

			# Obtain unit number from device.
			unit=`expr ${dev} : 'sio\([0-9]\{1,\}\)'`
			if [ -z ${unit} ]; then
				${logger} "Unable to get sio unit number: ${dev}"
				exit 1
			fi

			${hcseriald} -f /dev/cuad${unit} -n ${dev}
			sleep 1 # wait a little bit

			if [ ! -f "/var/run/hcseriald.${dev}.pid" ]; then
				${logger} "Failed to setup ${dev}"
				exit 1
			fi
			;;

		# 3Com Bluetooth Adapter 3CRWB60-A
		btccc*)
			hook="hook"

			# Obtain unit number from device.
			unit=`expr ${dev} : 'btccc\([0-9]\{1,\}\)'`
			if [ -z ${unit} ]; then
				${logger} "Unable to get bt3c unit number: ${dev}"
				exit 1
			fi
			;;

		# USB Bluetooth adapters
		ubt*)
			hook="hook"

			# Obtain unit number from device.
			unit=`expr ${dev} : 'ubt\([0-9]\{1,\}\)'`
			if [ -z ${unit} ]; then
				${logger} "Unable to get ubt unit number: ${dev}"
				exit 1
			fi
			;;

		# Unknown
		*)
			${logger} "Unsupported device: ${dev}"
			exit 1
			;;
	esac

#	${sysctl} net.bluetooth.hci.sockets.raw.debug_level=9
#	${sysctl} net.bluetooth.l2cap.sockets.raw.debug_level=9
#	${sysctl} net.bluetooth.l2cap.sockets.seq.debug_level=9
#	${sysctl} net.bluetooth.rfcomm.sockets.stream.debug_level=9

	setup_stack	${dev} ${hook}
	/etc/rc.d/hcsecd start
	/etc/rc.d/sdpd start
}

bluetooth_stop()
{
	# Try to figure out device type by looking at device name
	case ${dev} in
		# sioX - serial/UART Bluetooth device
		sio*)
			if [ -f "/var/run/hcseriald.${dev}.pid" ]; then
				kill `cat /var/run/hcseriald.${dev}.pid`
				sleep 1 # wait a little bit
			fi
			;;

		# 3Com Bluetooth Adapter 3CRWB60-A
		btccc*)
			;;

		# USB Bluetooth adapters
		ubt*)
			;;

		# Unknown
		*)
			${logger} "Unsupported device: ${dev}"
			exit 1
			;;
	esac

	/etc/rc.d/hcsecd stop
	/etc/rc.d/sdpd stop
	shutdown_stack	${dev}
}

dev="$2"

load_rc_config $name
run_rc_command "$1"

--------------050801090108070509050005
Content-Type: text/plain;
 name="hcsecd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="hcsecd"

#!/bin/sh

# PROVIDE: hcsecd
# REQUIRE: bluetooth
# BEFORE:  sdpd
# KEYWORD: nojail nostart

. /etc/rc.subr

name="hcsecd"
command="/usr/sbin/${name}"

load_rc_config $name
run_rc_command "$1"

--------------050801090108070509050005
Content-Type: text/plain;
 name="sdpd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="sdpd"

#!/bin/sh

# PROVIDE: sdpd
# REQUIRE: hcsecd
# KEYWORD: nojail nostart

. /etc/rc.subr

name="sdpd"
command="/usr/sbin/${name}"

load_rc_config $name
run_rc_command "$1"

--------------050801090108070509050005
Content-Type: text/plain;
 name="ubt.conf"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ubt.conf"

# When a USB Bluetooth dongle appears, start the bluetooth subsystem.
attach 100 {
	device-name "ubt[0-9]+";
	action "/etc/rc.d/bluetooth start $device-name";
};
detach 100 {
	device-name "ubt[0-9]+";
	action "/etc/rc.d/bluetooth stop $device-name";
};

--------------050801090108070509050005--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?43519460.1090605>