-
Notifications
You must be signed in to change notification settings - Fork 104
/
telive.c
2992 lines (2570 loc) · 70.6 KB
/
telive.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
/* telive v1.10 - tetra live monitor
* (c) 2014-2023 Jacek Lipkowski <sq5bpf@lipkowski.org>
* Licensed under GPLv3, please read the file LICENSE, which accompanies
* the telive program sources
*
* Changelog:
* v1.10 - show information about encryption, fix tickf scheduling under load --sq5bpf
* v1.9 - better text input, scanning support --sq5bpf
* v1.8 - handle osmo-tetra-sq5bpf protocol changes, use call identifiers for some stuff, various fixes --sq5bpf
* v1.7 - show country and network name --sq5bpf
* v1.6 - various fixes, add a star to the status line to show if there is network coverage, show afc info --sq5bpf
* v1.5 - added 'z' key to clear learned info, added play lock file --sq5bpf
* v1.4 - added frequency window --sq5bpf
* v1.3 - add frequency info --sq5bpf
* v1.2 - fixed various crashes, made timeouts configurable via env variables --sq5bpf
* v1.1 - made some buffers bigger, ignore location with INVALID_POSITION --sq5bpf
* v1.0 - add KML export --sq5bpf
* v0.9 - add TETRA_KEYS, add option to filter playback, fixed crash on problems playing, report when the network parameters change too quickly --sq5bpf
* v0.8 - code cleanups, add the verbose option, display for text sds in the status window --sq5bpf
* v0.7 - initial public release --sq5bpf
*
*
* TODO:
* ack! functions with no bounds checking, usage of popen, environment
* variables instead of getopt(), spaghetti code - basically this should
* be rewritten from scratch some day :)
*/
#include <fnmatch.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <ncurses.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <sys/file.h>
#include <fcntl.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <ctype.h>
#include "telive_util.h"
#include "telive.h"
#include "telive_receiver.h"
#define TELIVE_VERSION "1.10"
#define BUFLEN 8192
#define PORT 7379
/******* definitions *******/
int rec_timeout=30; /* after how long we stop to record a usage identifier */
int ssi_timeout=60; /* after how long we forget SSIs */
int idx_timeout=8; /* after how long we disable the active flag */
int curplaying_timeout=5; /* after how long we stop playing the current usage identifier */
int vbuf=1;
char *outdir;
char def_outdir[BUFLEN]="/tetra/in";
char *logfile;
char def_logfile[BUFLEN]="telive.log";
char *freqlogfile;
char def_freqlogfile[BUFLEN]="telive_frequency.log";
char *freqreportfile;
char def_freqreportfile[BUFLEN]="telive_frequency_report.txt";
char *ssifile;
char def_ssifile[BUFLEN]="ssi_descriptions";
char ssi_filter[BUFLEN];
char def_tetraxmlfile[BUFLEN]="tetra.xml";
char *tetraxmlfile;
int use_filter=0;
char *kml_file=NULL;
char *kml_tmp_file=NULL;
int kml_interval;
int last_kml_save=0;
int kml_changed=0;
int freq_changed=0;
char *lock_file=NULL;
int lockfd=0;
int locked=0;
int log_found_freq=1; /* do we log the frequencies we find during scanning etc */
/* stuff for handling the tetra networks list in xml */
xmlDocPtr tetraxml_doc;
char *tetraxml_country=NULL;
char *tetraxml_network=NULL;
uint16_t tetraxml_last_queried_country=0;
uint16_t tetraxml_last_queried_network=0;
/* various times for scheduling in tickf() */
time_t last_1s_event=0;
time_t last_10s_event=0;
time_t last_1min_event=0;
struct timeval current_timeval; /* cached current time, so that we don't call gettimeofday() and time() all the time */
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
int verbose=0;
WINDOW *msgwin=0; /* message window */
WINDOW *statuswin=0; /* status window */
WINDOW *titlewin=0; /* title window */
WINDOW *mainwin=0; /* main window */
WINDOW *freqwin=0; /* frequency window */
WINDOW *inputwin=0; /* interactive text input window */
WINDOW *ssiwin=0; /* SSI window */
WINDOW *locwin=0; /* Location window */
WINDOW *displayedwin=0; /* the window currently displayed */
int ref; /* window refresh flag, set to refresh all windows */
/* text input that doesn't block the rest of the program */
enum interactive_input {
INPUT_NONE=0,
INPUT_FILTER=1,
INPUT_FREQ=2,
INPUT_BASEBAND=3,
INPUT_PPM=4,
INPUT_GAIN=5,
};
int interactive_text_input=0;
char interactive_text_buf[200];
char prevtmsg[BUFLEN]; /* contents of previous message */
struct netinfo_s {
uint16_t mcc;
uint16_t mnc;
uint8_t colour_code;
uint32_t dl_freq;
uint32_t ul_freq;
uint16_t la;
int crypt;
time_t last_change;
uint32_t changes;
} netinfo;
struct netinfo_s netinfo;
struct netinfo_s netinfo_tmp;
/* this structure is used to describe all known frequencies */
/* struct freqinfo {
uint32_t dl_freq;
uint32_t ul_freq;
uint16_t mcc;
uint16_t mnc;
uint16_t la;
time_t last_change;
int reason;
int rx;
void *next;
void *prev;
};
*/
struct usi {
unsigned int ssi[3];
time_t ssi_time[3];
time_t ssi_time_rec;
int encr;
int timeout;
int active;
int play;
char curfile[BUFLEN];
char curfiletime[32];
uint16_t cid;
int lastrx;
};
struct opisy {
unsigned int ssi;
char *opis;
void *next;
void *prev;
};
struct locations {
unsigned int ssi;
float lattitude;
float longtitude;
char *description;
time_t lastseen;
void *next;
void *prev;
};
int telive_receiver_mode=TELIVE_RX_NORMAL;
int telive_auto_tune=0;
int freq_timeout=600;
int receiver_timeout=60;
/* scanning stuff */
#define SCANSTATE_NULL 0
#define SCANSTATE_GOTSIGNAL 1
#define SCANSTATE_GOTBURST 2
#define SCANSTATE_GOTSYSINFO 3
int scan_state=SCANSTATE_NULL;
int scan_timeout_signal=50; /* how much to wait for the receiver to settle */
int scan_timeout_burst=300; /* how much to wait for bursts */
int scan_timeout_sysinfo=2000; /* how much to wait for the first sysinfo */
int previous_freq_signal=0; /* we had something on the last frequency we scanned, this flag is used to wait more before scanning the next frequency */
#define SCAN_UP 1
#define SCAN_DOWN 0
int scan_direction=SCAN_UP;
uint32_t scan_step=12500;
struct timeval scan_last_tune;
struct timeval scan_last_burst;
uint32_t scan_low=0;
uint32_t scan_high=0;
/* this should cover most allocations, although this list should be shortened for greater scanning speed */
char def_scan_list[256]="390-395/12.5;420-430/12.5;870-876/12.5;450-470/12.5;915-921/12.5";
char *scan_list=NULL;
int scan_list_item=0;
/* end scanning stuff */
char *grxml_url=NULL;
char *grxml_rx_description;
int grxml_rx_channels=0;
#define RX_TUNE_NORMAL 0
#define RX_TUNE_FORCE_BASEBAND 1
/* reciver info */
#define RX_DEFINED 1
#define RX_ACTIVE 2
#define RX_MUTED 4
struct receiver {
unsigned int rxid;
int afc;
uint32_t freq;
time_t lastseen;
void *next;
void *prev;
time_t lastburst;
int state;
int encoption; /* is the encryption option enabled on in this network */
int seen_encryptions; /* bitmask of the encryption types we've seen so far */
};
uint32_t receiver_baseband_freq=0; /* the baseband frequency of the receiver */
uint32_t receiver_sample_rate=0; /* the sample rate of the receiver */
float receiver_ppm=0; /* frequency correction of the receiver */
int receiver_ppm_autocorrect=1; /* can autotune the ppm value */
int receiver_baseband_autocorrect=1; /* can change the baseband frequency so that more channels are covered */
int receiver_gain=0;
struct receiver *receivers=NULL;
struct opisy *opisssi;
struct locations *kml_locations=NULL;
struct freqinfo *frequencies=NULL;
struct freqinfo *freqdb=NULL; /* a database of all frequencies found (by scanning etc) */
int curplayingidx=0;
time_t curplayingtime=0;
int curplayingticks=0;
FILE *playingfp=NULL;
int mutessi=0;
int alldump=0;
int ps_record=0;
int ps_mute=0;
int do_log=0;
int last_burst=0;
char encryption_info[3][8] = {"", "CLEAR", "Crypt"};
enum telive_screen { DISPLAY_IDX, DISPLAY_FREQ, DISPLAY_END };
int display_state=DISPLAY_IDX;
void appendfile(char *file,char *msg) {
FILE *f;
f=fopen(file,"ab");
if (f) {
fprintf(f,"%s\n",msg);
fclose(f);
}
}
void appendlog(char *msg) {
appendfile(logfile,msg);
}
/* tetra.xml file handling */
void tetraxml_read() {
tetraxml_doc=xmlParseFile(tetraxmlfile);
if (tetraxml_doc==NULL) {
wprintw(statuswin,"File %s was not read succesfully\n",tetraxmlfile);
return;
}
}
void tetraxml_query(uint16_t mcc, uint16_t mnc,xmlDocPtr xml_doc)
{
char unn[]="Unknown network";
char unc[]="Unknown country";
xmlChar xpath[128];
xmlNodeSetPtr nodeset;
xmlChar *keyword;
xmlXPathContextPtr xml_context;
xmlXPathObjectPtr result;
if ((tetraxml_last_queried_country==mcc)&&(tetraxml_last_queried_network==mnc)) return;
if (tetraxml_country) free(tetraxml_country);
if (tetraxml_network) free(tetraxml_network);
tetraxml_last_queried_country=mcc;
tetraxml_last_queried_network=mnc;
xml_context = xmlXPathNewContext(tetraxml_doc);
if (xml_context==NULL) {
wprintw(statuswin,"Error in xmlXPathNewContext\n");
return;
}
if ((!xml_doc)||(!xml_context)) {
tetraxml_country=strdup((char *)&unc);
tetraxml_network=strdup((char *)&unn);
return;
}
sprintf((char *)&xpath,"//country[mcc='%i']/countryname",mcc);
result = xmlXPathEvalExpression((xmlChar *)&xpath, xml_context);
if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
xmlXPathFreeContext(xml_context);
xmlXPathFreeObject(result);
tetraxml_country=strdup((char *)&unc);
tetraxml_network=strdup((char *)&unn);
return;
} else {
nodeset = result->nodesetval;
keyword=xmlNodeListGetString(tetraxml_doc, nodeset->nodeTab[0]->xmlChildrenNode, 1);
tetraxml_country= strdup(keyword);
xmlFree(keyword); //should be called for other nodeTab[xxx]
}
sprintf((char *)&xpath,"//country[mcc='%i']/network[mnc='%i']/name",mcc,mnc);
result = xmlXPathEvalExpression((xmlChar *)&xpath, xml_context);
if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
xmlXPathFreeContext(xml_context);
xmlXPathFreeObject(result);
tetraxml_network=strdup((char *)&unn);
return;
} else {
nodeset = result->nodesetval;
keyword=xmlNodeListGetString(tetraxml_doc, nodeset->nodeTab[0]->xmlChildrenNode, 1);
tetraxml_network= strdup(keyword);
xmlFree(keyword); //should be called for other nodeTab[xxx]
}
xmlXPathFreeContext(xml_context);
xmlXPathFreeObject (result);
}
void clearopisy()
{
struct opisy *ptr,*ptr2;
ptr=opisssi;
while(ptr)
{
ptr2=ptr->next;
free((void *)ptr->opis);
free(ptr);
ptr=ptr2;
}
opisssi=0;
}
time_t ostopis=0;
int newopis()
{
struct stat st;
if (stat(ssifile,&st)) return(0);
if (st.st_mtime>ostopis) {
ostopis=st.st_mtime;
return(1);
}
return(0);
}
int initopis()
{
FILE *g;
char str[100];
struct opisy *ptr,*prevptr;
char *c;
prevptr=NULL;
clearopisy();
g=fopen(ssifile,"r");
if (!g) return(0);
str[sizeof(str)-1]=0;
while(!feof(g))
{
if (!fgets(str,sizeof(str)-1,g)) break;
c=strchr(str,' ');
if (c==NULL) continue;
*c=0;
c++;
ptr=malloc(sizeof(struct opisy));
if (prevptr) {
prevptr->next=ptr;
ptr->prev=prevptr;
ptr->next=NULL;
} else
{
opisssi=ptr;
}
ptr->ssi=atoi(str);
ptr->opis=strdup(c);
prevptr=ptr;
}
fclose(g);
return(1);
}
const char nop[2]="-\0";
char *lookupssi(int ssi)
{
struct opisy *ptr=opisssi;
while(ptr)
{
if (ptr->ssi==ssi) return(ptr->opis);
ptr=ptr->next;
}
return((char *)&nop);
}
void add_location(int ssi,float lattitude,float longtitude,char *description)
{
struct locations *ptr=kml_locations;
struct locations *prevptr=NULL;
char *c;
if (ptr) {
/* maybe we already know this ssi? */
while(ptr) {
if (ptr->ssi==ssi) break;
prevptr=ptr;
ptr=ptr->next;
}
}
if (!ptr) {
ptr=calloc(1,sizeof(struct locations));
if (!kml_locations) kml_locations=ptr;
if (prevptr) {
ptr->prev=prevptr;
prevptr->next=ptr;
}
ptr->ssi=ssi;
} else {
free(ptr->description);
}
ptr->lastseen=time(0);
ptr->lattitude=lattitude;
ptr->longtitude=longtitude;
ptr->description=strdup(description);
c=ptr->description;
/* ugly hack so that we don't get <> there, which would break the xml */
while(*c) { if (*c=='>') *c='G'; if (*c=='<') *c='L'; c++; }
kml_changed=1;
}
void dump_kml_file() {
FILE *f;
struct locations *ptr=kml_locations;
if (verbose>1) wprintw(statuswin,"called dump_kml_file()\n");
if (!kml_tmp_file) return;
f=fopen(kml_tmp_file,"w");
if (!f) return;
if (verbose>1) wprintw(statuswin,"dump_kml_file(%s)\n",kml_tmp_file);
fprintf(f,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n<Folder>\n<name>");
fprintf(f,"Telive MCC:%5i MNC:%5i ColourCode:%3i Down:%3.4fMHz Up:%3.4fMHz LA:%5i",netinfo.mcc,netinfo.mnc,netinfo.colour_code,netinfo.dl_freq/1000000.0,netinfo.ul_freq/1000000.0,netinfo.la);
fprintf(f,"</name>\n<open>1</open>\n");
while(ptr)
{
fprintf(f,"<Placemark> <name>%i</name> <description>%s %s</description> <Point> <coordinates>%f,%f,0</coordinates></Point></Placemark>\n",ptr->ssi,lookupssi(ptr->ssi),ptr->description,ptr->longtitude,ptr->lattitude);
ptr=ptr->next;
}
fprintf(f,"</Folder></kml>\n");
fclose(f);
rename(kml_tmp_file,kml_file);
last_kml_save=time(0);
kml_changed=0;
}
/* delete the whole location info */
void clear_locations() {
struct locations *ptr=kml_locations;
struct locations *nextptr;
while(ptr) {
nextptr=ptr->next;
free(ptr->description);
free(ptr);
ptr=nextptr;
}
kml_locations=NULL;
}
#define MAXUS 64
struct usi ssis[MAXUS];
void diep(char *s)
{
wprintw(statuswin,"DIE: %s\n",s);
wrefresh(statuswin);
sleep(1);
perror(s);
exit(1);
}
#define RR 13
int getr(int idx) {
return((idx%RR)*4);
}
int getcl(int idx)
{
return ((idx/RR)*40);
}
/* update usage identifier display */
void updidx(int idx) {
char opis[40];
char opis2[11];
int i;
int row=getr(idx);
int col=getcl(idx);
int bold=0;
if ((idx<0)||(idx>=MAXUS)) { wprintw(statuswin,"BUG! updidx(%i)\n",idx); wrefresh(statuswin); return; }
opis[0]=0;
opis2[0]=0;
wmove(mainwin,row,col+5);
if (ssis[idx].active) strcat(opis,"OK ");
// if (ssis[idx].timeout) strcat(opis,"timeout ");
if (ssis[idx].encr) strcat(opis,"ENCR ");
if ((ssis[idx].play)&&(ssis[idx].active)) { bold=1; strcat(opis,"*PLAY* "); }
if (ssis[idx].cid) { sprintf(opis2,"%i [%i]",ssis[idx].cid,ssis[idx].lastrx); }
if (bold) wattron(mainwin,A_BOLD|COLOR_PAIR(1));
wprintw(mainwin,"%-20s %10s",opis,opis2);
if (bold) wattroff(mainwin,COLOR_PAIR(1));
for (i=0;i<3;i++) {
wmove(mainwin,row+i+1,col+5);
if (ssis[idx].ssi[i]) {
wprintw(mainwin,"%8i %20s",ssis[idx].ssi[i],lookupssi(ssis[idx].ssi[i]));
}
else
{
wprintw(mainwin," ");
}
}
if (bold) wattroff(mainwin,A_BOLD);
ref=1;
}
void draw_idx() {
int idx;
for (idx=0;idx<MAXUS;idx++)
{
wmove(mainwin,getr(idx),getcl(idx));
wprintw(mainwin,"%2i:",idx);
}
}
/* refresh the main window */
void display_mainwin() {
int idx;
wclear(mainwin);
draw_idx();
for (idx=0;idx<MAXUS;idx++) updidx(idx);
ref=1;
}
#define STATUSLINES 7
int initcur() {
int maxx,maxy;
initscr();
start_color();
cbreak();
init_pair(1, COLOR_RED, COLOR_BLUE);
init_pair(2, COLOR_YELLOW, COLOR_BLUE);
init_pair(3, COLOR_RED, COLOR_GREEN);
init_pair(4, COLOR_WHITE, COLOR_GREEN);
init_pair(5, COLOR_BLACK, COLOR_YELLOW);
init_pair(6, COLOR_WHITE, COLOR_RED);
clear();
inputwin=newwin(3,COLS,LINES/2,0);
scrollok(inputwin,TRUE);
wattron(inputwin,COLOR_PAIR(6));
wbkgdset(inputwin,COLOR_PAIR(6));
wclear(inputwin);
titlewin=newwin(1,COLS,0,0);
mainwin=newwin(LINES-STATUSLINES-1,COLS,1,0);
freqwin=newwin(LINES-STATUSLINES-1,COLS,1,0);
locwin=newwin(LINES-STATUSLINES-1,COLS,1,0);
ssiwin=newwin(LINES-STATUSLINES-1,COLS,1,0);
displayedwin=mainwin;
msgwin=newwin(STATUSLINES,COLS/2,LINES-STATUSLINES,0);
wattron(msgwin,COLOR_PAIR(2));
wbkgdset(msgwin,COLOR_PAIR(2));
wclear(msgwin);
wprintw(msgwin,"Message window\n");
scrollok(msgwin,TRUE);
statuswin=newwin(STATUSLINES,COLS/2,LINES-STATUSLINES,COLS/2+1);
wattron(statuswin,COLOR_PAIR(3));
wbkgdset(statuswin,COLOR_PAIR(3));
wclear(statuswin);
wprintw(statuswin,"#### Press ? for keystroke help ####\n");
getmaxyx(stdscr,maxy,maxx);
if ((maxx!=203)||(maxy!=60)) {
wprintw(statuswin,"\nWARNING: The terminal size is %ix%i, but it should be 203x60\nThe program will still work, but the display may be mangled\n\n",maxx,maxy);
}
scrollok(statuswin,TRUE);
wattron(titlewin,A_BOLD);
wprintw(titlewin,"** SQ5BPF TETRA Monitor %s **",TELIVE_VERSION);
wattroff(titlewin,A_BOLD);
draw_idx();
ref=1;
return(1);
}
void updopis()
{
wmove(titlewin,0,32);
wattron(titlewin,COLOR_PAIR(4)|A_BOLD);
wprintw(titlewin,"MCC:%5i MNC:%5i ColourCode:%3i Down:%3.4fMHz Up:%3.4fMHz LA:%5i %c",netinfo.mcc,netinfo.mnc,netinfo.colour_code,netinfo.dl_freq/1000000.0,netinfo.ul_freq/1000000.0,netinfo.la,last_burst?'*':' ');
wattroff(titlewin,COLOR_PAIR(4)|A_BOLD);
wprintw(titlewin," mutessi:%i alldump:%i mute:%i record:%i log:%i verbose:%i lock:%i",mutessi,alldump,ps_mute,ps_record,do_log,verbose,locked);
switch(use_filter)
{
case 0: wprintw(titlewin," no filter"); break;
case 1: wprintw(titlewin," Filter ON"); break;
case -1: wprintw(titlewin," Inv. Filt"); break;
}
wprintw(titlewin," [%s] ",ssi_filter);
wclrtobot(titlewin);
ref=1;
}
int addssi(int idx,int ssi)
{
int i;
if (!ssi) return(0);
if ((idx<0)||(idx>=MAXUS)) { wprintw(statuswin,"BUG! addssi(%i,%i)\n",idx,ssi); wrefresh(statuswin); return(0); }
for(i=0;i<3;i++) {
if (ssis[idx].ssi[i]==ssi) {
ssis[idx].ssi_time[i]=time(0);
return(1);
}
}
for(i=0;i<3;i++) {
if (!ssis[idx].ssi[i]) {
ssis[idx].ssi[i]=ssi;
ssis[idx].ssi_time[i]=time(0);
return(1);
}
}
/* no room to add, forget one ssi */
ssis[idx].ssi[0]=ssis[idx].ssi[1];
ssis[idx].ssi[1]=ssis[idx].ssi[2];
ssis[idx].ssi[2]=ssi;
ssis[idx].ssi_time[2]=time(0);
ssis[idx].active=1;
return(1);
}
int addssi2(int idx,int ssi,int i)
{
if (!ssi) return(0);
ssis[idx].ssi[i]=ssi;
ssis[idx].ssi_time[i]=time(0);
ssis[idx].active=1;
return(0);
}
/* add an ssi to a call identified by a call identifier */
int addcallssi(int cid,int ssi) {
int i;
if (!cid) return(0);
for (i=1;i<MAXUS;i++) {
if (ssis[i].cid==cid) {
addssi(i,ssi);
return(i);
}
}
return(0);
}
int addcallident(int idx,uint16_t cid,int rxid)
{
if (!idx) return(0);
if (!cid) return(0);
if ((verbose)&&(ssis[idx].cid)&&(ssis[idx].cid!=cid)) {
wprintw(statuswin,"Overlapping CIDs for IDX:%i. Previous: %i (RX:%i) Current: %i (RX:%i)\n",idx,ssis[idx].cid,ssis[idx].lastrx,cid,rxid);
ref=1;
}
ssis[idx].cid=cid;
ssis[idx].lastrx=rxid;
}
int releasessi(int ssi)
{
int i,j;
for (i=0;i<MAXUS;i++) {
for (j=0;j<3;j++) {
if ((ssis[i].active)&&(ssis[i].ssi[j]==ssi)) {
ssis[i].active=0;
ssis[i].play=0;
ssis[i].lastrx=0;
ssis[i].cid=0;
updidx(i);
ref=1;
}
}
}
return(0);
}
/* check if an SSI matches the filter expression */
int matchssi(int ssi)
{
int r;
char ssistr[16];
if (!ssi) return(0);
sprintf(ssistr,"%i",ssi);
if (strlen(ssi_filter)==0) return(1);
#ifdef FNM_EXTMATCH
r=fnmatch((char *)&ssi_filter,(char *)&ssistr,FNM_EXTMATCH);
#else
/* FNM_EXTMATCH is a GNU libc extension, not present in other libcs, like the MacOS X one */
#warning ----------- Extended match patterns for fnmatch are not supported by your libc. You will have to live with that. ------------
r=fnmatch((char *)&ssi_filter,(char *)&ssistr,0);
#endif
return(!r);
}
/* check if any SSIs for this usage identifier match the filter expression */
int matchidx(int idx)
{
int i;
int j=0;
if (!use_filter) return (1);
for(i=0;i<3;i++) {
if (matchssi(ssis[idx].ssi[i])) {
j=1;
break;
}
}
if (use_filter==-1) j=!j;
return(j);
}
/* locking functions */
int trylock() {
int i;
if (!lockfd) return(1);
i=flock(lockfd,LOCK_EX|LOCK_NB);
if (!i) { locked=1; updopis(); return(1); }
return(0);
}
void releaselock() {
int i=locked;
locked=0;
if (i) updopis();
if (!lockfd) return;
flock(lockfd,LOCK_UN);
}
/* receiver table functions */
//void update_receivers(int rx,int afc,uint32_t freq)
struct receiver *update_receivers(int rx)
{
struct receiver *ptr=receivers;
struct receiver *prevptr;
/* do we know this rx? */
while(ptr)
{
if (ptr->rxid==rx) break;
prevptr=ptr;
ptr=ptr->next;
}
/* nope, new one */
if (!ptr) {
ptr=calloc(1,sizeof(struct receiver));
if (!receivers) {
receivers=ptr;
} else {
prevptr->next=ptr;
ptr->prev=prevptr;
}
ptr->rxid=rx;
}
/* ptr->afc=afc;
if (freq) ptr->freq=freq;
*/
ptr->lastseen=time(0);
return(ptr);
}
/* time out old receivers */
void timeout_receivers() {
struct receiver *ptr=receivers;
struct receiver *prevptr,*nextptr;
if (!ptr) return;
time_t timenow=time(0);
while(ptr) {
if ((!(ptr->state&RX_DEFINED))&&((timenow-ptr->lastseen)>receiver_timeout))
{
prevptr=ptr->prev;
nextptr=ptr->next;
free(ptr);
if (prevptr) {
prevptr->next=nextptr;
if (nextptr) nextptr->prev=prevptr;
} else {
receivers=nextptr;
if (nextptr) nextptr->prev=NULL;
}
}
ptr=ptr->next;
}
}
/* clear all known receivers */
void clear_all_receivers() {
struct receiver *ptr=receivers;
struct receiver *ptr2;
while(ptr) {
ptr2=ptr;
ptr=ptr->next;
free(ptr2);
}
receivers=0;
}
/* find pointer for receiver */
struct receiver *find_receiver(int rx)
{
struct receiver *ptr=receivers;
while(ptr)
{
if (ptr->rxid==rx) return(ptr);
ptr=ptr->next;
}
return(NULL);
}
/* update receiver afc */
void update_receiver_afc(int rx,int afc) {
struct receiver *ptr;
ptr=find_receiver(rx);
if (ptr) ptr->afc=afc;
}
/* update receiver encryption info */
void update_receiver_encinfo(int rx,int encoption,int seen_encryptions) {
struct receiver *ptr;
ptr=find_receiver(rx);
if (ptr)
{
ptr->encoption=encoption;
ptr->seen_encryptions=seen_encryptions;
}
}
void update_receiver_freq(int rx,uint32_t freq) {
struct receiver *ptr;
if (!freq) return;
ptr=find_receiver(rx);
if (ptr) {
ptr->freq=freq;
/* frequency has changed, unmute it */
ptr->state=ptr->state&~RX_MUTED;
}
}
void update_receiver_lastburst(int rx) {
struct receiver *ptr;
ptr=find_receiver(rx);
if (ptr) ptr->lastburst=current_timeval.tv_sec;
}
/* frequency table functions */
#define REASON_NETINFO 1<<0
#define REASON_FREQINFO 1<<1
#define REASON_DLFREQ 1<<2
#define REASON_SCANNED 1<<3
void dump_freqdb()
{
char buf[256];
char buf2[64];
struct freqinfo *ptr;
struct freqinfo *ptr2;
FILE *f;
int lastmnc;
uint32_t tmpmnc=0;
uint32_t tmpmcc=0;
int i;
f=fopen(freqreportfile,"w+");
if (!f) return;
strftime(buf,sizeof(buf)-1,"Telive " TELIVE_VERSION " frequency report %Y%m%d %H:%M:%S\n\n",localtime(¤t_timeval.tv_sec));
fputs(buf,f);
/* sort by MNC, the list isn't that big, so we just use the dumbest algorithm which traverses the list multiple times */
lastmnc=0;
while(1) {
i=0;
tmpmnc=0xffff;
ptr=freqdb;
while(ptr) {
if (ptr->mnc>lastmnc) {
if (tmpmnc>ptr->mnc) { tmpmcc=ptr->mcc; tmpmnc=ptr->mnc; i=1; }
}
ptr=ptr->next;
}
if (!i) break;
lastmnc=tmpmnc;
tetraxml_query(tmpmcc,tmpmnc,tetraxml_doc);
sprintf(buf,"######## MCC %i (%s) MNC %i (%s) ########\n",tmpmcc,tetraxml_country,tmpmnc,tetraxml_network); fputs(buf,f);
ptr2=freqdb;
while(ptr2)
{