Skip to content

Commit c0f153b

Browse files
authored
Merge pull request #466 from sudoAlphaX/fallback-wifi-channel
fix: attempt to use channel of $WIFI_IFACE if default channel fails
2 parents 4627e3c + 0f2424b commit c0f153b

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

src/scripts/create_ap

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ usage() {
3232
echo "Options:"
3333
echo " -h, --help Show this help"
3434
echo " --version Print version number"
35-
echo " -c <channel> Channel number (default: 1)"
35+
echo " -c <channel> Channel number (default: 1 or fallback to currently connected channel)"
3636
echo " -w <WPA version> Use 1 for WPA, use 2 for WPA2, use 1+2 for both (default: 2)"
3737
echo " -n Disable Internet sharing (if you use this, don't pass"
3838
echo " the <interface-with-internet> argument)"
@@ -1040,6 +1040,10 @@ write_config() {
10401040
PASSPHRASE="$4"
10411041
fi
10421042

1043+
if [[ $FREQ_BAND_SET -eq 0 ]]; then
1044+
FREQ_BAND="default"
1045+
fi
1046+
10431047
for config_opt in "${CONFIG_OPTS[@]}"; do
10441048
eval echo $config_opt=\$$config_opt
10451049
done >> "$STORE_CONFIG"
@@ -1068,7 +1072,7 @@ read_config() {
10681072
opt_name="${line%%=*}"
10691073
opt_val="${line#*=}"
10701074

1071-
if [[ $opt_name == "FREQ_BAND" ]] ; then
1075+
if [[ $opt_name == "FREQ_BAND" && $opt_val != "default" ]] ; then
10721076
FREQ_BAND_SET=1
10731077
fi
10741078

@@ -1397,19 +1401,25 @@ elif [[ $RUNNING_AS_DAEMON -eq 1 && -n "$DAEMON_PIDFILE" ]]; then
13971401
echo $$ >$DAEMON_PIDFILE
13981402
fi
13991403

1400-
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
1401-
echo "ERROR: Invalid frequency band" >&2
1402-
exit 1
1404+
if [[ $FREQ_BAND_SET != 0 ]]; then
1405+
if [[ $FREQ_BAND != 2.4 && $FREQ_BAND != 5 ]]; then
1406+
echo "ERROR: Invalid frequency band" >&2
1407+
exit 1
1408+
fi
14031409
fi
14041410

14051411
if [[ $CHANNEL == default ]]; then
1412+
USING_DEFAULT_CHANNEL=1
14061413
if [[ $FREQ_BAND == 2.4 ]]; then
14071414
CHANNEL=1
14081415
else
14091416
CHANNEL=36
14101417
fi
1418+
else
1419+
USING_DEFAULT_CHANNEL=0
14111420
fi
14121421

1422+
14131423
if [[ $FREQ_BAND != 5 && $CHANNEL -gt 14 ]]; then
14141424
echo "Channel number is greater than 14, assuming 5GHz frequency band"
14151425
FREQ_BAND=5
@@ -1691,7 +1701,19 @@ if [[ -n "$COUNTRY" && $USE_IWCONFIG -eq 0 ]]; then
16911701
iw reg set "$COUNTRY"
16921702
fi
16931703

1694-
can_transmit_to_channel ${WIFI_IFACE} ${CHANNEL} || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz."
1704+
# Fallback to currently connected channel if the adapter can not transmit to the default channel (1)
1705+
if can_transmit_to_channel "${WIFI_IFACE}" "${CHANNEL}"; then
1706+
echo "Transmitting to channel ${CHANNEL}..."
1707+
else
1708+
if [[ $USING_DEFAULT_CHANNEL -eq 1 && $WIFI_IFACE_CHANNEL -ne $CHANNEL ]]; then
1709+
echo -e "Your adapter can not transmit to channel ${CHANNEL}" >&2
1710+
CHANNEL=$WIFI_IFACE_CHANNEL
1711+
echo -e "Falling back to channel ${CHANNEL}"
1712+
can_transmit_to_channel "${WIFI_IFACE}" "${CHANNEL}" || die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz."
1713+
else
1714+
die "Your adapter can not transmit to channel ${CHANNEL}, frequency band ${FREQ_BAND}GHz."
1715+
fi
1716+
fi
16951717

16961718
if networkmanager_exists && ! networkmanager_iface_is_unmanaged ${WIFI_IFACE}; then
16971719
echo -n "Network Manager found, set ${WIFI_IFACE} as unmanaged device... "

0 commit comments

Comments
 (0)