From a5b212e63fe20e4b92fdd11835583c161d3af4ea Mon Sep 17 00:00:00 2001 From: Daniel Hert Date: Wed, 26 Nov 2025 15:15:59 +0100 Subject: [PATCH 01/13] fix x500 udev --- subscripts/5Udev_rules/DISREGARD_udev_rules/X500.rules | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subscripts/5Udev_rules/DISREGARD_udev_rules/X500.rules b/subscripts/5Udev_rules/DISREGARD_udev_rules/X500.rules index 47edeb0..dc6c796 100644 --- a/subscripts/5Udev_rules/DISREGARD_udev_rules/X500.rules +++ b/subscripts/5Udev_rules/DISREGARD_udev_rules/X500.rules @@ -1,9 +1,9 @@ # RULES for X500 Distribution Board # Pixhawk -SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="03", SYMLINK+="pixhawk",OWNER="TO_BE_REPLACED",MODE="0666" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="02", SYMLINK+="pixhawk",OWNER="TO_BE_REPLACED",MODE="0666" # MRS Module 1 SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="MRS_MODULE1",OWNER="TO_BE_REPLACED",MODE="0666" # MRS Module 2 SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="01", SYMLINK+="MRS_MODULE2",OWNER="TO_BE_REPLACED",MODE="0666" # MRS Module 3 -SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="02", SYMLINK+="MRS_MODULE3",OWNER="TO_BE_REPLACED",MODE="0666" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", ENV{ID_USB_INTERFACE_NUM}=="03", SYMLINK+="MRS_MODULE3",OWNER="TO_BE_REPLACED",MODE="0666" From 1b6cc3d3b528ace915e276ad7fbd62aede591e3b Mon Sep 17 00:00:00 2001 From: Recep Koca Date: Mon, 20 Apr 2026 15:44:53 +0200 Subject: [PATCH 02/13] robocore udev rules --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 2 +- subscripts/5Udev_rules/DISREGARD_udev_rules/robocore.rules | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 subscripts/5Udev_rules/DISREGARD_udev_rules/robocore.rules diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index 877505f..e720826 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -151,7 +151,7 @@ sudo chown root:root $FILENAME sudo sed -i -e "s/TO_BE_REPLACED/$hostname/g" $FILENAME -devices=$(ls /dev | grep -e ttyUSB -e ttyACM) +devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) if [ -z "${devices}" ]; then error_msg "No devices matching the ttyUSBx or ttyACMx pattern found." diff --git a/subscripts/5Udev_rules/DISREGARD_udev_rules/robocore.rules b/subscripts/5Udev_rules/DISREGARD_udev_rules/robocore.rules new file mode 100644 index 0000000..1c73159 --- /dev/null +++ b/subscripts/5Udev_rules/DISREGARD_udev_rules/robocore.rules @@ -0,0 +1,2 @@ +SUBSYSTEM=="tty", KERNEL=="ttyTHS0", SYMLINK+="pixhawk", OWNER="TO_BE_REPLACED", MODE="0666" + From b72fe9c5b233d8dd87b340ba46a6aed4c54a253a Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:53:15 +0200 Subject: [PATCH 03/13] Fix title --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index e720826..91d1522 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -4,7 +4,7 @@ show_menu() { } yesno_def_no () { - whiptail --title "Netplan Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 + whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 ret_val=$? if [ $ret_val -eq 255 ]; then @@ -21,7 +21,7 @@ yesno_def_no () { } yesno_def_yes () { - whiptail --title "Netplan Config" --yesno "$1" 0 0 + whiptail --title "Udev Config" --yesno "$1" 0 0 ret_val=$? if [ $ret_val -eq 255 ]; then @@ -58,7 +58,7 @@ input_box () { } error_msg () { - whiptail --title "Netplan config" --msgbox "$1" 0 0 + whiptail --title "Udev config" --msgbox "$1" 0 0 } yesno_def_no "Delete some selected previous udev rules files? (Remanants from previous setups etc.)" From 1c6ba0ed25c5fac1cbc96e33834426b3f19693b6 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:53:55 +0200 Subject: [PATCH 04/13] Do not show error when deleting file that doesn't exist --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index 91d1522..b1d2940 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -103,7 +103,7 @@ if [ $ret_val -eq 1 ]; then fi FILENAME=/tmp/99-usb-serial-MRS.rules -sudo rm $FILENAME +sudo rm $FILENAME 2>/dev/null || true yesno_def_yes "Are you using an MRS distribution board?" ret_val=$? From 728c46781305d867c55cdd161408d37b2a9c4568 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:54:16 +0200 Subject: [PATCH 05/13] Delete unused vars --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index b1d2940..68c05db 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -79,7 +79,6 @@ if [ $ret_val -eq 1 ]; then if [[ ! -d ${file} ]]; then OPTIONS+=("$index") let "index++" - has_99=$(echo $filename | grep "99") OPTIONS+=("$filename") OPTIONS+=("OFF") FULL_FILEPATHS+=("$file") @@ -124,7 +123,6 @@ if [ $ret_val -eq 1 ]; then if [[ ! -d ${file} ]]; then OPTIONS+=("$index") let "index++" - has_99=$(echo $filename | grep "99") OPTIONS+=("$filename") FULL_FILEPATHS+=("$file") fi From 0bd50cc649ba3fe23727281853ef7b7c1cb62abe Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:55:35 +0200 Subject: [PATCH 06/13] Reformat --- .../5Udev_rules/1Setup_new_udev_rules_file.sh | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index 68c05db..a07996f 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -3,7 +3,7 @@ show_menu() { whiptail --title "Udev config" --menu "$1:" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3 } -yesno_def_no () { +yesno_def_no() { whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 ret_val=$? @@ -20,8 +20,8 @@ yesno_def_no () { fi } -yesno_def_yes () { - whiptail --title "Udev Config" --yesno "$1" 0 0 +yesno_def_yes() { + whiptail --title "Udev Config" --yesno "$1" 0 0 ret_val=$? if [ $ret_val -eq 255 ]; then @@ -37,7 +37,7 @@ yesno_def_yes () { fi } -input_box () { +input_box() { tmp=$(whiptail --inputbox "$1" 0 0 "$2" 3>&1 1>&2 2>&3) ret_val=$? @@ -57,7 +57,7 @@ input_box () { fi } -error_msg () { +error_msg() { whiptail --title "Udev config" --msgbox "$1" 0 0 } @@ -97,7 +97,7 @@ if [ $ret_val -eq 1 ]; then fi for CHOICE in $SELECTIONS; do - sudo rm ${FULL_FILEPATHS[$((CHOICE - 1))]} + sudo rm "${FULL_FILEPATHS[$((CHOICE - 1))]}" done fi @@ -132,13 +132,13 @@ if [ $ret_val -eq 1 ]; then chosen_filename="" choice=$(show_menu "What frame udev rules do you want to use?") if [ $? -eq 0 ]; then - chosen_filename=$(echo ${FULL_FILEPATHS[$((choice - 1))]}) + chosen_filename="${FULL_FILEPATHS[$((choice - 1))]}" else # echo "Menu canceled." exit 1 fi - cp $chosen_filename $FILENAME + cp "$chosen_filename" $FILENAME else touch $FILENAME @@ -155,7 +155,6 @@ if [ -z "${devices}" ]; then error_msg "No devices matching the ttyUSBx or ttyACMx pattern found." else - for device in ${devices}; do device_info=$(udevadm info /dev/$device | grep "S: serial/by-id/") @@ -177,7 +176,6 @@ else Serial=$(udevadm info /dev/$device | grep "ID_SERIAL_SHORT") Serial="${Serial##*"="}" - yesno_def_yes "Do you want to add udev rule for this device? $device:\n$device_info" ret_val=$? @@ -194,12 +192,12 @@ else if [ $ret_val -eq 1 ]; then exit 1 else - echo -e "\n#Following line was added by MRS UAV System Intall utility:" | sudo tee -a $FILENAME > /dev/null + echo -e "\n#Following line was added by MRS UAV System Intall utility:" | sudo tee -a $FILENAME >/dev/null if [ -z "${Serial}" ]; then - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME > /dev/null + echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null else - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME > /dev/null + echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null fi fi done From 8135d2582fdb604015372a8cfa382b7095ca1de1 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:59:25 +0200 Subject: [PATCH 07/13] Fix typos --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index a07996f..a0e0703 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -61,7 +61,7 @@ error_msg() { whiptail --title "Udev config" --msgbox "$1" 0 0 } -yesno_def_no "Delete some selected previous udev rules files? (Remanants from previous setups etc.)" +yesno_def_no "Delete some selected previous udev rules files? (Remnants from previous setups etc.)" ret_val=$? if [ $ret_val -eq 1 ]; then @@ -192,7 +192,7 @@ else if [ $ret_val -eq 1 ]; then exit 1 else - echo -e "\n#Following line was added by MRS UAV System Intall utility:" | sudo tee -a $FILENAME >/dev/null + echo -e "\n#Following line was added by MRS UAV System Install utility:" | sudo tee -a $FILENAME >/dev/null if [ -z "${Serial}" ]; then echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null From bf86b3498eb4aca5cdabbd54bbe8fb568c17073b Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 13:57:41 +0200 Subject: [PATCH 08/13] Fix owner Should be user not hostname --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index a0e0703..9cf5f89 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -144,10 +144,9 @@ else touch $FILENAME fi -hostname=$(cat /etc/hostname) sudo chown root:root $FILENAME -sudo sed -i -e "s/TO_BE_REPLACED/$hostname/g" $FILENAME +sudo sed -i -e "s/TO_BE_REPLACED/$USER/g" $FILENAME devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) @@ -195,9 +194,9 @@ else echo -e "\n#Following line was added by MRS UAV System Install utility:" | sudo tee -a $FILENAME >/dev/null if [ -z "${Serial}" ]; then - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null + echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null else - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null + echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null fi fi done From 804789bba201eaf901be59b61b565c6c684f19c6 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 15:14:47 +0200 Subject: [PATCH 09/13] Apply new rules immediately --- subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh index 9cf5f89..bdfc03e 100755 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh @@ -203,3 +203,7 @@ else fi sudo cp $FILENAME /etc/udev/rules.d/ + +# Apply the new udev rules +sudo udevadm control --reload-rules +sudo udevadm trigger From 846770c8984448f75bb7688df9d1e73c414e1169 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 16:50:33 +0200 Subject: [PATCH 10/13] New menu for adding udev rules --- .../1Add_from_existing_rules_file.sh | 67 ++++++ .../5Udev_rules/1Setup_new_udev_rules_file.sh | 209 ------------------ .../2Add_from_currently_connected_devices.sh | 89 ++++++++ .../2Add_udev_rules_to_an_existing_file.sh | 142 ------------ .../DISREGARD_common/udev_rules_common.sh | 179 +++++++++++++++ 5 files changed, 335 insertions(+), 351 deletions(-) create mode 100644 subscripts/5Udev_rules/1Add_from_existing_rules_file.sh delete mode 100755 subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh create mode 100644 subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh delete mode 100755 subscripts/5Udev_rules/2Add_udev_rules_to_an_existing_file.sh create mode 100644 subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh diff --git a/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh b/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh new file mode 100644 index 0000000..49265ab --- /dev/null +++ b/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" + +# Shows a dialog with a list of .rules files from the DISREGARD_udev_rules directory and allows the user to select one +template_file=$(choose_template_file) + +# If no template file was found or selected, exit the script +if [ -z "$template_file" ]; then + exit 1 +fi + +# Shows a dialog asking how to store the rule, either in a new file or appended to an existing one +# If the user chooses to create a new file, they are asked for the name of the new file. +# If a file with that name already exists, the script asks if the user wants to overwrite the file +write_mode=$(choose_write_mode "Udev config" "How do you want to store the selected udev rules?") +if [ "$write_mode" = "new" ]; then + target_file_name=$(input_box "What should the new udev rules file be named?" "99-usb-serial-MRS.rules") + if [ -z "$target_file_name" ]; then + exit 1 + fi + + target_file=$(resolve_rules_target_path "$target_file_name") + if [ -z "$target_file" ]; then + exit 1 + fi + + if [ -e "$target_file" ]; then + yesno_def_no "The file already exists: $target_file\n\nDo you want to overwrite this file with the selected rules instead?" + ret_val=$? + + if [ $ret_val -eq 0 ]; then + exit 1 + fi + + fi +elif [ "$write_mode" = "append" ]; then + target_file=$(list_existing_udev_rules_file) + if [ -z "$target_file" ]; then + exit 1 + fi +else + exit 1 +fi + +# Replace placeholder values in the rules template +template_contents=$(sed -e "s/TO_BE_REPLACED/$USER/g" "$template_file") + +# Confirm before writing +yesno_def_yes "Write the selected rules to this file?\n\nTarget: $target_file\n\nTemplate: $(basename "$template_file")\n\nContents:\n\n$template_contents" +ret_val=$? + +if [ ! $ret_val -eq 1 ]; then + exit 1 +fi + +write_text_to_target "$target_file" "$write_mode" "$template_contents" +ret_val=$? +if [ $ret_val -eq 1 ]; then + exit 1 +fi + +sudo chown root:root "$target_file" +sudo chmod 644 "$target_file" + +sudo udevadm control --reload-rules +sudo udevadm trigger diff --git a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh b/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh deleted file mode 100755 index bdfc03e..0000000 --- a/subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh +++ /dev/null @@ -1,209 +0,0 @@ -#!/bin/bash -show_menu() { - whiptail --title "Udev config" --menu "$1:" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3 -} - -yesno_def_no() { - whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - elif [ $ret_val -eq 1 ]; then - # echo "User hit Yes" - return 1 - elif [ $ret_val -eq 0 ]; then - # echo "User hit No" - return 0 - else - echo "Error state" - fi -} - -yesno_def_yes() { - whiptail --title "Udev Config" --yesno "$1" 0 0 - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - elif [ $ret_val -eq 0 ]; then - # echo "User hit Yes" - return 1 - elif [ $ret_val -eq 1 ]; then - # echo "User hit No" - return 0 - else - echo "Error state" - fi -} - -input_box() { - tmp=$(whiptail --inputbox "$1" 0 0 "$2" 3>&1 1>&2 2>&3) - ret_val=$? - - if [ $ret_val -eq 255 ]; then - # User hit Escape - exit 1 - elif [ $ret_val -eq 1 ]; then - # User hit Cancel - exit 1 - elif [ $ret_val -eq 0 ]; then - # valid input - echo $tmp #this will output the string that is user input, and we can capture it into a variable - e.g. foo=$(input_box) - return 0 - else - echo "Error state" - exit 0 - fi -} - -error_msg() { - whiptail --title "Udev config" --msgbox "$1" 0 0 -} - -yesno_def_no "Delete some selected previous udev rules files? (Remnants from previous setups etc.)" -ret_val=$? - -if [ $ret_val -eq 1 ]; then - OPTIONS=() - FULL_FILEPATHS=() - - folder_path="/etc/udev/rules.d" - index="1" - for file in "$folder_path"/*; do - filename=$file - filename="${file##*"/"}" - # filename="${filename%.*}" - # filename="${filename//_/ }" - - if [[ ! -d ${file} ]]; then - OPTIONS+=("$index") - let "index++" - OPTIONS+=("$filename") - OPTIONS+=("OFF") - FULL_FILEPATHS+=("$file") - fi - done - - SELECTIONS=$(whiptail --separate-output --title "Udev rules setup" --checklist "Delete any previous udev rules file?\nCAUTION - this can break things! Only delete files if you know what you are doing.\n\nChoose options by pressing the spacebar" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3) - - ret_val=$? - - if [[ "$ret_val" -eq 1 ]]; then - exit 1 - fi - if [[ "$ret_val" -eq 255 ]]; then - exit 1 - fi - - for CHOICE in $SELECTIONS; do - sudo rm "${FULL_FILEPATHS[$((CHOICE - 1))]}" - done -fi - -FILENAME=/tmp/99-usb-serial-MRS.rules -sudo rm $FILENAME 2>/dev/null || true - -yesno_def_yes "Are you using an MRS distribution board?" -ret_val=$? - -if [ $ret_val -eq 1 ]; then - - OPTIONS=() - FULL_FILEPATHS=() - - folder_path="$(dirname "$0")/DISREGARD_udev_rules" - index="1" - for file in "$folder_path"/*; do - filename=$file - filename="${file##*"/"}" - # filename="${filename%.*}" - # filename="${filename//_/ }" - - if [[ ! -d ${file} ]]; then - OPTIONS+=("$index") - let "index++" - OPTIONS+=("$filename") - FULL_FILEPATHS+=("$file") - fi - done - # echo $OPTIONS - - chosen_filename="" - choice=$(show_menu "What frame udev rules do you want to use?") - if [ $? -eq 0 ]; then - chosen_filename="${FULL_FILEPATHS[$((choice - 1))]}" - else - # echo "Menu canceled." - exit 1 - fi - - cp "$chosen_filename" $FILENAME - -else - touch $FILENAME -fi - -sudo chown root:root $FILENAME - -sudo sed -i -e "s/TO_BE_REPLACED/$USER/g" $FILENAME - -devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) - -if [ -z "${devices}" ]; then - error_msg "No devices matching the ttyUSBx or ttyACMx pattern found." -else - - for device in ${devices}; do - - device_info=$(udevadm info /dev/$device | grep "S: serial/by-id/") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "DEVNAME") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_VENDOR_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_VENDOR_ID") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL_ID") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL=") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_SERIAL=") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_SERIAL_SHORT") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_PCI_CLASS_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_PCI_SUBCLASS_FROM_DATABASE") - - idVendor=$(udevadm info /dev/$device | grep "ID_VENDOR_ID") - idVendor="${idVendor##*"="}" - idProduct=$(udevadm info /dev/$device | grep "ID_MODEL_ID") - idProduct="${idProduct##*"="}" - Serial=$(udevadm info /dev/$device | grep "ID_SERIAL_SHORT") - Serial="${Serial##*"="}" - - yesno_def_yes "Do you want to add udev rule for this device? $device:\n$device_info" - ret_val=$? - - if [ ! $ret_val -eq 1 ]; then - continue - fi - - symlink=$(input_box "What should this device be named?") # NOTE - when called like this, we cannot "exit 0" from within the inputbox() function, as it is ran in a sub-shell, and the exit will only exit the sub-shell - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - fi - if [ $ret_val -eq 1 ]; then - exit 1 - else - echo -e "\n#Following line was added by MRS UAV System Install utility:" | sudo tee -a $FILENAME >/dev/null - - if [ -z "${Serial}" ]; then - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null - else - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" | sudo tee -a $FILENAME >/dev/null - fi - fi - done -fi - -sudo cp $FILENAME /etc/udev/rules.d/ - -# Apply the new udev rules -sudo udevadm control --reload-rules -sudo udevadm trigger diff --git a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh new file mode 100644 index 0000000..f277929 --- /dev/null +++ b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" + +# Shows a dialog asking how to store the rule, either in a new file or appended to an existing one +# If the user chooses to create a new file, they are asked for the name of the new file. +# If a file with that name already exists, the script asks if the user wants to overwrite the file +write_mode=$(choose_write_mode "Udev config" "How do you want to store the device rules?") + +if [ "$write_mode" = "new" ]; then + target_file_name=$(input_box "What should the new udev rules file be named?" "99-usb-serial-MRS.rules") + target_file=$(resolve_rules_target_path "$target_file_name") + + if [ -e "$target_file" ]; then + yesno_def_no "The file already exists: $target_file\n\nDo you want to overwrite this file with the selected rules instead?" + ret_val=$? + + if [ $ret_val -eq 0 ]; then + exit 1 + fi + fi +else + target_file=$(list_existing_udev_rules_file) + if [ -z "$target_file" ]; then + exit 1 + fi +fi + +# List devices that the user might be interesting in adding udev rules for +devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) + +if [ -z "$devices" ]; then + error_msg "No devices matching the ttyUSBx, ttyACMx, or ttyTHS pattern found." + exit 1 +fi + +wrote_anything=false +generated_rules="" + +# Loop over found devices and for each prompt user whether they want to add a udev rule for that device. +# If they do, ask what they want to name the symlink for that device and then write the corresponding udev rule to target file +for device in $devices; do + device_info=$(get_device_info "$device") + idVendor=$(get_udev_value "$device" "ID_VENDOR_ID") + idProduct=$(get_udev_value "$device" "ID_MODEL_ID") + Serial=$(get_udev_value "$device" "ID_SERIAL_SHORT") + + yesno_def_yes "Do you want to add a udev rule for this device? $device:\n$device_info" + ret_val=$? + + if [ ! $ret_val -eq 1 ]; then + continue + fi + + symlink=$(input_box "What should this device be named?") + + rule_line="SUBSYSTEM=\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\"" + if [ -n "$Serial" ]; then + rule_line="$rule_line, ATTRS{serial}==\"$Serial\"" + fi + rule_line="$rule_line, SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" + + if [ "$wrote_anything" = false ]; then + generated_rules="# Following line was added by MRS UAV System Install utility:\n${rule_line}" + wrote_anything=true + else + generated_rules="${generated_rules}\n\n# Following line was added by MRS UAV System Install utility:\n${rule_line}" + fi +done + +if [ "$wrote_anything" = false ]; then + error_msg "No udev rules were added." + exit 1 +fi + +yesno_def_yes "Write the generated rules to this file?\n\nTarget: $target_file\n\nGenerated rules:\n\n$generated_rules" +ret_val=$? + +if [ ! $ret_val -eq 1 ]; then + exit 1 +fi + +write_text_to_target "$target_file" "$write_mode" "$generated_rules" + +sudo chown root:root "$target_file" +sudo chmod 644 "$target_file" + +sudo udevadm control --reload-rules +sudo udevadm trigger diff --git a/subscripts/5Udev_rules/2Add_udev_rules_to_an_existing_file.sh b/subscripts/5Udev_rules/2Add_udev_rules_to_an_existing_file.sh deleted file mode 100755 index 3384abe..0000000 --- a/subscripts/5Udev_rules/2Add_udev_rules_to_an_existing_file.sh +++ /dev/null @@ -1,142 +0,0 @@ -#!/bin/bash - -show_menu() { - whiptail --title "Udev config" --menu "$1:" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3 -} - -inputbox() { - whiptail --inputbox "$1" 10 30 3>&1 1>&2 2>&3 -} - -yesno_def_no () { - whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - elif [ $ret_val -eq 1 ]; then - # echo "User hit Yes" - return 1 - elif [ $ret_val -eq 0 ]; then - # echo "User hit No" - return 0 - else - echo "Error state" - fi -} - -yesno_def_yes () { - whiptail --title "Udev Config" --yesno "$1" 0 0 - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - elif [ $ret_val -eq 0 ]; then - # echo "User hit Yes" - return 1 - elif [ $ret_val -eq 1 ]; then - # echo "User hit No" - return 0 - else - echo "Error state" - fi -} - -error_msg () { - whiptail --title "Udev config" --msgbox "$1" 0 0 -} - -OPTIONS=() -FULL_FILEPATHS=() - -folder_path="/etc/udev/rules.d" -index="1" -for file in "$folder_path"/*; do - filename=$file - filename="${file##*"/"}" - # filename="${filename%.*}" - # filename="${filename//_/ }" - - if [[ ! -d ${file} ]]; then - OPTIONS+=("$index") - let "index++" - has_99=$(echo $filename | grep "99") - OPTIONS+=("$filename") - FULL_FILEPATHS+=("$file") - fi -done -# echo $OPTIONS - -chosen_filename="" -choice=$(show_menu "Select which udev rules file do you want to add to:") -if [ $? -eq 0 ]; then - chosen_filename=$(echo ${FULL_FILEPATHS[$((choice - 1))]}) -else - # echo "Menu canceled." - exit 1 -fi - - -yesno_def_yes "Do you want to add to this file? $chosen_filename?\n\ncontents: \n\n\n$(cat $chosen_filename)" -ret_val=$? - -if [ ! $ret_val -eq 1 ]; then - exit 1 -fi -devices=$(ls /dev | grep -e ttyUSB -e ttyACM) - -if [ -z "${devices}" ]; then - error_msg "No devices matching the ttyUSBx or ttyACMx pattern found." -else - - hostname=$(cat /etc/hostname) - - for device in ${devices}; do - - device_info=$(udevadm info /dev/$device | grep "S: serial/by-id/") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "DEVNAME") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_VENDOR_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_VENDOR_ID") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL_ID") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_MODEL=") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_SERIAL=") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_SERIAL_SHORT") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_PCI_CLASS_FROM_DATABASE") - device_info="${device_info}\n"$(udevadm info /dev/$device | grep "ID_PCI_SUBCLASS_FROM_DATABASE") - - idVendor=$(udevadm info /dev/$device | grep "ID_VENDOR_ID") - idVendor="${idVendor##*"="}" - idProduct=$(udevadm info /dev/$device | grep "ID_MODEL_ID") - idProduct="${idProduct##*"="}" - Serial=$(udevadm info /dev/$device | grep "ID_SERIAL_SHORT") - Serial="${Serial##*"="}" - - - yesno_def_yes "Do you want to add udev rule for this device? $device:\n$device_info" - ret_val=$? - - if [ ! $ret_val -eq 1 ]; then - continue - fi - - symlink=$(inputbox "What should this device be named?") # NOTE - when called like this, we cannot "exit 0" from within the inputbox() function, as it is ran in a sub-shell, and the exit will only exit the sub-shell - ret_val=$? - - if [ $ret_val -eq 255 ]; then - exit 1 - fi - if [ $ret_val -eq 1 ]; then - exit 1 - else - echo $symlink - echo -e "\n#Following line was added by MRS UAV System Intall utility:" | sudo tee -a $chosen_filename > /dev/null - - if [ -z "${Serial}" ]; then - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $chosen_filename > /dev/null - else - echo -e "SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\", ATTRS{serial}==\"$Serial\", SYMLINK+=\"$symlink\", OWNER=\"$hostname\", MODE=\"0666\"" | sudo tee -a $chosen_filename > /dev/null - fi - fi - done -fi diff --git a/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh b/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh new file mode 100644 index 0000000..93107cf --- /dev/null +++ b/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh @@ -0,0 +1,179 @@ +#!/bin/bash + +show_menu() { + whiptail --title "$1" --menu "$2" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3 +} + +yesno_def_no() { + whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 + ret_val=$? + + if [ $ret_val -eq 255 ]; then + exit 1 + elif [ $ret_val -eq 1 ]; then + return 1 + elif [ $ret_val -eq 0 ]; then + return 0 + else + echo "Error state" + fi +} + +yesno_def_yes() { + whiptail --title "Udev Config" --yesno "$1" 0 0 + ret_val=$? + + if [ $ret_val -eq 255 ]; then + exit 1 + elif [ $ret_val -eq 0 ]; then + return 1 + elif [ $ret_val -eq 1 ]; then + return 0 + else + echo "Error state" + fi +} + +input_box() { + local prompt="$1" + local default_value="$2" + local tmp + tmp=$(whiptail --inputbox "$prompt" 0 0 "$default_value" 3>&1 1>&2 2>&3) + ret_val=$? + + if [ $ret_val -eq 255 ]; then + exit 1 + elif [ $ret_val -eq 1 ]; then + exit 1 + elif [ $ret_val -eq 0 ]; then + printf '%s\n' "$tmp" + return 0 + else + echo "Error state" + exit 0 + fi +} + +error_msg() { + whiptail --title "Udev config" --msgbox "$1" 0 0 +} + +list_files_from_dir() { + local folder_path="$1" + local menu_title="$2" + local menu_prompt="$3" + local empty_message="$4" + local options=() + local files=() + local index=1 + local file + + shopt -s nullglob + for file in "$folder_path"/*; do + if [[ -d "$file" ]]; then + continue + fi + + options+=("$index" "${file##*/}") + files+=("$file") + index=$((index + 1)) + done + shopt -u nullglob + + if [ ${#files[@]} -eq 0 ]; then + error_msg "$empty_message" + return 1 + fi + + OPTIONS=("${options[@]}") + local choice + choice=$(show_menu "$menu_title" "$menu_prompt") + + if [ $? -ne 0 ]; then + exit 1 + fi + + printf '%s\n' "${files[$((choice - 1))]}" +} + +choose_write_mode() { + local title="$1" + local prompt="$2" + OPTIONS=( + "1" "Create a new file" + "2" "Append to existing file" + ) + local choice + choice=$(show_menu "$title" "$prompt") + + if [ $? -ne 0 ]; then + exit 1 + fi + + if [ "$choice" = "1" ]; then + printf '%s\n' "new" + else + printf '%s\n' "append" + fi +} + +list_existing_udev_rules_file() { + list_files_from_dir "/etc/udev/rules.d" "Udev config" "Select which udev rules file you want to use:" "No files found in /etc/udev/rules.d." +} + +choose_template_file() { + list_files_from_dir "$(dirname "$0")/DISREGARD_udev_rules" "Udev config" "Select which udev rules you want to use:" "No udev rules were found." +} + +resolve_rules_target_path() { + local file_name="$1" + + if [[ "$file_name" == /* ]]; then + printf '%s\n' "$file_name" + return + fi + + if [[ "$file_name" != *.rules ]]; then + file_name="${file_name}.rules" + fi + + printf '/etc/udev/rules.d/%s\n' "$file_name" +} + +write_text_to_target() { + local target_file="$1" + local append_mode="$2" + local content="$3" + + if [ "$append_mode" = "append" ]; then + printf '%s\n' "$content" | sudo tee -a "$target_file" >/dev/null + else + printf '%s\n' "$content" | sudo tee "$target_file" >/dev/null + fi +} + +get_device_info() { + local device="$1" + local device_path="/dev/$device" + local device_info + + device_info=$(udevadm info "$device_path" | grep "S: serial/by-id/" || true) + device_info="${device_info}\n$(udevadm info "$device_path" | grep "DEVNAME" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_ID" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_ID" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL=" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL=" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL_SHORT" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_CLASS_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_SUBCLASS_FROM_DATABASE" || true)" + + printf '%b\n' "$device_info" +} + +get_udev_value() { + local device="$1" + local key="$2" + udevadm info "/dev/$device" | grep "$key" | head -n 1 | sed -e "s/^.*=//" +} \ No newline at end of file From 76714ef0d8b1d2c82c30c0b92a17781f1a32224c Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 17:08:37 +0200 Subject: [PATCH 11/13] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../5Udev_rules/2Add_from_currently_connected_devices.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh index f277929..57a0ef2 100644 --- a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh +++ b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh @@ -54,17 +54,17 @@ for device in $devices; do symlink=$(input_box "What should this device be named?") - rule_line="SUBSYSTEM=\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\"" + rule_line="SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\"" if [ -n "$Serial" ]; then rule_line="$rule_line, ATTRS{serial}==\"$Serial\"" fi rule_line="$rule_line, SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" if [ "$wrote_anything" = false ]; then - generated_rules="# Following line was added by MRS UAV System Install utility:\n${rule_line}" + generated_rules="# Following line was added by MRS UAV System Install utility:"$'\n'"${rule_line}" wrote_anything=true else - generated_rules="${generated_rules}\n\n# Following line was added by MRS UAV System Install utility:\n${rule_line}" + generated_rules+=$'\n\n'"# Following line was added by MRS UAV System Install utility:"$'\n'"${rule_line}" fi done From b8696bc623f78087fb732dae30d4bf2045eb6373 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Mon, 1 Jun 2026 17:10:57 +0200 Subject: [PATCH 12/13] Add execute permission --- subscripts/5Udev_rules/1Add_from_existing_rules_file.sh | 0 subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh | 0 subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 subscripts/5Udev_rules/1Add_from_existing_rules_file.sh mode change 100644 => 100755 subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh mode change 100644 => 100755 subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh diff --git a/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh b/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh old mode 100644 new mode 100755 diff --git a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh old mode 100644 new mode 100755 diff --git a/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh b/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh old mode 100644 new mode 100755 From f2f799d35bfbb8589cd37bcd9255580aad18c508 Mon Sep 17 00:00:00 2001 From: Vid Smole Date: Tue, 2 Jun 2026 11:49:27 +0200 Subject: [PATCH 13/13] Redesign udev menu --- .../1Add_from_existing_rules_file.sh | 67 ------ subscripts/5Udev_rules/1Add_udev_rules.sh | 223 ++++++++++++++++++ .../2Add_from_currently_connected_devices.sh | 89 ------- subscripts/5Udev_rules/2Remove_udev_rules.sh | 23 ++ subscripts/5Udev_rules/3List_udev_rules.sh | 12 + .../DISREGARD_common/udev_rules_common.sh | 115 ++------- 6 files changed, 277 insertions(+), 252 deletions(-) delete mode 100755 subscripts/5Udev_rules/1Add_from_existing_rules_file.sh create mode 100755 subscripts/5Udev_rules/1Add_udev_rules.sh delete mode 100755 subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh create mode 100755 subscripts/5Udev_rules/2Remove_udev_rules.sh create mode 100755 subscripts/5Udev_rules/3List_udev_rules.sh diff --git a/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh b/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh deleted file mode 100755 index 49265ab..0000000 --- a/subscripts/5Udev_rules/1Add_from_existing_rules_file.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" - -# Shows a dialog with a list of .rules files from the DISREGARD_udev_rules directory and allows the user to select one -template_file=$(choose_template_file) - -# If no template file was found or selected, exit the script -if [ -z "$template_file" ]; then - exit 1 -fi - -# Shows a dialog asking how to store the rule, either in a new file or appended to an existing one -# If the user chooses to create a new file, they are asked for the name of the new file. -# If a file with that name already exists, the script asks if the user wants to overwrite the file -write_mode=$(choose_write_mode "Udev config" "How do you want to store the selected udev rules?") -if [ "$write_mode" = "new" ]; then - target_file_name=$(input_box "What should the new udev rules file be named?" "99-usb-serial-MRS.rules") - if [ -z "$target_file_name" ]; then - exit 1 - fi - - target_file=$(resolve_rules_target_path "$target_file_name") - if [ -z "$target_file" ]; then - exit 1 - fi - - if [ -e "$target_file" ]; then - yesno_def_no "The file already exists: $target_file\n\nDo you want to overwrite this file with the selected rules instead?" - ret_val=$? - - if [ $ret_val -eq 0 ]; then - exit 1 - fi - - fi -elif [ "$write_mode" = "append" ]; then - target_file=$(list_existing_udev_rules_file) - if [ -z "$target_file" ]; then - exit 1 - fi -else - exit 1 -fi - -# Replace placeholder values in the rules template -template_contents=$(sed -e "s/TO_BE_REPLACED/$USER/g" "$template_file") - -# Confirm before writing -yesno_def_yes "Write the selected rules to this file?\n\nTarget: $target_file\n\nTemplate: $(basename "$template_file")\n\nContents:\n\n$template_contents" -ret_val=$? - -if [ ! $ret_val -eq 1 ]; then - exit 1 -fi - -write_text_to_target "$target_file" "$write_mode" "$template_contents" -ret_val=$? -if [ $ret_val -eq 1 ]; then - exit 1 -fi - -sudo chown root:root "$target_file" -sudo chmod 644 "$target_file" - -sudo udevadm control --reload-rules -sudo udevadm trigger diff --git a/subscripts/5Udev_rules/1Add_udev_rules.sh b/subscripts/5Udev_rules/1Add_udev_rules.sh new file mode 100755 index 0000000..ee3a9aa --- /dev/null +++ b/subscripts/5Udev_rules/1Add_udev_rules.sh @@ -0,0 +1,223 @@ +#!/bin/bash + +source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" + +choose_write_mode() { + local title="$1" + local prompt="$2" + OPTIONS=( + "1" "Create a new file" + "2" "Append to existing file" + ) + local choice + choice=$(show_menu "$title" "$prompt") + + if [ $? -ne 0 ]; then + exit 1 + fi + + if [ "$choice" = "1" ]; then + printf '%s\n' "new" + else + printf '%s\n' "append" + fi +} + +choose_frame_template() { + list_files_from_dir "$(dirname "$0")/DISREGARD_udev_rules" "Udev config" "Select which udev rules you want to use:" "No udev rules were found." +} + +resolve_rules_target_path() { + local file_name="$1" + + if [[ "$file_name" == /* ]]; then + printf '%s\n' "$file_name" + return + fi + + if [[ "$file_name" != *.rules ]]; then + file_name="${file_name}.rules" + fi + + printf '/etc/udev/rules.d/%s\n' "$file_name" +} + +get_device_info() { + local device="$1" + local device_path="/dev/$device" + local device_info + + device_info=$(udevadm info "$device_path" | grep "S: serial/by-id/" || true) + device_info="${device_info}\n$(udevadm info "$device_path" | grep "DEVNAME" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_ID" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_ID" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL=" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL=" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL_SHORT" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_CLASS_FROM_DATABASE" || true)" + device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_SUBCLASS_FROM_DATABASE" || true)" + + printf '%b\n' "$device_info" +} + +get_udev_value() { + local device="$1" + local key="$2" + udevadm info "/dev/$device" | grep "$key" | head -n 1 | sed -e "s/^.*=//" +} + +choose_mrs_board() { + # Shows a dialog with a list of .rules files from the DISREGARD_udev_rules directory and allows the user to select one + template_file=$(choose_frame_template) + + # If no template file was found or selected, exit the script + if [ -z "$template_file" ]; then + exit 1 + fi + + # Replace placeholder values in the rules template + template_contents=$(sed -e "s/TO_BE_REPLACED/$USER/g" "$template_file") + + printf "%s\n\n" "$template_contents" >>"$tmp_file" +} + +configure_connected_devices() { + # List devices that the user might be interesting in adding udev rules for + devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) + + if [ -z "$devices" ]; then + msg_box "No devices matching the ttyUSBx, ttyACMx, or ttyTHS pattern found." + exit 1 + fi + + generated_rules="" + + # Loop over found devices and for each prompt user whether they want to add a udev rule for that device. + # If they do, ask what they want to name the symlink for that device and then write the corresponding udev rule to target file + for device in $devices; do + device_info=$(get_device_info "$device") + idVendor=$(get_udev_value "$device" "ID_VENDOR_ID") + idProduct=$(get_udev_value "$device" "ID_MODEL_ID") + Serial=$(get_udev_value "$device" "ID_SERIAL_SHORT") + + yesno_def_yes "Do you want to add a udev rule for this device? $device:\n$device_info" + ret_val=$? + + # skip unless user answered Yes (return code 0) + if [ $ret_val -ne 0 ]; then + continue + fi + + symlink=$(input_box "What should this device be named?") + if [ -z "$symlink" ]; then + continue + fi + + rule_line="SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\"" + if [ -n "$Serial" ]; then + rule_line="$rule_line, ATTRS{serial}==\"$Serial\"" + fi + rule_line="$rule_line, SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" + + generated_rules+="$rule_line"$'\n' + done + + if [ -z "$generated_rules" ]; then + msg_box "No udev rules were added." + exit 1 + fi + + echo "$generated_rules" >>"$tmp_file" +} + +choose_where_write_rules() { + # Shows a dialog asking how to store the rule, either in a new file or appended to an existing one + # If the user chooses to create a new file, they are asked for the name of the new file. + # If a file with that name already exists, the script asks if the user wants to overwrite the file + write_mode=$(choose_write_mode "Udev config" "How do you want to store these udev rules?") + if [ "$write_mode" = "new" ]; then + target_file_name=$(input_box "What should the new udev rules file be named?" "99-usb-serial-MRS.rules") + if [ -z "$target_file_name" ]; then + exit 1 + fi + + target_file=$(resolve_rules_target_path "$target_file_name") + if [ -z "$target_file" ]; then + exit 1 + fi + + if [ -e "$target_file" ]; then + yesno_def_no "The file already exists: $target_file\n\nDo you want to overwrite this file with the selected rules instead?" + ret_val=$? + + # If user did not agree to overwrite, abort + if [ $ret_val -ne 0 ]; then + exit 1 + fi + + fi + elif [ "$write_mode" = "append" ]; then + target_file=$(list_existing_udev_rules_file) + if [ -z "$target_file" ]; then + exit 1 + fi + else + exit 1 + fi +} + +# Create a temp file for the rules before we write them to the final destination +tmp_file=$(mktemp) +trap 'rm -f "$tmp_file"' EXIT # Ensure the temp file is removed when the script exits + +# Ask if the user has a distribution board +yesno_def_yes "Do you have a MRS distribution board with existing udev .rules file?" +ret_val=$? + +if [ $ret_val -eq 0 ]; then + choose_mrs_board +fi + +# Ask if the user wants to add rules for connected devices (ttyUSBx, ttyACMx, ttyTHSx) +yesno_def_yes "Do you want to add rules for connected devices (ttyUSBx, ttyACMx, ttyTHSx)?" +ret_val=$? + +if [ $ret_val -eq 0 ]; then + configure_connected_devices +fi + +# If the temp file is empty at this point, there are no rules to write, so we can exit the script +if [ ! -s "$tmp_file" ]; then + msg_box "No udev rules were selected or generated, so nothing to write." + exit 1 +fi + +# Ask the user where they want to write the rules (new file, overwrite existing file, append to existing file) +choose_where_write_rules + +sed -i '1s/^/# Following lines were added by MRS UAV System Install utility:\n/' $tmp_file + +yesno_def_yes "Write the following rules to this file?\nTarget: $target_file\n\nNew rules:\n$(cat "$tmp_file")" +ret_val=$? + +# abort if user did not confirm +if [ $ret_val -ne 0 ]; then + exit 1 +fi + +if [ "$write_mode" = "new" ]; then + # If the user chose to create a new file, we can just move the temp file to the target location + sudo mv "$tmp_file" "$target_file" +else + # If the user chose to append to an existing file, we need to concatenate the temp file with the existing file and write the result to the target location + cat "$tmp_file" >>"$target_file" + rm "$tmp_file" +fi + +sudo chown root:root "$target_file" +sudo chmod 644 "$target_file" + +sudo udevadm control --reload-rules +sudo udevadm trigger diff --git a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh b/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh deleted file mode 100755 index 57a0ef2..0000000 --- a/subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/bash - -source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" - -# Shows a dialog asking how to store the rule, either in a new file or appended to an existing one -# If the user chooses to create a new file, they are asked for the name of the new file. -# If a file with that name already exists, the script asks if the user wants to overwrite the file -write_mode=$(choose_write_mode "Udev config" "How do you want to store the device rules?") - -if [ "$write_mode" = "new" ]; then - target_file_name=$(input_box "What should the new udev rules file be named?" "99-usb-serial-MRS.rules") - target_file=$(resolve_rules_target_path "$target_file_name") - - if [ -e "$target_file" ]; then - yesno_def_no "The file already exists: $target_file\n\nDo you want to overwrite this file with the selected rules instead?" - ret_val=$? - - if [ $ret_val -eq 0 ]; then - exit 1 - fi - fi -else - target_file=$(list_existing_udev_rules_file) - if [ -z "$target_file" ]; then - exit 1 - fi -fi - -# List devices that the user might be interesting in adding udev rules for -devices=$(ls /dev | grep -e ttyUSB -e ttyACM -e ttyTHS) - -if [ -z "$devices" ]; then - error_msg "No devices matching the ttyUSBx, ttyACMx, or ttyTHS pattern found." - exit 1 -fi - -wrote_anything=false -generated_rules="" - -# Loop over found devices and for each prompt user whether they want to add a udev rule for that device. -# If they do, ask what they want to name the symlink for that device and then write the corresponding udev rule to target file -for device in $devices; do - device_info=$(get_device_info "$device") - idVendor=$(get_udev_value "$device" "ID_VENDOR_ID") - idProduct=$(get_udev_value "$device" "ID_MODEL_ID") - Serial=$(get_udev_value "$device" "ID_SERIAL_SHORT") - - yesno_def_yes "Do you want to add a udev rule for this device? $device:\n$device_info" - ret_val=$? - - if [ ! $ret_val -eq 1 ]; then - continue - fi - - symlink=$(input_box "What should this device be named?") - - rule_line="SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"$idVendor\", ATTRS{idProduct}==\"$idProduct\"" - if [ -n "$Serial" ]; then - rule_line="$rule_line, ATTRS{serial}==\"$Serial\"" - fi - rule_line="$rule_line, SYMLINK+=\"$symlink\", OWNER=\"$USER\", MODE=\"0666\"" - - if [ "$wrote_anything" = false ]; then - generated_rules="# Following line was added by MRS UAV System Install utility:"$'\n'"${rule_line}" - wrote_anything=true - else - generated_rules+=$'\n\n'"# Following line was added by MRS UAV System Install utility:"$'\n'"${rule_line}" - fi -done - -if [ "$wrote_anything" = false ]; then - error_msg "No udev rules were added." - exit 1 -fi - -yesno_def_yes "Write the generated rules to this file?\n\nTarget: $target_file\n\nGenerated rules:\n\n$generated_rules" -ret_val=$? - -if [ ! $ret_val -eq 1 ]; then - exit 1 -fi - -write_text_to_target "$target_file" "$write_mode" "$generated_rules" - -sudo chown root:root "$target_file" -sudo chmod 644 "$target_file" - -sudo udevadm control --reload-rules -sudo udevadm trigger diff --git a/subscripts/5Udev_rules/2Remove_udev_rules.sh b/subscripts/5Udev_rules/2Remove_udev_rules.sh new file mode 100755 index 0000000..09383d8 --- /dev/null +++ b/subscripts/5Udev_rules/2Remove_udev_rules.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" + +target_file=$(list_existing_udev_rules_file) +if [ -z "$target_file" ]; then + exit 1 +fi + +yesno_def_yes "Do you want to delete this file?\nFile: $target_file\nContents:\n$(cat "$target_file")" +ret_val=$? + +# abort if user did not confirm +if [ $ret_val -ne 0 ]; then + exit 1 +fi + +sudo rm "$target_file" + +sudo udevadm control --reload-rules +sudo udevadm trigger + +exit 1 # Exit 1 so that we go back to the udev menu instead of the main menu diff --git a/subscripts/5Udev_rules/3List_udev_rules.sh b/subscripts/5Udev_rules/3List_udev_rules.sh new file mode 100755 index 0000000..31b5f0c --- /dev/null +++ b/subscripts/5Udev_rules/3List_udev_rules.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +source "$(dirname "$0")/DISREGARD_common/udev_rules_common.sh" + +target_file=$(list_existing_udev_rules_file) +if [ -z "$target_file" ]; then + exit 1 +fi + +msg_box "File: $target_file\nContents:\n$(cat "$target_file")" + +exit 1 # Exit 1 so that we go back to the udev menu instead of the main menu diff --git a/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh b/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh index 93107cf..4061a29 100755 --- a/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh +++ b/subscripts/5Udev_rules/DISREGARD_common/udev_rules_common.sh @@ -4,36 +4,37 @@ show_menu() { whiptail --title "$1" --menu "$2" 0 0 0 "${OPTIONS[@]}" 3>&1 1>&2 2>&3 } -yesno_def_no() { - whiptail --title "Udev Config" --yesno "$1" --yes-button "No" --no-button "Yes" 0 0 - ret_val=$? +ask_yesno() { + local prompt="$1" + local default="$2" # "yes" or "no" - if [ $ret_val -eq 255 ]; then - exit 1 - elif [ $ret_val -eq 1 ]; then - return 1 - elif [ $ret_val -eq 0 ]; then - return 0 + if [ "$default" = "no" ]; then + whiptail --title "Udev Config" --yesno "$prompt" 0 0 --defaultno else - echo "Error state" + whiptail --title "Udev Config" --yesno "$prompt" 0 0 fi -} -yesno_def_yes() { - whiptail --title "Udev Config" --yesno "$1" 0 0 ret_val=$? - if [ $ret_val -eq 255 ]; then exit 1 elif [ $ret_val -eq 0 ]; then - return 1 - elif [ $ret_val -eq 1 ]; then return 0 + elif [ $ret_val -eq 1 ]; then + return 1 else echo "Error state" + exit 1 fi } +yesno_def_no() { + ask_yesno "$1" "no" +} + +yesno_def_yes() { + ask_yesno "$1" "yes" +} + input_box() { local prompt="$1" local default_value="$2" @@ -54,7 +55,7 @@ input_box() { fi } -error_msg() { +msg_box() { whiptail --title "Udev config" --msgbox "$1" 0 0 } @@ -81,7 +82,7 @@ list_files_from_dir() { shopt -u nullglob if [ ${#files[@]} -eq 0 ]; then - error_msg "$empty_message" + msg_box "$empty_message" return 1 fi @@ -96,84 +97,6 @@ list_files_from_dir() { printf '%s\n' "${files[$((choice - 1))]}" } -choose_write_mode() { - local title="$1" - local prompt="$2" - OPTIONS=( - "1" "Create a new file" - "2" "Append to existing file" - ) - local choice - choice=$(show_menu "$title" "$prompt") - - if [ $? -ne 0 ]; then - exit 1 - fi - - if [ "$choice" = "1" ]; then - printf '%s\n' "new" - else - printf '%s\n' "append" - fi -} - list_existing_udev_rules_file() { list_files_from_dir "/etc/udev/rules.d" "Udev config" "Select which udev rules file you want to use:" "No files found in /etc/udev/rules.d." } - -choose_template_file() { - list_files_from_dir "$(dirname "$0")/DISREGARD_udev_rules" "Udev config" "Select which udev rules you want to use:" "No udev rules were found." -} - -resolve_rules_target_path() { - local file_name="$1" - - if [[ "$file_name" == /* ]]; then - printf '%s\n' "$file_name" - return - fi - - if [[ "$file_name" != *.rules ]]; then - file_name="${file_name}.rules" - fi - - printf '/etc/udev/rules.d/%s\n' "$file_name" -} - -write_text_to_target() { - local target_file="$1" - local append_mode="$2" - local content="$3" - - if [ "$append_mode" = "append" ]; then - printf '%s\n' "$content" | sudo tee -a "$target_file" >/dev/null - else - printf '%s\n' "$content" | sudo tee "$target_file" >/dev/null - fi -} - -get_device_info() { - local device="$1" - local device_path="/dev/$device" - local device_info - - device_info=$(udevadm info "$device_path" | grep "S: serial/by-id/" || true) - device_info="${device_info}\n$(udevadm info "$device_path" | grep "DEVNAME" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_FROM_DATABASE" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_FROM_DATABASE" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_VENDOR_ID" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL_ID" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_MODEL=" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL=" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_SERIAL_SHORT" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_CLASS_FROM_DATABASE" || true)" - device_info="${device_info}\n$(udevadm info "$device_path" | grep "ID_PCI_SUBCLASS_FROM_DATABASE" || true)" - - printf '%b\n' "$device_info" -} - -get_udev_value() { - local device="$1" - local key="$2" - udevadm info "/dev/$device" | grep "$key" | head -n 1 | sed -e "s/^.*=//" -} \ No newline at end of file