@@ -14,51 +14,88 @@ rp_module_desc="Configure WiFi"
1414rp_module_section=" config"
1515rp_module_flags=" !x11"
1616
17+ function _get_interface_wifi() {
18+ local iface
19+ # look for the first wireless interface present
20+ for iface in /sys/class/net/* ; do
21+ if [[ -d " $iface /wireless" ]]; then
22+ echo " $( basename $iface ) "
23+ return 0
24+ fi
25+ done
26+ return 1
27+ }
28+
29+ function _get_mgmt_tool_wifi() {
30+ # get the WiFi connection manager
31+ if systemctl -q is-active NetworkManager.service; then
32+ echo " nm"
33+ else
34+ echo " wpasupplicant"
35+ fi
36+ }
1737function _set_interface_wifi() {
18- local state=" $1 "
38+ local iface=" $1 "
39+ local state=" $2 "
1940
2041 if [[ " $state " == " up" ]]; then
21- if ! ifup wlan0 ; then
22- ip link set wlan0 up
42+ if ! ifup $iface ; then
43+ ip link set $iface up
2344 fi
2445 elif [[ " $state " == " down" ]]; then
25- if ! ifdown wlan0 ; then
26- ip link set wlan0 down
46+ if ! ifdown $iface ; then
47+ ip link set $iface down
2748 fi
2849 fi
2950}
3051
31- function remove_wifi() {
52+ function remove_nm_wifi() {
53+ local iface=" $1 "
54+ # delete the NM connection named RetroPie-WiFi
55+ nmcli connection delete RetroPie-WiFi
56+ _set_interface_wifi $iface down 2> /dev/null
57+ }
58+
59+ function remove_wpasupplicant_wifi() {
60+ local iface=" $1 "
3261 sed -i ' /RETROPIE CONFIG START/,/RETROPIE CONFIG END/d' " /etc/wpa_supplicant/wpa_supplicant.conf"
33- _set_interface_wifi down 2> /dev/null
62+ _set_interface_wifi $iface down 2> /dev/null
3463}
3564
3665function list_wifi() {
3766 local line
3867 local essid
3968 local type
69+ local iface=" $1 "
70+
4071 while read line; do
4172 [[ " $line " =~ ^Cell && -n " $essid " ]] && echo -e " $essid \n$type "
4273 [[ " $line " =~ ^ESSID ]] && essid=$( echo " $line " | cut -d\" -f2)
4374 [[ " $line " == " Encryption key:off" ]] && type=" open"
4475 [[ " $line " == " Encryption key:on" ]] && type=" wep"
4576 [[ " $line " =~ ^IE:.* WPA ]] && type=" wpa"
46- done < <( iwlist wlan0 scan | grep -o " Cell .*\|ESSID:\" .*\" \|IE: .*WPA\|Encryption key:.*" )
77+ done < <( iwlist $iface scan | grep -o " Cell .*\|ESSID:\" .*\" \|IE: .*WPA\|Encryption key:.*" )
4778 echo -e " $essid \n$type "
4879}
4980
5081function connect_wifi() {
51- if [[ ! -d " /sys/class/net/wlan0/" ]]; then
52- printMsgs " dialog" " No wlan0 interface detected"
82+ local iface
83+ local mgmt_tool=" wpasupplicant"
84+
85+ iface=" $( _get_interface_wifi) "
86+ if [[ -z " $iface " ]]; then
87+ printMsgs " dialog" " No wireless interfaces detected"
5388 return 1
5489 fi
90+ mgmt_tool=" $( _get_mgmt_tool_wifi) "
91+
5592 local essids=()
5693 local essid
5794 local types=()
5895 local type
5996 local options=()
6097 i=0
61- _set_interface_wifi up 2> /dev/null
98+ _set_interface_wifi $iface up 2> /dev/null
6299 dialog --infobox " \nScanning for WiFi networks..." 5 40 > /dev/tty
63100 sleep 1
64101
@@ -67,10 +104,10 @@ function connect_wifi() {
67104 types+=(" $type " )
68105 options+=(" $i " " $essid " )
69106 (( i++ ))
70- done < <( list_wifi)
107+ done < <( list_wifi $iface )
71108 options+=(" H" " Hidden ESSID" )
72109
73- local cmd=(dialog --backtitle " $__backtitle " --menu " Please choose the network you would like to connect to" 22 76 16)
110+ local cmd=(dialog --backtitle " $__backtitle " --menu " Please choose the WiFi network you would like to connect to" 22 76 16)
74111 choice=$( " ${cmd[@]} " " ${options[@]} " 2>&1 > /dev/tty)
75112 [[ -z " $choice " ]] && return
76113
@@ -108,15 +145,46 @@ function connect_wifi() {
108145 done
109146 fi
110147
111- create_config_wifi " $type " " $essid " " $key "
148+ create_${mgmt_tool} _config_wifi " $type " " $essid " " $key " " $iface "
149+ gui_connect_wifi " $iface "
150+ }
151+
152+ function create_nm_config_wifi() {
153+ local type=" $1 "
154+ local essid=" $2 "
155+ local key=" $3 "
156+ local dev=" $4 "
157+ local con=" RetroPie-WiFi"
112158
113- gui_connect_wifi
159+ remove_nm_wifi
160+ nmcli connection add type wifi ifname " $dev " ssid " $essid " con-name " $con " autoconnect yes
161+ # configure security for the connection
162+ case $type in
163+ wpa)
164+ nmcli connection modify " $con " \
165+ wifi-sec.key-mgmt wpa-psk \
166+ wifi-sec.psk-flags 0 \
167+ wifi-sec.psk " $key "
168+ ;;
169+ wep)
170+ nmcli connection modify " $con " \
171+ wifi-sec.key-mgmt none \
172+ wifi-sec.wep-key-flags 0 \
173+ wifi-sec.wep-key-type 2 \
174+ wifi-sec.wep-key0 " $key "
175+ ;;
176+ open)
177+ ;;
178+ esac
179+
180+ [[ $hidden -eq 1 ]] && nmcli connection modify " $con " wifi.hidden yes
114181}
115182
116- function create_config_wifi () {
183+ function create_wpasupplicant_config_wifi () {
117184 local type=" $1 "
118185 local essid=" $2 "
119186 local key=" $3 "
187+ local dev=" $4 "
120188
121189 local wpa_config
122190 wpa_config+=" \tssid=\" $essid \" \n"
@@ -136,7 +204,7 @@ function create_config_wifi() {
136204
137205 [[ $hidden -eq 1 ]] && wpa_config+=" \tscan_ssid=1\n"
138206
139- remove_wifi
207+ remove_wpasupplicant_wifi
140208 wpa_config=$( echo -e " $wpa_config " )
141209 cat >> " /etc/wpa_supplicant/wpa_supplicant.conf" << _EOF_
142210# RETROPIE CONFIG START
@@ -148,11 +216,22 @@ _EOF_
148216}
149217
150218function gui_connect_wifi() {
151- _set_interface_wifi down 2> /dev/null
152- _set_interface_wifi up 2> /dev/null
153- # BEGIN workaround for dhcpcd trigger failure on Raspbian stretch
154- systemctl restart dhcpcd & > /dev/null
155- # END workaround
219+ local iface=" $1 "
220+ local mgmt_tool
221+
222+ mgmt_tool=" $( _get_mgmt_tool_wifi) "
223+ _set_interface_wifi $iface down 2> /dev/null
224+ _set_interface_wifi $iface up 2> /dev/null
225+
226+ if [[ " $mgmt_tool " == " wpasupplicant" ]]; then
227+ # BEGIN workaround for dhcpcd trigger failure on Raspbian stretch
228+ systemctl restart dhcpcd & > /dev/null
229+ # END workaround
230+ fi
231+ if [[ " $mgmt_tool " == " nm" ]]; then
232+ nmcli -w 0 connection up RetroPie-WiFi
233+ fi
234+
156235 dialog --backtitle " $__backtitle " --infobox " \nConnecting ..." 5 40 > /dev/tty
157236 local id=" "
158237 i=0
@@ -163,16 +242,15 @@ function gui_connect_wifi() {
163242 done
164243 if [[ -z " $id " ]]; then
165244 printMsgs " dialog" " Unable to connect to network $essid "
166- _set_interface_wifi down 2> /dev/null
245+ _set_interface_wifi $iface down 2> /dev/null
167246 fi
168247}
169248
170249function _check_country_wifi() {
171- [[ ! -f /etc/wpa_supplicant/wpa_supplicant.conf ]] && return
172- iniConfig " =" " " /etc/wpa_supplicant/wpa_supplicant.conf
173- iniGet " country"
174- if [[ -z " $ini_value " ]]; then
175- if dialog --defaultno --yesno " You don't currently have your WiFi country set in /etc/wpa_supplicant/wpa_supplicant.conf\n\nOn a Raspberry Pi 3B+/4B/400 your WiFI will be disabled until the country is set. You can do this via raspi-config which is available from the RetroPie menu in Emulation Station. Once in raspi-config you can set your country via menu 5 (Localisation Options)\n\nDo you want me to launch raspi-config for you now ?" 22 76 2>&1 > /dev/tty; then
250+ local country
251+ country=" $( raspi-config nonint get_wifi_country) "
252+ if [[ -z " $country " ]]; then
253+ if dialog --defaultno --yesno " You don't currently have your WiFi country set.\n\nOn a Raspberry Pi 3B+ and later your WiFi will be disabled until the country is set. You can do this via raspi-config which is available from the RetroPie menu in Emulation Station. Once in raspi-config you can set your country via menu 5 (Localisation Options)\n\nDo you want me to launch raspi-config for you now ?" 22 76 2>&1 > /dev/tty; then
176254 raspi-config
177255 fi
178256 fi
@@ -183,10 +261,16 @@ function gui_wifi() {
183261 isPlatform " rpi" && _check_country_wifi
184262
185263 local default
264+ local iface
265+ local mgmt_tool
266+
267+ iface=" $( _get_interface_wifi) "
268+ mgmt_tool=" $( _get_mgmt_tool_wifi) "
269+
186270 while true ; do
187271 local ip_current=" $( getIPAddress) "
188- local ip_wlan=" $( getIPAddress wlan0 ) "
189- local cmd=(dialog --backtitle " $__backtitle " --cancel-label " Exit" --item-help --help-button --default-item " $default " --menu " Configure WiFi\nCurrent IP: ${ip_current:- (unknown)} \nWireless IP: ${ip_wlan:- (unknown)} \nWireless ESSID: $( iwgetid -r) " 22 76 16)
272+ local ip_wlan=" $( getIPAddress $iface ) "
273+ local cmd=(dialog --backtitle " $__backtitle " --colors -- cancel-label " Exit" --item-help --help-button --default-item " $default " --title " Configure WiFi" --menu " Current IP: \Zb ${ip_current:- (unknown)} \ZB\ nWireless IP: \Zb ${ip_wlan:- (unknown)} \ZB\ nWireless ESSID: \Zb $( iwgetid -r || echo " none " ) \ZB " 22 76 16)
190274 local options=(
191275 1 " Connect to WiFi network"
192276 " 1 Connect to your WiFi network"
@@ -211,12 +295,12 @@ The file should contain two lines as follows\n\nssid = \"YOUR WIFI SSID\"\npsk =
211295 if [[ -n " $choice " ]]; then
212296 case " $choice " in
213297 1)
214- connect_wifi
298+ connect_wifi $iface
215299 ;;
216300 2)
217- dialog --defaultno --yesno " This will remove the WiFi configuration and stop the WiFi.\n\nAre you sure you want to continue ?" 12 35 2>&1 > /dev/tty
301+ dialog --defaultno --yesno " This will remove the WiFi configuration and stop the WiFi.\n\nAre you sure you want to continue ?" 12 60 2>&1 > /dev/tty
218302 [[ $? -ne 0 ]] && continue
219- remove_wifi
303+ remove_ ${mgmt_tool} _wifi $iface
220304 ;;
221305 3)
222306 if [[ -f " /boot/wifikeyfile.txt" ]]; then
@@ -225,8 +309,8 @@ The file should contain two lines as follows\n\nssid = \"YOUR WIFI SSID\"\npsk =
225309 local ssid=" $ini_value "
226310 iniGet " psk"
227311 local psk=" $ini_value "
228- create_config_wifi " wpa" " $ssid " " $psk "
229- gui_connect_wifi
312+ create_ ${mgmt_tool} _config_wifi " wpa" " $ssid " " $psk " " $iface "
313+ gui_connect_wifi " $iface "
230314 else
231315 printMsgs " dialog" " No /boot/wifikeyfile.txt found"
232316 fi
0 commit comments