aboutsummaryrefslogtreecommitdiffstats
path: root/src/eurosignal/main.c
blob: fbc7ebb1f796e4da432df2ba8b4845ed9b6966e0 (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
/* Eurosignal main
 *
 * (C) 2019 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/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libmobile/call.h"
#include "../libmobile/main_mobile.h"
#include "../liboptions/options.h"
#include "eurosignal.h"
#include "dsp.h"
#include "../anetz/besetztton.h"
#include "es_mitte.h"
#include "es_ges.h"
#include "es_teilges.h"
#include "es_kaudn.h"


static int fm = 0;		/* use fm */
static int tx = 0;		/* we transmit */
static int rx = 0;		/* we receive */
static int repeat = 4;		/* repeat given ID */
static int degraded = 0;	/* if station is degraded */
static int random_id = 0;	/* transmit pseudo random pattern */
static uint32_t scan_from = 0;
static uint32_t scan_to = 0;

void print_help(const char *arg0)
{
	main_mobile_print_help(arg0, "[--random] ");
	/*      -                                                                             - */
	printf(" -F --fm\n");
	printf("        Use frequency modulation instead of amplitude modulation. The carrier\n");
	printf("        frequency will be 7.5 KHz above/below the actual frequency. This\n");
	printf("        ensures that the change in frequency results as a change in amplitude\n");
	printf("        at the demodulator of the receiver.\n");
	printf(" -T --tx\n");
	printf("        Transmit Eurosignal on given channel, to page a receiver. (default)\n");
	printf(" -R --rx\n");
	printf("        Receive Eurosignal on given channel, so we are the receiver.\n");
	printf("        If none of the options -T nor -R is given, only transmitter is enabled.\n");
	printf(" -I --id <id> [-I ...]\n");
	printf("        Give one or more IDs to allow only the given IDs to be transmitted and\n");
	printf("        received. This option can be repeated many times. If this option is not\n");
	printf("        specified, any ID is allowed to transmit.\n");
	printf("        Also, any given ID will cause a beep when received. This requires call\n");
	printf("        device (sound card) to be defined or MNCC interface. (The caller ID will\n");
	printf("        be the received pager ID. The called number will be '1' for the first\n");
	printf("        ID given, '2' for scond, ...)\n");
	printf(" -D --degraded\n");
	printf("        Play the anouncement that the system is degraded due to failure of one\n");
	printf("        or more transmitters. If the caller hangs up during or rigt after the\n");
	printf("        announcement, no paging is performed.\n");
	printf(" -S --scan <from> <to>\n");
	printf("        Scan through given IDs once (no repetition). This can be useful to find\n");
	printf("        the ID of a vintage receiver. Note that scanning all IDs from 000000\n");
	printf("        through 999999 would take almost 10 Days.\n");
	printf("    --random\n");
	printf("        Repeat some pseudo random IDs, to make the transmitted signal sound\n");
	printf("        authentic and as it would handle many calls. It will still be possible\n");
	printf("        to page a recevier.\n");
	printf("    --repeat <num>\n");
	printf("        Repead paging ID <num> times when transmitting. (default = %d)\n", repeat);
	printf("\nstation-id: Give 6 digit station-id, you don't need to enter it for every\n");
	printf("        start of this program.\n");
	main_mobile_print_hotkeys();
}

#define	OPT_RANDOM	255
#define	OPT_REPEAT	258

static void add_options(void)
{
	main_mobile_add_options();
	option_add('F', "fm", 0);
	option_add('T', "tx", 0);
	option_add('R', "rx", 0);
	option_add('I', "id", 1);
	option_add('D', "degraded", 0);
	option_add('S', "scan", 2);
	option_add(OPT_RANDOM, "random", 0);
	option_add(OPT_REPEAT, "repeat", 1);
}

static int check_id(const char *id)
{
	int i;

	if (strlen(id) != 6) {
		fprintf(stderr, "Given paging ID must have exactly 6 digits!\n");
		return -EINVAL;
	}

	for (i = 0; i < 6; i++) {
		if (id[i] < '0' || id[i] > '9') {
			fprintf(stderr, "Given paging ID must have digits (0..9) only!\n");
			return -EINVAL;
		}
	}

	return 0;
}

static int handle_options(int short_option, int argi, char **argv)
{
	switch (short_option) {
	case 'F':
		fm = 1;
		break;
	case 'T':
		tx = 1;
		break;
	case 'R':
		rx = 1;
		break;
	case 'I':
		if (check_id(argv[argi]))
			return -EINVAL;
		euro_add_id(argv[argi]);
		break;
	case 'D':
		degraded = 1;
		break;
	case 'S':
		if (check_id(argv[argi]))
			return -EINVAL;
		scan_from = atoi(argv[argi++]);
		if (check_id(argv[argi]))
			return -EINVAL;
		scan_to = atoi(argv[argi++]) + 1;
		break;
	case OPT_RANDOM:
		random_id = 1;
		break;
	case OPT_REPEAT:
		repeat = atoi(argv[argi]);
		break;
	default:
		return main_mobile_handle_options(short_option, argi, argv);
	}

	return 1;
}

int main(int argc, char *argv[])
{
	int rc, argi;
	const char *station_id = "";
	int i;

	/* init common tones */
	init_besetzton();

	/* init system specific tones */
	init_es_mitte();
	init_es_ges();
	init_es_teilges();
	init_es_kaudn();

	/* init mobile interface */
	console_digits = "0123456789ABCDE";
	main_mobile_init();

	/* handle options / config file */
	add_options();
	rc = options_config_file("~/.osmocom/analog/eurosignal.conf", handle_options);
	if (rc < 0)
		return 0;
	argi = options_command_line(argc, argv, handle_options);
	if (argi <= 0)
		return argi;

	if (argi < argc) {
		station_id = argv[argi];
		if (strlen(station_id) != 6) {
			printf("Given receiver ID '%s' does not have 6 digits\n", station_id);
			return 0;
		}
	}

	if (!num_kanal) {
		printf("No channel (\"Kanal\") is specified, Use '-k list' to get a list of all channels.\n\n");
		print_help(argv[0]);
		return 0;
	}
	if (!strcasecmp(kanal[0], "list")) {
		euro_list_channels();
		goto fail;
	}
	if (use_sdr) {
		/* set audiodev */
		for (i = 0; i < num_kanal; i++)
			audiodev[i] = "sdr";
		num_audiodev = num_kanal;
	}
	if (num_kanal == 1 && num_audiodev == 0)
		num_audiodev = 1; /* use default */
	if (num_kanal != num_audiodev) {
		fprintf(stderr, "You need to specify as many sound devices as you have channels.\n");
		exit(0);
	}

	/* inits */
	fm_init(fast_math);
	dsp_init(samplerate);
	euro_init();

	/* TX is default */
	if (!tx && !rx)
		tx = 1;

	/* create transceiver instance */
	for (i = 0; i < num_kanal; i++) {
		rc = euro_create(kanal[i], audiodev[i], use_sdr, samplerate, rx_gain, tx_gain, fm, tx, rx, repeat, degraded, random_id, scan_from, scan_to, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback);
		if (rc < 0) {
			fprintf(stderr, "Failed to create \"Sender\" instance. Quitting!\n");
			goto fail;
		}
		printf("Base station for channel %s ready, please tune transmitter and/or receiver to %.4f MHz\n", kanal[i], euro_kanal2freq(kanal[i], fm) / 1e6);
	}

	main_mobile(&quit, latency, interval, NULL, station_id, 6);

fail:
	/* destroy transceiver instance */
	while(sender_head)
		euro_destroy(sender_head);

	/* exits */
	fm_exit();
	euro_exit();

	return 0;
}