#!/bin/sh
# AP half of a handset call on the S10e: calliope runs the voice path (VSS <->
# codec/amps), our two PCMs only keep the codec, amps and UAIFs powered.
# Called by exynos-cpd: exynos-callaudio start|stop
#
# The firmware boots only with the card's PCMs closed. PipeWire stays up for
# the call, and with it everything else it has: Bluetooth's audio endpoints, a
# car's hands-free gateway and its link (stopping PipeWire for a call dropped
# the car, 2026-11-24). The card goes to the "off" profile for the boot, which
# closes its PCMs, and back to its own profile after the call, never saved
# (exynos-callaudio-pw). A kernel with the call's own PCMs (hw:0,4-6) has the
# UCM "Voice Call" verb on them, for recording the call, playing into it and
# playing to this phone during it: the card is on "Voice Call" for the call
# then; without it the card stays off until the call ends. Only when the card
# has no PipeWire profile, its PCMs do not close, or the firmware does not boot
# with PipeWire up, does PipeWire stop for the call, as before. Which way a
# call went is in the journal (exynos-callaudio).
exec 9>/run/exynos-callaudio.lock
flock 9
D=/sys/kernel/debug/exynos9820-abox/firmware
S=/run/exynos-callaudio.state		# the profile to go back to, or "stopped"
U=$(id -u user 2>/dev/null)
CA=org.mobian_project.CallAudio
pw() { [ -n "$U" ] && su user -c "XDG_RUNTIME_DIR=/run/user/$U systemctl --user $1 pipewire.socket pipewire-pulse.socket pipewire pipewire-pulse wireplumber"; }
asuser() { [ -n "$U" ] && runuser -u user -- env XDG_RUNTIME_DIR=/run/user/$U DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$U/bus "$@" 9>&-; }
prof() { asuser /usr/local/sbin/exynos-callaudio-pw "$@"; }
log() { logger -t exynos-callaudio "$*"; }

# every PCM of the card closed, within 2 s
closed() {
	n=0
	while grep -qv '^closed' /proc/asound/card0/pcm*/sub*/status 2>/dev/null; do
		n=$((n + 1))
		[ $n -ge 20 ] && return 1
		sleep 0.1
	done
	return 0
}

# Leaving HiFi ran its Mic DisableSequence, which takes the mics off AIF1TX:
# exynos-callaudiod's mute state back on them.
mic() {
	m=false
	[ "$(asuser busctl --user get-property $CA /org/mobian_project/CallAudio $CA MicState 2>/dev/null)" = "u 0" ] && m=true
	asuser busctl --user call $CA /org/mobian_project/CallAudio $CA MuteMic b $m >/dev/null 2>&1 && return
	log "exynos-callaudiod did not answer: mics on"
	amixer -q -c0 cset name='AIF1TX1 Input 1' IN3L
	amixer -q -c0 cset name='AIF1TX2 Input 1' IN4L
}

# the card as it was before the call
back() {
	st=$(cat $S 2>/dev/null)
	rm -f $S
	case "$st" in
	""|stopped)
		pw start ;;
	*)
		prof set "$st" && log "stop: card profile $st back" && return
		log "stop: profile $st failed: PipeWire restarts"
		pw stop
		pw start ;;
	esac
}

case "$1" in
start)
	grep -q "in_call 1" $D && exit 0
	prev=$(prof get 2>/dev/null)
	if [ -n "$prev" ] && { [ "$prev" = off ] || prof set off; } && closed; then
		echo "$prev" > $S
		log "start: card profile $prev off for the call, PipeWire running"
	else
		if [ -z "$prev" ]; then
			log "start: no PipeWire profile for the card: PipeWire stops for this call"
		else
			log "start: the card kept a PCM open: PipeWire stops for this call"
		fi
		echo stopped > $S
		pw stop; sleep 0.5		# the firmware boots only with our PCMs closed
	fi
	if ! echo boot > $D; then
		ok=
		if [ "$(cat $S)" != stopped ]; then
			# something opened a PCM again after all: once more with PipeWire stopped, as before
			log "start: firmware boot failed: again with PipeWire stopped"
			echo stopped > $S
			pw stop; sleep 0.5
			echo boot > $D && ok=1
		fi
		if [ -z "$ok" ]; then
			log "firmware boot failed"
			back
			exit 1
		fi
	fi
	sleep 0.2
	aplay -q -D hw:0,1 -f S32_LE -r 48000 -c 4 /dev/zero </dev/null >/dev/null 2>&1 9>&- &
	arecord -q -D hw:0,0 -f S32_LE -r 48000 -c 2 /dev/null </dev/null >/dev/null 2>&1 9>&- &
	sleep 0.3
	echo call > $D || log "call setup failed"
	if [ "$(cat $S)" != stopped ]; then
		if prof has "Voice Call"; then
			prof set "Voice Call" || log "Voice Call profile failed: nothing records or plays into this call"
		else
			log "no Voice Call profile (a kernel without the call's PCMs): the card off until the call ends"
		fi
		mic
	fi
	log "$(head -1 $D)" ;;
stop)
	# the call's own PCMs close before the firmware goes, as with PipeWire stopped
	case "$(cat $S 2>/dev/null)" in
	""|stopped) ;;
	*) prof set off || log "profile off failed" ;;
	esac
	echo hangup > $D 2>/dev/null
	killall -q aplay arecord; sleep 0.3
	echo stop > $D
	amixer -q -c0 cset name='SPK Digital PCM Volume' 769
	amixer -q -c0 cset name='RCV Digital PCM Volume' 761	# gsm2sipd mutes it for a gateway call
	amixer -q -c0 cset name='RCV Analog PCM Volume' 17
	back
	log "$(head -1 $D)" ;;
esac
