1#!/bin/sh
2#
3# $NetBSD: bluetooth,v 1.3 2023/12/25 08:23:35 kre Exp $
4#
5
6# PROVIDE: bluetooth
7# REQUIRE: DAEMON
8# BEFORE:  LOGIN
9
10$_rc_subr_loaded . /etc/rc.subr
11
12name="bluetooth"
13rcvar=$name
14start_cmd="bluetooth_start"
15stop_cmd="bluetooth_stop"
16
17btattach_cmd="/usr/sbin/btattach"
18btconfig_cmd="/usr/sbin/btconfig"
19bthcid_cmd="/usr/sbin/bthcid"
20btdevctl_cmd="/usr/sbin/btdevctl"
21sdpd_cmd="/usr/sbin/sdpd"
22
23btattach_conf="/etc/bluetooth/btattach.conf"
24btdevctl_conf="/etc/bluetooth/btdevctl.conf"
25
26required_files="${btattach_conf} ${btdevctl_conf}"
27
28bluetooth_start()
29{
30	#
31	# attach Bluetooth serial controllers
32	#
33	while read type tty speed flags; do
34		case ${type} in
35		\#*|"")
36			continue
37			;;
38		esac
39
40		echo "attaching Bluetooth controller to $(basename ${tty})"
41		${btattach_cmd} ${flags} ${type} ${tty} ${speed}
42	done < ${btattach_conf}
43
44	#
45	# enable Bluetooth controllers.
46	#
47	# If ${btconfig_devices} is set, it is treated as a list of devices
48	# to configure. Otherwise, all available devices will be configured
49	#
50	# For each device we are configuring, enable it with maximum security
51	# settings (not discoverable, not connectable, auth and encryption
52	# required for all connections), relaxed link policy settings and
53	# the link master role, set a class of device for Computer, then apply
54	# any options from the 'btconfig_<dev>' or 'btconfig_args' variables
55	# on top of settings relaxing the security requirements, so that these
56	# can be overridden (btconfig parses all command line options before
57	# acting)
58	#
59	echo -n "configuring Bluetooth controllers:"
60	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
61		echo -n " ${dev}"
62		eval args=\${btconfig_${dev}:-\${btconfig_args}}
63		${btconfig_cmd} ${dev} enable -iscan -pscan auth encrypt
64		${btconfig_cmd} ${dev} switch hold sniff park master
65		${btconfig_cmd} ${dev} class 0x000100
66		${btconfig_cmd} ${dev} iscan pscan -auth -encrypt ${args}
67	done
68	echo "."
69
70	#
71	# start Bluetooth Link Key/PIN Code manager
72	#
73	if checkyesno bthcid; then
74		echo "starting Bluetooth Link Key/PIN Code manager"
75		${bthcid_cmd} ${bthcid_flags}
76	fi
77
78	#
79	# attach local Bluetooth service drivers
80	#
81	while read -r service addr dev junk; do
82		case ${service} in
83		\#*|"")
84			continue
85			;;
86		esac
87
88		if [ -z "${dev}" ] || [ -n "${junk}" ]; then
89			echo "${name}: invalid entry"
90			return 1
91		fi
92
93		echo "attaching Bluetooth ${service} service from \"${addr}\""
94		${btdevctl_cmd} -A -a "${addr}" -d "${dev}" -s "${service}"
95	done < ${btdevctl_conf}
96
97	#
98	# start Bluetooth Service Discovery server
99	#
100	if checkyesno sdpd; then
101		echo "starting Bluetooth Service Discovery server"
102		${sdpd_cmd} ${sdpd_flags}
103	fi
104}
105
106bluetooth_stop()
107{
108	#
109	# disable Bluetooth controllers, detaching local service drivers
110	#
111	echo -n "disabling Bluetooth controllers:"
112	for dev in ${btconfig_devices:-$(${btconfig_cmd} -l)}; do
113		echo -n " ${dev}"
114		${btconfig_cmd} "${dev}" disable
115	done
116	echo "."
117
118	#
119	# halt Service Discovery server, Link Key/PIN Code manager,
120	# and detach Bluetooth serial controllers
121	#
122	p1=$(check_pidfile /var/run/bthcid.pid ${bthcid_cmd})
123	p2=$(check_process ${sdpd_cmd})
124	p3=$(check_process ${btattach_cmd})
125	if [ -n "${p1}${p2}${p3}" ]; then
126		for pid in ${p1} ${p2} ${p3}; do
127			kill ${sig_stop} ${pid}
128		done
129		wait_for_pids ${p1} ${p2} ${p3}
130	fi
131}
132
133load_rc_config ${name}
134run_rc_command "$1"
135