-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm-lds-view.sh
185 lines (157 loc) · 3.99 KB
/
tm-lds-view.sh
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
182
183
184
185
#!/bin/sh
##
## tm-lds-view
## 2019-04-07, Initial version
##
## Display TimeMachines hardware "Locator Data Service" response.
## Use with TimeMachines https://timemachinescorp.com/ Hardware:
## o TM1000A firmware v2.6 or later
## o TM2000A firmware v0.3.3 or later
##
## Copyright (C) 2019 Lonnie Abelbeck
##
## This is free software, licensed under the GNU General Public License
## version 3 as published by the Free Software Foundation; you can
## redistribute it and/or modify it under the terms of the GNU
## General Public License; and comes with ABSOLUTELY NO WARRANTY.
##
## The LDS response packet:
## Bytes | Description
## ----------+-----------------------------------------------
## 0 | ID: TM1000A = 0x04, TM2000A = 0x05
## 1-4 | IPv4 address
## 5-10 | MAC address
## 11-12 | Firmware version Major:Minor
## 13 | Lock status 0=No Lock, 1=2D Lock, or 2=3D Lock
## 14-17 | NTP Sync count, 32 bits, MSB to LSB
## 18-20 | Current Time, H:M:S, UTC
## 21-45 | Location of unit 25 bytes, Latitude, Longitude, null terminated
## 46-80 | Name of Time Server, null terminated
## ----------+-----------------------------------------------
host="$1"
check_commands()
{
local cmd error IFS
error=0
IFS=' '
for cmd in $*; do
if ! command -v $cmd >/dev/null 2>&1; then
echo "tm-lds-view: Missing required command: \"$cmd\"" >&2
error=1
fi
done
return $error
}
print_header()
{
local title="$1" space center_col n i IFS
# Adjust center_col to allow the output to line up as desired
center_col=13
n=$((center_col - ${#title}))
space=""
unset IFS
for i in $(seq 1 $n); do
space="$space "
done
printf "%s%s: " "$space" "$title"
}
pop_byte()
{
byte="${data%% *}"
data="${data#* }"
}
## main
if [ -z "$host" ]; then
echo "Usage: tm-lds-view host-IP|host-name" >&2
exit 1
fi
if ! check_commands nc od tr seq; then
exit 1
fi
data="$(printf '\241\004\262' | nc -u -w1 $host 7372 | od -An -tx1 | tr -d '\n' | tr -s ' ')"
# Check if we received enough data...
# 80 bytes output as 2-hex plus 1-space for each byte, 80*(2+1)=240
if [ -z "$data" ]; then
echo "tm-lds-view: No response from host: $host" >&2
exit 1
fi
len=${#data}
if [ $len -lt 240 ]; then
echo "tm-lds-view: Invalid $((len/3)) byte response from host: $host" >&2
exit 1
fi
# remove leading space
data="${data# }"
printf "\n"
print_header "Hardware"
pop_byte
case $byte in
04) printf "TM1000A\n" ;;
05) printf "TM2000A\n" ;;
*) printf "Unknown\n" ;;
esac
print_header "IP Address"
pop_byte ; x1="$byte"
pop_byte ; x2="$byte"
pop_byte ; x3="$byte"
pop_byte ; x4="$byte"
printf "%d.%d.%d.%d\n" $((0x$x1)) $((0x$x2)) $((0x$x3)) $((0x$x4))
print_header "MAC Address"
pop_byte ; x1="$byte"
pop_byte ; x2="$byte"
pop_byte ; x3="$byte"
pop_byte ; x4="$byte"
pop_byte ; x5="$byte"
pop_byte ; x6="$byte"
printf "%02x:%02x:%02x:%02x:%02x:%02x\n" $((0x$x1)) $((0x$x2)) $((0x$x3)) $((0x$x4)) $((0x$x5)) $((0x$x6))
print_header "Firmware"
pop_byte ; x1="$byte"
pop_byte ; x2="$byte"
printf "v%d.%d\n" $((0x$x1)) $((0x$x2))
print_header "GPS Fix"
pop_byte
case $byte in
00) printf "No Lock\n" ;;
01) printf "2D Lock\n" ;;
02) printf "3D Lock\n" ;;
*) printf "Unknown\n" ;;
esac
print_header "NTP Lookups"
pop_byte ; x1="$byte"
pop_byte ; x2="$byte"
pop_byte ; x3="$byte"
pop_byte ; x4="$byte"
printf "%d\n" $((0x$x1$x2$x3$x4))
print_header "GPS Time"
pop_byte ; x1="$byte"
pop_byte ; x2="$byte"
pop_byte ; x3="$byte"
printf "%02d:%02d:%02d UTC\n" $((0x$x1)) $((0x$x2)) $((0x$x3))
print_header "Location"
str=""
end=0
unset IFS
for i in $(seq 1 25); do
pop_byte
if [ "$byte" = "00" ]; then
end=1
elif [ $end -eq 0 ]; then
str="$str$(printf "\\$(printf '%03o' $((0x$byte)))")"
fi
done
printf "%s\n" "$str"
print_header "Unit Name"
str=""
end=0
unset IFS
for i in $(seq 1 35); do
pop_byte
if [ "$byte" = "00" ]; then
end=1
elif [ $end -eq 0 ]; then
str="$str$(printf "\\$(printf '%03o' $((0x$byte)))")"
fi
done
printf "%s\n" "$str"
printf "\n"
exit 0