Skip to content

Commit ff55eef

Browse files
committed
changed after review
1 parent 8888a9c commit ff55eef

2 files changed

Lines changed: 106 additions & 20 deletions

File tree

gsmenu.sh

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,38 @@ list_wifi_channels() {
7272
iw list | grep MHz | grep -v disabled | grep -v "radar detection" | grep \* | tr -d '[]' | awk '{print $4 " (" $2 " " $3 ")"}' | grep '^[1-9]' | sort -n | uniq | sed -z '$ s/\n$//'
7373
}
7474

75+
# Add or update a network stanza in wpa_supplicant.conf.
76+
# Usage: wpa_conf_update_network <ssid> <psk> (psk may be empty for open networks)
77+
wpa_conf_update_network() {
78+
local ssid="$1"
79+
local psk="$2"
80+
local conf="/etc/wpa_supplicant.conf"
81+
local tmpfile
82+
tmpfile=$(mktemp)
83+
84+
# Remove any existing stanza for this SSID, then append the updated one
85+
if [ -f "$conf" ]; then
86+
awk -v ssid="$ssid" '
87+
/network=\{/ { in_block=1; block="" }
88+
in_block { block = block $0 "\n" }
89+
in_block && /\}/ {
90+
if (index(block, "ssid=\"" ssid "\"") == 0)
91+
printf "%s", block
92+
in_block=0; block=""
93+
next
94+
}
95+
!in_block { print }
96+
' "$conf" > "$tmpfile"
97+
fi
98+
99+
if [ -z "$psk" ]; then
100+
printf 'network={\n ssid="%s"\n key_mgmt=NONE\n}\n' "$ssid" >> "$tmpfile"
101+
else
102+
printf 'network={\n ssid="%s"\n psk="%s"\n}\n' "$ssid" "$psk" >> "$tmpfile"
103+
fi
104+
mv "$tmpfile" "$conf"
105+
}
106+
75107
# ══════════════════════════════════════════════════════════════════════════════
76108
# Cache refresh (only for air get commands)
77109
# ══════════════════════════════════════════════════════════════════════════════
@@ -863,12 +895,12 @@ case "$@" in
863895
nmcli connection show --active | grep -q "Hotspot" && echo 1 || echo 0
864896
;;
865897
"get gs wifi wlan")
866-
connection=$(nmcli -t connection show --active | grep wlan0 | cut -d : -f1)
898+
connection=$(nmcli -t connection show --active | grep wlan0 | grep -v Hotspot | cut -d : -f1)
867899
[ -z "${connection}" ] && echo 0 || echo 1
868900
;;
869901
"get gs wifi ssid")
870902
if [ -d /sys/class/net/wlan0 ]; then
871-
nmcli -t connection show --active | grep wlan0 | cut -d : -f1
903+
nmcli -t connection show --active | grep wlan0 | grep -v Hotspot | cut -d : -f1
872904
else
873905
echo -n ""
874906
fi
@@ -884,6 +916,51 @@ case "$@" in
884916
"get gs wifi IP")
885917
ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
886918
;;
919+
"get gs wifi savednetworks")
920+
# Enumerate saved WiFi connection profiles and output ESCAPED_SSID:password
921+
nmcli -t -f NAME,TYPE connection show 2>/dev/null | grep ":802-11-wireless$" | \
922+
sed 's/:802-11-wireless$//' | \
923+
while IFS= read -r name; do
924+
ssid=$(nmcli -s -t -f 802-11-wireless.ssid connection show "$name" 2>/dev/null | cut -d: -f2-)
925+
psk=$(nmcli -s -t -f 802-11-wireless-security.psk connection show "$name" 2>/dev/null | cut -d: -f2-)
926+
[ -z "$ssid" ] && continue
927+
escaped=$(printf '%s' "$ssid" | sed 's/:/\\:/g')
928+
printf '%s:%s\n' "$escaped" "$psk"
929+
done
930+
;;
931+
"get gs wifi networks")
932+
[ ! -d /sys/class/net/wlan0 ] && exit 0
933+
# Output ESCAPED_SSID:SECURITY:SIGNAL per line (nmcli already escapes ':' in SSIDs)
934+
# Use NF-based split so SSIDs with colons are reconstructed correctly
935+
nmcli -t -f SSID,SECURITY,SIGNAL device wifi list 2>/dev/null | \
936+
awk -F: '
937+
{
938+
signal = $NF
939+
security = $(NF-1)
940+
ssid = ""
941+
for (i = 1; i <= NF-2; i++) {
942+
if (i > 1) ssid = ssid ":"
943+
ssid = ssid $i
944+
}
945+
if (ssid == "") next
946+
sec = (security == "--") ? "--" : "WPA"
947+
print ssid ":" sec ":" signal
948+
}
949+
'
950+
;;
951+
"set gs wifi connect"*)
952+
[ ! -d /sys/class/net/wlan0 ] && exit 0
953+
if nmcli connection show | grep -q "$5"; then
954+
nmcli con up "$5"
955+
else
956+
nmcli device wifi connect "$5" $( [ -n "$6" ] && printf 'password "%s"' "$6" ) ifname wlan0
957+
fi
958+
;;
959+
"set gs wifi disconnect"*)
960+
[ ! -d /sys/class/net/wlan0 ] && exit 0
961+
connection=$(nmcli -t connection show --active | grep wlan0 | grep -v Hotspot | cut -d : -f1)
962+
[ -n "$connection" ] && nmcli con down "$connection" || true
963+
;;
887964

