aboutsummaryrefslogtreecommitdiffstats
path: root/src/radio/main.c
blob: d576ffeeee03e51573e49f63f649dc77f021ba7b (plain)
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
/* main function
 *
 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * 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 3 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/>.
 */

enum paging_signal;

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <signal.h>
#include <math.h>
#include <termios.h>
#include <unistd.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libsdr/sdr_config.h"
#include "../libsdr/sdr.h"
#include "../libdisplay/display.h"
#include "radio.h"

#define DEFAULT_LO_OFFSET -1000000.0

void *sender_head = NULL;
int use_sdr = 0;
int num_kanal = 1; /* only one channel used for debugging */

void *get_sender_by_empfangsfrequenz() { return NULL; }

static double frequency = 0.0;
static int samplerate = 100000;
static int latency = 30;
static const char *tx_wave_file = NULL;
static const char *rx_wave_file = NULL;
static const char *tx_audiodev = NULL;
static const char *rx_audiodev = NULL;
static enum modulation modulation = MODULATION_NONE;
static int rx = 0, tx = 0;
static double bandwidth_am = 4500.0;
static double bandwidth_fm = 15000.0;
static double bandwidth = 0.0;
static double deviation = 75000.0;
static double modulation_index = 1.0;
static double time_constant_us = 50.0;
static int stereo = 0;
static int rds = 0;
static int rds2 = 0;

/* global variable to quit main loop */
int quit = 0;

void sighandler(int sigset)
{
	if (sigset == SIGHUP)
		return;
	if (sigset == SIGPIPE)
		return;

//	clear_console_text();
	printf("Signal received: %d\n", sigset);

	quit = 1;
}

static int get_char()
{
	struct timeval tv = {0, 0};
	fd_set fds;
	char c = 0;
	int __attribute__((__unused__)) rc;

	FD_ZERO(&fds);
	FD_SET(0, &fds);
	select(0+1, &fds, NULL, NULL, &tv);
	if (FD_ISSET(0, &fds)) {
		rc = read(0, &c, 1);
		return c;
	} else
		return -1;
}

void print_help(const char *arg0)

{
	printf("Usage: %s --sdr-soapy|--sdr-uhd <sdr options> -f <frequency> -M <modulation> -R|-T [options]\n", arg0);
	/*      -                                                                             - */
	printf("\noptions:\n");
	printf(" -f --frequency <frequency>\n");
	printf("        Give frequency in Hertz.\n");
	printf(" -s --samplerate <sample rate>\n");
	printf("        Give signal processing sample rate in Hz. (default = %d)\n", samplerate);
	printf("        This sample rate must be high enough for the signal's spectrum to fit.\n");
	printf("        I will inform you, if this bandwidth is too low.\n");
	printf(" -r --tx-wave-file <filename>\n");
	printf("        Input transmitted audio from wave file\n");
	printf(" -w --rx-wave-file <filename>\n");
	printf("        Output received audio to wave file\n");
	printf(" -a --audio-device hw:<card>,<device>\n");
	printf("        Input audio from sound card's device number\n");
	printf(" -M --modulation fm | am | usb | lsb\n");
	printf("        fm = Frequency modulation to be used for VHF.\n");
	printf("        am = Amplitude modulation to be used for long/medium/short wave.\n");
	printf("        usb = Amplitude modulation with upper side band only.\n");
	printf("        lsb = Amplitude modulation with lower side band only.\n");
	printf(" -R --rx\n");
	printf("        Receive radio signal.\n");
	printf(" -T --tx\n");
	printf("        Transmit radio signal.\n");
	printf(" -B --bandwidth\n");
	printf("        Give bandwidth of audio frequency. (default AM=%.0f FM=%.0f)\n", bandwidth_am, bandwidth_fm);
	printf(" -D --deviation\n");
	printf("        Give deviation of frequency modulated signal. (default %.0f)\n", deviation);
	printf(" -I --modulation-index 0..1\n");
	printf("        Give modulation index of amplitude modulated signal. (default %.0f)\n", deviation);
	printf(" -E --emphasis <uS> | 0\n");
	printf("        Use given time constant of pre- and de-emphasis for frequency\n");
	printf("        modulation. Give 0 to disbale. (default = %.0f uS)\n", time_constant_us);
	printf("        VHF broadcast 50 uS in Europe and 75 uS in the United States.\n");
	printf("        Other radio FM should use 530 uS, to cover complete speech spectrum.\n");
	printf(" -S --stereo\n");
	printf("        Enables stereo carrier for frequency modulated UHF broadcast.\n");
	printf("        It uses the 'Pilot-tone' system.\n");
	sdr_config_print_help();
}

