#!/bin/sh # Long-lived supervisor. Logs are replaced before each run to stay bounded on tmpfs. set -u cd /data/opt/alice-backend || exit 1 umask 077 export ALICE_PLAYER=/data/opt/alice-backend/pulse-player.sh exec 9>/tmp/alice-backend-mic.lock flock -n 9 || exit 1 # Match stock boot configuration and expose the physical microphone mute input. echo 1 > /proc/sys/vm/overcommit_memory || echo 'Cannot set overcommit_memory' >&2 if [ ! -d /sys/class/gpio/gpio414 ]; then echo 414 > /sys/class/gpio/export || echo 'Cannot export GPIO414' >&2 if [ -d /sys/class/gpio/gpio414 ]; then echo in > /sys/class/gpio/gpio414/direction || echo 'Cannot set GPIO414 direction' >&2 fi fi child= timer= stopping=0 stop() { stopping=1 for p in "$child" "$timer"; do [ -z "$p" ] || kill -TERM "$p" 2>/dev/null || :; done } trap stop TERM INT HUP reset_duck() { helper=${ALICE_MUSIC_HELPER:-/data/opt/alice-backend/music-control} if [ -x "$helper" ]; then printf '%s\n' '{"name":"duck","arguments":{"active":false}}' | "$helper" >/dev/null 2>&1 || : fi } reset_duck while [ "$stopping" -eq 0 ]; do # Let DHCP and time synchronization finish before accepting voice turns. if [ ! -r device-token ] || [ "$(date +%s)" -lt 1700000000 ] || ! ip route | grep -q '^default '; then sleep 3 & timer=$!; wait "$timer" || :; timer= continue fi DEVICE_TOKEN=$(cat device-token) export DEVICE_TOKEN : > /tmp/alice-station.log ./alice-station -mode wake -voice-threshold 55 -templates /data/opt/alice-backend/wake-templates.json >> /tmp/alice-station.log 2>&1 9>&- & child=$! while [ "$stopping" -eq 0 ] && kill -0 "$child" 2>/dev/null; do if [ "$(wc -c < /tmp/alice-station.log)" -gt 524288 ]; then : >/tmp/alice-station.log; fi sleep 3 & timer=$!; wait "$timer" || :; timer= done wait "$child" || : # A signal interrupts wait; reap the station before releasing the microphone lock. [ "$stopping" -eq 0 ] || wait "$child" 2>/dev/null || : child= [ "$stopping" -ne 0 ] && break sleep 5 & timer=$!; wait "$timer" || :; timer= done reset_duck