-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure
executable file
·757 lines (620 loc) · 26.2 KB
/
configure
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#!/usr/bin/perl
#
# This script is for use with the Open Glider Network FLARM Detection System only
# Any other uses are NOT authorised
#
# Copyright (c) 2014, Melissa Jenkins, Temeletry Limited ([email protected])
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# - This code must only be used as part of OGN (Open Glider Network)
#
# - Neither the name of Temeletry Limited nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Code has been sponsored by Temeletry Limited (http://www.temeletry.co.uk)
#
#
# This script provides the device end of the web configuration process
# it works in conjunction with the httpi webserver and allows people
# to configure their device without SSH.
#
use strict;
use File::Copy;
use Data::Dumper;
my $version = '2.1c';
my $pwepoch = 1428964689; # on linux date -d '10/25/2014' '+%s', on bsd date -f '%m/%d/%Y' -j '+%s' '1/1/2000'
# this is the day after the build image was built - to make sure we can detect changed passwords. You need to
# wait at least a day before releasing otherwise the check may fail!
# make sure we have a htdocs directory
mkdir( "/tmp/htdocs" );
qx( /bin/chown ogn:ogn /tmp/htdocs );
# and move out STDOUT to there for the webpage
open(STDOUT, '>', "/tmp/htdocs/index.html") || close(STDOUT);
my $old_handle = select (STDOUT);
$| = 1; # perform flush after each write to STDOUT
select ($old_handle);
#952.400MHz: +1.4dB: +38.91 +38.66 +39.03 +38.81 +38.86 +38.96 [ppm]
#953.600MHz: +2.2dB: +38.25 +38.16 +38.62 +38.21 +38.28 [ppm]
#955.800MHz: +3.7dB: +38.55 +38.51 +38.71 +38.50 +38.57 +38.56 [ppm]
# what we read from the configuration file
my %config;
my @config_order;
my %config_description;
my %config_default;
my $aprscall = 'install';
## figure out our synchronisation key, we will generate a new one if keys are marked as wrong even if one
## exists
my $token;
if( ! -e "/etc/sitetoken.txt" || -e "/etc/ssh/ssh_host_keyswrong" ) {
my $code = qx( dd if=/dev/urandom bs=1 count=8 status=noxfer 2>/dev/null );
($token) = join('-',unpack('H4 H4 H4', $code ));
qx( echo $token > /etc/sitetoken.txt );
}
else {
$token = qx( cat /etc/sitetoken.txt );
chomp $token;
}
my $password = qx( dd if=/dev/urandom bs=1 count=12 status=noxfer 2>/dev/null | base64 2>/dev/null );
my $setuppassword = qx( dd if=/dev/urandom bs=1 count=12 status=noxfer 2>/dev/null | base64 2>/dev/null );
chomp($password);chomp($setuppassword);
my $reducedcontent;
# colours
my $og = "\033[32m";
my $cg = "\033[0m";
my $or = "\033[31m";
my $cr = $cg;
my $number_running = qx( ps aux | grep ogn.configure | grep -v sudo | wc -l ); chomp( $number_running );
if( $number_running > 3 ) {
qx( ps aux | grep ogn.configure | grep -v sudo > /tmp/ps.out );
status( "too many", 'ps', '/tmp/ps.out' );
output_screen( "configure appears to already be running ($number_running).\n\n".
"It will start automatically when the device boots, ".
"running it twice at the same time will cause things to break and possibly kick you out of ".
"your device.\n\n".
"Please check the website http://ognconfig.onglide.com/ and see if your device is".
"listed there.\n\n" );
exit;
}
# stop the sdr process if it was running, this is needed so we can start the webserver
qx( /usr/bin/sudo /usr/sbin/service rtlsdr-ogn stop );
# make sure the webserver is running as we will need it, even if they are running from the command line
system( "/usr/bin/sudo /usr/local/bin/httpi 2>/dev/null");
# remove the incorrectly left reboot and update, don't want them running while we are installing
qx( /usr/bin/crontab -u root -l | /bin/grep -v reboot | /bin/grep -v update | /usr/bin/crontab -u root - );
print "<!DOCTYPE html>\r\n<html><head><title>RTLSDR-Ogn Configuration Tool Client</title>".
'<meta http-equiv="refresh" content="5">'.
'</head><body>';
# and what's happening
output( '<h2>Welcome to the Open Glider Network Configuration Helper</h2>'.
'version '.$version.'<br/>');
output('<p>This tool allows you to configure your ogn receiver using a web browser.</p>'.
'<p>Go to <b>https://ognconfig.onglide.com/configuration.html#'.$token.'</b> to enter the site details</p>'.
"<p>While you do that we will scan for the strongest GSM channel and adjust your gain settings...<br/>".
"Please make sure you have your antenna installed!</p>".
"<br/><hr/>");
# make sure it is in english if the default language is not, otherwise we will not
# match the ifconfig settings properly
if( !$ENV{"LANG"} =~ /^en/ ) {
$ENV{"LANG"} = 'en';
}
# lookup and report the interface addresses.
my $ipstring = '/';
{
my @ips = qx( /sbin/ifconfig | grep "inet addr" | grep -v 127.0.0.1 );
foreach my $ip ( @ips ) {
if( $ip =~ /inet addr:([0-9.]+)/ ) {
$ipstring .= $1 . "+";
}
}
status( "start" );
my $uniqueid = qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=starting\\&version=$version );
chomp($uniqueid);
$uniqueid =~ /:([0-9]+)$/; $uniqueid = $1;
# make a js file that can be include by the other server to provide
# the wrapping website with the token to complete configuration.
# we need the unique id as well because the client browser doesn't get provided
# the token
open JH, ">", "/tmp/htdocs/ogn_configure_callback.js";
print JH "ogn_callback( $uniqueid, '$token' );\r\n";
close JH;
qx( /bin/chown ogn:ogn /tmp/htdocs/ogn_configure_callback.js );
}
output( "checking if ssh keys need to be regenerated:" );
if( -e '/etc/ssh/ssh_host_keyswrong' ) {
output( "yes, this takes a little while<br/>" );
my @list = ('ssh_host_dsa_key',
'ssh_host_dsa_key.pub',
'ssh_host_ecdsa_key',
'ssh_host_ecdsa_key.pub',
'ssh_host_key',
'ssh_host_key.pub',
'ssh_host_rsa_key',
'ssh_host_rsa_key.pub' );
foreach my $file( @list ) {
qx( /usr/bin/sudo /bin/rm /etc/ssh/$file );
}
qx( /usr/bin/sudo /usr/bin/ssh-keygen -A );
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=sshkeys );
status( "sshkeys" );
print STDERR "\nIf you have connected via ssh you will need to remove the old host key on your computer before you can reconnect\n\n";
qx( /usr/bin/sudo /bin/rm /etc/ssh/ssh_host_keyswrong );
sleep(10);
}
else {
output( " no, they are fine.<br/>" );
}
##
# read the file, die if it's a problem... file is hardcoded
read_config();
my $stopScanning = 0;
my $oldSigINT;
my $oldSigINT = $SIG{INT};
$SIG{INT}=\&interruptScan;
my $stopScanning = 0;
output( "Running gsm scan:<br/>" );
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=scanning );
status( "gsm scan" );
#print STDERR "\n----------\n".Dumper(\%config). "\n-------\n";
# check to see if we have previously scanned as an 850
my @scanfreqs;
if( ($config{RF_GSM_CenterFreq}||910) < 900 ) {
@scanfreqs = ( '--gsm-850', undef );
}
else {
@scanfreqs = ( undef, '--gsm-850' );
}
# now try all our scan frequencies until we find a centre frequency
my ($ppm,$gain,$freq);
foreach my $scanfreq ( @scanfreqs ) {
if( ! $freq ) {
($ppm,$gain,$freq) = gsm_scan( $config{RF_FreqCorr}, $config{RF_GSM_Gain}, $scanfreq );
}
}
# update the configuration with the scan results, if it ends correctly
if( ! $stopScanning ) {
if( $freq ) {
$config{RF_FreqCorr} = $ppm;
$config{RF_GSM_Gain} = $gain;
$config{RF_GSM_CenterFreq} = $freq;
# print STDERR Dumper(\%config);
# save the new scan results
write_config();
output( "Saved scan results into configuration file<br/>" );
}
else {
status( "scan failed", 'output', '/tmp/htdocs/index.html' );
output( "<b>There has been a problem with your scan</b><br/>".
"Please check the antenna and SDR receiver is correctly installed and power cycle the device<br/>" );
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=scanfailed );
exit 1;
}
}
else {
output( "Scan results discarded<br/>" );
}
$stopScanning = 0;
# send the status to the server
status( "done", 'output', '/tmp/htdocs/index.html' );
# check for a pushed configuration
output( "Checking for pushed configuration changes:<br/>" );
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=checking );
status( "checking" );
my %new_config = fetch_config($token);
my $retry_counter = 3600/30; # retry for an hour
if( ($new_config{not_found}||0) ) {
output( "<hr/><h3>No online configuration has been found!</h3><br/>".
"We will wait until you complete this before continuing.<br/>".
"If you have saved your configuration and it doesn't appear here automatically try refreshing the page.<br/>",
"If you haven't then please visit <a href='https://ognconfig.onglide.com/configuration.html#$token'>https://ognconfig.onglide.com/configuration.html#$token</a> to complete the configuration.<br/>");
do {
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=waiting );
sleep(30);
%new_config = fetch_config($token);
output( '.' );
$retry_counter --;
} while( ($new_config{not_found}||0) && ! $stopScanning && $retry_counter > 0 );
}
if( $retry_counter <= 0 ) {
status( "timeout" );
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=waiting );
output( "<hr/><h3>Timeout, please reboot the device to restart configuration</h3>" );
exit;
}
status( "done checking" );
if( ! $stopScanning ) {
$config{Position_Latitude}=$new_config{lat};
$config{Position_Longitude}=$new_config{lng};
$config{Position_Altitude}=$new_config{height}+$new_config{elevationantenna};
$config{Position_GeoidSepar}=$new_config{geoid};
$config{APRS_Call}='"'.$new_config{station}.'"';
# form the comment string and add it to the configuraiton
{
my @comment;
push @comment, '"'.$new_config{comments}.'"' if( $new_config{comments} );
push @comment, '"Contact: '.$new_config{email}.'"' if( ($new_config{email}||'') ne '' );
push @comment, '"Antenna: '.$new_config{antenna}.'"' if( ($new_config{antenna}||'') ne '' );
push @comment, '"Webcam: '.$new_config{webcam}.'"' if( ($new_config{webcam}||'') ne '' );
push @comment, '"Weather: '.$new_config{weather}.'"' if( ($new_config{weather}||'') ne '' );
if( scalar @comment ) {
$config{Beacon_Comment}='[ '.join(',', @comment).' ]'
}
}
# new country specific configuration
$config{RF_OGN_CenterFreq} = $new_config{centerfreq};
$config{RF_OGN_SampleRate} = $new_config{samplerate};
$config{RF_OGN_FreqHopChannels} = $new_config{freqhopchannels};
$config{Demodulator_ChannelOfs} = $new_config{channelofs};
$config{Demodulator_ScanMargin} = $new_config{scanmargin};
# print STDERR Dumper(\%config);
# and save these results
write_config();
# change what is going to run on reboot
if( -e "/etc/systemd" ) {
qx( /usr/bin/sudo /bin/systemctl disable first-install 2>/dev/null );
qx( /usr/bin/sudo /bin/systemctl enable rtlsdr-ogn.service 2>/dev/null);
}
else {
qx( /usr/bin/sudo /usr/sbin/update-rc.d -f first-install remove );
qx( /usr/bin/sudo /usr/sbin/update-rc.d -f rtlsdr-ogn defaults 2>/dev/null);
}
# make sure the software is updated
qx( /usr/bin/sudo /home/ogn/update -i 2>/dev/null);
# save the hostname
# if( $new_config{station} =~ /^[a-z0-9]+$/i ) {
# my $existinghost = qx( hostname -s ); chomp( $existinghost );
# qx( echo "ogn-$new_config{station}" > /etc/hostname );
# qx( sed -i "/$existinghost/ s/$existinghost/ogn-$new_config{station}/" /etc/hosts );
# }
# output all the addresses
my @macs = qx( /sbin/ifconfig | /usr/bin/awk '/Loopback/ || /^lo/ {\$0=""} /Link encap/ { print \$1" has a MAC address of " \$5 }' );
my ($ognuser,$wifiuser,$piuser) = (1,1,1);
$ognuser = system( 'echo "ogn:'.$password.'" | /usr/sbin/chpasswd 2>/dev/null' ) if( ! check_pw_changed( 'ogn' ));
$wifiuser = system( 'echo "wifi:'.$setuppassword.'" | /usr/sbin/chpasswd 2>/dev/null' ) if ( ! check_pw_changed( 'wifi' ));
$piuser = system( 'echo "pi:'.$password.'" | /usr/sbin/chpasswd 2>/dev/null' ) if ( ! check_pw_changed( 'pi' ));
# make a js file that can be include by the other server to provide
# the wrapping website with the token to complete configuration.
# we need the unique id as well because the client browser doesn't get provided
# the token
open JH, ">", "/tmp/htdocs/ogn_password_callback.js";
print JH "ogn_password_callback( '$password', $wifiuser, '$setuppassword' );\r\n";
close JH;
open(STDOUT, '>', "/tmp/htdocs/index.html");
print "<html><head><title>RTLSDR-Ogn Configuration Tool Client</title>".
'</head><body>';
output( '<h2>Welcome to the Open Glider Network Configuration Helper</h2>'.
"<p>The following Configuration has been received:<br/>".
"Station Name: ".$new_config{station}."<br/>".
"Altitude of Antenna (AMSL): ".$new_config{elevationantenna}.'+'.$new_config{height}.'='.($new_config{elevationantenna}+$new_config{height})."m<br/>".
"Position: ".sprintf("%0.9f,%0.9f",($new_config{lat}),($new_config{lng}))."<br/>".
"<br/>".
"Configuration was saved at ".$new_config{time}." UTC</p>" );
output( "<h3>" );
if( $ognuser == 0 ) {
output( "The <b>ogn</b> user password has been changed to <b>$password</b> please login and change this!<br/>" );
}
if( $piuser == 0 ) {
output( "The <b>pi</b> user password has been changed to <b>$password</b> please login and change this!<br/>");
}
if( $wifiuser == 0 ) {
output( "The <b>wifi</b> setup user password has been changed to <b>$setuppassword</b> please login and change this!");
}
output( "</h3>" );
output( "<br/>If you would like to configure your router with a static IP for this device you can use the following".
" information:<br/><div style='margin-left: 20px'>");
output( join( '<br/>', @macs ) );
# how to login and reconfigure
output( "</div><br/><hr/>If you wish to re-run this installation please login to the device using <b>ssh</b>" );
if( $ognuser == 0 || $piuser == 0 ) {
output( "and the username and password above." );
}
output( "<br/>Then type <b>sudo service first-install start</b> <br/>" );
# and we are done!
my $old_handle = select (STDOUT);
$| = 1; # perform flush after each write to STDOUT
select ($old_handle);
output( "<br/><br/>".
"<b style='color:green'>Configuration complete! You need to reboot your device now to start the ogn receiver (power cord will do just fine)</b>\n" );
close(STDOUT);
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=done );
# send the status to the server
status_stripped( "confirmation", 'output', '/tmp/htdocs/index.html' );
}
$SIG{INT}=$oldSigINT;
# check to see if the specified password has been changed. If it has then return
# which will stop it being changed again.
sub check_pw_changed {
my ($username) = @_;
# look up the change date
my $pwdata = qx( /usr/bin/passwd -S $username 2>/dev/null ); chomp( $pwdata );
if( $pwdata =~ /$username...([0-9]{2}\/[0-9]{2}\/[0-9]{4})/i ) {
# convert to epoch and then make sure it's a number
my $epoch = qx( /bin/date -d '$1' '+%s' ); chomp ($epoch); $epoch = $epoch + 0;
return ( $epoch > $pwepoch );
}
return 0;
}
sub gsm_scan {
my ($ppm,$gain,$options) = @_;
# if we are interactive and therefore don't need to scan
if( ($ARGV[0]||'') eq '-i' ) {
print "----\n";
return ($config{RF_FreqCorr}, $config{RF_GSM_Gain}, $config{RF_GSM_CenterFreq});
}
my $lastcount;
my $bestfrequency = 0;
my $beststrength = 0;
my $previousgain = $gain;
my $ppmchanged = 0;
$ppm = 0; #better to start at 0 in case a low-drift radio is used
$gain = $gain+0;
my $ppmretry = 0;
my $nochannelsever = 1;
# don't start too high, we want to scan
if( $gain > 40 ) {
$gain = 40;
}
my $loops = 0;
# anything more than 70 is too much, less than 15 means something is wrong
while( $gain < 75 && $gain > 10 && ! $stopScanning ) {
my $previousppm = $ppm;
my %frequencies;
$loops++;
output( "$loops: scanning at gain $gain and offset adjustment $ppm ".($options||'').': ');
print "/home/ogn/rtlsdr-ogn/gsm_scan --ppm $ppm --gain $gain $options 2>/dev/null\n";
my @result = qx( /home/ogn/rtlsdr-ogn/gsm_scan --ppm $ppm --gain $gain $options 2>/dev/null);
# check to make sure we got enough to do something with
if( grep /Not enough data was collected: please retry with higher gain to catch more GSM signals/, @result) {
output( "re-scanning with higher gain, insufficient GSM signals detected.".
($ppmretry >= 4 && $gain > 40 ? "Are you sure the antenna is attached?<br/>" : "<br/>"));
$previousgain = $gain;
$gain += 10;
# if we have hit a high gain then try with a different ppm
if( $nochannelsever && $gain > 60 && $ppmretry < 9 ) {
$gain = 40;
$ppm = ($ppm + 30)%95; if( $ppm < 12 ) { $ppm = 0; }
$ppmretry++;
output( "<br/>Checking with a different ppm adjustment to see if initial estimate was too far off<br/>" );
}
next;
}
# if we passed that then we have seen a channel and are probably not too far off
$nochannelsever = 0;
# check to see if the PPM is right, if it isn't then we will run the scan with a new one
my @ppmcorrection = grep /Receiver Xtal correction/, @result;
if( $ppmcorrection[0] =~ /Receiver Xtal correction = [0-9.+-]+ = ([0-9+-]+).* ppm/ ) {
$ppm = $1+0;
if( abs($ppm-$previousppm) > 1 ) {
$ppmchanged = 1;
$gain = 25;
output( "re-scanning with new ppm adjustment<br/>");
next;
}
}
if( grep /please retry with better initial estimate or with lower gain to reduce distortions/, @result ) {
output( "re-scanning with lower gain, inconsistent drift detected. If your scan fails it may be because the antenna is not attached?<br/>");
$previousgain = $gain;
$gain -= 10;
next;
}
# next we need to find the best frequency
my @gsm = grep /MHz[:]/, @result; #:
foreach my $line ( @gsm ) {
if( ! ($line =~ /([0-9.]+)MHz.*[+]([0-9.]+)dB:[^+-]([0-9.+ -]+)/) ) {
next;
}
my $frequency = $1;
my $strength = $2;
my $offsets = $3;
my @offset_array = split(/\s+/, $offsets );
my ($sum, $count) = (0,$#offset_array+1);
foreach my $offset (@offset_array) {
$sum += $offset;
}
# make sure the offset isn't too far out of line
if( abs($sum/$count) < 4 ) {
# store away so we can search when we are done
$frequencies{$frequency} = $strength-(abs($sum/$count)*2);
# output "{ correction => abs($sum/$count), gain => $gain, weight => $gain-(abs($sum/$count)*2) }<br/>";
}
}
# find the best
my @best = sort { $frequencies{$b} <=> $frequencies{$a} } keys(%frequencies);
# make sure we don't break things if adapter isn't installed
if( $best[0]+0 == 0 ) {
output( "<br/><br/><b>Are you sure you have the dongle installed, no channels were found!</b><br/>");
return (undef,undef,undef);
}
output( "best frequency is $best[0] @ ".(int($frequencies{$best[0]}*10)/10)." (weighted)<br/>" );
if( $frequencies{$best[0]} < ($beststrength+2.4) && ! $ppmchanged && $loops > 1 ) {
output( "Previous scan was better, will configure using gain of $previousgain and frequency $bestfrequency, ppm adjustment of $ppm<br/>");
return ($ppm, $previousgain, $bestfrequency ); #previousppm? seems to settle closer to right using current
}
# try again with more gain
$beststrength = $frequencies{$best[0]};
$bestfrequency = $best[0];
$previousgain = $gain;
$gain += 5;
$ppmchanged = 0;
}
if( ! $stopScanning ) {
output( "stopped scanning at gain $gain<br/>");
output( "Configuring with gain of $previousgain and frequency $bestfrequency<br/>");
return ($ppm, $previousgain, $bestfrequency );
}
}
sub read_config {
my @sections;
# if the configuration file is empty or doesn't exist then we need to
if( ! -e '/home/ogn/rtlsdr-ogn.conf' || -z '/home/ogn/rtlsdr-ogn.conf' ) {
output( "Copying Template.conf as configuration file appears to be missing<br/>" );
status( "using template.conf" );
copy( '/home/ogn/rtlsdr-ogn/Template.conf', '/home/ogn/rtlsdr-ogn.conf' );
}
# read it into the hash
open( my $fh, '<', "/home/ogn/rtlsdr-ogn.conf" ) || die "unable to open configuration file";
while( <$fh> ) {
my $line = $_;
chomp $line;
if( $line =~ /^\s*([A-Za-z]+):/ ) {
push @sections, $1;
push @config_order, "+$1";
}
if( $line =~ /^\s*}/ ) {
pop @sections;
push @config_order, "-$1";
}
if( $line =~ /(##|)\s*([A-Za-z]+)\s*=\s*([^;]+)(.*)/ ) {
my $key = join('_',@sections,$2);
if( $1 ne '##' ) { # keep as a default if we don't have a value
$config{$key} = $3;
$config_default{$key} = $3;
}
else {
$config_default{$key} = $3;
}
$config_description{$key} = $4;
push @config_order, "=$key,$2"; # so we know what order to write in
}
}
close ($fh);
# send the status to the server
status( "read", 'configuration', '/home/ogn/rtlsdr-ogn.conf' );
}
sub write_config {
my $depth = '';
my $first =0;
copy( '/home/ogn/rtlsdr-ogn.conf', '/home/ogn/rtlsdr-ogn/rtlsdr-ogn.conf.'.date(time()));
if( ! open( FH, '>', "/home/ogn/rtlsdr-ogn.conf" ) ) {
qx( curl -s https://ognconfig.onglide.com/perl/startconfiguration.pl?token=$token$ipstring\\&status=error );
status( "write failed $!" );
output( "<h1>Unable to write to configuration file! error: $!</h1>" );
die "unable to open configuration file";
}
foreach my $key ( @config_order ) {
my $action = substr($key,0,1);
my $id = substr($key,1);
if( $action eq '+' ) {
print FH $depth.$id . ":\n".$depth.'{';
$depth .= ' ';
$first = 1;
}
elsif( $action eq '-' ) {
$depth = substr($depth,0,-2);
print FH $depth."} ;\n";
}
elsif( $action eq '=' ) {
my ($k,$i) = split( ',', $id );
if( $first ) {
print FH ' ';
}
else {
print FH $depth;
}
if( ! defined($config{$k}) || $config{$k} eq '' ) {
print FH "## $i=" . $config_default{$k} . $config_description{$k}."\n";
}
else {
print FH $i . "=" . $config{$k} . $config_description{$k}."\n";
}
$first = 0;
}
}
close(FH);
status( "write", 'configuration', '/home/ogn/rtlsdr-ogn.conf' );
# make sure we can read the newly written file
qx( /bin/chown ogn:ogn /home/ogn/rtlsdr-ogn.conf );
}
sub fetch_config {
my ($token) = @_;
# request the configuration details from the website
# status( 'get configuration' );
my @result = qx( curl -s https://ognconfig.onglide.com/perl/getconfiguration.pl?token=$token );
my %newconfig;
if( $#result == -1 ) {
$newconfig{not_found} = 1;
}
foreach my $l ( @result ) {
chomp($l);
my ($k,@v) = split(':',$l);
$newconfig{$k} = join(':',@v);
}
# store the new callsign for status display
$aprscall = $newconfig{station}||'notdefined';
# print STDERR "\n****\n".Dumper(\%newconfig)."\n";
return %newconfig;
}
sub interruptScan {
$stopScanning = 1;
output( "<br/>\n** Interrupted, please wait **<br/>" );
status( 'interruptScan' );
}
sub date {
my @t = gmtime($_[0]);
return sprintf( "%04d-%02d-%02dT%02d-%02d-%02d", ($t[5]+1900),($t[4]+1),$t[3],$t[2],$t[1],$t[0]);
}
sub output {
my @items = @_;
print join( "\n", @items ) . "\n";
output_screen( @items );
}
sub output_screen {
my @items = @_;
# and dump a slightly tidier version to the screen
my $og = "\033[32m";
my $cg = "\033[0m";
my $or = "\033[31m";
foreach my $t (@items) {
$t =~ s%<(|/)(br|p)(|/)>%\n%g;
$t =~ s!<(|/)(br|p)(|/)>!\n!gm;
$t =~ s%<hr(/|)>%\n----------------------------------------\n%mg;
$t =~ s/<h[0-9]>/$og/mg; $t =~ s|</h[0-9]>|$cg\n|mg;
$t =~ s/<b(| style=[^>]*)>/$or/mg;$t =~ s|</b>|$cg|mg;
$t =~ s/<div(| style=[^>]*)>//mg;$t =~ s|</div>||mg;
$t =~ s/<a [^>]*>/$or/mg;$t =~ s|</a>|$cg|mg;
$t =~ s/\n\n\n/\n\n/mg;
print STDERR $t;
}
}
sub status {
my ($status,$type,$filename) = @_;
$status =~ s/[^A-Za-z0-9.,_+:-]//gi;
$aprscall =~ s/[^A-Za-z0-9.,_+:-]//gi;
if( $type && $filename ) {
system( "curl -s -F 'upload=\@$filename' -F 'type=$type' -F 'status=$status' -F 'station=$aprscall' -F 'token=$token' -F 'version=$version' -F 'pid=$$' https://ognconfig.onglide.com/perl/installstatus.pl > /dev/null" );
}
else {
system( "curl -s -F 'status=$status' -F 'station=$aprscall' -F 'token=$token' -F 'version=$version' -F 'pid=$$' https://ognconfig.onglide.com/perl/installstatus.pl > /dev/null" );
}
}
# send a file encrypted with the ognconfig public key
sub status_stripped {
my ($status,$type,$filename) = @_;
qx( /bin/grep -ve 'password has been changed to.*please login' $filename > $filename.stripped );
status( $status, $type, $filename.'.stripped' );
}