~mil/sxmo-utils

sxmo-utils/scripts/modem/sxmo_modemmonitor.sh -rwxr-xr-x 5.2 KiB
c0e64920Maarten van Gompel improve detection of active app_id and title for xwayland apps 5 days 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors
trap "gracefulexit" INT TERM

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. sxmo_common.sh

stderr() {
	sxmo_log "$*"
}

gracefulexit() {
	stderr "gracefully exiting (on signal or after error)"
	sxmo_daemons.sh stop modem_monitor_voice
	sxmo_daemons.sh stop modem_monitor_text
	sxmo_daemons.sh stop modem_monitor_finished_voice
	sxmo_daemons.sh stop modem_monitor_state_change
	sxmo_daemons.sh stop modem_monitor_mms
	sxmo_daemons.sh stop modem_monitor_vvm
	sxmo_daemons.sh stop modem_monitor_periodic_text
	sxmo_daemons.sh stop modem_monitor_periodic_calls
	exit
}

# see networkmananger 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() {
	# Display the icon
	sxmo_hook_statusbar.sh modem_monitor

	PIDS=""

	# get initial modem state
	(
		while ! newstate="$(mmcli -m any -K | grep "^modem.generic.state " | cut -d':' -f2 | sed 's/^ //')" || [ -z "$newstate" ]; do
			sleep 5
		done

		# fake oldstate (boot) and reason (0)
		sxmo_hook_modem.sh "boot" "$newstate" "0"
		sxmo_hook_statusbar.sh modem
	) &
	PIDS="$PIDS $!"

	# Monitor for incoming calls
	sxmo_daemons.sh start modem_monitor_voice \
		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 &
	PIDS="$PIDS $!"

	# Monitor for incoming texts
	sxmo_daemons.sh start modem_monitor_text \
		dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Messaging',type='signal',member='Added'" | \
		while read -r line; do
			echo "$line" | grep -qE "^signal" && sxmo_uniq_exec.sh sxmo_modem.sh checkfornewtexts
		done &
	PIDS="$PIDS $!"

	sxmo_daemons.sh start modem_monitor_periodic_text \
		sxmo_run_aligned.sh 300 sxmo_uniq_exec.sh sxmo_modem.sh checkfornewtexts

	# Monitor for finished calls
	sxmo_daemons.sh start modem_monitor_finished_voice \
		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_uniq_exec.sh sxmo_modem.sh checkforfinishedcalls
		done &
	PIDS="$PIDS $!"

	sxmo_daemons.sh start modem_monitor_periodic_calls \
		sxmo_run_aligned.sh 300 sxmo_uniq_exec.sh sxmo_modem.sh checkforfinishedcalls

	# Monitor for modem state change
	sxmo_daemons.sh start modem_monitor_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 &
	PIDS="$PIDS $!"

	if [ -f "${SXMO_MMS_BASE_DIR:-"$HOME"/.mms/modemmanager}/mms" ]; then
		# monitor for mms
		sxmo_daemons.sh start modem_monitor_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" "Received"
				fi
			done &
		PIDS="$PIDS $!"
	fi

	if [ -f "${SXMO_VVM_BASE_DIR:-"$HOME"/.vvm/modemmanager}/vvm" ]; then
		# monitor for vvm (Visual Voice Mail)
		VVM_START=0
		sxmo_daemons.sh start modem_monitor_vvm \
			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 &
		PIDS="$PIDS $!"
	fi

	for PID in $PIDS; do
		wait "$PID"
	done
}

# 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