Skip to content

Commit

Permalink
feat: add the ability to start wifi on boot
Browse files Browse the repository at this point in the history
When enabled, this will ensure there is a platform-specific auto.sh file. Once available, it will inject a start-on-boot stanza for the wifi pak, which will then enable the wireless network on boot (necessary on the Brick).

Closes #14
  • Loading branch information
josegonzalez committed Feb 8, 2025
1 parent a876bda commit fb652e0
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 65 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ bin/*
!bin/.gitkeep
res/fonts/*
!res/fonts/.gitkeep

!bin/wifi-enable
!bin/wifi-enabled
!bin/on-boot
12 changes: 12 additions & 0 deletions bin/on-boot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
bindir="$(dirname "$0")"

main() {
"$bindir/wifi-enable" &
}

if [ -f "$LOGS_PATH/Wifi.txt" ]; then
mv "$LOGS_PATH/Wifi.txt" "$LOGS_PATH/Wifi.txt.old"
fi

main "$@" >"$LOGS_PATH/Wifi.txt" 2>&1
45 changes: 45 additions & 0 deletions bin/wifi-enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
bindir="$(dirname "$0")"
JQ="$bindir/jq-arm"
if uname -m | grep -q '64'; then
JQ="$bindir/jq-arm64"
fi

main() {
if [ "$PLATFORM" = "tg3040" ] && [ -z "$DEVICE" ]; then
export DEVICE="brick"
export PLATFORM="tg5040"
fi

echo "Preparing to enable wifi..."
if [ "$PLATFORM" = "tg5040" ]; then
SYSTEM_JSON_PATH="/mnt/UDISK/system.json"
chmod +x "$JQ"
"$JQ" '.wifi = 1' "$SYSTEM_JSON_PATH" >"/tmp/system.json.tmp"
mv "/tmp/system.json.tmp" "$SYSTEM_JSON_PATH"
fi

echo "Unblocking wireless..."
rfkill unblock wifi || true

echo "Starting wpa_supplicant..."
if [ "$PLATFORM" = "tg5040" ]; then
/etc/init.d/wpa_supplicant stop || true
/etc/init.d/wpa_supplicant start || true
( (udhcpc -i wlan0 -q &) &)
elif [ "$PLATFORM" = "rg35xxplus" ]; then
ip link set wlan0 up
iw dev wlan0 set power_save off

systemctl start wpa_supplicant || true
systemctl start systemd-networkd || true
netplan apply
else
echo "$PLATFORM is not a supported platform for Wifi.pak"
return 1
fi

ifconfig wlan0 up || true
}

main "$@"
35 changes: 35 additions & 0 deletions bin/wifi-enabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
bindir="$(dirname "$0")"
JQ="$bindir/jq-arm"
if uname -m | grep -q '64'; then
JQ="$bindir/jq-arm64"
fi

main() {
SYSTEM_JSON_PATH="/mnt/UDISK/system.json"
if [ -f "$SYSTEM_JSON_PATH" ]; then
chmod +x "$JQ"
wifi_enabled="$("$JQ" '.wifi' "$SYSTEM_JSON_PATH")"
if [ "$wifi_enabled" != "1" ]; then
return 1
fi
fi

wifi_status="$(rfkill list wifi || true)"
if echo "$wifi_status" | grep -q "blocked: yes"; then
return 1
fi

if ! pgrep wpa_supplicant; then
return 1
fi

# check if the device is on
if [ "$(cat /sys/class/net/wlan0/flags 2>/dev/null)" != "0x1003" ]; then
return 1
fi

return 0
}

main "$@"
129 changes: 64 additions & 65 deletions launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ main_screen() {
minui_list_file="/tmp/minui-list"
rm -f "$minui_list_file"
touch "$minui_list_file"

start_on_boot=false
if will_start_on_boot; then
start_on_boot=true
fi

echo "Enabled: false" >>"$minui_list_file"
echo "Start on boot: $start_on_boot" >>"$minui_list_file"
echo "Enable" >>"$minui_list_file"
echo "Toggle start on boot" >>"$minui_list_file"

ip_address="N/A"
if wifi_enabled; then
if "$progdir/bin/wifi-enabled"; then
echo "Enabled: true" >"$minui_list_file"
echo "Start on boot: $start_on_boot" >>"$minui_list_file"
echo "Disable" >>"$minui_list_file"
echo "Connect to network" >>"$minui_list_file"
echo "Toggle start on boot" >>"$minui_list_file"
fi

ssid="N/A"
Expand Down Expand Up @@ -56,6 +66,7 @@ main_screen() {

if [ "$ssid" != "N/A" ] && [ "$ip_address" != "N/A" ]; then
echo "Enabled: true" >"$minui_list_file"
echo "Start on boot: $start_on_boot" >>"$minui_list_file"
echo "SSID: $ssid" >>"$minui_list_file"
if [ "$ip_address" = "N/A" ]; then
echo "IP: N/A" >>"$minui_list_file"
Expand All @@ -65,6 +76,7 @@ main_screen() {
fi
echo "Disable" >>"$minui_list_file"
echo "Connect to network" >>"$minui_list_file"
echo "Toggle start on boot" >>"$minui_list_file"
fi
fi

Expand Down Expand Up @@ -162,6 +174,29 @@ show_message() {
fi
}

disable_start_on_boot() {
sed -i '/wifi-start-on-boot/d' "$SDCARD_PATH/.userdata/$PLATFORM/auto.sh"
return 0
}

enable_start_on_boot() {
if [ ! -f "$SDCARD_PATH/.userdata/$PLATFORM/auto.sh" ]; then
echo '#!/bin/sh' >"$SDCARD_PATH/.userdata/$PLATFORM/auto.sh"
echo '' >>"$SDCARD_PATH/.userdata/$PLATFORM/auto.sh"
fi

echo "test -f \"\$SDCARD_PATH/Tools/\$PLATFORM/Wifi.pak/bin/on-boot\" && \"\$SDCARD_PATH/Tools/\$PLATFORM/Wifi.pak/bin/on-boot\" # wifi-start-on-boot" >>"$SDCARD_PATH/.userdata/$PLATFORM/auto.sh"
chmod +x "$SDCARD_PATH/.userdata/$PLATFORM/auto.sh"
return 0
}

will_start_on_boot() {
if grep -q "wifi-start-on-boot" "$SDCARD_PATH/.userdata/$PLATFORM/auto.sh" >/dev/null 2>&1; then
return 0
fi
return 1
}

write_config() {
echo "Generating wpa_supplicant.conf..."
cp "$progdir/res/wpa_supplicant.conf.tmpl" "$progdir/res/wpa_supplicant.conf"
Expand Down Expand Up @@ -237,65 +272,6 @@ write_config() {
fi
}

wifi_enable() {
echo "Preparing to enable wifi..."
if [ "$PLATFORM" = "tg5040" ]; then
SYSTEM_JSON_PATH="/mnt/UDISK/system.json"
chmod +x "$JQ"
"$JQ" '.wifi = 1' "$SYSTEM_JSON_PATH" >"/tmp/system.json.tmp"
mv "/tmp/system.json.tmp" "$SYSTEM_JSON_PATH"
fi

echo "Unblocking wireless..."
rfkill unblock wifi || true

echo "Starting wpa_supplicant..."
if [ "$PLATFORM" = "tg5040" ]; then
/etc/init.d/wpa_supplicant stop || true
/etc/init.d/wpa_supplicant start || true
( (udhcpc -i wlan0 -q &) &)
elif [ "$PLATFORM" = "rg35xxplus" ]; then
ip link set wlan0 up
iw dev wlan0 set power_save off

systemctl start wpa_supplicant || true
systemctl start systemd-networkd || true
netplan apply
else
show_message "$PLATFORM is not a supported platform" 2
return 1
fi

ifconfig wlan0 up || true
}

wifi_enabled() {
SYSTEM_JSON_PATH="/mnt/UDISK/system.json"
if [ -f "$SYSTEM_JSON_PATH" ]; then
chmod +x "$JQ"
wifi_enabled="$("$JQ" '.wifi' "$SYSTEM_JSON_PATH")"
if [ "$wifi_enabled" != "1" ]; then
return 1
fi
fi

wifi_status="$(rfkill list wifi || true)"
if echo "$wifi_status" | grep -q "blocked: yes"; then
return 1
fi

if ! pgrep wpa_supplicant; then
return 1
fi

# check if the device is on
if [ "$(cat /sys/class/net/wlan0/flags 2>/dev/null)" != "0x1003" ]; then
return 1
fi

return 0
}

wifi_off() {
echo "Preparing to toggle wifi off..."
if [ "$PLATFORM" = "tg5040" ]; then
Expand Down Expand Up @@ -338,7 +314,7 @@ wifi_on() {
return 1
fi

if ! wifi_enable; then
if ! "$progdir/bin/wifi-enable"; then
return 1
fi

Expand All @@ -358,9 +334,12 @@ wifi_on() {
}

network_loop() {
if ! wifi_enabled; then
if ! "$progdir/bin/wifi-enabled"; then
show_message "Enabling wifi..." forever
wifi_enable
if ! "$progdir/bin/wifi-enable"; then
show_message "Failed to enable wifi!" 2
return 1
fi
fi

next_screen="main"
Expand Down Expand Up @@ -465,7 +444,10 @@ main() {
fi
elif echo "$selection" | grep -q "^Enable$"; then
show_message "Enabling wifi..." forever
wifi_enable
if ! "$progdir/bin/wifi-enable"; then
show_message "Failed to enable wifi!" 2
continue
fi
sleep 2
elif echo "$selection" | grep -q "^Refresh connection$"; then
show_message "Disconnecting from wifi..." forever
Expand All @@ -475,7 +457,10 @@ main() {
exit 1
fi
show_message "Refreshing connection..." forever
wifi_enable
if ! "$progdir/bin/wifi-enable"; then
show_message "Failed to enable wifi!" 2
continue
fi
sleep 2
elif echo "$selection" | grep -q "^Disable$"; then
show_message "Disconnecting from wifi..." forever
Expand All @@ -484,6 +469,20 @@ main() {
killall sdl2imgshow 2>/dev/null || true
exit 1
fi
elif echo "$selection" | grep -q "^Toggle start on boot$"; then
if will_start_on_boot; then
show_message "Disabling start on boot..." forever
if ! disable_start_on_boot; then
show_message "Failed to disable start on boot!" 2
continue
fi
else
show_message "Enabling start on boot..." forever
if ! enable_start_on_boot; then
show_message "Failed to enable start on boot!" 2
continue
fi
fi
fi
done
killall sdl2imgshow 2>/dev/null || true
Expand Down

0 comments on commit fb652e0

Please sign in to comment.