-
Notifications
You must be signed in to change notification settings - Fork 35
/
gqrx-scan.c
1659 lines (1491 loc) · 54.1 KB
/
gqrx-scan.c
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2017 neural75
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* gqrx-scanner
* A simple frequency scanner for gqrx
*
* usage: gqrx-scanner [-h|--host <host>] [-p|--port <port>] [-m|--mode <sweep|bookmark>]
* [-f <central frequency>] [-b|--min <from freq>] [-e|--max <to freq>]
* [-d|--delay <lingering time on signals>]
* [-t|--tags <"tag1|tag2|...">]
* [-x|--disable-sweep-store] [-s <min_hit_to_remember>] [-m <max_miss_to_forget>]
*/
#define _GNU_SOURCE // strcasestr
#include <stdio.h>
#ifndef OSX
#include <stdio_ext.h>
#else
#endif
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdbool.h>
#ifndef OSX
#include <linux/limits.h>
#else
#include <sys/syslimits.h>
#endif
#include <termios.h>
#include <time.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "gqrx-prot.h"
#define NB_ENABLE true
#define NB_DISABLE false
//
// Globals definitions
//
typedef struct {
freq_t freq; // frequency in Mhz
double noise_floor; // averages noise floor of frequency
int count; // hit count on sweep scan
int miss; // miss count on sweep scan
char descr[BUFSIZE]; // ugly buffer for descriptions: TODO convert it in a pointer
char *tags[TAG_MAX]; // tags
int tag_max;
}FREQ;
typedef enum
{
sweep,
bookmark
} SCAN_MODE;
// Stores
/*FREQ Frequencies[FREQ_MAX] = {0};*/
FREQ* Frequencies; //if user would exceed 4096 different frequencies counter it would be bad
//I've took liberty of changing it to dynamic array
//implementation in main after ParseInputOptions()
int Frequencies_Max = 0;
FREQ SavedFrequencies[SAVED_FREQ_MAX] = {0};
int SavedFreq_Max = 0;
FREQ BannedFrequencies[SAVED_FREQ_MAX] = {0};
int BannedFreq_Max = 0;
static char freq_string[BUFSIZE] = {0};
//
// Defaults
//
const char *g_hostname = "localhost";
const int g_portno = 7356;
const freq_t g_freq_delta = 1000000; // +- 1Mhz default bandwidth to scan from tuned freq.
const freq_t g_default_scan_bw = 10000; // default scan frequency steps (10Khz)
const freq_t g_ban_tollerance = 10000; // +- 10Khz bandwidth to ban from current freq.
const long g_delay = 2500000; // 2 sec //LWVMOBILE: Doesn't this default value equal 20 seconds? Changing to a lower number. Was 2000000000
//LWVMOBILE: Above variable was set WAY too high, it wasn't 2 seconds, but 2000 seconds.
const char *g_bookmarksfile = "~/.config/gqrx/bookmarks.csv";
//LWVMOBILE: Insert new constants here for scan speed and date format?? Or just use variable with speed set already
//
// Input options
//
char *opt_hostname = NULL;
int opt_port = 0;
freq_t opt_freq = 0;
freq_t opt_min_freq = 0;
freq_t opt_max_freq = 0;
freq_t opt_scan_bw = g_default_scan_bw;
long opt_delay = 0; //LWVMOBILE: Changing this variable from 0 to 250 attempt to fix 'no delay argument given' stoppage on bookmark scan
//LWVMOBILE: New variables inserted here
long opt_speed = 250000;
long opt_date = 0;
//LWVMOBILE; End new variables.
SCAN_MODE opt_scan_mode = sweep;
bool opt_tag_search = false;
char *opt_tags[TAG_MAX] = {0};
int opt_tag_max = 0;
bool opt_disable_store = false;
long opt_max_listen = 0;
// only for debug
bool opt_verbose = false;
// set squelch delta
double opt_squelch_delta = 0.0;
bool opt_squelch_delta_auto_enable = false;
//
// Local Prototypes
//
bool BanFreq (freq_t freq_current);
bool IsBannedFreq (freq_t *freq_current);
void ClearAllBans ( void );
//
// ParseInputOptions
//
void print_usage ( char *name )
{
printf ("Usage:\n");
printf ("%s\n\t\t[-h|--host <host>] [-p|--port <port>] [-m|--mode <sweep|bookmark>]\n", name);
printf ("\t\t[-f <central frequency>] [-b|--min <from freq>] [-e|--max <to freq>]\n");
printf ("\t\t[-d|--delay <lingering time in milliseconds>]\n");
printf ("\t\t[-l|--max-listen <maximum listening time in milliseconds>]\n");
printf ("\t\t[-t|--tags <\"tag1|tag2|...\">]\n");
printf ("\t\t[-v|--verbose]\n");
printf ("\n");
printf ("-h, --host <host> Name of the host to connect. Default: localhost\n");
printf ("-p, --port <port> The number of the port to connect. Default: 7356\n");
printf ("-m, --mode <mode> Scan mode to be used. Default: sweep\n");
printf (" Possible values for <mode>: sweep, bookmark\n");
printf ("-f, --freq <freq> Frequency to scan with a range of +- 1MHz.\n");
printf (" Default: the current frequency tuned in Gqrx Incompatible with -b, -e\n");
printf ("-b, --min <freq> Frequency range begins with this <freq> in Hz. Incompatible with -f\n");
printf ("-e, --max <freq> Frequency range ends with this <freq> in Hz. Incompatible with -f\n");
printf ("-s, --step <freq> Frequency step <freq> in Hz. Default: %llu\n", g_default_scan_bw);
printf ("-d, --delay <time> Lingering time in milliseconds before the scanner reactivates. Default 2000\n");
printf ("-l, --max-listen <time> Maximum time to listen to an active frequency. Default 0, no maximum\n");
printf ("-x, --speed <time> Time in milliseconds for bookmark scan speed. Default 250 milliseconds.\n");
printf (" If scan lands on wrong bookmark during search, use -x 500 (ms) to slow down speed\n");
printf ("-y --date Date Format, default is 0.\n");
printf (" 0 = mm-dd-yy\n");
printf (" 1 = dd-mm-yy\n");
printf ("-q, --squelch_delta <dB>|a<dB> If set creates bottom squelch just for listening.\n");
printf (" It may reduce unnecessary squelch audio supress.\n");
printf (" Default: 0.0\n");
printf (" Ex.: 6.5\n");
printf (" Place \"a\" switch before <dB> value to turn into auto mode\n");
printf (" It will determine squelch delta based on noise floor and\n");
printf (" <dB> value will determine how far squelch delta will be placed from it.\n");
printf (" Ex.: a0.5\n");
printf ("-t, --tags <\"tags\"> Filter signals. Match only on frequencies marked with a tag found in \"tags\"\n");
printf (" \"tags\" is a quoted string with a '|' list separator: Ex: \"Tag1|Tag2\"\n");
printf (" tags are case insensitive and match also for partial string contained in a tag\n");
printf (" Works only with -m bookmark scan mode\n");
//printf ("\t\t[-x|--disable-sweep-store] [-s <min hit>] [-m <max miss>]
printf ("-v, --verbose Output more information during scan (used for debug). Default: false\n");
printf ("--help This help message.\n");
printf ("\n");
printf ("Examples:\n");
printf ("%s -m bookmark --min 430000000 --max 431000000 --tags \"DMR|Radio Links\"\n", name);
printf ("\tPerforms a scan using Gqrx bookmarks, monitoring only the frequencies\n");
printf ("\ttagged with \"DMR\" or \"Radio Links\" in the range 430MHz-431MHz\n");
printf ("%s --min 430000000 --max 431000000 -d 3000\n", name);
printf ("\tPerforms a sweep scan from frequency 430MHz to 431MHz, using a delay of \n");
printf ("\t3 secs as idle time after a signal is lost, restarting the sweep loop when this time expires\n");
printf ("\n");
printf ("Full documentation available at <https://github.com/neural75/gqrx-scanner>\n");
exit (EXIT_FAILURE);
}
bool ParseTags (char *tags)
{
char *tag = NULL;
tag = strtok (tags, "|");
int k = 0;
while (tag != NULL && k < TAG_MAX)
{
int len = strlen(tag) + 1 ;
opt_tags[k] = calloc(sizeof(char), len);
strncpy(opt_tags[k], tag, len);
tag = strtok(NULL, "|");
k++;
}
opt_tag_max = k;
if (k == 0) // wtf
{
printf ("Error: -t option requires a '|' separator for list of tags.\n");
return false;
}
return true;
}
bool ParseInputOptions (int argc, char **argv)
{
int c;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
//{"verbose", no_argument, &opt_verbose, 1},
/* These options don’t set a flag.
We distinguish them by their indices. */
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'w'},
{"host", required_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{"mode", required_argument, 0, 'm'},
{"freq", required_argument, 0, 'f'},
{"min", required_argument, 0, 'b'},
{"max", required_argument, 0, 'e'},
{"step", required_argument, 0, 's'},
{"tags", required_argument, 0, 't'},
{"delay", required_argument, 0, 'd'},
{"speed", required_argument, 0, 'x'},
{"date", required_argument, 0, 'y'},
{"squelch_delta", required_argument, 0, 'q'},
{"max-listen", required_argument, 0, 'l'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "vwh:p:m:f:b:e:s:t:d:x:y:q:l:",
long_options, &option_index);
// warning: I don't know why but required argument are not so "required"
// if a following option is encountered getopt_long returns this option as the argument in optarg
// instead of error, but if there is only one option with a missing arg then it returns an error.
//
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'v':
opt_verbose = true;
break;
case 'h':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
opt_hostname = optarg;
break;
case 'p':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_port = atoi (optarg)) == 0)
{
printf("Error: -%c: invalid port\n", c);
print_usage(argv[0]);
}
break;
case 'm':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if (strcmp (optarg, "sweep") == 0)
opt_scan_mode = sweep;
else if (strcmp (optarg, "bookmark") == 0)
opt_scan_mode = bookmark;
else
{
printf ("Error: -m, --mode <mode>. Mode not recognized. \n");
print_usage(argv[0]);
}
break;
case 'f':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_freq = atoll(optarg)) == 0)
{
printf ("Error: -%c: Invalid frequency\n", c);
print_usage(argv[0]);
}
if (opt_freq > g_freq_delta)
{
opt_min_freq = opt_freq - g_freq_delta;
opt_max_freq = opt_freq + g_freq_delta;
}
else
{
printf ("Error: -%c: Invalid frequency\n", c);
print_usage(argv[0]);
}
break;
case 'b':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_min_freq = atoll(optarg)) == 0)
{
printf ("Error: -%c: Invalid frequency\n", c);
print_usage(argv[0]);
}
break;
case 'e':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_max_freq = atoll(optarg)) == 0)
{
printf ("Error: -%c: Invalid frequency\n", c);
print_usage(argv[0]);
}
break;
case 'd':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_delay = atol(optarg)) == 0)
{
printf ("Error: -%c: Invalid delay\n", c);
print_usage(argv[0]);
}
opt_delay *= 1000; // in microsec
break;
case 'l':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_max_listen = atol(optarg)) == 0)
{
printf ("Error: -%c: Invalid time\n", c);
print_usage(argv[0]);
}
opt_max_listen *= 1000; // in microsec
break;
case 'x':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_speed = atol(optarg)) == 0)
{
printf ("Error: -%c: Invalid speed\n", c);
print_usage(argv[0]);
}
opt_speed *= 1000; // in microsec //LWVMOBILE: Made new opt_speed variable. Implemented and working for bookmark mode.
break;
case 'y':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
errno = 0;
char *endptr = NULL;
opt_date = strtol(optarg,&endptr,10);
if (errno != 0)
{
printf ("Error: -%c: Invalid date option\n", c);
print_usage(argv[0]);
}
break;
case 'q':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if (optarg[0] == 'a')
{
if ((opt_squelch_delta = atof(optarg+1)) == 0)
{
printf ("Error: -%c: Invalid squelch level\n", c);
print_usage(argv[0]);
}
opt_squelch_delta_auto_enable = true;
}
else
{
if ((opt_squelch_delta = atof(optarg)) == 0)
{
printf ("Error: -%c: Invalid squelch level\n", c);
print_usage(argv[0]);
}
}
printf("Squelch delta set: %f\n", opt_squelch_delta);
break;
case 't':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
optind--;
if (!ParseTags(argv[optind]))
print_usage(argv[0]);
optind++;
opt_tag_search = true;
break;
case 's':
if (optarg[0] == '-')
{
printf ("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
if ((opt_scan_bw = atoll(optarg)) == 0)
{
printf ("Error: -%c: Invalid frequency step\n", c);
print_usage(argv[0]);
}
break;
case '?':
/* getopt_long already printed an error message. */
case ':':
default:
print_usage(argv[0]);
}
}
return true;
}
//
// Utilities
//
// return a statically allocated string of the freq to be printed out.
char * print_freq (freq_t freq)
{
// fist round up to khz
freq = round ( freq / 1000.0 ) * 1000.0;
long Ghz = freq/1000000000;
long Mhz = (freq/1000000)%1000;
long Khz = (freq/1000)%1000;
freq_string[0] = '\0';
char temp[256];
if (Ghz)
{
sprintf (temp, "%ld.%3.3ld.%3.3ld GHz", Ghz, Mhz, Khz);
strcat(freq_string, temp);
return freq_string;
}
if (Mhz)
{
sprintf (temp, "%ld.%3.3ld MHz", Mhz, Khz);
strcat(freq_string, temp);
return freq_string;
}
sprintf (temp, "%ld KHz", Khz);
strcat(freq_string, temp);
return freq_string;
}
//
// Wait a key press
//
int kbhit(void)
{
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
//
// Set/Reset non blocking mode
//
void nonblock(int state)
{
struct termios ttystate;
//get the terminal state
tcgetattr(STDIN_FILENO, &ttystate);
if (state==NB_ENABLE)
{
//turn off canonical mode
ttystate.c_lflag &= ~ICANON;
//minimum of number input read.
ttystate.c_cc[VMIN] = 1;
}
else if (state==NB_DISABLE)
{
//turn on canonical mode
ttystate.c_lflag |= ICANON;
}
//set the terminal attributes.
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
}
//
// GetTime
// Get the time stamp dd-mm-yy hh:mm:ss
//
time_t GetTime(char *timestamp)
{
time_t etime = time(NULL);
struct tm *ltime = localtime (&etime);
switch (opt_date)
{
case 0:
sprintf(timestamp, "%2.2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", ltime->tm_mon+1, ltime->tm_mday, ltime->tm_year%100,
ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
break;
case 1:
sprintf(timestamp, "%2.2d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", ltime->tm_mday, ltime->tm_mon+1, ltime->tm_year%100,
ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
break;
}
return etime;
}
//LWVMOBILE: It is very tempting to lop off part of this function, how many TX lasts more than a few seconds or minutes??
//LWVMOBILE: Will still need to rework eventually, this will cause any time greater than 60 minutes to roll over back to 0 I believe
//neural: Why bother? if there are transmissions lasting more than 1 hour or 1 day, the string should be consistent with the long duration.
// Calculate difference in time in [dd days][hh:][mm:][ss secs]
time_t DiffTime(char *timestamp, time_t start_time)
{
double seconds;
time_t etime = time (NULL);
seconds = difftime(etime , start_time);
// casting to time_t, someone with better idea may change this to be more consistent
time_t elapsed = (time_t)seconds;
struct tm *ltime = localtime(&elapsed);
timestamp[0] = '\0';
if (ltime->tm_mday > 1)
{
char days[10];
sprintf(days, "%2d days ", ltime->tm_mday);
strcat(timestamp, days);
}
if (ltime->tm_hour > (int)(ltime->tm_gmtoff/3600))
{
char hours[10];
sprintf(hours, "%2.2d:", (int)(ltime->tm_hour - (ltime->tm_gmtoff/3600)) );
strcat(timestamp, hours);
}
if (ltime->tm_min > 0)
{
char min[16];
sprintf(min, "%2.2d:", ltime->tm_min);
strcat(timestamp, min);
}
char sec[16];
sprintf(sec, "%2.2d sec", ltime->tm_sec);
strcat(timestamp, sec);
return elapsed;
}
//
// CheckUserInput
//
// Clear the bans if 'c' is pressed during the scan cycles
//
void CheckUserInput (void)
{
int hit = 0;
char c;
bool pause = false;
long sleep = 100000; // 100 ms
#ifndef OSX
__fpurge(stdin);
#else
fpurge(stdin);
#endif
nonblock(NB_ENABLE);
do
{
hit = kbhit();
if (hit != 0)
{
c = fgetc(stdin);
switch (c)
{
case 'c':
{
// Clear all bans
ClearAllBans();
continue;
}
case 'p':
{
// pause until another 'p'
pause ^= true; // switch pause mode
break;
}
default:
break;
}
}
if (pause)
{
usleep (sleep);
continue;
}
} while ( hit != 0 || pause );
nonblock(NB_DISABLE);
return;
}
//
// WaitUserInputOrDelay
// Waits for user input or a delay after the carrier is gone
// Returns if the user has pressed <space> or <enter> to skip frequency
//
bool WaitUserInputOrDelay (int sockfd, long delay, freq_t *current_freq)
{
double squelch;
double level;
long sleep_time = 0, listen_time = 0, sleep = 100000; // 100 ms
int exit = 0;
char c;
bool skip = false;
bool pause = false;
#ifndef OSX
__fpurge(stdin);
#else
fpurge(stdin);
#endif
nonblock(NB_ENABLE);
do
{
GetCurrentFreq(sockfd, current_freq);
GetSquelchLevel(sockfd, &squelch);
GetSignalLevel(sockfd, &level );
exit = kbhit();
if (exit != 0)
{
c = fgetc(stdin);
switch (c)
{
case ' ':
case '\n':
{
exit = 1; // exit
skip = true;
break;
}
case 'b':
{
// Ban a frequency
BanFreq(*current_freq);
exit = 1;
skip = true;
break;
}
case 'c':
{
// Clear all bans
ClearAllBans();
exit = 0;
break;
}
case 'p':
{
// pause until another 'p'
pause ^= true; // switch pause mode
exit = 0;
break;
}
default:
exit = 0;
}
if (exit == 1)
break;
}
if (pause)
{
usleep (sleep);
continue;
}
listen_time += sleep;
if (opt_max_listen != 0 && opt_max_listen <= listen_time) {
exit = 1;
skip = true;
}
// exit = 0
if (level < squelch )
{
// Signal drop below the threshold, start counting sleep time
sleep_time += sleep;
if (sleep_time > delay)
{
exit = 1;
skip = false;
}
}
else
{
sleep_time = 0; //
}
// someone is tx'ing
usleep(sleep);
} while ( !exit ) ;
nonblock(NB_DISABLE);
// restart scanning
*current_freq+=g_ban_tollerance;
// round up to next near tenth of khz 145892125 -> 145900000
*current_freq = ceil( *current_freq / (double)opt_scan_bw ) * opt_scan_bw;
#ifndef OSX
__fpurge(stdin);
#else
fpurge(stdin);
#endif
return skip;
}
//
// Open
//
FILE * Open (const char * filename)
{
FILE * filefd;
const char *homedir;
char filename2[PATH_MAX];
if (filename[0] == '~')
{
struct passwd *pw = getpwuid(getuid());
homedir = pw->pw_dir;
sprintf(filename2, "%s%s", homedir, filename+1);
}
else
sprintf(filename2, "%s", filename);
filefd = fopen (filename2, "r");
if (filefd == (FILE *)NULL)
error("ERROR opening gqrx bookmarks file");
return filefd;
}
bool prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
//
// LoadFrequencies from gqrx file format
//
bool LoadFrequencies (FILE *bookmarksfd)
{
char buf[BUFSIZE];
char *line;
bool start = false;
char *freq, *other;
int i = 0;
while (1)
{
line = fgets(buf, BUFSIZE, bookmarksfd );
if (line == (char *) NULL)
break;
if (prefix("# Frequency ;", line))
{
start = true;
continue;
}
if (start)
{
char * token = strtok(line, "; "); // freq
sscanf(token, "%llu", &Frequencies[i].freq);
token = strtok(NULL, ";"); // descr
strncpy(Frequencies[i].descr, token , BUFSIZE);
token = strtok(NULL, ";"); // mode
token = strtok(NULL, ";"); // bw
token = strtok(NULL, ";"); // tags, comma separated
char * tag = strtok(token,",\n");
int k = 0;
while (tag != NULL && k < TAG_MAX)
{
int len = strlen(tag) + 1 ;
Frequencies[i].tags[k] = calloc(sizeof(char), len);
// exclude initial spaces
int s;
for (s = 0; isspace(tag[s]) ; s++);
strncpy(Frequencies[i].tags[k], &tag[s], len);
k++;
tag = strtok (NULL, ",\n");
}
Frequencies[i].tag_max = k;
//printf(":%llu: %s\n", Frequencies[i].freq, Frequencies[i].descr);
i++;
}
}
Frequencies_Max = i;
return true;
}
//
// FilterFrequency
// Use specified tags (if any) to return the frequency matching the tag
// return 0 otherwise
freq_t FilterFrequency (int idx)
{
freq_t current_freq = Frequencies[idx].freq;
if (!opt_tag_search)
return current_freq;
bool found = false;
for (int i = 0; i < Frequencies[idx].tag_max ; i++)
{
char *tag = Frequencies[idx].tags[i]; // tag to search
for (int k = 0; k < opt_tag_max; k++)
{
if (strcasestr(tag , opt_tags[k]) != NULL) // ignore case
{
found = true;
break;
}
}
if (found)
break;
}
if (!found)
return (freq_t) 0;
return current_freq;
}
bool ScanBookmarkedFrequenciesInRange(int sockfd, freq_t freq_min, freq_t freq_max, double squelch_delta)
{
freq_t freq = 0;
GetCurrentFreq(sockfd, &freq);
double level = 0;
GetSignalLevel(sockfd, &level );
double squelch = 0;
double squelch_backup = 0;
GetSquelchLevel(sockfd, &squelch);
freq_t current_freq = freq_min;
bool skip = false;
long sleep_cycle_active = 500000; // skipping from active frequency need more time to wait squelch level to kick in
long sleep_cyle_saved = 85000 ; // skipping freqeuency need more time to get signal level
long slow_scan_cycle = 1000000; // LWVMOBILE: Just doubling numbers to slow down scan time in bookmark search, 1,000,000 = 1 second. EDIT: DOES THIS VARIABLE DO ANYTHING?
long slow_cycle_saved = 250000; // LWVMOBILE: Just doubling numbers to slow down scan time in bookmark search. THIS ONE SEEMS TO ACTUALLY SLOW SCAN SPEED DOWN.
char timestamp[BUFSIZE] = {0};
while (true)
{
CheckUserInput();
for (int i = 0; i < Frequencies_Max; i++)
{
if (Frequencies[i].noise_floor == 0)
Frequencies[i].noise_floor = level;
//printf("\rNoise floor: %2.2f ", Frequencies[i].noise_floor);
//fflush(stdout);
if ((current_freq = FilterFrequency(i)) == (freq_t) 0 )
continue;
if (IsBannedFreq(¤t_freq))
continue;
if ( ( ( current_freq >= freq_min) && // in the valid range
( current_freq < freq_max) ) ||
(freq_min == freq_max) ) // or using the entire frequencies
{
// Found a bookmark in the range
SetFreq(sockfd, current_freq);
GetSquelchLevel(sockfd, &squelch);
//usleep((skip)?sleep_cycle_active:sleep_cyle_saved); //LWVMOBILE: Perhaps place a small sleep here of 1000ms, slow scan to prevent 'slipping' issue in bookmark search.
//usleep((skip)?slow_scan_cycle:slow_cycle_saved); //LWVMOBILE: Find a way to implement these variables as a command line option -s 'slow scan' and input time in milli-seconds.
usleep((skip)?slow_scan_cycle:opt_speed); //LWVMOBILE: Using new variable set by default and also by user switch. Seems to work. GJ ME.
// LWVMOBILE: Scan stoppage due to no delay argument given has been fixed, was a variable set way too high.
GetSignalLevelEx(sockfd, &level, 5 );
if (level >= squelch)
{
time_t hit_time = GetTime(timestamp);
if (opt_squelch_delta_auto_enable)
{
squelch_backup = squelch;
SetSquelchLevel(sockfd, Frequencies[i].noise_floor + squelch_delta);
printf ("\n[%s] Freq: %s active [%s],\nLevel: %2.2f/%2.2f, Squelch set: %2.2f ",
timestamp, print_freq(current_freq),
Frequencies[i].descr, level, squelch, Frequencies[i].noise_floor + squelch_delta);
}
else
{
printf ("[%s] Freq: %s active [%s], Level: %2.2f/%2.2f ",
timestamp, print_freq(current_freq),
Frequencies[i].descr, level, squelch);