@@ -14,51 +14,88 @@ rp_module_desc="Configure WiFi"
14
14
rp_module_section=" config"
15
15
rp_module_flags=" !x11"
16
16
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
+ }
17
37
function _set_interface_wifi() {
18
- local state=" $1 "
38
+ local iface=" $1 "
39
+ local state=" $2 "
19
40
20
41
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
23
44
fi
24
45
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
27
48
fi
28
49
fi
29
50
}
30
51
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 "
32
61
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
34
63
}
35
64
36
65
function list_wifi() {
37
66
local line
38
67
local essid
39
68
local type
69
+ local iface=" $1 "
70
+
40
71
while read line; do
41
72
[[ " $line " =~ ^Cell && -n " $essid " ]] && echo -e " $essid \n$type "
42
73
[[ " $line " =~ ^ESSID ]] && essid=$( echo " $line " | cut -d\" -f2)
43
74
[[ " $line " == " Encryption key:off" ]] && type=" open"
44
75
[[ " $line " == " Encryption key:on" ]] && type=" wep"
45
76
[[ " $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:.*" )
47
78
echo -e " $essid \n$type "
48
79
}
49
80
50
81
function 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"
53
88
return 1
54
89
fi
90
+ mgmt_tool=" $( _get_mgmt_tool_wifi) "
91
+
55
92
local essids=()
56
93
local essid
57
94
local types=()
58
95
local type
59
96
local options=()
60
97
i=0
61
- _set_interface_wifi up 2> /dev/null
98
+ _set_interface_wifi $iface up 2> /dev/null
62
99
dialog --infobox " \nScanning for WiFi networks..." 5 40 > /dev/tty
63
100
sleep 1
64
101
@@ -67,10 +104,10 @@ function connect_wifi() {
67
104
types+=(" $type " )
68
105
options+=(" $i " " $essid " )
69
106
(( i++ ))
70
- done < <( list_wifi)
107
+ done < <( list_wifi $iface )
71
108
options+=(" H" " Hidden ESSID" )
72
109
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)
74
111
choice=$( " ${cmd[@]} " " ${options[@]} " 2>&1 > /dev/tty)
75
112
[[ -z " $choice " ]] && return
76
113
@@ -108,15 +145,46 @@ function connect_wifi() {
108
145
done
109
146
fi
110
147
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"
112
158
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
114
181
}
115
182
116
- function create_config_wifi () {
183
+ function create_wpasupplicant_config_wifi () {
117
184
local type=" $1 "
118
185
local essid=" $2 "
119
186
local key=" $3 "
187
+ local dev=" $4 "
120
188
121
189
local wpa_config
122
190
wpa_config+=" \tssid=\" $essid \" \n"
@@ -136,7 +204,7 @@ function create_config_wifi() {
136
204
137
205
[[ $hidden -eq 1 ]] && wpa_config+=" \tscan_ssid=1\n"
138
206
139
- remove_wifi
207
+ remove_wpasupplicant_wifi
140
208
wpa_config=$( echo -e " $wpa_config " )
141
209
cat >> " /etc/wpa_supplicant/wpa_supplicant.conf" << _EOF_
142
210
# RETROPIE CONFIG START
@@ -148,11 +216,22 @@ _EOF_
148
216
}
149
217
150
218
function 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
+
156
235
dialog --backtitle " $__backtitle " --infobox " \nConnecting ..." 5 40 > /dev/tty
157
236
local id=" "
158
237
i=0
@@ -163,16 +242,15 @@ function gui_connect_wifi() {
163
242
done
164
243
if [[ -z " $id " ]]; then
165
244
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
167
246
fi
168
247
}
169
248
170
249
function _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
176
254
raspi-config
177
255
fi
178
256
fi
@@ -183,10 +261,16 @@ function gui_wifi() {
183
261
isPlatform " rpi" && _check_country_wifi
184
262
185
263
local default
264
+ local iface
265
+ local mgmt_tool
266
+
267
+ iface=" $( _get_interface_wifi) "
268
+ mgmt_tool=" $( _get_mgmt_tool_wifi) "
269
+
186
270
while true ; do
187
271
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)
190
274
local options=(
191
275
1 " Connect to WiFi network"
192
276
" 1 Connect to your WiFi network"
@@ -211,12 +295,12 @@ The file should contain two lines as follows\n\nssid = \"YOUR WIFI SSID\"\npsk =
211
295
if [[ -n " $choice " ]]; then
212
296
case " $choice " in
213
297
1)
214
- connect_wifi
298
+ connect_wifi $iface
215
299
;;
216
300
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
218
302
[[ $? -ne 0 ]] && continue
219
- remove_wifi
303
+ remove_ ${mgmt_tool} _wifi $iface
220
304
;;
221
305
3)
222
306
if [[ -f " /boot/wifikeyfile.txt" ]]; then
@@ -225,8 +309,8 @@ The file should contain two lines as follows\n\nssid = \"YOUR WIFI SSID\"\npsk =
225
309
local ssid=" $ini_value "
226
310
iniGet " psk"
227
311
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 "
230
314
else
231
315
printMsgs " dialog" " No /boot/wifikeyfile.txt found"
232
316
fi
0 commit comments