~mil/sxmo-utils

ref: 103a6612f3987807b1abb99621d79fb6d59ff5cb sxmo-utils/scripts/modem/sxmo_modemmonitortoggle.sh -rwxr-xr-x 2.5 KiB
103a6612Stacy Harper Fix autorotation script 11 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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"

if [ -f /etc/os-release ]; then
	# freedesktop.org and systemd
	. /etc/os-release
	OS=$NAME
else
	OS="Unknown"
fi

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

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