aboutsummaryrefslogtreecommitdiffstats
path: root/src/magnetic/main.c
blob: b247f4477c23592ae9da36dda334f9e828e22ff7 (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
/* main function
 *
 * (C) 2021 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/>.
 */

#ifndef ARDUINO

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "../libsample/sample.h"
#include "../libsound/sound.h"
#include "../libwave/wave.h"
#include "../liblogging/logging.h"
#include "../liboptions/options.h"
#include "../libaaimage/aaimage.h"
#include <osmocom/cc/misc.h>
#include "iso7811.h"

int num_kanal = 1;
static int quit = 0;
#ifdef HAVE_ALSA
static void *sound = NULL;
static int dsp_buffer = 50;
#endif
static int dsp_samplerate = 48000;
static const char *dsp_audiodev = "hw:0,0";
static const char *wave_file = NULL;
static int baudrate = 2666;
static const char *sicherung = "12345";

/* Measurements done in summer 2021 with an original card, applied with iron oxyde. */
/* Conforms to Track 3 (210 bpi) with 60 bits lead-in, 20 digits data, about 550 bits lead out */
/* Note that LEAD_OUT here is longer, because the switch must be manually pressed during lead-out. */
#define CNETZ_LEAD_IN		12	/* number of zero-digits before start sentinel (60 bits) */
#define CNETZ_LEAD_OUT		150	/* number of zero-digits after LRC sentinel */
#define CNETZ_SWITCH_ON		27	/* switch closing during lead-out, in digit-duration */ 
#define CNETZ_SWITCH_OFF	42	/* switch opening during lead-out, in digit-duration */ 

static void print_help(const char *arg0)
{
	printf("Usage: %s [options] -a hw:0,0 <number> | service\n", arg0);
	/*      -                                                                             - */
	printf("General options:\n");
	printf(" -h --help\n");
	printf("        This help\n");
	printf(" --config [~/]<path to config file>\n");
	printf("        Give a config file to use. If it starts with '~/', path is at home dir.\n");
	printf("        Each line in config file is one option, '-' or '--' must not be given!\n");
	logging_print_help();
	printf(" -a --audio-device hw:<card>,<device>\n");
	printf("        Input audio from sound card's device number\n");
	printf(" -s --samplerate <sample rate>\n");
	printf("        Give audio device sample rate in Hz. (default = %d)\n", dsp_samplerate);
	printf(" -w --write-wave <filename>\n");
	printf("        Output sound as wave file\n");
	printf("\nMagnetic card simulator options:\n");
	printf(" -B --baud-rate <baud>\n");
	printf("        Playback baud rate (default = %d)\n", baudrate);
	printf(" -S --sicherung <security code>\n");
	printf("        Card's security code for simple authentication (default = '%s')\n", sicherung);
	printf("\n<number>: Give any valid 7 digit (optionally 8 digit) subscriber number. May\n");
	printf("        be prefixed with 0160.\n");
	printf("\n'service': BSA44 service card (to unlock phone after battery replacement)\n");
}

static void add_options(void)
{
	option_add('h', "help", 0);
	option_add('v', "debug", 1);
	option_add('a', "audio-device", 1);
	option_add('s', "samplerate", 1);
	option_add('w', "write-wave", 1);
	option_add('B', "baud-rate", 1);
	option_add('S', "sicherung", 1);
};

static int handle_options(int short_option, int argi, char **argv)
{
	int rc;

	switch (short_option) {
	case 'h':
		print_help(argv[0]);
		return 0;
	case 'v':
		rc = parse_logging_opt(argv[argi]);
		if (rc > 0)
			return 0;
		if (rc < 0) {
			fprintf(stderr, "Failed to parse logging option, please use -h for help.\n");
			return rc;
		}
		break;
	case 'a':
		dsp_audiodev = options_strdup(argv[argi]);
		break;
	case 's':
		dsp_samplerate = atof(argv[argi]);
		break;
	case 'w':
		wave_file = options_strdup(argv[argi]);
		break;
	case 'B':
		baudrate = atoi(argv[argi]);
		break;
	case 'S':
		sicherung = options_strdup(argv[argi]);
		break;
	default:
		return -EINVAL;
	}

	return 1;
}

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

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

	quit = -1;
}

