Skip to content

Commit 81917c2

Browse files
committed
Update comments for serial device script
1 parent 04d2c31 commit 81917c2

File tree

1 file changed

+106
-19
lines changed

1 file changed

+106
-19
lines changed
Lines changed: 106 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,134 @@
11
#!/bin/bash
2-
3-
# Script to set up udev rules for ttyUSB0 and ttyUSB1 devices
4-
# Gives chmod 777 permissions to these devices when plugged in
5-
# Usage:
6-
# sudo ./setup_ttyusb_udev.sh enable - Enable udev rules
7-
# sudo ./setup_ttyusb_udev.sh disable - Disable udev rules
2+
# configure_serial_device_access.sh - Configure automatic permissions for USB serial devices
3+
#
4+
# DESCRIPTION:
5+
# This script configures udev rules to automatically set permissions on USB serial devices
6+
# when they are plugged in. This is needed for the Coffee Buddy robot project to access
7+
# Dynamixel servo controllers and other USB-to-serial devices without requiring sudo.
8+
#
9+
# The script handles two types of devices:
10+
# 1. Generic USB serial devices (ttyUSB0, ttyUSB1) - for general serial communication
11+
# 2. U2D2 device (Dynamixel interface) - specific hardware for servo control
12+
#
13+
# ⚠️ SECURITY WARNING:
14+
# This script sets 777 permissions (read/write for everyone) on ttyUSB devices.
15+
# This is a SECURITY RISK as it allows any user on the system to access these devices.
16+
#
17+
# For better security, consider adding your user to the 'dialout' group instead:
18+
# sudo usermod -a -G dialout $USER
19+
# (requires logout/login to take effect)
20+
#
21+
# HARDWARE SUPPORTED:
22+
# - Generic USB-to-Serial converters (ttyUSB0, ttyUSB1)
23+
# - U2D2 Dynamixel Interface (ROBOTIS U2D2 servo controller interface)
24+
# - FTDI-based serial devices (vendor ID 0403, product ID 6014)
25+
#
26+
# USAGE:
27+
# sudo ./configure_serial_device_access.sh enable - Enable automatic permissions
28+
# sudo ./configure_serial_device_access.sh disable - Remove automatic permissions
29+
#
30+
# REQUIREMENTS:
31+
# - Root privileges (uses sudo)
32+
# - udev system (standard on most Linux distributions)
33+
#
34+
# WHAT IT CREATES:
35+
# Creates /etc/udev/rules.d/99-ttyusb-permissions.rules with device permission rules
36+
#
37+
# WHEN YOU NEED THIS:
38+
# - You get "Permission denied" errors when accessing /dev/ttyUSB* devices
39+
# - Your ROS2 nodes can't communicate with Dynamixel servos
40+
# - You want to avoid using sudo for every serial device access
41+
#
42+
# ALTERNATIVES (More Secure):
43+
# Instead of 777 permissions, you can:
44+
# 1. Add user to dialout group: sudo usermod -a -G dialout $USER
45+
# 2. Use specific device permissions with group access
46+
# 3. Use udev rules with GROUP="dialout", MODE="0660"
47+
#
48+
# VERIFICATION:
49+
# After enabling, check: ls -l /dev/ttyUSB*
50+
# You should see permissions like: crw-rw-rw- (for 777) or crw-rw---- (for 660)
851

952
# Check if running as root
1053
if [ "$EUID" -ne 0 ]; then
11-
echo "This script requires root privileges. Please run with sudo."
54+
echo "❌ Error: This script requires root privileges."
55+
echo " Please run with: sudo $0 [enable|disable]"
1256
exit 1
1357
fi
1458

1559
# Define the udev rule file path
16-
UDEV_RULE_FILE="/etc/udev/rules.d/99-ttyusb-permissions.rules"
60+
UDEV_RULE_FILE="/etc/udev/rules.d/99-coffee-buddy-serial-permissions.rules"
1761

