diff --git a/scripts/modem/sxmo_modemcall.sh b/scripts/modem/sxmo_modemcall.sh index 77fb2bc..42d595f 100755 --- a/scripts/modem/sxmo_modemcall.sh +++ b/scripts/modem/sxmo_modemcall.sh @@ -1,16 +1,17 @@ #!/usr/bin/env sh LOGDIR="$XDG_DATA_HOME"/sxmo/modem -ALSASTATEFILE="/tmp/precall.alsa.state" +ALSASTATEFILE="$XDG_CACHE_HOME"/precall.alsa.state +CACHEDIR="$XDG_CACHE_HOME"/sxmo trap "gracefulexit" INT TERM modem_n() { MODEMS="$(mmcli -L)" - echo "$MODEMS" | grep -qoE 'Modem\/([0-9]+)' || fatalerr "Couldn't find modem - is your modem enabled?" + echo "$MODEMS" | grep -qoE 'Modem\/([0-9]+)' || finish "Couldn't find modem - is your modem enabled?" echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2 } -fatalerr() { +finish() { # E.g. hangup all calls, switch back to default audio, notify user, and die sxmo_vibratepine 1000 & mmcli -m "$(modem_n)" --voice-hangup-all @@ -24,14 +25,18 @@ fatalerr() { else alsactl --file /usr/share/sxmo/alsa/default_alsa_sound.conf restore fi - echo "$1">&2 - notify-send "$1" setsid -f sh -c 'sleep 2; sxmo_statusbarupdate.sh' + if [ -n "$1" ]; then + echo "$1">&2 + notify-send "$1" + fi kill -9 0 + pkill -9 dmenu #just in case the call menu survived somehow? + exit 1 } gracefulexit() { - fatalerr "Terminated via SIGTERM/SIGINT" + finish "Call ended" } @@ -39,7 +44,7 @@ modem_cmd_errcheck() { RES="$(mmcli "$@" 2>&1)" OK="$?" echo "Command: mmcli $*">&2 - if [ "$OK" != 0 ]; then fatalerr "Problem executing mmcli command!\n$RES"; fi + if [ "$OK" != 0 ]; then finish "Problem executing mmcli command!\n$RES"; fi echo "$RES" } @@ -97,11 +102,13 @@ acceptcall() { echo "Invoking pickup hook (async)">&2 "$XDG_CONFIG_HOME/sxmo/hooks/pickup" & fi + touch "$CACHEDIR/${CALLID}.pickedupcall" #this signals that we picked this call up + #to other asynchronously running processes modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --accept log_event "call_pickup" "$CALLID" echo "Picked up call $CALLID">&2 else - fatalerr "Couldn't initialize call with callid <$CALLID>; unknown direction <$DIRECTION>" + finish "Couldn't initialize call with callid <$CALLID>; unknown direction <$DIRECTION>" fi alsactl --file "$ALSASTATEFILE" store } @@ -143,11 +150,11 @@ incallmonitor() { while true; do sxmo_statusbarupdate.sh if mmcli -m "$(modem_n)" -K -o "$CALLID" | grep -E "^call.properties.state.+terminated"; then - mmcli -m "$(modem_n)" --voice-delete-call="$CALLID" + #note: deletion will be handled asynchronously by sxmo_modemmonitor's checkforfinishedcalls() if [ "$NUMBER" = "--" ]; then - fatalerr "Call with unknown number terminated" + finish "Call with unknown number terminated" else - fatalerr "Call with $NUMBER terminated" + finish "Call with $NUMBER terminated" fi fi echo "Call still in progress">&2 @@ -214,5 +221,5 @@ pickup() { incallmenuloop "$1" } -modem_n || fatalerr "Couldn't determine modem number - is modem online?" +modem_n || finish "Couldn't determine modem number - is modem online?" "$@" diff --git a/scripts/modem/sxmo_modemmonitor.sh b/scripts/modem/sxmo_modemmonitor.sh index 4e758cc..946e221 100755 --- a/scripts/modem/sxmo_modemmonitor.sh +++ b/scripts/modem/sxmo_modemmonitor.sh @@ -1,6 +1,7 @@ #!/usr/bin/env sh LOGDIR="$XDG_DATA_HOME"/sxmo/modem NOTIFDIR="$XDG_DATA_HOME"/sxmo/notifications +CACHEDIR="$XDG_CACHE_HOME"/sxmo trap "gracefulexit" INT TERM err() { @@ -48,35 +49,41 @@ lookupcontactname() { fi } -checkformissedcalls() { - if pgrep -vf sxmo_modemcall.sh; then - # Add a notification for every missed call - # Note sxmo_modemcall.sh cleanups/delete the callid from the modem; so - # effectivly any incoming call thats terminated here is a missed call! - for MISSEDCALLID in $( - mmcli -m "$(modem_n)" --voice-list-calls | - grep incoming | - grep terminated | - grep -oE "Call\/[0-9]+" | - cut -d'/' -f2 - ); do - MISSEDNUMBER="$(lookupnumberfromcallid "$MISSEDCALLID")" - mmcli -m "$(modem_n)" --voice-delete-call "$MISSEDCALLID" - rm -f "$NOTIFDIR/incomingcall_${MISSEDCALLID}_notification" - - - TIME="$(date --iso-8601=seconds)" - mkdir -p "$LOGDIR" - printf %b "$TIME\tcall_missed\t$MISSEDNUMBER\n" >> "$LOGDIR/modemlog.tsv" - - CONTACT="$(lookupcontactname "$MISSEDNUMBER")" +checkforfinishedcalls() { + #find all finished calls + for FINISHEDCALLID in $( + mmcli -m "$(modem_n)" --voice-list-calls | + grep incoming | + grep terminated | + grep -oE "Call\/[0-9]+" | + cut -d'/' -f2 + ); do + FINISHEDNUMBER="$(lookupnumberfromcallid "$FINISHEDCALLID")" + mmcli -m "$(modem_n)" --voice-delete-call "$FINISHEDCALLID" + rm -f "$NOTIFDIR/incomingcall_${FINISHEDCALLID}_notification" + + TIME="$(date --iso-8601=seconds)" + mkdir -p "$LOGDIR" + if [ -f "$CACHEDIR/${FINISHEDCALLID}.pickedupcall" ]; then + #this call was picked up + pkill -f sxmo_modemcall.sh #kill call (softly) in case it is still in progress (remote party hung up) + echo "Finished call from $FINISHEDNUMBER">&2 + rm -f "$CACHEDIR/${FINISHEDCALLID}.pickedupcall" + printf %b "$TIME\tcall_finished\t$FINISHEDNUMBER\n" >> "$LOGDIR/modemlog.tsv" + else + #this is a missed call + # Add a notification for every missed call + echo "Missed call from $FINISHEDNUMBER">&2 + printf %b "$TIME\tcall_missed\t$FINISHEDNUMBER\n" >> "$LOGDIR/modemlog.tsv" + + CONTACT="$(lookupcontactname "$FINISHEDNUMBER")" sxmo_notificationwrite.sh \ random \ "st -f Terminus-20 -e sh -c \"echo 'Missed call from $CONTACT at $(date)' && read\"" \ none \ "Missed call - $CONTACT" - done - fi + fi + done } checkforincomingcalls() { @@ -151,26 +158,26 @@ checkfornewtexts() { } mainloop() { - checkformissedcalls + checkforfinishedcalls checkforincomingcalls checkfornewtexts # Monitor for incoming calls dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Voice',type='signal',member='CallAdded'" | \ - while read -r; do + while read -r line; do checkforincomingcalls done & # Monitor for incoming texts dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Messaging',type='signal',member='Added'" | \ - while read -r; do + while read -r line; do checkfornewtexts done & - # Monitor for missed calls + # Monitor for finished calls dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Call'" | \ - while read -r; do - checkformissedcalls + while read -r line; do + checkforfinishedcalls done & wait @@ -178,4 +185,5 @@ mainloop() { wait } +rm -f "$CACHEDIR"/*.pickedupcall 2>/dev/null #new session, forget all calls we picked up previously mainloop