static struct option long_options_common[] = {
	{"help", 0, 0, 'h'},
	{"frequency", 1, 0, 'f'},
	{"samplerate", 1, 0, 's'},
	{"tx-wave-file", 1, 0, 'r'},
	{"rx-wave-file", 1, 0, 'w'},
	{"audio-device", 1, 0, 'a'},
	{"modulation", 1, 0, 'M'},
	{"rx", 0, 0, 'R'},
	{"tx", 0, 0, 'T'},
	{"bandwidth", 1, 0, 'B'},
	{"deviation", 1, 0, 'D'},
	{"modulation-index", 1, 0, 'I'},
	{"emphasis", 1, 0, 'E'},
	{"stereo", 0, 0, 'S'},
	{0, 0, 0, 0}
};

static const char *optstring_common = "hf:s:r:w:a:M:RTB:D:I:E:S";

struct option *long_options;
char *optstring;

static void check_duplicate_option(int num, struct option *option)
{
	int i;

	for (i = 0; i < num; i++) {
		if (long_options[i].val == option->val) {
			fprintf(stderr, "Duplicate option %d. Please fix!\n", option->val);
			abort();
		}
	}
}

void set_options_common(void)
{
	int i = 0, j;

	long_options = calloc(sizeof(*long_options), 256);
	for (j = 0; long_options_common[i].name; i++, j++) {
		check_duplicate_option(i, &long_options_common[j]);
		memcpy(&long_options[i], &long_options_common[j], sizeof(*long_options));
	}
	for (j = 0; sdr_config_long_options[j].name; i++, j++) {
		check_duplicate_option(i, &sdr_config_long_options[j]);
		memcpy(&long_options[i], &sdr_config_long_options[j], sizeof(*long_options));
	}
	
	optstring = calloc(256, 2);
	strcpy(optstring, optstring_common);
	strcat(optstring, sdr_config_optstring);
}

static int handle_options(int argc, char **argv)
{
	int skip_args = 0;
	int rc;

	set_options_common();

	while (1) {
		int option_index = 0, c;

		c = getopt_long(argc, argv, optstring, long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_help(argv[0]);
			exit(0);
		case 'f':
			frequency = atof(optarg);
			skip_args += 2;
			break;
		case 's':
			samplerate = atof(optarg);
			skip_args += 2;
			break;
		case 'r':
			tx_wave_file = strdup(optarg);
			skip_args += 2;
			break;
		case 'w':
			rx_wave_file = strdup(optarg);
			skip_args += 2;
			break;
		case 'a':
			tx_audiodev = strdup(optarg);
			rx_audiodev = strdup(optarg);
			skip_args += 2;
			break;
		case 'M':
			if (!strcasecmp(optarg, "fm"))
				modulation = MODULATION_FM;
			else
			if (!strcasecmp(optarg, "am"))
				modulation = MODULATION_AM_DSB;
			else
			if (!strcasecmp(optarg, "usb"))
				modulation = MODULATION_AM_USB;
			else
			if (!strcasecmp(optarg, "lsb"))
				modulation = MODULATION_AM_LSB;
			else
			{
				fprintf(stderr, "Invalid modulation option, see help!\n");
				exit(0);
			}
			skip_args += 2;
			break;
		case 'R':
			rx = 1;
			skip_args += 1;
			break;
		case 'T':
			tx = 1;
			skip_args += 1;
			break;
		case 'B':
			bandwidth = atof(optarg);
			skip_args += 2;
			break;
		case 'D':
			deviation = atof(optarg);
			skip_args += 2;
			break;
		case 'I':
			modulation_index = atof(optarg);
			if (modulation_index < 0.0 || modulation_index > 1.0) {
				fprintf(stderr, "Invalid modulation index, see help!\n");
				exit(0);
			}
			skip_args += 2;
			break;
		case 'E':
			time_constant_us = atof(optarg);
			skip_args += 2;
			break;
		case 'S':
			stereo = 1;
			skip_args += 1;
			break;
		default:
			rc = sdr_config_opt_switch(c, &skip_args);
			if (rc < 0)
				exit(0);
			break;
		}
	}

	return skip_args;
}