888965
"set gs wifi wlan"*)
889966
[ ! -d /sys/class/net/wlan0 ] && exit 0
@@ -904,6 +981,7 @@ case "$@" in
904981
"set gs wifi hotspot"*)
905982
[ ! -d /sys/class/net/wlan0 ] && exit 0
906983
if [ "$5" = "on" ]; then
984+
nmcli connection show --active | grep -q "Hotspot" && exit 0 # already on
907985
if nmcli connection show | grep -q "Hotspot"; then
908986
echo "Hotspot connection exists. Starting it..."
909987
nmcli con up Hotspot
@@ -918,6 +996,7 @@ case "$@" in
918996
nmcli con up Hotspot
919997
fi
920998
else
999+
nmcli connection show --active | grep -q "Hotspot" || exit 0 # already off
9211000
nmcli con down Hotspot
9221001
fi
9231002
;;

src/gsmenu/gs_wifi.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,21 @@ static void rebuild_network_list(void) {
303303
lv_obj_t * lbl = lv_label_create(net_list_cont);
304304
lv_label_set_text(lbl, "No networks found");
305305
} else {
306-
/* Fetch currently connected SSID to mark it */
306+
/* Fetch currently connected SSID to mark it.
307+
* Skip when hotspot is active — the interface is in AP mode. */
307308
char connected_ssid[MAX_SSID_LEN] = "";
308-
char * conn = run_command("gsmenu.sh get gs wifi ssid");
309-
if (conn) {
310-
size_t len = strlen(conn);
311-
while (len > 0 && (conn[len-1] == '\n' || conn[len-1] == '\r'))
312-
conn[--len] = '\0';
313-
if (len > 0 && len < MAX_SSID_LEN)
314-
strncpy(connected_ssid, conn, MAX_SSID_LEN - 1);
315-
free(conn);
309+
bool hotspot_on = (hotspot_sw_ref && lv_obj_is_valid(hotspot_sw_ref) &&
310+
lv_obj_has_state(hotspot_sw_ref, LV_STATE_CHECKED));
311+
if (!hotspot_on) {
312+
char * conn = run_command("gsmenu.sh get gs wifi ssid");
313+
if (conn) {
314+
size_t len = strlen(conn);
315+
while (len > 0 && (conn[len-1] == '\n' || conn[len-1] == '\r'))
316+
conn[--len] = '\0';
317+
if (len > 0 && len < MAX_SSID_LEN)
318+
strncpy(connected_ssid, conn, MAX_SSID_LEN - 1);
319+
free(conn);
320+
}
316321
}
317322

318323
for (int i = 0; i < network_count; i++) {
@@ -380,15 +385,13 @@ static void rebuild_network_list(void) {
380385
/* ── Status ──────────────────────────────────────────────────────────────── */
381386

382387
static void update_status(void) {
383-
/* When hotspot is active the interface is in AP mode — not a client */
384-
char * hs = run_command("gsmenu.sh get gs wifi hotspot");
385-
if (hs) {
386-
bool hotspot_on = (hs[0] == '1');
387-
free(hs);
388-
if (hotspot_on) {
389-
lv_label_set_text(status_lbl, LV_SYMBOL_WIFI " Hotspot active");
390-
return;
391-
}
388+
/* When hotspot is active the interface is in AP mode — not a client.
389+
* Read from the already-loaded switch widget to avoid a redundant
390+
* gsmenu.sh call on page load (reload_switch_value runs first). */
391+
if (hotspot_sw_ref && lv_obj_is_valid(hotspot_sw_ref) &&
392+
lv_obj_has_state(hotspot_sw_ref, LV_STATE_CHECKED)) {
393+
lv_label_set_text(status_lbl, LV_SYMBOL_WIFI " Hotspot active");
394+
return;
392395
}
393396
char * conn = run_command("gsmenu.sh get gs wifi ssid");
394397
if (conn) {
@@ -433,6 +436,10 @@ static void hotspot_switch_status_cb(lv_event_t * e) {
433436
} else {
434437
lv_label_set_text(status_lbl, "Not connected");
435438
}
439+
/* Refresh the network list so no network remains marked "Connected"
440+
* after hotspot is toggled on, and vice versa. */
441+
if (network_count > 0)
442+
rebuild_network_list();
436443
}
437444

438445
/* ── Page load callback ──────────────────────────────────────────────────── */

0 commit comments

Comments
 (0)