Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions subscripts/5Udev_rules/1Add_from_existing_rules_file.sh
Original file line number Diff line number Diff line change
@@ -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
210 changes: 0 additions & 210 deletions subscripts/5Udev_rules/1Setup_new_udev_rules_file.sh

This file was deleted.

89 changes: 89 additions & 0 deletions subscripts/5Udev_rules/2Add_from_currently_connected_devices.sh
Original file line number Diff line number Diff line change
@@ -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\""
Comment thread
vidvidex marked this conversation as resolved.
Outdated

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
Comment thread
vidvidex marked this conversation as resolved.
Outdated
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
Loading