-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrtl_hpsdr.c
1257 lines (1005 loc) · 33.1 KB
/
rtl_hpsdr.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
/* This file rtl_hpsdr.c is part of rtl_hpsdr.
*
* rtl_hpsdr - an RTL to HPSDR software translation server
* Copyright (C) 2014 Richard Koch
*
* rtl_hpsdr 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.
*
* rtl_hpsdr 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 rtl_hpsdr. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rtl_hpsdr.h"
struct main_cb mcb;
static pthread_mutex_t iqready_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t iqready_cond = PTHREAD_COND_INITIALIZER;
static u_int rcvr_flags = 0;
static pthread_mutex_t send_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t send_cond = PTHREAD_COND_INITIALIZER;
static u_int send_flags = 0;
static pthread_mutex_t done_send_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t done_send_cond = PTHREAD_COND_INITIALIZER;
static pthread_t rtl_read_thr[MAX_RCVRS];
static pthread_t hpsdrsim_sendiq_thr[MAX_RCVRS];
static int num_copy_rcvrs = 0, do_exit = 0;
static int copy_rcvr[MAX_RCVRS];
static u_char last_num_rcvrs = 0;
static u_char last_rate = 0;
static int last_freq[MAX_RCVRS] = { 0, };
static int common_freq = 0;
static int reveal_socket;
static struct sockaddr_in my_addr;
static socklen_t my_length;
static struct sockaddr_in their_addr;
static socklen_t their_length;
static int nsamps_packet[8] = { 126, 72, 50, 38, 30, 26, 22, 20 };
static int frame_offset1[8] = { 520, 520, 516, 510, 496, 510, 500, 516 };
static int frame_offset2[8] =
{ 1032, 1032, 1028, 1022, 1008, 1022, 1012, 1028 };
static int revealed = 0;
static int running = 0;
static unsigned char hw_address[6];
static long ip_address;
static char server_ip_address[16];
static u_char buffer[MAX_BUFFER_LEN];
static u_char payload[HPSDR_FRAME_LEN];
static u_int hpsdr_sequence = 0;
static u_int pc_sequence;
static float rtl_lut[256];
void
rtl_sighandler(int signum) {
printf("Signal caught, exiting!\n");
do_exit = 1;
hpsdrsim_stop_threads();
}
#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
int
get_addr(int sock, char* ifname) {
struct ifreq* ifr;
struct ifreq ifrr;
struct sockaddr_in sa;
unsigned char* u;
int i;
ifr = &ifrr;
ifrr.ifr_addr.sa_family = AF_INET;
strncpy(ifrr.ifr_name, ifname, sizeof(ifrr.ifr_name));
if(ioctl(sock, SIOCGIFADDR, ifr) < 0) {
printf("No %s interface.\n", ifname);
return -1;
}
ip_address = inaddrr(ifr_addr.sa_data).s_addr;
if(ioctl(sock, SIOCGIFHWADDR, ifr) < 0) {
printf("No %s interface.\n", ifname);
return -1;
}
u = (unsigned char*) &ifr->ifr_addr.sa_data;
for(i = 0; i < 6; i++)
hw_address[i] = u[i];
return 0;
}
void
hpsdrsim_reveal(void) {
int rc, bytes_read;
int i, on = 1;
u_char init_buffer[19] =
{ 0xEF, 0xFE, 2 + running, 0, 0, 0, 0, 0, 0, HERMES_FW_VER, 1,
'R', 'T', 'L', '_', 'N', '1', 'G', 'P' }; // special ID for SkimSrv
char s_ip[16];
bool ready = false;
printf("Revealing myself as a Hermes rcvr.\n");
reveal_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(reveal_socket < 0) {
perror("create socket failed for reveal_socket\n");
exit(1);
}
rc = setsockopt(reveal_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if(rc != 0) {
printf("cannot set SO_REUSEADDR: rc=%d\n", rc);
exit(1);
}
// get my MAC address and IP address
if(get_addr(reveal_socket, mcb.net_dev) < 0) {
exit(1);
}
sprintf(s_ip, "%ld.%ld.%ld.%ld",
ip_address & 0xFF,
(ip_address >> 8) & 0xFF,
(ip_address >> 16) & 0xFF, (ip_address >> 24) & 0xFF);
printf("%s IP Address: %s\n", mcb.net_dev, s_ip);
printf("%s MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
mcb.net_dev,
hw_address[0], hw_address[1], hw_address[2], hw_address[3],
hw_address[4], hw_address[5]);
for(i = 0; i < 6; i++)
init_buffer[3 + i] = hw_address[i];
// bind to this interface
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(reveal_socket, (struct sockaddr*) &my_addr, sizeof(my_addr)) < 0) {
perror("1 bind socket failed for reveal_socket");
exit(1);
}
// allow broadcast on the socket
rc = setsockopt(reveal_socket, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
if(rc != 0) {
printf("cannot set SO_BROADCAST: rc=%d\n", rc);
exit(1);
}
their_length = sizeof(their_addr);
memset(&their_addr, 0, their_length);
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
// Get discovered
while(!ready) {
if((bytes_read = recvfrom(reveal_socket, buffer, sizeof(buffer), 0,
(struct sockaddr*) &their_addr, &their_length)) < 0) {
if(!do_exit)
printf("Bad recvfrom, NOT discovered!");
exit(0);
} //else
//printf("Received %d bytes, waiting to be discovered...\n", bytes_read);
// the discovery process is complete, continue on
if((bytes_read > 1000) && revealed) {
ready = true;
continue;
}
if(buffer[0] == 0xEF && buffer[1] == 0xFE && buffer[2] == 0x02) {
strcpy(server_ip_address, inet_ntoa(their_addr.sin_addr));
printf("Was discovered by %s\n", server_ip_address);
// Send acknowledgement of discovery
for(i = 0; i < sizeof(init_buffer); i++) {
buffer[i] = init_buffer[i];
}
// the 49 trailing bytes are undefined, HermesIntf.dll
// will use the RTL_N1GP emulation ID and the dongle count
buffer[sizeof(init_buffer)] = mcb.total_num_rcvrs;
for(i = sizeof(init_buffer) + 1; i < 60; i++) {
buffer[i] = 1;
}
if(sendto(reveal_socket, buffer, 60, 0, (struct sockaddr*) &their_addr,
sizeof(their_addr)) < 0) {
perror("sendto() failed sending acknowledgement!");
exit(1);
} else
printf("Sent discovery acknowledgement.\n");
revealed = 1;
} else {
//printf("Was NOT discovered by %s\n", inet_ntoa(their_addr.sin_addr));
}
}
if((reveal_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("socket() reveal_socket error!");
exit(1);
}
my_length = sizeof(my_addr);
memset(&my_addr, 0, my_length);
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
rc = setsockopt(reveal_socket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if(rc != 0) {
printf("cannot set SO_REUSEADDR: rc=%d\n", rc);
exit(1);
}
if(bind(reveal_socket, (struct sockaddr*) &my_addr, my_length) < 0) {
perror("2 bind socket failed for reveal_socket");
exit(1);
}
rc = pthread_create(&hpsdrsim_thread_id, NULL, hpsdrsim_thread, NULL);
if(rc != 0) {
printf("pthread_create failed on hpsdr_thread: rc=%d\n", rc);
exit(1);
}
}
void
load_packet(struct rcvr_cb* rcb) {
int b, i, j, k, copy_total = (mcb.active_num_rcvrs - 1) + num_copy_rcvrs;
// if we need to copy a receiver we'll choose the last active 'real' one
bool do_copy = ((num_copy_rcvrs > 0)
&& (rcb->rcvr_num == mcb.active_num_rcvrs - 1));
int offset1 = (num_copy_rcvrs > 0) ? frame_offset1[copy_total] : mcb.frame_offset1;
int offset2 = (num_copy_rcvrs > 0) ? frame_offset2[copy_total] : mcb.frame_offset2;
int offsetx;
float* out_buf = &rcb->iqSamples[rcb->iqSample_offset * 2];
short IQdata;
i = 0;
k = 0;
b = 16;
// insert data in lower and upper bank for each of the receivers
while(k++ < 2) {
offsetx = (i == 0) ? offset1 : offset2;
while(b < offsetx) {
for(j = 0; j < mcb.active_num_rcvrs + num_copy_rcvrs; j++) {
if((j == rcb->rcvr_num) || (do_copy && copy_rcvr[j] == j)) {
IQdata = (short) out_buf[i];
payload[b++] = IQdata >> 8;
payload[b++] = IQdata & 0xff;
payload[b++] = 0;
IQdata = (short) out_buf[i + 1];
payload[b++] = IQdata >> 8;
payload[b++] = IQdata & 0xff;
payload[b++] = 0;
if(do_copy) {
if(j == copy_total)
i += 2;
} else {
i += 2;
}
} else {
b += 6;
}
}
b += 2; // skip mic data
}
b = 528;
}
pthread_mutex_lock(&send_lock);
send_flags |= rcb->rcvr_mask;
pthread_cond_broadcast(&send_cond);
pthread_mutex_unlock(&send_lock);
pthread_mutex_lock(&done_send_lock);
while(send_flags & rcb->rcvr_mask) {
pthread_cond_wait(&done_send_cond, &done_send_lock);
}
pthread_mutex_unlock(&done_send_lock);
}
void*
hpsdrsim_sendiq_thr_func(void* arg) {
int samps_packet;
struct rcvr_cb* rcb = (struct rcvr_cb*) arg;
rcb->iqSample_offset = rcb->iqSamples_remaining = 0;
//printf("ENTERING hpsdrsim_sendiq_thr_func() rcvr %d...\n", rcb->rcvr_num+1);
while(!do_exit) {
samps_packet = (num_copy_rcvrs > 0)
? nsamps_packet[(mcb.active_num_rcvrs - 1) + num_copy_rcvrs]
: mcb.nsamps_packet;
pthread_mutex_lock(&iqready_lock);
while(!(rcvr_flags & rcb->rcvr_mask)) {
pthread_cond_wait(&iqready_cond, &iqready_lock);
}
rcvr_flags &= ~rcb->rcvr_mask;
pthread_mutex_unlock(&iqready_lock);
// can happen when switching between rcvr numbers
if(rcb->iqSamples_remaining < 0)
rcb->iqSamples_remaining = 0;
// downsample starting at any remaining offset
downsample(rcb);
switch(mcb.output_rate) {
case 48000:
rcb->iqSamples_remaining += RTL_READ_COUNT / (DOWNSAMPLE_192 * 8);
break;
case 96000:
rcb->iqSamples_remaining += RTL_READ_COUNT / (DOWNSAMPLE_192 * 4);
break;
case 192000:
rcb->iqSamples_remaining += RTL_READ_COUNT / (DOWNSAMPLE_192 * 2);
break;
case 384000:
rcb->iqSamples_remaining += RTL_READ_COUNT / DOWNSAMPLE_192;
break;
}
while(rcb->iqSamples_remaining > samps_packet) {
load_packet(rcb);
rcb->iqSamples_remaining -= samps_packet;
rcb->iqSample_offset += samps_packet;
}
// move remaining samples to beginning of buffer
if(rcb->iqSample_offset && (rcb->iqSamples_remaining > 0)) {
memcpy(&(rcb->iqSamples[0]),
&(rcb->iqSamples[rcb->iqSample_offset * 2]),
rcb->iqSamples_remaining * 2 * sizeof(float));
rcb->iqSample_offset = 0;
}
}
pthread_exit(NULL);
//printf("EXITING hpsdrsim_sendiq_thr_func() rcvr_mask%d...\n", rcb->rcvr_mask);
}
void*
hpsdrsim_thread(void* arg) {
int bytes_read, ep;
int i, j, rc, offset;
int freq, num_rcvrs, xtra;
u_char C0_1, C0_2;
//printf("ENTERING hpsdrsim_thread active rcvrs: %d\n", mcb.active_num_rcvrs);
// start a watchdog to make sure we are sending frames
rc = pthread_create(&watchdog_thread_id, NULL,
hpsdrsim_watchdog_thread, NULL);
if(rc != 0) {
printf("pthread_create failed on hpsdrsim_watchdog_thread: rc=%d\n",
rc);
exit(1);
}
while(!do_exit) {
if(buffer[0] == 0xEF && buffer[1] == 0xFE) {
switch(buffer[2]) {
case 1:
// get the end point
ep = buffer[3] & 0xFF;
// for some reason cuSDR 3.2.13 sends command
// packets by an extra 4 bytes, cuSDR 3.2.14 is OK
xtra = ((buffer[8] == 0x7f && buffer[9] == 0x7f && buffer[10] == 0x7f)) ? 0 : 4;
switch(ep) {
case 6:
printf("EP6 data\n");
break;
case 4:
printf("EP4 data\n");
break;
case 2:
// REMEMBER, 2 USB packets of data here (audio, C0, ...)
//printf("EP2 data\n");
// get the pc_sequence number
//pc_sequence=((buffer[4]&0xFF)<<24)+((buffer[5]&0xFF)<<16)+((buffer[6]&0xFF)<<8)+(buffer[7]&0xFF);
//printf("Received data ep=%d pc_sequence=%d\n",ep,pc_sequence);
C0_1 = buffer[11 + xtra] & 0xFE;
C0_2 = buffer[523 + xtra] & 0xFE;
freq = 0;
if((C0_1 >= 4) && (C0_1 <= (4 + ((mcb.total_num_rcvrs - 1) * 2)))) {
offset = xtra;
freq = 1;
j = (C0_1 - 4) / 2;
} else if((C0_2 >= 4) && (C0_2 <= (4 + ((mcb.total_num_rcvrs - 1) * 2)))) {
offset = 512 + xtra;
freq = 1;
j = (C0_2 - 4) / 2;
}
if(freq) {
freq = (int) buffer[12 + offset] << 24 | (int) buffer[13 + offset]
<< 16 | (int) buffer[14 + offset] << 8 | (int) buffer[15 + offset];
if(last_freq[j] != freq) {
// squelch minor (scrolling) freq changes to reduce print output
ftime(&mcb.freq_ttime[j]);
if((((((mcb.freq_ttime[j].time * 1000) + mcb.freq_ttime[j].millitm) -
(mcb.freq_ltime[j].time * 1000) + mcb.freq_ltime[j].millitm)) > 2000)
|| (hpsdr_sequence < 5000)) {
if(common_freq)
printf("Received common freq(%d) %d hz, with offset %d hz, for all rcvrs\n",
j + 1, freq, freq + mcb.freq_offset[j]);
else
printf("Received freq %d hz, with offset %d hz, on rcvr %d\n",
freq, freq + mcb.freq_offset[j], j + 1);
}
if(common_freq) {
for(i = 0; i < mcb.active_num_rcvrs; i++) {
rc = rtlsdr_set_center_freq(mcb.rcb[i].rtldev,
freq + mcb.freq_offset[i]);
if(rc < 0)
printf("WARNING: Failed to set Common dongle freq %d\n", freq);
}
} else if(j < mcb.total_num_rcvrs) {
rc = rtlsdr_set_center_freq(mcb.rcb[j].rtldev, freq +
mcb.freq_offset[j]);
if(rc < 0)
printf("WARNING: Failed to set dongle freq for rcvr %d\n", j);
}
last_freq[j] = freq;
ftime(&mcb.freq_ltime[j]);
}
}
if((C0_1 == 0x00) || (C0_2 == 0x00)) {
offset = (C0_1 == 0x00) ? xtra : 512 + xtra;
if(last_rate != (buffer[12 + offset] & 3)) {
last_rate = (buffer[12 + offset] & 3);
switch(last_rate) {
case 3:
mcb.output_rate = 384000;
break;
case 2:
mcb.output_rate = 192000;
break;
case 1:
mcb.output_rate = 96000;
break;
case 0:
mcb.output_rate = 48000;
break;
default:
printf("WARNING: UNSUPPORTED RATE: %x!!!\n", last_rate);
}
printf("Setting hpsdr output rate to %d hz\n",
mcb.output_rate);
}
if(last_num_rcvrs != (buffer[15 + offset] & 0x38)) {
last_num_rcvrs = (buffer[15 + offset] & 0x38);
num_rcvrs = (last_num_rcvrs >> 3) + 1;
if(num_rcvrs > MAX_RCVRS) {
printf("ERROR: Attempt to exceed max number of rcvrs: %d\n", MAX_RCVRS);
hpsdrsim_stop_threads();
exit(-1);
} else if(num_rcvrs > 1) {
if(num_rcvrs <= mcb.total_num_rcvrs) {
num_copy_rcvrs = 0;
mcb.active_num_rcvrs = num_rcvrs;
}
else {
num_copy_rcvrs = num_rcvrs - mcb.total_num_rcvrs;
mcb.active_num_rcvrs = mcb.total_num_rcvrs;
}
mcb.nsamps_packet = nsamps_packet[mcb.active_num_rcvrs - 1];
mcb.frame_offset1 = frame_offset1[mcb.active_num_rcvrs - 1];
mcb.frame_offset2 = frame_offset2[mcb.active_num_rcvrs - 1];
mcb.rcvrs_mask = 1;
// disable all previous rcvrs except rcvr 1
for(i = 1; i < mcb.total_num_rcvrs; i++) {
mcb.rcb[i].rcvr_mask = 0;
}
// now enable any new ones
for(i = 1; i < mcb.active_num_rcvrs; i++) {
mcb.rcvrs_mask |= 1 << i;
mcb.rcb[i].rcvr_mask = 1 << i;
}
printf("Requested %d Activated %d actual rcvr(s)\n",
num_rcvrs, mcb.active_num_rcvrs);
if(num_copy_rcvrs > 0) {
for(i = mcb.active_num_rcvrs; i < num_rcvrs; i++) {
copy_rcvr[i] = i;
}
printf("Activated %d COPY(S) of rcvr %d\n",
num_copy_rcvrs, mcb.active_num_rcvrs);
}
}
}
common_freq = (buffer[15 + xtra] & 0x80) ? 1 : 0;
}
// handle the audio data if we assigned an audio device
if(mcb.sound_dev[0])
write_local_sound(&(buffer[8 + xtra]));
break;
default:
printf("unexpected EP %d length=%d\n", ep, bytes_read);
break;
}
break;
case 2:
//ignore
break;
case 4: // start / stop command
if(buffer[3] & 1) {
printf("Received Start command\n");
running = 1;
} else {
printf("Received Stop command\n");
running = 0;
hpsdr_sequence = 0;
}
break;
default:
printf("unexpected packet type: 0x%02X\n", buffer[2]);
break;
}
} else {
printf("Received bad header bytes on data port %02X,%02X\n",
buffer[0], buffer[1]);
}
bytes_read = recvfrom(reveal_socket, buffer, sizeof(buffer), 0,
(struct sockaddr*) &my_addr, &my_length);
if(bytes_read < 0) {
perror("recvfrom socket failed for hpsdrsim_thread");
exit(1);
} //else printf("hpsdrsim_thread, RECV'D %d bytes\n", bytes_read); //1036 cuSDR64, 1032 ghpsdr3
}
//printf("EXITING hpsdrsim_thread()\n");
pthread_exit(NULL);
}
void
hpsdrsim_stop_threads() {
int i;
revealed = running = 0;
for(i = 0; i < mcb.total_num_rcvrs; i++) {
rtlsdr_cancel_async(mcb.rcb[i].rtldev);
pthread_cancel(mcb.rcb[i].rtl_read_thr);
pthread_cancel(mcb.rcb[i].hpsdrsim_sendiq_thr);
}
// unblock held mutexes so we can exit
pthread_mutex_lock(&send_lock);
send_flags = mcb.rcvrs_mask;
pthread_cond_broadcast(&send_cond);
pthread_mutex_unlock(&send_lock);
pthread_mutex_lock(&iqready_lock);
rcvr_flags = mcb.rcvrs_mask;
pthread_cond_broadcast(&iqready_cond);
pthread_mutex_unlock(&iqready_lock);
pthread_cancel(watchdog_thread_id);
pthread_cancel(hpsdrsim_thread_id);
}
void*
hpsdrsim_watchdog_thread(void* arg) {
u_int i, last_sequence = 0xffffffff;
// wait until we get a start command
while(!running)
sleep(1);
//printf("ENTERING hpsdrsim_watchdog_thread active rcvrs: %d\n", mcb.active_num_rcvrs);
// sleep for 1 second, check if we're sending packets
while(1) {
sleep(1);
if(last_sequence == hpsdr_sequence) {
printf("No hpsdr packets sent for 1 second, restarting...\n");
break;
}
last_sequence = hpsdr_sequence;
}
running = revealed = 0;
pthread_cancel(hpsdrsim_thread_id);
// unblock held mutexes so we can exit
pthread_mutex_lock(&send_lock);
send_flags = mcb.rcvrs_mask;
pthread_cond_broadcast(&send_cond);
pthread_mutex_unlock(&send_lock);
pthread_mutex_lock(&iqready_lock);
rcvr_flags = mcb.rcvrs_mask;
pthread_cond_broadcast(&iqready_cond);
pthread_mutex_unlock(&iqready_lock);
// set everything back to a 1 rcvr state
mcb.rcvrs_mask = 1;
mcb.rcb[0].rcvr_mask = 1;
for(i = 1; i < mcb.total_num_rcvrs; i++) {
mcb.rcb[i].rcvr_mask = 0;
}
mcb.active_num_rcvrs = 1;
num_copy_rcvrs = 0;
for(i = 0; i < MAX_RCVRS; i++)
copy_rcvr[i] = -1;
mcb.nsamps_packet = nsamps_packet[0];
mcb.frame_offset1 = frame_offset1[0];
mcb.frame_offset2 = frame_offset2[0];
rcvr_flags &= 1;
send_flags &= 1;
last_num_rcvrs = last_rate = 0;
mcb.output_rate = 48000;
printf("Setting hpsdr output rate to %d hz\n", mcb.output_rate);
hpsdr_sequence = 0;
//printf("EXITING hpsdrsim_watchdog_thread\n");
pthread_exit(NULL);
}
void
rtlsdr_callback(unsigned char* buf, uint32_t len, void* ctx) {
int i, j;
struct rcvr_cb* rcb = (struct rcvr_cb*) ctx;
if(do_exit || !running || !ctx) {
return;
} else if(rcb->rcvr_mask == 0)
return;
if(RTL_READ_COUNT != len) {
perror("rtlsdr_callback(): RTL_READ_COUNT != len!");
return;
}
// Convert to float and copy data to buffer, offset by coefficient length * 2.
// The downsample routine will move the previous last coefficient length * 2
// to the beginning of the buffer. This is because of the FIR filter length, the
// filtering routine takes in 'filter_length' more samples than it outputs or
// coefficient length * 2 for I&Q (stereo) input samples.
for(i = 0, j = COEFF3072_H_16_LENGTH * 2; i < RTL_READ_COUNT; i++, j++)
rcb->iq_buf[j] = rtl_lut[buf[i]] * mcb.signal_multiplier[rcb->rcvr_num];
// rcb->iq_buf[j] = (float)(buf[i]-127);
pthread_mutex_lock(&iqready_lock);
rcvr_flags |= rcb->rcvr_mask;
pthread_cond_broadcast(&iqready_cond);
pthread_mutex_unlock(&iqready_lock);
}
void
format_payload(void) {
int i;
u_char hpsdr_header[8] = { 0xEF, 0xFE, 1, 6, 0, 0, 0, 0 };
u_char proto_header[8] = { 0x7f, 0x7f, 0x7f, 0, 0x1e, 0, 0, HERMES_FW_VER };
for(i = 0; i < HPSDR_FRAME_LEN; i++)
payload[i] = 0;
for(i = 0; i < 8; i++)
payload[i] = hpsdr_header[i];
for(i = 8; i < 16; i++)
payload[i] = proto_header[i - 8];
for(i = 520; i < 528; i++)
payload[i] = proto_header[i - 520];
}
void*
rtl_read_thr_func(void* arg) {
struct rcvr_cb* rcb = (struct rcvr_cb*) arg;
int r, i = rcb->rcvr_num;
//printf("ENTERING rtl_read_thr_func() rcvr %d\n", i+1);
r = rtlsdr_read_async(rcb->rtldev, rtlsdr_callback,
(void*)(&mcb.rcb[i]), 1, RTL_READ_COUNT);
//printf("EXITING rtl_read_thr_func() rcvr %d\n", i+1);
pthread_exit(NULL);
}
int
init_rtl(int rcvr_num, int dev_index) {
int r;
char num[16];
rtlsdr_dev_t* rtldev;
r = rtlsdr_open(&(mcb.rcb[rcvr_num].rtldev), dev_index);
if(r < 0) {
printf("Failed to open rtlsdr device\n");
return (-1);
}
rtldev = mcb.rcb[rcvr_num].rtldev;
r = rtlsdr_set_sample_rate(rtldev, RTL_SAMPLE_RATE);
if(r < 0) {
printf("WARNING: Failed to set sample rate to %d!\n", RTL_SAMPLE_RATE);
return (-1);
}
sprintf(num, "%d", mcb.gain[rcvr_num]);
if(mcb.gain[rcvr_num]) {
r = rtlsdr_set_tuner_gain_mode(rtldev, 1);
r |= rtlsdr_set_tuner_gain(rtldev, mcb.gain[rcvr_num]);
} else
r = rtlsdr_set_tuner_gain_mode(rtldev, 0);
if(r < 0) {
printf("WARNING: Failed to set tuner gain!\n");
return (-1);
} else
printf(" tuner gain\t\t%s\n", (mcb.gain[rcvr_num]) ? num : "auto");
rtlsdr_set_center_freq(rtldev, 100000000);
if(r < 0)
printf("WARNING: Failed to set tuner freq to 100000000hz!\n");
r = rtlsdr_set_direct_sampling(rtldev, mcb.direct_mode[rcvr_num]);
if(r < 0) {
printf("WARNING: Failed to set direct sampling!\n");
return (-1);
} else
printf(" direct sampling\t%s\n",
(mcb.direct_mode[rcvr_num]) ? "on" : "off");
r = rtlsdr_set_agc_mode(rtldev, mcb.agc_mode[rcvr_num]);
if(r < 0) {
printf("WARNING: Failed to set automatic gain!\n");
return (-1);
} else
printf(" agc mode\t\t%s\n\n", (mcb.agc_mode[rcvr_num]) ? "on" : "off");
r = rtlsdr_reset_buffer(rtldev);
if(r < 0) {
printf("WARNING: Failed to reset buffers!\n");
return (-1);
}
return (0);
}
void
usage(char* progname) {
printf(
"\n%s, an HPSDR Hermes simulator for RTL2832 based DVB-T receivers", progname);
printf(
"\nSee rtl_hpsdr.conf for configuration option descriptions.\n"
"\nUsage:\n" "\tPer rcvr options (comma separated i.e. 1,0,1,1):\n"
"\t[-a internal agc of the rtl2832 0|1 (defaults 0 or off)]\n"
"\t[-d direct sampling mode 0|1|2 (defaults 0 or off, 1=I 2=Q)]\n"
"\t[-f freq offset in hz (defaults 0)]\n"
"\t[-g gain in tenths of a db (defaults 0 for auto)]\n"
"\t[-m signal multiplier (default 1)]\n"
"\t[-o rcvr order (defaults to 1 - number detected)]\n\n"
"\tGlobal options:\n"
"\t[-c path to config file (overrides these options)]\n"
"\t[-h help (prints this usage)]\n"
"\t[-l length of fir coeffients 32|64 (default 32)]\n"
"\t[-n network device (default eth0)]\n"
"\t[-r number of rcvrs to use (defaults to all detected)]\n"
"\t[-s sound device (alsa) for audio (i.e. plughw:0,0 defaults to none)]\n"
"\t[-v print out version info]\n\n");
exit(1);
}
int
set_option(int* option, char* value) {
char params[MAX_RCVRS][MAXSTR];
int i, count = 0;
char* token;
const char s[2] = ",";
// get the first token
token = strtok(value, s);
// walk through other tokens
while((token != NULL) && (count < MAX_RCVRS)) {
strcpy(&(params[count++][0]), token);
token = strtok(NULL, s);
}
for(i = 0; i < MAX_RCVRS; i++) {
if(i < count) {
option[i] = atoi(params[i]);
option[MAX_RCVRS] = option[i]; // save last
} else
option[i] = option[MAX_RCVRS]; // set to last
}
return (count);
}
int
parse_config(char* conf_file) {
FILE* fp;
int count, line = 0;
char option[MAXSTR], value[MAXSTR];
char confbuf[MAXSTR];
if((fp = fopen(conf_file, "r")) != NULL) {
printf("\nParsing config file %s\n\n", conf_file);
while(fgets(confbuf, MAXSTR, fp) != NULL) {
line++;
if(strchr(confbuf, '#') != NULL)
continue;
option[0] = value[0] = '\0';
sscanf(confbuf, "%s %s", option, value);
if(!strcmp("direct_mode", option)) {
count = set_option(mcb.direct_mode, value);
} else if(!strcmp("tuner_gain", option)) {
count = set_option(mcb.gain, value);
} else if(!strcmp("freq_offset", option)) {
count = set_option(mcb.freq_offset, value);
} else if(!strcmp("agc_mode", option)) {
count = set_option(mcb.agc_mode, value);
} else if(!strcmp("signal_multiplier", option)) {
count = set_option(mcb.signal_multiplier, value);
} else if(!strcmp("rcvr_order", option)) {
count = set_option(mcb.rcvr_order, value);
} else if(!strcmp("sound_dev", option)) {
strcpy(mcb.sound_dev, value);
} else if(!strcmp("length_fir", option)) {
mcb.length_fir = atoi(value);
} else if(!strcmp("total_num_rcvrs", option)) {
mcb.total_num_rcvrs = atoi(value);
} else if(!strcmp("net_dev", option)) {
strcpy(mcb.net_dev, value);
}
}
return (0);
} else {
printf("Cannot find %s\n", conf_file);
exit (-1);
}
}
int
main(int argc, char* argv[]) {
int i, r, opt;
bool loop = true;
struct sigaction sigact;
char conf_file[MAXSTR];
char* progname = basename(argv[0]);
// set defaults
mcb.length_fir = 32;
mcb.sound_dev[0] = 0;
conf_file[0] = 0;
mcb.output_rate = 48000;
strcpy(mcb.net_dev, "eth0");
for(i = 0; i < MAX_RCVRS; i++) {
mcb.agc_mode[i] = 0;
mcb.direct_mode[i] = 0;
mcb.gain[i] = 0;
mcb.freq_offset[i] = 0;
mcb.signal_multiplier[i] = 1;
mcb.rcvr_order[i] = i + 1;
copy_rcvr[i] = -1;
memset(&mcb.freq_ltime[i], 0, sizeof(mcb.freq_ltime[i]));
}
while(loop && ((opt = getopt(argc, argv, "c:a:d:f:g:hl:m:n:o:r:s:v")) != -1)) {
switch(opt) {
case 'a':
r = set_option(mcb.agc_mode, optarg);
break;
case 'd':
r = set_option(mcb.direct_mode, optarg);
break;
case 'c':
strcpy(conf_file, optarg);
parse_config(conf_file);
loop = false;
break;
case 'f':
r = set_option(mcb.freq_offset, optarg);
break;
case 'g':
r = set_option(mcb.gain, optarg);
break;
case 'l':
mcb.length_fir = atoi(optarg);
if((mcb.length_fir != 32) && (mcb.length_fir != 64)) {
printf("Invalid coeffient length %d\n", mcb.length_fir);
usage(progname);
}
break;
case 'm':