aboutsummaryrefslogtreecommitdiffstats
path: root/src/nmt/main.c
blob: d29ee628a4f2c56c309e19c083f70f0f4b0ec506 (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
/* NMT main
 *
 * (C) 2016 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 <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "../common/sample.h"
#include "../common/main.h"
#include "../common/debug.h"
#include "../common/timer.h"
#include "../common/mncc_sock.h"
#include "nmt.h"
#include "frame.h"
#include "dsp.h"
#include "image.h"
#include "tones.h"
#include "announcement.h"
#include "countries.h"

#define SMS_DELIVER "/tmp/nmt_sms_deliver"
#define SMS_SUBMIT "/tmp/nmt_sms_submit"
static int sms_deliver_fd = -1;

/* settings */
int nmt_system = 450;
int num_chan_type = 0;
enum nmt_chan_type chan_type[MAX_SENDER] = { CHAN_TYPE_CC_TC };
int ms_power = 1; /* 0..3 */
char country[16] = "";
char traffic_area[3] = "";
char area_no = 0;
int compandor = 1;
int num_supervisory = 0;
int supervisory[MAX_SENDER] = { 1 };
const char *smsc_number = "767";
int send_callerid = 0;

void print_help(const char *arg0)
{
	print_help_common(arg0, "[-N 900] -Y <traffic area> | list  [-I 1] [-0 1] ");
	/*      -                                                                             - */
	printf(" -N --nmt-system 450/900\n");
	printf("        Give NMT type as first paramer. (default = '%d')\n", nmt_system);
	printf("        Note: This option must be given at first!\n");
	printf(" -T --channel-type <channel type> | list\n");
	printf("        Give channel type, use 'list' to get a list. (default = '%s')\n", chan_type_short_name(nmt_system, chan_type[0]));
	printf(" -P --ms-power <power level>\n");
	printf("        Give power level of the mobile station 0..3. (default = '%d')\n", ms_power);
    if (nmt_system == 450)
	printf("        NMT-450: 3 = 15 W / 7 W (handheld), 2 = 1.5 W, 1/0 = 150 mW\n");
    else
	printf("        NMT-900: 3/2 = 6 W, 1 = 1.5 W, 0 = 150 mW\n");
	printf(" -Y --traffic-area <traffic area> | list\n");
	printf("        NOTE: MUST MATCH WITH YOUR ROAMING SETTINGS IN THE PHONE!\n");
	printf("              Your phone will not connect, if country code is different!\n");
	printf("        Give short country code and traffic area seperated by comma.\n");
	printf("        (Example: Give 'SE,1' for Sweden, traffic area 1)\n");
	printf("        Add '!' to force traffic area that is not supported by country.\n");
	printf("        (Example: Give 'B,12!' for Belgium, traffic area 12)\n");
	printf("        Use 'list' to get a list of available country code names\n");
	printf(" -A --area-number <area no> | 0\n");
	printf("        Give area number 1..4 or 0 for no area number. (default = '%d')\n", area_no);
	printf(" -C --compandor 1 | 0\n");
	printf("        Make use of the compandor to reduce noise during call. (default = '%d')\n", compandor);
	printf(" -0 --supervisory 1..4 | 0\n");
	printf("        Use supervisory signal 1..4 to detect loss of signal from mobile\n");
	printf("        station, use 0 to disable. (default = '%d')\n", supervisory[0]);
	printf(" -S --smsc-number <digits>\n");
	printf("        If this number is dialed, the mobile is connected to the SMSC (Short\n");
	printf("        Message Service Center). (default = '%s')\n", smsc_number);
	printf(" -I --caller-id 1 | 0\n");
	printf("        If set, the caller ID is sent while ringing the phone. (default = '%d')\n", send_callerid);
	printf("\nstation-id: Give 7 digits of station-id, you don't need to enter it\n");
	printf("        for every start of this program.\n");
	print_hotkeys_common();
}

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

	static struct option long_options_special[] = {
		{"nmt-system", 1, 0, 'N'},
		{"channel-type", 1, 0, 'T'},
		{"ms-power", 1, 0, 'P'},
		{"traffic-area", 1, 0, 'Y'},
		{"area-number", 1, 0, 'A'},
		{"compandor", 1, 0, 'C'},
		{"supervisory", 1, 0, '0'},
		{"smsc-number", 1, 0, 'S'},
		{"caller-id", 1, 0, 'I'},
		{0, 0, 0, 0}
	};

	set_options_common("N:T:P:Y:A:C:0:S:I:", long_options_special);

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

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

		if (c == -1)
			break;

		switch (c) {
		case 'N':
			nmt_system = atoi(optarg);
			if (nmt_system != 450 && nmt_system != 900) {
				fprintf(stderr, "Error, NMT system type '%s' unknown. Please use '-N 450' for NMT-450 or '-N 900' for NMT-900.\n", optarg);
				exit(0);
			}
			if (nmt_system == 900)
				ms_power = 0;
			if (!first_option) {
				fprintf(stderr, "Please specify the NMT system (-N) as first command line option!\n");
				exit(0);
			}
			skip_args += 2;
			break;
		case 'T':
			if (!strcmp(optarg, "list")) {
				nmt_channel_list(nmt_system);
				exit(0);
			}
			rc = nmt_channel_by_short_name(nmt_system, optarg);
			if (rc < 0) {
				fprintf(stderr, "Error, channel type '%s' unknown. Please use '-t list' to get a list. I suggest to use the default.\n", optarg);
				exit(0);
			}
			OPT_ARRAY(num_chan_type, chan_type, rc)
			skip_args += 2;
			break;
		case 'P':
			ms_power = atoi(optarg);
			if (ms_power > 3)
				ms_power = 3;
			if (ms_power < 0)
				ms_power = 0;
			skip_args += 2;
			break;
		case 'Y':

			if (!strcmp(optarg, "list")) {
				nmt_country_list(nmt_system);
				exit(0);
			}
			/* digits */
			strncpy(country, optarg, sizeof(country) - 1);
			country[sizeof(country) - 1] = '\0';
			p = strchr(country, ',');
			if (!p) {
				fprintf(stderr, "Illegal traffic area '%s', see '-h' for help\n", optarg);
				exit(0);
			}
			*p++ = '\0';
			rc = nmt_country_by_short_name(nmt_system, country);
			if (rc < 0) {
error_ta:
				fprintf(stderr, "Invalid traffic area '%s', use '-Y list' for a list of valid areas\n", optarg);
				exit(0);
			}
			traffic_area[0] = rc + '0';
			if (p[strlen(p) - 1] != '!') {
				rc = nmt_ta_by_short_name(nmt_system, country, atoi(p));
				if (rc < 0)
					goto error_ta;
			}
			nmt_value2digits(atoi(p), traffic_area + 1, 1);
			traffic_area[2] = '\0';
			skip_args += 2;
			break;
		case 'A':
			area_no = optarg[0] - '0';
			if (area_no > 4) {
				fprintf(stderr, "Area number '%s' out of range, please use 1..4 or 0 for no area\n", optarg);
				exit(0);
			}
			skip_args += 2;
			break;
		case 'C':
			compandor = atoi(optarg);
			skip_args += 2;
			break;
		case '0':
			super = atoi(optarg);
			if (super < 0 || super > 4) {
				fprintf(stderr, "Given supervisory signal is wrong, use '-h' for help!\n");
				exit(0);
			}
			OPT_ARRAY(num_supervisory, supervisory, super)
			skip_args += 2;
			break;
		case 'S':
			smsc_number = strdup(optarg);
			skip_args += 2;
			break;
		case 'I':
			send_callerid = atoi(optarg);
			skip_args += 2;
			break;
		default:
			opt_switch_common(c, argv[0], &skip_args);
		}
		first_option = 0;
	}

	free(long_options);

	return skip_args;
}