int main(int argc, char *argv[])
{
	const char *number;
	char string[19];
	uint8_t data[CNETZ_LEAD_IN + 21 + CNETZ_LEAD_OUT];
	int length;
	int rc, argi;
	int i, j;

	loglevel = LOGL_INFO;
	logging_init();

	add_options();
	rc = options_config_file(argc, argv, "~/.osmocom/analog/magnetic.conf", handle_options);
	if (rc < 0)
		return 0;

	/* parse command line */
	argi = options_command_line(argc, argv, handle_options);
	if (argi <= 0)
		return argi;

	if (argi >= argc) {
		fprintf(stderr, "Expecting phone number, use '-h' for help!\n");
		return 0;
	} else if (!strcmp(argv[argi], "service")) {
		bsa44_service(string);
	} else {
		number = argv[argi];
		/* remove prefix, if given */
		if (strlen(number) >= 10 && !strncmp(number, "0160", 4))
			number += 4;
		if (strlen(number) < 7 || strlen(number) > 8) {
			fprintf(stderr, "Expecting phone number to be 7 or 8 digits, use '-h' for help!\n");
			return 0;
		}
		for (i = 0; number[i]; i++) {
			if (number[0] < '0' || number[i] > '9') {
				fprintf(stderr, "Given phone number has invalid digits, use '-h' for help!\n");
				return 0;
			}
		}
		if (number[0] > '7') {
inval_number:
			fprintf(stderr, "Given digits of phone number are out of range for 'C-Netz', use '-h' for help!\n");
			return 0;
		}
		if (strlen(number) == 8) {
			if ((number[1] - '0') * 10 + (number[2] - '0') > 31)
				goto inval_number;
			if (atoi(number + 3) > 65535)
				goto inval_number;
		} else {
			if (atoi(number + 2) > 65535)
				goto inval_number;
		}
		for (i = 0; sicherung[i]; i++) {
			if (sicherung[0] < '0' || sicherung[i] > '9') {
				fprintf(stderr, "Given security code has invalid digits, use '-h' for help!\n");
				return 0;
			}
		}
		if (!sicherung[0] || (sicherung[0] == '0' && sicherung[1] == '0') || atoi(sicherung) > 65535) {
			fprintf(stderr, "Given security code is out of range, use '-h' for help!\n");
			return 0;
		}
		cnetz_card(string, number, sicherung);
	}

	length = encode_track(data, string, CNETZ_LEAD_IN, CNETZ_LEAD_OUT);
	if (length > (int)sizeof(data)) {
		fprintf(stderr, "Software error: Array too small, PLEASE FIX!\n");
		return -1;
	}

	/* alloc space depending on bit rate (length of half-bit: round up to next integer) */
	int samples_per_halfbit = (dsp_samplerate + (baudrate * 2) - 1) / (baudrate * 2);
	int total_samples = samples_per_halfbit * 2 * 5 * length;
	sample_t sample[total_samples], *samples[1], silence[dsp_samplerate], level = 1;
#ifdef HAVE_ALSA
	int switch_on_samples = samples_per_halfbit * 2 * 5 * (CNETZ_LEAD_IN + 21 + CNETZ_SWITCH_ON);
	int switch_off_samples = samples_per_halfbit * 2 * 5 * (CNETZ_LEAD_IN + 21 + CNETZ_SWITCH_OFF);
	int buffer_size = dsp_samplerate * dsp_buffer / 1000;
#endif

	/* generate sample */
	int s, ss = 0;
	for (i = 0; i < length; i++) {
		for (j = 0; j < 5; j++) {
			level = -level;
			for (s = 0; s < samples_per_halfbit; s++)
				sample[ss++] = level;
			if (((data[i] >> j) & 1))
				level = -level;
			for (s = 0; s < samples_per_halfbit; s++)
				sample[ss++] = level;
		}
	}
	memset(silence, 0, sizeof(silence));

	LOGP(DDSP, LOGL_INFO, "Total bits: %d\n", length * 5);
	LOGP(DDSP, LOGL_INFO, "Samples per bit: %d\n", samples_per_halfbit * 2);
	LOGP(DDSP, LOGL_INFO, "Total samples: %d (duration: %.3f seconds)\n", total_samples, (double)total_samples / (double)dsp_samplerate);

	if (wave_file) {
		wave_rec_t wave_rec;

		/* open wave file */
		rc = wave_create_record(&wave_rec, wave_file, dsp_samplerate, 1, 1.0);
		if (rc < 0) {
			LOGP(DRADIO, LOGL_ERROR, "Failed to create WAVE record instance!\n");
			goto error;
		}
		samples[0] = silence;
		wave_write(&wave_rec, samples, dsp_samplerate / 2);
		samples[0] = sample;
		wave_write(&wave_rec, samples, total_samples);
		samples[0] = silence;
		wave_write(&wave_rec, samples, dsp_samplerate / 2);
		wave_destroy_record(&wave_rec);
		goto done;
	}

#ifdef HAVE_ALSA
	/* open audio device */
	sound = sound_open(SOUND_DIR_PLAY, dsp_audiodev, NULL, NULL, NULL, 1, 0.0, dsp_samplerate, buffer_size, 1.0, 1.0, 0.0, 2.0);
	if (!sound) {
		rc = -EIO;
		LOGP(DRADIO, LOGL_ERROR, "Failed to open sound device!\n");
		goto error;
	}
#else
	rc = -ENOTSUP;
	LOGP(DRADIO, LOGL_ERROR, "No sound card support compiled in!\n");
	goto error;
#endif

	print_aaimage();
	printf("String to send: ;%s?\n", string);
	for (i = 0; i < 5; i++) {
		if (i < 4)
			printf("2^%d: ...", i);
		else
			printf("Par: ...");
		for (j = CNETZ_LEAD_IN - 4; j < CNETZ_LEAD_IN + 4 + 21; j++)
			printf(" %d", (data[j] >> i) & 1);
		printf(" ...\n");
	}

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

#ifdef HAVE_ALSA
	sound_start(sound);

	int count;
	while (!quit) {
		ss = 0;
		while (!quit) {
			usleep(1000);
			count = sound_get_tosend(sound, buffer_size);
			if (count <= 0)
				continue;
			samples[0] = silence + ss;
			ss += count;
			if (ss > dsp_samplerate) {
				count -= ss - dsp_samplerate;
				ss = dsp_samplerate;
			}
			sound_write(sound, samples, NULL, count, NULL, NULL, 1);
			if (ss == dsp_samplerate)
				break;
		}
		printf("\033[0;32m  -> \033[1;31mTX\033[0;32m <-\033[0;39m\r"); fflush(stdout);
		ss = 0;
		while (!quit) {
			usleep(1000);
			count = sound_get_tosend(sound, buffer_size);
			if (count <= 0)
				continue;
			if ((ss >= 0 && ss < count) || (ss - switch_off_samples >= 0 && ss - switch_off_samples < count)) {
				printf("\033[0;32m  -> \033[1;31mTX\033[0;32m <-             \033[0;39m\r"); fflush(stdout);
			}
			if (ss - switch_on_samples >= 0 && ss - switch_on_samples < count) {
				printf("\033[0;32m  -> \033[1;31mTX\033[0;32m <-  -> \033[1;33mCLICK\033[0;32m <-\033[0;39m\r"); fflush(stdout);
			}
			samples[0] = sample + ss;
			ss += count;
			if (ss > total_samples) {
				count -= ss - total_samples;
				ss = total_samples;
			}
			sound_write(sound, samples, NULL, count, NULL, NULL, 1);
			if (ss == total_samples)
				break;
		}
		printf("                                 \r"); fflush(stdout);
	}
#endif

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

error:
#ifdef HAVE_ALSA
	if (sound)
		sound_close(sound);
#endif

done:
	options_free();

	return 0;
}

void osmo_cc_set_log_cat(int __attribute__((unused)) cc_log_cat) {}

void display_measurements_add(void);
void *display_measurements_update(void);
void *get_sender_by_empfangsfrequenz(void);
void display_measurements_add(void) {}
void *display_measurements_update(void) { return NULL; }
void *get_sender_by_empfangsfrequenz(void) { return NULL; }

#endif /* ARDUINO */