Skip to content

Commit

Permalink
Optimize wifi configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
RAKwireless authored and RAKwireless committed Aug 30, 2019
1 parent 632c29f commit 4e941e7
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lora/set_eui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ ! -e "/opt/ttn-gateway/packet_forwarder/lora_pkt_fwd/local_conf.json" ]; th
if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
GATEWAY_EUI_NIC="wlan0"
fi

if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
GATEWAY_EUI_NIC="usb0"
fi
Expand Down
4 changes: 4 additions & 0 deletions loraserver/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
GATEWAY_EUI_NIC="wlan0"
fi

if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
GATEWAY_EUI_NIC="usb0"
fi

if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
echo "ERROR: No network interface found. Cannot set gateway ID."
exit 1
Expand Down
186 changes: 184 additions & 2 deletions rak/gateway-config
Original file line number Diff line number Diff line change
Expand Up @@ -613,22 +613,204 @@ do_configure_wlan_ip() {
fi
}

#!/bin/bash

INTERACTIVE=True


get_wifi_country() {
CODE=${1:-0}
IFACE="$(list_wlan_interfaces | head -n 1)"
if [ -z "$IFACE" ]; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "No wireless interface found" 20 60
fi
return 1
fi
if ! wpa_cli -i "$IFACE" status > /dev/null 2>&1; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Could not communicate with wpa_supplicant" 20 60
fi
return 1
fi
wpa_cli -i "$IFACE" save_config > /dev/null 2>&1
COUNTRY="$(wpa_cli -i "$IFACE" get country)"
if [ "$COUNTRY" = "FAIL" ]; then
return 1
fi
if [ $CODE = 0 ]; then
echo "$COUNTRY"
fi
return 0
}

do_wifi_country() {
IFACE="$(list_wlan_interfaces | head -n 1)"
if [ -z "$IFACE" ]; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "No wireless interface found" 20 60
fi
return 1
fi

if ! wpa_cli -i "$IFACE" status > /dev/null 2>&1; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Could not communicate with wpa_supplicant" 20 60
fi
return 1
fi

oIFS="$IFS"
if [ "$INTERACTIVE" = True ]; then
IFS="/"
value=$(cat /usr/share/zoneinfo/iso3166.tab | tail -n +26 | tr '\t' '/' | tr '\n' '/')
COUNTRY=$(whiptail --nocancel --menu "Select the country in which the Pi is to be used" 20 60 10 ${value} 3>&1 1>&2 2>&3)
IFS=$oIFS
else
COUNTRY=$1
true
fi
if [ $? -eq 0 ];then
wpa_cli -i "$IFACE" set country "$COUNTRY"
wpa_cli -i "$IFACE" save_config > /dev/null 2>&1
if ! iw reg set "$COUNTRY" 2> /dev/null; then
ASK_TO_REBOOT=1
fi
if hash rfkill 2> /dev/null; then
rfkill unblock wifi
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Wi-fi country set to $COUNTRY" 20 60 1
fi
fi
# if [ $# -eq 0 ]; then
# echo "van 001" >> /home/pi/7.txt
# do_main_menu
# fi
}

list_wlan_interfaces() {
for dir in /sys/class/net/*/wireless; do
if [ -d "$dir" ]; then
basename "$(dirname "$dir")"
fi
done
}

do_wifi_ssid_passphrase() {
RET=0
IFACE_LIST="$(list_wlan_interfaces)"
IFACE="$(echo "$IFACE_LIST" | head -n 1)"

if [ -z "$IFACE" ]; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "No wireless interface found" 20 60
fi
return 1
fi

if ! wpa_cli -i "$IFACE" status > /dev/null 2>&1; then
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Could not communicate with wpa_supplicant" 20 60
fi
return 1
fi

if [ "$INTERACTIVE" = True ] && [ -z "$(get_wifi_country)" ]; then
do_wifi_country "not_goto_main_menu"
fi

SSID="$1"
while [ -z "$SSID" ] && [ "$INTERACTIVE" = True ]; do
SSID=$(whiptail --nocancel --inputbox "Please enter SSID" 10 60 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
elif [ -z "$SSID" ]; then
whiptail --msgbox "SSID cannot be empty. Please try again." 20 60
fi
done

PASSPHRASE="$2"
while [ "$INTERACTIVE" = True ]; do
PASSPHRASE=$(whiptail --nocancel --inputbox "Please enter passphrase. Leave it empty if none." 10 60 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
else
break
fi
done

# Escape special characters for embedding in regex below
local ssid="$(echo "$SSID" \
| sed 's;\\;\\\\;g' \
| sed -e 's;\.;\\\.;g' \
-e 's;\*;\\\*;g' \
-e 's;\+;\\\+;g' \
-e 's;\?;\\\?;g' \
-e 's;\^;\\\^;g' \
-e 's;\$;\\\$;g' \
-e 's;\/;\\\/;g' \
-e 's;\[;\\\[;g' \
-e 's;\];\\\];g' \
-e 's;{;\\{;g' \
-e 's;};\\};g' \
-e 's;(;\\(;g' \
-e 's;);\\);g' \
-e 's;";\\\\\";g')"

wpa_cli -i "$IFACE" list_networks \
| tail -n +2 | cut -f -2 | grep -P "\t$ssid$" | cut -f1 \
| while read ID; do
wpa_cli -i "$IFACE" remove_network "$ID" > /dev/null 2>&1
done

ID="$(wpa_cli -i "$IFACE" add_network)"
wpa_cli -i "$IFACE" set_network "$ID" ssid "\"$SSID\"" 2>&1 | grep -q "OK"
RET=$((RET + $?))

if [ -z "$PASSPHRASE" ]; then
wpa_cli -i "$IFACE" set_network "$ID" key_mgmt NONE 2>&1 | grep -q "OK"
RET=$((RET + $?))
else
wpa_cli -i "$IFACE" set_network "$ID" psk "\"$PASSPHRASE\"" 2>&1 | grep -q "OK"
RET=$((RET + $?))
fi

if [ $RET -eq 0 ]; then
wpa_cli -i "$IFACE" enable_network "$ID" > /dev/null 2>&1
else
wpa_cli -i "$IFACE" remove_network "$ID" > /dev/null 2>&1
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Failed to set SSID or passphrase" 20 60
fi
fi

wpa_cli -i "$IFACE" save_config > /dev/null 2>&1

echo "$IFACE_LIST" | while read IFACE; do
wpa_cli -i "$IFACE" reconfigure > /dev/null 2>&1
done
return $RET
}

do_configure_wifi() {
default_item=`do_get_gateway_info wifi.wifi_mode`

FUN=$(dialog --title "Configure wifi" --cancel-label "Cancel" --default-item $default_item --menu "Configuration options:" 12 60 20 \
FUN=$(dialog --title "Configure wifi" --cancel-label "Cancel" --default-item $default_item --menu "Configuration options:" 14 60 20 \
1 "Enable AP Mode/Disable Client Mode" \
2 "Enable Client Mode/Disable AP Mode" \
3 "Modify SSID and pwd for AP Mode" \
4 "Add New SSID for Client" \
5 "Change Wi-fi Country" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 0 ]; then
case "$FUN" in
1) do_enable_ap_mode;;
2) do_enable_wifi_mode;;
3) do_modify_ssid_for_ap;;
4) do_add_new_ssid;;
4) do_wifi_ssid_passphrase;;
5) do_wifi_country;;
esac
fi

Expand Down

0 comments on commit 4e941e7

Please sign in to comment.