~mil/sxmo-utils

a0b91310ad7f47888b0b4c8f4f5052848143f02a — Peter John Hartman 30 days ago 8926890
modemaudio: error checking

This moves the dbus calls to cleaner functions and adds some
error checking/warnings on failures.

Signed-off-by: Stacy Harper <contact@stacyharper.net>
1 files changed, 79 insertions(+), 60 deletions(-)

M scripts/modem/sxmo_modemaudio.sh
M scripts/modem/sxmo_modemaudio.sh => scripts/modem/sxmo_modemaudio.sh +79 -60
@@ 3,6 3,19 @@
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh

ca_dbus_get_prop() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:"$1"
}

ca_dbus_set_prop() {
	dbus-send --session --print-reply --type=method_call \
		--dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio."$1" "$2" |\
		grep -q "boolean true" && return 0 || return 1
}

setup_audio() {
	# implies speaker off, mic not muted
	enable_call_audio_mode


@@ 22,99 35,105 @@ toggle_speaker() {
}

is_muted_mic() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:MicState | \
		tail -n1 | grep -q "uint32 0"
	ca_dbus_get_prop MicState | tail -n1 | grep -q "uint32 0"
}

is_unmuted_mic() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:MicState | \
		tail -n1 | grep -q "uint32 1"
	ca_dbus_get_prop MicState | tail -n1 | grep -q "uint32 1"
}

mute_mic() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.MuteMic boolean:true >/dev/null

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop MuteMic boolean:true; then
		sxmo_hook_statusbar.sh volume
		sxmo_log "Successfully muted mic."
		return 0
	else
		sxmo_notify_user.sh "Failed to mute mic."
		return 1
	fi
}

unmute_mic() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.MuteMic boolean:false >/dev/null

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop MuteMic boolean:false; then
		sxmo_hook_statusbar.sh volume
		sxmo_log "Successfully unmuted mic."
		return 0
	else
		sxmo_notify_user.sh "Failed to unmute mic."
		return 1
	fi
}

is_call_audio_mode() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:AudioMode | \
		tail -n1 | grep -q "uint32 1"
	ca_dbus_get_prop AudioMode | tail -n1 | grep -q "uint32 1"
}

is_default_audio_mode() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:AudioMode | \
		tail -n1 | grep -q "uint32 0"
	ca_dbus_get_prop AudioMode | tail -n1 | grep -q "uint32 0"
}

enable_call_audio_mode() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.SelectMode uint32:1 >/dev/null

	# fixes bug where sometimes we start with speaker on and mic off
	enable_speaker
	disable_speaker
	mute_mic
	unmute_mic

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop SelectMode uint32:1; then
		sxmo_log "Successfully enabled call audio mode."
		sxmo_log "Attempting hack to get things just right."
		# fixes bug where sometimes we start with speaker on and mic off
		enable_speaker
		disable_speaker
		mute_mic
		unmute_mic
		sxmo_hook_statusbar.sh volume
		return 0
	else
		sxmo_notify_user.sh "Failed to enable call audio mode."
		return 1
	fi
}

disable_call_audio_mode() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.SelectMode uint32:0 >/dev/null

	# fixes bug where sometimes we leave call with speaker off and mic on
	disable_speaker
	enable_speaker
	unmute_mic
	mute_mic

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop SelectMode uint32:0; then
		sxmo_log "Successfully disabled call audio mode."
		sxmo_log "Attempting hack to get things just right."
		# fixes bug where sometimes we leave call with speaker off and mic on
		disable_speaker
		enable_speaker
		unmute_mic
		mute_mic
		sxmo_hook_statusbar.sh volume
		return 0
	else
		sxmo_notify_user.sh "Failed to disable call audio mode."
		return 1
	fi
}

is_enabled_speaker() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:SpeakerState | \
		tail -n1 | grep -q "uint32 1"
	ca_dbus_get_prop SpeakerState | tail -n1 | grep -q "uint32 1"
}

is_disabled_speaker() {
	dbus-send --session --print-reply --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.freedesktop.DBus.Properties.Get \
		string:org.mobian_project.CallAudio string:SpeakerState | \
		tail -n1 | grep -q "uint32 0"
	ca_dbus_get_prop SpeakerState | tail -n1 | grep -q "uint32 0"
}

enable_speaker() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.EnableSpeaker boolean:true >/dev/null

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop EnableSpeaker boolean:true; then
		sxmo_hook_statusbar.sh volume
		sxmo_log "Successfully enabled speaker."
		return 0
	else
		sxmo_notify_user.sh "Failed to enable speaker."
		return 1
	fi
}

disable_speaker() {
	dbus-send --session --print-reply --type=method_call --dest=org.mobian_project.CallAudio \
		/org/mobian_project/CallAudio org.mobian_project.CallAudio.EnableSpeaker boolean:false >/dev/null

	sxmo_hook_statusbar.sh volume
	if ca_dbus_set_prop EnableSpeaker boolean:false; then
		sxmo_hook_statusbar.sh volume
		sxmo_log "Successfully disabled speaker."
		return 0
	else
		sxmo_notify_user.sh "Failed to disable speaker."
		return 1
	fi
}

"$@"