From 4cad43f876b4f3f62b75989ce7f7680bfa696bed Mon Sep 17 00:00:00 2001 From: sMiik Date: Wed, 21 May 2025 01:25:03 +0300 Subject: [PATCH] Choose AP channel by the wifi device (#805) Choose AP channel by the wifi device Actually use fallback channel 1 as described by comment --- Dockerfile | 2 +- src/setup_apmode.sh | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d830286..ed5bee0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM python:3.9.18-slim-bullseye AS base -RUN apt-get -qq update && apt-get install -qy --no-install-recommends git hostapd rfkill dnsmasq build-essential libssl-dev iproute2 mosquitto +RUN apt-get -qq update && apt-get install -qy --no-install-recommends git hostapd rfkill dnsmasq build-essential libssl-dev iproute2 mosquitto iw FROM base AS python-deps diff --git a/src/setup_apmode.sh b/src/setup_apmode.sh index da92b88..2a9d046 100755 --- a/src/setup_apmode.sh +++ b/src/setup_apmode.sh @@ -4,6 +4,41 @@ GATEWAY=10.42.42.1 WLAN=${1:-UNKNOWN} VERBOSE_OUTPUT=${2:-"false"} +get_channel() { + iw dev "$1" info 2>/dev/null | awk '/channel/ {if ($2 ~ /^[0-9]+$/) print $2}' +} +get_wiphy() { + iw dev "$1" info | awk '/wiphy/ {print $2}' +} +get_ap_channel() { + local iface="$1" + local channel=$(get_channel "$iface" 2>/dev/null) + if [[ -n "$channel" ]]; then + # Found channel directly from the wanted interface info + echo "$channel" + return 0 + fi + + # Find the "parent" interface i.e. the one for which this is an additional virtual interface + # to get channel. + local wiphy=$(get_wiphy "$iface") || return 1 + + for other_iface in $(iw dev | awk '/Interface/ {print $2}'); do + [[ "$other_iface" == "$iface" ]] && continue + local other_wiphy=$(get_wiphy "$other_iface") + if [[ "$other_wiphy" == "$wiphy" ]]; then + channel=$(get_channel "$other_iface") + if [[ -n "$channel" ]]; then + echo "$channel" + return 0 + fi + fi + done + + echo "Unable to find channel for $iface" + return 1 +} + echo "Using WLAN adapter: ${WLAN}" ip addr flush dev $WLAN @@ -28,9 +63,17 @@ rfkill unblock wifi # 1. 802.11n in 2.4GHz (hw_mode=g) - some devices scan for it # 2. WPA2-PSK - some devices do not connect otherwise # 3. Enforced WPA2 - same as above +# 4. Channel parsed from wlan device info or 1 as fallback +AP_CHANNEL=$(get_ap_channel "$WLAN") +if [[ $? -eq 0 ]]; then + echo "Using hostapd channel $AP_CHANNEL parsed from wlan info." +else + AP_CHANNEL="1" + echo "Unable to get channel from wlan info. Using $AP_CHANNEL as fallback." +fi hostapd /dev/stdin -P $(pwd)/hostapd.pid -B <<- EOF ssid=cloudcutterflash -channel=1 +channel=$AP_CHANNEL logger_stdout_level=4 hw_mode=g wmm_enabled=1