-
Notifications
You must be signed in to change notification settings - Fork 18
/
qo100websdr.c
392 lines (338 loc) · 9.14 KB
/
qo100websdr.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
/*
* Web based SDR Client for SDRplay and RTLsdr
* =============================================================
* Author: DJ0ABR
*
* (c) DJ0ABR
* www.dj0abr.de
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* q100websdr.c ... file containing main() and calling all other functions
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <unistd.h>
#include <rtl-sdr.h>
#include "sdrplay.h"
#include "audio.h"
#include "wf_univ.h"
#include "websocketserver.h"
#include "setqrg.h"
#include "rtlsdr.h"
#include "fifo.h"
#include "ssbfft.h"
#include "wb_fft.h"
#include "cat.h"
#include "minitiouner.h"
#include "setup.h"
#include "plutodrv.h"
void stop_SDR();
int hwtype = 0; // 1=playSDR, 2=rtlsdr
int samplesPerPacket;
char htmldir[256] = { "." };
int stopped = 0;
void sighandler(int signum)
{
stopped = 1;
printf("signal %d, exit program\n",signum);
stop_SDR();
#ifdef WIDEBAND
remove_udp_minitiouner();
#endif
#ifdef PLUTO
pluto_shutdown();
#endif
save_config();
exit(0);
}
void sighandler_mem(int signum)
{
stopped = 1;
printf("memory error, signal %d, exit program\n",signum);
stop_SDR();
save_config();
exit(0);
}
// check if playSDReshail2 is already running
void isRunning()
{
int num = 0;
char s[256];
sprintf(s,"ps -e | grep playSDReshail2");
FILE *fp = popen(s,"r");
if(fp)
{
// gets the output of the system command
while (fgets(s, sizeof(s)-1, fp) != NULL)
{
if(strstr(s,"playSDReshail2") && !strstr(s,"grep"))
{
if(++num == 2)
{
printf("playSDReshail2 is already running, do not start twice !");
pclose(fp);
exit(0);
}
}
}
pclose(fp);
}
}
// cleans white spaces from beginning, middle and end of a string
char *cleanString(char *str, int cleanspace)
{
static char hs[256];
char *hp = str;
int idx = 0;
// putze den Anfang
while(*hp == ' ' || *hp == ',' || *hp == '\n' || *hp == '\r' || *hp == '\'' || *hp == '\"')
{
hp++;
}
// putze die Mitte
if(cleanspace)
{
while(*hp)
{
if(*hp != ' ' && *hp != ',' && *hp != '\n' && *hp != '\r' && *hp != '\'' && *hp != '\"')
hs[idx++] = *hp;
hp++;
}
}
else
{
while(*hp)
hs[idx++] = *hp++;
}
// putze das Ende
hp = hs+idx-1;
while(*hp == ' ' || *hp == ',' || *hp == '\n' || *hp == '\r' || *hp == '\'' || *hp == '\"')
{
*hp = 0;
hp--;
}
hs[idx] = 0;
return hs;
}
int sret;
void *pret;
void searchHTMLpath()
{
if(htmldir[0] == '.')
{
// search for the apache woking directory
sret = system("find /srv -xdev -name htdocs -print > pfad 2>/dev/null");
sret = system("find /var -xdev -name htdocs -print >> pfad 2>/dev/null");
sret = system("find /var -xdev -name html -print >> pfad 2>/dev/null");
sret = system("find /usr -xdev -name htdocs -print >> pfad 2>/dev/null");
// if the directory was found its name is in file: "pfad"
FILE *fp=fopen("./pfad","r");
if(fp)
{
char p[256];
pret = fgets(p,255,fp);
char *cp= cleanString(p,0);
if(strlen(cp)>3)
{
strcpy(htmldir,cp);
printf("Webserver Path: %s\n",htmldir);
}
else
{
printf("Path to apache webserver files not found\n");
exit(0);
}
fclose(fp);
}
else
{
printf("Path to apache webserver files not found\n");
exit(0);
}
}
}
void installHTMLfiles()
{
// the HTML files are located in the html folder below the es folder
// copy these files into the Apache HTML folder
int i;
if ((i=getuid()))
{
printf("\nYou are not root, HTML files are not automatically copied into the Apache folder! Start this program ONCE with root privileges\n");
return;
}
char fn[512];
snprintf(fn,sizeof(fn),"cp ./html/* %s",htmldir);
printf("copy Web Site files to: %s",htmldir);
FILE *fp = popen(fn,"r");
if(fp)
{
// Liest die Ausgabe von cp
while (fgets(fn, sizeof(fn)-1, fp) != NULL)
{
//printf("Output: %s\n",fn);
}
pclose(fp);
}
}
void calc_system_parameters()
{
// calculate settings
calc_setup();
// load external configuration (if used)
readAMSConfig();
printf("\nSDR parameters:\n\n");
printf("SDR base QRG: %d Hz\n",tuned_frequency);
printf("SDR sample rate: %d S/s\n",SDR_SAMPLE_RATE);
printf("WF width: %d Hz\n",WF_RANGE_HZ);
printf("WF width: %d pixel\n",WF_WIDTH);
printf("1st downsampling:%d S/s\n",(WF_RANGE_HZ * 2));
printf("1st decim. rate: %d\n",SDR_SAMPLE_RATE / (WF_RANGE_HZ * 2));
printf("1st FFT resol.: %d Hz\n",WF_RANGE_HZ / WF_WIDTH);
printf("1st FTT smp/pass:%d\n",((WF_RANGE_HZ * 2) / (WF_RANGE_HZ / WF_WIDTH)));
}
void start_SDR()
{
#ifdef EXTUDP
hwtype = 0;
return;
#else
#ifdef WIDEBAND
#ifdef SDR_PLAY
if(init_SDRplay())
hwtype = 1;
#endif
#ifdef PLUTO
if(init_pluto())
{
hwtype = 3;
}
#endif
#else
// for NB transponder try RTLsdr first
if(init_rtlsdr())
{
hwtype = 2;
samplesPerPacket = SAMPLES_PER_PASS;
}
#ifdef PLUTO
if(init_pluto())
{
hwtype = 3;
}
#endif
#ifdef SDR_PLAY
if(hwtype == 0)
{
if(init_SDRplay())
hwtype = 1;
}
#endif
#endif
if(hwtype == 0)
{
printf("no SDR hardware found.\n");
save_config();
exit(0);
}
#endif
}
void stop_SDR()
{
#ifndef EXTUDP
#ifdef SDR_PLAY
if(hwtype == 1) remove_SDRplay();
#endif
if(hwtype == 2) rtlsdr_close(0);
#endif
}
int main(int argc, char *argv[])
{
// check if it is already running, if yes then exit
isRunning();
// look for the apache HTML path
searchHTMLpath();
// Install or Update the html files
installHTMLfiles();
// signal handler, mainly used if the user presses Ctrl-C
struct sigaction sigact;
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
//sigaction(SIGPIPE, &sigact, NULL); // signal 13
// switch off signal 13 (broken pipe)
// instead handle the return value of the write or send function
signal(SIGPIPE, SIG_IGN);
/*struct sigaction sigact_mem;
sigact_mem.sa_handler = sighandler_mem;
sigemptyset(&sigact_mem.sa_mask);
sigact_mem.sa_flags = 0;
sigaction(SIGSEGV, &sigact_mem, NULL);*/
calc_system_parameters();
#ifdef WIDEBAND
init_fwb();
init_udp_minitiouner();
#else
init_fssb();
#endif // WIDEBAND
// init the FIFO
initpipe();
// init the Websocket Server
ws_init();
// init audio output
#ifdef SOUNDLOCAL
init_soundprocessing();
#endif
// init CAT interface
cat_init();
printf("\nInitialisation complete, system running ... stop with Ctrl-C\n");
// init SDRplay hardware
// this MUST be the LAST initialisation because
// the callback starts immediately after init_SDRplay
start_SDR();
// infinite loop,
// stop program with Ctrl-C
while(1)
{
if(retune_setup)
{
// setup requested new settings of the tuner
retune_setup = 0;
save_config();
calc_system_parameters();
#ifndef EXTUDP
printf("set pluto freq\n");
re_set_freq();
#endif
}
#ifndef EXTUDP
set_frequency();
#endif
if(configrequest)
sendConfigToBrowser();
usleep(1000);
}
removepipe();
save_config();
return 0;
}