mirror of
https://dev.iopsys.eu/feed/iopsys.git
synced 2025-12-10 07:44:50 +01:00
288 lines
6.6 KiB
Bash
Executable file
288 lines
6.6 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
. /usr/share/libubox/jshn.sh
|
|
. /lib/functions.sh
|
|
|
|
state="exit"
|
|
TMPPATH="/tmp/netmode_config_backup"
|
|
CURMODE=""
|
|
SLEEPTIME=300
|
|
|
|
set_wireless_values() {
|
|
local iface_num="$1"
|
|
local ssid="$2"
|
|
local key="$3"
|
|
local encryption="$4"
|
|
local device="$5"
|
|
local old_ssid old_key old_encryption
|
|
if ! uci -q get wireless.@wifi-iface[$iface_num] >/dev/null 2>&1; then
|
|
state="reload"
|
|
uci add wireless wifi-iface >/dev/null 2>&1
|
|
uci set wireless.@wifi-iface[$iface_num].device="$device"
|
|
fi
|
|
local network="$(uci -q get wireless.@wifi-iface[$iface_num].network)"
|
|
old_ssid="$(uci -q get wireless.@wifi-iface[$iface_num].ssid)"
|
|
old_key="$(uci -q get wireless.@wifi-iface[$iface_num].key)"
|
|
old_encryption="$(uci -q get wireless.@wifi-iface[$iface_num].encryption)"
|
|
if [ "$old_ssid" != "$ssid" -o "$old_encryption" != "$encryption" -o "$old_key" != "$key" -o -z "$network" ]; then
|
|
#TODO: get network dynamicaly
|
|
[ "$state" == "exit" ] && state="apply"
|
|
[ -z "$network" ] && network="wan"
|
|
uci set wireless.@wifi-iface[$iface_num].network="$network"
|
|
uci set wireless.@wifi-iface[$iface_num].ssid="$ssid"
|
|
uci set wireless.@wifi-iface[$iface_num].key="$key"
|
|
uci set wireless.@wifi-iface[$iface_num].encryption="$encryption"
|
|
fi
|
|
}
|
|
|
|
get_wifi_device_from_band(){
|
|
local section="$1"
|
|
local band="$2"
|
|
local __ret="$3"
|
|
local b
|
|
config_get b $section "band"
|
|
if [ "$band" == "$b" ]; then
|
|
eval "export -- \"$__ret=$section\""
|
|
fi
|
|
}
|
|
|
|
get_device(){
|
|
local band="$1"
|
|
config_foreach get_wifi_device_from_band "wifi-device" "$band" "$2"
|
|
}
|
|
|
|
get_iface_num() {
|
|
local device="$1"
|
|
local prev="$2"
|
|
local __save="$3"
|
|
local i=0
|
|
local dev
|
|
while true; do
|
|
[ $i -gt 100 ] && break ## just a safty messure
|
|
if ! uci -q get wireless.@wifi-iface[$i] >/dev/null; then
|
|
break
|
|
fi
|
|
mode="$(uci -q get wireless.@wifi-iface[$i].mode)"
|
|
if [ "$mode" == "wet" -o "$mode" == "sta" ]; then
|
|
i=$((i+1))
|
|
continue
|
|
fi
|
|
dev="$(uci -q get wireless.@wifi-iface[$i].device)"
|
|
if [ "$dev" != "$device" ]; then
|
|
[ -z "$dev" ] && break
|
|
i=$((i+1))
|
|
elif [ $prev -ne 0 ]; then
|
|
i=$((i+1))
|
|
prev=$((prev-1))
|
|
else
|
|
break
|
|
fi
|
|
done
|
|
eval "export -- \"$__save=$i\""
|
|
}
|
|
|
|
restore() {
|
|
if [ "$1" == "back" ]; then
|
|
uci set juci.juci.homepage='netmode-wizard'
|
|
uci commit juci
|
|
cp $TMPPATH/* /etc/config/
|
|
uci set netmode.setup.curmode=$CURMODE
|
|
uci commit netmode
|
|
ubus call network reload
|
|
wifi reload
|
|
ubus call router.network reload
|
|
fi
|
|
rm -r $TMPPATH
|
|
ubus call leds set '{"state":"normal"}'
|
|
}
|
|
|
|
backup() {
|
|
mkdir -p $TMPPATH
|
|
cp /etc/config/* $TMPPATH/
|
|
CURMODE="$(uci -q get netmode.setup.curmode)"
|
|
ubus call leds set '{"state":"allflash"}'
|
|
}
|
|
|
|
###### START HERE #######
|
|
|
|
file="$1"
|
|
from_gui="$2"
|
|
|
|
[ -f "$file" ] || exit
|
|
|
|
# let netmode-handler up to 20 seconds to finish switching mode
|
|
for tm in 2 4 6 8; do
|
|
if [ ! -f /tmp/switching_mode ]; then
|
|
break
|
|
fi
|
|
sleep $tm
|
|
done
|
|
|
|
[ "$from_gui" == "true" ] && backup
|
|
|
|
json_load "`cat $file`"
|
|
json_select "wifi_ifaces"
|
|
|
|
local curmode repmode
|
|
config_load netmode
|
|
config_get curmode setup curmode
|
|
|
|
case $curmode in
|
|
*repeater*)
|
|
;;
|
|
*)
|
|
uci set netmode.setup.curmode="repeater"
|
|
uci commit netmode
|
|
/etc/init.d/netmode reload
|
|
curmode="$(uci -q get netmode.setup.curmode)"
|
|
;;
|
|
esac
|
|
|
|
local apcliband="a"
|
|
case $curmode in
|
|
*_2g_*) apcliband="b";;
|
|
esac
|
|
|
|
i=1
|
|
|
|
local dummy band ssid key encryption device iface_num
|
|
|
|
while json_get_var dummy $i; do
|
|
json_select $i
|
|
json_get_var band band
|
|
[ "$band" == "" -o "$apcliband" == "$band" ] && break
|
|
json_select ..
|
|
i=$((i+1))
|
|
done
|
|
|
|
[ "$apcliband" == "$band" -o "$band" == "" ] || {
|
|
[ "$from_gui" == "true" ] && restore "back"
|
|
exit
|
|
}
|
|
|
|
repeater_iface_num=$(uci -q show wireless | grep -e ".mode='wet'" -e ".mode='sta'" | sed 's/.*\[\([0-9]\)\].*/\1/')
|
|
|
|
[ -z "$repeater_iface_num" ] && {
|
|
[ "$from_gui" == "true" ] && restore "back"
|
|
exit
|
|
}
|
|
|
|
json_get_var ssid ssid
|
|
json_get_var key key
|
|
json_get_var encryption encryption
|
|
|
|
set_wireless_values "$repeater_iface_num" "$ssid" "$key" "$encryption"
|
|
|
|
json_load "`cat $file`"
|
|
json_select "wifi_ifaces"
|
|
|
|
config_load wireless
|
|
local b_num=0
|
|
local a_num=0
|
|
|
|
i=1
|
|
while json_get_var dummy $i; do
|
|
json_select $i
|
|
json_get_var band band
|
|
json_get_var ssid ssid
|
|
json_get_var encryption encryption
|
|
json_get_var key key
|
|
if [ "$band" == "" ]; then
|
|
get_device "a" device
|
|
get_iface_num "$device" "$a_num" iface_num
|
|
a_num=$((a_num+1))
|
|
[ -z $iface_num ] && {
|
|
[ "$from_gui" == "true" ] && restore "back"
|
|
exit
|
|
}
|
|
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
|
|
get_device "b" device
|
|
get_iface_num "$device" "$b_num" iface_num
|
|
b_num=$((b_num+1))
|
|
[ -z $iface_num ] && {
|
|
[ "$from_gui" == "true" ] && restore "back"
|
|
exit
|
|
}
|
|
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
|
|
else
|
|
get_device "$band" device
|
|
case $band in
|
|
a)
|
|
get_iface_num "$device" "$a_num" iface_num
|
|
a_num=$((a_num+1))
|
|
;;
|
|
b)
|
|
get_iface_num "$device" "$b_num" iface_num
|
|
b_num=$((b_num+1))
|
|
;;
|
|
esac
|
|
[ -z $iface_num ] && {
|
|
[ "$from_gui" == "true" ] && restore "back"
|
|
exit
|
|
}
|
|
set_wireless_values "$iface_num" "$ssid" "$key" "$encryption" "$device"
|
|
fi
|
|
i=$((i+1))
|
|
json_select ..
|
|
done
|
|
|
|
i=$((b_num + a_num + 1))
|
|
|
|
if [ $repeater_iface_num -gt $i ]; then
|
|
i=$((i-1))
|
|
fi
|
|
|
|
while uci -q get wireless.@wifi-iface[$i] >/dev/null; do
|
|
# if repeater_iface_num is greater than the number of
|
|
# configured downlink we need to make sure its not deleted
|
|
local mode="$(uci -q get wireless.@wifi-iface[$i].mode)"
|
|
if [ "$mode" == "wet" -o "$mode" == "sta" ]; then
|
|
i=$((i+1))
|
|
continue
|
|
fi
|
|
state="reload"
|
|
uci -q delete wireless.@wifi-iface[$i] >/dev/null
|
|
# do not increment i. The next interface will now
|
|
# have the same index as the deleted interface
|
|
done
|
|
|
|
uci commit wireless
|
|
|
|
if [ "$from_gui" == "true" ]; then
|
|
# check for connectivity
|
|
wifi reload
|
|
[ -f /etc/init.d/layer2_interface_ethernet -a -f /etc/config/layer2_interface_ethernet ] && /etc/init.d/layer2_interface_ethernet reload
|
|
i=$SLEEPTIME;
|
|
|
|
while [ $i -gt 0 ]; do
|
|
ip=`route -n | awk '/^0.0.0.0/{print $2}'`
|
|
if [ "$ip" == "" ]; then
|
|
i=$((i-10))
|
|
sleep 10
|
|
continue
|
|
fi
|
|
ping -w1 $ip
|
|
if [ $? -eq 0 ]; then
|
|
restore
|
|
exit
|
|
else
|
|
i=$((i-10))
|
|
sleep 9
|
|
fi
|
|
done
|
|
restore "back"
|
|
else
|
|
if [ "$state" == "exit" ]; then
|
|
return
|
|
fi
|
|
ubus call leds set '{"state":"allflash"}'
|
|
if [ "$state" == "apply" ]; then
|
|
# wifi apply
|
|
wifi reload
|
|
else
|
|
# wifi reload
|
|
wifi reload
|
|
fi
|
|
[ -f /etc/init.d/layer2_interface_ethernet -a -f /etc/config/layer2_interface_ethernet ] && /etc/init.d/layer2_interface_ethernet reload
|
|
ubus call leds set '{"state":"normal"}'
|
|
fi
|