From 4e941e7afaa57b3483918ccc78b84e936a623ea5 Mon Sep 17 00:00:00 2001 From: RAKwireless Date: Fri, 30 Aug 2019 10:26:12 +0800 Subject: [PATCH] Optimize wifi configuration --- lora/set_eui.sh | 2 +- loraserver/install.sh | 4 + rak/gateway-config | 186 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 189 insertions(+), 3 deletions(-) diff --git a/lora/set_eui.sh b/lora/set_eui.sh index 85c35db..c55b4f2 100755 --- a/lora/set_eui.sh +++ b/lora/set_eui.sh @@ -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 diff --git a/loraserver/install.sh b/loraserver/install.sh index 76c8329..b4d2fa2 100755 --- a/loraserver/install.sh +++ b/loraserver/install.sh @@ -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 diff --git a/rak/gateway-config b/rak/gateway-config index 82fec7e..485f9a8 100755 --- a/rak/gateway-config +++ b/rak/gateway-config @@ -613,14 +613,195 @@ 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 @@ -628,7 +809,8 @@ do_configure_wifi() { 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