From e623f044964531a56479315ca4d59844dff14fbc Mon Sep 17 00:00:00 2001 From: nagumii Date: Tue, 19 Nov 2024 23:01:22 +0200 Subject: [PATCH] Added USB configuration tool --- scripts/luckfox-install.sh | 9 ++- scripts/rc.local | 1 + scripts/usbconfig.sh | 131 +++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 scripts/usbconfig.sh diff --git a/scripts/luckfox-install.sh b/scripts/luckfox-install.sh index a58d3b0..2168267 100644 --- a/scripts/luckfox-install.sh +++ b/scripts/luckfox-install.sh @@ -15,7 +15,8 @@ echo "*** $(date "+%H:%M:%S %Z"): Enlarged tmpfs ***\e[0m\n" sudo chmod +x buttonservice.sh sudo mv buttonservice.sh /usr/local/bin sudo mv button.service /etc/systemd/system -sudo usermod -aG input $USER +sudo usermod -aG input femto +echo "femto ALL=(ALL) NOPASSWD: /sbin/reboot" | sudo tee -a /etc/sudoers sudo systemctl daemon-reload sudo systemctl enable button.service echo "*** $(date "+%H:%M:%S %Z"): Added reboot on BOOT button press ***\e[0m\n" @@ -29,7 +30,7 @@ echo "*** $(date "+%H:%M:%S %Z"): Updating and upgrading Ubuntu... ***\e[ sudo DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y upgrade echo "*** $(date "+%H:%M:%S %Z"): Ubuntu upgrade / update complete ***\e[0m\n" echo "*** $(date "+%H:%M:%S %Z"): Installing necessary packages... ***\e[0m\n" -sudo apt-get install linux-firmware wireless-tools git python3.10-venv libgpiod-dev libyaml-cpp-dev libbluetooth-dev openssl libssl-dev libulfius-dev liborcania-dev -y +sudo apt-get install linux-firmware wireless-tools git python3.10-venv libgpiod-dev libyaml-cpp-dev libbluetooth-dev openssl libssl-dev libulfius-dev liborcania-dev evtest -y echo "*** $(date "+%H:%M:%S %Z"): Necessary packages installed ***\e[0m\n" echo "*** $(date "+%H:%M:%S %Z"): Installing pip packages... ***\e[0m\n" pip3 install pytap2 meshtastic pypubsub @@ -39,6 +40,7 @@ echo "*** $(date "+%H:%M:%S %Z"): Pip packages installed ***\e[0m\n" echo "*** $(date "+%H:%M:%S %Z"): Getting latest Meshtasticd beta... ***\e[0m\n" URL=$(wget -qO- https://api.github.com/repos/meshtastic/firmware/releases/latest | grep -oP '"browser_download_url": "\K[^"]*armhf\.deb' | head -n 1); FILENAME=$(basename $URL); wget -O /tmp/$FILENAME $URL && sudo apt install /tmp/$FILENAME -y && sudo rm /tmp/$FILENAME echo "*** $(date "+%H:%M:%S %Z"): Installed latest Meshtasticd beta ***\e[0m\n" + echo "*** $(date "+%H:%M:%S %Z"): Getting custom FemtoFox files... ***\e[0m\n" sudo cp ../liborcania_2.3_armhf/* /usr/lib/arm-linux-gnueabihf/ sudo mv /etc/meshtasticd/config.yaml /etc/meshtasticd/config.yaml.bak @@ -50,6 +52,9 @@ sudo mv /etc/update-motd.d/10-help-text /etc/update-motd.d/10-help-text.bak sudo mv /etc/update-motd.d/60-unminimize /etc/update-motd.d/60-unminimize.bak echo "*** $(date "+%H:%M:%S %Z"): Copied custom FemtoFox files ***\e[0m\n" +sudo mv usbconfig.sh /usr/local/bin/ +echo "*** $(date "+%H:%M:%S %Z"): Added USB configuration tool ***\e[0m\n" + #serial port permissions sudo usermod -a -G tty $USER sudo usermod -a -G dialout $USER diff --git a/scripts/rc.local b/scripts/rc.local index 6e835d9..8c700d4 100644 --- a/scripts/rc.local +++ b/scripts/rc.local @@ -47,6 +47,7 @@ echo "none" > /sys/class/leds/work/trigger & #does not appear to lower power consumption, but needs more testing #(sleep 5 && cpufreq-set -u 408000 && echo "Boot complete, setting CPU frequency to 408mhz.") & +/usr/local/bin/usbconfig.sh #boot complete, blink USER LED 5 times in background echo 34 > /sys/class/gpio/export && echo out > /sys/class/gpio/gpio34/direction && ( diff --git a/scripts/usbconfig.sh b/scripts/usbconfig.sh new file mode 100644 index 0000000..3572816 --- /dev/null +++ b/scripts/usbconfig.sh @@ -0,0 +1,131 @@ +#!/bin/bash + +# Set the mount point +MOUNT_POINT="/mnt/usb" + +echo 34 > /sys/class/gpio/export && echo out > /sys/class/gpio/gpio34/direction + +# Function to log both to the screen and syslog +log_message() { + local msg="USB automount: $1" + echo "$msg" # Echo to the screen + logger "USB automount: $msg" # Log to the system log +} + +#Blink +blink() { + echo 1 > /sys/class/gpio/gpio34/value; + sleep "$1"; + echo 0 > /sys/class/gpio/gpio34/value; +} + +escape_sed() { + echo "$1" | sed -e 's/[]\/$*.^[]/\\&/g' +} + +# Check if the mount point exists and if a USB drive is plugged in +USB_DEVICE=$(lsblk -o NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT | grep -E "vfat|ext4|ntfs|exfat" | grep -E "sd[a-z][0-9]" | awk '{print $1}' | sed 's/[^a-zA-Z0-9]//g' | head -n 1) + +if [ -d "$MOUNT_POINT" ]; then + sudo rmdir "$MOUNT_POINT" + log_message "/mnt/usb deleted." +fi + +# If no USB device is found, exit +if [ -z "$USB_DEVICE" ]; then + log_message "No USB drive found." + exit 0 +fi + +# Create the mount point if it doesn't exist +if [ ! -d "$MOUNT_POINT" ]; then + sudo mkdir -p "$MOUNT_POINT" +fi + +# Construct the full device path +USB_DEVICE="/dev/$USB_DEVICE" + +# Debugging: Log and echo the extracted device name +log_message "Extracted device name: $USB_DEVICE" + +# Check if the USB drive is already mounted +if mount | grep "$USB_DEVICE" > /dev/null; then + log_message "USB drive is already mounted." +else + # Mount the USB drive to the specified mount point + sudo mount "$USB_DEVICE" "$MOUNT_POINT" + if [ $? -eq 0 ]; then + log_message "USB drive mounted successfully at $MOUNT_POINT." + else + log_message "Failed to mount USB drive." + blink "4" && sleep "0.5" + exit 1 + fi +fi + +# Check if the mounted USB drive contains a file femtofox-config.txt +if [ -f "$MOUNT_POINT/femtofox-config.txt" ]; then + log_message "femtofox-config.txt found on USB drive." + + WPA_SUPPLICANT_CONF="/etc/wpa_supplicant/wpa_supplicant.conf" + USB_CONFIG="$MOUNT_POINT/femtofox-config.txt" + + # Initialize variables + SSID="" + PSK="" + COUNTRY="" + FOUNDCONFIG="false" + + # Read the fields from the USB config file if they exist + if grep -q 'ssid=' "$USB_CONFIG"; then + SSID=$(grep 'ssid=' "$USB_CONFIG" | sed 's/ssid=//' | tr -d '"') + fi + if grep -q 'psk=' "$USB_CONFIG"; then + PSK=$(grep 'psk=' "$USB_CONFIG" | sed 's/psk=//' | tr -d '"') + fi + if grep -q 'country=' "$USB_CONFIG"; then + COUNTRY=$(grep 'country=' "$USB_CONFIG" | sed 's/country=//' | tr -d '"') + fi + + # Escape special characters for sed + ESCAPED_SSID=$(escape_sed "$SSID") + ESCAPED_PSK=$(escape_sed "$PSK") + ESCAPED_COUNTRY=$(escape_sed "$COUNTRY") + + # Update wpa_supplicant.conf with the new values if they exist + if [[ -n "$COUNTRY" ]]; then + sed -i "s/^\(country=\).*/\1$ESCAPED_COUNTRY/" "$WPA_SUPPLICANT_CONF" + log_message "Updated country in wpa_supplicant.conf from femtofox-config.txt to $COUNTRY." + FOUNDCONFIG="true" + fi + if [[ -n "$SSID" ]]; then + sed -i "/ssid=/s/\".*\"/\"$ESCAPED_SSID\"/" "$WPA_SUPPLICANT_CONF" + log_message "Updated SSID in wpa_supplicant.conf from femtofox-config.txt to $SSID." + FOUNDCONFIG="true" + fi + if [[ -n "$PSK" ]]; then + sed -i "/psk=/s/\".*\"/\"$ESCAPED_PSK\"/" "$WPA_SUPPLICANT_CONF" + log_message "Updated PSK in wpa_supplicant.conf from femtofox-config.txt." + FOUNDCONFIG="true" + fi + + if [ "$FOUNDCONFIG" = true ]; then + for _ in {1..5}; do + blink "0.125" && sleep 0.125 + done + log_message "wpa_supplicant.conf updated, rebooting." + sleep 2 && reboot + + else + log_message "femtofox-config.txt does not contain valid configuration info, ignoring." + for _ in {1..5}; do + blink "1.5" && sleep 0.5 + done + fi + +else + log_message "USB drive mounted but femtofox-config.txt not found, ignoring." + for _ in {1..3}; do + blink "1.5" && sleep 0.5 + done +fi \ No newline at end of file