Files
bananarchy/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh
T

175 lines
6.0 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh
# see networkmanager documentation for these state names
statenumtoname() {
case "$1" in
"int32 -1") printf "failed" ;;
"int32 0") printf "unknown" ;;
"int32 1") printf "initializing" ;;
"int32 2") printf "locked" ;;
"int32 3") printf "disabled" ;;
"int32 4") printf "disabling" ;;
"int32 5") printf "enabling" ;;
"int32 6") printf "enabled" ;;
"int32 7") printf "searching" ;;
"int32 8") printf "registered" ;;
"int32 9") printf "disconnecting" ;;
"int32 10") printf "connecting" ;;
"int32 11") printf "connected" ;;
*) printf "ERROR" ;;
esac
}
mainloop() {
# get initial modem state and handle SIM2/QMI PIN lock or unprovisioned slot
(
tries=0
newstate=""
while [ "$tries" -lt 15 ]; do
newstate="$(mmcli -m any -K 2>/dev/null | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')"
if [ -n "$newstate" ]; then
break
fi
sleep 2
tries=$((tries + 1))
done
uim_st="$(doas -n /usr/libexec/msm-modem-uim-selection status 2>/dev/null || /usr/libexec/msm-modem-uim-selection status 2>/dev/null || true)"
if printf "%s\n" "$uim_st" | grep -qE "SLOT[12]_PIN1_STATE=(enabled-not-verified|blocked)|SLOT[12]_APP_STATE=(pin1|puk1)"; then
newstate="locked"
elif printf "%s\n" "$uim_st" | grep -qE "SLOT[12]_STATE=present" && printf "%s\n" "$uim_st" | grep -q "^PRIMARY_GW_SLOT=0$"; then
doas -n /usr/libexec/msm-modem-uim-selection boot >/dev/null 2>&1 || true
newstate="$(mmcli -m any -K 2>/dev/null | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')"
fi
if [ -n "$newstate" ]; then
# fake oldstate (boot) and reason (0)
sxmo_hook_modem.sh "boot" "$newstate" "0"
sxmo_hook_statusbar.sh modem
fi
) &
# Monitor for incoming calls
dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Voice',type='signal',member='CallAdded'" |
while read -r line; do
echo "$line" | grep -qE "^signal" && sxmo_modem.sh checkforincomingcalls
done &
# Monitor for incoming texts
dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Messaging',type='signal',member='Added'" |
while read -r line; do
echo "$line" | grep -qE "^signal" && sxmo_modem.sh checkfornewtexts
done &
# Check for new texts periodically
sxmo_run_aligned.sh 300 sxmo_modem.sh checkfornewtexts &
# Monitor for finished calls
dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Call'" |
while read -r line; do
echo "$line" | grep -qE "^signal" && sxmo_modem.sh checkforfinishedcalls
done &
# Periodically remove finished calls
sxmo_run_aligned.sh 300 sxmo_modem.sh checkforfinishedcalls &
# Monitor for modem state change
dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem',type='signal',member='StateChanged'" |
while read -r line; do
if echo "$line" | grep -qE "^signal.*StateChanged"; then
read -r oldstate
read -r newstate
read -r reason
sxmo_hook_modem.sh "$(statenumtoname "$oldstate")" \
"$(statenumtoname "$newstate")" \
"$(echo "$reason" | sed 's/uint32 //')"
sxmo_hook_statusbar.sh modem
fi
done &
# Monitor for 3GPP USSD
dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Modem.Modem3gpp.Ussd'" |
while read -r line; do
case "$line" in
*"/org/freedesktop/ModemManager1/Modem"*)
MODEM=$(echo "$line" | awk -F'[;=]' '{print $6}')
CHANGED=0
;;
*"string \"NetworkNotification"*)
TYPE="NetworkNotification"
CHANGED=1
;;
*"string \"NetworkRequest"*)
TYPE="NetworkRequest"
CHANGED=1
;;
*)
CHANGED=0
;;
esac
if [ "$CHANGED" = "1" ]; then
# Handle unsolicited USSD in background without spawning a terminal window
sxmo_ussd.sh "$MODEM" "$TYPE" "" &
fi
done &
if [ -f "${SXMO_MMS_BASE_DIR:-"$HOME"/.mms/modemmanager}/mms" ]; then
# monitor for received mms
dbus-monitor "interface='org.ofono.mms.Service',type='signal',member='MessageAdded'" |
while read -r line; do
if echo "$line" | grep -q '^object path'; then
MESSAGE_PATH="$(echo "$line" | cut -d\" -f2)"
fi
if echo "$line" | grep -q 'string "received"'; then
sxmo_mms.sh processmms "$MESSAGE_PATH"
fi
done &
fi
if [ -f "${SXMO_VVM_BASE_DIR:-"$HOME"/.vvm/modemmanager}/vvm" ]; then
# monitor for vvm (Visual Voice Mail)
VVM_START=0
dbus-monitor "interface='org.kop316.vvm.Service',type='signal',member='MessageAdded'" |
while read -r line; do
if echo "$line" | grep -q '^object path'; then
VVM_ID="$(echo "$line" | cut -d\" -f2 | rev | cut -d'/' -f1 | rev)"
VVM_START=1
fi
if echo "$line" | grep -q '^]'; then
VVM_START=0
sxmo_vvm.sh processvvm "$VVM_DATE" "$VVM_SENDER" "$VVM_ID" "$VVM_ATTACHMENT"
fi
if [ "$VVM_START" -eq 1 ]; then
if echo "$line" | grep -q '^string "Date"'; then
read -r line
VVM_DATE="$(echo "$line" | cut -d\" -f2)"
elif echo "$line" | grep -q '^string "Sender"'; then
read -r line
VVM_SENDER="$(echo "$line" | cut -d\" -f2)"
elif echo "$line" | grep -q '^string "Attachments"'; then
read -r line
VVM_ATTACHMENT="$(echo "$line" | cut -d\" -f2)"
fi
fi
done &
fi
wait
}
# new session, clean up all phone related files
rm "$XDG_RUNTIME_DIR/sxmo_calls/"* 2>/dev/null || true
rm -f "$XDG_RUNTIME_DIR"/sxmo.ring.pid 2>/dev/null
rm -f "$SXMO_NOTIFDIR"/incomingcall* 2>/dev/null
mainloop