From e5d121472b51750fd26c566ef6bcaa483a401e00 Mon Sep 17 00:00:00 2001 From: nagumii Date: Thu, 2 Jan 2025 00:19:12 +0200 Subject: [PATCH] Move updatemeshtastic tool a function of femto-meshtastic-config.sh called with -m - the rest is the same. --- .../usr/local/bin/button.sh | 4 +- .../usr/local/bin/femto-config | 64 ++------- .../usr/local/bin/femto-meshtasticd-config.sh | 54 +++++++- .../usr/local/bin/femto-network-config.sh | 131 ++++++++++++++++++ .../usr/local/bin/femto-wifi-config.sh | 81 ----------- ...stic.sh => updatemeshtastic.sh.deprecated} | 0 .../usr/local/bin/usbconfig.sh | 6 +- 7 files changed, 196 insertions(+), 144 deletions(-) create mode 100644 foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh delete mode 100755 foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-config.sh rename foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/{updatemeshtastic.sh => updatemeshtastic.sh.deprecated} (100%) mode change 100755 => 100644 diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh index 52c2791..1adc0d3 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/button.sh @@ -21,10 +21,10 @@ evtest --grab /dev/input/event0 | while read line; do wifi_status=$(cat /root/.portduino/default/prefs/config.proto | protoc --decode_raw | awk '/4 {/, /}/ {if ($1 == "1:") print $2}') if [ "$wifi_status" -eq 1 ]; then logger "$duration second button press detected. Toggling wifi off." - /usr/local/bin/updatemeshtastic.sh "--set network.wifi_enabled false" 10 "Button toggle wifi" + femto-meshtasticd-config.sh -m "--set network.wifi_enabled false" 10 "Button toggle wifi" else logger "$duration second button press detected. Toggling wifi on." - /usr/local/bin/updatemeshtastic.sh "--set network.wifi_enabled true" 10 "Button toggle wifi" + femto-meshtasticd-config.sh -m "--set network.wifi_enabled true" 10 "Button toggle wifi" fi else logger "$duration second button press detected. Rebooting." diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config index 071e8a8..5a30fa7 100644 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config @@ -38,26 +38,8 @@ networking_settings() { case $option in 1) - wifi_country=$(grep -m 1 "^country=" "$wpa_supplicant_conf" | cut -d '=' -f 2) - wifi_ssid=$(grep -m 1 '^\s*ssid=' "$wpa_supplicant_conf" | cut -d '"' -f 2) - wifi_psk=$(grep -m 1 '^\s*psk=' "$wpa_supplicant_conf" | cut -d '"' -f 2) - if [ "$(cat /root/.portduino/default/prefs/config.proto | protoc --decode_raw | awk '/4 {/, /}/ {if ($1 == "1:") print $2}')" -eq 1 ]; then - mesh_wifi_status="enabled" - else - mesh_wifi_status="disabled" - fi - dialog --title "$title2" --msgbox "\ -SSID: $wifi_ssid\n\ -Password: $wifi_psk\n\ -Country: $wifi_country\n\ -Meshtastic wifi: $mesh_wifi_status\n\ -\n\ -Connected to: $(iwconfig 2>/dev/null | grep -i 'ESSID' | awk -F 'ESSID:"' '{print $2}' | awk -F '"' '{print $1}')\n\ -Signal Strength: $(iwconfig 2>/dev/null | grep -i 'Signal level' | awk -F 'Signal level=' '{print $2}' | awk '{print $1}')\n\ -MAC address: $(ifconfig wlan0 | grep ether | awk '{print $2}')\n\ -Current IP: $(hostname -I | awk '{print $1}')\n\ -For more details, enter \`iwconfig\`. - " 0 0 + echo "Getting wifi configuration..." + dialog --msgbox "$(femto-network-config.sh -w)" 0 0 ;; 2) local wifi_ssid="" @@ -66,9 +48,9 @@ For more details, enter \`iwconfig\`. wifi_ssid=$(dialog --title "$title2" --inputbox "Enter Wi-Fi SSID:" 8 40 3>&1 1>&2 2>&3) wifi_psk=$(dialog --title "$title2" --inputbox "Enter Wi-Fi Password:" 8 40 3>&1 1>&2 2>&3) wifi_country=$(dialog --title "$title2" --inputbox "Enter Country Code (e.g., US, DE) - optional:" 9 40 3>&1 1>&2 2>&3) - cmd="femto-wifi-config.sh -s \"$wifi_ssid\" -p \"$wifi_psk\"" + cmd="femto-network-config.sh -s \"$wifi_ssid\" -p \"$wifi_psk\"" [ -n "$wifi_country" ] && cmd="$cmd -c \"$wifi_country\"" - echo $cmd + cmd="$cmd -r" if [ -z "$wifi_ssid" ]; then dialog --title "$title2" --msgbox "\nSSID is required." 8 40 return @@ -79,36 +61,16 @@ For more details, enter \`iwconfig\`. 3) dialog --title "$title2" --yesno "Wifi and Ethernet will be restarted.\nMeshtastic wifi setting will be set to ON." 7 50 if [ $? -eq 0 ]; then #unless cancel/no - femto-wifi-config.sh -r + femto-network-config.sh -r fi ;; 4) - dialog --title "Ethernet (eth0) settings" --msgbox "\ -IPv4 Address: $(ifconfig eth0 | grep 'inet ' | awk '{print $2}')\n\ -IPv6 Address: $(ifconfig eth0 | grep 'inet6 ' | awk '{print $2}')\n\ -MAC Address: $(ifconfig eth0 | grep 'ether ' | awk '{print $2}')" 0 0 + echo "Getting ethernet configuration..." + dialog --title "Ethernet (eth0) settings" --msgbox "$(femto-network-config.sh -e)" 0 0 ;; 5) echo "Testing internet connection..." - # Define ping targets and initialize counter - targets=("1.1.1.1" "8.8.8.8") - successful=0 - count=0 - total=0 - - # Ping each target 5 times and count successful pings - for target in "${targets[@]}"; do - count=$(ping -c 5 -W 1 "$target" | grep -c 'bytes from') # Count lines with successful pings - successful=$((successful + count)) - ((total+=5)) - done - - # Use dialog to display message based on successful pings count - if [ "$successful" -eq 0 ]; then - dialog --msgbox "No internet connection detected." 6 50 - else - dialog --msgbox "Internet connection is up.\n\nPinged $(echo "${targets[*]}" | sed 's/ /, /g').\nReceived $successful/$total responses." 8 40 - fi + dialog --title "Ethernet (eth0) settings" --msgbox "$(femto-network-config.sh -t)" 8 40 ;; 6) return @@ -166,7 +128,7 @@ misc_settings() { 1) femto-set-timezone.sh ;; - 2) + 2) # the contents of this should probably moved to a script dialog --title "$title" --msgbox "Amateur radio support can be added in kernel for:\n\n\ Amateur Radio AX.25 Level 2 protocol\n\ AX.25 DAMA Slave support\n\ @@ -309,7 +271,7 @@ meshtasticd_settings() { 7 "Clear admin keys" \ 8 "Enable legacy admin channel" \ 9 "Disable legacy admin channel" \ - 10 "Upgrade Meshtasticd" \ + 10 "Upgrade/install Meshtasticd" \ 11 "Uninstall Meshtasticd" \ 12 "Back to Main Menu" 3>&1 1>&2 2>&3) @@ -379,12 +341,11 @@ meshtasticd_settings() { fi ;; 11) - dialog --title "$title" --yesno "Uninstall Meshtasticd?" 0 0 + dialog --title "$title" --yesno "Uninstall Meshtasticd?\n\nFoxbuntu was designed with Meshtasticd integration in mind, and may behave unexpectedly if Meshtasticd is removed." 0 0 if [ $? -eq 0 ]; then femto-meshtasticd-config.sh -r dialog --msgbox "Meshtasticd uninstalled. To purge all remnants, run \`sudo apt purge\`.\nAfter reinstallation but before first launch, run \`sudo runonce.sh\`." 8 40 fi - pause ;; 12) return @@ -425,8 +386,7 @@ help_menu() { ;; 4) femto-license.sh -F - echo -e "\nPress any key to continue...\n" - read -n 1 -s -r + pause ;; 5) dialog --msgbox "$(femto-license.sh -m)" 0 0 diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-meshtasticd-config.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-meshtasticd-config.sh index 0f51ddd..3cbe06f 100644 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-meshtasticd-config.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-meshtasticd-config.sh @@ -18,6 +18,8 @@ Options are: -d Disable legacy admin channel -u Upgrade Meshtasticd -r Uninstall Meshtasticd +-m Meshtastic update tool. Syntax: \`femto-meshtasticd-config.sh -m \"--set security.admin_channel_enabled false\" 10 \"Disable legacy admin\"\` + Will retry the \`--set security.admin_channel_enabled false\` command until successful or up to 10 times, and tag status reports with \`Disable legacy admin\` via echo and to system log. EOF ) @@ -27,8 +29,44 @@ if [ $# -eq 0 ]; then exit 1 fi +meshtastic_update() { + local command="$1" + local attempts=$2 + local ref="$3: " + echo "Submitting to Meshtastic..." + for retries in $(seq 1 $attempts); do + local output=$(meshtastic --host $command) #>&2 lets meshtastic's output display on screen + echo $output + logger $output + if echo "$output" | grep -qiE "Abort|invalid|Error|refused|Errno"; then + if [ "$retries" -lt $attempts ]; then + local msg="${ref:+$ref }Meshtastic update failed, retrying ($(($retries + 1))/$attempts)..." + echo "$msg" + logger "$msg" + sleep 2 # Add a small delay before retrying + fi + else + local success="true" + msg="${ref:+$ref }Meshtastic update successful!" + echo "$msg" + logger "$msg" + if [ -n "$external" ]; then + exit 0 + fi + fi + done + if [ -z "$success" ]; then + msg="${ref:+$ref }Meshtastic update failed." + echo "$msg" + logger "$msg" + if [ -n "$external" ]; then + exit 1 + fi + fi +} + # Parse options -while getopts ":hgkl:q:va:cedur" opt; do +while getopts ":hgkl:q:va:cedurm" opt; do case ${opt} in h) # Option -h (help) echo -e "$help" @@ -61,7 +99,7 @@ while getopts ":hgkl:q:va:cedur" opt; do fi ;; q) # Option -q (set config URL) - updatemeshtastic.sh "--seturl $OPTARG" 10 "Set URL" + meshtastic_update "--seturl $OPTARG" 10 "Set URL" ;; v) # Option -v (view admin keys) echo "Getting admin keys..." @@ -69,16 +107,16 @@ while getopts ":hgkl:q:va:cedur" opt; do echo "${keys:-none}" ;; a) # Option -a (add admin key) - updatemeshtastic.sh "--set security.admin_key base64:$OPTARG" 10 "Set admin key" + meshtastic_update "--set security.admin_key base64:$OPTARG" 10 "Set admin key" ;; c) # Option -c (clear admin key list) - updatemeshtastic.sh "--set security.admin_key 0" 10 "Set admin key" + meshtastic_update "--set security.admin_key 0" 10 "Set admin key" ;; e) # Option -e (enable legacy admin) - updatemeshtastic.sh "--set security.admin_channel_enabled true" 10 "Enable legacy admin" + meshtastic_update "--set security.admin_channel_enabled true" 10 "Enable legacy admin" ;; d) # Option -d (disable legacy admin) - updatemeshtastic.sh "--set security.admin_channel_enabled false" 10 "Disable legacy admin" + meshtastic_update "--set security.admin_channel_enabled false" 10 "Disable legacy admin" ;; u) # Option -u (upgrade meshtasticd) apt update @@ -87,6 +125,10 @@ while getopts ":hgkl:q:va:cedur" opt; do r) # Option -r (uninstall meshtasticd) apt remove meshtasticd ;; + m) + external="true" # set a variable so the function knows it was called by an an external script and not locally + meshtastic_update "$2" $3 "$4" + ;; \?) # Invalid option echo "Invalid option: -$OPTARG" echo -e "$help" diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh new file mode 100644 index 0000000..1af4d6e --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-network-config.sh @@ -0,0 +1,131 @@ +#!/bin/bash +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root. Try \`sudo femto-wifi-config\`." + exit 1 +fi + +wpa_supplicant_conf="/etc/wpa_supplicant/wpa_supplicant.conf" +help=$(cat </dev/null | grep -i 'ESSID' | awk -F 'ESSID:"' '{print $2}' | awk -F '"' '{print $1}')\n\ +Signal Strength: $(iwconfig 2>/dev/null | grep -i 'Signal level' | awk -F 'Signal level=' '{print $2}' | awk '{print $1}')\n\ +MAC address: $(ifconfig wlan0 | grep ether | awk '{print $2}')\n\ +Current IP: $(hostname -I | awk '{print $1}')\n\ +For more details, enter \`iwconfig\`." 0 0 + ;; + t) # Option -t (test internet connection) + # Define ping targets and initialize counter + targets=("1.1.1.1" "8.8.8.8") + successful=0 + count=0 + total=0 + # Ping each target 5 times and count successful pings + for target in "${targets[@]}"; do + count=$(ping -c 5 -W 1 "$target" | grep -c 'bytes from') # Count lines with successful pings + successful=$((successful + count)) + ((total+=5)) + done + # Use dialog to display message based on successful pings count + if [ "$successful" -eq 0 ]; then + echo "No internet connection detected." + else + echo "Internet connection is up.\n\nPinged $(echo "${targets[*]}" | sed 's/ /, /g').\nReceived $successful/$total responses." + fi + ;; + r) # Option -r (restart wifi) + updated_wifi="true" + ;; + \?) # Invalid option + echo "Invalid option: -$OPTARG" + echo -e "$help" + exit 1 + ;; + :) # Missing argument for option + echo "Option -$OPTARG requires a setting." + echo -e "$help" + exit 1 + ;; + esac +done + +if [ "$updated_wifi" = true ]; then + systemctl restart wpa_supplicant + wpa_cli -i wlan0 reconfigure # <-------- add watch for FAIL response, error out + timeout 30s dhclient -v + echo " Wifi restarted. Enabling Meshtastic wifi setting." + femto-meshtasticd-config.sh -m "--set network.wifi_enabled true" 10 "USB config" #| tee -a /tmp/femtofox-config.log + if [ $? -eq 1 ]; then + echo "Update of Meshtastic FAILED." + exit 1 + else + echo "Updated Meshtastic successfully." + exit 0 + fi +fi diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-config.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-config.sh deleted file mode 100755 index fdb3322..0000000 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-config.sh +++ /dev/null @@ -1,81 +0,0 @@ -#!/bin/bash -if [[ $EUID -ne 0 ]]; then - echo "This script must be run as root. Try \`sudo femto-wifi-config\`." - exit 1 -fi - -wpa_supplicant_conf="/etc/wpa_supplicant/wpa_supplicant.conf" -help=$(cat <&1 | tee -a /tmp/femtofox-config.log - /usr/local/bin/updatemeshtastic.sh "--set network.wifi_enabled true" 10 "USB config" #| tee -a /tmp/femtofox-config.log + femto-meshtasticd-config.sh -m "--set network.wifi_enabled true" 10 "USB config" #| tee -a /tmp/femtofox-config.log if [ $? -eq 1 ]; then log_message "Update of Meshtastic FAILED." else @@ -247,7 +247,7 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then if [ "$update_meshtastic_url" != "" ]; then log_message "Connecting to Meshtastic radio and submitting $update_meshtastic_url" - /usr/local/bin/updatemeshtastic.sh "$update_meshtastic_url" 10 "USB config" #| tee -a /tmp/femtofox-config.log + femto-meshtasticd-config.sh -m "$update_meshtastic_url" 10 "USB config" #| tee -a /tmp/femtofox-config.log if [ $? -eq 1 ]; then log_message "Update of Meshtastic FAILED." else @@ -257,7 +257,7 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then if [ "$update_meshtastic_security" != "" ]; then log_message "Connecting to Meshtastic radio and submitting $update_meshtastic_security" - /usr/local/bin/updatemeshtastic.sh "$update_meshtastic_security" 10 "USB config" #| tee -a /tmp/femtofox-config.log + femto-meshtasticd-config.sh -m "$update_meshtastic_security" 10 "USB config" #| tee -a /tmp/femtofox-config.log if [ $? -eq 1 ]; then log_message "Update of Meshtastic FAILED." else