1862
# Check for command line arguments
1963
if [ $# -ne 1 ]; then
2064
echo "Usage: $0 [enable|disable]"
65+
echo ""
66+
echo "Examples:"
67+
echo " sudo $0 enable # Enable automatic USB serial device permissions"
68+
echo " sudo $0 disable # Remove automatic permissions"
2169
exit 1
2270
fi
2371

2472
case "$1" in
2573
enable)
74+
echo "=== Configuring USB Serial Device Access ==="
75+
echo ""
76+
echo "⚠️ Security Warning:"
77+
echo " This will set 777 permissions on ttyUSB devices (accessible by everyone)"
78+
echo " For better security, consider adding users to 'dialout' group instead"
79+
echo ""
80+
2681
# Create the udev rule file
27-
echo "Creating udev rule file..."
82+
echo "Creating udev rules for USB serial devices..."
2883
cat > "$UDEV_RULE_FILE" << 'EOF'
29-
# Set permissions for ttyUSB0 and ttyUSB1 devices
84+
# Coffee Buddy Robot - USB Serial Device Permissions
85+
#
86+
# Generic USB-to-Serial devices (ttyUSB0, ttyUSB1)
87+
# WARNING: 777 permissions allow access by any user (security risk)
3088
KERNEL=="ttyUSB[0-1]", MODE="0777"
3189
32-
# U2D2 specific rule (FT583QPG) with dialout group
90+
# ROBOTIS U2D2 Dynamixel Interface (More secure with dialout group)
91+
# This device is specifically used for Dynamixel servo communication
3392
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", ATTRS{serial}=="FT583QPG", GROUP="dialout", MODE="0660"
93+
94+
# Alternative secure rule for all USB serial devices (commented out)
95+
# Uncomment this and comment out the 777 rule above for better security
96+
# KERNEL=="ttyUSB*", GROUP="dialout", MODE="0660"
3497
EOF
3598

3699
# Check if the file was created successfully
37100
if [ ! -f "$UDEV_RULE_FILE" ]; then
38-
echo "Failed to create udev rule file."
101+
echo "Failed to create udev rule file at $UDEV_RULE_FILE"
39102
exit 1
40103
fi
41104

42-
echo "Udev rule file created successfully."
105+
echo "Udev rule file created: $UDEV_RULE_FILE"
43106

44107
# Reload udev rules and trigger them
45-
echo "Applying udev rules..."
108+
echo ""
109+
echo "Applying udev rules to system..."
46110
udevadm control --reload-rules
47111
udevadm trigger
48112

49-
echo "Done! ttyUSB0 and ttyUSB1 devices will now have 777 permissions when connected."
50-
echo "The U2D2 device will have dialout group permissions."
113+
echo ""
114+
echo "✓ USB serial device access configured!"
115+
echo ""
116+
echo "Configured devices:"
117+
echo " • ttyUSB0, ttyUSB1: 777 permissions (⚠️ less secure)"
118+
echo " • U2D2 Dynamixel interface: dialout group, 660 permissions (✓ secure)"
119+
echo ""
120+
echo "To verify, plug in a USB serial device and check:"
121+
echo " ls -l /dev/ttyUSB*"
122+
echo ""
123+
echo "For better security in the future, consider:"
124+
echo " sudo usermod -a -G dialout \$USER"
125+
echo " (then logout/login and use the secure udev rules)"
51126
;;
52127

53128
disable)
129+
echo "=== Removing USB Serial Device Rules ==="
130+
echo ""
131+
54132
# Remove the udev rule file if it exists
55133
if [ -f "$UDEV_RULE_FILE" ]; then
56134
echo "Removing udev rule file..."
@@ -60,15 +138,24 @@ EOF
60138
echo "Reloading udev rules..."
61139
udevadm control --reload-rules
62140

63-
echo "Udev rules disabled successfully."
141+
echo "✓ USB serial device rules removed successfully"
142+
echo ""
143+
echo "USB serial devices will now use default system permissions."
144+
echo "You may need to use sudo or add users to dialout group for access."
64145
else
65-
echo "Udev rule file not found. Nothing to disable."
146+
echo "ℹ️ No udev rule file found at $UDEV_RULE_FILE"
147+
echo " USB serial device rules are not currently enabled."
66148
fi
67149
;;
68150

69151
*)
70-
echo "Invalid option: $1"
152+
echo "❌ Invalid option: $1"
153+
echo ""
71154
echo "Usage: $0 [enable|disable]"
155+
echo ""
156+
echo "Examples:"
157+
echo " sudo $0 enable # Enable automatic USB serial device permissions"
158+
echo " sudo $0 disable # Remove automatic permissions"
72159
exit 1
73160
;;
74161
esac

0 commit comments

Comments
 (0)