forked from g0orx/pihpsdr
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathhpsdrsim.c
1927 lines (1625 loc) · 60.9 KB
/
hpsdrsim.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
/* Copyright (C)
* 2019 - Christoph van Wüllen, DL1YCF
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/*
*
* This program simulates a HPSDR board.
* If an SDR program such as phipsdr "connects" with this program, it
* writes to stdout what goes on. This is great for debugging.
*
* In addition, I have built in the following features:
*
* This device has four "RF sources"
*
* RF1: ADC noise plus a signal at 14.1 MHz at -73 dBm
* RF2: ADC noise
* RF3: TX feedback signal with some distortion.
* RF4: normalized undistorted TX signal
*
* RF1 and RF2 signal strenght vary according to Preamp and Attenuator settings
* RF3 signal strength varies according to TX-drive and TX-ATT settings
* RF4 signal strength is normalized to amplitude of 0.407 (old protocol) or 0.2899 (new protocol)
* note HERMESLITEV2 old protocol: 0.23
*
* The connection with the ADCs are:
* ADC0: RF1 upon receive, RF3 upon transmit
* ADC1: RF2 (for HERMES: RF4)
* ADC2: RF4
*
* RF4 is the TX DAC signal. Upon TX, it goes to RX2 for Metis, RX4 for Hermes, and RX5 beyond.
* Since the feedback runs at the RX sample rate while the TX sample rate is fixed (48000 Hz),
* we have to re-sample and do this in a very stupid way (linear interpolation).
* NOTE: anan10E flag: use RX2 for TX DAC in the HERMES case.
*
* The "noise" is a random number of amplitude 0.00001
* that is about -100 dBm spread onto a spectrum whose width is the sample rate.
*
* The SDR application has to make the proper ADC settings, except for STEMlab
* (RedPitaya based SDRs), where there is a fixed association
* RX1=ADC1, RX2=ADC2, RX3=ADC2, RX4=TX-DAC
* and the PureSignal feedback signal is connected to the second ADC.
*
* If invoked with the "-diversity" flag, broad "man-made" noise is fed to ADC1 and
* ADC2 upon RXing. The ADC2 signal is phase shifted by 90 degrees and somewhat
* stronger. This noise can completely be eliminated using DIVERSITY.
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include <termios.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#ifdef __APPLE__
#include "MacOS.h" // emulate clock_gettime on old MacOS systems
#endif
#define EXTERN
#include "hpsdrsim.h"
/*
* These variables store the state of the "old protocol" SDR.
* Whenevery they are changed, this is reported.
*/
static int AlexTXrel = -1;
static int alexRXout = -1;
static int alexRXant = -1;
static int MicTS = -1;
static int duplex = -1;
static int receivers = -1;
static int rate = -1;
static int preamp = -1;
static int LTdither = -1;
static int LTrandom = -1;
static int ref10 = -1;
static int src122 = -1;
static int PMconfig = -1;
static int MicSrc = -1;
static int txdrive = 0;
static int txatt = 0;
static int sidetone_volume = -1;
static int cw_internal = -1;
static int envgain = 0;
static int pwmmin = 0;
static int pwmmax = 0;
static int adc2bpf = 0;
static int anan7kps = 0;
static int anan7kxvtr = 0;
static int rx_att[2] = {-1, -1};
static int rx1_attE = -1;
static int rx_preamp[4] = {-1, -1, -1, -1};
static int MerTxATT0 = -1;
static int MerTxATT1 = -1;
static int MetisDB9 = -1;
static int PeneSel = -1;
static int PureSignal = -1;
static int LineGain = -1;
static int MicPTT = -1;
static int tip_ring = -1;
static int MicBias = -1;
static int ptt = 0;
static int AlexAtt = -1;
static int TX_class_E = -1;
static int OpenCollectorOutputs = -1;
static long tx_freq = -1;
static long rx_freq[7] = {-1, -1, -1, -1, -1, -1, -1};
static int alex_lpf = -1;
static int alex_hpf = -1;
static int alex_manual = -1;
static int alex_bypass = -1;
static int lna6m = -1;
static int alexTRdisable = -1;
static int vna = -1;
static int line_in = -1;
static int mic_boost = -1;
static int apollo_filter = -1;
static int apollo_tuner = -1;
static int apollo_auto_tune = -1;
static int alex_apollo = -1;
static int hl2_q5 = -1;
static int hl2_tune = -1;
static int hl2_pa = -1;
static int hl2_tx_latency = 1;
static int hl2_ptt_hang = -1;
static int c25_ext_board_i2c_data = -1;
static int rx_adc[7] = {-1, -1, -1, -1, -1, -1, -1};
static int cw_hang = -1;
static int cw_reversed = -1;
static int cw_speed = -1;
static int cw_mode = -1;
static int cw_weight = -1;
static int cw_spacing = -1;
static int cw_delay = -1;
static int CommonMercuryFreq = -1;
static int freq = -1;
static int rx2gnd = -1;
static int TXDAC = 1;
struct hl2word {
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
} hl2addr[128];
const double hl2drv[16] = { 0.421697, 0.446684, 0.473151, 0.501187, 0.530884, 0.562341, 0.595662,
0.630957, 0.668344, 0.707946, 0.749894, 0.794328, 0.841395, 0.891251,
0.944061, 1.000000
};
// floating-point represeners of TX att, RX att, and RX preamp settings
static double txdrv_dbl = 0.99;
static double txatt_dbl = 1.0;
static double rxatt_dbl[4] = {1.0, 1.0, 1.0, 1.0}; // this reflects both ATT and PREAMP
/*
* Socket for communicating with the "PC side"
*/
static int sock_TCP_Server = -1;
static int sock_TCP_Client = -1;
static int sock_udp;
/*
* These two variables monitor whether the TX thread is active
*/
static int enable_thread = 0;
static int active_thread = 0;
static void process_ep2(uint8_t *frame);
static void *handler_ep6(void *arg);
static double last_i_sample = 0.0;
static double last_q_sample = 0.0;
static int txptr = -1;
static int oldnew = 3; // 1: only P1, 2: only P2, 3: P1 and P2,
static int anan10e = 0; // HERMES with anan10e set behaves like METIS
static double txlevel;
static double tonearg, tonearg2;
static double tonedelta, tonedelta2;
static int do_tone, t3p, t3l;
int main(int argc, char *argv[]) {
int i, j, size;
int count = 0;
pthread_t thread;
uint8_t id[4] = { 0xef, 0xfe, 1, 6 };
uint32_t code;
int16_t sample;
struct sockaddr_in addr_udp;
uint8_t buffer[1032];
struct timeval tv;
int yes = 1;
uint8_t *bp;
unsigned long checksum = 0;
socklen_t lenaddr;
struct sockaddr_in addr_from;
unsigned int seed;
memset(hl2addr, 0, sizeof(hl2addr));
uint32_t last_seqnum = 0xffffffff, seqnum; // sequence number of received packet
int udp_retries = 0;
int bytes_read, bytes_left;
uint32_t *code0 = (uint32_t *) buffer; // fast access to code of first buffer
double run, off, off2, inc;
struct timeval tvzero = {0, 0};
fd_set fds;
struct termios tios;
/*
* Examples for METIS: ATLAS bus with Mercury/Penelope boards
* Examples for HERMES: ANAN10, ANAN100 (Note ANAN-10E/100B behave like METIS)
* Examples for ANGELIA: ANAN100D
* Examples for ORION: ANAN200D
* Examples for ORION2: ANAN7000, ANAN8000
*
* Examples for C25: RedPitaya based boards with fixed ADC connections
*/
//
// put stdin into raw mode
//
tcgetattr(0, &tios);
tios.c_lflag &= ~ICANON;
tios.c_lflag &= ~ECHO;
tcsetattr(0, TCSANOW, &tios);
radio_digi_changed = 0; // used to trigger a highprio packet
radio_ptt = 0;
radio_dash = 0;
radio_dot = 0;
radio_io1 = 1;
radio_io2 = 1;
radio_io3 = 1;
radio_io4 = 1;
radio_io5 = 1;
radio_io6 = 1;
radio_io8 = 0;
// seed value for random number generator
seed = ((uintptr_t) &seed) & 0xffffff;
tonearg = 0.0;
tonedelta = 0.0;
tonearg2 = 0.0;
tonedelta2 = 0.0;
do_tone = 0;
diversity = 0;
noiseblank = 0;
nb_pulse = 0;
nb_width = 0;
const int MAC1 = 0x00;
const int MAC2 = 0x1C;
const int MAC3 = 0xC0;
const int MAC4 = 0xA2;
int MAC5 = 0x10;
const int MAC6 = 0xDD; // P1
const int MAC6N = 0xDD; // P2
OLDDEVICE = ODEV_ORION2;
NEWDEVICE = NDEV_ORION2;
for (i = 1; i < argc; i++) {
if (!strncmp(argv[i], "-atlas", 6)) {OLDDEVICE = ODEV_METIS; NEWDEVICE = NDEV_ATLAS; MAC5 = 0x11; continue;}
if (!strncmp(argv[i], "-metis", 6)) {OLDDEVICE = ODEV_METIS; NEWDEVICE = NDEV_ATLAS; MAC5 = 0x12; continue;}
if (!strncmp(argv[i], "-hermeslite2", 12)) {OLDDEVICE = ODEV_HERMES_LITE2; NEWDEVICE = NDEV_HERMES_LITE2; MAC5 = 0x13; oldnew = 1; continue;}
if (!strncmp(argv[i], "-hermeslite", 11)) {OLDDEVICE = ODEV_HERMES_LITE; NEWDEVICE = NDEV_HERMES_LITE; MAC5 = 0x14; oldnew = 1; continue;}
if (!strncmp(argv[i], "-hermes", 7)) {OLDDEVICE = ODEV_HERMES; NEWDEVICE = NDEV_HERMES; MAC5 = 0x15; continue;}
if (!strncmp(argv[i], "-griffin", 8)) {OLDDEVICE = ODEV_GRIFFIN; NEWDEVICE = NDEV_HERMES2; MAC5 = 0x16; continue;}
if (!strncmp(argv[i], "-angelia", 8)) {OLDDEVICE = ODEV_ANGELIA; NEWDEVICE = NDEV_ANGELIA; MAC5 = 0x17; continue;}
if (!strncmp(argv[i], "-orion2", 7)) {OLDDEVICE = ODEV_ORION2; NEWDEVICE = NDEV_ORION2; MAC5 = 0x18; continue;}
if (!strncmp(argv[i], "-g2", 3)) {OLDDEVICE = ODEV_NONE; NEWDEVICE = NDEV_SATURN; MAC5 = 0x19; oldnew = 2; continue;}
if (!strncmp(argv[i], "-orion", 6)) {OLDDEVICE = ODEV_ORION; NEWDEVICE = NDEV_ORION; MAC5 = 0x1A; continue;}
if (!strncmp(argv[i], "-c25", 4)) {OLDDEVICE = ODEV_C25; NEWDEVICE = NDEV_C25; MAC5 = 0x1B; oldnew = 1; continue;}
if (!strncmp(argv[i], "-diversity", 10)) {diversity = 1; continue;}
if (!strncmp(argv[i], "-P1", 3)) {oldnew = 1; continue;}
if (!strncmp(argv[i], "-P2", 3)) {oldnew = 2; continue;}
if (!strncmp(argv[i], "-anan10e", 8)) {anan10e = 1; continue;}
if (!strncmp(argv[i], "-nb", 3)) {
noiseblank = 1;
if (i < argc - 1) { sscanf(argv[++i], "%d", &nb_pulse); }
if (i < argc - 1) { sscanf(argv[++i], "%d", &nb_width); }
if (nb_pulse < 1 || nb_pulse > 200) { nb_pulse = 5; }
if (nb_width < 1 || nb_width > 200) { nb_width = 100; }
continue;
}
t_print("Unknown option: %s\n", argv[i]);
t_print("Valid options are: -atlas | -metis | -hermes | -griffin | -angelia |\n");
t_print(" -orion | -orion2 | -hermeslite | -hermeslite2 | -c25 |\n");
t_print(" -diversity | -P1 | -P2 |\n");
t_print(" -nb <num> <width>\n");
exit(8);
}
switch (NEWDEVICE) {
case NDEV_ATLAS:
t_print("DEVICE is ATLAS/METIS\n");
c1 = 3.3;
c2 = 0.090;
TXDAC = 1;
maxpwr = 20.0;
break;
case NDEV_HERMES:
t_print("DEVICE is HERMES\n");
c1 = 3.3;
c2 = 0.095;
maxpwr = 200.0;
if (anan10e) {
TXDAC = 1;
t_print("Anan10E/Anan100B simulation\n");
maxpwr = 20.0;
} else {
TXDAC = 3;
}
break;
case NDEV_HERMES2:
t_print("DEVICE is HERMES2/GRIFFIN\n");
c1 = 3.3;
c2 = 0.095;
TXDAC = 3;
maxpwr = 200.0;
break;
case NDEV_ANGELIA:
t_print("DEVICE is ANGELIA\n");
c1 = 3.3;
c2 = 0.095;
TXDAC = 4;
maxpwr = 200.0;
break;
case NDEV_HERMES_LITE:
t_print("DEVICE is HermesLite V1\n");
c1 = 3.3;
c2 = 1.5;
TXDAC = 1;
maxpwr = 7.0;
break;
case NDEV_HERMES_LITE2:
t_print("DEVICE is HermesLite V2\n");
c1 = 3.3;
c2 = 1.5;
TXDAC = 3;
maxpwr = 7.0;
break;
case NDEV_ORION:
t_print("DEVICE is ORION\n");
c1 = 5.0;
c2 = 0.108;
TXDAC = 4;
maxpwr = 200.0;
break;
case NDEV_ORION2:
t_print("DEVICE is ORION MkII\n");
c1 = 5.0;
c2 = 0.12;
TXDAC = 4;
maxpwr = 500.0;
break;
case NDEV_SATURN:
t_print("DEVICE is SATURN/G2\n");
c1 = 5.0;
c2 = 0.12;
TXDAC = 4;
maxpwr = 200.0;
break;
case NDEV_C25:
t_print("DEVICE is STEMlab/C25\n");
c1 = 3.3;
c2 = 0.090;
TXDAC = 3;
maxpwr = 20.0;
break;
}
//
// Initialize the data in the sample tables
//
t_print(".... producing random noise\n");
// Produce some noise
j = RAND_MAX / 2;
for (i = 0; i < LENNOISE; i++) {
noiseItab[i] = ((double) rand_r(&seed) / j - 1.0) * 0.00003;
noiseQtab[i] = ((double) rand_r(&seed) / j - 1.0) * 0.00003;
}
//
// Use only one buffer, so diversity and
// noise blanker testing are mutually exclusive
// so diversity==0 means "no man-made noise",
// diversity==1 && noiseblank == 0 means "noise for testing diversity"
// diversity==1 && noiseblank == 1 means "noise for testing noise blanker"
//
if (noiseblank) { diversity = 1; }
if (diversity && !noiseblank) {
//
// The diversity signal is a "comb" with a lot
// of equally spaces cosines
//
t_print("DIVERSITY testing activated!\n");
t_print(".... producing some man-made noise\n");
memset(divtab, 0, LENDIV * sizeof(double));
for (j = 1; j <= 200; j++) {
run = 0.0;
off = 0.25 * j * j;
inc = j * 0.00039269908169872415480783042290994;
for (i = 0; i < LENDIV; i++) {
divtab[i] += cos(run + off);
run += inc;
}
}
// normalize
off = 0.0;
for (i = 0; i < LENDIV; i++) {
if ( divtab[i] > off) { off = divtab[i]; }
if (-divtab[i] > off) { off = -divtab[i]; }
}
off = 1.0 / off;
t_print("(normalizing with %f)\n", off);
for (i = 0; i < LENDIV; i++) {
divtab[i] = divtab[i] * off;
}
}
if (diversity && noiseblank) {
//
// Create impulse noise as a real-time signal
// n impulses per second
// m samples wide
// about -80 dBm in 1000 Hz
//
off = sqrt(0.05 / (nb_pulse * nb_width));
memset(divtab, 0, LENDIV * sizeof(double));
t_print("NOISE BLANKER test activated: %d pulses of width %d within %d samples\n",
nb_pulse, nb_width, LENDIV);
for (i = 0; i < nb_pulse; i++) {
for (j = (i * LENDIV) / nb_pulse; j < (i * LENDIV) / nb_pulse + nb_width; j++) { divtab[j] = off; }
}
}
//
// clear TX fifo
//
txptr = -1;
memset (isample, 0, OLDRTXLEN * sizeof(double));
memset (qsample, 0, OLDRTXLEN * sizeof(double));
if ((sock_udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
t_perror("socket");
return EXIT_FAILURE;
}
setsockopt(sock_udp, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
setsockopt(sock_udp, SOL_SOCKET, SO_REUSEPORT, (void *)&yes, sizeof(yes));
tv.tv_sec = 0;
tv.tv_usec = 1000;
setsockopt(sock_udp, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
memset(&addr_udp, 0, sizeof(addr_udp));
addr_udp.sin_family = AF_INET;
addr_udp.sin_addr.s_addr = htonl(INADDR_ANY);
addr_udp.sin_port = htons(1024);
if (bind(sock_udp, (struct sockaddr *)&addr_udp, sizeof(addr_udp)) < 0) {
t_perror("bind");
return EXIT_FAILURE;
}
if ((sock_TCP_Server = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
t_perror("socket tcp");
return EXIT_FAILURE;
}
setsockopt(sock_TCP_Server, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));
int tcpmaxseg = 1032;
setsockopt(sock_TCP_Server, IPPROTO_TCP, TCP_MAXSEG, (const char *)&tcpmaxseg, sizeof(int));
int sndbufsize = 65535;
int rcvbufsize = 65535;
setsockopt(sock_TCP_Server, SOL_SOCKET, SO_SNDBUF, (const char *)&sndbufsize, sizeof(int));
setsockopt(sock_TCP_Server, SOL_SOCKET, SO_RCVBUF, (const char *)&rcvbufsize, sizeof(int));
tv.tv_sec = 0;
tv.tv_usec = 1000;
setsockopt(sock_TCP_Server, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
if (bind(sock_TCP_Server, (struct sockaddr *)&addr_udp, sizeof(addr_udp)) < 0) {
t_perror("bind tcp");
return EXIT_FAILURE;
}
listen(sock_TCP_Server, 1024);
t_print( "Listening for TCP client connection request\n");
int flags = fcntl(sock_TCP_Server, F_GETFL, 0);
fcntl(sock_TCP_Server, F_SETFL, flags | O_NONBLOCK);
while (1) {
memcpy(buffer, id, 4);
count++;
//
// If the keyboard has been hit, read character and consume it
//
FD_ZERO(&fds);
FD_SET(0, &fds); // 0 is stdin
if (select(1, &fds, NULL, NULL, &tvzero) > 0) {
unsigned char c;
int rc = read(0, &c, sizeof(c));
if (rc > 0) {
radio_digi_changed = 1;
switch (c) {
case '1':
radio_io1 = !radio_io1;
break;
case '2':
radio_io2 = !radio_io2;
break;
case '3':
radio_io3 = !radio_io3;
break;
case '4':
radio_io4 = !radio_io4;
break;
case '5':
radio_io5 = !radio_io5;
break;
case '6':
radio_io6 = !radio_io6;
break;
case '8':
radio_io8 = !radio_io8;
break;
case 'l':
radio_dot = !radio_dot;
break;
case 'r':
radio_dash = !radio_dash;
break;
case 'p':
radio_ptt = !radio_ptt;
break;
}
}
}
if (sock_TCP_Client > -1) {
// Using recvmmsg with a time-out should be used for a byte-stream protocol like TCP
// (Each "packet" in the datagram may be incomplete). This is especially true if the
// socket has a receive time-out, but this problem also occurs if the is no such
// receive time-out.
// Therefore we read a complete packet here (1032 bytes). Our TCP-extension to the
// HPSDR protocol ensures that only 1032-byte packets may arrive here.
bytes_read = 0;
bytes_left = 1032;
while (bytes_left > 0) {
size = recvfrom(sock_TCP_Client, buffer + bytes_read, (size_t)bytes_left, 0, NULL, 0);
if (size < 0 && errno == EAGAIN) { continue; }
if (size < 0) { break; }
bytes_read += size;
bytes_left -= size;
}
#ifdef PACKETLIST
t_print("TCP P1\n");
#endif
bytes_read = size;
if (size >= 0) {
// 1032 bytes have successfully been read by TCP.
// Let the downstream code know that there is a single packet, and its size
bytes_read = 1032;
// In the case of a METIS-discovery packet, change the size to 63
if (*code0 == 0x0002feef) {
bytes_read = 63;
}
// In principle, we should check on (*code0 & 0x00ffffff) == 0x0004feef,
// then we cover all kinds of start and stop packets.
// In the case of a METIS-stop packet, change the size to 64
if (*code0 == 0x0004feef) {
bytes_read = 64;
}
// In the case of a METIS-start TCP packet, change the size to 64
// The special start code 0x11 has no function any longer, but we shall still support it.
if (*code0 == 0x1104feef || *code0 == 0x0104feef) {
bytes_read = 64;
}
}
} else {
lenaddr = sizeof(addr_from);
bytes_read = recvfrom(sock_udp, buffer, 1032, 0, (struct sockaddr *)&addr_from, &lenaddr);
if (bytes_read > 0) {
udp_retries = 0;
#ifdef PACKETLIST
t_print("UDP P1\n");
#endif
} else {
udp_retries++;
}
}
if (bytes_read < 0 && errno != EAGAIN) {
t_perror("recvfrom");
return EXIT_FAILURE;
}
// If nothing has arrived via UDP for some time, try to open TCP connection.
// "for some time" means 10 subsequent un-successful UDP rcvmmsg() calls
if (sock_TCP_Client < 0 && udp_retries > 10 && oldnew != 2) {
if ((sock_TCP_Client = accept(sock_TCP_Server, (struct sockaddr *)&addr_from, &lenaddr)) > -1) {
t_print("sock_TCP_Client: Connected from %s\n", inet_ntoa(addr_from.sin_addr));
}
// This avoids firing accept() too often if it constantly fails
udp_retries = 0;
}
if (count >= 5000 && active_thread) {
t_print( "WATCHDOG STOP the transmission via handler_ep6\n");
enable_thread = 0;
while (active_thread) { usleep(1000); }
txptr = -1;
if (sock_TCP_Client > -1) {
close(sock_TCP_Client);
sock_TCP_Client = -1;
}
continue;
}
if (bytes_read <= 0) { continue; }
count = 0;
code = *code0;
switch (code) {
// PC to SDR transmission via process_ep2
case 0x0201feef:
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 1032) {
t_print("InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, (int)bytes_read);
break;
}
// sequence number check
seqnum = ((buffer[4] & 0xFF) << 24) + ((buffer[5] & 0xFF) << 16) + ((buffer[6] & 0xFF) << 8) + (buffer[7] & 0xFF);
if (seqnum != last_seqnum + 1) {
t_print("SEQ ERROR: last %ld, recvd %ld\n", (long)last_seqnum, (long)seqnum);
}
last_seqnum = seqnum;
process_ep2(buffer + 11);
process_ep2(buffer + 523);
if (labs(7100000L - rx_freq[0]) < (24000 << rate)) {
//
// weak single-tone signal at 7100 kHz
//
off = (double)(7100000 - rx_freq[0]);
tonedelta = -6.283185307179586476925286766559 * off / ((double) (48000 << rate));
do_tone = 3;
t3l = 9600 << rate;
} else if (labs(14100000L - rx_freq[0]) < (24000 << rate)) {
//
// -73 dBm single-tone signal at 14100 kHz
//
off = (double)(14100000 - rx_freq[0]);
tonedelta = -6.283185307179586476925286766559 * off / ((double) (48000 << rate));
do_tone = 1;
} else if (labs(21100000L - rx_freq[0]) < (24000 << rate)) {
//
// two -73 dBm signals at 21100.0 and 21000.9 kHz
//
off = (double)(21100000 - rx_freq[0]);
tonedelta = -6.283185307179586476925286766559 * off / ((double) (48000 << rate));
off2 = (double)(21100900 - rx_freq[0]);
tonedelta2 = -6.283185307179586476925286766559 * off2 / ((double) (48000 << rate));
do_tone = 2;
} else {
do_tone = 0;
}
if (active_thread) {
if (txptr < 0) {
txptr = OLDRTXLEN / 2;
}
// Put TX IQ samples into the ring buffer
// In the old protocol, samples come in groups of 8 bytes L1 L0 R1 R0 I1 I0 Q1 Q0
// Here, L1/L0 and R1/R0 are audio samples, and I1/I0 and Q1/Q0 are the TX iq samples
// I1 contains bits 8-15 and I0 bits 0-7 of a signed 16-bit integer. We convert this
// here to double. If the RX sample rate is larger than the TX on, we perform a
// simple linear interpolation between the last and current sample.
// Note that this interpolation causes weak "sidebands" at 48/96/... kHz distance (the
// strongest ones at 48 kHz).
double disample, dqsample, idelta, qdelta;
double sum;
bp = buffer + 16; // skip 8 header and 8 SYNC/C&C bytes
sum = 0.0;
for (j = 0; j < 126; j++) {
bp += 4; // skip audio samples
sample = (int)((signed char) * bp++) << 8;
sample |= (int) ((signed char) * bp++ & 0xFF);
disample = (double) sample * 0.000030517578125; // division by 32768
sample = (int)((signed char) * bp++) << 8;
sample |= (int) ((signed char) * bp++ & 0xFF);
dqsample = (double) sample * 0.000030517578125;
sum += (disample * disample + dqsample * dqsample);
switch (rate) {
case 0: // RX sample rate = TX sample rate = 48000
isample[txptr ] = disample;
qsample[txptr++] = dqsample;
break;
case 1: // RX sample rate = 96000; TX sample rate = 48000
idelta = 0.5 * (disample - last_i_sample);
qdelta = 0.5 * (dqsample - last_q_sample);
isample[txptr ] = last_i_sample + idelta;
qsample[txptr++] = last_q_sample + qdelta;
isample[txptr ] = disample;
qsample[txptr++] = dqsample;
break;
case 2: // RX sample rate = 192000; TX sample rate = 48000
idelta = 0.25 * (disample - last_i_sample);
qdelta = 0.25 * (dqsample - last_q_sample);
isample[txptr ] = last_i_sample + idelta;
qsample[txptr++] = last_q_sample + qdelta;
isample[txptr ] = last_i_sample + 2.0 * idelta;
qsample[txptr++] = last_q_sample + 2.0 * qdelta;
isample[txptr ] = last_i_sample + 3.0 * idelta;
qsample[txptr++] = last_q_sample + 3.0 * qdelta;
isample[txptr ] = disample;
qsample[txptr++] = dqsample;
break;
case 3: // RX sample rate = 384000; TX sample rate = 48000
idelta = 0.125 * (disample - last_i_sample);
qdelta = 0.125 * (dqsample - last_q_sample);
isample[txptr ] = last_i_sample + idelta;
qsample[txptr++] = last_q_sample + qdelta;
isample[txptr ] = last_i_sample + 2.0 * idelta;
qsample[txptr++] = last_q_sample + 2.0 * qdelta;
isample[txptr ] = last_i_sample + 3.0 * idelta;
qsample[txptr++] = last_q_sample + 3.0 * qdelta;
isample[txptr ] = last_i_sample + 4.0 * idelta;
qsample[txptr++] = last_q_sample + 4.0 * qdelta;
isample[txptr ] = last_i_sample + 5.0 * idelta;
qsample[txptr++] = last_q_sample + 5.0 * qdelta;
isample[txptr ] = last_i_sample + 6.0 * idelta;
qsample[txptr++] = last_q_sample + 6.0 * qdelta;
isample[txptr ] = last_i_sample + 7.0 * idelta;
qsample[txptr++] = last_q_sample + 7.0 * qdelta;
isample[txptr ] = disample;
qsample[txptr++] = dqsample;
break;
}
last_i_sample = disample;
last_q_sample = dqsample;
if (j == 62) { bp += 8; } // skip 8 SYNC/C&C bytes of second block
}
txlevel = txdrv_dbl * txdrv_dbl * sum * 0.0079365;
// wrap-around of ring buffer
if (txptr >= OLDRTXLEN) { txptr = 0; }
}
break;
// respond to an incoming Metis detection request
case 0x0002feef:
if (oldnew == 2) {
t_print("OldProtocol detection request from %s IGNORED.\n", inet_ntoa(addr_from.sin_addr));
break; // Swallow P1 detection requests
}
t_print( "Respond to an incoming Metis detection request from %s / code: 0x%08x\n", inet_ntoa(addr_from.sin_addr),
code);
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 63) {
t_print("InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, (int)bytes_read);
break;
}
memset(buffer, 0, 60);
buffer[0] = 0xEF;
buffer[1] = 0xFE;
buffer[2] = 0x02;
buffer[3] = MAC1; // buffer[3:8] is MAC address
buffer[4] = MAC2;
buffer[5] = MAC3;
buffer[6] = MAC4;
buffer[7] = MAC5; // specifies type of radio
buffer[8] = MAC6; // encodes old protocol
buffer[ 2] = 2;
if (active_thread || new_protocol_running()) {
buffer[2] = 3;
}
buffer[9] = 31; // software version
buffer[10] = OLDDEVICE;
if (OLDDEVICE == ODEV_HERMES_LITE2) {
// use HL1 device ID and new software version
buffer[9] = 73;
buffer[10] = ODEV_HERMES_LITE;
buffer[19] = 4; // number of receivers
buffer[21] = 2; // Version 73.2
}
if (sock_TCP_Client > -1) {
// We will get into trouble if we respond via TCP while the radio is
// running with TCP.
// We simply suppress the response in this (very unlikely) case.
if (!active_thread) {
if (send(sock_TCP_Client, buffer, 60, 0) < 0) {
t_print( "TCP send error occurred when responding to an incoming Metis detection request!\n");
}
// close the TCP socket which was only used for the detection
close(sock_TCP_Client);
sock_TCP_Client = -1;
}
} else {
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
}
break;
// stop the SDR to PC transmission via handler_ep6
case 0x0004feef:
t_print( "STOP the transmission via handler_ep6 / code: 0x%08x\n", code);
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 64) {
t_print("InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, bytes_read);
break;
}
enable_thread = 0;
while (active_thread) { usleep(1000); }
txptr = -1;
if (sock_TCP_Client > -1) {
close(sock_TCP_Client);
sock_TCP_Client = -1;
}
break;
case 0x0104feef:
case 0x0204feef:
case 0x0304feef:
if (new_protocol_running()) {
t_print("OldProtocol START command received but NewProtocol radio already running!\n");
break;
}
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 64) {
t_print("InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, bytes_read);
break;
}
t_print( "START the PC-to-SDR handler thread / code: 0x%08x\n", code);
enable_thread = 0;
while (active_thread) { usleep(1000); }
memset(&addr_old, 0, sizeof(addr_old));
addr_old.sin_family = AF_INET;
addr_old.sin_addr.s_addr = addr_from.sin_addr.s_addr;
addr_old.sin_port = addr_from.sin_port;
memset(isample, 0, OLDRTXLEN * sizeof(double));
memset(qsample, 0, OLDRTXLEN * sizeof(double));
enable_thread = 1;
active_thread = 1;
if (pthread_create(&thread, NULL, handler_ep6, NULL) < 0) {
t_perror("create old protocol thread");
return EXIT_FAILURE;
}
pthread_detach(thread);
break;
default:
/*
* Here we have to handle the following "non standard" cases:
* OldProtocol "program" packet 264 bytes starting with EF FE 03 01
* OldProtocol "erase" packet 64 bytes starting with EF FE 03 02
* OldProtocol "Set IP" packet 63 bytes starting with EF FE 03
* NewProtocol "Discovery" packet 60 bytes starting with 00 00 00 00 02
* NewProtocol "program" packet 265 bytes starting with xx xx xx xx 05 (XXXXXXXX = Seq. Number)
* NewProtocol "erase" packet 60 bytes starting with 00 00 00 00 04
* NewProtocol "Set IP" packet 60 bytes starting with 00 00 00 00 03
* NewProtocol "General" packet 60 bytes starting with 00 00 00 00 00
* ==> this starts NewProtocol radio
*/
if (bytes_read == 264 && buffer[0] == 0xEF && buffer[1] == 0xFE && buffer[2] == 0x03 && buffer[3] == 0x01) {
static unsigned long cnt = 0;
unsigned long blks = (buffer[4] << 24) + (buffer[5] << 16) + (buffer[6] << 8) + buffer[7];
t_print("OldProtocol Program blks=%lu count=%ld\r", blks, ++cnt);
usleep(1000);
memset(buffer, 0, 60);
buffer[0] = 0xEF;
buffer[1] = 0xFE;
buffer[2] = 0x04;