~mil/sxmo-utils

ref: f7698e4a25f11c5d673c9c41d46d11c131ae866e sxmo-utils/scripts/modem/sxmo_modemmonitortoggle.sh -rwxr-xr-x 2.6 KiB
f7698e4aMaxim Karasev Move operating system detection to sxmo_common a month 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
#!/usr/bin/env sh

# This script toggles the modem monitor
# It optionally takes a parameter "on" or "off"
# forcing it to toggle only to that desired state if applicable.
# It may also take a "reset" parameter that forces the
# entire modem subsystem to reload

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

daemon_start() {
	case "$OS" in
		"Alpine Linux"|postmarketOS)
			sudo rc-service "$1" start
			;;
		"Arch Linux ARM"|alarm)
			[ "$1" = "modemmanager" ] && set -- ModemManager
			sudo systemctl start "$1"
			;;
	esac
}

daemon_stop() {
	case "$OS" in
		"Alpine Linux"|postmarketOS)
			sudo rc-service "$1" stop
			;;
		"Arch Linux ARM"|alarm)
			[ "$1" = "modemmanager" ] && set -- ModemManager
			sudo systemctl stop "$1"
			;;
	esac
}

daemon_isrunning() {
	daemon_exists "$1" || return 0
	case "$OS" in
		"Alpine Linux"|postmarketOS)
			rc-service "$1" status | grep -q started
			;;
		"Arch Linux ARM"|alarm)
			[ "$1" = "modemmanager" ] && set -- ModemManager
			systemctl status "$1" | grep -q running
			;;
	esac
}

daemon_exists() {
	case "$OS" in
		"Alpine Linux"|postmarketOS)
			[ -f /etc/init.d/"$1" ]
			;;
		"Arch Linux ARM"|alarm)
			systemctl status "$1" >/dev/null
			;;
	esac
}

ensure_daemon() {
	TRIES=0
	while ! daemon_isrunning "$1"; do
		if [ "$TRIES" -eq 10 ]; then
			return 1
		fi
		TRIES=$((TRIES+1))
		daemon_start "$1"
		sleep 5
	done

	return 0
}

ensure_daemons() {
	if (daemon_isrunning eg25-manager) && \
			(daemon_isrunning modemmanager); then
		return
	fi

	echo "sxmo_modemmonitortoggle: forcing modem restart" >&2
	notify-send "Resetting modem daemons, this may take a minute..."

	daemon_stop modemmanager
	daemon_stop eg25-manager
	sleep 2

	if ! (ensure_daemon eg25-manager && ensure_daemon modemmanager); then
		printf "failed\n" > "$MODEMSTATEFILE"
		notify-send --urgency=critical "We failed to start the modem daemons. We may need hard reboot."
		return 1
	fi
}

on() {
	rm "$NOTIFDIR"/incomingcall*

	TRIES=0
	while ! printf %s "$(mmcli -L)" 2> /dev/null | grep -qoE 'Modem\/([0-9]+)'; do
		TRIES=$((TRIES+1))
		if [ "$TRIES" -eq 10 ]; then
			printf "failed\n" > "$MODEMSTATEFILE"
			notify-send --urgency=critical "We failed to start the modem monitor. We may need hard reboot."
		fi
		sleep 5
	done

	setsid -f sxmo_modemmonitor.sh &

	sleep 1
}

off() {
	pkill -TERM -f sxmo_modemmonitor.sh
}

if [ -z "$1" ]; then
	if pgrep -f sxmo_modemmonitor.sh; then
		set -- off
	else
		set -- on
	fi
fi

case "$1" in
	restart) off; ensure_daemons && on;;
	on) ensure_daemons && on;;
	off) off;;
esac

sxmo_statusbarupdate.sh