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