diff --git a/tools/build/Dockerfile b/tools/build/Dockerfile index e52e870..a6f4bec 100644 --- a/tools/build/Dockerfile +++ b/tools/build/Dockerfile @@ -85,7 +85,7 @@ RUN mkdir -p /root/.config RUN for svc in \ ipa_fws brightnessd fs_setup gpio init-qcom lte \ avahi-ssh-publish power_monitor power_drop_monitor \ - screen_calibration serial-hostname \ + avahi-alias-publish screen_calibration \ sound varwatch busybox-ntpd \ irsc_util leprop adsp cdsp \ qrtr-ns rmtfs tqftpserv pd-mapper \ diff --git a/userspace/root/etc/runit/1 b/userspace/root/etc/runit/1 index 7fd1d12..36eb64d 100755 --- a/userspace/root/etc/runit/1 +++ b/userspace/root/etc/runit/1 @@ -132,9 +132,8 @@ for part in \ done echo "=> Setting hostname..." -if [ -f /etc/hostname ]; then - hostname "$(cat /etc/hostname)" -else +if ! /usr/comma/set-hostname.sh; then + echo "WARN: failed to derive serial hostname, falling back to comma" hostname comma fi diff --git a/userspace/root/etc/sv/avahi-alias-publish/finish b/userspace/root/etc/sv/avahi-alias-publish/finish new file mode 100755 index 0000000..fbb5e8d --- /dev/null +++ b/userspace/root/etc/sv/avahi-alias-publish/finish @@ -0,0 +1,3 @@ +#!/bin/sh +# Delay restart to avoid spamming console on failure +sleep 5 diff --git a/userspace/root/etc/sv/avahi-alias-publish/run b/userspace/root/etc/sv/avahi-alias-publish/run new file mode 100755 index 0000000..2db0f8d --- /dev/null +++ b/userspace/root/etc/sv/avahi-alias-publish/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec /usr/comma/avahi-publish-aliases.sh diff --git a/userspace/root/usr/comma/avahi-publish-aliases.sh b/userspace/root/usr/comma/avahi-publish-aliases.sh new file mode 100755 index 0000000..8fcb080 --- /dev/null +++ b/userspace/root/usr/comma/avahi-publish-aliases.sh @@ -0,0 +1,89 @@ +#!/bin/bash +set -u +exec 2>&1 + +. /usr/comma/mdns_helpers.sh + +WATCH_INTERFACES=(wlan0 usb0) +PUBLISH_PIDS=() + +cleanup_publishers() { + local pid + + for pid in "${PUBLISH_PIDS[@]}"; do + kill "$pid" 2>/dev/null || true + done + + for pid in "${PUBLISH_PIDS[@]}"; do + wait "$pid" 2>/dev/null || true + done + + PUBLISH_PIDS=() +} + +wait_for_avahi() { + until [ -S /run/avahi-daemon/socket ]; do + sleep 1 + done +} + +get_interface_addresses() { + local iface + + for iface in "${WATCH_INTERFACES[@]}"; do + ip -4 -o addr show dev "$iface" scope global 2>/dev/null | awk '{split($4, cidr, "/"); print cidr[1]}' + done | sort -u +} + +refresh_publishers() { + local alias alias_fqdn addresses address + + alias="$(get_model_alias 2>/dev/null || true)" + cleanup_publishers + + if [ -z "$alias" ]; then + return 0 + fi + + alias_fqdn="${alias}.local" + + wait_for_avahi + addresses="$(get_interface_addresses)" + if [ -z "$addresses" ]; then + echo "No active IPv4 addresses for $alias" + return 0 + fi + + while IFS= read -r address; do + [ -n "$address" ] || continue + avahi-publish -a -R "$alias_fqdn" "$address" & + PUBLISH_PIDS+=("$!") + done <<< "$addresses" + + echo "Publishing $alias for: $(printf '%s ' $addresses)" +} + +handle_monitor_event() { + case "$1" in + *"wlan0"*|*"usb0"*) + refresh_publishers + ;; + esac +} + +trap 'cleanup_publishers' EXIT INT TERM + +if ! get_model_alias >/dev/null 2>&1; then + echo "Skipping model alias publish: unsupported device model" + exec sleep infinity +fi + +refresh_publishers + +while true; do + while IFS= read -r line; do + handle_monitor_event "$line" + done < <(ip monitor address 2>/dev/null) + + sleep 1 +done diff --git a/userspace/root/usr/comma/get-bt-address.sh b/userspace/root/usr/comma/get-bt-address.sh index 20c1374..7123237 100644 --- a/userspace/root/usr/comma/get-bt-address.sh +++ b/userspace/root/usr/comma/get-bt-address.sh @@ -1,7 +1,9 @@ #!/bin/sh set -e -SERIAL="$(/usr/comma/get-serial.sh)" +. /usr/comma/serial_helpers.sh + +SERIAL="$(get_device_serial)" if [ -z "$SERIAL" ]; then exit 0 diff --git a/userspace/root/usr/comma/get-serial.sh b/userspace/root/usr/comma/get-serial.sh index b8981c6..071aca1 100644 --- a/userspace/root/usr/comma/get-serial.sh +++ b/userspace/root/usr/comma/get-serial.sh @@ -1,3 +1,5 @@ #!/bin/sh -sed -n 's/.*androidboot.serialno=\([^ ]*\).*/\1/p' /proc/cmdline +. /usr/comma/serial_helpers.sh + +get_device_serial diff --git a/userspace/root/usr/comma/mdns_helpers.sh b/userspace/root/usr/comma/mdns_helpers.sh new file mode 100755 index 0000000..2e10e5e --- /dev/null +++ b/userspace/root/usr/comma/mdns_helpers.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +. /usr/comma/serial_helpers.sh + +get_device_model() { + local raw_model + + if [ -n "${COMMA_DEVICE_MODEL:-}" ]; then + raw_model="$COMMA_DEVICE_MODEL" + else + raw_model="$(tr -d '\0' < /sys/firmware/devicetree/base/model 2>/dev/null || true)" + fi + + case "$raw_model" in + *tizi*) + printf 'tizi\n' + ;; + *mici*) + printf 'mici\n' + ;; + *) + return 1 + ;; + esac +} + +get_model_alias() { + local model + + model="$(get_device_model)" || return 1 + printf 'comma-%s\n' "$model" +} diff --git a/userspace/root/usr/comma/serial_helpers.sh b/userspace/root/usr/comma/serial_helpers.sh new file mode 100644 index 0000000..5944740 --- /dev/null +++ b/userspace/root/usr/comma/serial_helpers.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +get_device_serial() { + if [ -n "${COMMA_SERIAL:-}" ]; then + printf '%s\n' "$COMMA_SERIAL" + return 0 + fi + + sed -n 's/.*androidboot.serialno=\([^ ]*\).*/\1/p' /proc/cmdline 2>/dev/null | head -n 1 +} + +get_serial_hostname() { + local serial + + serial="$(get_device_serial)" + if [ -n "$serial" ]; then + printf 'comma-%s\n' "$serial" + else + printf 'comma\n' + fi +} diff --git a/userspace/root/usr/comma/set-bt-address.sh b/userspace/root/usr/comma/set-bt-address.sh index 937dfec..0d6f493 100644 --- a/userspace/root/usr/comma/set-bt-address.sh +++ b/userspace/root/usr/comma/set-bt-address.sh @@ -1,7 +1,9 @@ #!/bin/sh set -e -SERIAL="$(/usr/comma/get-serial.sh)" +. /usr/comma/serial_helpers.sh + +SERIAL="$(get_device_serial)" ADDR="$(/usr/comma/get-bt-address.sh)" if [ -z "$SERIAL" ]; then diff --git a/userspace/root/usr/comma/set-hostname.sh b/userspace/root/usr/comma/set-hostname.sh index 4ea0e10..edcd55d 100755 --- a/userspace/root/usr/comma/set-hostname.sh +++ b/userspace/root/usr/comma/set-hostname.sh @@ -1,6 +1,10 @@ #!/bin/bash set -e -SERIAL="$(/usr/comma/get-serial.sh)" +. /usr/comma/mdns_helpers.sh + +HOSTNAME="$(get_serial_hostname)" +echo "hostname: '$HOSTNAME'" +SERIAL="$(get_device_serial)" echo "serial: '$SERIAL'" -sysctl kernel.hostname="comma-$SERIAL" +sysctl kernel.hostname="$HOSTNAME" >/dev/null diff --git a/userspace/root/usr/comma/set_ncm.sh b/userspace/root/usr/comma/set_ncm.sh index b9f11af..138439b 100755 --- a/userspace/root/usr/comma/set_ncm.sh +++ b/userspace/root/usr/comma/set_ncm.sh @@ -2,6 +2,8 @@ # Enables/disables USB NCM networking based on UsbNcmEnabled param. # Called by ncm-param-watcher on param changes. +. /usr/comma/serial_helpers.sh + GADGET=/config/usb_gadget/g1 USB_IF="usb0" USB_ADDR="192.168.42.2/24" @@ -29,7 +31,7 @@ ensure_gadget_base() { echo 250 > configs/c.1/MaxPower local serial model - serial="$(/usr/comma/get-serial.sh)" + serial="$(get_device_serial)" model="$(tr -d '\0' < /sys/firmware/devicetree/base/model 2>/dev/null || true)" echo "$serial" > strings/0x409/serialnumber