From 9d1becc89f42e47acf8c4b299005ee4d79476110 Mon Sep 17 00:00:00 2001 From: nagumii Date: Thu, 23 Jan 2025 20:57:35 +0200 Subject: [PATCH] Add activity LED setting, with menu and usbconfig support --- assets/femtofox-config.txt | 31 ++- .../usr/local/bin/femto-boot-complete.sh | 3 +- .../usr/local/bin/femto-config | 205 +++++------------- .../usr/local/bin/femto-usb-config-tool.sh | 41 ++-- .../usr/local/bin/femto-utils.sh | 194 +++++++++++++++++ 5 files changed, 290 insertions(+), 184 deletions(-) create mode 100644 foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-utils.sh diff --git a/assets/femtofox-config.txt b/assets/femtofox-config.txt index 401ad63..c197c5a 100644 --- a/assets/femtofox-config.txt +++ b/assets/femtofox-config.txt @@ -1,10 +1,14 @@ ## femtofox-config.txt -## Help: https://github.com/noon92/femtofox/usbconfig.md +## Help: https://github.com/femtofox/femtofox/usbconfig.md ## To set a setting, uncomment a line by removing its preceeding `#` ## All settings are optional - remove any you don't want to set. ### Foxbuntu settings +## Activity LED setting. CaSe SeNsItIvE +## Options are "enable" and "disable" +#act_led="disable" + ## Localization settings. CaSe SeNsItIvE ## List of timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones #wifi_country="US" @@ -18,23 +22,26 @@ ### Meshtastic settings ## Radio module to use. Options are: -## `ebyte-e22-900m30s` -## `ebyte-e22-900m22s` -## `ebyte-e80-900m22s` (experimental) -## `heltec-ht-ra62` -## `seeed-wio-sx1262` -## `waveshare-sx126x-xxxm` -## `ai-thinker-ra-01sh` -## `sx1262_tcxo` (for Ebyte E22-900M30SM, Ebyte E22-900M22S, Heltec HT-RA62, Seeed Wio SX1262) -## `sx1262_xtal` (for AiThinker RA01SH, and Waveshare Pi hat) -## `lr1121_tcxo` (experimental, for Ebyte E80-900M2213S) -## `none` (for simulated radio) +## "ebyte-e22-900m30s" +## "ebyte-e22-900m22s" +## "ebyte-e80-900m22s" (experimental) +## "heltec-ht-ra62" +## "seeed-wio-sx1262" +## "waveshare-sx126x-xxxm" +## "ai-thinker-ra-01sh" +## "sx1262_tcxo" (for Ebyte E22-900M30SM, Ebyte E22-900M22S, Heltec HT-RA62, Seeed Wio SX1262) +## "sx1262_xtal" (for AiThinker RA01SH, and Waveshare Pi hat) +## "lr1121_tcxo" (experimental, for Ebyte E80-900M2213S) +## "none" (for simulated radio) #meshtastic_lora_radio="radio_name" ## URL from Meshtastic QR code to configure channels #meshtastic_url="https://meshtastic.org/e/..." ## Meshtastic security settings. CaSe SeNsItIvE +#meshtastic_public_key="XX=" +#meshtastic_private_key="XX=" + ## The admin key list can contain up to three keys - if more are added they will be ignored. ## The USB configuration tool supports clearing the admin key list, after which you will need to re-add your admin key/s in a second operation. ## To clear the admin key list, enter `meshtastic_admin_key="clear"` diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-boot-complete.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-boot-complete.sh index 2875bc7..437d5b1 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-boot-complete.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-boot-complete.sh @@ -1,8 +1,7 @@ #!/bin/bash echo "Boot complete" -echo "Disabling ACT LED..." -echo "none" > /sys/class/leds/work/trigger & +femto-utils.sh -a & sleep 1 # blink successful boot code 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 9b50a45..935d565 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-config @@ -163,141 +163,12 @@ femto_pinouts() { done } -# enable/disable system logging -logging() { - enable_logging() { - chattr -i /var/log - msg="Enabling system logging by making /var/log writable." - logger $msg - echo $msg - return 0 - } - disable_logging() { - msg="Disabling system logging by making /var/log immutable." - logger $msg - echo $msg - chattr +i /var/log - return 0 - } - check_logging() { - lsattr -d /var/log | grep -q 'i' && echo -e "\033[0;31mdisabled\033[0m" || echo -e "\033[0;34menabled\033[0m" - } - if [ "$1" = "true" ]; then - disable_logging - elif [ "$1" = "false" ]; then - enable_logging - elif [ "$1" = "check" ]; then - check_logging - elif [ -z "$1" ]; then - while true; do - choice=$(dialog --no-collapse --colors --title "Logging" --menu "Before deploying a Femtofox, it is advisable to disable logging to reduce SD card wear.\nCurrent state: $(replace_colors "$(check_logging)")" 12 50 2 \ - 1 "Enable (default)" \ - 2 "Disable" 3>&1 1>&2 2>&3) - exit_status=$? # This line checks the exit status of the dialog command - if [ $exit_status -ne 0 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog - break - fi - case $choice in - 1) enable_logging ;; - 2) disable_logging ;; - esac - done - else - echo "$1 is not a valid argument for -L. Options are \"true\" and \"false\"." - fi -} - -# at some point this will be moved to a different script, and have many functions instead of one giant dump -system_info() { - local cpu_model="$(dmesg | grep "soc_id" | sed -n 's/.*soc_id: //p')" - local cpu_architecture="$(uname -m) ($(dpkg --print-architecture) $(python3 -c "import platform; print(platform.architecture()[0])"))" - local cpu_temp="$(echo "scale=1; $(cat /sys/class/thermal/thermal_zone0/temp) / 1000" | bc)°C" - local cpu_speed="$(lscpu | grep "CPU min MHz" | awk '{print int($4)}')-$(lscpu | grep "CPU max MHz" | awk '{print int($4)}')mhz" - local cpu_serial="$(awk '/Serial/ {print $3}' /proc/cpuinfo)" - - local microsd_size="$(df --block-size=1 / | awk 'NR==2 {total=$2; avail=$4; total_human=sprintf("%.2f", total/1024/1024/1024); avail_human=sprintf("%.2f", avail/1024/1024/1024); printf "%.2f GB (%.2f%% free)", total_human, (avail/total)*100}')" - local memory="$(free -m | awk 'NR==2{printf "%d MB (%.2f%% free)\n", $2, 100 - (($3/$2)*100)}')" - local swap="$(free -m | awk 'NR==3 {if ($2 > 1000) {printf "%.2f GB (%.2f%% free)", $2/1024, ($4/$2)*100} else {printf "%d MB (%.2f%% free)", $2, ($4/$2)*100}}')" - local mounted_drives="$([ "$(for dir in /mnt/*/; do echo -n "/mnt${dir#"/mnt"} "; done)" ] && echo "Mounted drives: $(for dir in /mnt/*; do echo -n "/mnt${dir#"/mnt"} "; done | sed 's/\/$//')")" - - local os_version="Foxbuntu v$(grep -oP 'major=\K[0-9]+' /etc/foxbuntu-release).$(grep -oP 'minor=\K[0-9]+' /etc/foxbuntu-release)$(output=$(grep -o 'patch=[1-9][0-9]*' /etc/foxbuntu-release | cut -d= -f2) && [ -n "$output" ] && echo ".$output")$(grep -oP 'hotfix=\K[a-z]+' /etc/foxbuntu-release) ($(lsb_release -d | awk -F'\t' '{print $2}') $(lsb_release -c | awk -F'\t' '{print $2}'))" - local system_uptime="$(uptime -p | awk '{$1=""; print $0}' | sed -e 's/ day\b/d/g' -e 's/ hour\b/h/g' -e 's/ hours\b/h/g' -e 's/ minute\b/m/g' -e 's/ minutes\b/m/g' | sed 's/,//g')" - local logging_enabled="$(logging "check" | sed 's/\x1b\[[0-9;]*m//g')" - local kernel_active_modules="$(lsmod | awk 'NR>1 {print $1}' | tr '\n' ' ' && echo)" - local kernel_boot_modules="$(modules=$(sed -n '6,$p' /etc/modules | sed ':a;N;$!ba;s/\n/, /g;s/, $//'); [ -z "$modules" ] && echo "none" || echo "$modules")" - - local wifi_status="$(replace_colors "$(femto-network-config.sh -w | grep -v '^$' | grep -v '^Hostname')")" #remove hostname line, as it's identical to the one in ethernet settings - local eth_status="$(replace_colors "$(femto-network-config.sh -e)")" - - local usb_mode="$(cat /sys/devices/platform/ff3e0000.usb2-phy/otg_mode)" - local spi0_state="$([ "$(awk -F= '/^SPI0_M0_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" - local spi0_speed="$((0x$(xxd -p /sys/firmware/devicetree/base/spi@ff500000/spidev@0/spi-max-frequency | tr -d '\n')))" - local i2c3_state="$([ "$(awk -F= '/^I2C3_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" - local i2c3_speed="$(awk -F= '/^I2C3_M1_SPEED/ {print $2}' /etc/luckfox.cfg)" - local uart3_state="$([ "$(awk -F= '/^UART3_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" - local uart4_state="$([ "$(awk -F= '/^UART4_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" - - local lora_radio="$(replace_colors "$(femto-meshtasticd-config.sh -k)")" - local usb_devices="$(lsusb | grep -v 'root hub' | awk 'NR>0{printf "USB: "; for(i=7;i<=NF;i++) printf "%s ", $i; print ""} END {if (NR == 0) printf "USB: None detected"}')" - local i2c_addresses="$(i2cdetect -y 3 | awk 'NR>1 {for(i=2;i<=17;i++) if ($i == "ff" || $i == "UU") { printf "0x%02x ", (i-2) + (NR-2)*16} }' | tr -d '\n' )" - - local meshtasticd_service_status="$(replace_colors "$(femto-meshtasticd-config.sh -S)")" - local meshtasticd_info="$(replace_colors "$(femto-meshtasticd-config.sh -i)")" - echo -e "\ - Femtofox\n\ -Core: $(cat /sys/firmware/devicetree/base/model)\n\ -Operating System: $os_version\n\ -Kernel version: $(uname -r)\n\ -Uptime: $system_uptime\n\ -Logging: $logging_enabled\n\ -System time: $(date)\n\ -K modules active: $kernel_active_modules\n\ -K boot modules: $kernel_boot_modules\n\ -\n\ - CPU:\n\ -Model: $cpu_model\n\ -Architecture: $cpu_architecture\n\ -Speed: $cpu_speed x $(nproc) cores\n\ -Temperature: $cpu_temp\n\ -Serial # $cpu_serial\n\ -\n\ - Storage:\n\ -microSD size: $microsd_size\n\ -Memory: $memory\n\ -Swap: $swap\n\ -$mounted_drives\n\ -\n\ - Networking (wlan0 & eth0):\n\ -$wifi_status\n\ ---- -$eth_status\n\ -\n\ - Interfaces:\n\ -USB mode: $usb_mode\n\ -SPI-0 state: $spi0_state\n\ -SPI-0 speed: $spi0_speed\n\ -i2c-3 state: $i2c3_state\n\ -i2c-3 speed: $i2c3_speed\n\ -UART-3 state: $uart3_state\n\ -UART-4 state: $uart4_state\n\ -\n\ - Attached devices:\n\ -LoRa radio: $lora_radio\n\ -$usb_devices\n\ -i2c devices: $i2c_addresses\n\ -\n\ - Meshtasticd:\n\ -Service status: $meshtasticd_service_status\n\ -$meshtasticd_info\ -" -} - # utilities utilities() { local title="Utilities" while true; do local option="" - option=$(dialog --no-collapse --cancel-label "Back" --menu "$title" 19 40 4 \ + option=$(dialog --no-collapse --cancel-label "Back" --menu "$title" 21 50 4 \ 1 "System info" \ 2 "Set system time" \ 3 "Kernel modules" \ @@ -306,10 +177,11 @@ utilities() { 6 "Run OEM luckfox-config" \ 7 "Process viewer/manager" \ 8 "Disable/enable logging" \ - 9 "Reboot" \ - 10 "Shutdown" \ + 9 "Disable/enable activity LED" \ + 10 "Reboot" \ + 11 "Shutdown" \ "" "" \ - 11 "Back to Main Menu" 3>&1 1>&2 2>&3) + 12 "Back to Main Menu" 3>&1 1>&2 2>&3) exit_status=$? # This line checks the exit status of the dialog command if [ $exit_status -ne 0 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog @@ -319,7 +191,7 @@ utilities() { case $option in 1) dialog --no-collapse --infobox "Loading system info..." 5 45 - dialog --no-collapse --colors --title "System info" --msgbox "$(system_info)" 0 0 + dialog --no-collapse --colors --title "System info" --msgbox "$(femto-utils.sh -i)" 0 0 ;; 2) femto-set-time.sh ;; # set timezone) 3) # Kernel modules) @@ -328,7 +200,7 @@ utilities() { ;; 4) femto-runonce.sh ;; # Re-run first-boot script) 5) # USB config) - dialog --no-collapse --title "$title" --yesno "The USB configuration tool allows you to set critical Femtofox settings with a text file on a USB flash drive.\n\nFor more information, visit\ngithub.com/noon92/femtofox/blob/main/usb_config.md\n\nThis tool is run automatically at boot.\n\nRun now?" 14 60 + dialog --no-collapse --title "$title" --yesno "The USB configuration tool allows you to set critical Femtofox settings with a text file on a USB flash drive.\n\nFor more information, visit\ngithub.com/femtofox/femtofox/blob/main/usb_config.md\n\nThis tool is run automatically at boot.\n\nRun now?" 14 60 if [ $? -eq 0 ]; then #unless cancel/no femto-usb-config-tool.sh pause @@ -336,23 +208,57 @@ utilities() { ;; 6) luckfox-config ;; 7) htop ;; - 8) logging ;; - 9) # reboot) + 8) # logging) + while true; do + choice=$(dialog --no-collapse --colors --title "Logging" --menu "Before deploying a Femtofox, it is advisable to disable logging to reduce SD card wear.\nCurrent state: $(replace_colors "$(femto-utils.sh -l "check")")" 14 50 2 \ + 1 "Enable (default)" \ + 2 "Disable" \ + "" "" \ + 3 "Return to previous menu" 3>&1 1>&2 2>&3) + exit_status=$? # This line checks the exit status of the dialog command + if [ $exit_status -ne 0 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog + break + fi + case $choice in + 1) femto-utils.sh -l "enable" ;; + 2) femto-utils.sh -l "disable" ;; + 3) break ;; + esac + done + ;; + 9) # logging) + while true; do + choice=$(dialog --no-collapse --colors --title "Logging" --menu "Before deploying a Femtofox, it is advisable to disable the Activity LED to save power.\nCurrent state: $(replace_colors "$(femto-utils.sh -a "check")")" 14 50 2 \ + 1 "Enable (default)" \ + 2 "Disable" \ + "" "" \ + 3 "Return to previous menu" 3>&1 1>&2 2>&3) + exit_status=$? # This line checks the exit status of the dialog command + if [ $exit_status -ne 0 ]; then # Exit the loop if the user selects "Cancel" or closes the dialog + break + fi + case $choice in + 1) femto-utils.sh -a "enable" ;; + 2) femto-utils.sh -a "disable" ;; + 3) break ;; + esac + done + ;; + 10) # reboot) dialog --no-collapse --title "$title" --yesno "Reboot now?\n\nTip: Reboot can also be triggered by long-pressing the BOOT button on the luckfox." 10 45 if [ $? -eq 0 ]; then #unless cancel/no dialog --no-collapse --infobox "Rebooting..." 5 45 - reboot + femto-utils.sh -r fi ;; - 10) # shutdown) + 11) # shutdown) dialog --no-collapse --title "$title" --yesno "Shut down now?\n\nTo power system back on, disconnect/reconnect power.\n\nThe system shutdown command prevents damage to the filesystem from power interrupts, but does not stop power consumption.\n\nTip: You can also shut down the Femtofox by pressing the BOOT button for 5 seconds or by running \`sudo halt\`." 14 60 if [ $? -eq 0 ]; then #unless cancel/no dialog --no-collapse --infobox "Shutting down...\n\nPower consumption will not stop." 5 45 - logger "User requested system halt" - halt + femto-utils.sh -s fi ;; - 11) return ;; + 12) return ;; esac done } @@ -715,9 +621,8 @@ help="If script is run without arguments, femto-config menu will load.\n\ Options are:\n\ -h This message\n\ -l Set LoRa radio cpu_model menu\n\ --w Set Wi-Fi config menu\n\ --L \"true\" Set system logging true/false/check --i Get system info" +-w Set Wi-Fi config menu\ +" while getopts ":hlwL:i" opt; do case ${opt} in h) # Option -h (help) @@ -729,23 +634,15 @@ while getopts ":hlwL:i" opt; do w) # Option -w (Wi-Fi config) wifi_config ;; - L) # Option -L (Logging) - logging $OPTARG - ;; - i) system_info ;; \?) # Unknown option) echo -e "Unknown argument $1.\n$help" ;; - :) # Missing argument for option) - echo "Option -$OPTARG requires a setting." - echo -e "$help" - exit 1 - ;; esac done if [ -n "$1" ]; then exit fi + # Main menu while true; do choice=$(dialog --no-collapse --title "$(date)" --cancel-label "Exit" --menu "Femtofox Config" 15 40 5 \ diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-usb-config-tool.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-usb-config-tool.sh index 2b1ed1e..e6c1c93 100755 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-usb-config-tool.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-usb-config-tool.sh @@ -1,5 +1,10 @@ #!/bin/bash +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root. Try \`sudo femto-meshtasticd-config\`." + exit 1 +fi + mount_point="/mnt/usb" # Set the mount point # Function to log to screen, syslog and logfile to be saved to usb drive @@ -113,6 +118,7 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then fi value=$(echo "$value" | tr -d '"') case "$key" in + act_led) act_led=$(escape_sed "$value") ;; wifi_ssid) wifi_ssid=$(escape_sed "$value") ;; wifi_psk) wifi_psk=$(escape_sed "$value") ;; wifi_country) wifi_country=$(escape_sed "$value") ;; @@ -125,7 +131,7 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then meshtastic_admin_key) meshtastic_admin_key=$(escape_sed "$value") ;; dont_run_if_log_exists) dont_run_if_log_exists=$(escape_sed "$value") ;; esac - done < <(grep -E '^(wifi_ssid|wifi_psk|wifi_country|meshtastic_lora_radio|timezone|meshtastic_url|meshtastic_legacy_admin|meshtastic_public_key|meshtastic_private_key|meshtastic_admin_key|dont_run_if_log_exists)=' "$usb_config") + done < <(grep -E '^(act_led|wifi_ssid|wifi_psk|wifi_country|meshtastic_lora_radio|timezone|meshtastic_url|meshtastic_legacy_admin|meshtastic_public_key|meshtastic_private_key|meshtastic_admin_key|dont_run_if_log_exists)=' "$usb_config") # Check if the log exits and if the dont_run_if_log_exists line is set in the script if $log_exists && [[ $dont_run_if_log_exists = "true" ]]; then @@ -135,14 +141,11 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then done exit_script 1 fi - - # Update wpa_supplicant.conf with the new values, if specified - if [[ -n "$wifi_country" ]]; then - # Update country field - wifi_command="$wifi_command -c \"$wifi_country\"" - log_message "Updating Wi-Fi country in wpa_supplicant.conf to $wifi_country." - found_config="true" - update_wifi="true" + + if [[ -n "$act_led" ]]; then + # Update the ssid in the network block + log_message "Updating Activity LED status to \`$act_led\`." + femto-utils.sh -a "$act_led" fi if [[ -n "$wifi_ssid" ]]; then @@ -161,7 +164,15 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then found_config="true" update_wifi="true" fi - + + # Update wpa_supplicant.conf with the new values, if specified + if [[ -n "$wifi_country" ]]; then + # Update country field + wifi_command="$wifi_command -c \"$wifi_country\"" + log_message "Updating Wi-Fi country in wpa_supplicant.conf to $wifi_country." + found_config="true" + update_wifi="true" + fi #get meshtastic_lora_radio model, if specified, and copy appropriate yaml to /etc/meshtasticd/config.d/ if [[ -n "$meshtastic_lora_radio" ]]; then @@ -225,21 +236,21 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then found_config="true" fi - if [[ -n "$meshtastic_public_key" ]]; then + if [[ -n "$meshtastic_public_key" ]]; then # public key meshtastic_public_key="$(echo "$meshtastic_public_key" | sed 's/\\//g')" # remove weirdo windows characters log_message "Updating Meshtastic public key." found_config="true" meshtastic_security_command+=" -U \"$meshtastic_public_key\"" # add to the command list fi - if [[ -n "$meshtastic_private_key" ]]; then + if [[ -n "$meshtastic_private_key" ]]; then # private key meshtastic_private_key="$(echo "$meshtastic_private_key" | sed 's/\\//g')" # remove weirdo windows characters log_message "Updating Meshtastic private key." found_config="true" meshtastic_security_command+=" -R \"$meshtastic_private_key\"" # add to the command list fi - if [[ -n "$meshtastic_admin_key" ]]; then + if [[ -n "$meshtastic_admin_key" ]]; then # admin key meshtastic_admin_key=$(echo "$meshtastic_admin_key" | sed 's/\\//g') # remove weirdo windows characters if [ "$meshtastic_admin_key" = "clear" ]; then log_message "Clearing Meshtastic admin key list." @@ -252,7 +263,7 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then meshtastic_security_command+=" -A \"$meshtastic_admin_key\"" # add to the command list fi - if [[ -n "$meshtastic_legacy_admin" ]]; then + if [[ -n "$meshtastic_legacy_admin" ]]; then # legacy admin meshtastic_legacy_admin=$(echo "$meshtastic_legacy_admin" | sed 's/\\//g') # remove weirdo windows characters log_message "Updating Meshtastic legacy admin." found_config="true" @@ -260,7 +271,6 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then fi if [ "$found_config" = true ]; then #if we found a config file containing valid data - if [ "$update_wifi" = true ]; then #if wifi config found, restart wifi log_message "Making changes to wifi settings and restarting wifi." wifi_command="$wifi_command -r" @@ -288,7 +298,6 @@ if [ -f "$mount_point/femtofox-config.txt" ]; then done exit_script 1 fi - else log_message "\e[31mUSB drive mounted but femtofox-config.txt not found, ignoring.\e[0m" for _ in {1..3}; do #boot code diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-utils.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-utils.sh new file mode 100644 index 0000000..268fb45 --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-utils.sh @@ -0,0 +1,194 @@ +#!/bin/bash + +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root. Try \`sudo femto-meshtasticd-config\`." + exit 1 +fi + +help=$(cat < /sys/class/leds/work/trigger + grep -qE '^act_led=' /etc/femto.conf && sudo sed -i -E 's/^act_led=.*/act_led=disable/' /etc/femto.conf || echo 'act_led=disable' | sudo tee -a /etc/femto.conf > /dev/null + echo "Disabled activity LED." + exit 0 + elif [ "$1" = "enable" ]; then + echo "activity" > /sys/class/leds/work/trigger + grep -qE '^act_led=' /etc/femto.conf && sudo sed -i -E 's/^act_led=.*/act_led=enable/' /etc/femto.conf || echo 'act_led=enable' | sudo tee -a /etc/femto.conf > /dev/null + echo "Enabled activity LED." + exit 0 + elif [ "$1" = "check" ]; then + if grep -qE '^act_led=enable' /etc/femto.conf; then + echo -e "\033[0;34menabled\033[0m" + elif grep -qE '^act_led=disable' /etc/femto.conf; then + echo -e "\033[0;31mdisabled\033[0m" + else + echo "unknown" + fi + elif [ -z $1 ]; then + echo "check state" + local state=$(act_led "check" 2>/dev/null) + if [[ "$state" =~ "enabled" ]]; then + act_led "enable" + elif [[ "$state" =~ "disabled" ]]; then + act_led "disable" + fi + fi +} + +# at some point this will have many functions instead of one giant dump +system_info() { + local cpu_model="$(dmesg | grep "soc_id" | sed -n 's/.*soc_id: //p')" + local cpu_architecture="$(uname -m) ($(dpkg --print-architecture) $(python3 -c "import platform; print(platform.architecture()[0])"))" + local cpu_temp="$(echo "scale=1; $(cat /sys/class/thermal/thermal_zone0/temp) / 1000" | bc)°C" + local cpu_speed="$(lscpu | grep "CPU min MHz" | awk '{print int($4)}')-$(lscpu | grep "CPU max MHz" | awk '{print int($4)}')mhz" + local cpu_serial="$(awk '/Serial/ {print $3}' /proc/cpuinfo)" + + local microsd_size="$(df --block-size=1 / | awk 'NR==2 {total=$2; avail=$4; total_human=sprintf("%.2f", total/1024/1024/1024); avail_human=sprintf("%.2f", avail/1024/1024/1024); printf "%.2f GB (%.2f%% free)", total_human, (avail/total)*100}')" + local memory="$(free -m | awk 'NR==2{printf "%d MB (%.2f%% free)\n", $2, 100 - (($3/$2)*100)}')" + local swap="$(free -m | awk 'NR==3 {if ($2 > 1000) {printf "%.2f GB (%.2f%% free)", $2/1024, ($4/$2)*100} else {printf "%d MB (%.2f%% free)", $2, ($4/$2)*100}}')" + local mounted_drives="$([ "$(for dir in /mnt/*/; do echo -n "/mnt${dir#"/mnt"} "; done)" ] && echo "Mounted drives: $(for dir in /mnt/*; do echo -n "/mnt${dir#"/mnt"} "; done | sed 's/\/$//')")" + + local os_version="Foxbuntu v$(grep -oP 'major=\K[0-9]+' /etc/foxbuntu-release).$(grep -oP 'minor=\K[0-9]+' /etc/foxbuntu-release)$(output=$(grep -o 'patch=[1-9][0-9]*' /etc/foxbuntu-release | cut -d= -f2) && [ -n "$output" ] && echo ".$output")$(grep -oP 'hotfix=\K[a-z]+' /etc/foxbuntu-release) ($(lsb_release -d | awk -F'\t' '{print $2}') $(lsb_release -c | awk -F'\t' '{print $2}'))" + local system_uptime="$(uptime -p | awk '{$1=""; print $0}' | sed -e 's/ day\b/d/g' -e 's/ hour\b/h/g' -e 's/ hours\b/h/g' -e 's/ minute\b/m/g' -e 's/ minutes\b/m/g' | sed 's/,//g')" + local logging_enabled="$(logging "check" | sed 's/\x1b\[[0-9;]*m//g')" + local kernel_active_modules="$(lsmod | awk 'NR>1 {print $1}' | tr '\n' ' ' && echo)" + local kernel_boot_modules="$(modules=$(sed -n '6,$p' /etc/modules | sed ':a;N;$!ba;s/\n/, /g;s/, $//'); [ -z "$modules" ] && echo "none" || echo "$modules")" + + local wifi_status="$(replace_colors "$(femto-network-config.sh -w | grep -v '^$' | grep -v '^Hostname')")" #remove hostname line, as it's identical to the one in ethernet settings + local eth_status="$(replace_colors "$(femto-network-config.sh -e)")" + + local usb_mode="$(cat /sys/devices/platform/ff3e0000.usb2-phy/otg_mode)" + local spi0_state="$([ "$(awk -F= '/^SPI0_M0_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" + local spi0_speed="$((0x$(xxd -p /sys/firmware/devicetree/base/spi@ff500000/spidev@0/spi-max-frequency | tr -d '\n')))" + local i2c3_state="$([ "$(awk -F= '/^I2C3_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" + local i2c3_speed="$(awk -F= '/^I2C3_M1_SPEED/ {print $2}' /etc/luckfox.cfg)" + local uart3_state="$([ "$(awk -F= '/^UART3_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" + local uart4_state="$([ "$(awk -F= '/^UART4_M1_STATUS/ {print $2}' /etc/luckfox.cfg)" -eq 1 ] && echo "enabled" || echo "disabled")" + + local lora_radio="$(replace_colors "$(femto-meshtasticd-config.sh -k)")" + local usb_devices="$(lsusb | grep -v 'root hub' | awk 'NR>0{printf "USB: "; for(i=7;i<=NF;i++) printf "%s ", $i; print ""} END {if (NR == 0) printf "USB: None detected"}')" + local i2c_addresses="$(i2cdetect -y 3 | awk 'NR>1 {for(i=2;i<=17;i++) if ($i == "ff" || $i == "UU") { printf "0x%02x ", (i-2) + (NR-2)*16} }' | tr -d '\n' )" + + local meshtasticd_service_status="$(replace_colors "$(femto-meshtasticd-config.sh -S)")" + local meshtasticd_info="$(replace_colors "$(femto-meshtasticd-config.sh -i)")" + echo -e "\ + Femtofox\n\ +Core: $(cat /sys/firmware/devicetree/base/model)\n\ +Operating System: $os_version\n\ +Kernel version: $(uname -r)\n\ +Uptime: $system_uptime\n\ +Logging: $logging_enabled\n\ +System time: $(date)\n\ +K modules active: $kernel_active_modules\n\ +K boot modules: $kernel_boot_modules\n\ +\n\ + CPU:\n\ +Model: $cpu_model\n\ +Architecture: $cpu_architecture\n\ +Speed: $cpu_speed x $(nproc) cores\n\ +Temperature: $cpu_temp\n\ +Serial # $cpu_serial\n\ +\n\ + Storage:\n\ +microSD size: $microsd_size\n\ +Memory: $memory\n\ +Swap: $swap\n\ +$mounted_drives\n\ +\n\ + Networking (wlan0 & eth0):\n\ +$wifi_status\n\ +--- +$eth_status\n\ +\n\ + Interfaces:\n\ +USB mode: $usb_mode\n\ +SPI-0 state: $spi0_state\n\ +SPI-0 speed: $spi0_speed\n\ +i2c-3 state: $i2c3_state\n\ +i2c-3 speed: $i2c3_speed\n\ +UART-3 state: $uart3_state\n\ +UART-4 state: $uart4_state\n\ +\n\ + Attached devices:\n\ +LoRa radio: $lora_radio\n\ +$usb_devices\n\ +i2c devices: $i2c_addresses\n\ +\n\ + Meshtasticd:\n\ +Service status: $meshtasticd_service_status\n\ +$meshtasticd_info\ +" +} + +# enable/disable/check system logging +logging() { + if [ "$1" = "disable" ]; then + msg="Disabling system logging by making /var/log immutable." + logger $msg + echo $msg + chattr +i /var/log + return 0 + elif [ "$1" = "enable" ]; then + chattr -i /var/log + msg="Enabling system logging by making /var/log writable." + logger $msg + echo $msg + return 0 + elif [ "$1" = "check" ]; then + lsattr -d /var/log | grep -q 'i' && echo -e "\033[0;31mdisabled\033[0m" || echo -e "\033[0;34menabled\033[0m" + else + echo "\`$1\` is not a valid argument for -L. Options are \"enable\" and \"disable\"." + echo -e "$help" + fi +} + +while getopts ":harsl:i" opt; do + case ${opt} in + h) # Option -h (help) + echo -e $help + ;; + a) # Option -a (ACT LED enable/disable/check) + act_led $2 + exit 0 # exit immediately for boot speed + ;; + r) # Option -r (reboot) + echo -e "Rebooting..." + reboot + ;; + s) # Option -s (shutdown) + echo -e "Shutting down...\n\nPower consumption will not stop." + logger "User requested system halt" + halt + ;; + l) # Option -l (Logging enable/disable/check) + logging $OPTARG + ;; + i) system_info ;; # Option -i (sysinfo) + \?) # Unknown option) + echo -e "Unknown argument $1.\n$help" + ;; + esac +done \ No newline at end of file