Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

#!/bin/sh
#
# $NetBSD: bluetooth,v 1.2 2021/11/26 12:51:24 sborrill Exp $
#

# PROVIDE: bluetooth
# REQUIRE: DAEMON
# BEFORE:  LOGIN

$_rc_subr_loaded . /etc/rc.subr

name="bluetooth"
rcvar=$name
start_cmd="bluetooth_start"
stop_cmd="bluetooth_stop"

btattach_cmd="/usr/sbin/btattach"
btconfig_cmd="/usr/sbin/btconfig"
bthcid_cmd="/usr/sbin/bthcid"
btdevctl_cmd="/usr/sbin/btdevctl"
sdpd_cmd="/usr/sbin/sdpd"

btattach_conf="/etc/bluetooth/btattach.conf"
btdevctl_conf="/etc/bluetooth/btdevctl.conf"

required_files="${btattach_conf} ${btdevctl_conf}"

bluetooth_start()
{
	#
	# attach Bluetooth serial controllers
	#
	while read type tty speed flags; do
		case ${type} in
		\#*|"")
			continue
			;;
		esac

		echo "attaching Bluetooth controller to $(basename ${tty})"
		${btattach_cmd} ${flags} ${type} ${tty} ${speed}
	done < ${btattach_conf}

	#
	# enable Bluetooth controllers.
	#
	# If ${btconfig_devices} is set, it is treated as a list of devices
	# to configure. Otherwise, all available devices will be configured
	#
	# For each device we are configuring, enable it with maximum security
	# settings (not discoverable, not connectable, auth and encryption
	# required for all connections), relaxed link policy settings and
	# the link master role, set a class of device for Computer, then apply
	# any options from the 'btconfig_<dev>' or 'btconfig_args' variables
	# on top of settings relaxing the security requirements, so that these
	# can be overridden (btconfig parses all command line options before
	# acting)
	#
	echo -n "configuring Bluetooth controllers:"
	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
		echo -n " ${dev}"
		eval args=\${btconfig_${dev}:-\${btconfig_args}}
		${btconfig_cmd} ${dev} enable -iscan -pscan auth encrypt
		${btconfig_cmd} ${dev} switch hold sniff park master
		${btconfig_cmd} ${dev} class 0x000100
		${btconfig_cmd} ${dev} iscan pscan -auth -encrypt ${args}
	done
	echo "."

	#
	# start Bluetooth Link Key/PIN Code manager
	#
	if checkyesno bthcid; then
		echo "starting Bluetooth Link Key/PIN Code manager"
		${bthcid_cmd} ${bthcid_flags}
	fi

	#
	# attach local Bluetooth service drivers
	#
	while read -r service addr dev junk; do
		case ${service} in
		\#*|"")
			continue
			;;
		esac

		if [ -z ${dev} -o ${junk} ]; then
			echo "${name}: invalid entry"
			return 1
		fi

		echo "attaching Bluetooth ${service} service from \"${addr}\""
		${btdevctl_cmd} -A -a ${addr} -d ${dev} -s ${service}
	done < ${btdevctl_conf}

	#
	# start Bluetooth Service Discovery server
	#
	if checkyesno sdpd; then
		echo "starting Bluetooth Service Discovery server"
		${sdpd_cmd} ${sdpd_flags}
	fi
}

bluetooth_stop()
{
	#
	# disable Bluetooth controllers, detaching local service drivers
	#
	echo -n "disabling Bluetooth controllers:"
	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
		echo -n " ${dev}"
		${btconfig_cmd} ${dev} disable
	done
	echo "."

	#
	# halt Service Discovery server, Link Key/PIN Code manager,
	# and detach Bluetooth serial controllers
	#
	p1=$(check_pidfile /var/run/bthcid.pid ${bthcid_cmd})
	p2=$(check_process ${sdpd_cmd})
	p3=$(check_process ${btattach_cmd})
	if [ -n "${p1}${p2}${p3}" ]; then
		for pid in ${p1} ${p2} ${p3}; do
			kill ${sig_stop} ${pid}
		done
		wait_for_pids ${p1} ${p2} ${p3}
	fi
}

load_rc_config ${name}
run_rc_command "$1"