From a4b2724c4aeaeda2e3608e051299cecbb228114a Mon Sep 17 00:00:00 2001 From: Miles Alan Date: Thu, 18 Jun 2020 19:05:09 -0500 Subject: [PATCH] Clean up modem functionality & don't ever kill dmenu; move call monitoring to call script --- scripts/core/sxmo_appmenu.sh | 44 ++++--- scripts/modem/sxmo_modemcall.sh | 147 ++++++++++++--------- scripts/modem/sxmo_modemmonitor.sh | 159 ++++++++++------------- scripts/modem/sxmo_modemmonitortoggle.sh | 2 +- 4 files changed, 178 insertions(+), 174 deletions(-) diff --git a/scripts/core/sxmo_appmenu.sh b/scripts/core/sxmo_appmenu.sh index db68212..253f4eb 100755 --- a/scripts/core/sxmo_appmenu.sh +++ b/scripts/core/sxmo_appmenu.sh @@ -1,6 +1,12 @@ #!/usr/bin/env sh +signal gracefulexit INT TERM WIN=$(xdotool getwindowfocus) +gracefulexit() { + echo "Gracefully exiting $0" + kill -9 0 +} + programchoicesinit() { XPROPOUT="$(xprop -id "$(xdotool getactivewindow)")" WMCLASS="${1:-$(echo "$XPROPOUT" | grep WM_CLASS | cut -d ' ' -f3-)}" @@ -221,29 +227,27 @@ quit() { } mainloop() { - DMENUIDX=0 - PICKED="" - ARGS="$*" - - while : - do - # E.g. sets PROGCHOICES - getprogchoices "$ARGS" - - PICKED="$( - echo "$PROGCHOICES" | - cut -d'^' -f1 | - dmenu -idx $DMENUIDX -l 14 -c -fn "Terminus-30" -p "$WINNAME" - )" + getprogchoices "$ARGS" + echo "$PROGCHOICES" | + cut -d'^' -f1 | + dmenu -idx "$DMENUIDX" -l 14 -c -fn "Terminus-30" -p "$WINNAME" | ( + PICKED="$(cat)" LOOP="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f2)" CMD="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f3)" DMENUIDX="$(echo "$PROGCHOICES" | grep -F -n "$PICKED" | cut -d ':' -f1)" echo "Eval: <$CMD> from picked <$PICKED> with loop <$LOOP>" - eval "$CMD" - echo "$LOOP" | grep 1 || quit - done + if echo "$LOOP" | grep 1; then + eval "$CMD" + mainloop + else + eval "$CMD" & + quit + fi + ) & wait } -pgrep -f sxmo_appmenu.sh | grep -Ev "^${$}$" | xargs kill -9 -pkill -9 dmenu -mainloop "$@" \ No newline at end of file +pgrep -f sxmo_appmenu.sh | grep -Ev "^${$}$" | xargs kill -TERM +DMENUIDX=0 +PICKED="" +ARGS="$*" +mainloop \ No newline at end of file diff --git a/scripts/modem/sxmo_modemcall.sh b/scripts/modem/sxmo_modemcall.sh index 0b107d7..59ce85d 100755 --- a/scripts/modem/sxmo_modemcall.sh +++ b/scripts/modem/sxmo_modemcall.sh @@ -1,16 +1,23 @@ #!/usr/bin/env sh LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem -trap "kill 0" INT +trap "gracefulexit" INT TERM -err() { - printf %b "$1" | dmenu -fn Terminus-20 -c -l 10 +fatalerr() { + # E.g. hangup all calls, switch back to default audio, notify user, and die + sxmo_vibratepine 1000 + mmcli -m "$(mmcli -L | grep -qoE 'Modem\/([0-9]+)')" --voice-hangup-all alsactl --file /usr/share/sxmo/default_alsa_sound.conf restore + notify-send "$1" kill -9 0 } +gracefulexit() { + fatalerr "Terminated via SIGTERM/SIGINT" +} + modem_n() { MODEMS="$(mmcli -L)" - echo "$MODEMS" | grep -qoE 'Modem\/([0-9]+)' || err "Couldn't find modem - is your modem enabled?" + echo "$MODEMS" | grep -qoE 'Modem\/([0-9]+)' || fatalerr "Couldn't find modem - is your modem enabled?" echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2 } @@ -25,7 +32,7 @@ modem_cmd_errcheck() { RES="$(mmcli $@ 2>&1)" OK="$?" echo "Command: mmcli $*" - if [ "$OK" != 0 ]; then err "Problem executing mmcli command!\n$RES"; fi + if [ "$OK" != 0 ]; then fatalerr "Problem executing mmcli command!\n$RES"; fi echo "$RES" } @@ -64,7 +71,7 @@ toggleflagset() { dialmenu() { - CONTACTS="$(contacts)" + CONTACTS="$(contacts)" NUMBER="$( printf %b "Close Menu\n$CONTACTS" | grep . | @@ -73,39 +80,39 @@ dialmenu() { echo "$NUMBER" | grep "Close Menu" && kill 0 NUMBER="$(echo "$NUMBER" | awk -F' ' '{print $NF}' | tr -d -)" - echo "$NUMBER" | grep -qE '^[+0-9]+$' || err "$NUMBER is not a number" + echo "$NUMBER" | grep -qE '^[+0-9]+$' || fatalerr "$NUMBER is not a number" echo "Attempting to dial: $NUMBER" >&2 - VID="$( + CALLID="$( mmcli -m "$(modem_n)" --voice-create-call "number=$NUMBER" | grep -Eo "Call/[0-9]+" | grep -oE "[0-9]+" )" - echo "Starting call with VID: $VID" >&2 - startcall "$VID" >&2 - echo "$VID" + echo "Starting call with CALLID: $CALLID" >&2 + startcall "$CALLID" >&2 + echo "$CALLID" } startcall() { - VID="$1" - #modem_cmd_errcheck --voice-status -o $VID - modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --start - log_event "call_start" "$VID" + CALLID="$1" + #modem_cmd_errcheck --voice-status -o $CALLID + modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --start + log_event "call_start" "$CALLID" } acceptcall() { - VID="$1" - echo "Attempting to pickup VID $VID" - #mmcli --voice-status -o $VID - modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --accept - log_event "call_pickup" "$VID" + CALLID="$1" + echo "Attempting to pickup CALLID $CALLID" + #mmcli --voice-status -o $CALLID + modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --accept + log_event "call_pickup" "$CALLID" } hangup() { - VID="$1" - modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --hangup - log_event "call_hangup" "$VID" - err "Call hungup ok" + CALLID="$1" + modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --hangup + log_event "call_hangup" "$CALLID" + fatalerr "Call hungup ok" exit 1 } @@ -117,12 +124,11 @@ togglewindowify() { fi } -incallmenu() { +incallsetup() { DMENUIDX=0 - VID="$1" - NUMBER="$(vid_to_number "$VID")" + CALLID="$1" + NUMBER="$(vid_to_number "$CALLID")" WINDOWIFIED=0 - # E.g. There's some bug with the modem that' requires us to toggle the # DAI a few times before starting the call for it to kick in FLAGS=" " @@ -131,43 +137,51 @@ incallmenu() { toggleflagset "-2" toggleflagset "-2" toggleflagset "-2" +} +incallmonitor() { + CALLID="$1" while true; do - CHOICES=" - Volume ↑ ^ sxmo_vol.sh up - Volume ↓ ^ sxmo_vol.sh down - Mic $(echo -- "$FLAGS" | grep -q -- -m && echo ✓) ^ toggleflagset -m - Linemic $(echo -- "$FLAGS" | grep -q -- -l && echo ✓) ^ toggleflagset -l - Echomic $(echo -- "$FLAGS" | grep -q -- -z && echo ✓) ^ toggleflagset -z - Earpiece $(echo -- "$FLAGS" | grep -q -- -e && echo ✓) ^ toggleflagset -e - Linejack $(echo -- "$FLAGS" | grep -q -- -h && echo ✓) ^ toggleflagset -h - Speakerphone $(echo -- "$FLAGS" | grep -q -- -s && echo ✓) ^ toggleflagset -s - DTMF Tones ^ dtmfmenu $VID - Hangup ^ hangup $VID - $([ "$WINDOWIFIED" = 0 ] && echo Windowify || echo Unwindowify) ^ togglewindowify - " - - PICKED="" - PICKED=$( - echo "$CHOICES" | - xargs -0 echo | - cut -d'^' -f1 | - sed '/^[[:space:]]*$/d' | - awk '{$1=$1};1' | - dmenu -idx $DMENUIDX -l 14 $([ "$WINDOWIFIED" = 0 ] && echo "-c" || echo "-wm") -fn "Terminus-30" -p "$NUMBER" - ) - - # E.g. in modem watcher script we just kill dmenu if the other side hangsup - echo "$PICKED" | grep -Ev "." && err "$NUMBER hung up the call" - - CMD=$(echo "$CHOICES" | grep "$PICKED" | cut -d '^' -f2) - DMENUIDX=$(echo "$(echo "$CHOICES" | grep -n "$PICKED" | cut -d ':' -f1)" - 1 | bc) - eval "$CMD" + if mmcli -m "$(modem_n)" -K -o "$CALLID" | grep -E "^call.properties.state.+terminated"; then + fatalerr "$NUMBER hung up the call" + fi + echo "Call still in progress" + sleep 3 done } +incallmenuloop() { + CHOICES=" + Volume ↑ ^ sxmo_vol.sh up + Volume ↓ ^ sxmo_vol.sh down + Mic $(echo -- "$FLAGS" | grep -q -- -m && echo ✓) ^ toggleflagset -m + Linemic $(echo -- "$FLAGS" | grep -q -- -l && echo ✓) ^ toggleflagset -l + Echomic $(echo -- "$FLAGS" | grep -q -- -z && echo ✓) ^ toggleflagset -z + Earpiece $(echo -- "$FLAGS" | grep -q -- -e && echo ✓) ^ toggleflagset -e + Linejack $(echo -- "$FLAGS" | grep -q -- -h && echo ✓) ^ toggleflagset -h + Speakerphone $(echo -- "$FLAGS" | grep -q -- -s && echo ✓) ^ toggleflagset -s + DTMF Tones ^ dtmfmenu $CALLID + Hangup ^ hangup $CALLID + $([ "$WINDOWIFIED" = 0 ] && echo Windowify || echo Unwindowify) ^ togglewindowify + " + echo "$CHOICES" | + xargs -0 echo | + cut -d'^' -f1 | + sed '/^[[:space:]]*$/d' | + awk '{$1=$1};1' | + dmenu -idx $DMENUIDX -l 14 "$([ "$WINDOWIFIED" = 0 ] && echo "-c" || echo "-wm")" -fn "Terminus-30" -p "$NUMBER" | + ( + PICKED="$(cat)"; + echo "$PICKED" | grep -Ev "." && fatalerr "$NUMBER hung up the call" + CMD=$(echo "$CHOICES" | grep "$PICKED" | cut -d '^' -f2) + DMENUIDX=$(echo "$(echo "$CHOICES" | grep -n "$PICKED" | cut -d ':' -f1)" - 1 | bc) + eval "$CMD" + incallmenuloop + ) & wait # E.g. bg & wait to allow for SIGINT propogation +} + dtmfmenu() { - VID="$1" + CALLID="$1" DTMFINDEX=0 NUMS="0123456789*#ABCD" @@ -178,19 +192,24 @@ dtmfmenu() { )" echo "$PICKED" | grep "Return to Call Menu" && return DTMFINDEX=$(echo "$NUMS" | grep -bo "$PICKED" | cut -d: -f1 | xargs -IN echo 2+N | bc) - modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --send-dtmf="$PICKED" + modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --send-dtmf="$PICKED" done } + dial() { - VID="$(dialmenu)" - incallmenu "$VID" + CALLID="$(dialmenu)" + incallsetup "$CALLID" + incallmonitor "$CALLID" & + incallmenuloop "$CALLID" } pickup() { acceptcall "$1" - incallmenu "$1" + incallsetup "$1" + incallmonitor "$1" & + incallmenuloop "$1" } -modem_n || err "Couldn't determine modem number - is modem online?" +modem_n || fatalerr "Couldn't determine modem number - is modem online?" "$1" "$2" \ No newline at end of file diff --git a/scripts/modem/sxmo_modemmonitor.sh b/scripts/modem/sxmo_modemmonitor.sh index b3d7a66..d8201f8 100755 --- a/scripts/modem/sxmo_modemmonitor.sh +++ b/scripts/modem/sxmo_modemmonitor.sh @@ -1,130 +1,111 @@ #!/usr/bin/env sh TIMEOUT=3 LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem -ACTIVECALL="NONE" -trap "kill 0" INT +trap "gracefulexit" INT TERM err() { - printf %b "$1" | dmenu -fn Terminus-20 -c -l 10 + notify-send "$1" + gracefulexit +} + +gracefulexit() { + echo "gracefully exiting $0!" + sxmo_setpineled green 0 kill -9 0 } modem_n() { - MODEMS="$(mmcli -L)" - echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' > /dev/null || err "Couldn't find modem - is your modem enabled?\nDisabling modem monitor" - echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2 + MODEMS="$(mmcli -L)" + echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' > /dev/null || err "Couldn't find modem - is your modem enabled?\nDisabling modem monitor" + echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2 } -newcall() { - VID="$1" - sxmo_vibratepine 2000 & - sxmo_setpineled green 1 +checkforincomingcalls() { + VOICECALLID="$( + mmcli -m "$(modem_n)" --voice-list-calls -a | + grep -Eo '[0-9]+ incoming \(ringing-in\)' | + grep -Eo '[0-9]+' + )" + + if echo "$VOICECALLID" | grep -v .; then + rm -f /tmp/sxmo_incomingcall + return + fi + + sxmo_vibratepine 2000 & sxmo_setpineled green 1 - # Delete all terminated calls - for i in $(mmcli -m "$(modem_n)" --voice-list-calls | grep terminated | grep -oE "Call\/[0-9]+" | cut -d'/' -f2); do - mmcli -m "$(modem_n)" --voice-delete-call "$i" + # Delete all previous calls which have been terminated calls + for CALLID in $( + mmcli -m "$(modem_n)" --voice-list-calls | + grep terminated | + grep -oE "Call\/[0-9]+" | + cut -d'/' -f2 + ); do + mmcli -m "$(modem_n)" --voice-delete-call "$CALLID" done + # Determine the incoming phone number echo "Incoming Call:" INCOMINGNUMBER=$( - mmcli -m "$(modem_n)" --voice-list-calls -o "$VID" -K | + mmcli -m "$(modem_n)" --voice-list-calls -o "$VOICECALLID" -K | grep call.properties.number | cut -d ':' -f 2 | sed 's/^[+]//' | sed 's/^1//' ) + # Log to /tmp/incomingcall to allow pickup and log into modemlog TIME="$(date --iso-8601=seconds)" mkdir -p "$LOGDIR" printf %b "$TIME\tcall_ring\t$INCOMINGNUMBER\n" >> "$LOGDIR/modemlog.tsv" - echo "$VID:$INCOMINGNUMBER" > /tmp/sxmo_incomingcall - echo "Number: $INCOMINGNUMBER (VID: $VID)" - + echo "$VOICECALLID:$INCOMINGNUMBER" > /tmp/sxmo_incomingcall + echo "Number: $INCOMINGNUMBER (VOICECALLID: $VOICECALLID)" } -newtexts() { - sxmo_setpineled green 1 - - echo "New Texts:" - for i in $(printf %b "$1") ; do - DAT="$(mmcli -m "$(modem_n)" -s "$i" -K)" - - TEXT="$(echo "$DAT" | grep sms.content.text | sed -E 's/^sms\.content\.text\s+:\s+//')" +checkfornewtexts() { + TEXTIDS="$( + mmcli -m "$(modem_n)" --messaging-list-sms | + grep -Eo '/SMS/[0-9]+ \(received\)' | + grep -Eo '[0-9]+' + )" + echo "$TEXTIDS" | grep -v . && return + + # Loop each textid received and read out the data into appropriate logfile + { + sxmo_setpineled green 1 + sxmo_vibratepine 200; + sleep 0.1; + sxmo_vibratepine 200; + sleep 0.1; + sxmo_vibratepine 200; + } & + + for TEXTID in $(printf %b "$TEXTIDS") ; do + TEXTDATA="$(mmcli -m "$(modem_n)" -s "$TEXTID" -K)" + TEXT="$(echo "$TEXTDATA" | grep sms.content.text | sed -E 's/^sms\.content\.text\s+:\s+//')" NUM="$( - echo "$DAT" | + echo "$TEXTDATA" | grep sms.content.number | sed -E 's/^sms\.content\.number\s+:\s+[+]?//' | sed 's/^[+]//' | sed 's/^1//' )" - TIME="$(echo "$DAT" | grep sms.properties.timestamp | sed -E 's/^sms\.properties\.timestamp\s+:\s+//')" - TEXTSIZE="${#TEXT}" + TIME="$(echo "$TEXTDATA" | grep sms.properties.timestamp | sed -E 's/^sms\.properties\.timestamp\s+:\s+//')" mkdir -p "$LOGDIR/$NUM" printf %b "Received from $NUM at $TIME:\n$TEXT\n\n" >> "$LOGDIR/$NUM/sms.txt" - printf %b "$TIME\trecv_txt\t$NUM\t$TEXTSIZE chars\n" >> "$LOGDIR/modemlog.tsv" - mmcli -m "$(modem_n)" --messaging-delete-sms="$i" - - sxmo_vibratepine 300 && sleep 0.1 - sxmo_vibratepine 300 && sleep 0.1 - sxmo_vibratepine 300 + printf %b "$TIME\trecv_txt\t$NUM\t${#TEXT} chars\n" >> "$LOGDIR/modemlog.tsv" + mmcli -m "$(modem_n)" --messaging-delete-sms="$TEXTID" done } -killinprogresscall() { - echo "Kill the in progress call" - pkill -9 dmenu -} - -inprogresscallchecker() { - # E.g. register current call in progress as ACTIVECALL - CURRENTCALLS="$(mmcli -m "$(modem_n)" --voice-list-calls)" - - # E.g. if we've previously registered an ACTIVECALL, check if it - # was terminated by the otherside, if so kill the incall script - # and notify user - if echo "$ACTIVECALL" | grep -E '[0-9]+'; then - echo "$CURRENTCALLS" | grep -E "Call/${ACTIVECALL}.+terminated" && - killinprogresscall - fi - - # Register the active call so we can check in future loops if - # other side hung up - ACTIVECALL="$( - echo "$CURRENTCALLS" | - grep -oE "[0-9]+ (incoming|outgoing).+active" | - cut -d' ' -f1 - )" - - echo "Set new Activecall:<$ACTIVECALL>" +mainloop() { + while true; do + sxmo_setpineled green 0 + checkforincomingcalls + checkfornewtexts + sleep $TIMEOUT & wait + done } -while true -do - sxmo_setpineled green 0 - VOICECALLID="$( - mmcli -m "$(modem_n)" --voice-list-calls -a | - grep -Eo '[0-9]+ incoming \(ringing-in\)' | - grep -Eo '[0-9]+' - )" - - TEXTIDS="$( - mmcli -m "$(modem_n)" --messaging-list-sms | - grep -Eo '/SMS/[0-9]+ \(received\)' | - grep -Eo '[0-9]+' - )" - - echo "Check status, VIDS: $VOICECALLID, TIDS: $TEXTIDS" - - inprogresscallchecker - - if echo "$VOICECALLID" | grep .; then - newcall "$VOICECALLID" - else - rm -f /tmp/sxmo_incomingcall - fi - - echo "$TEXTIDS" | grep . && newtexts "$TEXTIDS" - - sleep $TIMEOUT -done +mainloop diff --git a/scripts/modem/sxmo_modemmonitortoggle.sh b/scripts/modem/sxmo_modemmonitortoggle.sh index e374081..6f3c9c0 100755 --- a/scripts/modem/sxmo_modemmonitortoggle.sh +++ b/scripts/modem/sxmo_modemmonitortoggle.sh @@ -1,6 +1,6 @@ #!/usr/bin/env sh if pgrep -f sxmo_modemmonitor.sh; then - pkill -9 -f sxmo_modemmonitor.sh + pgrep -f sxmo_modemmonitor.sh | grep -Ev "^${$}$" | xargs -IP kill -TERM P else sxmo_modemmonitor.sh & fi