bluetooth revision 1.2
11.1Splunky#!/bin/sh
21.1Splunky#
31.2Ssborrill# $NetBSD: bluetooth,v 1.2 2021/11/26 12:51:24 sborrill Exp $
41.1Splunky#
51.1Splunky
61.1Splunky# PROVIDE: bluetooth
71.1Splunky# REQUIRE: DAEMON
81.1Splunky# BEFORE:  LOGIN
91.1Splunky
101.1Splunky$_rc_subr_loaded . /etc/rc.subr
111.1Splunky
121.1Splunkyname="bluetooth"
131.2Ssborrillrcvar=$name
141.1Splunkystart_cmd="bluetooth_start"
151.1Splunkystop_cmd="bluetooth_stop"
161.1Splunky
171.1Splunkybtattach_cmd="/usr/sbin/btattach"
181.1Splunkybtconfig_cmd="/usr/sbin/btconfig"
191.1Splunkybthcid_cmd="/usr/sbin/bthcid"
201.1Splunkybtdevctl_cmd="/usr/sbin/btdevctl"
211.1Splunkysdpd_cmd="/usr/sbin/sdpd"
221.1Splunky
231.1Splunkybtattach_conf="/etc/bluetooth/btattach.conf"
241.1Splunkybtdevctl_conf="/etc/bluetooth/btdevctl.conf"
251.1Splunky
261.1Splunkyrequired_files="${btattach_conf} ${btdevctl_conf}"
271.1Splunky
281.1Splunkybluetooth_start()
291.1Splunky{
301.1Splunky	#
311.1Splunky	# attach Bluetooth serial controllers
321.1Splunky	#
331.1Splunky	while read type tty speed flags; do
341.1Splunky		case ${type} in
351.1Splunky		\#*|"")
361.1Splunky			continue
371.1Splunky			;;
381.1Splunky		esac
391.1Splunky
401.1Splunky		echo "attaching Bluetooth controller to $(basename ${tty})"
411.1Splunky		${btattach_cmd} ${flags} ${type} ${tty} ${speed}
421.1Splunky	done < ${btattach_conf}
431.1Splunky
441.1Splunky	#
451.1Splunky	# enable Bluetooth controllers.
461.1Splunky	#
471.1Splunky	# If ${btconfig_devices} is set, it is treated as a list of devices
481.1Splunky	# to configure. Otherwise, all available devices will be configured
491.1Splunky	#
501.1Splunky	# For each device we are configuring, enable it with maximum security
511.1Splunky	# settings (not discoverable, not connectable, auth and encryption
521.1Splunky	# required for all connections), relaxed link policy settings and
531.1Splunky	# the link master role, set a class of device for Computer, then apply
541.1Splunky	# any options from the 'btconfig_<dev>' or 'btconfig_args' variables
551.1Splunky	# on top of settings relaxing the security requirements, so that these
561.1Splunky	# can be overridden (btconfig parses all command line options before
571.1Splunky	# acting)
581.1Splunky	#
591.1Splunky	echo -n "configuring Bluetooth controllers:"
601.1Splunky	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
611.1Splunky		echo -n " ${dev}"
621.1Splunky		eval args=\${btconfig_${dev}:-\${btconfig_args}}
631.1Splunky		${btconfig_cmd} ${dev} enable -iscan -pscan auth encrypt
641.1Splunky		${btconfig_cmd} ${dev} switch hold sniff park master
651.1Splunky		${btconfig_cmd} ${dev} class 0x000100
661.1Splunky		${btconfig_cmd} ${dev} iscan pscan -auth -encrypt ${args}
671.1Splunky	done
681.1Splunky	echo "."
691.1Splunky
701.1Splunky	#
711.1Splunky	# start Bluetooth Link Key/PIN Code manager
721.1Splunky	#
731.1Splunky	if checkyesno bthcid; then
741.1Splunky		echo "starting Bluetooth Link Key/PIN Code manager"
751.1Splunky		${bthcid_cmd} ${bthcid_flags}
761.1Splunky	fi
771.1Splunky
781.1Splunky	#
791.1Splunky	# attach local Bluetooth service drivers
801.1Splunky	#
811.1Splunky	while read -r service addr dev junk; do
821.1Splunky		case ${service} in
831.1Splunky		\#*|"")
841.1Splunky			continue
851.1Splunky			;;
861.1Splunky		esac
871.1Splunky
881.1Splunky		if [ -z ${dev} -o ${junk} ]; then
891.1Splunky			echo "${name}: invalid entry"
901.1Splunky			return 1
911.1Splunky		fi
921.1Splunky
931.1Splunky		echo "attaching Bluetooth ${service} service from \"${addr}\""
941.1Splunky		${btdevctl_cmd} -A -a ${addr} -d ${dev} -s ${service}
951.1Splunky	done < ${btdevctl_conf}
961.1Splunky
971.1Splunky	#
981.1Splunky	# start Bluetooth Service Discovery server
991.1Splunky	#
1001.1Splunky	if checkyesno sdpd; then
1011.1Splunky		echo "starting Bluetooth Service Discovery server"
1021.1Splunky		${sdpd_cmd} ${sdpd_flags}
1031.1Splunky	fi
1041.1Splunky}
1051.1Splunky
1061.1Splunkybluetooth_stop()
1071.1Splunky{
1081.1Splunky	#
1091.1Splunky	# disable Bluetooth controllers, detaching local service drivers
1101.1Splunky	#
1111.1Splunky	echo -n "disabling Bluetooth controllers:"
1121.1Splunky	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
1131.1Splunky		echo -n " ${dev}"
1141.1Splunky		${btconfig_cmd} ${dev} disable
1151.1Splunky	done
1161.1Splunky	echo "."
1171.1Splunky
1181.1Splunky	#
1191.1Splunky	# halt Service Discovery server, Link Key/PIN Code manager,
1201.1Splunky	# and detach Bluetooth serial controllers
1211.1Splunky	#
1221.1Splunky	p1=$(check_pidfile /var/run/bthcid.pid ${bthcid_cmd})
1231.1Splunky	p2=$(check_process ${sdpd_cmd})
1241.1Splunky	p3=$(check_process ${btattach_cmd})
1251.1Splunky	if [ -n "${p1}${p2}${p3}" ]; then
1261.1Splunky		for pid in ${p1} ${p2} ${p3}; do
1271.1Splunky			kill ${sig_stop} ${pid}
1281.1Splunky		done
1291.1Splunky		wait_for_pids ${p1} ${p2} ${p3}
1301.1Splunky	fi
1311.1Splunky}
1321.1Splunky
1331.1Splunkyload_rc_config ${name}
1341.1Splunkyrun_rc_command "$1"
135