-
Notifications
You must be signed in to change notification settings - Fork 6
/
hl2_tcp.c
1844 lines (1637 loc) · 56.4 KB
/
hl2_tcp.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
//
// hl2_tcp.c
//
#define VERSION "v.1.4.105" // 2023-01-05 2021-04-11
// macOS : clang -lm -lpthread -Os -o hl2_tcp hl2_tcp.c hl2_tx.c
// pi : cc -lm -lpthread -Os -o hl2_tcp hl2_tcp.c hl2_tx.c
// (hl2_tx.c)
//
// Serves IQ data using the rtl_tcp protocol
// from an Hermes Lite 2 on port 1024
// to iPv6 port 1234
//
// initial version 2020-01-27 rhn
//
// Copyright 2017,2020,2022 Ronald H Nicholson Jr. All Rights Reserved.
// This code may only be redistributed under terms of
// the Mozilla Public License 2.0 plus Exhibit B (no copyleft exception)
// See: https://www.mozilla.org/en-US/MPL/2.0/
// #define DEBUG_MODE_0
int discover_use_ip = 0;
#ifdef __clang__
#endif
#include <stddef.h>
float cwGain1 = 0.0;
int slices = 1;
int sliceLoop = 0;
int epcnt = 0;
int ep4cnt = 0;
extern long int random(void);;
int tx_freq ;
int dbugCnt = 0;
#define TX_OK
// #define NO_MAIN
#define TITLE ("hl2_tcp ")
#define SOCKET_READ_TIMEOUT ( 30.0 * 60.0 ) // 30 minutes in seconds
// #define SAMPLE_BITS ( 8) // default to match rtl_tcp
#define SAMPLE_BITS (16) // default to match rsp_tcp
// #define SAMPLE_BITS (32) // HPSDR 24-bit->float32 IQ data ?
// #define GAIN8 (4096.0) // default gain ?
#define TCP_PORT (1234) // default rtp_tcp server port
#define HERMES_PORT (1024) // UDP port
#define RING_BUFFER_ALLOCATION (2L * 1024L * 1024L) // 2MB
#define MAX_NUMBER_OF_START_COMMANDS (8)
#define START_LOOP_DELAY (50000) // microseconds
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <inttypes.h>
#include <unistd.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/time.h>
#ifndef _UNISTD_H_
int usleep(unsigned long int usec);
#endif
#ifndef M_PI
#define M_PI (3.14159265358979323846264)
typedef void *caddr_t;
#endif
static void sighandler(int signum);
void *tcp_connection_handler(void); // main thread
void *udp_rcv_thread_runner(void *param); // on a pthread
int discover_main(void);
void hl2_stop(void);
void print_hl2_stats(void) ;
void setFilterEnables(void) ;
uint32_t hl2_ipAddr = 0x7f000001; // 127.0.0.1 == localhost
int portno = TCP_PORT; //
int hl2_running = 0; // Metis response
int udp_running = 0; // udp socket live
int C0_addr = 0; //
int hl2_sampRate = 192000; // 384000;
int hl2_sampleBits = SAMPLE_BITS;
volatile int hermes_rx_freq = 14100000; // 100k to 30M
volatile int hermes_rx2_freq = 14110000; //
volatile int hermes_rx3_freq = 14120000; //
volatile int hermes_tx_freq = 0; //
volatile int hermes_tx_offset = 0; //
volatile int hermes_lna_gain = 19; // LNA gain -12 to +48 dB
int hermes_tx_drive_level = 0; // 0 .. 15
int hermes_enable_power_amp = 0; //
int hermes_Q5_switch_ext_ptt_lp = 0;
int tx_gear_ratio = 1;
int tx_gear_counter = 0;
volatile int seqNum = 0; // tx sequence number
int hl2_rcvSeqNum = 0;
int hl2_ep6 = 0;
// Rx and Tx filters; default no HPF ?
int n2adr_filter_rx = 0;
int n2adr_filter_tx = 0;
int sendErrorFlag = 0;
// int sampleBits = SAMPLE_BITS;
int sampleRates = 1;
long int totalSamples = 0;
long sampRate = 192000; // 48000 to 384000
long previousSRate = -1;
int gClientSocketID = -1;
char *ring_buffer_ptr = NULL;
int decimateFlag = 1;
int decimateCntr = 0;
volatile float gain0 = 32768.0; // 8.0 * GAIN8;
struct sigaction sigact, sigign;
int tcp_listen_sockfd = -1;
static volatile int do_exit = 0;
float acc_r = 0.0; // accumulated rounding
float sMax = 0.0; // for debug
float sMin = 0.0;
int sendblockcount = 0;
int threads_running = 0;
char *hl2_ip_string = "127.0.0.1"; // 0x7f000001 == localhost
// for TX_OK
long int tcp_tx_cmd = 0;
int hl2_tx_on = 0; // includes hang
int tx_key_down_1 = 0; // Morse code key for hang
int tx_delay = 0; // tx/ptt hold time
int last_key_down = 0;
int tx_param_x = 0;
int tx_param_w = 0;
int tx_dot_offset = 0; // dot Tx df frequency offset
float hl2_tx_lvl = 0.0;
int hl2_tx_drive = 0;
#ifdef TX_OK
// external transmit data routines
extern void tx_setup();
extern void tx_cleanup();
extern int get_tx_key(void) ; // sets MOX bit
extern void tx_block_setup(int seqN); // prepare 2*63 samples
extern void get_tx_sample(int *tx_i, int *tx_q) ;
extern int get_tx_drive(void) ; // 0 .. 15
// extern int get_tx_offset() { return(tx_dot_offset); } // f0 df
extern void resetTxDotQueue() ;
extern int queueTxDotCommand(int k, int on, int off) ;
extern void set_tx_offset(float df);
extern void set_tx_level(float v);
extern void set_tx_drive(float d);
#else
// dummy stubs
void tx_setup() { return; }
void tx_cleanup() { return; }
#define get_tx_key() (0)
#define tx_block_setup(X) // nop
#define get_tx_sample(X,Y) // nop
#define get_tx_drive() (0)
#define get_tx_offset() (0)
#define resetTxDotQueue() (0)
static int queueTxDotCommand(int k, int on, int off) { return 0; }
#endif
char UsageString1[]
= "Usage: hl2_tcp -a hermes_IPaddr [-p local_port] [-b 8/16]";
char UsageString2[]
= " hl2_tcp -d ";
void print_usage_and_exit()
{
printf("%s\n", UsageString1);
printf("%s\n", UsageString2);
exit(0);
}
int hl2_tcp(void);
#ifndef NO_MAIN
// int tcp_main(int argc, char *argv[])
int main(int argc, char *argv[])
{
if (argc > 1) {
if (strcmp(argv[1], "-d") == 0) {
if (argc > 2) {
hl2_ip_string = argv[2];
discover_use_ip = 1;
} else {
}
int e = discover_main();
return(e);
}
if ((argc % 2) != 1) {
print_usage_and_exit();
}
for (int arg=3; arg<=argc; arg+=2) {
if (strcmp(argv[arg-2], "-p") == 0) {
portno = atoi(argv[arg-1]);
if (portno == 0) {
printf("invalid port number entry %s\n", argv[arg-1]);
exit(0);
}
} else if (strcmp(argv[arg-2], "-b") == 0) {
if (strcmp(argv[arg-1],"16") == 0) {
hl2_sampleBits = 16;
} else if (strcmp(argv[arg-1],"8") == 0) {
hl2_sampleBits = 8;
} else {
print_usage_and_exit();
}
} else if (strcmp(argv[arg-2], "-a") == 0) {
hl2_ip_string = argv[arg-1];
} else if (strcmp(argv[arg-2], "-x") == 0) {
tx_param_x = atoi(argv[arg-1]);
} else {
print_usage_and_exit();
}
}
} else {
print_usage_and_exit();
}
return(hl2_tcp());
}
#endif
int hl2_tcp()
{
struct sockaddr_in6 serv_addr ;
char client_addr_ipv6[100];
printf("hl2_tcp Version %s\n", VERSION);
printf("Will look for Hermes Lite 2 at IP: %s UDP Port: %d \n",
hl2_ip_string, HERMES_PORT);
ring_buffer_ptr = (char *)malloc(RING_BUFFER_ALLOCATION + 4);
if (ring_buffer_ptr == NULL) { exit(-1); }
bzero(ring_buffer_ptr, RING_BUFFER_ALLOCATION + 2);
printf("Converts Metis to rtl_tcp format %d-bit IQ samples \n",
hl2_sampleBits);
printf("Starting hl2_tcp server on TCP port %d\n", portno);
tx_setup();
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
#ifdef __APPLE__
signal(SIGPIPE, SIG_IGN);
#else
sigign.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sigign, NULL);
#endif
previousSRate = sampRate;
// printf("\nhl2_tcp server started on port %d\n", portno);
tcp_listen_sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if (tcp_listen_sockfd < 0) {
printf("ERROR opening socket");
return(-1);
}
struct linger ling = {1,0};
int rr = 1;
setsockopt(tcp_listen_sockfd, SOL_SOCKET, SO_REUSEADDR,
(char *)&rr, sizeof(int));
setsockopt(tcp_listen_sockfd, SOL_SOCKET, SO_LINGER,
(char *)&ling, sizeof(ling));
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_port = htons(portno);
// Sockets Layer Call: bind()
if (bind( tcp_listen_sockfd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr) ) < 0) {
printf("ERROR on bind to listen\n");
return(-1);
}
listen(tcp_listen_sockfd, 5);
fprintf(stdout, "listening for socket connection \n");
while (1) {
// accept a connection
struct sockaddr_in6 cli_addr;
socklen_t claddrlen = sizeof(cli_addr);
gClientSocketID = accept( tcp_listen_sockfd,
(struct sockaddr *) &cli_addr,
&claddrlen );
if (gClientSocketID < 0) {
printf("ERROR on accept\n");
break;
}
inet_ntop(AF_INET6, &(cli_addr.sin6_addr), client_addr_ipv6, 100);
printf("\nConnected to client with IP address: %s\n",
client_addr_ipv6);
tcp_connection_handler();
printf("tcp connection ended \n");
}
udp_running = -1;
hl2_stop();
tx_cleanup();
fflush(stdout);
return 0;
} // main
static void sighandler(int signum)
{
fprintf(stderr, "Signal caught, exiting!\n");
fflush(stderr);
close(tcp_listen_sockfd);
if (gClientSocketID != 0) {
close(gClientSocketID);
gClientSocketID = -1;
}
udp_running = -1;
hl2_stop();
tx_cleanup();
exit(-1);
// do_exit = 1;
}
int stop_send_thread = 0;
int thread_counter = 0;
int thread_running = 0;
// circular buffer / ring buffer
// char *ring_buffer_ptr = NULL;
int ring_buffer_size = RING_BUFFER_ALLOCATION;
volatile long int ring_wr_count = 0;
volatile long int ring_wr_index = 0;
volatile long int ring_rd_index = 0;
void ring_init()
{
ring_wr_count = 0;
ring_wr_index = 0;
ring_rd_index = 0;
}
int ring_data_available()
{
long int n = 0;
long int w_index = ring_wr_index; //
long int r_index = ring_rd_index; //
n = w_index - r_index;
if (n < 0) { n += ring_buffer_size; }
if (n < 0) { n = 0; } // error condition ?
if (n >= ring_buffer_size) { n = 0; } // error condition
return(n);
}
int ring_write(unsigned char *from_ptr, int amount)
{
int wrap = 0;
long int w_index = ring_wr_index; // my index
long int r_index = ring_rd_index; // other threads index
if ( ring_buffer_ptr == NULL ) { return(-1); }
if ( (w_index < 0)
|| (w_index >= ring_buffer_size) ) { return(-1); } // error !
if (decimateFlag > 1) {
int i;
for (i = 0; i < amount; i += 2) {
if (decimateCntr == 0) {
ring_buffer_ptr[w_index ] = from_ptr[i ];
ring_buffer_ptr[w_index+1] = from_ptr[i+1];
w_index += 2;
if (w_index >= ring_buffer_size) { w_index = 0; }
}
decimateCntr += 1;
if (decimateCntr >= decimateFlag) { decimateCntr = 0; }
}
} else if (w_index + amount < ring_buffer_size) {
memcpy(&ring_buffer_ptr[w_index], from_ptr, amount);
w_index += amount;
} else {
int i;
for (i = 0; i < amount; i += 1) {
ring_buffer_ptr[w_index] = from_ptr[i];
w_index += 1;
if (w_index >= ring_buffer_size) { w_index = 0; }
}
}
//
/// ToDo: insert memory barrier here
//
ring_wr_index = w_index; // update lock free input info
int m = ring_data_available();
if (m > ring_buffer_size/2) { wrap = 1; }
ring_wr_count += amount; // assume no decimate
return(wrap);
}
int ring_read(unsigned char *to_ptr, int amount, int always)
{
int bytes_read = 0;
long int r_index = ring_rd_index; // my index
long int w_index = ring_wr_index; // other threads index
long int available = w_index - r_index;
if (available < 0) { available += ring_buffer_size; }
if ( ring_buffer_ptr == NULL ) { return(-1); }
if ( to_ptr == NULL ) { return(-1); }
if (always != 0) {
bzero(to_ptr, amount);
}
if (available <= 0) { return(bytes_read); }
long int n = amount;
if (n > available) { n = available; } // min(n, available)
if (r_index + n < ring_buffer_size) {
memcpy(to_ptr, &ring_buffer_ptr[r_index], n);
r_index += n;
} else {
int i;
for (i = 0; i < n; i += 1) {
to_ptr[i] = ring_buffer_ptr[r_index];
r_index += 1;
if (r_index >= ring_buffer_size) { r_index = 0; }
}
}
bytes_read = n;
ring_rd_index = r_index; // update lock free extract info
return(bytes_read);
}
float tmpFPBuf[4*32768];
uint8_t tmpBuf[ 4*32768];
long int *param = NULL;
int tcp_send_poll()
{
int sz0 = 1408; // MTU size or 1008 ??
int pad = 32768 * 2;
ssize_t b = 0;
int send_sockfd = gClientSocketID ;
if (send_sockfd > 0) {
if (ring_data_available() >= (sz0 + pad)) {
int sz = ring_read(tmpBuf, sz0, 0);
if (sz > 0) {
if (totalSamples == 0) {
// fprintf(stderr, "hl2 udp IQ data received \n");
}
#ifdef __APPLE__
b = send(send_sockfd, tmpBuf, sz, 0);
#else
b = send(send_sockfd, tmpBuf, sz, MSG_NOSIGNAL);
#endif
if (b <= 0) { sendErrorFlag = -1; }
if (totalSamples == 0) {
fprintf(stderr,
"Started rtl_tcp IQ streaming with %ld bytes\n", b);
}
totalSamples += sz;
sendblockcount += 1;
}
pad = 0;
}
}
return(b);
}
int hl2_udp_setup(void);
void hl2_send_start_cmds(int n);
void hl2_udp_rcv(int loopFlag);
int hl2_udp_rcv_end(void);
void *tcp_connection_handler()
{
char buffer[256];
int m = 0;
int printCmdBytes = 0; //
int i;
if (do_exit != 0) { return(NULL); }
if (1) { // send 12 or 16-byte rtl_tcp header
ssize_t b = 0;
int sz = 16;
if (hl2_sampleBits == 8) { sz = 12; }
// "HL20" in 16 byte header
char header[16] = { 0x48,0x4C,0x32,0x30,
0x30,0x30,0x30+sampleRates,0x30+hl2_sampleBits,
0,0,0,1, 0,0,0,2 };
#ifdef __APPLE__
b = send(gClientSocketID, header, sz, 0);
#else
b = send(gClientSocketID, header, sz, MSG_NOSIGNAL);
#endif
/*
fprintf(stdout, "rtl_tcp header sent , %d bytes\n", n); //
fflush(stdout);
*/
}
sendErrorFlag = 0;
stop_send_thread = 0;
ring_wr_index = 0;
ring_rd_index = 0;
param = (long int *)malloc(4 * sizeof(long int)); /// ToDo: fix leak
for (i=0;i<4;i++) { param[i] = 0; }
if (hl2_running == 0) {
ring_init();
resetTxDotQueue() ;
hl2_udp_setup();
hl2_send_start_cmds(1);
pthread_t udp_rcv_thread;
if ( pthread_create( &udp_rcv_thread,
NULL ,
udp_rcv_thread_runner,
(void *)param ) < 0 ) {
printf("could not create udp streaming thread");
return(NULL);
} else {
printf("udp streaming thread started \n");
}
int n = MAX_NUMBER_OF_START_COMMANDS;
hl2_send_start_cmds(n);
}
acc_r = 0.0;
totalSamples = 0;
// printf("hl2 start status = %d\n", m);
if (m < 0) { exit(-1); }
usleep(250L * 1000L);
// set a timeout so receive call won't block forever
struct timeval timeout;
timeout.tv_sec = SOCKET_READ_TIMEOUT; // seconds
timeout.tv_usec = 0;
setsockopt( gClientSocketID, SOL_SOCKET, SO_RCVTIMEO,
&timeout, sizeof(timeout) );
ssize_t b = 1;
while ((b > 0) && (sendErrorFlag == 0)) {
int i, j;
// receive 5 byte commands (or a multiple thereof)
memset(buffer,0, 256);
b = recv(gClientSocketID, buffer, 255, 0);
if ((b <= 0) || (sendErrorFlag != 0)) {
udp_running = -1;
close(gClientSocketID);
gClientSocketID = -1;
// fprintf(stdout, "hl2 stop status = %d\n", m);
fflush(stdout);
break;
}
if (b > 0) {
for (i=0; i < b; i+=5) {
// decode 5 byte rtl_tcp command messages
int msg = 0x00ff & buffer[i];
if (printCmdBytes) { printf("0x%02x ", msg); }
int data = 0;
for (j=1;j<5;j++) {
int byte = (0x00ff & buffer[i+j]);
data = (256 * data) + byte;
if (printCmdBytes) { printf("0x%02x ", byte); }
}
if (printCmdBytes) { printf(" = %d\n", data); }
if (msg == 0x01) { // set frequency
int f0 = data;
if (f0 >= 100000 && f0 <= 30000000) {
hermes_rx_freq = f0; // hl2
setFilterEnables();
// hermes_tx_offset = get_tx_offset();
}
fprintf(stdout, "setting frequency to: %d\n", f0);
} else if (msg == 0x02) { // set sample rate
int r = data;
if ( (r == 48000)
|| (r == 96000)
|| (r == 192000)
|| (r == 384000)) {
// if (r != previousSRate) {
sampRate = r;
tx_gear_ratio = r / 48000;
printf("setting samplerate to : %d\n", r);
// }
} else {
// ignore
}
} else if (msg == 0x03) { // other
fprintf(stdout, "message = %d, data = %d\n", msg, data);
} else if (msg == 0x04) { // gain
if ( (hl2_sampleBits == 8)
|| (hl2_sampleBits == 16) ) {
// from set gain 0 to 400
float g1 = data; // data : in 10th dB's
float g2 = 0.1 * (float)(data); // undo 10ths
// fprintf(stdout, "setting gain to: %f dB\n", g2);
float g2h = 1.50 * g2 ;
int g9 = 0.0 + roundf(g2h - 12.0);
// hermes_lna_gain : LNA gain -12 to +48 dB
if (g9 < -12) { g9 = -12; }
if (g9 > 48) { g9 = 48; }
hermes_lna_gain = g9;
fprintf(stdout, "set hl2 lna gain to : %d\n", g9);
}
#ifdef TX_OK
} else if (msg == 77) { // 0x4d tx command extension
// command added 2022-12
if (data < 256) {
tcp_tx_cmd = 3 * data; // 381 = 127 * 3
} else {
int data0 = (0xff000000L & data) >> 24;
int data1 = (0x00ff0000L & data) >> 16;
int data2 = (0x0000ff00L & data) >> 8;
int data3 = (0x000000ffL & data) ;
int k = 0;
if (data0 == 68) { // 'D'
// convert milliseconds to Tx samples
int on = 48 * data1;
int off = 48 * data2;
if (on == 0 && off == 0) {
k = 0;
queueTxDotCommand(k, on, off) ;
} else {
k = 1;
queueTxDotCommand(k, on, off) ;
}
}
if (data0 == 70) { // 'F'
// convert to Hz for f0 offset
if (data3 > 127) { data3 = data3 - 256; }
float f0 = 10 * data3;
tx_dot_offset = f0;
set_tx_offset(f0);
}
if (data0 == 76) { // 'L'
float v = 128 * data3;
hl2_tx_lvl = v;
set_tx_level(v);
#ifdef DEBUG_MODE_0
fprintf(stderr, "set tx level %f \n", v);
#endif
}
if (data0 == 66) { // 'B' drive
float d = data3;
hl2_tx_drive = d;
set_tx_drive(d);
#ifdef DEBUG_MODE_0
fprintf(stderr, "set drive %f \n", d);
#endif
}
}
// printf("tcp_tx_cmd = %d \n", tcp_tx_cmd); //
#endif
} else { // other
fprintf(stdout, "message = %d, data = %d\n", msg, data);
if (msg == 8) {
fprintf(stdout, "set agc mode ignored\n");
}
}
}
}
if (b < 0) {
fprintf(stdout, "read socket timeout %ld \n", b);
fflush(stdout);
}
// loop until error (socket close) or timeout
} ;
if (m) {
fprintf(stdout,"stopping now 00 \n");
printf("hl2 stop status = %d\n", m);
}
udp_running = -1;
close(gClientSocketID);
gClientSocketID = -1;
return(param);
} // tcp_connection_handler()
// uint8_t tmpBuf[4*32768];
typedef union
{
uint32_t i;
float f;
} Float32_t;
float rand_float_co()
{
Float32_t x;
x.i = 0x3f800000 | (rand() & 0x007fffff);
return(x.f - 1.0f);
}
//
//
#define FILTER_HPF (0x40)
#define FILTER_160 (0x01)
#define FILTER_80 (0x02)
#define FILTER_40 (0x04)
#define FILTER_30_20 (0x08)
#define FILTER_17_15 (0x10)
#define FILTER_10 (0x20)
struct sockaddr_in recv_Addr;
socklen_t addrLen = sizeof(recv_Addr);
unsigned char udpBuffer[1600]; // Original Protocol Command & Control
//
double samp_db = 0.0;
float rnd0v = 0.0;
float rnd0u = 0.0;
float tmp_temperature = 0.0;
float tmp_fwd_power = 0.0;
float tmp_rev_power = 0.0;
float tmp_pa_current = 0.0;
int tmp_temp_count = 0;
int tmp_revp_count = 0;
float hermes_temperature = 0;
float hermes_fwd_power = 0;
float hermes_rev_power = 0;
float hermes_pa_current = 0;
int hwCmdState = 0;
int hwCmdSeqNum = 0;
unsigned char hwCmd[8] = { 0,0,0,0,0 }; // 1+4 fpga command bytes
void *udp_rcv_thread_runner(void *param)
{
if (hl2_running == 0) {
hl2_udp_rcv(1);
hl2_udp_rcv_end();
}
return(param);
}
// extract command replies, status, and 24-bit IQ
// transcode to 8 or 16 bit IQ
int hl2_adcOverload ;
int hl2_txfifoLevel ;
int firmwareVersion ;
double slice2Pwr = 0.0;
int hl2_clip8_flag = 0;
float hl2_tx_hang = 0.2; // seconds
int hl2_tx_mode = 0;
int last_t1 = -1;
int handleRcvData(unsigned char *hl2Buf, int n)
{
unsigned char *buf = hl2Buf;
int j, jj;
int rcvSeqNum;
double samp_m2 = 0.0;
unsigned char uv[1024];
hl2_clip8_flag = 0;
// gain scaling adjustments
// 012345670123456701234567 HPDSR 24 bits
// ============ ADC 12 bits
// ---------------- TCP 16 bits
// -------- TCP 8 bits
float g8_r = 1.0 / 65536.0; // shift down 16 bits
float g16_r = 1.0 / 4096.0; // shift down 12 bits
int headerOK = ( (buf[ 0] == 0xEF)
&& (buf[ 1] == 0xFE)
&& (buf[ 2] == 0x01) );
int ep = buf[ 3] ;
if (ep != 6) {
printf("******** ep error \n" );
return(0);
}
int syncErr = ( (buf[11 - 3] != 0x7F)
|| (buf[11 - 2] != 0x7F)
|| (buf[11 - 1] != 0x7F));
if (syncErr) { return(-1); }
rcvSeqNum = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | (buf[7]);
if (rcvSeqNum != (hl2_rcvSeqNum + 1)) {
hl2_ep6 += 1;
// printf("err %d + 1 != %d \n", hl2_rcvSeqNum , rcvSeqNum);
}
hl2_rcvSeqNum = rcvSeqNum;
for (jj=0; jj<1024; jj+=512) {
int replyBit = (buf[jj+11] & 0x80) ;
int dt = rcvSeqNum - hwCmdSeqNum;
if (replyBit) {
int i;
int cmdEcho = (buf[jj+11] & 0x7F) >> 1;
// printf("******** # %d hw cmd ack 0x%02x : ", dt, cmdEcho);
// for (i=0;i<5;i++) { printf("0x%02x ", buf[jj+11+i]); }
// printf("\n");
cmdEcho = hwCmd[0]; /// ToDo: test or fix
if ( cmdEcho == hwCmd[0] ) {
hwCmdState = 0;
// command acknowledged ?
// hwCmdSeqNum = rcvSeqNum;
}
if (buf[jj+11] == 0xff) {
hwCmdState = -1;
// fprintf(stdout,"******** hw cmd error \n" );
}
}
}
int dtype0 = buf[11 ] >> 3;
int dtype1 = buf[11+512] >> 3;
if (dtype0 == 0) {
int jj = 0;
int kk = 11+jj;
hl2_txfifoLevel = ( (buf[kk+1] << 24)
| (buf[kk+2] << 16)
| (buf[kk+3] << 8)
| (buf[kk+4] ) );
hl2_adcOverload = buf[11+jj+1] ;
// hl2_txfifoLevel = buf[11+jj+3] ;
firmwareVersion = buf[11+jj+4] ;
}
if (dtype1 == 0) {
int jj = 512;
int kk = 11+jj;
hl2_txfifoLevel = ( (buf[kk+1] << 24)
| (buf[kk+2] << 16)
| (buf[kk+3] << 8)
| (buf[kk+4] ) );
hl2_adcOverload = buf[11+jj+1] ;
// hl2_txfifoLevel = buf[11+jj+3] ;
firmwareVersion = buf[11+jj+4] ;
}
if (dtype0 == 1) {
int jj = 0;
tmp_temperature += ((buf[11+jj+1] << 8) | (buf[11+jj+2]));
tmp_fwd_power += ((buf[11+jj+3] << 8) | (buf[11+jj+4]));
tmp_temp_count += 1;
}
if (dtype1 == 1) {
int jj = 512;
tmp_temperature += ((buf[11+jj+1] << 8) | (buf[11+jj+2]));
tmp_fwd_power += ((buf[11+jj+3] << 8) | (buf[11+jj+4]));
tmp_temp_count += 1;
}
if (dtype0 == 2) {
tmp_rev_power += ((buf[11 + 1] << 8) | (buf[11 + 2]));
tmp_pa_current += ((buf[11 + 3] << 8) | (buf[11 + 4]));
tmp_revp_count += 1;
}
if (dtype1 == 2) {
int jj = 512;
tmp_rev_power += ((buf[11+jj+1] << 8) | (buf[11+jj+2]));
tmp_pa_current += ((buf[11+jj+3] << 8) | (buf[11+jj+4]));
tmp_revp_count += 1;
}
// 1 slice 8..15 step by 8
// <I2><I1><I0><Q2><Q1><Q0><M1><M0>
// 2 slices 8..21 step by 14
// <I12><I11><I10><Q12><Q11><Q10><I22><I21><I20><Q22><Q21><Q20><M1><M0>
int sfc = 8;
if (slices == 2) { sfc = 14; }
double scale = 32768.0 * 32768.0 * 2.0; // 33 bit shift
double scale_r = 1.0 / scale;
int16_t *tmp16ptr = (int16_t *)&uv[0];
int k = 0;
int kk = 0;
for (jj=0; jj<1024; jj+=512) {
for (j=8+jj+8; j<8+jj+512; j += 8) {
int imagp1, realp1;
double u1,v1;
// reversed order IQ
imagp1 = buf[j ] << 24 | buf[j + 1] << 16 | buf[j + 2] << 8;
realp1 = buf[j + 3] << 24 | buf[j + 4] << 16 | buf[j + 5] << 8;
v1 = realp1;
u1 = imagp1;
if (slices == 2) {
int imagp2, realp2;
double u2,v2;
// reversed order IQ
imagp2 = buf[j+6] << 24 | buf[j+7 ] << 16 | buf[j+8 ] << 8;
realp2 = buf[j+9] << 24 | buf[j+10] << 16 | buf[j+11] << 8;
// sddddddd ddddx000 00000000
// 00000000 ssssdddd dddddddx
/// ToDo: // currently 2nd slice data is unused
}
// transmitter duplex gain reductions
if (hl2_tx_on > 0) {
if (hl2_tx_mode == 55) { // ssb
v1 *= 10.0;
u1 *= 10.0;
}
if (hl2_tx_lvl > 255) { // relative to Tx power
v1 /= (hl2_tx_lvl + 1.0) / 256.0; // ???
u1 /= (hl2_tx_lvl + 1.0) / 256.0;
}
}
if (hl2_sampleBits == 16) { // for HERMES !!!
float v2 = g16_r * v1;
float u2 = g16_r * u1;
int vv = (int)roundf(v2);
int uu = (int)roundf(u2);
tmp16ptr[kk ] = vv;
tmp16ptr[kk+1] = uu;
kk += 2; // 2 16-bit samples
k += 4; // is 4 bytes
} else { // convert to 8-bit samples for rtl_tcp compatibility
// magnitude for testing
samp_m2 += u1*u1 + v1*v1;
// scale and add triangular noise shaping (dither)
float vv = g8_r * v1;
float rnd1v = rand_float_co();
float rv = rnd1v - rnd0v;
vv = vv + rv;
float rvv = roundf(vv);
int vi = (int)rvv;
rnd0v = rnd1v;
float uu = g8_r * u1;
float rnd1u = rand_float_co();
float ru = rnd1u - rnd0u;
uu = uu + ru;
float ruu = roundf(uu);
int ui = (int)ruu;
rnd0u = rnd1u;
if (vi > 127) { vi = 127; hl2_clip8_flag++; }
if (vi < -128) { vi = -128; hl2_clip8_flag++; }
if (ui > 127) { vi = 127; hl2_clip8_flag++; }
if (ui < -128) { vi = -128; hl2_clip8_flag++; }
// unsigned 8-bit samples
uv[k] = vi + 128;
uv[k+1] = ui + 128;
k += 2; // 2 bytes for 8-bit IQ
}
}
}
ring_write(&uv[0], k);
//
int nSamples = (2 * (512-8))/4; // == 1008/4 == 252
double samp_rms = sqrt(samp_m2 / nSamples) / scale;
if (samp_rms > 0.0) {
samp_db = 20.0 * log10(samp_rms);
}
//
if (tmp_temp_count >= 16) {
hermes_temperature = tmp_temperature / (float)tmp_temp_count;