Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions debian/arduino-app-cli/DEBIAN/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ chown -R arduino:arduino /home/arduino/.local/share/arduino-app-cli

systemctl enable arduino-app-cli
systemctl enable arduino-burn-bootloader
systemctl enable arduino-avahi-serial.service
Copy link
Contributor

@dido18 dido18 Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the .service suffix needed ?

Suggested change
systemctl enable arduino-avahi-serial.service
systemctl enable arduino-avahi-serial

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
systemctl enable arduino-avahi-serial.service
systemctl enable arduino-avahi-serial

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Configure Avahi with board serial number
After=avahi-daemon.service
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do it before not after

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it "after" to ensure I could find the file "/etc/avahi/services/arduino.service".
Also, we don't need to restart Avahi after modifying the file.
(Btw I change it in "Before")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it "after" to ensure I could find the file "/etc/avahi/services/arduino.service".

The file should be always there

Also, we don't need to restart Avahi after modifying the file.

Not sure about that I think it need a restart

ConditionPathExists=!/var/lib/arduino/avahi_serial_configured.flag

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/sbin/arduino-avahi-serial.sh
ExecStartPost=/bin/mkdir -p /var/lib/arduino
ExecStartPost=/bin/touch /var/lib/arduino/avahi_serial_configured.flag

StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
46 changes: 46 additions & 0 deletions debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh
#
# Configure Avahi with the serial number.
# This operation is non-blocking: if it fails,
# the script will exit with success in order to
# not to interrupt the post-install process.
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this script should and could fail. If there is an issue, I would prefer that it start the next time instead of never

#

TARGET_FILE="/etc/avahi/services/arduino.service"
MARKER_LINE="</service>"
SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number"

echo "Configuring Avahi with serial number for network discovery..."

if [ ! -f "$SERIAL_NUMBER_PATH" ]; then
echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH. Skipping." >&2
exit 0
Comment on lines +16 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH. Skipping." >&2
exit 0
echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH" >&2
exit 1

fi


if [ ! -w "$TARGET_FILE" ]; then
echo "Warning: Target file $TARGET_FILE not found or not writable. Skipping." >&2
exit 0
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Warning: Target file $TARGET_FILE not found or not writable. Skipping." >&2
exit 0
echo "Warning: Target file $TARGET_FILE not found or not writable." >&2
exit 1

fi

SERIAL_NUMBER=$(cat "$SERIAL_NUMBER_PATH")

if [ -z "$SERIAL_NUMBER" ]; then
echo "Warning: Serial number file is empty. Skipping." >&2
exit 0
Comment on lines +29 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Warning: Serial number file is empty. Skipping." >&2
exit 0
echo "Warning: Serial number file is empty." >&2
exit 1

fi

if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then
echo "Serial number ($SERIAL_NUMBER) already configured. Skipping."
exit 0
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Serial number ($SERIAL_NUMBER) already configured. Skipping."
exit 0
echo "Serial number ($SERIAL_NUMBER) already configured."
exit 1

fi

SERIAL_NUMBER_ESCAPED=$(echo "$SERIAL_NUMBER" | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/\&/\\\&/g')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serial number on the unoq is just a number I don't think you need to escape anything

NEW_LINE=" <txt-record>serial_number=${SERIAL_NUMBER_ESCAPED}</txt-record>"

echo "Adding serial number to $TARGET_FILE..."

sed -i "\#${MARKER_LINE}#i ${NEW_LINE}" "$TARGET_FILE"

echo "Avahi configuration attempt finished."
exit 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exit 0