-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpsdr2alsa.c
277 lines (221 loc) · 10.3 KB
/
hpsdr2alsa.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
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if_arp.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <string.h>
#include <errno.h>
#include "metis.h"
#include "metis.cc"
#include "HermesProxy.h"
#include "HermesProxy.cc"
#include "alsathreads.c"
#include "alsathreads.h"
#include <getopt.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <signal.h>
int sigtermnum = 0;
bool kbhit()
{
termios term;
tcgetattr(0, &term);
termios term2 = term;
term2.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &term2);
int byteswaiting;
ioctl(0, FIONREAD, &byteswaiting);
tcsetattr(0, TCSANOW, &term);
return byteswaiting > 0;
}
void term(int signum)
{
printf("Caught! %i\n",signum);
sigtermnum=signum;
}
HermesProxy* Hermes;
struct hpsdrinfos
{
int frx[9]={-1,-1,-1,-1,-1,-1,-1,-1};
char *adrx[9]={NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; //plughw:CARD=loopTest1,DEV=0 dmix:CARD=PCH,DEV=0
int nRX=0;
int samplerate=48000;
char *hpsdrMAC=(char *)"*";
char *interface=(char *)"eth0";
unsigned int hermesbuffersize=4096;
unsigned int alsabuffersize=128*1024;
} hpsdroptions;
void prexample(char *argv[]){
fprintf (stderr,"\033[0;32m");
fprintf (stderr,"%s --interface=wlo1 --samplerate=48000 --adrx1=plughw:CARD=PCH,DEV=0 --frx1=14074000 --adrx2=plughw:CARD=loopTest1,DEV=0 --frx2=7200000 --nRX=2\n",argv[0]);
fprintf (stderr,"--interface is the network interface where is your receiver connected.");
fprintf (stderr,"--samplerate is the general sample rate of your receiver.Default is %u.", hpsdroptions.samplerate);
fprintf (stderr,"--adrx1 is the ouput alsa sound device for rx1, adrx2 for rx2 etc...");
fprintf (stderr,"--frx1 is the frequency to set for adrx1 in hz, frx2 for adrx2, etc...");
fprintf (stderr,"--nRX is your rx count, if you use 2 receiver set to 2.");
fprintf (stderr,"--hermesbuffersize is the hermes buffer size. Default is %u.", hpsdroptions.hermesbuffersize);
fprintf (stderr,"--alsabuffersize is the alsa output buffer size. Default is %u.", hpsdroptions.alsabuffersize);
fprintf (stderr,"\033[0m");
}
int main(int argc, char *argv[])
{
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = term;
sigaction(SIGTERM, &action, NULL);
int c;
int digit_optind = 0;
static struct option long_options[] = {
{"frx1",required_argument,0,0},{"adrx1",required_argument,0,0},
{"frx2",required_argument,0,0},{"adrx2",required_argument,0,0},
{"frx3",required_argument,0,0},{"adrx3",required_argument,0,0},
{"frx4",required_argument,0,0},{"adrx4",required_argument,0,0},
{"frx5",required_argument,0,0},{"adrx5",required_argument,0,0},
{"frx6",required_argument,0,0},{"adrx6",required_argument,0,0},
{"frx7",required_argument,0,0},{"adrx7",required_argument,0,0},
{"frx8",required_argument,0,0},{"adrx8",required_argument,0,0},
{"nRX",required_argument,0,0},
{"samplerate",required_argument,0,0},
{"hpsdrMAC",required_argument,0,0},
{"interface",required_argument,0,0},
{"hermesbuffersize",required_argument,0,0},
{"alsabuffersize",required_argument,0,0},
{0,0,0,0}
};
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
c = getopt_long(argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg) {
printf(" is %s\n", optarg);
if(long_options[option_index].name[0] == 'f') {hpsdroptions.frx[(int)(long_options[option_index].name[3]-'0')]=atoi(optarg);}
else if(long_options[option_index].name[0] == 'a') {hpsdroptions.adrx[(int)(long_options[option_index].name[4]-'0')]=optarg;}
else if(strcmp(long_options[option_index].name, "nRX")==0) {hpsdroptions.nRX=atoi(optarg);}
else if(strcmp(long_options[option_index].name, "samplerate")==0) {hpsdroptions.samplerate=atoi(optarg);}
else if(strcmp(long_options[option_index].name, "hpsdrMAC")==0) {hpsdroptions.hpsdrMAC=optarg;}
else if(strcmp(long_options[option_index].name, "interface")==0) {hpsdroptions.interface=optarg;}
else if(strcmp(long_options[option_index].name, "hermesbuffersize")==0) {hpsdroptions.hermesbuffersize=atoi(optarg);}
else if(strcmp(long_options[option_index].name, "alsabuffersize")==0) {hpsdroptions.alsabuffersize=atoi(optarg);}
}
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("non-option elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
if(hpsdroptions.nRX==0) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "please give the number of receivers --nRX=\n");
fprintf (stderr,"\033[0m");
prexample(argv);
exit(1);
}
for(int i=1; i<=hpsdroptions.nRX; ++i) {
if(hpsdroptions.adrx[i] == NULL || hpsdroptions.frx[i] == -1) {
fprintf (stderr,"\033[1;31m");
fprintf (stderr, "rx%d not initialized, miss frequency or alsa device\n",i);
fprintf (stderr,"\033[0m");
exit(1);
}
}
Hermes = new HermesProxy(hpsdroptions.frx[1],hpsdroptions.frx[2],hpsdroptions.frx[3],hpsdroptions.frx[4],
hpsdroptions.frx[5], hpsdroptions.frx[6],hpsdroptions.frx[7], hpsdroptions.frx[8],
0, 0,
0, 0, 0,
'0', hpsdroptions.samplerate, hpsdroptions.interface,
"0xF8", 0, 0,
0, 0, 0, hpsdroptions.nRX,
hpsdroptions.hpsdrMAC);
pthread_t alsadeviceth[hpsdroptions.nRX];
int NumRx = Hermes->NumReceivers;
int SamplesPerRx = Hermes->USBRowCount[NumRx-1];
struct thargs *argvvv[NumRx];
//short *output_items[NumRx][SamplesPerRx][2];
pthread_attr_t attr;
struct sched_param param;
pthread_attr_init (&attr);
pthread_attr_getschedparam (&attr, ¶m);
(param.sched_priority)++;
pthread_attr_setschedparam (&attr, ¶m);
for(int i=1; i<=NumRx; ++i) {
argvvv[i] = (struct thargs *)malloc(sizeof(struct thargs));
argvvv[i]->index = i;
argvvv[i]->adrx = hpsdroptions.adrx[i];
argvvv[i]->samplerate = (unsigned int)hpsdroptions.samplerate;
argvvv[i]->alsabuffersize = (unsigned int)hpsdroptions.alsabuffersize;
argvvv[i]->SamplesPerRx = SamplesPerRx;
argvvv[i]->output_items = (short *) malloc (hpsdroptions.hermesbuffersize * sizeof(short) * SamplesPerRx * 2);
memset(argvvv[i]->output_items, 0, hpsdroptions.hermesbuffersize * sizeof(short) * SamplesPerRx * 2);
argvvv[i]->hermesbuffersize = hpsdroptions.hermesbuffersize;
argvvv[i]->stopth = false;
argvvv[i]->RxWriteCounter=0;
argvvv[i]->state=1;
pthread_create(&alsadeviceth[i],&attr,&alsapbth,(void*)argvvv[i]);
}
for(int i=1; i<=NumRx; ++i) {
while(argvvv[i]->state==1) usleep(5);
if(argvvv[i]->state == -1) {
Hermes->End();
for(int i=1; i<=NumRx; i++) {
argvvv[i]->stopth = 1;
}
exit(1);
}
}
IQBuf_t Rx;
unsigned RxWriteCounter=0;
int waittotimeperframe = ((SamplesPerRx*1000000)/hpsdroptions.samplerate)/4;
fprintf (stderr,"\033[0;34m");
fprintf (stderr,"Press any key to exit\n");
fprintf (stderr,"\033[0m");
while(!kbhit() && !sigtermnum) {
if( (Rx = Hermes->GetRxIQ()) != NULL) {
for (int index=0; index<SamplesPerRx; ++index)
for (int receiver=0; receiver < NumRx; ++receiver) {
int indexotp = index*2+(RxWriteCounter* SamplesPerRx * 2);
argvvv[receiver+1]->output_items[indexotp]= *Rx++;
argvvv[receiver+1]->output_items[indexotp+1]= *Rx++;
}
for(int i=1; i<=NumRx; ++i) {
argvvv[i]->RxWriteCounter = RxWriteCounter;
}
++RxWriteCounter; if(RxWriteCounter > hpsdroptions.hermesbuffersize) RxWriteCounter=0;
}else{usleep(waittotimeperframe/10);}
}
Hermes->End();
for(int i=1; i<=NumRx; ++i) {
argvvv[i]->stopth = 1;
}
usleep(500);
exit(0);
}