File: sxmo_networkmonitor.sh

package info (click to toggle)
sxmo-utils 1.14.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,016 kB
  • sloc: sh: 9,166; ansic: 117; makefile: 68
file content (65 lines) | stat: -rwxr-xr-x 1,862 bytes parent folder | download
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
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

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

gracefulexit() {
	sxmo_log "gracefully exiting (on signal or after error)"
	sxmo_daemons.sh stop network_monitor_device
	trap - INT TERM EXIT
}

trap "gracefulexit" INT TERM EXIT

getdevtype() {
	nmcli -g GENERAL.TYPE device show "$1" 2>/dev/null
}

# Send the initial states to the statusbar
nmcli -g GENERAL.TYPE,GENERAL.DEVICE d show | grep . | while read -r type; do
	read -r name || break
	sxmo_log "$name initializing network tracking"
	sxmo_hook_statusbar.sh network "$type" "$name"
done

# shellcheck disable=2016
sxmo_daemons.sh start network_monitor_device \
	nmcli device monitor | stdbuf -o0 awk '
	{ newstate=$2 }
	/device removed$/ {newstate="disconnected"}
	newstate == "unavailable" {newstate="disconnected"}

	{
		sub(":$", "", $1) # remove trailing colon from device name
		printf "%s\n%s\n", $1, newstate
	}' | while read -r devicename; do
		read -r newstate || break

		devicetype="$(getdevtype "$devicename")"
		case "$newstate" in
			"connected")
				sxmo_log "$devicename up"
				sxmo_hook_network_up.sh "$devicename" "$devicetype"
				sxmo_hook_statusbar.sh network "$devicetype" "$devicename"
				;;
			"disconnected")
				sxmo_log "$devicename down"
				sxmo_hook_network_down.sh "$devicename" "$devicetype"
				sxmo_hook_statusbar.sh network "$devicetype" "$devicename"
				;;
			"deactivating")
				sxmo_hook_network_pre_down.sh "$devicename" "$devicetype"
				sxmo_hook_statusbar.sh network "$devicetype" "$devicename"
				;;
			"connecting")
				sxmo_hook_network_pre_up.sh "$devicename" "$devicetype"
				sxmo_hook_statusbar.sh network "$devicetype" "$devicename"
				;;
			*)
				sxmo_log "$devicename unknown state: $newstate"
				;;
		esac
	done