int main(int argc, char *argv[])
{
	int skip_args;
	int rc;
	const char *arg0 = argv[0];
	radio_t radio;
	struct termios term, term_orig;
	int c;
	int latspl;

	debuglevel = 0;

	sdr_config_init(DEFAULT_LO_OFFSET);

	skip_args = handle_options(argc, argv);
	argc -= skip_args + 1;
	argv += skip_args + 1;

	if (frequency == 0.0) {
		printf("No frequency given, I suggest to use 100000000 (100 MHz) and FM\n\n");
		print_help(arg0);
		exit(0);
	}

	rc = sdr_configure(samplerate);
	if (rc < 0)
		return rc;
	if (rc == 0) {
		fprintf(stderr, "Please select SDR, see help!\n");
		exit(0);
	}

	if (modulation == MODULATION_NONE) {
		fprintf(stderr, "Please select modulation, see help!\n");
		exit(0);
	}

	if (bandwidth == 0) {
		if (modulation == MODULATION_FM)
			bandwidth = bandwidth_fm;
		else
			bandwidth = bandwidth_am;
	}

	if (stereo && modulation != MODULATION_FM) {
		fprintf(stderr, "Stereo works with FM only, see help!\n");
		exit(0);
	}
	if (!rx && !tx) {
		fprintf(stderr, "You need to specify --rx (receiver) and/or --tx (transmitter), see help!\n");
		exit(0);
	}
	if (stereo && bandwidth != 15000.0) {
		fprintf(stderr, "Warning: Stereo works with bandwidth of 15 KHz only, using this bandwidth!\n");
	}
	if (stereo && time_constant_us != 75.0 && time_constant_us != 50.0) {
		fprintf(stderr, "Stereo works with time constant of 50 uS or 75 uS only, see help!\n");
		exit(0);
	}

	/* now we have latency and sample rate */
	latspl = samplerate * latency / 1000;

	rc = radio_init(&radio, latspl, samplerate, tx_wave_file, rx_wave_file, (tx) ? tx_audiodev : NULL, (rx) ? rx_audiodev : NULL, modulation, bandwidth, deviation, modulation_index, time_constant_us, stereo, rds, rds2);
	if (rc < 0) {
		fprintf(stderr, "Failed to initialize radio with given options, exitting!\n");
		exit(0);
	}

	void *sdr = NULL;
	float *sendbuff = NULL;

	sendbuff = calloc(latspl * 2, sizeof(*sendbuff));
	if (!sendbuff) {
		fprintf(stderr, "No mem!\n");
		goto error;
	}

	double tx_frequencies[1], rx_frequencies[1];
	tx_frequencies[0] = frequency;
	rx_frequencies[0] = frequency;
	sdr = sdr_open(NULL, tx_frequencies, rx_frequencies, 1, 0.0, samplerate, latspl, 0.0, 0.0);
	if (!sdr)
		goto error;
	sdr_start(sdr);

	/* prepare terminal */
	tcgetattr(0, &term_orig);
	term = term_orig;
	term.c_lflag &= ~(ISIG|ICANON|ECHO);
	term.c_cc[VMIN]=1;
	term.c_cc[VTIME]=2;
	tcsetattr(0, TCSANOW, &term);

	/* catch signals */
	signal(SIGINT, sighandler);
	signal(SIGHUP, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGPIPE, sighandler);

	printf("Starting radio...\n");
	rc = radio_start(&radio);
	if (rc < 0) {
		fprintf(stderr, "Failed to start radio's streaming, exitting!\n");
		goto error;
	}

	int tosend, got;
	while (!quit) {
		usleep(1000);
		got = sdr_read(sdr, (void *)sendbuff, latspl, 0, NULL);
		if (rx) {
			got = radio_rx(&radio, sendbuff, got);
			if (got < 0)
				break;
		}
		if (tx) {
			tosend = sdr_get_tosend(sdr, latspl);
			if (tosend > latspl / 10)
				tosend = latspl / 10;
			if (tosend == 0) {
				continue;
			}
			/* perform radio modulation */
			tosend = radio_tx(&radio, sendbuff, tosend);
			if (tosend < 0)
				break;
			/* write to SDR */
			sdr_write(sdr, (void *)sendbuff, NULL, tosend, NULL, NULL, 0);
		}

		/* process keyboard input */
next_char:
		c = get_char();
		switch (c) {
		case 3:
			/* quit */
//			if (clear_console_text)
//				clear_console_text();
			printf("CTRL+c received, quitting!\n");
			quit = 1;
			goto next_char;
#if 0
- carrier frequency
- deviation
- modulation index
- stereo pilot
		case 'm':
			/* toggle measurements display */
			display_iq_on(0);
			display_spectrum_on(0);
			display_measurements_on(-1);
			goto next_char;
#endif
		case 'q':
			/* toggle IQ display */
			display_measurements_on(0);
			display_spectrum_on(0);
			display_iq_on(-1);
			goto next_char;
		case 's':
			/* toggle spectrum display */
			display_measurements_on(0);
			display_iq_on(0);
			display_spectrum_on(-1);
			goto next_char;
		case 'B':
			calibrate_bias();
			goto next_char;
		}
	}

	/* reset signals */
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);

	/* reset terminal */
	tcsetattr(0, TCSANOW, &term_orig);
	
error:
	free(sendbuff);
	if (sdr)
		sdr_close(sdr);
	radio_exit(&radio);

	return 0;
}