Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion tools/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
5 changes: 2 additions & 3 deletions userspace/root/etc/runit/1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions userspace/root/etc/sv/avahi-alias-publish/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
# Delay restart to avoid spamming console on failure
sleep 5
2 changes: 2 additions & 0 deletions userspace/root/etc/sv/avahi-alias-publish/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
exec /usr/comma/avahi-publish-aliases.sh
89 changes: 89 additions & 0 deletions userspace/root/usr/comma/avahi-publish-aliases.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion userspace/root/usr/comma/get-bt-address.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion userspace/root/usr/comma/get-serial.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/sh

sed -n 's/.*androidboot.serialno=\([^ ]*\).*/\1/p' /proc/cmdline
. /usr/comma/serial_helpers.sh

get_device_serial
32 changes: 32 additions & 0 deletions userspace/root/usr/comma/mdns_helpers.sh
Original file line number Diff line number Diff line change
@@ -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"
}
21 changes: 21 additions & 0 deletions userspace/root/usr/comma/serial_helpers.sh
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion userspace/root/usr/comma/set-bt-address.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions userspace/root/usr/comma/set-hostname.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion userspace/root/usr/comma/set_ncm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading