Skip to content

Commit

Permalink
Add Wi-Fi toggle for time synchronization at startup (#1728)
Browse files Browse the repository at this point in the history
Added a toggle for temporarily enabling Wi-Fi at startup for syncing
time.

Closes #1501
  • Loading branch information
Aemiii91 authored Dec 23, 2024
1 parent 80a5596 commit 42da308
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
12 changes: 11 additions & 1 deletion src/tweaks/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool _writeDateString(char *label_out)
void menu_datetime(void *_)
{
if (!_menu_date_time._created) {
_menu_date_time = list_create(6, LIST_SMALL);
_menu_date_time = list_create(7, LIST_SMALL);
strcpy(_menu_date_time.title, "Date and time");
list_addItem(&_menu_date_time,
(ListItem){
Expand Down Expand Up @@ -125,6 +125,16 @@ void menu_datetime(void *_)
" \n"
"Ensures that time is synced before a game\n"
"is launched.");
list_addItemWithInfoNote(&_menu_date_time,
(ListItem){
.label = "Enable Wi-Fi temporarily",
.item_type = TOGGLE,
.disabled = !network_state.ntp,
.value = (int)network_state.force_wifi_on_startup,
.action = network_setNtpForceState},
"Enables Wi-Fi temporarily at startup\n"
"to sync the time. It will be turned off\n"
"again after the sync (if it was off).");
list_addItemWithInfoNote(&_menu_date_time,
(ListItem){
.label = "Get time zone via IP address",
Expand Down
8 changes: 8 additions & 0 deletions src/tweaks/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "utils/json.h"
#include "utils/keystate.h"
#include "utils/netinfo.h"
#include "utils/process.h"

#include "./appstate.h"

Expand All @@ -43,6 +44,7 @@ static struct network_s {
bool keep_alive;
bool vncserv;
bool loaded;
bool force_wifi_on_startup;
int vncfps;
} network_state = {
.vncfps = 20,
Expand All @@ -61,6 +63,7 @@ void network_loadState(void)
network_state.hotspot = config_flag_get(".hotspotState");
network_state.ntp = config_flag_get(".ntpState");
network_state.ntp_wait = config_flag_get(".ntpWait");
network_state.force_wifi_on_startup = config_flag_get(".ntpForce");
network_state.auth_ftp = config_flag_get(".authftpState");
network_state.auth_http = config_flag_get(".authhttpState");
network_state.auth_ssh = config_flag_get(".authsshState");
Expand Down Expand Up @@ -288,6 +291,11 @@ void network_setNtpState(void *pt)
all_changed = true;
}

void network_setNtpForceState(void *pt)
{
network_setState(&network_state.force_wifi_on_startup, ".ntpForce", ((ListItem *)pt)->value);
}

void network_setNtpWaitState(void *pt)
{
network_setState(&network_state.ntp_wait, ".ntpWait", ((ListItem *)pt)->value);
Expand Down
8 changes: 5 additions & 3 deletions static/build/.tmp_update/runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,14 @@ start_networking() {
}

check_networking() {
if [ ! -f /tmp/network_changed ]; then
return
fi

if pgrep -f update_networking.sh; then
log "update_networking already running"
else
if [ -f /tmp/network_changed ]; then
rm /tmp/network_changed
fi
rm /tmp/network_changed
$sysdir/script/network/update_networking.sh check
fi
}
Expand Down
74 changes: 48 additions & 26 deletions static/build/.tmp_update/script/network/update_networking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ main() {
;;
authed)
${service}_authed &
store_state &
store_state &
;;
*)
print_usage
Expand Down Expand Up @@ -73,18 +73,29 @@ main() {
# Standard check from runtime for startup.
check() {
log "Network Checker: Update networking"
if wifi_enabled && [ "$is_booting" -eq 1 ]; then
bootScreen Boot "Waiting for network..."
local force_wifi_on_startup=$([ -f $sysdir/config/.ntpForce ] && echo 1 || echo 0)
local has_wifi=$(wifi_enabled && echo 1 || echo 0)

if [ "$is_booting" -eq 1 ]; then
if [ "$has_wifi" -eq 0 ]; then
if [ "$force_wifi_on_startup" -eq 1 ]; then
bootScreen Boot "Turning on Wi-Fi..."
wifi_on
has_wifi=1
fi
else
bootScreen Boot "Waiting for network..."
fi
fi

check_wifi
check_ftpstate &
check_sshstate &
check_telnetstate &
check_httpstate &
check_smbdstate &

if wifi_enabled && flag_enabled ntpWait && [ $is_booting -eq 1 ]; then
if [ "$has_wifi" -eq 1 ] && flag_enabled ntpWait && [ $is_booting -eq 1 ]; then
bootScreen Boot "Syncing time..."
check_ntpstate && bootScreen Boot "Time synced: $(date +"%H:%M")" || bootScreen Boot "Time sync failed"
sleep 1
Expand All @@ -95,11 +106,15 @@ check() {
if [ -f "$sysdir/.updateAvailable" ] && [ $is_booting -eq 1 ]; then
bootScreen Boot "Update available!"
sleep 1
elif wifi_enabled && [ ! -f /tmp/update_checked ]; then
elif [ "$has_wifi" -eq 1 ] && [ ! -f /tmp/update_checked ]; then
touch /tmp/update_checked
$sysdir/script/ota_update.sh check &
fi

if [ "$is_booting" -eq 1 ] && [ "$force_wifi_on_startup" -eq 1 ]; then
bootScreen Boot "Turning off Wi-Fi..."
wifi_off
fi
}

# Function to help disable and kill off all processes
Expand All @@ -124,7 +139,7 @@ disable_all_services() {
check_wifi() {
# Fixes lockups entering some apps after enabling wifi (because wpa_supp/udhcpc are preloaded with libpadsp.so)
libpadspblocker &

if is_running hostapd; then
return
else
Expand All @@ -136,25 +151,34 @@ check_wifi() {
pkill -9 udhcpc
rm /tmp/restart_wifi
fi
log "Network Checker: Initializing wifi..."
/customer/app/axp_test wifion
sleep 2
ifconfig wlan0 up
$miyoodir/app/wpa_supplicant -B -D nl80211 -iwlan0 -c /appconfigs/wpa_supplicant.conf
udhcpc -i wlan0 -s /etc/init.d/udhcpc.script &
iw dev wlan0 set power_save off
wifi_on
fi
else
if [ ! -f "/tmp/dont_restart_wifi" ]; then
store_state
pkill -9 wpa_supplicant
pkill -9 udhcpc
/customer/app/axp_test wifioff
wifi_off
fi
fi
fi
}

wifi_on() {
log "Network Checker: Turning on Wi-Fi..."
/customer/app/axp_test wifion
sleep 2
ifconfig wlan0 up
$miyoodir/app/wpa_supplicant -B -D nl80211 -iwlan0 -c /appconfigs/wpa_supplicant.conf
udhcpc -i wlan0 -s /etc/init.d/udhcpc.script &
iw dev wlan0 set power_save off
}

wifi_off() {
log "Network Checker: Turning off Wi-Fi..."
pkill -9 wpa_supplicant
pkill -9 udhcpc
/customer/app/axp_test wifioff
}

# Starts the samba daemon if the toggle is set to on
check_smbdstate() {
if flag_enabled smbdState; then
Expand Down Expand Up @@ -568,16 +592,16 @@ init_json() {
}

# unhook libpadsp.so on the wifi servs
libpadspblocker() {
libpadspblocker() {
wpa_pid=$(ps -e | grep "[w]pa_supplicant" | awk 'NR==1{print $1}')
udhcpc_pid=$(ps -e | grep "[u]dhcpc" | awk 'NR==1{print $1}')
if [ -n "$wpa_pid" ] && [ -n "$udhcpc_pid" ]; then
if grep -q "libpadsp.so" /proc/$wpa_pid/maps || grep -q "libpadsp.so" /proc/$udhcpc_pid/maps; then
echo "Network Checker: $wpa_pid(WPA) and $udhcpc_pid(UDHCPC) found preloaded with libpadsp.so"
unset LD_PRELOAD
killall -9 wpa_supplicant
killall -9 udhcpc
$miyoodir/app/wpa_supplicant -B -D nl80211 -iwlan0 -c /appconfigs/wpa_supplicant.conf &
killall -9 udhcpc
$miyoodir/app/wpa_supplicant -B -D nl80211 -iwlan0 -c /appconfigs/wpa_supplicant.conf &
udhcpc -i wlan0 -s /etc/init.d/udhcpc.script &
echo "Network Checker: Removing libpadsp.so preload on wpa_supp/udhcpc"
fi
Expand All @@ -602,13 +626,12 @@ store_state() {
service_key="$service_key \"$service\": \"0\""
fi
done

service_key="$service_key }"
echo "$service_key" > "$jsonfile"
}


# Restore the network service state, don't use jq it's too slow and holds the UI thread until it returns
# Restore the network service state, don't use jq it's too slow and holds the UI thread until it returns
# (you can't background this, you'll miss the checks for net servs as the flags won't be set)
restore_state() {
[ ! -f "$jsonfile" ] && return
Expand All @@ -617,7 +640,7 @@ restore_state() {
grep -E '"(httpState|ftpState|smbdState|sshState|authsshState|authftpState|authhttpState)":\s*"[01]"' "$jsonfile" | while IFS=":" read -r key value; do
key=$(echo "$key" | tr -d ' "{}')
value=$(echo "$value" | tr -d ' ",')

if echo "$services" | grep -wq "$key"; then
if [ "$value" = "1" ]; then
enable_flag "$key"
Expand Down Expand Up @@ -696,7 +719,7 @@ enable_flag() {

disable_flag() {
flag="$1"
mv "$sysdir/config/.$flag" "$sysdir/config/.$flag_"
mv "$sysdir/config/.$flag" "$sysdir/config/.$flag_"
}

is_running() {
Expand All @@ -719,4 +742,3 @@ if [ -f $sysdir/config/.logging ]; then
else
main "$@" 2>&1 > /dev/null
fi

Empty file.
Empty file.

0 comments on commit 42da308

Please sign in to comment.