Skip to content

Commit

Permalink
openwrt: enable/disables radios via dhcp options (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarcasticadmin authored Feb 2, 2025
2 parents db3a7cb + df673cb commit 07a7032
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
25 changes: 20 additions & 5 deletions openwrt/files/etc/udhcpc.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
#!/bin/bash

# set case insensitive match. This only works if a comparison is made with
# double brackets
shopt -s nocasematch

case "$1" in
# Same actions for renew or bound for the time being
Expand All @@ -7,10 +11,21 @@ case "$1" in
set > /tmp/dhcp.params
radio0=`uci show 'wireless.radio0.channel'|cut -f 2 -d "'"`
radio1=`uci show 'wireless.radio1.channel'|cut -f 2 -d "'"`
if [ ! -z "$opt224" ] || [ ! -z "$opt225" ]; then
if [ "$opt224" != "$radio0" ] || [ "$opt225" != "$radio1" ]; then
uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224")
uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225")

if [[ ! -z "$opt224" ]] && [[ ! -z "$opt225" ]]; then
if [[ "$opt224" != "$radio0" ]] || [[ "$opt225" != "$radio1" ]]; then
if [[ "$opt224" != "off" ]]; then
uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224")
uci set 'wireless.radio0.disabled'=0
else
uci set 'wireless.radio0.disabled'=1
fi
if [[ "$opt225" != "off" ]]; then
uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225")
uci set 'wireless.radio1.disabled'=0
else
uci set 'wireless.radio1.disabled'=1
fi
uci commit
wifi reload
fi
Expand Down
25 changes: 20 additions & 5 deletions tests/unit/openwrt/golden/ath79/etc/udhcpc.user
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
#!/bin/bash

# set case insensitive match. This only works if a comparison is made with
# double brackets
shopt -s nocasematch

case "$1" in
# Same actions for renew or bound for the time being
Expand All @@ -7,10 +11,21 @@ case "$1" in
set > /tmp/dhcp.params
radio0=`uci show 'wireless.radio0.channel'|cut -f 2 -d "'"`
radio1=`uci show 'wireless.radio1.channel'|cut -f 2 -d "'"`
if [ ! -z "$opt224" ] || [ ! -z "$opt225" ]; then
if [ "$opt224" != "$radio0" ] || [ "$opt225" != "$radio1" ]; then
uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224")
uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225")

if [[ ! -z "$opt224" ]] && [[ ! -z "$opt225" ]]; then
if [[ "$opt224" != "$radio0" ]] || [[ "$opt225" != "$radio1" ]]; then
if [[ "$opt224" != "off" ]]; then
uci set 'wireless.radio0.channel'=$(printf %d "0x$opt224")
uci set 'wireless.radio0.disabled'=0
else
uci set 'wireless.radio0.disabled'=1
fi
if [[ "$opt225" != "off" ]]; then
uci set 'wireless.radio1.channel'=$(printf %d "0x$opt225")
uci set 'wireless.radio1.disabled'=0
else
uci set 'wireless.radio1.disabled'=1
fi
uci commit
wifi reload
fi
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/openwrt/test_udhcpc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
#
# Tests some scenarios of the udhcpc.user script. There is room for improvement
# here to mock out uci to return radio status as part of the test. There is
# also room to cover more of the udhcpcp.user script like setting the hostname
# and chaning the config version.
#
TOPDIR=$(git rev-parse --show-toplevel)
export PATH="$TOPDIR/tests/unit/openwrt:$PATH"

test_dhcp(){
testname=$1
echo "Running $testname"
export UCILOG="$testname"_uci.log
export opt224=$2 # radio0 channel | off
export opt225=$3 # radio1 channel | off

echo "" > $UCILOG
bash $TOPDIR/openwrt/files/etc/udhcpc.user renew

if ! diff "$testname"_uci.log "$testname"_expected.log; then
echo "FAILED: $testname"
exit 1
fi
}


cat > both_off_expected.log << EOF
show wireless.radio0.channel
show wireless.radio1.channel
set wireless.radio0.disabled=1
set wireless.radio1.disabled=1
commit
EOF
test_dhcp "both_off" off OFF

cat > enable_0_expected.log << EOF
show wireless.radio0.channel
show wireless.radio1.channel
set wireless.radio0.channel=3
set wireless.radio0.disabled=0
set wireless.radio1.disabled=1
commit
EOF
test_dhcp "enable_0" 3 OFF

cat > enable_1_expected.log << EOF
show wireless.radio0.channel
show wireless.radio1.channel
set wireless.radio0.channel=3
set wireless.radio0.disabled=0
set wireless.radio1.channel=4
set wireless.radio1.disabled=0
commit
EOF
test_dhcp "enable_1" 3 4

echo "PASS"
2 changes: 2 additions & 0 deletions tests/unit/openwrt/uci
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo $@ >> $UCILOG
Empty file added tests/unit/openwrt/wifi
Empty file.

0 comments on commit 07a7032

Please sign in to comment.