Skip to content
Snippets Groups Projects

udev: split wifi and bt rules into two separate files, add systemd unit file, and add BT mac retry

Merged Clayton Craft requested to merge craftyguy/split-udev into master
Files
4
+ 46
7
@@ -16,9 +16,18 @@ WLAN_INTERFACE="wlan0"
BT_INTERFACE="hci0"
# Default MAC prefix
MAC_PREFIX="0200"
BT_TIMEOUT=${BT_TIMEOUT:-5} # seconds
WLAN_TIMEOUT=${WLAN_TIMEOUT:-5} # seconds
USE_LOGGER=true
[ -n "$SYSTEMD_EXEC_PID" ] && USE_LOGGER=false
log() {
echo "$@" | logger -t "bootmac"
if "$USE_LOGGER"; then
echo "$@" | logger -t "bootmac"
else
echo "$@"
fi
}
help_info() {
@@ -141,9 +150,6 @@ mac_generate() {
}
mac_bluetooth() {
# Some Bluetooth adapters take time before they can be configured
sleep 1
log "setting Bluetooth MAC to $BT_MAC"
# Check if the Bluetooth interface is up
@@ -162,7 +168,24 @@ mac_bluetooth() {
# The 'yes | btmgmt xxx' workaround is needed for proper working of btmgmt
# as noted in https://gitlab.com/postmarketOS/bootmac/-/issues/3
yes | btmgmt -i "$BT_INTERFACE" power off
yes | btmgmt -i "$BT_INTERFACE" public-addr "$BT_MAC"
# Setting the mac addr might fail if the device/driver hasn't finished
# initializing, so retry
timeout=0
while [ $timeout -lt "$BT_TIMEOUT" ]; do
if yes | btmgmt -i "$BT_INTERFACE" public-addr "$BT_MAC"; then
break
fi
log "Unable to set Bluetooth MAC, retrying after 1 second..."
sleep 1
timeout=$((timeout + 1))
done
if [ $timeout -ge "$BT_TIMEOUT" ]; then
log "Failed to set Bluetooth MAC address, command timed out after $BT_TIMEOUT seconds"
return 1
fi
if [ -n "$BT_RFKILL_UNBLOCKED" ]; then
log "restoring Bluetooth rfkill state to unblocked"
rfkill unblock bluetooth
@@ -180,8 +203,24 @@ mac_wlan() {
set -e
# Bring WLAN down, set the extracted MAC and bring it up again
ip link set dev "$WLAN_INTERFACE" down
ip link set dev "$WLAN_INTERFACE" address "$WLAN_MAC"
# Setting the mac addr might fail if the device/driver hasn't finished
# initializing, so retry
timeout=0
while [ $timeout -lt "$WLAN_TIMEOUT" ]; do
if ip link set dev "$WLAN_INTERFACE" down \
&& ip link set dev "$WLAN_INTERFACE" address "$WLAN_MAC" ; then
break
fi
log "Unable to set wlan MAC, retrying after 1 second..."
sleep 1
timeout=$((timeout + 1))
done
if [ $timeout -ge "$WLAN_TIMEOUT" ]; then
log "Failed to set wlan MAC address, command timed out after $WLAN_TIMEOUT seconds"
return 1
fi
if [ -n "$WLAN_RFKILL_UNBLOCKED" ]; then
log "restoring WLAN rfkill state to unblocked"
rfkill unblock wlan
Loading