Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVG_$CHANNEL=$(( ($CURRENT_AVG + $CURRENT_VALUE + 1) / 2 )) --> that is not an average #5

Open
0xCAFEDECAF opened this issue Jun 14, 2023 · 1 comment

Comments

@0xCAFEDECAF
Copy link

https://github.com/PoweredLocal/openwrt-auto-channel-select/blame/675477958f5c34370969903c8b4bf3bfeaaf5ecf/setAutoChannel.sh#L63

That is not an average

@0xCAFEDECAF 0xCAFEDECAF changed the title eval "AVG_$CHANNEL=$(( ($CURRENT_AVG + $CURRENT_VALUE + 1) / 2 ))": that is not an average AVG_$CHANNEL=$(( ($CURRENT_AVG + $CURRENT_VALUE + 1) / 2 )) --> that is not an average Jun 14, 2023
@0xCAFEDECAF
Copy link
Author

0xCAFEDECAF commented Jun 14, 2023

Here is my version of this script:

#!/bin/ash

# Basic Wi-Fi auto channel selector by PoweredLocal
# (c) 2017 PoweredLocal, Melbourne, Australia
# https://www.poweredlocal.com
#
# Denis Mysenko 20/07/2017

# Config
TEST_INTERVAL=5
ITERATIONS=3
DEBUG=1

# No need to touch anything below, in most cases
if [[ -z $1 ]]; then
  echo "# Basic Wi-Fi auto channel selector by PoweredLocal"
  echo $0 [interface index]
  exit 1
fi

INTERFACE_INDEX=$1
SIGNAL=NO

scan() {
 /usr/sbin/iw wlan${INTERFACE_INDEX} scan | grep -E "primary channel|signal" | {
  while read line
  do
    FIRST=`echo "$line" | awk '{ print $1 }'`
    if [[ "$FIRST" == "signal:" ]]; then
      SIGNAL=`echo $line | awk '{ print ($2 < -60) ? "NO" : $2 }'`
    fi

    if [[ "$FIRST" == "*" && "$SIGNAL" != "NO" ]]; then
      CHANNEL=`echo "$line" | awk '{ print $4 }'`
      eval "CURRENT_VALUE=CHANNEL_$CHANNEL"
      eval "CURRENT_VALUE=$CURRENT_VALUE"
      SUM=$(( $CURRENT_VALUE + 1 ))
      eval "CHANNEL_${CHANNEL}=$SUM"
    fi
  done

  echo $CHANNEL_1 $CHANNEL_6 $CHANNEL_11
 }
}

for ITERATION in $(seq 1 1 $ITERATIONS)
do
  [[ -n "$DEBUG" ]] && echo Iteration $ITERATION
  RESULT=$(scan)
  eval "RESULT_${ITERATION}_1=`echo $RESULT | awk '{ print $1 }'`"
  eval "RESULT_${ITERATION}_6=`echo $RESULT | awk '{ print $2 }'`"
  eval "RESULT_${ITERATION}_11=`echo $RESULT | awk '{ print $3 }'`"
  [[ $ITERATION -lt $ITERATIONS ]] && sleep $TEST_INTERVAL
done

for ITERATION in $(seq 1 1 $ITERATIONS)
do
  for CHANNEL in 1 6 11
  do
    eval "CURRENT_VALUE=RESULT_${ITERATION}_${CHANNEL}"
    eval "SUM_$CHANNEL=$(( $SUM_$CHANNEL + $CURRENT_VALUE ))"

    if [[ $ITERATION -eq $ITERATIONS && -n "$DEBUG" ]]
    then
        eval "SUM=$(( SUM_${CHANNEL} ))"
        echo Channel $CHANNEL : seen $SUM / $ITERATIONS networks
    fi
  done
done

if [[ $SUM_1 -le $SUM_6 && $SUM_1 -le $SUM_11 ]]; then
  CHANNEL=1
elif [[ $SUM_6 -le $SUM_1 && $SUM_6 -le $SUM_11 ]]; then
  CHANNEL=6
elif [[ $SUM_11 -le $SUM_1 && $SUM_11 -le $SUM_6 ]]; then
  CHANNEL=11
fi

if [[ -n "$CHANNEL" ]]; then
  /sbin/uci set wireless.radio${INTERFACE_INDEX}.channel="$CHANNEL"
  /sbin/uci changes | grep -q ^wireless
  if [ $? -ne 0 ]
  then
    echo Keeping Wi-Fi channel \'$CHANNEL\'
  else
    echo Changing Wi-Fi to channel \'$CHANNEL\'
    /sbin/uci commit
    /sbin/wifi
  fi
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant