This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edacs-fm.c
1609 lines (1433 loc) · 59.7 KB
/
edacs-fm.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
/*-------------------------------------------------------------------------------
* EDACS-FM Decoder
* A program for decoding EDACS
*
* ASCII art generated by:
* https://fsymbols.com/generators/carty/
*
* Portions of this software originally from:
* https://github.com/sp5wwp/ledacs
* XTAL Labs
* 30 IV 2016
* Many thanks to SP5WWP for permission to use and modify this software
*
* Encoder/decoder for binary BCH codes in C (Version 3.1)
* Robert Morelos-Zaragoza
* 1994-7
*
* LWVMOBILE
* 2022-04 Version EDACS-FM Florida Man Edition
*-----------------------------------------------------------------------------*/
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <ncurses.h>
#include <stdio_ext.h>
#include <math.h>
#include <netdb.h>
#include <sys/stat.h>
#include <pwd.h>
#include <termios.h>
#include <getopt.h>
#include <ctype.h>
#include <stdbool.h>
#include <locale.h>
#include "bch3.h" //BCH support
#define BSIZE 999
#define UDP_BUFLEN 5 //maximum UDP buffer length
#define SRV_IP "127.0.0.1" //IP
#define UDP_PORT 6020 //UDP port
#define SAMP_NUM (48 + 6 * 40) * 2 * 3 //EDACS96 288-bit cycle
#define SYNC_FRAME 0x555557125555 << (64 - 48) //EDACS96 synchronization frame (12*4=48bit)
#define SYNC_MASK 0xFFFFFFFFFFFF << (64 - 48) //EDACS96 synchronization frame mask
unsigned long long sync_timeout = 3; //making sync_timeout a user definable variable now, default 3 seconds
unsigned char samples[SAMP_NUM]; //8-bit samples from rtl_fm (or rtl_udp)
signed short int raw_stream[SAMP_NUM / 2]; //16-bit signed int samples
signed int AFC = 2400; //Auto Frequency Control -> DC offset
signed int min = SHRT_MAX, max = SHRT_MIN; //min and max sample values
signed short int avg_arr[SAMP_NUM / 2 / 3]; //array containing 16-bit samples
unsigned int avg_cnt = 0; //avg array index variable
unsigned long long sr_0 = 0; //64-bit shift registers for pushing decoded binary data
unsigned long long sr_1 = 0; //288/64=4.5
unsigned long long sr_2 = 0; //
unsigned long long sr_3 = 0; //
unsigned long long sr_4 = 0; //
unsigned long long fr_1 = 0xFFFFFFFFFF; //40-bit shift registers for pushing decoded binary data
//unsigned long long fr_2 = 0; //each is a 40 bit message that repeats 3 times
//unsigned long long fr_3 = 0; //two messages per frame
unsigned long long fr_4 = 0xFFFFFFFFFF; //These are the human readable versions for debugging etc
//unsigned long long fr_5 = 0;
//unsigned long long fr_6 = 0;
//BCH stuff
long long int fr_1m = 0xFFFFFFF; //28-bit 7X message portion to pass to bch handler
long long int fr_1t = 0xFFFFFFFFFF; //40 bit return from BCH with poly attached
long long int fr_4m = 0xFFFFFFF; //28-bit 7X message portion to pass to bch handler
long long int fr_4t = 0xFFFFFFFFFF; //40 bit return from BCH with poly attached
double good = 1;
double bad = 1; //don't set as 0 so we won't accidentally divide by 0 and blow up the universe
double gbr = 1;
//end BCH stuff
unsigned short a_len = 4; //AFS allocation type
unsigned short f_len = 4; //bit lengths
unsigned short s_len = 3; //AFS bits default to 443 scheme by default if not specified
unsigned short a_mask = 0x0; //and corresponding masks, set to 0 by default or they will accumulate when shifting
unsigned short f_mask = 0x0; //
unsigned short s_mask = 0x0; //
unsigned short x_mask = 0x0;
unsigned short x_choice = 0;
unsigned long long afs = 0; //AFS 11-bit info
unsigned long long int patch_site = 0;
unsigned long long int site_id = 0;
unsigned long long int start_site_id = 0;
unsigned long long int tempsite_id = 999;
unsigned long long int tgroupx = 999;
signed long long int senderx = 0;
unsigned long long int groupx = 0;
signed long long int sourcep = 0;
signed long long int targetp = 0;
signed long long int patch_array[51][2];
signed long long kicked = 0;
signed long long int tsenderx = 0;
unsigned long long tafs = 999;
//call_matrix 32 columns (lcns) with rows for time of call, group/afs, sender, status and logged bit
unsigned long long int call_matrix[33][5]; //bump to 5 for logged bit
//group_matrix for groupname and mode
char * group_matrix[33][2];
char * mode_a;
char * mode_b;
int modecompare;
char * mode;
char * groupx_name;
char * site_name;
char * location_name;
unsigned char mt1 = 0x1F;
unsigned char mt2 = 0xF;
unsigned char mta = 0;
int adjust = 0; //next on the chopping block
int cyclecc = 0;
signed int ppm = 0;
unsigned int vcmd = 0xEE; //voice command variable set by argument
unsigned int idcmd = 0xFD;
unsigned int peercmd = 0xF88; //using for EA detection test
unsigned int netcmd = 0xF3; //using for Networked Test
unsigned char command = 0; //read from control channel
unsigned char lcn = 0;
unsigned char lcn_tally = 0;
unsigned char status = 0; //
unsigned char agency = 4, fleet = 4, subfleet = 3; // going to initialize with 4-4-3 scheme, most universal
short int active = 0; //0 for inactive/ready, 1 for active/busy
char * sitecsv;
char * groupcsv;
int rfgain = 0;
unsigned long long int last_sync_time = 0; //last received sync timestamp
unsigned char current_lcn = 0; //current LCN
unsigned long long int LCN_list[32]; //LCN list
unsigned char cc = 1;
unsigned char deny_num = 0;
unsigned int deny_total = 0;
signed int deny_flag = 0;
short int udeny = 0;
short int allow = 0;
unsigned long long int CC_LCN = 0;
unsigned long long int hanguptime = 0;
short int hangup = 0;
unsigned long long int resettime = 0;
unsigned long long int logtime = 0;
char * FM_banner[9] = {
" ",
" ███████╗██████╗ █████╗ █████╗ ██████╗ ███████╗███╗ ███╗ █████╗",
" ██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝ ██╔════╝████╗ ████║ ██╔══██╗",
" █████╗ ██║ ██║███████║██║ ╚═╝╚█████╗ ███ █████╗ ██╔████╔██║█████║ ╚═╝",
" ██╔══╝ ██║ ██║██╔══██║██║ ██╗ ╚═══██╗ ██╔══╝ ██║╚██╔╝██║═══██║ ██╗",
" ███████╗██████╔╝██║ ██║╚█████╔╝██████╔╝ ██║ ██║ ╚═╝ ██║ ╚█████╔╝",
" ╚══════╝╚═════╝ ╚═╝ ╚═╝ ╚════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚════╝",
};
signed int peer_counter = 0;
signed long long int peer_list[12]; //make 2d array to store control lcn for peer
signed long long int peer = 0;
unsigned long long int peer_lcn = 0;
//CLI options for Site Extra, Call Matrix, Patches, and Logging
short int S = 0; //Display Site Extra in printw area
short int C = 0; //Display Call Matrix in printw area
short int P = 0; //Display Patches in printw area
short int Q = 0; //Enable logging of peers and patches
short int L = 0; //Enable voice call logging
short int A = 0; //Enable Autodetection for EDACS Type
signed int debug = 0; //debug value for printing out status and data on different command codes, etc
int handle; //for UDP
unsigned short port = UDP_PORT;
char data[UDP_BUFLEN] = {
0
};
struct sockaddr_in address;
//--------------------------------------------
typedef struct key_value {
char siteN[20];
char location[20];
char lcn0N[50];
char lcn1N[50];
char lcn2N[50];
char lcn3N[50];
char lcn4N[50];
char lcn5N[50];
char lcn6N[50];
char lcn7N[50];
char lcn8N[50];
char lcn9N[50];
char lcn10N[50];
char lcn11N[50];
char lcn12N[50];
char lcn13N[50];
char lcn14N[50];
char lcn15N[50];
char lcn16N[50];
char lcn17N[50];
char lcn18N[50];
char lcn19N[50];
char lcn20N[50];
char lcn21N[50];
char lcn22N[50];
char lcn23N[50];
char lcn24N[50];
char lcn25N[50];
char lcn26N[50];
char lcn27N[50];
char lcn28N[50];
char lcn29N[50];
char lcn30N[50];
char lcn31N[50];
}
dict;
void loadLCN(int tsite_id, dict site_array[]) //load LCN frequencies from csv importer/struct
{
LCN_list[0] = atoi(site_array[tsite_id].lcn0N);
LCN_list[1] = atoi(site_array[tsite_id].lcn1N);
LCN_list[2] = atoi(site_array[tsite_id].lcn2N);
LCN_list[3] = atoi(site_array[tsite_id].lcn3N);
LCN_list[4] = atoi(site_array[tsite_id].lcn4N);
LCN_list[5] = atoi(site_array[tsite_id].lcn5N);
LCN_list[6] = atoi(site_array[tsite_id].lcn6N);
LCN_list[7] = atoi(site_array[tsite_id].lcn7N);
LCN_list[8] = atoi(site_array[tsite_id].lcn8N);
LCN_list[9] = atoi(site_array[tsite_id].lcn9N);
LCN_list[10] = atoi(site_array[tsite_id].lcn10N);
LCN_list[11] = atoi(site_array[tsite_id].lcn11N);
LCN_list[12] = atoi(site_array[tsite_id].lcn12N);
LCN_list[13] = atoi(site_array[tsite_id].lcn13N);
LCN_list[14] = atoi(site_array[tsite_id].lcn14N);
LCN_list[15] = atoi(site_array[tsite_id].lcn15N);
LCN_list[16] = atoi(site_array[tsite_id].lcn16N);
LCN_list[17] = atoi(site_array[tsite_id].lcn17N);
LCN_list[18] = atoi(site_array[tsite_id].lcn18N);
LCN_list[19] = atoi(site_array[tsite_id].lcn19N);
LCN_list[20] = atoi(site_array[tsite_id].lcn20N);
LCN_list[21] = atoi(site_array[tsite_id].lcn21N);
LCN_list[22] = atoi(site_array[tsite_id].lcn22N);
LCN_list[23] = atoi(site_array[tsite_id].lcn23N);
LCN_list[24] = atoi(site_array[tsite_id].lcn24N);
LCN_list[25] = atoi(site_array[tsite_id].lcn25N);
LCN_list[26] = atoi(site_array[tsite_id].lcn26N);
LCN_list[27] = atoi(site_array[tsite_id].lcn27N);
LCN_list[28] = atoi(site_array[tsite_id].lcn28N);
LCN_list[29] = atoi(site_array[tsite_id].lcn29N);
LCN_list[30] = atoi(site_array[tsite_id].lcn30N);
LCN_list[31] = atoi(site_array[tsite_id].lcn31N);
site_name = site_array[tsite_id].siteN;
location_name = site_array[tsite_id].location;
}
int csvImport() {
char filename[] = "site.csv";
strcpy(filename, sitecsv);
char buffer[BSIZE];
FILE * fp;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Unable to open file '%s'\n", filename);
exit(1);
}
int row_count = 0;
int field_count = 0;
dict site_array[9999]; //array to struct to store values
int tsite_id = site_id;
while (fgets(buffer, BSIZE, fp)) {
field_count = 0;
row_count++;
if (row_count == 1)
continue; //don't want labels
char * field = strtok(buffer, ","); //seperate by comma
int i = atoi(field); //field is seeing site id, make dict based on i value for site id, then cross reference later on for LCN
while (field) {
if (field_count == 0)
strcpy(site_array[i].siteN, field);
if (field_count == 1)
strcpy(site_array[i].location, field);
if (field_count == 2)
strcpy(site_array[i].lcn0N, field);
if (field_count == 3)
strcpy(site_array[i].lcn1N, field);
if (field_count == 4)
strcpy(site_array[i].lcn2N, field);
if (field_count == 5)
strcpy(site_array[i].lcn3N, field);
if (field_count == 6)
strcpy(site_array[i].lcn4N, field);
if (field_count == 7)
strcpy(site_array[i].lcn5N, field);
if (field_count == 8)
strcpy(site_array[i].lcn6N, field);
if (field_count == 9)
strcpy(site_array[i].lcn7N, field);
if (field_count == 10)
strcpy(site_array[i].lcn8N, field);
if (field_count == 11)
strcpy(site_array[i].lcn9N, field);
if (field_count == 12)
strcpy(site_array[i].lcn10N, field);
if (field_count == 13)
strcpy(site_array[i].lcn11N, field);
if (field_count == 14)
strcpy(site_array[i].lcn12N, field);
if (field_count == 15)
strcpy(site_array[i].lcn13N, field);
if (field_count == 16)
strcpy(site_array[i].lcn14N, field);
if (field_count == 17)
strcpy(site_array[i].lcn15N, field);
if (field_count == 18)
strcpy(site_array[i].lcn16N, field);
if (field_count == 19)
strcpy(site_array[i].lcn17N, field);
if (field_count == 20)
strcpy(site_array[i].lcn18N, field);
if (field_count == 21)
strcpy(site_array[i].lcn19N, field);
if (field_count == 22)
strcpy(site_array[i].lcn20N, field);
if (field_count == 23)
strcpy(site_array[i].lcn21N, field);
if (field_count == 24)
strcpy(site_array[i].lcn22N, field);
if (field_count == 25)
strcpy(site_array[i].lcn23N, field);
if (field_count == 26)
strcpy(site_array[i].lcn24N, field);
if (field_count == 27)
strcpy(site_array[i].lcn25N, field);
if (field_count == 28)
strcpy(site_array[i].lcn26N, field);
if (field_count == 29)
strcpy(site_array[i].lcn27N, field);
if (field_count == 30)
strcpy(site_array[i].lcn28N, field);
if (field_count == 31)
strcpy(site_array[i].lcn29N, field);
if (field_count == 32)
strcpy(site_array[i].lcn30N, field);
if (field_count == 33)
strcpy(site_array[i].lcn31N, field);
field = strtok(NULL, ",");
field_count++;
}
}
fclose(fp);
loadLCN(tsite_id, site_array);
return 0;
}
typedef struct key_v {
signed long long int groupNumber;
char groupMode[8];
char groupName[50];
}
groupinfo;
void loadGroupMatrix(unsigned char tlcn, signed long long int tgroup_id, groupinfo group_array[]) {
group_matrix[tlcn][0] = group_array[tgroup_id].groupName;
group_matrix[tlcn][1] = group_array[tgroup_id].groupMode;
groupx_name = group_array[tgroup_id].groupName;
mode = group_array[tgroup_id].groupMode;
}
signed long long int csvGroupImport() {
char filename[] = "group.csv";
strcpy(filename, groupcsv);
char buffer[BSIZE];
FILE * fp;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Unable to open file '%s'\n", filename);
exit(1);
}
long long int row_count = 0;
int field_count = 0;
groupinfo group_array[67000]; //array to struct to store values
signed long long int tgroup_id2 = groupx;
unsigned char tlcn2 = lcn;
while (fgets(buffer, BSIZE, fp)) {
field_count = 0;
row_count++;
if (row_count == 1)
continue; //don't want labels
char * field = strtok(buffer, ","); //seperate by comma
signed long long int i = atoi(field); //field is seeing site id, make dict based on i value for site id, then cross reference later on for LCN
while (field) {
if (field_count == 0)
group_array[i].groupNumber = atoi(field);
if (field_count == 1)
strcpy(group_array[i].groupMode, field);
if (field_count == 2)
strcpy(group_array[i].groupName, field);
field = strtok(NULL, ",");
field_count++;
}
}
fclose(fp);
loadGroupMatrix(tlcn2, tgroup_id2, group_array);
return 0;
}
int init_udp() //UDP init
{
handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (handle <= 0) {
printf("Failed to create socket\n");
return 1;
}
printf("Sockets successfully initialized\n");
memset((char * ) & address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(SRV_IP); //address of host
address.sin_port = htons(port);
return 0;
}
char * getTime(void) //get pretty hh:mm:ss timestamp
{
time_t t = time(NULL);
char * curr;
char * stamp = asctime(localtime( & t));
curr = strtok(stamp, " ");
curr = strtok(NULL, " ");
curr = strtok(NULL, " ");
curr = strtok(NULL, " ");
return curr;
}
char * getDate(void) {
char datename[32];
char * curr2;
struct tm * to;
time_t t;
t = time(NULL);
to = localtime( & t);
strftime(datename, sizeof(datename), "%Y-%m-%d", to);
curr2 = strtok(datename, " ");
return curr2;
}
//--------------------------------------------
void tune(unsigned long long int freq) //tuning to LCN freq
{
data[0] = 0;
data[1] = freq & 0xFF;
data[2] = (freq >> 8) & 0xFF;
data[3] = (freq >> 16) & 0xFF;
data[4] = (freq >> 24) & 0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr * ) & address, sizeof(struct sockaddr_in));
}
void tuneCC(unsigned long long int ccfreq) //tuning to CC freq, only works with PyEDACS tuner, not rtl_udp
{
data[0] = 9;
data[1] = ccfreq & 0xFF;
data[2] = (ccfreq >> 8) & 0xFF;
data[3] = (ccfreq >> 16) & 0xFF;
data[4] = (ccfreq >> 24) & 0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr * ) & address, sizeof(struct sockaddr_in));
}
//rework ppmAdjust just for initial ppm correction, and not for self adjustments, change argument to required and send data to pyEdacs, see gainSet
void ppmAdjust(unsigned long long int ccppm) //adjustments to CC PPM, only works with PyEDACS tuner, not rtl_fm/udp
{
data[0] = 7;
data[1] = ccppm & 0xFF;
data[2] = (ccppm >> 8) & 0xFF;
data[3] = (ccppm >> 16) & 0xFF;
data[4] = (ccppm >> 24) & 0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr * ) & address, sizeof(struct sockaddr_in));
}
void squelchSet(unsigned long long int sq) //squelch
{
data[0] = 2;
data[1] = sq & 0xFF;
data[2] = (sq >> 8) & 0xFF;
data[3] = (sq >> 16) & 0xFF;
data[4] = (sq >> 24) & 0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr * ) & address, sizeof(struct sockaddr_in));
}
void gainSet(int gain) //adjustments to RF Gain in PyEDACS tuner, may also function in rtl_udp
{
data[0] = 3; // 3 on rtl_udp is for new_gain, so maybe works with both, unsure yet, 8 is AGC mode
data[1] = gain & 0xFF;
data[2] = (gain >> 8) & 0xFF;
data[3] = (gain >> 16) & 0xFF;
data[4] = (gain >> 24) & 0xFF;
sendto(handle, data, UDP_BUFLEN, 0, (const struct sockaddr * ) & address, sizeof(struct sockaddr_in));
}
void FM() //Print a swanky ascii art banner
{
for (short int i = 0; i < 7; i++) {
printf("%s \n", FM_banner[i]);
}
}
//need to work this back into future release
void print_usage(char * name) {
printf("Usage:\n");
exit(EXIT_FAILURE);
}
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. */
{"Sync Timeout", required_argument,0,'t'},
{"Universal Denial", no_argument,0,'d'},
{"verbose", no_argument,0,'v'},
{"legacy", no_argument,0,'l'},
{"esk", no_argument,0,'e'},
{"ea", no_argument,0,'E'},
{"esk-ea", no_argument,0,'x'},
{"afs A bit", required_argument,0,'a'},
{"afs F bit", required_argument,0,'f'},
{"siteinfo", required_argument,0,'s'},
{"sitenumber", required_argument,0,'c'},
{"group", required_argument,0,'g'},
{"RF gain", required_argument,0,'r'},
{"ppm-auto-adjustments", no_argument,0,'p'},
{"Site Extra", no_argument,0,'S'},
{"Call Matrix", no_argument,0,'C'},
{"Patches", no_argument,0,'P'},
{"P Log", no_argument,0,'Q'},
{"Call Log", no_argument,0,'L'},
{"Autodetect", no_argument,0,'A'},
{0,0,0,0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long(argc, argv, "t: d v l e E x a:f:s:c:g:r:p S C P Q L A",
long_options, & option_index);
// warning: need to use : after required arguments, no colon for optional ones
// 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 't':
sync_timeout = atol(optarg);
printf("Sync Timeout = [%lld] seconds\n", sync_timeout);
break;
case 'd':
udeny = 1;
printf("Universal Denial Mode Set - Only groups with mode [A] will be granted voice channel\n");
break;
case 'v':
debug = 0;
//printf("Verbosity Mode Enabled - Debug set to 3 \n");
break;
case 'x':
printf("Extended Addressing with ESK Mode Enabled \n");
x_mask = 0xA0; //XOR for ESK
idcmd = 0xFD;
vcmd = 0xB8; //not using commands anymore for EA, but correcting this anyways
x_choice = 1;
break;
case 'E':
printf("Extended Addressing Mode Enabled \n"); //Extended Addressing without ESK, noticed a new trend lately
x_mask = 0x0;
idcmd = 0xFD;
vcmd = 0xB8;
x_choice = 1;
break;
case 'e':
printf("ESK Mode Enabled \n");
x_mask = 0xA0; //XOR for ESK
idcmd = 0xFD;
vcmd = 0xEE;
x_choice = 2;
break;
case 'l':
printf("EDACS Standard Mode Enabled \n");
x_mask = 0x0; //no XOR for legacy
vcmd = 0xEE;
idcmd = 0xFD;
x_choice = 2;
break;
case 'a':
a_len = atol(optarg);
printf("Agency bit setting = [%d] bits\n", a_len);
break;
case 'f':
f_len = atol(optarg);
printf("Fleet bit setting = [%d] bits\n", f_len);
break;
case 's':
if (optarg[0] == '-') {
printf("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
sitecsv = optarg;
printf("Site CSV File name = %s \n", sitecsv);
break;
case 'c':
start_site_id = atol(optarg);
printf("Site ID CC to Hunt For = [%lld] \n", start_site_id);
break;
case 'g':
if (optarg[0] == '-') {
printf("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
groupcsv = optarg;
printf("Group CSV File name = %s \n", groupcsv);
break;
case 'r':
if (optarg[0] == '-') {
printf("Error: -%c: option requires an argument\n", c);
print_usage(argv[0]);
}
rfgain = atoi(optarg);
printf("RF Gain = %d \n", rfgain);
break;
case 'p':
adjust = 1;
printf("AFC/PPM auto adjust enabled - warning, highly temperamental \n");
break;
case 'S':
S = 1;
printf("Site Extra Display Enabled \n");
break;
case 'C':
C = 1;
printf("Call Matrix Display Enabled \n");
break;
case 'P':
P = 1;
printf("Patch Display Enabled \n");
break;
case 'Q':
Q = 1;
printf("Patch and Peers Logging Enabled \n");
break;
case 'L':
L = 1;
printf("Voice Call Logging Enabled \n");
break;
case 'A':
A = 1;
printf("Autodetect EDACS Type - Experimental \n");
printf("AFS set to 4-4-3 when using Autodetect \n");
break;
}
}
}
//--------------------------------------------MAIN--------------------------------------
int main(int argc, char ** argv) {
setlocale(LC_ALL, "");
FM();
read_p(); //Read m
generate_gf(); //Construct the Galois Field GF(2**m)
gen_poly(); //Compute the generator polynomial of BCH code
printf("Galois Field GF(2**m) Constructed. Generator Polynomial Computed.\n");
printf("40-28-6-2 BCH Scheme for Error Detection and Correction.\n");
cc = 0;
groupcsv = "group.csv";
sitecsv = "site.csv";
resettime = time(NULL);
logtime = time(NULL) - 540;
ParseInputOptions(argc, argv);
signed int avg = 0; //sample average
s_len = 11 - (a_len + f_len);
if (x_choice == 0){ //if x_choice value not set at start, set to automatic
A = 1; }
if (x_choice == 2 || A == 1) {
printf("Subfleet bit setting = [%X] bits \n", s_len);
for (unsigned short int i = 0; i < a_len; i++) //A
{
a_mask = a_mask << 1;
a_mask |= 1;
}
a_mask = a_mask << (11 - a_len);
printf("a_mask = [%X] \n", a_mask);
for (unsigned short int i = 0; i < f_len; i++) //F
{
f_mask = f_mask << 1;
f_mask |= 1;
}
f_mask = f_mask << s_len;
printf("f_mask = [%X] \n", f_mask);
for (unsigned short int i = 0; i < s_len; i++) //S
{
s_mask = s_mask << 1;
s_mask |= 1;
}
printf("s_mask = [%X] \n", s_mask);
}
init_udp();
sleep(1); //patience is a virtue
//if gain specified by user, then change gain in PyEDACS from default value
squelchSet(5000);
if (rfgain > 0) {
gainSet(rfgain);
}
//When using PyEDACS and changing site you want to monitor, this will set to a presumably empty channel so it won't be stuck on the last monitored channel
if (start_site_id > 0) {
tuneCC(850000000);
}
last_sync_time = time(NULL); //set a sync_time here so we don't jump straight to no signal
for (int i = 0; i < SAMP_NUM / 2 / 3 - 1; i++) //zero array
{
avg_arr[i] = 0;
}
//let's get the party started
while (1) {
initscr(); //Initialize NCURSES screen window
start_color();
init_pair(1, COLOR_YELLOW, COLOR_BLACK); //Yellow/Amber for frame sync/control channel, NV style
init_pair(2, COLOR_RED, COLOR_BLACK); //Red for Terminated Calls
init_pair(3, COLOR_GREEN, COLOR_BLACK); //Green for Active Calls
init_pair(4, COLOR_CYAN, COLOR_BLACK); //Cyan for Site Extra and Patches
init_pair(5, COLOR_MAGENTA, COLOR_BLACK); //Magenta for no frame sync/signal
noecho();
cbreak();
if (hangup == 0 && (time(NULL) - hanguptime) > 30) { //extending to 30 seconds just in case dot detection doesn't catch, or long winded caller
squelchSet(5000);
hangup = 1;
}
if ((time(NULL) - last_sync_time) > sync_timeout) //Check to see if control channel is still there
{
erase();
attron(COLOR_PAIR(5));
for (short int i = 0; i < 7; i++) {
printw("%s \n", FM_banner[i]);
}
attroff(COLOR_PAIR(5));
printw("Control Channel not found/lost. Timeout. Waiting...\n");
last_sync_time = time(NULL); //this one is still needed to allow time for frame sync to resume
kicked = 0;
targetp = 0;
sourcep = 0;
peer = 0;
good = 1;
bad = 1;
gbr = 1; //"zero" out good bad and gbr
//active = 0; //disable to check behavior now that we are testing signal loss events, don't want leave channel open without dot detection
current_lcn = 0;
//reset log peers and patches when signal time out
if (x_choice == 1 && patch_array[0][0] > 0 && Q == 1) { //check patch_array to see if anything is in it, otherwise, will keep logging blanks until signal regained
FILE * pFile;
pFile = fopen("pandp.log", "a");
fprintf(pFile, "%s %s SITE %3lld Signal Loss Logging \n", getDate(), getTime(), tempsite_id);
fprintf(pFile, "Peer Sites ");
for (short int i = 0; i < 12; i++) {
if (peer_list[i] > 0) {
fprintf(pFile, "[%lld]", peer_list[i]);
}
}
fprintf(pFile, "\n");
for (short int i = 0; i < 49; i++) {
if (patch_array[i][0] > 0) {
fprintf(pFile, "Patch Group #%2d [%5lld] to [%5lld]", i + 1, patch_array[i][1], patch_array[i][0]);
fprintf(pFile, "\n");
}
}
fclose(pFile);
}
//end logging peers and patches before wipe
for (short int i = 0; i < 12; i++) { //zero out peer_list
peer_list[i] = 0;
}
for (short int i = 0; i < 49; i++) { //zero out patch_array
patch_array[i][0] = 0;
patch_array[i][1] = 0;
}
patch_site = 0;
tempsite_id = 999;
lcn_tally = 0;
resettime = time(NULL);
logtime = time(NULL) - 540;
if (start_site_id == 0) {
site_id = 0;
site_name = "Searching";
location_name = "Searching";
}
if (start_site_id > 0) {
printw("Attemping to tune Site [%d] LCNs to find Control Channel\n", start_site_id);
site_id = start_site_id;
csvImport();
if (cyclecc > -1 && cyclecc < 31) { //cycle through a possibility of 32 LCN channels in the sites.csv file
if (LCN_list[cyclecc] > 1) {
tuneCC(LCN_list[cyclecc]);
printw("LCN Freq: [%d]", LCN_list[cyclecc]);
cyclecc = cyclecc + 1;
refresh();
sleep(2);
}
}
if (LCN_list[cyclecc] == 0) { //if no more frequencies in sites.csv, jump back to beginning of cycle
cyclecc = 0;
}
if (cyclecc > 31) { //if cyclecc exceeds 31, jump back to beginning, assuming there are 32 LCN channels specified
cyclecc = 0;
}
}
refresh();
}
read(0, samples, 3 * 2); //read 3 samples (6 unsigned chars)
raw_stream[0] = (signed short int)((samples[0 + 1] << 8) | (samples[0] & 0xFF));
raw_stream[1] = (signed short int)((samples[2 + 1] << 8) | (samples[2] & 0xFF));
raw_stream[2] = (signed short int)((samples[4 + 1] << 8) | (samples[4] & 0xFF));
avg = (raw_stream[0] + raw_stream[1] + raw_stream[2]) / 3; //
//AFC recomputing using averaged samples
avg_arr[avg_cnt] = avg;
avg_cnt++;
if (avg_cnt >= SAMP_NUM / 2 / 3 - 1) //reset after filling avg_array
{
avg_cnt = 0;
min = SHRT_MAX;
max = SHRT_MIN;
for (int i = 0; i < SAMP_NUM / 2 / 3 - 1; i++) //simple min/max detector
{
if (avg_arr[i] > max)
max = avg_arr[i];
if (avg_arr[i] < min)
min = avg_arr[i];
}
AFC = (min + max) / 2;
}
//--------------------------------------
//pushing data into shift registers
sr_0 = (sr_0 << 1) | (sr_1 >> 63);
sr_1 = (sr_1 << 1) | (sr_2 >> 63);
sr_2 = (sr_2 << 1) | (sr_3 >> 63);
sr_3 = (sr_3 << 1) | (sr_4 >> 63);
sr_4 = sr_4 << 1;
if (avg < AFC)
sr_4 |= 1;
//---------------------------------
if ((sr_0 & SYNC_MASK) == SYNC_FRAME) //extract data after receiving the sync frame
{
last_sync_time = time(NULL); //set sync_time right at start of frame
//put sr data in human readable/easier to work with fr 40 bit (10 hex) messages
//disabling all but fr_1 and fr_4 due to BCH enabled, no need for extra redundancy
//will leave these values here for future reference, or if needed
fr_1 = ((sr_0 & 0xFFFF) << 24) | ((sr_1 & 0xFFFFFF0000000000) >> 40);
//fr_2 = sr_1 & 0xFFFFFFFFFF;
//fr_3 = (sr_2 & 0xFFFFFFFFFF000000) >> 24;
fr_4 = ((sr_2 & 0xFFFFFF) << 16) | ((sr_3 & 0xFFFF000000000000) >> 48);
//fr_5 = ((sr_3 & 0xFFFFFFFFFF00) >> 8);
//fr_6 = ((sr_3 & 0xFF) << 32) | ((sr_4 & 0xFFFFFFFF00000000) >> 32);
//BCH error detection
fr_1m = (fr_1 & 0xFFFFFFF000) >> 12; //message portion to send to bch to calc polynomial
BCH(fr_1m); //send through the bch
fr_1t = messagepp & 0xFFFFFFFFFF; //return from the bch
fr_4m = (fr_4 & 0xFFFFFFF000) >> 12; //message portion to send to bch to calc polynomial
BCH(fr_4m); //send through the bch
fr_4t = messagepp & 0xFFFFFFFFFF; //return from the bch
//ESK on/off detection
if ( (((fr_1t & 0xF000000000) >> 36) != 0xB) && (((fr_1t & 0xF000000000) >> 36) != 0x1) && (((fr_1t & 0xFF00000000) >> 32) != 0xF3) && A == 1 ) //fixed all to fr_1t
{
if ( (((fr_1t & 0xF000000000) >> 36) <= 0x8 )){ //experimenting with values here, not too high, and not too low
x_mask = 0xA0; }
if ( (((fr_1t & 0xF000000000) >> 36) > 0x8 ) ){ //ideal value would be 5, but some other values exist that don't allow it
x_mask = 0x0; }
}
//EA Auto detection