Clean up modem functionality & don't ever kill dmenu; move call monitoring to call script

master
Miles Alan 5 years ago
parent 3c5a85a9a1
commit a4b2724c4a
  1. 44
      scripts/core/sxmo_appmenu.sh
  2. 147
      scripts/modem/sxmo_modemcall.sh
  3. 159
      scripts/modem/sxmo_modemmonitor.sh
  4. 2
      scripts/modem/sxmo_modemmonitortoggle.sh

@ -1,6 +1,12 @@
#!/usr/bin/env sh #!/usr/bin/env sh
signal gracefulexit INT TERM
WIN=$(xdotool getwindowfocus) WIN=$(xdotool getwindowfocus)
gracefulexit() {
echo "Gracefully exiting $0"
kill -9 0
}
programchoicesinit() { programchoicesinit() {
XPROPOUT="$(xprop -id "$(xdotool getactivewindow)")" XPROPOUT="$(xprop -id "$(xdotool getactivewindow)")"
WMCLASS="${1:-$(echo "$XPROPOUT" | grep WM_CLASS | cut -d ' ' -f3-)}" WMCLASS="${1:-$(echo "$XPROPOUT" | grep WM_CLASS | cut -d ' ' -f3-)}"
@ -221,29 +227,27 @@ quit() {
} }
mainloop() { mainloop() {
DMENUIDX=0 getprogchoices "$ARGS"
PICKED="" echo "$PROGCHOICES" |
ARGS="$*" cut -d'^' -f1 |
dmenu -idx "$DMENUIDX" -l 14 -c -fn "Terminus-30" -p "$WINNAME" | (
while : PICKED="$(cat)"
do
# E.g. sets PROGCHOICES
getprogchoices "$ARGS"
PICKED="$(
echo "$PROGCHOICES" |
cut -d'^' -f1 |
dmenu -idx $DMENUIDX -l 14 -c -fn "Terminus-30" -p "$WINNAME"
)"
LOOP="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f2)" LOOP="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f2)"
CMD="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f3)" CMD="$(echo "$PROGCHOICES" | grep -F "$PICKED" | cut -d '^' -f3)"
DMENUIDX="$(echo "$PROGCHOICES" | grep -F -n "$PICKED" | cut -d ':' -f1)" DMENUIDX="$(echo "$PROGCHOICES" | grep -F -n "$PICKED" | cut -d ':' -f1)"
echo "Eval: <$CMD> from picked <$PICKED> with loop <$LOOP>" echo "Eval: <$CMD> from picked <$PICKED> with loop <$LOOP>"
eval "$CMD" if echo "$LOOP" | grep 1; then
echo "$LOOP" | grep 1 || quit eval "$CMD"
done mainloop
else
eval "$CMD" &
quit
fi
) & wait
} }
pgrep -f sxmo_appmenu.sh | grep -Ev "^${$}$" | xargs kill -9 pgrep -f sxmo_appmenu.sh | grep -Ev "^${$}$" | xargs kill -TERM
pkill -9 dmenu DMENUIDX=0
mainloop "$@" PICKED=""
ARGS="$*"
mainloop

