aboutsummaryrefslogtreecommitdiffstats
path: root/src/eurosignal/eurosignal.c
blob: 661f6dc2a75d8b2717266628ceffb19600c62178 (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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/* Eurosignal protocol handling
 *
 * (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/>.
 */

#define CHAN euro->sender.kanal

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "../libsample/sample.h"
#include "../libdebug/debug.h"
#include "../libmobile/call.h"
#include "../libmncc/cause.h"
#include "eurosignal.h"
#include "dsp.h"

/* anouncement timers */
#define ANSWER_TIME		1.0	/* wait after answer */
#define OOO_TIME		3.8	/* announcement 1.7 s, pause 2.1 s */
#define UNASSIGNED_TIME1	2.2	/* announcement 2.2 s s */
#define UNASSIGNED_TIME2	2.9	/* announcement 2.2 s, pause 0.7 s */
#define DEGRADED_TIME		4.95	/* announcement 2.25 s, pause 2.7 s */
#define ACKNOWLEDGE_TIME1	2.8	/* announcement 1.7 s, pause 1.1 s */
#define ACKNOWLEDGE_TIME2	4.6	/* announcement 1.7 s, pause 2.9 s */
#define BEEP_TIME		4.0	/* beep after answer */

/* Call reference for calls from mobile station to network
   This offset of 0x400000000 is required for MNCC interface. */
static int new_callref = 0x40000000;

/* these calls are not associated with a transmitter */
euro_call_t *ooo_call_list = NULL;

struct id_list {
	struct id_list *next;
	char id[7];
};

static struct id_list *id_list = NULL, **id_list_tail = &id_list;

/* add ID that is allowed to page */
void euro_add_id(const char *id)
{
	struct id_list *entry;

	entry = calloc(1, sizeof(struct id_list));
	if (!entry) {
		fprintf(stderr, "No mem!\n");
		abort();
	}
	strcpy(entry->id, id);
	(*id_list_tail) = entry;
	id_list_tail = &entry->next;
}

static int search_id(const char *id)
{
	struct id_list *entry = id_list;
	int count = 0;

	while (entry) {
		count++;
		if (!strcmp(entry->id, id))
			return count;
		entry = entry->next;
	}

	return 0;
}

static void euro_call_destroy(euro_call_t *call);

static void flush_id(void)
{
	struct id_list *entry;

	while (ooo_call_list)
		euro_call_destroy(ooo_call_list);
	while (id_list) {
		entry = id_list;
		id_list = entry->next;
		free(entry);
	}
	id_list_tail = &id_list;
}

static const char *call_state_name(enum euro_call_state state)
{
	static char invalid[16];

	switch (state) {
	case EURO_CALL_NULL:
		return "(NULL)";
	case EURO_CALL_ANSWER:
		return "ANSWER";
	case EURO_CALL_DEGRADED:
		return "DEGRADED";
	case EURO_CALL_ACKNOWLEDGE:
		return "ACKNOWLEDGE";
	case EURO_CALL_RELEASED:
		return "RELEASED";
	case EURO_CALL_UNASSIGNED:
		return "UNASSIGNED";
	case EURO_CALL_OUTOFORDER:
		return "OUT-OF-ORDER";
	case EURO_CALL_BEEPING:
		return "BEEPING";
	}

	sprintf(invalid, "invalid(%d)", state);
	return invalid;
}

static void euro_display_status(void)
{
	sender_t *sender;
	euro_t *euro;
	euro_call_t *call;

	display_status_start();
	for (call = ooo_call_list; call; call = call->next)
		display_status_subscriber(call->station_id, call_state_name(call->state));
	for (sender = sender_head; sender; sender = sender->next) {
		euro = (euro_t *) sender;
		display_status_channel(euro->sender.kanal, NULL, (euro->degraded) ? "Degraded" : "Working");
		for (call = euro->call_list; call; call = call->next)
			display_status_subscriber(call->station_id, call_state_name(call->state));
	}
	display_status_end();
}


static void call_new_state(euro_call_t *call, enum euro_call_state new_state)
{
	if (call->state == new_state)
		return;
	PDEBUG(DEURO, DEBUG_DEBUG, "State change: %s -> %s\n", call_state_name(call->state), call_state_name(new_state));
	call->state = new_state;
	euro_display_status();
}

/* Convert channel number to frequency */
double euro_kanal2freq(const char *kanal, int fm)
{
	double freq = 87.34;
	char digit = kanal[0];

	if (strlen(kanal) != 1)
		return 0.0;

	if (digit >= 'a' && digit <= 'd')
		digit -= 'a';
	else if (digit >= 'A' && digit <= 'D')
		digit -= 'A';
	else
		return 0.0;

	if (fm)
		freq -= 0.0075;
		
	return (freq + 0.025 * (double)digit) * 1e6;
}

static struct channel_info {
	const char *kanal;
	const char *country;
	const char *station;
} channel_info[] = {
	{ "A",	"Germany", "Eurosignal Mitte" },
	{ NULL,	"France", "Centre Est" },
	{ "B",	"Germany", "Eurosignal Nord" },
	{ NULL,	"Germany", "Eurosignal Sued" },
	{ NULL,	"France", "North" },
	{ "C",	"France", "West" },
	{ NULL,	"France", "South East" },
	{ NULL,	"France", "East" },
	{ "D",	"France", "South West" },
	{ NULL,	"France", "South West (Corsica)" },
	{ NULL,	"Switzerland", "" },
	{ NULL, NULL, NULL },
};

/* list all channels */
void euro_list_channels(void)
{
	int i;

	printf("Channel\t\tFrequency\tCountry\t\tStation Name\n");
	printf("------------------------------------------------------------------------\n");
	for (i = 0; channel_info[i].country; i++) {
		if (channel_info[i].kanal)
			printf("%s\t\t%.3f MHz\t", channel_info[i].kanal, euro_kanal2freq(channel_info[i].kanal, 0) / 1e6);
		else
			printf("\t\t\t\t");
		printf("%s\t", channel_info[i].country);
		if (strlen(channel_info[i].country) < 8)
			printf("\t");
		printf("%s\n", channel_info[i].station);
	}
	printf("\n");
}

sample_t beep_tone[160];

/* global init */
int euro_init(void)
{
	int i;

	for (i = 0; i < 160; i++)
		beep_tone[i] = sin(2.0 * M_PI * (double)i * 2500.0 / 8000.0);

	return 0;
}

/* global exit */
void euro_exit(void)
{
	flush_id();
}

static void call_timeout(struct timer *timer);

/* Create transceiver instance and link to a list. */
int euro_create(const char *kanal, const char *audiodev, int use_sdr, int samplerate, double rx_gain, int fm, int tx, int rx, int repeat, int degraded, int random, uint32_t scan_from, uint32_t scan_to, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback)
{
	euro_t *euro;
	int rc;

	if (euro_kanal2freq(kanal, 0) == 0.0) {
		PDEBUG(DEURO, DEBUG_ERROR, "Channel ('Kanal') number %s invalid, use 'list' to get a list.\n", kanal);
		return -EINVAL;
	}

	if (atoi(kanal) == 19) {
		PDEBUG(DEURO, DEBUG_ERROR, "Selected calling channel ('Rufkanal') number %s can't be used as traffic channel.\n", kanal);
		return -EINVAL;
	}

	euro = calloc(1, sizeof(*euro));
	if (!euro) {
		PDEBUG(DEURO, DEBUG_ERROR, "No memory!\n");
		return -ENOMEM;
	}

	PDEBUG(DEURO, DEBUG_DEBUG, "Creating 'Eurosignal' instance for 'Kanal' = %s (sample rate %d).\n", kanal, samplerate);

	/* init general part of transceiver */
	rc = sender_create(&euro->sender, kanal, euro_kanal2freq(kanal, fm), euro_kanal2freq(kanal, fm), audiodev, use_sdr, samplerate, rx_gain, 0, 0, write_rx_wave, write_tx_wave, read_rx_wave, read_tx_wave, loopback, PAGING_SIGNAL_NONE);
	if (rc < 0) {
		PDEBUG(DEURO, DEBUG_ERROR, "Failed to init transceiver process!\n");
		goto error;
	}

	/* init audio processing */
	rc = dsp_init_sender(euro, samplerate, fm);
	if (rc < 0) {
		PDEBUG(DEURO, DEBUG_ERROR, "Failed to init audio processing!\n");
		goto error;
	}

	euro->tx = tx;
	euro->rx = rx;
	euro->repeat = repeat;
	euro->degraded = degraded;
	euro->random = random;
	euro->scan_from = scan_from;
	euro->scan_to = scan_to;

	euro_display_status();

	PDEBUG(DEURO, DEBUG_NOTICE, "Created 'Kanal' %s\n", kanal);

	return 0;

error:
	euro_destroy(&euro->sender);

	return rc;
}

/* Destroy transceiver instance and unlink from list. */
void euro_destroy(sender_t *sender)
{
	euro_t *euro = (euro_t *) sender;

	PDEBUG(DEURO, DEBUG_DEBUG, "Destroying 'Eurosignal' instance for 'Kanal' = %s.\n", sender->kanal);

	while (euro->call_list)
		euro_call_destroy(euro->call_list);
	dsp_cleanup_sender(euro);
	sender_destroy(&euro->sender);
	free(euro);
}

/* Create call instance */
static euro_call_t *euro_call_create(euro_t *euro, uint32_t callref, const char *id)
{
	euro_call_t *call, **callp;

	PDEBUG(DEURO, DEBUG_INFO, "Creating call instance to page ID '%s'.\n", id);

	/* create */
	call = calloc(1, sizeof(*call));
	if (!call) {
		PDEBUG(DEURO, DEBUG_ERROR, "No mem!\n");
		abort();
	}

	/* init */
	call->callref = callref;
	strcpy(call->station_id, id);
	if (euro)
		call->page_count = euro->repeat;
	timer_init(&call->timer, call_timeout, call);
	timer_start(&call->timer, ANSWER_TIME);

	/* link */
	call->euro = euro;
	callp = (euro) ? (&euro->call_list) : (&ooo_call_list);
	while ((*callp))
		callp = &(*callp)->next;
	(*callp) = call;

	return call;
}

/* Destroy call instance */
static void euro_call_destroy(euro_call_t *call)
{
	euro_call_t **callp;

	/* unlink */
	callp = (call->euro) ? (&call->euro->call_list) : (&ooo_call_list);
	while ((*callp) != call)
		callp = &(*callp)->next;
	(*callp) = call->next;

	/* cleanup */
	timer_exit(&call->timer);

	/* destroy */
	free(call);

	/* update display */
	euro_display_status();
}

/* Return ID to page. If there is nothing scheduled, return idle pattern */
void euro_get_id(euro_t *euro, char *id)
{
	euro_call_t *call;
	int i;

	if (euro->sender.loopback) {
		PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Transmitting test ID '123456'.\n");
		memcpy(id, "123456", 6);
		goto encode;
	}

	if (euro->scan_from < euro->scan_to) {
		sprintf(id, "%06d", euro->scan_from++);
		PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Transmitting ID '%s'.\n", id);
		goto encode;
	}

	for (call = euro->call_list; call; call = call->next) {
		if ((call->state == EURO_CALL_ACKNOWLEDGE || call->state == EURO_CALL_RELEASED) && call->page_count) {
			call->page_count--;
			PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Transmitting ID '%s'.\n", call->station_id);
			memcpy(id, call->station_id, 6);
			if (call->page_count == 0 && call->state == EURO_CALL_RELEASED)
				euro_call_destroy(call);
			/* reset random counter, so aferr call has been transmitted, the random mode continues with a new ID */
			euro->random_count = 0;
			break;
		}
	}

	if (!call && euro->random) {
		if (!euro->random_count) {
			if (((uint32_t)random() % 2) == 0 && euro->random_id[0] != 'R') {
				memcpy(euro->random_id, "RIIIII", 6);
				euro->random_count = (uint32_t)random() % 2 + 2;
			} else {
				sprintf(euro->random_id, "%06d", (uint32_t)(random() % 1000000));
				euro->random_count = (uint32_t)random() % 3 + 2;
			}
		}
		memcpy(id, euro->random_id, 6);
		euro->random_count--;
		if (id[0] == 'R') {
			PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Randomly transmitting Idle sequence.\n");
			return;
		}
		PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Randomly transmitting ID '%s'.\n", euro->random_id);
		goto encode;
	}

	if (!call) {
		PDEBUG_CHAN(DEURO, DEBUG_DEBUG, "Transmitting Idle sequence.\n");
		memcpy(id, "RIIIII", 6);
		return;
	}

encode:
	/* return station ID (upper case) with repeat digit, when required */
	for (i = 0; i < 6; i++) {
		/* to upper case */
		if (id[i] >= 'a' && id[i] <= 'z')
			id[i] = id[i] - 'a' + 'A';
		/* repeat digit */
		if (i && id[i - 1] == id[i])
			id[i] = 'R';
	}
}

/* A station ID was received. */
void euro_receive_id(euro_t *euro, char *id)
{
	int i;
	int count = 0;

	if (id[0] == 'R') {
		PDEBUG_CHAN(DEURO, DEBUG_DEBUG, "Received Idle sequence'\n");
		return;
	}

	/* turn repeat digit to normal digit */
	for (i = 1; i < 6; i++) {
		if (id[i] == 'R')
			id[i] = id[i - 1];
	}

	/* loopback display */
	if (euro->sender.loopback) {
		PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Received ID '%s'\n", id);
		return;
	}

	/* check for ID selection */
	if (id_list) {
		count = search_id(id);
		if (!count) {
			PDEBUG_CHAN(DEURO, DEBUG_INFO, "Received ID '%s' is not for us.\n", id);
			return;
		}
	}

	PDEBUG_CHAN(DEURO, DEBUG_NOTICE, "Received ID '%s'\n", id);

	/* we want to send beep via MNCC */
	if (id_list) {
		euro_call_t *call;
		char dialing[32];
		int callref;
		int rc;

		/* check if we already have a call that beeps */
		for (call = ooo_call_list; call; call = call->next) {
			if (!strcmp(call->station_id, id) && call->state == EURO_CALL_BEEPING)
				break;
		}
		/* if already beeping */
		if (call)
			return;

		/* create call and send setup */
		PDEBUG_CHAN(DEURO, DEBUG_INFO, "Sending setup towards network.'\n");
		callref = ++new_callref;
		call = euro_call_create(NULL, callref, id);
		call_new_state(call, EURO_CALL_BEEPING);
		sprintf(dialing, "%d", count);
		rc = call_up_setup(callref, call->station_id, dialing);
		if (rc < 0)
			euro_call_destroy(call);
	}
}

int16_t *es_mitte_spl;
int es_mitte_size;
int16_t *es_ges_spl;
int es_ges_size;
int16_t *es_teilges_spl;
int es_teilges_size;
int16_t *es_kaudn_spl;
int es_kaudn_size;

/* play announcement for one call */
static void call_play_announcement(euro_call_t *call)
{
	int i;
	int16_t chunk[160];
	sample_t spl[160];

	for (i = 0; i < 160; i++) {
		/* announcement or silence, if finished or not set */
		if (call->announcement_index < call->announcement_size)
			chunk[i] = call->announcement_spl[call->announcement_index++] >> 2;
		else
			chunk[i] = 0.0;
	}
	int16_to_samples(spl, chunk, 160);
	call_up_audio(call->callref, spl, 160);
}

/* play paging tone */
static void call_play_beep(euro_call_t *call)
{
	call_up_audio(call->callref, beep_tone, 160);
}

/* loop through all calls and play the announcement */
void call_down_clock(void)
{
	sender_t *sender;
	euro_t *euro;
	euro_call_t *call;

	/* clock all calls without a transceiver */
	for (call = ooo_call_list; call; call = call->next) {
		/* no callref */
		if (!call->callref)
			continue;
		/* beep or announcement */
		if (call->state == EURO_CALL_BEEPING)
			call_play_beep(call);
		else
			call_play_announcement(call);
	}

	/* clock all calls that have a transceiver */
	for (sender = sender_head; sender; sender = sender->next) {
		euro = (euro_t *) sender;
		for (call = euro->call_list; call; call = call->next) {
			/* no callref */
			if (!call->callref)
				continue;
			/* announcement */
			call_play_announcement(call);
		}
	}
}

/* Timeout handling */
static void call_timeout(struct timer *timer)
{
	euro_call_t *call = (euro_call_t *)timer->priv;

	switch (call->state) {
	case EURO_CALL_ANSWER:
		/* if no station is linked to the call, we are out-of-order */
		if (!call->euro) {
			PDEBUG(DEURO, DEBUG_INFO, "Station is unavailable, playing announcement.\n");
			call->announcement_spl = es_ges_spl;
			call->announcement_size = es_ges_size;
			call->announcement_index = 0;
			timer_start(&call->timer, OOO_TIME);
			call_new_state(call, EURO_CALL_OUTOFORDER);
			break;
		}
		/* if subcriber list is available, but ID is not found, we are unassigned */
		if (id_list && !search_id(call->station_id)) {
			PDEBUG(DEURO, DEBUG_INFO, "Subscriber unknwon, playing announcement.\n");
			call->announcement_spl = es_kaudn_spl;
			call->announcement_size = es_kaudn_size;
			call->announcement_index = 0;
			call->announcement_count = 1;
			timer_start(&call->timer, UNASSIGNED_TIME1);
			call_new_state(call, EURO_CALL_UNASSIGNED);
			break;
		}
		/* if station is degraded, play that announcement */
		if (call->euro->degraded) {
			PDEBUG(DEURO, DEBUG_INFO, "Station is degraded, playing announcement.\n");
			call->announcement_spl = es_teilges_spl;
			call->announcement_size = es_teilges_size;
			call->announcement_index = 0;
			timer_start(&call->timer, DEGRADED_TIME);
			call_new_state(call, EURO_CALL_DEGRADED);
			break;
		}
	/* fall through */
	case EURO_CALL_DEGRADED:
		PDEBUG(DEURO, DEBUG_INFO, "Station acknowledges, playing announcement.\n");
		call->announcement_spl = es_mitte_spl;
		call->announcement_size = es_mitte_size;
		call->announcement_index = 0;
		call->announcement_count = 1;
		timer_start(&call->timer, ACKNOWLEDGE_TIME1);
		call_new_state(call, EURO_CALL_ACKNOWLEDGE);
		break;
	case EURO_CALL_ACKNOWLEDGE:
		if (call->announcement_count == 1) {
			call->announcement_spl = es_mitte_spl;
			call->announcement_size = es_mitte_size;
			call->announcement_index = 0;
			call->announcement_count = 2;
			timer_start(&call->timer, ACKNOWLEDGE_TIME2);
			break;
		}
		if (call->page_count) {
			PDEBUG(DEURO, DEBUG_INFO, "Announcement played, receiver has not been paged yet, releasing call.\n");
			call_up_release(call->callref, CAUSE_NORMAL);
			call->callref = 0;
			call_new_state(call, EURO_CALL_RELEASED);
			break;
		}
		PDEBUG(DEURO, DEBUG_INFO, "Announcement played, receiver has been paged, releasing call.\n");
		call_up_release(call->callref, CAUSE_NORMAL);
		euro_call_destroy(call);
		break;
	case EURO_CALL_OUTOFORDER:
		PDEBUG(DEURO, DEBUG_INFO, "Announcement played, releasing call.\n");
		call_up_release(call->callref, CAUSE_NORMAL);
		euro_call_destroy(call);
		break;
	case EURO_CALL_UNASSIGNED:
		if (call->announcement_count == 1) {
			call->announcement_spl = es_kaudn_spl;
			call->announcement_size = es_kaudn_size;
			call->announcement_index = 0;
			call->announcement_count = 2;
			timer_start(&call->timer, UNASSIGNED_TIME2);
			break;
		}
		PDEBUG(DEURO, DEBUG_INFO, "Announcement played, playing again.\n");
		call->announcement_spl = es_kaudn_spl;
		call->announcement_size = es_kaudn_size;
		call->announcement_index = 0;
		call->announcement_count = 1;
		timer_start(&call->timer, UNASSIGNED_TIME1);
		break;
	case EURO_CALL_BEEPING:
		PDEBUG(DEURO, DEBUG_INFO, "Beep played, releasing.\n");
		call_up_release(call->callref, CAUSE_NORMAL);
		call->callref = 0;
		euro_call_destroy(call);
		break;
	default:
		break;
	}
}

/* Call control starts call towards paging network. */
int call_down_setup(int callref, const char __attribute__((unused)) *caller_id, enum number_type __attribute__((unused)) caller_type, const char *dialing)
{
	char channel = '\0';
	sender_t *sender;
	euro_t *euro;
	euro_call_t *call;
	int i;

	/* check prefix to choose correct channel */
	if (strlen(dialing) == 10) {
		if (!strncmp(dialing, "0279", 4)) {
			dialing += 4;
			channel = 'A';
		}
		if (!strncmp(dialing, "0509", 4)) {
			dialing += 4;
			channel = 'B';
		}
		if (!strncmp(dialing, "0709", 4)) {
			dialing += 4;
			channel = 'B';
		}
	}
	/* number invalid */
	if (strlen(dialing) != 6) {
inval:
		PDEBUG(DEURO, DEBUG_NOTICE, "Call to invalid ID '%s', rejecting!\n", dialing);
		return -CAUSE_INVALNUMBER;
	}
	for (i = 0; i < 6; i++) {
		if (!(dialing[i] >= '0' && dialing[i] <= '9')
		 && !(dialing[i] >= 'a' && dialing[i] <= 'e')
		 && !(dialing[i] >= 'A' && dialing[i] <= 'E'))
			goto inval;
	}

	/* find transmitter */
	for (sender = sender_head; sender; sender = sender->next) {
		/* skip channels that are different than requested */
		if (channel && sender->kanal[0] != channel)
			continue;
		euro = (euro_t *) sender;
		/* check if base station cannot transmit */
		if (!euro->tx)
			continue;
		break;
	}
	/* just (ab)use busy signal when no station is available */
	if (!sender) {
		if (channel)
			PDEBUG(DEURO, DEBUG_NOTICE, "Cannot page receiver, because given station not available, rejecting!\n");
		else
			PDEBUG(DEURO, DEBUG_NOTICE, "Cannot page receiver, no station not available, rejecting!\n");
		euro = NULL;
	}

	/* create call process to page station or send out-of-order message */
	call = euro_call_create(euro, callref, dialing);
	call_new_state(call, EURO_CALL_ANSWER);
	call_up_answer(callref, dialing);

	return 0;
}

void call_down_answer(int __attribute__((unused)) callref)
{
	euro_call_t *call;

	PDEBUG(DEURO, DEBUG_INFO, "Call has been answered by network.\n");

	for (call = ooo_call_list; call; call = call->next) {
		if (call->callref == callref)
			break;
	}
	if (!call) {
		PDEBUG(DEURO, DEBUG_NOTICE, "Answer from network, but no callref!\n");
		call_up_release(callref, CAUSE_INVALCALLREF);
		return;
	}

	timer_start(&call->timer, BEEP_TIME);
}

static void _release(int callref, int __attribute__((unused)) cause)
{
	sender_t *sender;
	euro_t *euro;
	euro_call_t *call;

	PDEBUG(DEURO, DEBUG_INFO, "Call has been disconnected by network.\n");

	for (sender = sender_head; sender; sender = sender->next) {
		euro = (euro_t *) sender;
		for (call = euro->call_list; call; call = call->next) {
			if (call->callref == callref)
				break;
		}
		if (call)
			break;
	}
	if (!sender) {
		for (call = ooo_call_list; call; call = call->next) {
			if (call->callref == callref)
				break;
		}
	}
	if (!call) {
		PDEBUG(DEURO, DEBUG_NOTICE, "Outgoing disconnect, but no callref!\n");
		call_up_release(callref, CAUSE_INVALCALLREF);
		return;
	}

	call->callref = 0;

	/* queued ID will keep in release state until trasmission has finished */
	if (call->state == EURO_CALL_ACKNOWLEDGE && call->page_count) {
		call_new_state(call, EURO_CALL_RELEASED);
		return;
	}

	euro_call_destroy(call);
}

/* Call control sends disconnect.
 * A queued ID will be kept until transmitted by mobile station.
 */
void call_down_disconnect(int callref, int cause)
{
	_release(callref, cause);

	call_up_release(callref, cause);
}

/* Call control releases call toward mobile station. */
void call_down_release(int callref, int cause)
{
	_release(callref, cause);
}

/* Receive audio from call instance. */
void call_down_audio(int __attribute__((unused)) callref, sample_t __attribute__((unused)) *samples, int __attribute__((unused)) count)
{
}

void dump_info(void) {}