static void myhandler(void)
{
	static char buffer[256];
	static int pos = 0, rc, i;
	int space = sizeof(buffer) - pos;

	rc = read(sms_deliver_fd, buffer + pos, space);
	if (rc > 0) {
		pos += rc;
		if (pos == space) {
			fprintf(stderr, "SMS buffer overflow!\n");
			pos = 0;
		}
		/* check for end of line */
		for (i = 0; i < pos; i++) {
			if (buffer[i] == '\r' || buffer[i] == '\n')
				break;
		}
		/* send sms */
		if (i < pos) {
			buffer[i] = '\0';
			pos = 0;
			deliver_sms(buffer);
		}
	}
}

int submit_sms(const char *sms)
{
	FILE *fp;

	fp = fopen(SMS_SUBMIT, "a");
	if (!fp) {
		fprintf(stderr, "Failed to open SMS submit file '%s'!\n", SMS_SUBMIT);
		return -1;
	}

	fprintf(fp, "%s\n", sms);

	fclose(fp);

	return 0;
}

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

	/* init common tones */
	init_nmt_tones();
	init_announcement();

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

	if (argc > 1) {
		station_id = argv[1];
		if (strlen(station_id) != 7) {
			printf("Given station ID '%s' does not have 7 digits\n", station_id);
			return 0;
		}
	}

	if (!num_kanal) {
		printf("No channel (\"Kanal\") is specified, I suggest channel 1 (-k 1).\n\n");
		mandatory = 1;
	}
	if (use_sdr) {
		/* set audiodev */
		for (i = 0; i < num_kanal; i++)
			audiodev[i] = "sdr";
		num_audiodev = num_kanal;
		/* set chan_type */
		if (num_chan_type == 0) {
			for (i = 0; i < num_kanal; i++)
				chan_type[i] = CHAN_TYPE_CC_TC;
			num_chan_type = num_kanal;
		}
		if (num_supervisory == 0) {
			/* set supervisory signal */
			for (i = 0; i < num_kanal; i++)
				supervisory[i] = (i % 4) + 1;
			num_supervisory = 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);
	}
	if (num_kanal == 1 && num_chan_type == 0)
		num_chan_type = 1; /* use default */
	if (num_kanal != num_chan_type) {
		fprintf(stderr, "You need to specify as many channel types as you have channels.\n");
		exit(0);
	}
	if (num_kanal == 1 && num_supervisory == 0)
		num_supervisory = 1; /* use default */
	if (num_kanal != num_supervisory) {
		fprintf(stderr, "You need to specify as many supervisory signals as you have channels.\n");
		fprintf(stderr, "They shall be different at channels that are close to each other.\n");
		exit(0);
	}
	if (num_kanal) {
		uint8_t super[5] = { 0, 0, 0, 0, 0 };

		for (i = 0; i < num_kanal; i++) {
			if (supervisory[i] == 0) {
				fprintf(stderr, "No supervisory signal given for channel %d. This is ok, but signal loss dannot be detected. \n", kanal[i]);
				continue;
			}
			if (super[supervisory[i]]) {
				fprintf(stderr, "Supervisory signal %d is selected for both cannels #%d and #%d. I advice to use different signal, to avoid co-channel interferences.\n", supervisory[i], kanal[i], super[supervisory[i]]);
				continue;
			}
			super[supervisory[i]] = kanal[i];
		}
	}

	if (!traffic_area[0]) {
		printf("No traffic area is specified, I suggest to use Sweden (-Y SE,1) and set the phone's roaming to 'SE' also.\n\n");
		mandatory = 1;
	}

	if (mandatory) {
		print_help(argv[-skip_args]);
		return 0;
	}

	/* create pipe for SMS delivery */
	unlink(SMS_DELIVER);
	rc = mkfifo(SMS_DELIVER, 0666);
	if (rc < 0) {
		fprintf(stderr, "Failed to create SMS deliver FIFO '%s'!\n", SMS_DELIVER);
		goto fail;
	} else {
		sms_deliver_fd = open(SMS_DELIVER, O_RDONLY | O_NONBLOCK);
		if (sms_deliver_fd < 0) {
			fprintf(stderr, "Failed to open SMS deliver FIFO! '%s'\n", SMS_DELIVER);
			goto fail;
		}
	}

	if (!loopback)
		print_image();

	/* init functions */
	rc = init_frame();
	if (rc < 0) {
		fprintf(stderr, "Failed to setup frames. Quitting!\n");
		return -1;
	}
	dsp_init();

	/* SDR always requires emphasis */
	if (use_sdr) {
		do_pre_emphasis = 1;
		do_de_emphasis = 1;
	}

	/* create transceiver instance */
	for (i = 0; i < num_kanal; i++) {
		rc = nmt_create(nmt_system, country, kanal[i], (loopback) ? CHAN_TYPE_TEST : chan_type[i], audiodev[i], use_sdr, samplerate, rx_gain, do_pre_emphasis, do_de_emphasis, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, ms_power, nmt_digits2value(traffic_area, 2), area_no, compandor, supervisory[i], smsc_number, send_callerid, loopback);
		if (rc < 0) {
			fprintf(stderr, "Failed to create transceiver instance. Quitting!\n");
			goto fail;
		}
		printf("base station on channel %d ready, please tune transmitter to %.4f MHz and receiver to %.4f MHz.\n", kanal[i], nmt_channel2freq(nmt_system, country, kanal[i], 0, NULL, NULL, NULL) / 1e6, nmt_channel2freq(nmt_system, country, kanal[i], 1, NULL, NULL, NULL) / 1e6);
	}

	nmt_check_channels(nmt_system);

	main_common(&quit, latency, interval, myhandler, station_id, 7);

fail:
	/* fifo */
	if (sms_deliver_fd > 0)
		close(sms_deliver_fd);
	unlink(SMS_DELIVER);

	/* destroy transceiver instance */
	while (sender_head)
		nmt_destroy(sender_head);

	return 0;
}

// dummy, will be replaced by DMS test program
void test_dms_frame(const char __attribute__((unused)) *frame, int __attribute__((unused)) length) {}