@ -1,16 +1,23 @@
#!/usr/bin/env sh #!/usr/bin/env sh
LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem
trap "kill 0" INT trap "gracefulexit" INT TERM
err() { fatalerr() {
printf %b "$1" | dmenu -fn Terminus-20 -c -l 10 # 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 alsactl --file /usr/share/sxmo/default_alsa_sound.conf restore
notify-send "$1"
kill -9 0 kill -9 0
} }
gracefulexit() {
fatalerr "Terminated via SIGTERM/SIGINT"
}
modem_n() { modem_n() {
MODEMS="$(mmcli -L)" 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 echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2
} }
@ -25,7 +32,7 @@ modem_cmd_errcheck() {
RES="$(mmcli $@ 2>&1)" RES="$(mmcli $@ 2>&1)"
OK="$?" OK="$?"
echo "Command: mmcli $*" 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" echo "$RES"
} }
@ -64,7 +71,7 @@ toggleflagset() {
dialmenu() { dialmenu() {
CONTACTS="$(contacts)" CONTACTS="$(contacts)"
NUMBER="$( NUMBER="$(
printf %b "Close Menu\n$CONTACTS" | printf %b "Close Menu\n$CONTACTS" |
grep . | grep . |
@ -73,39 +80,39 @@ dialmenu() {
echo "$NUMBER" | grep "Close Menu" && kill 0 echo "$NUMBER" | grep "Close Menu" && kill 0
NUMBER="$(echo "$NUMBER" | awk -F' ' '{print $NF}' | tr -d -)" 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 echo "Attempting to dial: $NUMBER" >&2
VID="$( CALLID="$(
mmcli -m "$(modem_n)" --voice-create-call "number=$NUMBER" | mmcli -m "$(modem_n)" --voice-create-call "number=$NUMBER" |
grep -Eo "Call/[0-9]+" | grep -Eo "Call/[0-9]+" |
grep -oE "[0-9]+" grep -oE "[0-9]+"
)" )"
echo "Starting call with VID: $VID" >&2 echo "Starting call with CALLID: $CALLID" >&2
startcall "$VID" >&2 startcall "$CALLID" >&2
echo "$VID" echo "$CALLID"
} }
startcall() { startcall() {
VID="$1" CALLID="$1"
#modem_cmd_errcheck --voice-status -o $VID #modem_cmd_errcheck --voice-status -o $CALLID
modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --start modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --start
log_event "call_start" "$VID" log_event "call_start" "$CALLID"
} }
acceptcall() { acceptcall() {
VID="$1" CALLID="$1"
echo "Attempting to pickup VID $VID" echo "Attempting to pickup CALLID $CALLID"
#mmcli --voice-status -o $VID #mmcli --voice-status -o $CALLID
modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --accept modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --accept
log_event "call_pickup" "$VID" log_event "call_pickup" "$CALLID"
} }
hangup() { hangup() {
VID="$1" CALLID="$1"
modem_cmd_errcheck -m "$(modem_n)" -o "$VID" --hangup modem_cmd_errcheck -m "$(modem_n)" -o "$CALLID" --hangup
log_event "call_hangup" "$VID" log_event "call_hangup" "$CALLID"
err "Call hungup ok" fatalerr "Call hungup ok"
exit 1 exit 1
} }
@ -117,12 +124,11 @@ togglewindowify() {
fi fi
} }
incallmenu() { incallsetup() {
DMENUIDX=0 DMENUIDX=0
VID="$1" CALLID="$1"
NUMBER="$(vid_to_number "$VID")" NUMBER="$(vid_to_number "$CALLID")"
WINDOWIFIED=0 WINDOWIFIED=0
# E.g. There's some bug with the modem that' requires us to toggle the # 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 # DAI a few times before starting the call for it to kick in
FLAGS=" " FLAGS=" "
@ -131,43 +137,51 @@ incallmenu() {
toggleflagset "-2" toggleflagset "-2"
toggleflagset "-2" toggleflagset "-2"
toggleflagset "-2" toggleflagset "-2"
}
incallmonitor() {
CALLID="$1"
while true; do while true; do
CHOICES=" if mmcli -m "$(modem_n)" -K -o "$CALLID" | grep -E "^call.properties.state.+terminated"; then
Volume ↑ ^ sxmo_vol.sh up fatalerr "$NUMBER hung up the call"
Volume ↓ ^ sxmo_vol.sh down fi
Mic $(echo -- "$FLAGS" | grep -q -- -m && echo) ^ toggleflagset -m echo "Call still in progress"
Linemic $(echo -- "$FLAGS" | grep -q -- -l && echo) ^ toggleflagset -l sleep 3
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"
done 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() { dtmfmenu() {
VID="$1" CALLID="$1"
DTMFINDEX=0 DTMFINDEX=0
NUMS="0123456789*#ABCD" NUMS="0123456789*#ABCD"
@ -178,19 +192,24 @@ dtmfmenu() {
)" )"
echo "$PICKED" | grep "Return to Call Menu" && return echo "$PICKED" | grep "Return to Call Menu" && return
DTMFINDEX=$(echo "$NUMS" | grep -bo "$PICKED" | cut -d: -f1 | xargs -IN echo 2+N | bc) 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 done
} }
dial() { dial() {
VID="$(dialmenu)" CALLID="$(dialmenu)"
incallmenu "$VID" incallsetup "$CALLID"
incallmonitor "$CALLID" &
incallmenuloop "$CALLID"
} }
pickup() { pickup() {
acceptcall "$1" 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" "$1" "$2"

@ -1,130 +1,111 @@
#!/usr/bin/env sh #!/usr/bin/env sh
TIMEOUT=3 TIMEOUT=3
LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem
ACTIVECALL="NONE" trap "gracefulexit" INT TERM
trap "kill 0" INT
err() { 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 kill -9 0
} }
modem_n() { modem_n() {
MODEMS="$(mmcli -L)" 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]+)' > /dev/null || err "Couldn't find modem - is your modem enabled?\nDisabling modem monitor"
echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2 echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2
} }
newcall() { checkforincomingcalls() {
VID="$1" VOICECALLID="$(
sxmo_vibratepine 2000 & mmcli -m "$(modem_n)" --voice-list-calls -a |
sxmo_setpineled green 1 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 # Delete all previous calls which have been terminated calls
for i in $(mmcli -m "$(modem_n)" --voice-list-calls | grep terminated | grep -oE "Call\/[0-9]+" | cut -d'/' -f2); do for CALLID in $(
mmcli -m "$(modem_n)" --voice-delete-call "$i" 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 done
# Determine the incoming phone number
echo "Incoming Call:" echo "Incoming Call:"
INCOMINGNUMBER=$( 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 | grep call.properties.number |
cut -d ':' -f 2 | cut -d ':' -f 2 |
sed 's/^[+]//' | sed 's/^[+]//' |
sed 's/^1//' sed 's/^1//'
) )
# Log to /tmp/incomingcall to allow pickup and log into modemlog
TIME="$(date --iso-8601=seconds)" TIME="$(date --iso-8601=seconds)"
mkdir -p "$LOGDIR" mkdir -p "$LOGDIR"
printf %b "$TIME\tcall_ring\t$INCOMINGNUMBER\n" >> "$LOGDIR/modemlog.tsv" printf %b "$TIME\tcall_ring\t$INCOMINGNUMBER\n" >> "$LOGDIR/modemlog.tsv"
echo "$VID:$INCOMINGNUMBER" > /tmp/sxmo_incomingcall echo "$VOICECALLID:$INCOMINGNUMBER" > /tmp/sxmo_incomingcall
echo "Number: $INCOMINGNUMBER (VID: $VID)" echo "Number: $INCOMINGNUMBER (VOICECALLID: $VOICECALLID)"
} }
newtexts() { checkfornewtexts() {
sxmo_setpineled green 1 TEXTIDS="$(
mmcli -m "$(modem_n)" --messaging-list-sms |
echo "New Texts:" grep -Eo '/SMS/[0-9]+ \(received\)' |
for i in $(printf %b "$1") ; do grep -Eo '[0-9]+'
DAT="$(mmcli -m "$(modem_n)" -s "$i" -K)" )"
echo "$TEXTIDS" | grep -v . && return
TEXT="$(echo "$DAT" | grep sms.content.text | sed -E 's/^sms\.content\.text\s+:\s+//')"
# 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="$( NUM="$(
echo "$DAT" | echo "$TEXTDATA" |
grep sms.content.number | grep sms.content.number |
sed -E 's/^sms\.content\.number\s+:\s+[+]?//' | sed -E 's/^sms\.content\.number\s+:\s+[+]?//' |
sed 's/^[+]//' | sed 's/^[+]//' |
sed 's/^1//' sed 's/^1//'
)" )"
TIME="$(echo "$DAT" | grep sms.properties.timestamp | sed -E 's/^sms\.properties\.timestamp\s+:\s+//')" TIME="$(echo "$TEXTDATA" | grep sms.properties.timestamp | sed -E 's/^sms\.properties\.timestamp\s+:\s+//')"
TEXTSIZE="${#TEXT}"
mkdir -p "$LOGDIR/$NUM" mkdir -p "$LOGDIR/$NUM"
printf %b "Received from $NUM at $TIME:\n$TEXT\n\n" >> "$LOGDIR/$NUM/sms.txt" 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" printf %b "$TIME\trecv_txt\t$NUM\t${#TEXT} chars\n" >> "$LOGDIR/modemlog.tsv"
mmcli -m "$(modem_n)" --messaging-delete-sms="$i" mmcli -m "$(modem_n)" --messaging-delete-sms="$TEXTID"
sxmo_vibratepine 300 && sleep 0.1
sxmo_vibratepine 300 && sleep 0.1
sxmo_vibratepine 300
done done
} }
killinprogresscall() { mainloop() {
echo "Kill the in progress call" while true; do
pkill -9 dmenu sxmo_setpineled green 0
} checkforincomingcalls
checkfornewtexts
inprogresscallchecker() { sleep $TIMEOUT & wait
# E.g. register current call in progress as ACTIVECALL done
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>"
} }
while true mainloop
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

@ -1,6 +1,6 @@
#!/usr/bin/env sh #!/usr/bin/env sh
if pgrep -f sxmo_modemmonitor.sh; then 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 else
sxmo_modemmonitor.sh & sxmo_modemmonitor.sh &
fi fi

Loading…
Cancel
Save