Skip to content

Commit 606a084

Browse files
authored
Add Serial Number to Avahi Service for Board Deduplication (#48)
* set serial number for network discovery at post install time * adds space in a comment * set script running before Avahi service * code review fix * code review fix * code review fix
1 parent dbc97ff commit 606a084

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

debian/arduino-app-cli/DEBIAN/postinst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ chown -R arduino:arduino /home/arduino/.local/share/arduino-app-cli
44

55
systemctl enable arduino-app-cli
66
systemctl enable arduino-burn-bootloader
7+
systemctl enable arduino-avahi-serial.service

debian/arduino-app-cli/DEBIAN/prerm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
systemctl disable arduino-app-cli
44
systemctl disable arduino-burn-bootloader
5+
systemctl disable arduino-avahi-serial.service
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Configure Avahi with board serial number
3+
Before=avahi-daemon.service
4+
ConditionPathExists=!/var/lib/arduino-app-cli/avahi_serial_configured.flag
5+
6+
[Service]
7+
Type=oneshot
8+
RemainAfterExit=true
9+
ExecStart=/var/lib/arduino-app-cli/arduino-avahi-serial.sh
10+
ExecStartPost=/bin/mkdir -p /var/lib/arduino-app-cli
11+
ExecStartPost=/bin/touch /var/lib/arduino-app-cli/avahi_serial_configured.flag
12+
13+
StandardOutput=journal
14+
StandardError=journal
15+
16+
[Install]
17+
WantedBy=multi-user.target
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
# Configure Avahi with the serial number.
3+
4+
5+
TARGET_FILE="/etc/avahi/services/arduino.service"
6+
SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number"
7+
8+
echo "Configuring Avahi with serial number for network discovery..."
9+
10+
if [ ! -f "$SERIAL_NUMBER_PATH" ]; then
11+
echo "Error: Serial number path not found at $SERIAL_NUMBER_PATH." >&2
12+
exit 1
13+
fi
14+
15+
16+
if [ ! -w "$TARGET_FILE" ]; then
17+
echo "Error: Target file $TARGET_FILE not found or not writable." >&2
18+
exit 1
19+
fi
20+
21+
SERIAL_NUMBER=$(cat "$SERIAL_NUMBER_PATH")
22+
23+
if [ -z "$SERIAL_NUMBER" ]; then
24+
echo "Error: Serial number file is empty." >&2
25+
exit 1
26+
fi
27+
28+
if grep -q "serial_number=" "$TARGET_FILE"; then
29+
echo "Serial number ($SERIAL_NUMBER) already configured."
30+
exit 0
31+
fi
32+
33+
echo "Adding serial number to $TARGET_FILE..."
34+
sed -i "/<\/service>/i <txt-record>serial_number=${SERIAL_NUMBER}<\/txt-record>" "$TARGET_FILE"
35+
36+
echo "Avahi configuration attempt finished."
37+
exit 0

0 commit comments

Comments
 (0)