-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathhpsdrsim.c
1468 lines (1340 loc) · 51.2 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
/*
* HPSDR simulator, (C) Christoph van Wuellen, April/Mai 2019
*
* 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 800 Hz signal plus 5000 Hz signal at -73dBm
* 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).
*
* The "noise" is a random number of amplitude 0.00003 (last bit on a 16-bit ADC),
* that is about -90 dBm spread onto a spectrum whose width is the sample rate. Therefore
* the "measured" noise floor in a filter 5 kHz wide is -102 dBm for a sample rate of 48 kHz
* but -111 dBm for a sample rate of 384000 kHz. This is a nice demonstration how the
* spectral density of "ADC noise" goes down when increasing 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.
*
* Audio sent to the "radio" is played via the first available output channel.
* This works on MacOS (PORTAUDIO) and Linux (ALSASOUND).
*
* 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>
#ifdef __APPLE__
#include "MacOS.h" // emulate clock_gettime on old MacOS systems
#endif
//#define PACKETLIST // indicate incoming packets with time-stamp
#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 dash = 0;
static int dot = 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 hermes_config=-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 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;
struct hl2word {
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
} hl2addr[64];
// 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=0;
static int oldnew=3; // 1: only P1, 2: only P2, 3: P1 and P2,
static double txlevel;
int main(int argc, char *argv[])
{
int i, j, size;
struct sched_param param;
pthread_attr_t attr;
pthread_t thread;
uint8_t id[4] = { 0xef, 0xfe, 1, 6 };
uint32_t code;
int16_t sample,l,r;
struct sockaddr_in addr_udp;
uint8_t buffer[1032];
struct timeval tv;
struct timespec ts;
int yes = 1;
uint8_t *bp;
unsigned long checksum;
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
int fd;
long cnt;
double run,off,inc;
/*
* Examples for METIS: ATLAS bus with Mercury/Penelope boards
* Examples for HERMES: ANAN10, ANAN100
* Examples for ANGELIA: ANAN100D
* Examples for ORION: ANAN200D
* Examples for ORION2: ANAN7000, ANAN8000
*
* Examples for C25: RedPitaya based boards with fixed ADC connections
*/
// seed value for random number generator
seed = ((uintptr_t) &seed) & 0xffffff;
diversity=0;
noiseblank=0;
nb_pulse=0;
nb_width=0;
MAC5=0x66;
OLDDEVICE=DEVICE_ORION2;
NEWDEVICE=NEW_DEVICE_ORION2;
for (i=1; i<argc; i++) {
if (!strncmp(argv[i],"-atlas" , 6)) {OLDDEVICE=DEVICE_METIS; NEWDEVICE=NEW_DEVICE_ATLAS; MAC5=0x11;}
if (!strncmp(argv[i],"-hermes" , 7)) {OLDDEVICE=DEVICE_HERMES; NEWDEVICE=NEW_DEVICE_HERMES; MAC5=0x22;}
if (!strncmp(argv[i],"-griffin" , 8)) {OLDDEVICE=DEVICE_GRIFFIN; NEWDEVICE=NEW_DEVICE_HERMES2; MAC5=0x33;}
if (!strncmp(argv[i],"-angelia" , 8)) {OLDDEVICE=DEVICE_ANGELIA; NEWDEVICE=NEW_DEVICE_ANGELIA; MAC5=0x44;}
if (!strncmp(argv[i],"-orion" , 6)) {OLDDEVICE=DEVICE_ORION; NEWDEVICE=NEW_DEVICE_ORION; MAC5=0x55;}
if (!strncmp(argv[i],"-orion2" , 7)) {OLDDEVICE=DEVICE_ORION2; NEWDEVICE=NEW_DEVICE_ORION2; MAC5=0x66;}
if (!strncmp(argv[i],"-hermeslite" , 11)) {OLDDEVICE=DEVICE_HERMES_LITE; NEWDEVICE=NEW_DEVICE_HERMES_LITE; MAC5=0x77;}
if (!strncmp(argv[i],"-hermeslite2", 12)) {OLDDEVICE=DEVICE_HERMES_LITE2;NEWDEVICE=NEW_DEVICE_HERMES_LITE2; MAC5=0x88;}
if (!strncmp(argv[i],"-c25" , 4)) {OLDDEVICE=DEVICE_C25; NEWDEVICE=NEW_DEVICE_HERMES; MAC5=0x99;}
if (!strncmp(argv[i],"-diversity", 10)) {diversity=1;}
if (!strncmp(argv[i],"-P1", 3)) {oldnew=1;}
if (!strncmp(argv[i],"-P2", 3)) {oldnew=2;}
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;
}
}
switch (OLDDEVICE) {
case DEVICE_METIS: fprintf(stderr,"DEVICE is METIS\n"); c1=3.3; c2=0.090; break;
case DEVICE_HERMES: fprintf(stderr,"DEVICE is HERMES\n"); c1=3.3; c2=0.095; break;
case DEVICE_GRIFFIN: fprintf(stderr,"DEVICE is GRIFFIN\n"); c1=3.3; c2=0.095; break;
case DEVICE_ANGELIA: fprintf(stderr,"DEVICE is ANGELIA\n"); c1=3.3; c2=0.095; break;
case DEVICE_HERMES_LITE: fprintf(stderr,"DEVICE is HermesLite V1\n"); c1=3.3; c2=0.095; break;
case DEVICE_HERMES_LITE2: fprintf(stderr,"DEVICE is HermesLite V2\n"); c1=3.3; c2=0.095; break;
case DEVICE_ORION: fprintf(stderr,"DEVICE is ORION\n"); c1=5.0; c2=0.108; break;
case DEVICE_ORION2: fprintf(stderr,"DEVICE is ORION MkII\n"); c1=5.0; c2=0.108; break;
case DEVICE_C25: fprintf(stderr,"DEVICE is STEMlab/C25\n"); c1=3.3; c2=0.090; break;
}
//
// Initialize the data in the sample tables
//
fprintf(stderr,".... 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;
}
fprintf(stderr,".... producing signals\n");
// Produce an 800 Hz tone at 0 dBm
run=0.0;
off=0.0;
for (i=0; i<LENTONE; i++) {
toneQtab[i]=cos(run)+cos(off);
toneItab[i]=sin(run)+sin(off);
run += 0.0032724923474893679567319201909161;
off += 0.016362461737446839783659600954581;
}
//
// 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
//
fprintf(stderr,"DIVERSITY testing activated!\n");
fprintf(stderr,".... 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;
fprintf(stderr,"(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));
fprintf(stderr,"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
//
memset (isample, 0, OLDRTXLEN*sizeof(double));
memset (qsample, 0, OLDRTXLEN*sizeof(double));
if ((sock_udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
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)
{
perror("bind");
return EXIT_FAILURE;
}
if ((sock_TCP_Server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
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)
{
perror("bind tcp");
return EXIT_FAILURE;
}
listen(sock_TCP_Server, 1024);
fprintf(stderr, "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);
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
clock_gettime(CLOCK_MONOTONIC, &ts);
fprintf(stderr,"TCP:%d.%03d\n", (int) (ts.tv_sec % 1000), (int) (ts.tv_nsec/1000000L));
#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
clock_gettime(CLOCK_MONOTONIC, &ts);
fprintf(stderr,"UDP:%d.%03d\n", (int) (ts.tv_sec % 1000), (int) (ts.tv_nsec/1000000L));
#endif
}
else
{
udp_retries++;
}
}
if (bytes_read < 0 && errno != EAGAIN)
{
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)
{
if((sock_TCP_Client = accept(sock_TCP_Server, NULL, NULL)) > -1)
{
fprintf(stderr,"sock_TCP_Client: %d connected to sock_TCP_Server: %d\n", sock_TCP_Client, sock_TCP_Server);
}
// This avoids firing accept() too often if it constantly fails
udp_retries=0;
}
if (bytes_read <= 0) continue;
memcpy(&code, buffer, 4);
switch (code)
{
// PC to SDR transmission via process_ep2
case 0x0201feef:
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 1032)
{
fprintf(stderr,"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)
{
fprintf(stderr,"SEQ ERROR: last %ld, recvd %ld\n", (long)last_seqnum, (long)seqnum);
}
last_seqnum = seqnum;
process_ep2(buffer + 11);
process_ep2(buffer + 523);
if (active_thread) {
// 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) {
fprintf(stderr,"OldProtocol detection request IGNORED.\n");
break; // Swallow P1 detection requests
}
fprintf(stderr, "Respond to an incoming Metis detection request / code: 0x%08x\n", code);
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 63)
{
fprintf(stderr,"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]=0xAA; // buffer[3:8] is MAC address
buffer[4]=0xBB;
buffer[5]=0xCC;
buffer[6]=0xDD;
buffer[7]=MAC5; // specifies type of radio
buffer[8]=0xFF; // 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 == DEVICE_HERMES_LITE2) {
// use HL1 device ID and new software version
buffer[9]=71;
buffer[10]=DEVICE_HERMES_LITE;
buffer[19]=4; // number of receivers
}
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)
{
fprintf(stderr, "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:
fprintf(stderr, "STOP the transmission via handler_ep6 / code: 0x%08x\n", code);
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 64)
{
fprintf(stderr,"InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, bytes_read);
break;
}
enable_thread = 0;
while (active_thread) usleep(1000);
if (sock_TCP_Client > -1)
{
close(sock_TCP_Client);
sock_TCP_Client = -1;
}
break;
case 0x0104feef:
case 0x0204feef:
case 0x0304feef:
if (new_protocol_running()) {
fprintf(stderr,"OldProtocol START command received but NewProtocol radio already running!\n");
break;
}
// processing an invalid packet is too dangerous -- skip it!
if (bytes_read != 64)
{
fprintf(stderr,"InvalidLength: RvcMsg Code=0x%08x Len=%d\n", code, bytes_read);
break;
}
fprintf(stderr, "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;
//
// The initial value of txptr defines the delay between
// TX samples sent to the SDR and PURESIGNAL feedback
// samples arriving
//
txptr=OLDRTXLEN/2;
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)
{
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
* OldProtocol "erase" packet
* OldProtocol "Set IP" packet
* NewProtocol "Discovery" packet
* NewProtocol "program" packet
* NewProtocol "erase" packet
* NewProtocol "Set IP" packet
* NewProtocol "General" packet ==> this starts NewProtocol radio
*/
if (bytes_read == 264 && buffer[0] == 0xEF && buffer[1] == 0xFE && buffer[2] == 0x03 && buffer[3] == 0x01) {
static long cnt=0;
unsigned long blks=(buffer[4] << 24) + (buffer[5] << 16) + (buffer[6] << 8) + buffer[7];
fprintf(stderr,"OldProtocol Program blks=%lu count=%ld\r", blks, ++cnt);
usleep(1000);
memset(buffer, 0, 60);
buffer[0]=0xEF;
buffer[1]=0xFE;
buffer[2]=0x04;
buffer[3]=0xAA;
buffer[4]=0xBB;
buffer[5]=0xCC;
buffer[6]=0xDD;
buffer[7]=MAC5; // specifies type of radio
buffer[8]=0xFF; // encodes old protocol
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
if (blks == cnt) fprintf(stderr,"\n\n Programming Done!\n");
break;
}
if (bytes_read == 64 && buffer[0] == 0xEF && buffer[1] == 0xFE && buffer[2] == 0x03 && buffer[3] == 0x02) {
fprintf(stderr,"OldProtocol Erase packet received:\n");
sleep(1);
cnt=0;
memset(buffer, 0, 60);
buffer[0]=0xEF;
buffer[1]=0xFE;
buffer[2]=0x03;
buffer[3]=0xAA;
buffer[4]=0xBB;
buffer[5]=0xCC;
buffer[6]=0xDD;
buffer[7]=MAC5; // specifies type of radio
buffer[8]=0xFF; // encodes old protocol
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
break;
}
if (bytes_read == 63 && buffer[0] == 0xEF && buffer[1] == 0xFE && buffer[2] == 0x03) {
fprintf(stderr,"OldProtocol SetIP packet received:\n");
fprintf(stderr,"MAC address is %02x:%02x:%02x:%02x:%02x:%02x\n", buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8]);
fprintf(stderr,"IP address is %03d:%03d:%03d:%03d\n", buffer[9], buffer[10], buffer[11], buffer[12]);
buffer[2]=0x02;
memset(buffer+9, 0, 54);
sendto(sock_udp, buffer, 63, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
break;
}
if (code == 0 && buffer[4] == 0x02) {
if (oldnew == 1) {
fprintf(stderr,"NewProtocol discovery packet IGNORED.\n");
break;
}
fprintf(stderr,"NewProtocol discovery packet received\n");
// prepeare response
memset(buffer, 0, 60);
buffer [4]=0x02+new_protocol_running();
buffer [5]=0xAA;
buffer[ 6]=0xBB;
buffer[ 7]=0xCC;
buffer[ 8]=0xDD;
buffer[ 9]=MAC5; // specifies type of radio
buffer[10]=0xFE; // encodes new protocol
buffer[11]=NEWDEVICE;
buffer[12]=38;
buffer[13]=19;
buffer[20]=2;
buffer[21]=1;
buffer[22]=3;
// HERMES_LITE2 is a HermesLite with a new software version
if (NEWDEVICE == NEW_DEVICE_HERMES_LITE2) {
buffer[11]=NEW_DEVICE_HERMES_LITE;
}
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
break;
}
if (code == 0 && buffer[4] == 0x04) {
if (oldnew == 1) {
fprintf(stderr,"NewProtocol erase packet IGNORED.\n");
break;
}
fprintf(stderr,"NewProtocol erase packet received\n");
memset(buffer, 0, 60);
buffer [4]=0x02+active_thread;
buffer [5]=0xAA;
buffer[ 6]=0xBB;
buffer[ 7]=0xCC;
buffer[ 8]=0xDD;
buffer[ 9]=MAC5; // specifies type of radio
buffer[10]=0xFE; // encodes new protocol
buffer[11]=NEWDEVICE;
buffer[12]=38;
buffer[13]=103;
buffer[20]=2;
buffer[21]=1;
buffer[22]=3;
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
sleep(5); // pretend erase takes 5 seconds
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
break;
}
if (bytes_read == 265 && buffer[4] == 0x05) {
if (oldnew == 1) {
fprintf(stderr,"NewProtocol program packet IGNORED.\n");
break;
}
unsigned long seq, blk;
seq=(buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
blk=(buffer[5] << 24) + (buffer[6] << 16) + (buffer[7] << 8) + buffer[8];
fprintf(stderr,"NewProtocol Program packet received: seq=%lu blk=%lu\r",seq,blk);
if (seq == 0) checksum=0;
for (j=9; j<=264; j++) checksum += buffer[j];
memset(buffer+4, 0, 56); // keep seq. no
buffer[ 4]=0x04;
buffer [5]=0xAA;
buffer[ 6]=0xBB;
buffer[ 7]=0xCC;
buffer[ 8]=0xDD;
buffer[ 9]=MAC5; // specifies type of radio
buffer[10]=0xFE; // encodes new protocol
buffer[11]=103;
buffer[12]=NEWDEVICE;
buffer[13]=(checksum >> 8) & 0xFF;
buffer[14]=(checksum ) & 0xFF;
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
if (seq+1 == blk) fprintf(stderr,"\n\nProgramming Done!\n");
break;
}
if (bytes_read == 60 && code == 0 && buffer[4] == 0x06) {
if (oldnew == 1) {
fprintf(stderr,"NewProtocol SetIP packet IGNORED.\n");
break;
}
fprintf(stderr,"NewProtocol SetIP packet received for MAC %2x:%2x:%2x:%2x%2x:%2x IP=%d:%d:%d:%d\n",
buffer[5],buffer[6],buffer[7],buffer[8],buffer[9],buffer[10],
buffer[11],buffer[12],buffer[13],buffer[14]);
// only respond if this is for OUR device
if (buffer[ 5] != 0xAA) break;
if (buffer[ 6] != 0xBB) break;
if (buffer[ 7] != 0xCC) break;
if (buffer[ 8] != 0xDD) break;
if (buffer[ 9] != MAC5) break; // specifies type of radio
if (buffer[10] != 0xFE) break; // encodes new protocol
memset(buffer, 0, 60);
buffer [4]=0x02+active_thread;
buffer [5]=0xAA;
buffer[ 6]=0xBB;
buffer[ 7]=0xCC;
buffer[ 8]=0xDD;
buffer[ 9]=MAC5; // specifies type of radio
buffer[10]=0xFE; // encodes new protocol
buffer[11]=NEWDEVICE;
buffer[12]=38;
buffer[13]=103;
buffer[20]=2;
buffer[21]=1;
buffer[22]=3;
sendto(sock_udp, buffer, 60, 0, (struct sockaddr *)&addr_from, sizeof(addr_from));
break;
}
if (bytes_read == 60 && buffer[4] == 0x00) {
if (oldnew == 1) {
fprintf(stderr,"NewProtocol General packet IGNORED.\n");
break;
}
// handle "general packet" of the new protocol
memset(&addr_new, 0, sizeof(addr_new));
addr_new.sin_family = AF_INET;
addr_new.sin_addr.s_addr = addr_from.sin_addr.s_addr;
addr_new.sin_port = addr_from.sin_port;
new_protocol_general_packet(buffer);
break;
}
else
{
fprintf(stderr,"Invalid packet (len=%d) detected: ",bytes_read);
for (i=0; i<16; i++) fprintf(stderr,"%02x ",buffer[i]);
fprintf(stderr,"\n");
}
break;
}
}
close(sock_udp);
if (sock_TCP_Client > -1)
{
close(sock_TCP_Client);
}
if (sock_TCP_Server > -1)
{
close(sock_TCP_Server);
}
return EXIT_SUCCESS;
}
#define chk_data(a,b,c) if ((a) != b) { b = (a); printf("%20s= %08lx (%10ld)\n", c, (long) b, (long) b ); }
void process_ep2(uint8_t *frame)
{
uint16_t data;
int rc;
int mod_ptt;
int mod;
chk_data(frame[0] & 1, ptt, "PTT");
switch (frame[0])
{
case 0:
case 1:
chk_data((frame[1] & 0x03) >> 0, rate, "SampleRate");
chk_data((frame[1] & 0x0C) >> 3, ref10, "Ref10MHz");
chk_data((frame[1] & 0x10) >> 4, src122, "Source122MHz");
chk_data((frame[1] & 0x60) >> 5, PMconfig, "Penelope/Mercury config");
chk_data((frame[1] & 0x80) >> 7, MicSrc, "MicSource");
chk_data(frame[2] & 1, TX_class_E, "TX CLASS-E");
chk_data((frame[2] & 0xfe) >> 1, OpenCollectorOutputs,"OpenCollector");
chk_data(((frame[4] >> 3) & 7) + 1, receivers, "RECEIVERS");
chk_data(((frame[4] >> 6) & 1), MicTS, "TimeStampMic");
chk_data(((frame[4] >> 7) & 1), CommonMercuryFreq,"Common Mercury Freq");
mod=0;
rc=frame[3] & 0x03;
if (rc != AlexAtt) {
mod=1;
AlexAtt=rc;
}
rc=(frame[3] & 0x04) >> 2;
if (rc != preamp) {
mod=1;
preamp=rc;
}
rc=(frame[3] & 0x08) >> 3;
if (rc != LTdither) {
mod=1;
LTdither=rc;
}
rc=(frame[3] & 0x10) >> 4;
if (rc != LTrandom) {
mod=1;
LTrandom=rc;
}
if (mod) fprintf(stderr,"AlexAtt=%d Preamp=%d Dither=%d Random=%d\n", AlexAtt,preamp,LTdither,LTrandom);
mod=0;
rc=(frame[3] & 0x60) >> 5;
if (rc != alexRXant) {
mod=1;
alexRXant=rc;
}
rc=(frame[3] & 0x80) >> 7;
if (rc != alexRXout) {
mod=1;