-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel_scan.sh
executable file
·343 lines (309 loc) · 8.42 KB
/
channel_scan.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/bin/bash
#set -x
######################################################################################
#
# channel_scan.sh
# v2020.07.04.r1
#
# This script will scan a HDHomeRun for ATSC channels and print a formatted list
#
# Use the -h option for additional runtime options
#
# A note about deciphering the results here:
# http://www.silicondust.com/forum2/viewtopic.php?t=4474
#
# Twitter:// @shmick
# BitCoin:// 1BFSzGSSU5LazfYv8rYYxzT3M3GRf9EPov
######################################################################################
#Specify the full path to your hdhomerun_config binary if it's not found
HDHRConfig=""
###############################################
# There are no options to set below this line #
###############################################
DetectOS() {
if [ -z "$HDHRConfig" ]; then
case "$OSTYPE" in
darwin*)
HDHRConfig="$(which hdhomerun_config)"
;;
linux*)
HDHRConfig="$(which hdhomerun_config)"
;;
esac
fi
if [ ! -x "$HDHRConfig" ]; then
echo -e "\nUnable to locate the hdhomerun_config utility, please edit the HDHRConfig variable in this script"
echo -e "\nIf it's not installed, download it from here: http://www.silicondust.com/support/hdhomerun/downloads\n"
exit
fi
}
DisplayHelp() {
echo -e "\nUsage: $(basename $0) [option]"
echo -e "\nOptions are:\n"
echo "-d <device> // Provide a device ID or IP instead of using auto discovery"
echo "-t <tuner> // Provide a tuner ID instead of using auto discovery"
echo "-c // Output scan results in CSV format"
echo "-l <filename> // Log the output to <filename>"
echo "-b // Brief mode. No dB info or secondary channels"
echo "-D // Debug mode. To extrapolate SS values > 100%"
echo "-A // Debug mode. To extrapolate SS values > 100% on all channels"
echo "-n // Do not scan, only report devices and tuners available"
echo -e "-h // This help info\n"
exit
}
ReportLockStatus() {
echo ""
for Unit in $Devices; do
for Tuner in 0 1; do
echo -n "Lock status for Device $Unit Tuner $Tuner: "
echo "$($HDHRConfig $Unit get /tuner$Tuner/lockkey)"
done
done
}
NoScan() {
DiscoverDevices
CheckTunerLockStatus
ReportLockStatus
echo -e "\nSelecting Device $ScanDev Tuner $ScanTuner\n"
exit
}
CheckOpts() {
local OPTIND
while getopts ":hnd:t:l:cbDA" OPTIONS; do
case "$OPTIONS" in
h)
DisplayHelp
;;
n)
NoScan
;;
d)
Devices=$OPTARG
if [ "$(echo ${#Devices})" != "8" ]; then
valid_ip ${Devices}
if [ "$?" != "0" ]; then
NoDevID
exit 1
fi
fi
;;
t)
ScanTuner=$OPTARG
if [ "$ScanTuner" -ge "2" ]; then
echo -e "\nTuner ID must be 0 or 1"
DisplayHelp
fi
;;
l)
DataLog=$OPTARG
;;
c)
CSV="y"
;;
b)
BRIEF="y"
;;
D)
DEBUG="y"
LOCKKEY="12121212"
OUTFILTER="seq 100"
;;
A)
DEBUG="y"
LOCKKEY="12121212"
OUTFILTER="."
;;
:)
echo -e "\nOption -$OPTARG requires an argument."
DisplayHelp
;;
esac
done
}
NoDevID() {
echo "You must specify a proper device ID"
}
# From https://www.linuxjournal.com/content/validating-ip-address-bash-script
valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && \
${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
DiscoverDevices() {
# Attempt to discover HDHR devices on the LAN
# Not used if you specify a device ID with the -d flag
if [ -z "$Devices" ]; then
Devices=$($HDHRConfig discover | sort -nr | awk '/^hdhomerun device/ {print $3}')
fi
# Exit if no device are found
if [ "$Devices" = "found" ]; then
echo "No HDHomeRun units detected, exiting"
exit
fi
}
CheckTunerLockStatus() {
for Unit in $Devices; do
if [ -n "$ScanTuner" ]; then # If a tuner was specified on the command line
# with the -t option, use that, otherwise try
# tuner 0 and 1
TryTuners=$ScanTuner
else
TryTuners="0 1"
fi
for Tuner in $TryTuners; do
if [ $($HDHRConfig $Unit get /tuner$Tuner/lockkey) = "none" ]; then
ScanDev=$Unit
ScanTuner=$Tuner
break
fi
done
if [ -n "$ScanDev" ]; then
break
fi
done
if [ -z "$ScanDev" ]; then
echo -e "\nSorry, all tuners are in use right now. Try again later.\n"
exit
fi
}
GetScanData() {
# GetScanData : Run a scan, parse the output and
# store the results in $ScanResults
echo -e "\nBeginning scan on $ScanDev, tuner $ScanTuner at $(date '+%D %T')\n"
ScanResults=$($HDHRConfig $ScanDev scan $ScanTuner |
tr -s "\n()=:" " " |
sed 's/SCANNING/\'$'\n/g' |
grep "seq 100")
NumChannels=$(wc -l <<<"$ScanResults")
echo "$NumChannels channels found"
}
GetDebugData() {
# GetDebugData : Tune each channel, and grab stats using debug mode so we can extrapolate strong signals above SS = 100%
# parse the output and plotting 'dbg = xxx' (X axis) vs SS, dBmV (Y axis) in excel within the usable 0-100 SS range
# yields a linear relationship
# My tuner (HDHR3-US) worked out to having a slope = 0.139874758, round to .14. and an intercept = 58.7553810994, round to 58.8
# HDHR4 looks to be: slope = .00991, intercept = 45.3
echo -e "\nBeginning scan on $ScanDev, tuner $ScanTuner at $(date '+%D %T')\n"
echo -e "Locking $ScanDev, tuner $ScanTuner with key $LOCKKEY\n"
$HDHRConfig $ScanDev set /tuner$ScanTuner/lockkey $LOCKKEY
ScanResults=$(for CHANNEL in {2..51}; do
$HDHRConfig $ScanDev key $LOCKKEY set /tuner$ScanTuner/channel auto:$CHANNEL
sleep 1
RESULTS=$($HDHRConfig $ScanDev get /tuner$ScanTuner/debug |
grep "tun" |
tr "/()=:" " " |
sed 's/none/none none/g' |
grep "$OUTFILTER")
echo $RESULTS
done)
echo -e "Unocking $ScanDev, tuner $ScanTuner with key $LOCKKEY\n"
$HDHRConfig $ScanDev key $LOCKKEY set /tuner$ScanTuner/lockkey none
if [ "$OUTFILTER" != "." ]; then
ScanResults=$(echo "$ScanResults" | grep ^tun)
fi
NumChannels=$(wc -l <<<"$ScanResults")
echo "$NumChannels channels found"
}
LogOutput() {
# LogOutput : Re-parse the $ScanResults data and
# append it to $DataLog
timestamp=$(date "+%Y-%m-%d %H:%M")
awk -v ts="$timestamp" '{OFS="," ; print ts,$3,$7 \
,($7 * 60 / 100 -60) \
,($7 * 60 / 100 -60 -48.75) \
,$9,$11,$16,$17}' \
<<<"$ScanResults" |
sort -n >>$DataLog
}
BriefOutput() {
# No dBmV, dBm or secondary channel info
echo -e "RF\tStrngth\tQuality\tSymbol\tVirt #\tName"
printf '%.0s-' {1..49}
echo
awk -v OFS='\t' '{print $3,$7,$9,$11,$16,$17}' \
<<<"$ScanResults" |
sort -n
}
StdOutput() {
# StdOutput : This is the standard output when no options are used
echo -e "RF\tStrngth\tdBmV\tdBm\tQuality\tSymbol\tVirt#1\tName \
\tVirt#2\tName\t\tVirt#3\tName\t\tVirt#4\tName\t\tVirt#5\tName"
printf '%.0s-' {1..72}
echo
awk -v OFS='\t' '{print $3,$7 \
,($7 * 60 / 100 - 60) \
,($7 * 60 / 100 -60 -48.75) \
,$9,$11,$16,$17,"\t"$20,$21,"\t"$24,$25,"\t"$28,$29,"\t"$32,$33,"\t"$36,$37}' \
<<<"$ScanResults" |
sort -n
}
CSVOutput() {
# CSVOutput : Same as standard output, but in CSV format
awk -v OFS=',' '{print $3,$7 \
,($7 * 60 / 100 - 60) \
,($7 * 60 / 100 -60 -48.75) \
,$9,$11,$16,$17,"\t"$20,$21,"\t"$24,$25,"\t"$28,$29,"\t"$32,$33,"\t"$36,$37}' \
<<<"$ScanResults" |
sort -n
}
DebugOutput() {
# DebugOutput : Used when selecting debug mode
# If the dbg= value is less than 5 characters, it uses the calc .14 * $15 + 58.8
# If the dbg= value is 5 characters, it's a newer HDHR4 or HDTC unit and uses
# the calc .00991 * $15 + 45.3
echo -e "RF\tStrngth\tdBmV\tdBm\tQuality\tSNR\tSymbol\tdbg\tCalc_dBmV\tCalc_dBm"
printf '%.0s-' {1..92}
echo
awk -v OFS='\t' \
'{ \
if (length($15) < 5) \
{ printf "%u\t%u\t%3.1f\t%3.1f\t%u\t%2.1f\t%u\t%s\t%2.1f\t\t%2.1f\n", $4,$9 \
,($9 * 60 / 100 - 60) \
,($9 * 60 / 100 -60 -48.75) \
,$11,($11 / 100 * 33),$13,$15,(.14 * $15 + 58.8),"\t"(.14 * $15 + 58.8 - 48.75) } \
else
{ printf "%u\t%u\t%3.1f\t%3.1f\t%u\t%2.1f\t%u\t%s\t%2.1f\t\t%2.1f\n", $4,$9 \
,($9 * 60 / 100 - 60) \
,($9 * 60 / 100 -60 -48.75) \
,$11,($11 / 100 * 33),$13,$15,(.00991 * $15 + 45.3),"\t"(.00991 * $15 + 45.3 - 48.75) } \
}' \
<<<"$ScanResults" |
sort -n
}
ScanType() {
if [ -n "$DEBUG" ]; then
echo "debug mode"
GetDebugData
else
GetScanData
fi
}
FinalOutput() {
if [ -n "$DEBUG" ]; then
DebugOutput
elif [ -n "$DataLog" ]; then
LogOutput
elif [ -n "$CSV" ]; then
CSVOutput
elif [ -n "$BRIEF" ]; then
BriefOutput
else
StdOutput
fi
}
# Calls the above functions in the correct order
DetectOS
CheckOpts "$@"
DiscoverDevices
CheckTunerLockStatus
ScanType
FinalOutput