-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_rule.sh
More file actions
executable file
·65 lines (54 loc) · 2.29 KB
/
add_rule.sh
File metadata and controls
executable file
·65 lines (54 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
# add_rule.sh - Create a persistent udev symlink for the Rosmaster board.
# Usage: sudo ./add_rule.sh myserial
# Optional second argument: vendor:product (default 1a86:7523 for CH340/CH34x)
# Example with custom name and default VID:PID:
# sudo ./add_rule.sh my_ros_board
# Example with custom VID:PID (hex):
# sudo ./add_rule.sh my_ros_board 1a86:7523
# After running, replug the device or run:
# sudo udevadm control --reload-rules && sudo udevadm trigger
# Then verify:
# ls -l /dev/<name>
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "[ERROR] Please run as root (use sudo)." >&2
exit 1
fi
NAME="${1:-}"
if [[ -z "$NAME" ]]; then
echo "Usage: sudo $0 <symlink_name> [vendor:product]" >&2
exit 1
fi
VIDPID="${2:-1a86:7523}"
if [[ ! "$VIDPID" =~ ^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$ ]]; then
echo "[ERROR] vendor:product must look like 1a86:7523" >&2
exit 1
fi
VID="${VIDPID%%:*}"
PID="${VIDPID##*:}"
RULE_FILE="/etc/udev/rules.d/99-rosmaster.rules"
# Correct udev rule (previous version had a syntax typo: ATTRS{idVendor}\"==\" which invalidated the line)
# Narrow with SUBSYSTEM and use standard 0666 permissions (0777 unnecessary). Add SYMLINK with provided name.
RULE_LINE="SUBSYSTEM==\"tty\", KERNEL==\"ttyUSB*\", ATTRS{idVendor}==\"$VID\", ATTRS{idProduct}==\"$PID\", MODE:=\"0666\", SYMLINK+=\"$NAME\""
# Write (replace or append uniquely)
if [[ -f "$RULE_FILE" ]]; then
# Remove any previous line for this name
grep -v "SYMLINK+=\\\"$NAME\\\"" "$RULE_FILE" > "${RULE_FILE}.tmp" || true
mv "${RULE_FILE}.tmp" "$RULE_FILE"
fi
echo "$RULE_LINE" >> "$RULE_FILE"
echo "[INFO] Rule written to $RULE_FILE:" >&2
cat "$RULE_FILE" >&2
echo "[INFO] Reloading udev rules..." >&2
udevadm control --reload-rules
udevadm trigger || true
echo "[INFO] Testing rule against existing ttyUSB devices (if any)..." >&2
for dev in /sys/class/tty/ttyUSB*; do
[[ -e "$dev" ]] || continue
echo "[DEBUG] udevadm test for $dev" >&2
udevadm test "$dev" 2>&1 | grep -E "rosmaster|$NAME" || true
done
echo "[INFO] Done. If the symlink isn't present yet, unplug and replug the board." >&2
echo "[INFO] Verify with: ls -l /dev/$NAME" >&2
echo "[HINT] If it still does not appear: (1) confirm VID:PID with: udevadm info -a -n /dev/ttyUSB0 | egrep 'idVendor|idProduct' (2) ensure it matches $VID:$PID" >&2