forked from dj0abr/QO-100_SSB-WebSDR_DATV-WebSpectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.c
More file actions
476 lines (410 loc) · 12.7 KB
/
setup.c
File metadata and controls
476 lines (410 loc) · 12.7 KB
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
/*
* Web based SDR Client for SDRplay
* =============================================================
* 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.
*
*
* setup.c ... configuration parameters and functions
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <unistd.h>
#include "qo100websdr.h"
#include "beaconlock.h"
#include "civ.h"
#include "websocket/websocketserver.h"
int read_config();
void get_ownIP();
extern char myfullip[20];
char callsign[20] = "CALLSIGN";
int64_t lnb_lo = 0;
uint32_t lnb_crystal = DEFAULT_LNB_CRYSTAL;
int32_t tuned_frequency = 739500000;
int32_t lnb_multiplier = DEFAULT_LNB_MULTIPLIER;
int32_t downmixer_outqrg = DEFAULT_DOWNMIXER_OUTQRG;
int32_t minitiouner_offset = 0;
char mtip[20] = {"192.168.11.22"};
char pluto_ip[20] = {"0"};
int minitiouner_port = 8765;
int minitiouner_local = 1;
int websock_port = DEFAULT_WEBSOCK_PORT;
int allowRemoteAccess = 1;
int configrequest = 0;
int retune_setup = 0;
int tx_correction = 0;
int icom_satmode = 0;
int mt_mode = 1;
void calc_setup()
{
int ret = 0;
ret = read_config();
if(ret == 0)
{
// keep default values from qo100websdr.h as assigned above
printf("using default configuration\n");
}
if(ret == 1)
{
// new frequency settings
// re-tune required
if(downmixer_outqrg == 0)
{
// LO of the LNB's internal mixer in Hz
lnb_lo = (((int64_t)lnb_crystal * (int64_t)lnb_multiplier)/(int64_t)1000);
}
else
{
// sum-LO of the complete chain: LNB and Downmixer in Hz
lnb_lo = (((int64_t)10489 - (int64_t)downmixer_outqrg)*(int64_t)1000000);
}
#ifdef WIDEBAND
// default SDR tuner frequency in Hz, which must be the middle of the spectrum
tuned_frequency = (int32_t)((((int64_t)LEFT_MARGIN_QRG_KHZ + (((int64_t)WF_RANGE_HZ/(int64_t)1000) / (int64_t)2)) * (int64_t)1000) - lnb_lo);
minitiouner_offset = ((lnb_crystal/1000000)*lnb_multiplier);
#else
// default SDR tuner frequency in Hz, which must be the left margin of the spectrum
tuned_frequency = (((int64_t)LEFT_MARGIN_QRG_KHZ * (int64_t)1000) - lnb_lo);
#endif
}
if(ret == 2)
{
// no re-tuning required
}
configrequest = 1; // send config to browser
}
// we need different config files for root and user
// otherwise a user would not be able to write a cfg file created by root
#ifdef WIDEBAND
#define CFG_FILENAME "wb.cfg"
#define CFG_FILENAME_ROOT "wb_admin.cfg"
#else
#define CFG_FILENAME "nb.cfg"
#define CFG_FILENAME_ROOT "nb_admin.cfg"
#endif
// save config values in a readable format in file: wb.cfg
void save_config()
{
FILE *fw;
char cfgfilename[256];
int i;
if((i=getuid()))
// we are a normal user
strcpy(cfgfilename,CFG_FILENAME);
else
// we are root
strcpy(cfgfilename,CFG_FILENAME_ROOT);
printf("save configuration\n");
if((fw = fopen(cfgfilename,"wb")))
{
fprintf(fw,"callsign:%s\n",callsign);
fprintf(fw,"lnb_crystal:%lld\n",(long long int)lnb_crystal);
fprintf(fw,"lnb_multiplier:%lld\n",(long long int)lnb_multiplier);
fprintf(fw,"downmixer_outqrg:%lld\n",(long long int)downmixer_outqrg);
fprintf(fw,"minitiouner_ip:%s\n",mtip);
fprintf(fw,"minitiouner_port:%lld\n",(long long int)minitiouner_port);
fprintf(fw,"minitiouner_local:%lld\n",(long long int)minitiouner_local);
fprintf(fw,"websock_port:%lld\n",(long long int)websock_port);
fprintf(fw,"allowRemoteAccess:%lld\n",(long long int)allowRemoteAccess);
fprintf(fw,"CIV_address:%lld\n",(long long int)civ_adr);
fprintf(fw,"tx_correction:%lld\n",(long long int)tx_correction);
fprintf(fw,"icom_satmode:%lld\n",(long long int)icom_satmode);
fprintf(fw,"pluto_ip:%s\n",pluto_ip);
fprintf(fw,"mt_mode:%lld\n",(long long int)mt_mode);
fclose(fw);
}
else
printf("ERROR: cannot write config file: %s\n",CFG_FILENAME);
}
char *getCfgFilename()
{
static char cfgfilename[256];
int i;
if((i=getuid()))
{
// we are a normal user
strcpy(cfgfilename,CFG_FILENAME);
// if no cfg file is available, use a cfg file previously created by root
FILE *fp = fopen(CFG_FILENAME,"rb");
if(fp)
fclose(fp);
else
{
printf("using config created by root: ");
strcpy(cfgfilename,CFG_FILENAME_ROOT);
}
}
else
{
// we are root
strcpy(cfgfilename,CFG_FILENAME_ROOT);
}
return cfgfilename;
}
int readnum(uint64_t *pnum, char *elem)
{
FILE *fr;
char s[100];
char selem[100];
char snum[100];
if((fr = fopen(getCfgFilename(),"rb")))
{
while(fgets(s,100,fr) != NULL)
{
// search delimiter ':'
char *p = strchr(s,':');
if(p == NULL)
{
// not found, format error in config file
printf("config file format error\n");
fclose(fr);
return 0;
}
// split line
*p = 0;
strcpy(selem,s);
strcpy(snum,p+1);
if(!strncmp(selem,elem,strlen(elem)))
{
*pnum = atoll(snum);
printf("cfg file read: %s = %lld\n",selem,(long long)(*pnum));
fclose(fr);
return 1;
}
}
fclose(fr);
}
else
{
printf("no config file, use defaults\n");
}
printf("config file: element %s not found, using default config\n",elem);
return 0;
}
char *readstring(char *elem)
{
FILE *fr;
char s[100];
char selem[100];
static char sstr[100];
if((fr = fopen(getCfgFilename(),"rb")))
{
while(fgets(s,100,fr) != NULL)
{
// search delimiter ':'
char *p = strchr(s,':');
if(p == NULL)
{
// not found, format error in config file
printf("config file format error\n");
fclose(fr);
return NULL;
}
// split line
*p = 0;
strcpy(selem,s);
strcpy(sstr,p+1);
if(!strncmp(selem,elem,strlen(elem)))
{
// strip CRLF
char *p = strchr(sstr,'\n');
if(p) *p=0;
p = strchr(sstr,'\r');
if(p) *p=0;
printf("cfg file read: %s = %s\n",selem,sstr);
fclose(fr);
return sstr;
}
}
fclose(fr);
}
else
{
printf("no config file, use defaults\n");
return NULL;
}
printf("config file: element %s not found, using default config\n",elem);
return NULL;
}
// 0=no config found use default, 1=new config read re-tuning required, 2=as 1 but no need to re-tune
int read_config()
{
uint64_t num = 0;
static uint32_t old_lnb_crystal = 0;
static int32_t old_lnb_multiplier = 0;
static int32_t old_downmixer_outqrg = 0;
char *p = readstring("callsign");
if(p != NULL) strcpy(callsign,p); else return 0;
if(readnum(&num,"lnb_crystal")) lnb_crystal = num; else return 0;
if(readnum(&num,"lnb_multiplier")) lnb_multiplier = num; else return 0;
if(readnum(&num,"downmixer_outqrg")) downmixer_outqrg = num; else return 0;
p = readstring("minitiouner_ip");
if(p != NULL)
{
strncpy(mtip,p,19);
mtip[19]=0;
}
else return 0;
if(readnum(&num,"minitiouner_port")) minitiouner_port = num; else return 0;
if(readnum(&num,"minitiouner_local")) minitiouner_local = num; else return 0;
if(readnum(&num,"websock_port")) websock_port = num; else return 0;
if(readnum(&num,"allowRemoteAccess")) allowRemoteAccess = num; else return 0;
if(readnum(&num,"CIV_address")) civ_adr = num; else return 0;
if(readnum(&num,"tx_correction")) tx_correction = num; else return 0;
if(readnum(&num,"icom_satmode")) icom_satmode = num; else return 0;
p = readstring("pluto_ip");
if(p != NULL)
{
strncpy(pluto_ip,p,19);
pluto_ip[19]=0;
}
else return 0;
if(readnum(&num,"mt_mode")) mt_mode = num; else return 0;
if( old_lnb_crystal != lnb_crystal ||
old_lnb_multiplier != lnb_multiplier ||
old_downmixer_outqrg != downmixer_outqrg)
{
printf("new QRG settings, re-tune SDR\n");
old_lnb_crystal = lnb_crystal;
old_lnb_multiplier = lnb_multiplier;
old_downmixer_outqrg = downmixer_outqrg;
return 1;
}
return 2;
}
unsigned char cfgdata[256];
int idx = 0;
void insertCfg4(uint32_t v)
{
cfgdata[idx++] = v >> 24;
cfgdata[idx++] = v >> 16;
cfgdata[idx++] = v >> 8;
cfgdata[idx++] = v & 0xff;
}
// strings have a fixed len of CFGSTRLEN bytes, independent of the real length of s
#define CFGSTRLEN 20
void insertCfgString(char *s)
{
memset(cfgdata+idx,0,CFGSTRLEN);
memcpy(cfgdata+idx,s,strlen(s));
idx += CFGSTRLEN;
}
void sendConfigToBrowser()
{
//set default Minitiouner Port and IP
if(strcmp(mtip,"192.168.11.22") == 0)
{
get_ownIP();
strcpy(mtip,myfullip);
}
idx = 2;
configrequest = 0;
insertCfgString(callsign);
insertCfg4(websock_port);
insertCfg4(allowRemoteAccess);
if(lnb_multiplier == 390000)
insertCfg4(25);
else
insertCfg4(27);
insertCfg4(lnb_crystal);
insertCfg4(downmixer_outqrg);
insertCfgString(mtip);
insertCfg4(minitiouner_port);
insertCfg4(minitiouner_local);
insertCfg4(civ_adr);
insertCfg4(tx_correction);
insertCfg4(icom_satmode);
insertCfgString(pluto_ip);
insertCfg4(mt_mode);
ws_send_config(cfgdata, idx);
}
char *hp_elem;
char *he_elem;
int getNextElement(char *s)
{
he_elem = strchr(hp_elem,';');
if(!he_elem)
{
printf("getConfigfromBrowser: format error\n");
return 0;
}
*he_elem = 0;
if(strlen(hp_elem) > 20)
{
printf("getConfigfromBrowser: length error\n");
return 0;
}
strcpy(s,hp_elem);
hp_elem = he_elem+1;
return 1;
}
// called if the user closes the setup window
// Format: "CALLSIGN;8091;1;25;24000000;0;192.168.0.25;6789;1"
void getConfigfromBrowser(char *scfg)
{
char s[20] = {0};
static uint32_t old_lnb_crystal = 0;
static int32_t old_lnb_multiplier = 0;
static int32_t old_downmixer_outqrg = 0;
hp_elem = scfg;
//printf("elem:<%s>\n",scfg);
if(getNextElement(callsign) == 0) return;
if(getNextElement(s) == 0) return;
// websock_port = atoi(s); ignore port from browser, use default port only
if(getNextElement(s) == 0) return;
allowRemoteAccess = atoi(s);
if(getNextElement(s) == 0) return;
int i = atoi(s);
if(i==25) lnb_multiplier=390000; else lnb_multiplier=361111;
if(getNextElement(s) == 0) return;
lnb_crystal = atoi(s);
if(getNextElement(s) == 0) return;
downmixer_outqrg = atoi(s);
if(getNextElement(s) == 0) return;
strncpy(mtip,s,19);
mtip[19] = 0;
if(getNextElement(s) == 0) return;
minitiouner_port = atoi(s);
if(getNextElement(s) == 0) return;
minitiouner_local = atoi(s);
if(getNextElement(s) == 0) return;
civ_adr = atoi(s);
if(getNextElement(s) == 0) return;
tx_correction = atoi(s);
if(getNextElement(s) == 0) return;
icom_satmode = atoi(s);
if(getNextElement(s) == 0) return;
strncpy(pluto_ip,s,19);
pluto_ip[19] = 0;
if(getNextElement(s) == 0) return;
mt_mode = atoi(s);
if( old_lnb_crystal != lnb_crystal ||
old_lnb_multiplier != lnb_multiplier ||
old_downmixer_outqrg != downmixer_outqrg)
{
old_lnb_crystal = lnb_crystal;
old_lnb_multiplier = lnb_multiplier;
old_downmixer_outqrg = downmixer_outqrg;
retune_setup = 1; // calc and install new tuner settings
}
}