forked from f4fhh/rsp_tcp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
rsp_tcp.c
executable file
·993 lines (892 loc) · 29.6 KB
/
rsp_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
/*
* rtl-sdr, turns your Realtek RTL2832 based DVB dongle into a SDR receiver
* Copyright (C) 2012 by Steve Markgraf <[email protected]>
* Copyright (C) 2012-2013 by Hoernchen <[email protected]>
*
* 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 2 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 <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <math.h>
#include <pthread.h>
#include <mirsdrapi-rsp.h>
#include <stdbool.h>
#define closesocket close
#define SOCKADDR struct sockaddr
#define SOCKET int
#define SOCKET_ERROR -1
static SOCKET s;
static pthread_t tcp_worker_thread;
static pthread_t command_thread;
//static pthread_cond_t exit_cond;
//static pthread_mutex_t exit_cond_lock;
static pthread_mutex_t ll_mutex;
static pthread_cond_t cond;
struct llist {
char *data;
size_t len;
struct llist *next;
};
typedef struct { /* structure size must be multiple of 2 bytes */
char magic[4];
uint32_t tuner_type;
uint32_t tuner_gain_count;
} dongle_info_t;
double atofs(char *s)
/* standard suffixes */
{
char last;
int len;
double suff = 1.0;
len = strlen(s);
last = s[len-1];
s[len-1] = '\0';
switch (last) {
case 'g':
case 'G':
suff *= 1e3;
/* fall-through */
case 'm':
case 'M':
suff *= 1e3;
/* fall-through */
case 'k':
case 'K':
suff *= 1e3;
suff *= atof(s);
s[len-1] = last;
return suff;
}
s[len-1] = last;
return atof(s);
}
static int global_numq = 0;
static struct llist *ll_buffers = 0;
static int llbuf_num = 500;
static int ignore_f_command = 0;
static int ignore_s_command = 0;
static volatile int do_exit = 0;
static volatile int ctrlC_exit = 0;
#define MAX_DEVS 8
#define WORKER_TIMEOUT_SEC 3
#define DEFAULT_BW_T mir_sdr_BW_1_536
#define DEFAULT_AGC_SETPOINT -30 // original -24 //Bas -34
#define DEFAULT_GAIN_REDUCTION 34 // original 40 //Bas 34
#define DEFAULT_LNA 0 // 0 = off to 9 - Attenuator!
#define RTLSDR_TUNER_R820T 5
#define MAX_DECIMATION_FACTOR 32
static int devModel = 0;
static char* devSerial = "";
static int bwType = DEFAULT_BW_T;
static int agcSetPoint = DEFAULT_AGC_SETPOINT;
static int gainReduction = DEFAULT_GAIN_REDUCTION;
static int rspLNA = DEFAULT_LNA;
static int infoOverallGr;
static int samples_per_packet;
static int last_gain_idx = 0;
static int verbose = 0;
static int wideband = 2; // wideband 0=small / 1=wide / 2=optimised for samplerate
static int edgefilter = 0;
static int noiseshape = 1; //Noiseshape 0=off / 1=on
////waardes
static int device = 0;
static int antenna = 0;
static int enable_biastee = 0;
static int enable_dabnotch = 1;
static int enable_broadcastnotch = 1;
static int enable_refout = 0;
static int deci = 1;
uint32_t sample_rate = 2048000;
////AGC beware to change all!
static int agc_type = mir_sdr_AGC_100HZ; //AGC 5-50-100HZ or DISABLE
static int agctype = 100; // just the number of above
static void sighandler(int signum)
{
fprintf(stderr, "Signal (%d) caught, ask for exit!\n", signum);
// exit(signum); // can be used to force ctrl-c instant
do_exit = 1;
pthread_cond_signal(&cond);
}
void gc_callback(unsigned int gRdB, unsigned int lnaGRdB, void* cbContext )
{
if (gRdB == mir_sdr_ADC_OVERLOAD_DETECTED)
{
printf("adc overload detected\n");
mir_sdr_GainChangeCallbackMessageReceived();
}
else if (gRdB == mir_sdr_ADC_OVERLOAD_CORRECTED)
{
printf("adc overload corrected\n");
mir_sdr_GainChangeCallbackMessageReceived();
}
if (verbose)
printf("new gain reduction (%d), lna gain reduction (%d)\n", gRdB, lnaGRdB);
}
void rx_callback(short *xi, short *xq, unsigned int firstSampleNum, int grChanged, int rfChanged, int fsChanged, unsigned int numSamples, unsigned int reset, unsigned int hwRemoved, void* cbContext)
{
unsigned int i;
if(!do_exit) {
struct llist *rpt = (struct llist*)malloc(sizeof(struct llist));
rpt->data = (char*)malloc(2 * numSamples);
// assemble the data
short xqi = 0;
short xqq = 0;
short errori = 0;
short errorq = 0;
short errorIa[5] = {0,0,0,0,0};
short errorQa[5] = {0,0,0,0,0};
// short H[4] = {-0.75-0.125/2, 0.75, -0.5, 0.25 -0.125/2}; // filter response
short error_corrI = 0;
short error_corrQ = 0;
char *data;
data = (char*)rpt->data;
for (i = 0; i < numSamples; i++, xi++, xq++) {
if (noiseshape == 0) {
xqi = (((*xi ) << 2) >> 8);
xqq = (((*xq ) << 2) >> 8);
}
else {
error_corrI = -((errorIa[0]>>1)+(errorIa[0]>>2)+(errorIa[0]>>4));
error_corrQ = -((errorQa[0]>>1)+(errorQa[0]>>2)+(errorQa[0]>>4));
error_corrI += ((errorIa[1]>>1)+(errorIa[1]>>2));
error_corrQ += ((errorQa[1]>>1)+(errorQa[1]>>2));
error_corrI -= (errorIa[2]>>1);
error_corrQ -= (errorQa[2]>>1);
error_corrI += (errorIa[3]>>2);
error_corrQ += (errorQa[3]>>2);
error_corrI -= (errorIa[4]>>4);
error_corrQ -= (errorQa[4]>>4);
xqi = (((*xi + error_corrI) << 2) >> 8);
xqq = (((*xq + error_corrQ) << 2) >> 8);
errori = (xqi << 6) - *xi; // 6 ipv. 8 ivm. sample_shift=2
errorq = (xqq << 6) - *xq;
errorIa[4]=errorIa[3];
errorIa[3]=errorIa[2];
errorIa[2]=errorIa[1];
errorIa[1]=errorIa[0];
errorIa[0]=errori-error_corrI;
errorQa[4]=errorQa[3];
errorQa[3]=errorQa[2];
errorQa[2]=errorQa[1];
errorQa[1]=errorQa[0];
errorQa[0]=errorq-error_corrQ;
}
*(data++) = (unsigned char)(xqi + 128);
*(data++) = (unsigned char)(xqq + 128);
rpt->len = 2 * numSamples;
}
rpt->next = NULL;
pthread_mutex_lock(&ll_mutex);
if (ll_buffers == NULL) {
ll_buffers = rpt;
} else {
struct llist *cur = ll_buffers;
int num_queued = 0;
while (cur->next != NULL) {
cur = cur->next;
num_queued++;
}
if(llbuf_num && llbuf_num == num_queued-2){
struct llist *curelem;
free(ll_buffers->data);
curelem = ll_buffers->next;
free(ll_buffers);
ll_buffers = curelem;
}
cur->next = rpt;
global_numq = num_queued;
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&ll_mutex);
}
}
static void *tcp_worker(void *arg)
{
struct llist *curelem,*prev;
int bytesleft,bytessent, index;
struct timeval tv= {1,0};
struct timespec ts;
struct timeval tp;
fd_set writefds;
int r = 0;
while(1) {
if(do_exit)
pthread_exit(0);
pthread_mutex_lock(&ll_mutex);
gettimeofday(&tp, NULL);
ts.tv_sec = tp.tv_sec + WORKER_TIMEOUT_SEC;
ts.tv_nsec = tp.tv_usec * 1000;
r = pthread_cond_timedwait(&cond, &ll_mutex, &ts);
if(r == ETIMEDOUT) {
pthread_mutex_unlock(&ll_mutex);
printf("worker cond timeout\n");
sighandler(0);
pthread_exit(NULL);
}
curelem = ll_buffers;
ll_buffers = 0;
pthread_mutex_unlock(&ll_mutex);
while(curelem != 0) {
bytesleft = curelem->len;
index = 0;
bytessent = 0;
while(bytesleft > 0) {
FD_ZERO(&writefds);
FD_SET(s, &writefds);
tv.tv_sec = 2; //was 1
tv.tv_usec = 0;
r = select(s+1, NULL, &writefds, NULL, &tv);
if(r) {
bytessent = send(s, &curelem->data[index], bytesleft, 0);
bytesleft -= bytessent;
index += bytessent;
}
if(bytessent == SOCKET_ERROR || do_exit) {
printf("worker socket bye\n");
sighandler(0);
pthread_exit(NULL);
}
}
prev = curelem;
curelem = curelem->next;
free(prev->data);
free(prev);
}
}
}
// gain reduction list in back order to emulate R820T gain
//--bas--
//const int gain_list[] = { 78, 75, 72, 69, 66, 63, 60, 57, 54, 51, 48, 45, 42, 39, 36, 33, 30, 27, 24, 21, 18, 15, 12, 9, 6, 3, 0 };
const int gain_list[] = { 40 };
static int set_gain_by_index(unsigned int index)
{
int r;
// gainReduction = gain_list[index];
// r = mir_sdr_Reinit(&gainReduction, 0, 0, 0, 0, 0, rspLNA, &infoOverallGr, mir_sdr_USE_SET_GR_ALT_MODE, &samples_per_packet, mir_sdr_CHANGE_GR);
r = mir_sdr_Reinit(&gainReduction, 0, 0, 0, 0, 0, rspLNA, &infoOverallGr, mir_sdr_USE_RSP_SET_GR, &samples_per_packet, mir_sdr_CHANGE_GR);
if (r != mir_sdr_Success) {
printf("set gain reduction error (%d)\n", r);
}
last_gain_idx = index;
return r;
}
//end gain reduction
static int set_tuner_gain_mode(unsigned int mode)
{
int r;
if (mode)
{
r = mir_sdr_AgcControl(agc_type, agcSetPoint, 0, 0, 0, 0, rspLNA);
r = set_gain_by_index(last_gain_idx);
printf("agc disabled\n");
}
else
{
//r = mir_sdr_AgcControl(mir_sdr_AGC_100HZ, agcSetPoint, 0, 0, 0, 0, rspLNA);
// Changes by PA0SIM =======================================
r = mir_sdr_AgcControl(agc_type, agcSetPoint, 0, 0, 0, 0, rspLNA);
printf("agc enabled\n");
}
if (r != mir_sdr_Success) {
printf("tuner gain (agc) control error (%d)\n", r);
}
return r;
}
static int set_agc_mode(unsigned int mode)
{
int r;
if (mode)
{
//rspLNA = 1;
printf("enable LNA\n");
}
else
{
//rspLNA = 0;
printf("disable LNA\n");
}
r = set_gain_by_index(last_gain_idx);
return r;
}
static int set_freq_correction(int32_t corr)
{
int r;
r = mir_sdr_SetPpm((double)corr);
if (r != mir_sdr_Success) {
printf("set freq correction error (%d)\n", r);
}
return r;
}
static int set_freq(uint32_t f)
{
int r;
r = mir_sdr_Reinit(&gainReduction, 0, (double)f/1e6, 0, bwType, 0, 0, &infoOverallGr, 0, &samples_per_packet, mir_sdr_CHANGE_RF_FREQ);
if (r != mir_sdr_Success) {
printf("set freq error (%d)\n", r);
}
return r;
}
static int set_sample_rate(uint32_t sr)
{
int r;
double f;
if (sr < (2000000 / MAX_DECIMATION_FACTOR) || sr > 10000000) {
fprintf(stderr, "sample rate %u is not supported\n", sr);
return -1;
}
if (sr < 2000000)
{
int c = 0;
// Find best decimation factor
while (sr * (1 << c) < 2000000 && (1 << c) < MAX_DECIMATION_FACTOR) {
c++;
}
deci = 1 << c;
if (sr >= 1536000 && sr < 2000000)
{
if (wideband == 1) bwType = mir_sdr_BW_5_000;
else bwType = mir_sdr_BW_1_536;
}
else
if (sr >= 600000 && sr < 1536000)
{
if (wideband >= 1) bwType = mir_sdr_BW_1_536;
else bwType = mir_sdr_BW_0_600;
}
else
if (sr >= 300000 && sr < 600000)
{
if (wideband >= 1) bwType = mir_sdr_BW_0_600;
else bwType = mir_sdr_BW_0_300;
}
else
if (sr >= 200000 && sr < 300000)
{
if (wideband >= 1) bwType = mir_sdr_BW_0_300;
else bwType = mir_sdr_BW_0_200;
}
else
{
bwType = mir_sdr_BW_0_200;
}
}
else
{
deci =1;
if (sr >= 8000000 && sr <= 10000000)
{
bwType = mir_sdr_BW_8_000;
}
else
if (sr >= 7000000 && sr < 8000000)
{
if (wideband == 1) bwType = mir_sdr_BW_8_000;
else bwType = mir_sdr_BW_7_000;
}
else
if (sr >= 6000000 && sr < 7000000)
{
if (wideband == 1) bwType = mir_sdr_BW_7_000;
else bwType = mir_sdr_BW_6_000;
}
else
if (sr >= 5000000 && sr < 6000000)
{
if (wideband == 1) bwType = mir_sdr_BW_6_000;
else bwType = mir_sdr_BW_5_000;
}
else
if (sr >= 2500000 && sr < 5000000)
{
if (wideband >= 1 && sr >= 2880000) bwType = mir_sdr_BW_5_000;
else bwType = mir_sdr_BW_1_536;
}
else
{
if (wideband == 1 && sr >= 2048000) bwType = mir_sdr_BW_5_000;
else bwType = mir_sdr_BW_1_536;
}
}
f = (double)(sr * deci);
if (deci <= 1 && edgefilter == 0)
mir_sdr_DecimateControl(0, 0, 0);
else if (deci >= 1 && edgefilter == 0)
mir_sdr_DecimateControl(1, deci, 0);
else if (deci >= 1 && edgefilter >= 1)
mir_sdr_DecimateControl(1, deci, 1);
printf("device SR %.2f, decim %d, output SR %u, IF Filter BW %d kHz\n", f, deci, sr, bwType);
//r = mir_sdr_Reinit(&gainReduction, (double)f/1e6, 0, bwType, 0, 0, 0, &infoOverallGr, 0, &samples_per_packet, mir_sdr_CHANGE_FS_FREQ | mir_sdr_CHANGE_BW_TYPE);
// Changes by PA0SIM ===========================
r = mir_sdr_Reinit(&gainReduction, (double)f/1e6, 0, bwType, 0, 0, 0, &infoOverallGr, 0, &samples_per_packet, mir_sdr_CHANGE_FS_FREQ | mir_sdr_CHANGE_BW_TYPE | mir_sdr_CHANGE_IF_TYPE);
if (r != mir_sdr_Success) {
printf("set sample rate error (%d)\n", r);
}
if (enable_biastee == 1) {
// set Bias-T
if (devModel == 2) mir_sdr_RSPII_BiasTControl(enable_biastee);
else if (devModel == 3) mir_sdr_rsp1a_BiasT(enable_biastee);
else if (devModel == 255) mir_sdr_rspDuo_BiasT(enable_biastee);
}
return r;
}
struct command{
unsigned char cmd;
unsigned int param;
}__attribute__((packed));
static void *command_worker(void *arg)
{
int left, received = 0;
fd_set readfds;
struct command cmd={0, 0};
struct timeval tv= {1, 0};
int r = 0;
uint32_t tmp;
while(1) {
left=sizeof(cmd);
while(left >0) {
FD_ZERO(&readfds);
FD_SET(s, &readfds);
tv.tv_sec = 2; //bas was 1
tv.tv_usec = 0;
r = select(s+1, &readfds, NULL, NULL, &tv);
if(r) {
received = recv(s, (char*)&cmd+(sizeof(cmd)-left), left, 0);
left -= received;
}
if(received == SOCKET_ERROR || do_exit) {
printf("comm recv bye\n");
sighandler(0);
pthread_exit(NULL);
}
}
switch(cmd.cmd) {
case 0x01:
// printf("set freq %d\n", ntohl(cmd.param));
// set_freq(ntohl(cmd.param));
if (ignore_f_command) {
printf("set freq %d ignored because -f used at commandline\n", ntohl(cmd.param));
} else {
printf("set freq %d\n", ntohl(cmd.param));
set_freq(ntohl(cmd.param));
}
break;
case 0x02:
// printf("set sample rate %d\n", ntohl(cmd.param));
// set_sample_rate(ntohl(cmd.param));
if (ignore_s_command) {
printf("set sample rate %d ignored because -s used at commandline\n", ntohl(cmd.param));
} else {
printf("set sample rate %d\n", ntohl(cmd.param));
set_sample_rate(ntohl(cmd.param));
}
break;
case 0x03:
printf("set gain mode %d\n", ntohl(cmd.param));
set_tuner_gain_mode(ntohl(cmd.param));
break;
case 0x04:
printf("set gain %d\n", ntohl(cmd.param));
// rtlsdr_set_tuner_gain(dev, ntohl(cmd.param));
break;
case 0x05:
printf("set freq correction %d\n", ntohl(cmd.param));
set_freq_correction(ntohl(cmd.param));
break;
case 0x06:
tmp = ntohl(cmd.param);
printf("set if stage %d gain %d\n", tmp >> 16, (short)(tmp & 0xffff));
// rtlsdr_set_tuner_if_gain(dev, tmp >> 16, (short)(tmp & 0xffff));
break;
case 0x07:
printf("set test mode %d\n", ntohl(cmd.param));
// rtlsdr_set_testmode(dev, ntohl(cmd.param));
break;
case 0x08:
printf("set agc mode %d\n", ntohl(cmd.param));
set_agc_mode(ntohl(cmd.param));
break;
case 0x09:
printf("set direct sampling %d\n", ntohl(cmd.param));
// rtlsdr_set_direct_sampling(dev, ntohl(cmd.param));
break;
case 0x0a:
printf("set offset tuning %d\n", ntohl(cmd.param));
// rtlsdr_set_offset_tuning(dev, ntohl(cmd.param));
break;
case 0x0b:
printf("set rtl xtal %d\n", ntohl(cmd.param));
// rtlsdr_set_xtal_freq(dev, ntohl(cmd.param), 0);
break;
case 0x0c:
printf("set tuner xtal %d\n", ntohl(cmd.param));
// rtlsdr_set_xtal_freq(dev, 0, ntohl(cmd.param));
break;
case 0x0d:
printf("set tuner gain by index %d\n", ntohl(cmd.param));
set_gain_by_index(ntohl(cmd.param));
break;
case 0x0e:
printf("set bias tee %d\n", ntohl(cmd.param));
// rtlsdr_set_bias_tee(dev, (int)ntohl(cmd.param));
break;
default:
break;
}
cmd.cmd = 0xff;
fflush(stdout);
}
}
void usage(void)
{
printf("rsp_tcp, an I/Q spectrum server for SDRPlay receivers - modified by Bas ON5HB for websdr.org - NoiseShaping by Jan PA0SIM - Serialnumber by Nanko PA0NVY"
#ifdef SERVER_VERSION
"VERSION "SERVER_VERSION
#endif
"\n\n Usage:\n"
"\t-a Listen address (default: 127.0.0.1)\n"
"\t-p Listen port (default: 1234)\n"
"\t-S Device serial number (-S list for a list)\n"
"\t-d RSP device to use (default: 1, first found)\n"
"\t-P Antenna Port select (0/1/2, default: 0, Port A)\n"
"\t-r Gain reduction (default: 34 / values 20-59) - Not set in websdr.cfg see tips\n"
"\t-l LNA attenuator level about -6dB each step (default: 0 / values 0-9) - See tips below\n"
"\t-T Bias-T enable* (default: disabled)\n"
"\t-f Frequency to tune center in Hertz (default: 1000000 = 1MHz) - If freq set centerfreq and progfreq are ignored! - Normally set in websdr.cfg\n"
"\t-s Samplerate in samples-per-second (default: 2048000) - If sample rate is set it will be ignored from client! - Normally set in websdr.cfg\n\n"
"\t-W Wideband-pass-filter mode (default: 2 / values: 0 small / 1 wide / 2 = optimised)\n"
"\t-D DAB band-reject-filter* (default: enabled)\n"
"\t-B MW band-reject-filter* (default: enabled)\n"
"\t-R Refclk output* (default: disabled)\n"
"\t-E Edge-steep-filter enable* (default: disabled) - Beware CPU load could go high!\n\n"
"\t-A Auto Gain Control setpoint (default: -30 / values -1 to -69 / other disabled)\n"
"\t-G Auto Gain Control speed in Hz (default: 100 / values 0/5/50/100) - Sets overloading adjustment-speed\n"
"\t-N NoiseShaping* disable (default: enabled)\n"
"\t-n Max number of linked list buffers to keep (default: 500)\n"
"\t-v Verbose output (debug) enable* (default: disabled)\n\n"
"\t* Marked options are switches they toggle on/off\n"
"\t Tip1 - start with -l 0 level correction first, if too much signal try -l 1, -l 2 etc, most simple way start receiving correctly\n"
"\t Tip2 - websdr.cfg gain setting is cosmetic only, it does not impact the receivers performance!\n"
"\t Tip3 - if you have ghost-signals, set wideband-pass to small as it's probably too wide, mostly happens on HF!\n\n"
);
exit(1);
}
int main(int argc, char **argv)
{
int r, opt, i;
char* addr = "127.0.0.1";
int port = 1234;
uint32_t frequency = 1000000;
struct sockaddr_in local, remote;
struct llist *curelem,*prev;
pthread_attr_t attr;
void *status;
struct timeval tv = {1,0};
struct linger ling = {1,0};
SOCKET listensocket;
socklen_t rlen;
fd_set readfds;
dongle_info_t dongle_info;
float ver;
mir_sdr_DeviceT devices[MAX_DEVS];
unsigned int numDevs;
/////waardes
struct sigaction sigact, sigign;
while ((opt = getopt(argc, argv, "a:p:r:f:s:n:d:l:P:q:A:G:S:W:NTvDBRE")) != -1) {
switch (opt) {
case 'd':
device = atoi(optarg) - 1;
break;
case 'S':
devSerial = optarg;
break;
case 'P':
antenna = atoi(optarg);
break;
case 'r':
gainReduction = atoi(optarg);
break;
case 'f':
frequency = (uint32_t)atofs(optarg);
ignore_f_command = 1;
break;
case 's':
sample_rate = (uint32_t)atofs(optarg);
ignore_s_command = 1;
break;
case 'A':
agcSetPoint = atoi(optarg);
break;
case 'a':
addr = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'n':
llbuf_num = atoi(optarg);
break;
case 'W':
wideband = atoi(optarg);
break;
case 'E':
edgefilter = 1;
break;
case 'N':
noiseshape = 0;
break;
case 'l':
rspLNA = atoi(optarg);
break;
case 'G':
agctype = atoi(optarg);
break;
case 'T':
enable_biastee = 1;
break;
case 'D':
enable_dabnotch = 0;
break;
case 'B':
enable_broadcastnotch = 0;
break;
case 'R':
enable_refout = 1;
break;
case 'v':
verbose = 1;
break;
default:
usage();
break;
}
}
if (agctype == 5) agc_type = mir_sdr_AGC_5HZ;
else if (agctype == 50) agc_type = mir_sdr_AGC_50HZ;
else if (agctype == 100) agc_type = mir_sdr_AGC_100HZ;
else { agc_type = mir_sdr_AGC_DISABLE;
agctype = 0;}
if (gainReduction < 20 || gainReduction > 59) gainReduction = DEFAULT_GAIN_REDUCTION;
if (wideband < 0 || wideband > 2 ) wideband = 2;
printf("\nrsp_tcp, an I/Q spectrum server for SDRPlay receivers - modified by Bas ON5HB for websdr.org - NoiseShaping by Jan PA0SIM - Serialnumber by Nanko PA0NVY "
#ifdef SERVER_VERSION
"VERSION "SERVER_VERSION
#endif
);
printf("\nuse --help for options and explanation\n\n");
// check API version
r = mir_sdr_ApiVersion(&ver);
if (ver != MIR_SDR_API_VERSION) {
// Error detected, include file does not match dll. Deal with error condition.
printf("library libmirsdrapi-rsp must be version %f\n", ver);
exit(1);
}
printf("libmirsdrapi-rsp version %.2f found\n", ver);
// enable debug output
if (verbose)
mir_sdr_DebugEnable(1);
// select RSP device
r = mir_sdr_GetDevices(&devices[0], &numDevs, MAX_DEVS);
if (r != mir_sdr_Success) {
fprintf(stderr, "Failed to get device list (%d)\n", r);
exit(1);
}
if (strcmp(devSerial,"list") == 0) {
for (i = 0; i < numDevs; i++) {
printf("Device: %d, Serial: %s Available: %d\n", i, devices[i].SerNo,devices[i].devAvail);
}
exit(1);
}
else {
for (i = 0; i < numDevs; i++) {
if (strcmp(devSerial,devices[i].SerNo) == 0) {
device = i;
printf("Device selected: %d, Serial: %s\n", i, devices[i].SerNo);
}
}
}
if (devices[device].devAvail != 1) {
printf("Detected RSP Avail != 0 %d\n", devices[device].devAvail);
fprintf(stderr, "RSP selected (%d) is not available.\n", (device + 1));
exit(1);
}
r = mir_sdr_SetDeviceIdx(device);
if (r != mir_sdr_Success) {
fprintf(stderr, "Failed to set device index (%d)\n", r);
exit(1);
}
// get RSP model and display modelname.
devModel = devices[device].hwVer;
if (devModel == 1) printf("detected RSP model (hw version %d) = RSP1\n", devModel);
else if (devModel == 2) printf("detected RSP model (hw version %d) = RSP2\n", devModel);
else if (devModel == 3) printf("detected RSP model (hw version %d) = RSPduo\n", devModel);
else if (devModel == 255) printf("detected RSP model (hw version %d) = RSP1A\n", devModel);
else printf("detected RSP model (hw version %d) = Unknown\n", devModel);
/* printf("Selected RSP Serial %s\n", devices[device].SerNo);
printf("Selected RSP Name %s\n", devices[device].DevNm);
printf("Selected RSP hwVer %d\n", devices[device].hwVer);
printf("Selected RSP Avail %d\n", devices[device].devAvail);
printf("Selected RSP Serial %s\n", devices[device].SerNo); */
// select antenna
switch (antenna) {
case 1:
mir_sdr_RSPII_AntennaControl(mir_sdr_RSPII_ANTENNA_B);
mir_sdr_AmPortSelect(0);
break;
case 2:
mir_sdr_AmPortSelect(1);
break;
default:
mir_sdr_RSPII_AntennaControl(mir_sdr_RSPII_ANTENNA_A);
mir_sdr_AmPortSelect(0);
}
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigign.sa_handler = SIG_IGN;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
sigaction(SIGPIPE, &sigign, NULL);
//pthread_mutex_init(&exit_cond_lock, NULL);
pthread_mutex_init(&ll_mutex, NULL);
//pthread_mutex_init(&exit_cond_lock, NULL);
pthread_cond_init(&cond, NULL);
//pthread_cond_init(&exit_cond, NULL);
memset(&local,0,sizeof(local));
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr(addr);
listensocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
r = 1;
setsockopt(listensocket, SOL_SOCKET, SO_REUSEADDR, (char *)&r, sizeof(int));
setsockopt(listensocket, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling));
bind(listensocket,(struct sockaddr *)&local,sizeof(local));
r = fcntl(listensocket, F_GETFL, 0);
r = fcntl(listensocket, F_SETFL, r | O_NONBLOCK);
while(1) {
printf("listening...\n");
printf("Use the device argument 'rsp_tcp=%s:%d' in OsmoSDR "
"(gr-osmosdr) source\n"
"to receive samples in GRC and control "
"rsp_tcp parameters (frequency, gain, ...).\n",
addr, port);
listen(listensocket,1);
while(1) {
FD_ZERO(&readfds);
FD_SET(listensocket, &readfds);
tv.tv_sec = 2; //was 1
tv.tv_usec = 0;
r = select(listensocket+1, &readfds, NULL, NULL, &tv);
if(do_exit) {
goto out;
} else if(r) {
rlen = sizeof(remote);
s = accept(listensocket,(struct sockaddr *)&remote, &rlen);
break;
}
}
setsockopt(s, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling));
printf("client accepted!\n");
printf("AGC-type set %dHz (0 means disabled)\n", agctype);
printf("Low-Noise-Amp mode set %u \n", rspLNA);
printf("Gain-Reduction set %d \n", gainReduction);
printf("AGC-Gain-Setpoint set %d \n", agcSetPoint);
printf("Edgefilter set %d (0=off 1=on)\n", edgefilter);
printf("Noiseshaping set %d (0=off 1=on)\n", noiseshape);
memset(&dongle_info, 0, sizeof(dongle_info));
memcpy(&dongle_info.magic, "RTL0", 4);
dongle_info.tuner_type = htonl(RTLSDR_TUNER_R820T);
dongle_info.tuner_gain_count = htonl(sizeof(gain_list)/sizeof(gain_list[0]) - 1);
r = send(s, (const char *)&dongle_info, sizeof(dongle_info), 0);
if (sizeof(dongle_info) != r)
printf("failed to send dongle information\n");
// must start the tcp_worker before the first samples are available from the rx
// because the rx_callback tries to send a condition to the worker thread
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
r = pthread_create(&tcp_worker_thread, &attr, tcp_worker, NULL);
/*
r = pthread_create(&command_thread, &attr, command_worker, NULL);
pthread_attr_destroy(&attr);
*/
// initialise API and start the rx
// r = mir_sdr_StreamInit(&gainReduction, (sample_rate/1e6), (frequency/1e6), bwType, 0, rspLNA, &infoOverallGr, mir_sdr_USE_SET_GR_ALT_MODE, &samples_per_packet, rx_callback, gc_callback, (void *)NULL);
// Changes by PA0SIM =============================
r = mir_sdr_StreamInit(&gainReduction, (sample_rate/1e6), (frequency/1e6), bwType, 0, rspLNA, &infoOverallGr, mir_sdr_USE_RSP_SET_GR, &samples_per_packet, rx_callback, gc_callback, (void *)NULL);
if (r != mir_sdr_Success)
{
printf("failed to start the RSP device, return (%d)\n", r);
break;
}
fprintf(stderr,"started rx\n");
//Notches and other stuff must be here!!....
// enable DC offset and IQ imbalance correction
mir_sdr_DCoffsetIQimbalanceControl(1, 1);
// enable AGC with a setPoint of -30dBfs
mir_sdr_AgcControl(agc_type, agcSetPoint, 0, 0, 0, 0, rspLNA);
// set the DC offset correction mode for the tuner (moved from below)
mir_sdr_SetDcMode(4, 1);
// set the time period over which the DC offset is tracked when in one shot mode.
mir_sdr_SetDcTrackTime(10);
// set Notch
mir_sdr_RSPII_RfNotchEnable(enable_broadcastnotch);
mir_sdr_rsp1a_DabNotch(enable_dabnotch);
mir_sdr_rsp1a_BroadcastNotch(enable_broadcastnotch);
mir_sdr_rspDuo_DabNotch(enable_dabnotch);
mir_sdr_rspDuo_BroadcastNotch(enable_broadcastnotch);
mir_sdr_rspDuo_Tuner1AmNotch(enable_broadcastnotch);
// set external reference output
mir_sdr_RSPII_ExternalReferenceControl(enable_refout);
mir_sdr_rspDuo_ExtRef(enable_refout);
// the rx must be started before accepting commands from the command worker
r = pthread_create(&command_thread, &attr, command_worker, NULL);
pthread_attr_destroy(&attr);
// wait for the workers to exit
pthread_join(tcp_worker_thread, &status);
pthread_join(command_thread, &status);
closesocket(s);
//
// stop the receiver
mir_sdr_StreamUninit();
//
printf("all threads dead..\n");
curelem = ll_buffers;
ll_buffers = 0;
while(curelem != 0) {
prev = curelem;
curelem = curelem->next;
free(prev->data);
free(prev);
}
if (!ctrlC_exit) do_exit = 0;
global_numq = 0;
}
out:
mir_sdr_StreamUninit();
mir_sdr_ReleaseDeviceIdx();
closesocket(listensocket);
closesocket(s);
printf("bye!\n");
return r >= 0 ? r : -r;
}