~mil/sxmo-utils

ref: 9f930e742a8e5443d6ecce4252aaa78f668a50cb sxmo-utils/scripts/modem/sxmo_modemmonitor.sh -rwxr-xr-x 2.8 KiB
9f930e74Miles Alan Simplify sxmo_contacts.sh - use less tempfiles and join logic, output w/ colon 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env sh
TIMEOUT=3
LOGDIR="$XDG_CONFIG_HOME"/sxmo/modem
trap "gracefulexit" INT TERM

err() {
	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
}

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 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 "$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 "$VOICECALLID:$INCOMINGNUMBER" > /tmp/sxmo_incomingcall
	echo "Number: $INCOMINGNUMBER (VOICECALLID: $VOICECALLID)"
}

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 "$TEXTDATA" | 
			grep sms.content.number | 
			sed -E 's/^sms\.content\.number\s+:\s+[+]?//' |
			sed 's/^[+]//' |
			sed 's/^1//'
		)"
		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${#TEXT} chars\n" >> "$LOGDIR/modemlog.tsv"
		mmcli -m "$(modem_n)" --messaging-delete-sms="$TEXTID"
	done
}

mainloop() {
	while true; do
		sxmo_setpineled green 0
		checkforincomingcalls
		checkfornewtexts
		sleep $TIMEOUT & wait
	done
}

mainloop