summaryrefslogtreecommitdiff
path: root/files/common/etc/fsm/update/watch/default
blob: a6858afd23c19a39f8d6c065c5b041cd8742eea0 (plain)
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
#!/bin/sh -e
set -x

. ../common.sh

# announce a transition and terminate
trans () {
    echo $1
    exit
}

# is the firmware file available?
checkFw () {
    [ -n "$TargetFw" -a -f "$FwDst" ] || trans idle
}

checkFwHash () {
    [ "$(sha256sum <$FwDst 2>/dev/null | cut -f1 -d' ')" == "$TargetFw" ] || trans idle
}

# is target time passed but not set
checkTime () {
    [ $CurTime -lt "$TargetTime" -o "$AckTime" -eq "$TargetTime" ] 2>/dev/null \
	|| trans ready
}

# check all-or-nothing-invariant for whole update state table
checkPeerState() {
    p2ptbl show $Tbl \
	| (
	IFS=$'\t'
	while read OId OCurFw OTargetFw OTargetTime OAckTime; do
	    [ -z "$OTargetFw" -a -z "$OTargetTime" -a -z "$OAckTime" ] && continue
	    [ -n "$OTargetFw" -a "$OTargetTime" -eq "$OAckTime" ] && continue
	    exit 1
	done ) || trans ready
}


case $CurState in
    idle)
	checkFw
	checkFwHash
	# ^^ check hash of firmware file once if the hash is correct;
	# do not optimize for the rare case of a wrong hash or
	# continuously failing updates
	trans ready
	;;
    ready)
	checkFw
	checkTime
	[ "$TargetTime" -gt $CurTime ] 2>/dev/null || trans ready
	trans scheduled
	;;
    scheduled)
	checkFw
	checkTime
	[ $CurTime -gt "$TargetTime" ] || trans scheduled
	checkPeerState
	trans applying
	;;
    *)
	# anything else is illegal; especially the state "applying"
	# must not be reached: during transition toward it the
	# firmware is flashed and the router rebooted
	echo "illegal state $CurState" >&2
	exit 1
	;;
esac

# default and if any case falls through
trans idle
contact: Jan Huwald // Impressum