-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanytone-channel-checker.pl
executable file
·386 lines (324 loc) · 9.09 KB
/
anytone-channel-checker.pl
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/perl
use strict;
use Text::CSV;
use constant {
CHAN_NUM => 0,
CHAN_NAME => 1,
CHAN_RX_FREQ => 2,
CHAN_TX_FREQ => 3,
CHAN_MODE => 4,
CHAN_POWER => 5,
CHAN_BANDWIDTH => 6,
CHAN_CTCSS_DEC => 7,
CHAN_CTCSS_ENC => 8,
CHAN_CONTACT => 9,
CHAN_CALL_TYPE => 10,
CHAN_RADIO_ID => 11,
CHAN_TX_PERMIT => 13,
CHAN_SQUELCH => 14,
CHAN_OPT_SIG => 15,
CHAN_DTMF_ID => 16,
CHAN_2TONE_ID => 17,
CHAN_COLOR_CODE => 18,
CHAN_SLOT => 21,
CHAN_SCANLIST_NAME => 22,
};
use constant {
VAL_DIGITAL => 'D-Digital',
VAL_ANALOG => 'A-Analog',
};
use constant {
COLOR_OK => "#88FF88",
COLOR_WARN => "#FFFF88",
COLOR_BAD => "#FF8888",
};
my $csv = Text::CSV_XS->new({binary => 1, auto_diag => 1, quote_char=>'"'});
main();
exit 0;
sub main
{
my @data;
my @header;
if (scalar (@ARGV) == 0)
{
usage();
exit(-1);
}
html_start();
my $filename = $ARGV[0];
# walk through the file and build a giant 2D array of the rows and columns in this file.
open(my $fh, $filename) or die("Couldn't open file '$filename': $!\n");
for(my $line_no = 0; my $row = $csv->getline($fh); $line_no++)
{
if ($line_no == 0)
{
@header = @$row;
}
else
{
push @data, $row;
}
}
close ($fh) or die("Couldn't close file '$filename': $!\n");
report_digital_repeater_freq_pairs(\@data);
report_digital_repeater_color_codes(\@data);
report_digital_repeater_talk_groups_slots(\@data);
report_digital_repeater_arbitrary_fields(\@data);
html_end();
}
sub report_digital_repeater_freq_pairs
{
my ($data_ref) =@_;
my @key_array = (0);
analyze_key_value_pairs($data_ref, \@key_array, \&repeater_name, \&repeater_pair, \&digital_repeater_filter,
\&chan_num_and_name, "Scan List Name", "Frequency Pair (RX / TX)",
join_freq_pairs("???.?????","???.?????"),
"Digital Repeater Frequency Pair Consistency Report");
}
sub report_digital_repeater_color_codes
{
my ($data_ref) = @_;
my @key_array = (0);
analyze_key_value_pairs($data_ref, \@key_array, \&repeater_name, \&color_code, \&digital_repeater_filter,
\&chan_num_and_name, "Scan List Name", "Color Code", "??",
"Digital Repeater Color Code Consistency Report");
}
sub report_digital_repeater_talk_groups_slots
{
my ($data_ref) = @_;
my @key_array = (0);
analyze_key_value_pairs($data_ref, \@key_array, \&talk_group, \&time_slot, \&digital_repeater_filter,
\&chan_num_and_name, "Talk Group", "Time Slot", "??",
"Digital Repeater Talk Group / Time Slot Consistency Report");
}
sub report_digital_repeater_arbitrary_fields
{
my ($data_ref) = @_;
my @key_array = (5, 6, 7, 8, 10, 12, 13, 14, 15, 16, 17, 18, 23, 24, 25, 26, 27, 28, 29, 30, 31,32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44);
analyze_key_value_pairs($data_ref, \@key_array, \&arbitrary_field_key, \&arbitrary_field_value,
\&digital_repeater_filter, \&chan_num_and_name, "Field", "Value", "??",
"Digital Repeater Other Field Consistency Report");
}
sub color_code
{
my ($id, $chan) = @_;
return $chan->[CHAN_COLOR_CODE];
}
sub talk_group
{
my ($id, $chan) = @_;
return $chan->[CHAN_CONTACT];
}
sub time_slot
{
my ($id, $chan) = @_;
return $chan->[CHAN_SLOT];
}
sub chan_num_and_name
{
my ($id, $chan) = @_;
return "Channel " . $chan->[CHAN_NUM] . " - \"" . $chan->[CHAN_NAME] . "\"";
}
sub repeater_pair
{
my ($id, $chan) = @_;
return join_freq_pairs($chan->[CHAN_RX_FREQ], $chan->[CHAN_TX_FREQ]);
}
sub repeater_name
{
my ($id, $chan) = @_;
return $chan->[CHAN_SCANLIST_NAME];
}
sub arbitrary_field_value
{
my ($id, $chan) = @_;
return $chan->[$id];
}
sub arbitrary_field_key
{
my ($id, $chan) = @_;
return anytone_channels_csv_column_name($id);
}
sub digital_repeater_filter
{
my ($chan) = @_;
return $chan->[CHAN_MODE] eq VAL_DIGITAL && $chan->[CHAN_RX_FREQ] ne $chan->[CHAN_TX_FREQ];
}
sub analyze_key_value_pairs
{
my ($data_ref, $key_array, $key_func, $value_func, $filter_func, $chan_name_func,
$key_name, $value_name,
$unknown_value, $title) = @_;
html_section($title);
my %data;
foreach my $chan (@{$data_ref})
{
if ($filter_func->($chan))
{
foreach my $key_id (@{$key_array})
{
my $value = $value_func->($key_id, $chan);
my $key = $key_func->($key_id, $chan);
push @{$data{$key}->{$value}}, $chan_name_func->($key_id, $chan);
}
}
}
print "<table>";
print "<tr><th>$key_name</th><th>Count</th><th>$value_name</th></tr>";
foreach my $key (sort keys %data)
{
my $sum = 0;
my $max_count = 0;
my $max_value = '';
my $distinct_values = 0;
foreach my $value (sort keys %{$data{$key}})
{
my $count = scalar @{$data{$key}->{$value}};
if ($count > $max_count)
{
$max_value = $value;
$max_count = $count;
}
$sum += $count;
$distinct_values++;
}
my $likely_value;
my $likely_count = $max_count;
my $bg_color;
if ($distinct_values == 1)
{
$bg_color = COLOR_OK;
$likely_value = $max_value;
}
elsif ($max_count / $sum > 0.70) # if 70% of the channels are using this pair it's the dominate pair
{
$bg_color = COLOR_BAD;
$likely_value = $max_value;
}
else
{
$bg_color = COLOR_WARN;
$likely_value = $unknown_value;
$likely_count = "??";
}
print "<tr bgcolor='$bg_color'><td><b>$key</b></td><td>$likely_count</td><td><b>$likely_value</b></td></tr>";
# OK... so we found more than one value for this key... let's highlight the odd-balls
if ($distinct_values != 1)
{
foreach my $value (sort keys %{$data{$key}})
{
next if ($value eq $likely_value); # these are the good ones, no need to print them
foreach my $chan_desc (@{$data{$key}->{$value}})
{
print "<tr bgcolor='$bg_color'><td> $chan_desc</td>";
print "<td></td>";
print "<td>$value</td></tr>";
}
}
}
}
print "</table>";
}
sub usage
{
print "$0 <channels file CSV>\n";
}
sub join_freq_pairs
{
my ($rx, $tx) = @_;
return "$rx $tx";
}
sub split_freq_pairs
{
my ($pair) = @_;
my ($rx, $tx);
if ($pair =~ /^(.*?)\s+(.*)/)
{
$rx = $1;
$tx = $2;
}
return ($rx, $tx);
}
sub html_start
{
print <<'HTML_START';
<html><head><title>Andrew's Booger Detector</title>
<style type="text/css">
table, th, td {
border: 1px solid black;
border-collapse: collapse;
font-family: monospace;
text-align: left;
padding: 2px;
}
</style>
</head><body>
<h1>Andrew's Booger Detector</h1>
HTML_START
}
sub html_section
{
my $section = shift @_;
print "<h2>$section<h2>";
}
sub html_end
{
print "</body></html>";
}
sub anytone_channels_csv_column_name
{
my $col_num = shift @_;
my %cols = (
0 => "No.",
1 => "Channel Name",
2 => "Receive Frequency",
3 => "Transmit Frequency",
4 => "Channel Type",
5 => "Transmit Power",
6 => "Band Width",
7 => "CTCSS/DCS Decode",
8 => "CTCSS/DCS Encode",
9 => "Contact",
10 => "Contact Call Type",
11 => "Contact TG/DMR ID",
12 => "Radio ID",
13 => "Busy Lock/TX Permit",
14 => "Squelch Mode",
15 => "Optional Signal",
16 => "DTMF ID",
17 => "2Tone ID",
18 => "5Tone ID",
19 => "PTT ID",
20 => "Color Code",
21 => "Slot",
22 => "Scan List",
23 => "Receive Group List",
24 => "TX Prohibit",
25 => "Reverse",
26 => "Simplex TDMA",
27 => "TDMA Adaptive",
28 => "Encryption Type",
29 => "Digital Encryption",
30 => "Call Confirmation",
31 => "Talk Around",
32 => "Work Alone",
33 => "Custom CTCSS",
34 => "2TONE Decode",
35 => "Ranging",
36 => "Through Mode",
37 => "Digi APRS RX",
38 => "Analog APRS PTT Mode",
39 => "Digital APRS PTT Mode",
40 => "APRS Report Type",
41 => "Digital APRS Report Channel",
42 => "Correct Frequency[Hz]",
43 => "SMS Confirmation",
44 => "Exclude channel from roaming",
);
my $result = "Unknown Column '$col_num'";
if (length($cols{$col_num}) > 0)
{
$result = $cols{$col_num};
}
return $result;
}