aboutsummaryrefslogtreecommitdiffstats
path: root/src/imts/imts.c
blob: 205680627516ae490b364abee91659e515dc45cd (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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
/* MTS/IMTS 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/>.
 *
 * 
 * How it works:
 *
 * (There should be a more detailed description of the call process!)
 *
 * There are call states defined by imts->state.
 * imts_receive_tone() is called whenever a tone/silence/noise is detected.
 * imts_lost_tone() is calles as soon as (only) a tone is gone.
 * imts_timeout() is called when the timer has timed out.
 * All these callbacks are used to process the call setup and disconnect.
 * The imts_timeout() function will not only handle failures due to timeouts,
 * but also E.g. end of pulsed digit or seize detection.
 *
 */

#define CHAN imts->sender.kanal

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

#define CUT_OFF_EMPHASIS_IMTS	796.0 /* FIXME: really 200 uS time constant? */

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

/* band info */
#define VHF_LOW 0
#define VHF_HIGH 1
#define UHF 2
static const char *band_name[] = {
	"VHF-Low Band",
	"VHF-High Band",
	"UHF Band",
};

/* I measured VHF_HIGH deviation with 5000, others told me that all bands are 5000. */
#define DEV_VHF_LOW	5000.0
#define DEV_VHF_HIGH	5000.0
#define DEV_UHF		5000.0

static struct channel_info {
	int		band;		/* which band it belongs to */
	char		*name;		/* name of channel */
	double		downlink_mhz;	/* base station frequency */
	double		uplink_mhz;	/* mobile station frequency */
	int		canada_only;	/* channel used in canada only */
} channel_info[] = {
	{ VHF_LOW,	"ZO",	35.26,		43.26,		0 },
	{ VHF_LOW,	"ZF",	35.30,		43.30,		0 },
	{ VHF_LOW,	"ZM",	35.38,		43.38,		0 },
	{ VHF_LOW,	"ZH",	35.34,		43.34,		0 },
	{ VHF_LOW,	"ZA",	35.42,		43.32,		0 },
	{ VHF_LOW,	"ZY",	35.46,		43.46,		0 },
	{ VHF_LOW,	"ZR",	35.50,		43.50,		0 },
	{ VHF_LOW,	"ZB",	35.54,		43.54,		0 },
	{ VHF_LOW,	"ZW",	35.62,		43.62,		0 },
	{ VHF_LOW,	"ZL",	35.66,		43.66,		0 },
	{ VHF_HIGH,	"JJ",	152.48,		157.74,		1 },
	{ VHF_HIGH,	"JL",	152.51,		157.77,		0 },
	{ VHF_HIGH,	"YL",	152.54,		157.80,		0 },
	{ VHF_HIGH,	"JP",	152.57,		157.83,		0 },
	{ VHF_HIGH,	"YP",	152.60,		157.86,		0 },
	{ VHF_HIGH,	"YJ",	152.63,		157.89,		0 },
	{ VHF_HIGH,	"YK",	152.66,		157.92,		0 },
	{ VHF_HIGH,	"JS",	152.69,		157.95,		0 },
	{ VHF_HIGH,	"YS",	152.72,		157.98,		0 },
	{ VHF_HIGH,	"YR",	152.75,		158.01,		0 },
	{ VHF_HIGH,	"JK",	152.78,		158.04,		0 },
	{ VHF_HIGH,	"JR",	152.81,		158.07,		0 },
	{ VHF_HIGH,	"JW",	152.84,		158.10,		1 },
	{ UHF,		"QC",	454.375,	459.375,	0 },
	{ UHF,		"QJ",	454.40,		459.40,		0 },
	{ UHF,		"QD",	454.425,	459.425,	0 },
	{ UHF,		"QA",	454.45,		459.45,		0 },
	{ UHF,		"QE",	454.475,	459.475,	0 },
	{ UHF,		"QP",	454.50,		459.50,		0 },
	{ UHF,		"QK",	454.525,	459.525,	0 },
	{ UHF,		"QB",	454.55,		459.55,		0 },
	{ UHF,		"QO",	454.575,	459.575,	0 },
	{ UHF,		"QR",	454.60,		459.60,		0 },
	{ UHF,		"QY",	454.625,	459.625,	0 },
	{ UHF,		"QF",	454.65,		459.65,		0 },
	{ 0, NULL, 0.0, 0.0, 0}
};

void imts_list_channels(void)
{
	int last_band = -1;
	int i;

	for (i = 0; channel_info[i].name; i++) {
		if (last_band != channel_info[i].band) {
			last_band = channel_info[i].band;
			printf("\n%s:\n\n", band_name[channel_info[i].band]);
			printf("Channel\t\tDownlink\tUplink\t\tCountry\n");
			printf("----------------------------------------------------------------\n");
		}
		printf("%s\t\t%.3f MHz\t%.3f MHz\t%s\n", channel_info[i].name, channel_info[i].downlink_mhz, channel_info[i].uplink_mhz, (channel_info[i].canada_only) ? "USA + Canada" : "USA");
	}
	printf("\n");
}

/* Timers */
#define PAGING_TO	4.0	/* Time to wait for the phone to respond */
#define RINGING_TO	45.0	/* Time to wait for the mobile user to answer */
#define SEIZE_TO	1.0	/* Time to wait for the phone to seize (Connect tone) */
#define ANI_TO		1.000	/* Time to wait for first / next digit */
#define DIALTONE_TO	10.0	/* Time to wait until dialing must be performed */
#define DIALING_TO	3.0	/* Time to wait until number is recognized as complete */
#define RELEASE_TO	0.350	/* Time to turn off transmitter before going idle ".. for about 300 ms .." */
#define ANI_PULSE_TO	0.100	/* Time to detect end of digit */
#define DIAL_PULSE_TO	0.200	/* Time to detect end of digit */
#define DISC_PULSE_TO	0.100	/* Time until aborting disconnect detection */
#define PAGE_PULSE_TO	0.200	/* Time to detect end of digit */

/* Counters */
#define DISC_COUNT	6	/* Number of pulses to detect disconnect FIXME */
#define RING_PULSES	40	/* 2 seconds ringer on */

/* Durations */
#define IDLE_DETECT	0.500	/* Time to detect Idle signal (loopback) */
#define PAGE_SEIZE	0.400	/* Time to seize channel until start paging pulses FIXME */
#define PAGE_PAUSE	0.400	/* Time to pause after each digit FIXME */
#define PAGE_MARK	0.050	/* Mark duration of page pulse */
#define PAGE_SPACE	0.050	/* Space duration of page pulse */
#define PAGE_PULSE	0.100	/* Duration of a complete pulse (MTS) */
#define RING_MARK	0.025	/* Mark duration of ring pulse */
#define RING_SPACE	0.025	/* Space duration of ring pulse */
#define RING_OFF	4.0	/* 4 seconds ringer off */
#define GUARD_TIME	0.200	/* Time until detecting Guard tone from mobile */
#define SEIZE_TIME	0.300	/* Time until sending Seize tone >= 250 */
#define SEIZE_LENGTH	0.250	/* Length of Seize */
#define RECEIVE_TIME	0.200	/* Time until detecting receive signal (Guard tone) from mobile */
#define ANSWER_TIME	0.200	/* Time until detecting answer signal (Connect tone) from mobile */

const char *imts_state_name(enum imts_state state)
{
	static char invalid[16];

	switch (state) {
	case IMTS_NULL:
		return "(NULL)";
	case IMTS_OFF:
		return "IDLE (off)";
	case IMTS_IDLE:
		return "IDLE (tone)";
	case IMTS_SEIZE:
		return "SEIZE (mobile call)";
	case IMTS_ANI:
		return "ANI (mobile call)";
	case IMTS_DIALING:
		return "DIALING (mobile call)";
	case IMTS_PAGING:
		return "PAGING (station call)";
	case IMTS_RINGING:
		return "RINGING (station call)";
	case IMTS_CONVERSATION:
		return "CONVERSATION";
	case IMTS_RELEASE:
		return "RELEASE";
	case IMTS_PAGING_TEST:
		return "PAGING TEST";
	case IMTS_DETECTOR_TEST:
		return "DETECTOR TEST";
	}

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

static void imts_display_status(void)
{
	sender_t *sender;
	imts_t *imts;

	display_status_start();
	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		display_status_channel(imts->sender.kanal, NULL, imts_state_name(imts->state));
		if (imts->station_id[0])
			display_status_subscriber(imts->station_id, NULL);
	}
	display_status_end();
}

static void imts_new_state(imts_t *imts, enum imts_state new_state)
{
	if (imts->state == new_state)
		return;
	PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "State change: %s -> %s\n", imts_state_name(imts->state), imts_state_name(new_state));
	imts->state = new_state;
	imts_display_status();
}

/* Convert channel name to frequency number of base station.
   Set 'uplink' to 1 to get frequency of mobile station. */
double imts_channel2freq(const char *kanal, int uplink)
{
	int i;

	for (i = 0; channel_info[i].name; i++) {
		if (!strcasecmp(channel_info[i].name, kanal)) {
			if (uplink == 2)
				return (channel_info[i].downlink_mhz - channel_info[i].uplink_mhz) * 1e6;
			else if (uplink)
				return channel_info[i].uplink_mhz * 1e6;
			else
				return channel_info[i].downlink_mhz * 1e6;
		}
	}

	return 0.0;
}

int imts_channel2band(const char *kanal)
{
	int i;

	for (i = 0; channel_info[i].name; i++) {
		if (!strcasecmp(channel_info[i].name, kanal))
			return channel_info[i].band;
	}

	return 0.0;
}

double imts_is_canada_only(const char *kanal)
{
	int i;

	for (i = 0; channel_info[i].name; i++) {
		if (!strcasecmp(channel_info[i].name, kanal))
			return channel_info[i].canada_only;
	}

	return 0;
}

/* global init */
int imts_init(void)
{
	return 0;
}

static void imts_timeout(struct timer *timer);
static void imts_go_idle(imts_t *imts);
static void imts_paging(imts_t *imts, const char *dial_string, int loopback);
static void imts_detector_test(imts_t *imts, double length_1, double length_2, double length_3);

/* Create transceiver instance and link to a list. */
int imts_create(const char *kanal, const char *audiodev, int use_sdr, int samplerate, double rx_gain, int pre_emphasis, int de_emphasis, const char *write_rx_wave, const char *write_tx_wave, const char *read_rx_wave, const char *read_tx_wave, int loopback, double squelch_db, int ptt, int station_length, double fast_seize, enum mode mode, const char *operator, double length_1, double length_2, double length_3)
{
	imts_t *imts;
	int rc;

	if (imts_channel2freq(kanal, 0) == 0.0) {
		PDEBUG(DIMTS, DEBUG_ERROR, "Channel number %s invalid.\n", kanal);
		return -EINVAL;
	}
	if (imts_is_canada_only(kanal)) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
		PDEBUG(DIMTS, DEBUG_NOTICE, "Given channel '%s' was only available in Canada with Canadian phones.\n", kanal);
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
	}
	if (mode == MODE_IMTS && imts_channel2band(kanal) == VHF_LOW) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
		PDEBUG(DIMTS, DEBUG_NOTICE, "Given channel '%s' was only available at MTS network.\n", kanal);
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
		return -EINVAL;
	}
	if (mode == MODE_MTS && imts_channel2band(kanal) == UHF) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
		PDEBUG(DIMTS, DEBUG_NOTICE, "Given channel '%s' was only available at IMTS network.\n", kanal);
		PDEBUG(DIMTS, DEBUG_NOTICE, "*******************************************************************************\n");
		return -EINVAL;
	}

	imts = calloc(1, sizeof(imts_t));
	if (!imts) {
		PDEBUG(DIMTS, DEBUG_ERROR, "No memory!\n");
		return -EIO;
	}

	PDEBUG(DIMTS, DEBUG_DEBUG, "Creating 'IMTS' instance for channel = %s (sample rate %d).\n", kanal, samplerate);

	imts->station_length = station_length;
	imts->fast_seize = fast_seize;
	imts->mode = mode;
	imts->operator = operator;
	imts->ptt = ptt;

	/* init general part of transceiver */
	/* do not enable emphasis, since it is done by imts code, not by common sender code */
	rc = sender_create(&imts->sender, kanal, imts_channel2freq(kanal, 0), imts_channel2freq(kanal, 1), 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(DIMTS, DEBUG_ERROR, "Failed to init 'Sender' processing!\n");
		goto error;
	}

	/* init audio processing */
	rc = dsp_init_transceiver(imts, squelch_db, ptt);
	if (rc < 0) {
		PDEBUG(DIMTS, DEBUG_ERROR, "Failed to init signal processing!\n");
		goto error;
	}

	timer_init(&imts->timer, imts_timeout, imts);

	imts->pre_emphasis = pre_emphasis;
	imts->de_emphasis = de_emphasis;
	rc = init_emphasis(&imts->estate, samplerate, CUT_OFF_EMPHASIS_IMTS, CUT_OFF_HIGHPASS_DEFAULT, CUT_OFF_LOWPASS_DEFAULT);
	if (rc < 0)
		goto error;

	if (length_1 > 0.0 || length_2 > 0.0 || length_3 > 0.0) {
		imts_detector_test(imts, length_1, length_2, length_3);
		/* go into detector test state */
	} else if (loopback) {
		/* go into loopback test state */
		imts_paging(imts, "1234567890", loopback);
	} else {
		/* go into idle state */
		imts_go_idle(imts);
	}

	PDEBUG(DIMTS, DEBUG_NOTICE, "Created channel #%s\n", kanal);

	return 0;

error:
	imts_destroy(&imts->sender);

	return rc;
}

/* Destroy transceiver instance and unlink from list. */
void imts_destroy(sender_t *sender)
{
	imts_t *imts = (imts_t *) sender;

	PDEBUG(DIMTS, DEBUG_DEBUG, "Destroying 'IMTS' instance for channel = %s.\n", sender->kanal);

	timer_exit(&imts->timer);
	dsp_cleanup_transceiver(imts);
	sender_destroy(&imts->sender);
	free(sender);
}

/* Return to IDLE */
static void imts_go_idle(imts_t *imts)
{
	sender_t *sender;
	imts_t *idle;

	timer_stop(&imts->timer);
	imts->station_id[0] = '\0'; /* remove station ID before state change, so status is shown correctly */

	for (sender = sender_head; sender; sender = sender->next) {
		idle = (imts_t *) sender;
		if (idle == imts)
			continue;
		if (idle->state == IMTS_IDLE)
			break;
	}
	if (sender) {
		PDEBUG(DIMTS, DEBUG_INFO, "Entering IDLE state on channel %s, turning transmitter off.\n", imts->sender.kanal);
		imts_new_state(imts, IMTS_OFF);
		imts_set_dsp_mode(imts, DSP_MODE_OFF, 0, 0.0, 0);
	} else {
		if (imts->mode == MODE_IMTS) {
			PDEBUG(DIMTS, DEBUG_INFO, "Entering IDLE state on channel %s, sending 2000 Hz tone.\n", imts->sender.kanal);
			imts_new_state(imts, IMTS_IDLE);
			/* also reset detector, so if there is a new call it is answered */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, 0.0, 1);
		} else {
			PDEBUG(DIMTS, DEBUG_INFO, "Entering IDLE state on channel %s, sending 600 Hz tone.\n", imts->sender.kanal);
			imts_new_state(imts, IMTS_IDLE);
			/* also reset detector, so if there is a new call it is answered */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_600, 0.0, 1);
		}
	}
}

/* If a channel is occupied, we need to hunt for an IDLE channel to be turned on. */
static void imts_activate_idle(void)
{
	sender_t *sender;
	imts_t *idle;

	for (sender = sender_head; sender; sender = sender->next) {
		idle = (imts_t *) sender;
		if (idle->state == IMTS_OFF)
			break;
	}
	if (sender)
		imts_go_idle(idle);
	else
		PDEBUG(DIMTS, DEBUG_INFO, "All channels are busy now, cannot activate any other channel.\n");
}

/* Release connection towards mobile station by sending pause for a while. */
static void imts_release(imts_t *imts)
{
	timer_stop(&imts->timer);
	/* remove station ID before state change, so status is shown correctly */
	imts->station_id[0] = '\0';

	if (imts->mode == MODE_MTS && imts->state == IMTS_RINGING) {
		/*
		 * In MTS mode we abort ringing by sending pulse.
		 * Afterwards we call imts_release again and turn transmitter off.
		 */
		int tone;
		if (imts->tone == TONE_1500)
			tone = TONE_600;
		else
			tone = TONE_1500;
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Sending pulse to stop ringing of the phone.\n");
		imts_new_state(imts, IMTS_RELEASE);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, tone, PAGE_PAUSE, 0);
	} else {
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Turing transmitter off.\n");
		if (imts->state != IMTS_RELEASE)
			imts_new_state(imts, IMTS_RELEASE);
		imts_set_dsp_mode(imts, DSP_MODE_OFF, 0, 0.0, 0);
		timer_start(&imts->timer, RELEASE_TO);
	}
}

/* Enter detector test state */
static void imts_detector_test(imts_t *imts, double length_1, double length_2, double length_3)
{
	PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Entering detector test state, sending test sequence.\n");
	imts->detector_test_length_1 = length_1;
	imts->detector_test_length_2 = length_2;
	imts->detector_test_length_3 = length_3;
	imts_new_state(imts, IMTS_DETECTOR_TEST);
	imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, 1.0, 0);
	
}

/* Enter paging state */
static void imts_paging(imts_t *imts, const char *dial_string, int loopback)
{
	/* stop timer, since it may be running while measuring Guard tone at IDLE state */
	timer_stop(&imts->timer);

	if (loopback)
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Entering paging test state, sending digits %s.\n", dial_string);
	else
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Entering paging state, sending phone's ID '%s'.\n", dial_string);
	/* set station ID before state change, so status is shown correctly */
	strncpy(imts->station_id, dial_string, sizeof(imts->station_id) - 1);
	imts->tx_page_index = 0;
	imts->tx_page_pulse = 0;
	imts_new_state(imts, (loopback) ? IMTS_PAGING_TEST : IMTS_PAGING);
	imts_activate_idle(); /* must activate another channel right after station is not idle anymore */
	if (imts->mode == MODE_IMTS) {
		/* seize channel before dialing */
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, PAGE_SEIZE, 0);
	} else {
		/* send single pulse + pause, which causes the call selector to reset */
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_1500, PAGE_PAUSE, 0);
		/* for loopback test, we need time stamp */
		imts->tx_page_timestamp = get_time();
	}
}

/* Enter ringing state */
static void imts_ringing(imts_t *imts)
{
	PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received response from mobile phone, ringing.\n");
	imts->tx_ring_pulse = 0;
	imts_new_state(imts, IMTS_RINGING);
	imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, RING_MARK, 0);
	timer_start(&imts->timer, RINGING_TO);
}

/* Enter conversation state */
static void imts_answer(imts_t *imts)
{
	PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received answer from mobile phone, conversation started.\n");
	timer_stop(&imts->timer);
	imts_new_state(imts, IMTS_CONVERSATION);
	imts_set_dsp_mode(imts, DSP_MODE_AUDIO, 0, 0.0, 0);
	imts->rx_disc_pulse = 0;
}

/* Loss of signal was detected, release active call. */
void imts_loss_indication(imts_t *imts, double loss_time)
{
	/* stop timer */
	if (imts->mode == MODE_MTS && (imts->state == IMTS_IDLE || imts->state == IMTS_RINGING))
		timer_stop(&imts->timer);

	if (!imts->ptt && imts->state == IMTS_CONVERSATION) {
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Detected loss of signal after %.1f seconds, releasing.\n", loss_time);
		imts_release(imts);
		call_up_release(imts->callref, CAUSE_TEMPFAIL);
		imts->callref = 0;
	}
}

/* if signal is detected, phone picked up */
void imts_signal_indication(imts_t *imts)
{
	/* setup a call from mobile to base station */
	if (imts->mode == MODE_MTS && imts->state == IMTS_IDLE) {
		int callref = ++new_callref;
		int rc;

		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Detectes RF signal in IDLE mode, calling the opterator at '%s'.\n", imts->operator);
		rc = call_up_setup(callref, NULL, imts->operator);
		if (rc < 0) {
			PDEBUG_CHAN(DANETZ, DEBUG_NOTICE, "Call rejected (cause %d), releasing!\n", -rc);
			imts_release(imts);
			return;
		}
		imts->callref = callref;
		imts_new_state(imts, IMTS_CONVERSATION);
		imts_set_dsp_mode(imts, DSP_MODE_AUDIO, 0, 0.0, 0);
	}

	/* answer a call from base station to mobile */
	if (imts->mode == MODE_MTS && imts->state == IMTS_RINGING) {
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Detected RF signal, mobile is now transmitting.\n");
		call_up_answer(imts->callref, imts->station_id);
		imts_answer(imts);
	}
}

/* decode seize from mobile */
static void imts_receive_seize(imts_t *imts, int tone)
{
	/* other tone stops IDLE / GUARD timer */
	timer_stop(&imts->timer);

	switch (tone) {
	case TONE_IDLE:
	case TONE_600:
		timer_start(&imts->timer, IDLE_DETECT);
		break;
	case TONE_GUARD:
		imts->rx_guard_timestamp = get_time();
		if (imts->fast_seize)
			timer_start(&imts->timer, imts->fast_seize);
		break;
	case TONE_CONNECT:
		if (imts->last_tone == TONE_GUARD && imts->rx_guard_duration >= GUARD_TIME) {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received seize (Guard + Connect tone) from mobile phone.\n");
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, " -> Guard tone duration: %.0f ms (level %.0f%%)\n", (get_time() - imts->rx_guard_timestamp) * 1000.0, imts->last_sigtone_amplitude * 100.0);
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, 0.0, 0);
			imts_new_state(imts, IMTS_SEIZE);
			imts_activate_idle(); /* must activate another channel right after station is not idle anymore */
			timer_start(&imts->timer, SEIZE_TIME);
		}
		break;
	default:
		;
	}
}

/* decode ANI digits */
static void imts_receive_ani(imts_t *imts, int tone)
{
	/* wait for connect tone the first time */
	if (!imts->rx_ani_totpulses && tone != TONE_CONNECT)
		return;

	switch (tone) {
	case TONE_CONNECT:
		/* pulse detected */
		imts->rx_ani_pulse++;
		imts->rx_ani_totpulses++;
		if (imts->rx_ani_pulse > 10) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received too many pulses, releasing!\n");
			imts_release(imts);
			break;
		}
		PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Detected ANI pulse #%d.\n", imts->rx_ani_pulse);
		timer_start(&imts->timer, ANI_PULSE_TO);
		break;
	case TONE_GUARD:
		/* even pulse completed */
		if ((imts->rx_ani_totpulses & 1)) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Parity error: Received Guard tone after %d (odd) pulses, releasing!\n", imts->rx_ani_totpulses);
			imts_release(imts);
			break;
		}
		timer_start(&imts->timer, ANI_PULSE_TO);
		break;
	case TONE_SILENCE:
		/* odd pulse completed */
		if (!(imts->rx_ani_totpulses & 1)) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Parity error: Received silence after %d (even) pulses, releasing!\n", imts->rx_ani_totpulses);
			imts_release(imts);
			break;
		}
		timer_start(&imts->timer, ANI_PULSE_TO);
		break;
	default:
		/* received noise */
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received noise while dialing, releasing!\n");
		imts_release(imts);
	}
}

/* decode dialing digits */
static void imts_receive_dialing(imts_t *imts, int tone)
{
	switch (tone) {
	case TONE_CONNECT:
		/* turn off dialtone */
		if (!imts->dial_number[0] && !imts->rx_dial_pulse)
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, 0.0, 0);
		/* pulse detected */
		imts->rx_dial_pulse++;
		if (imts->rx_dial_pulse > 10) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received too many pulses, releasing!\n");
			imts_release(imts);
			break;
		}
		PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Detected dialing pulse #%d.\n", imts->rx_dial_pulse);
		timer_start(&imts->timer, DIAL_PULSE_TO);
		break;
	case TONE_GUARD:
		/* pulse completed */
		if (imts->rx_dial_pulse)
			timer_start(&imts->timer, DIAL_PULSE_TO);
		break;
	default:
		;
#if 0
		/* received noise */
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received noise while dialing, releasing!\n");
		imts_release(imts);
#endif
	}
}

/* check for disconnect */
static void imts_receive_disconnect(imts_t *imts, int tone, double elapsed, double amplitude)
{
	/* reset disc counter on timeout */
	if (elapsed > DISC_PULSE_TO) {
		if (imts->rx_disc_pulse) {
			PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Timeout Disconnect sequence\n");
			imts->rx_disc_pulse = 0;
		}
		return;
	}

	switch (tone) {
	case TONE_DISCONNECT:
		imts->rx_disc_pulse++;
		PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Detected Disconnect pulse #%d.\n", imts->rx_disc_pulse);
		break;
	case TONE_GUARD:
		if (imts->rx_disc_pulse == DISC_COUNT) {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received disconnect sequence from mobile phone (level %.0f%%).\n", amplitude * 100.0);
			if (imts->state == IMTS_SEIZE
			 || imts->state == IMTS_ANI
			 || imts->state == IMTS_DIALING
			 || imts->state == IMTS_PAGING
			 || imts->state == IMTS_RINGING
			 || imts->state == IMTS_CONVERSATION) {
				if (imts->callref)
					call_up_release(imts->callref, CAUSE_NORMAL);
				imts_release(imts);
			}
			break;
		}
		break;
	default:
		if (imts->rx_disc_pulse) {
			PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Disconnect sequence not detected anymore\n");
			imts->rx_disc_pulse = 0;
		}
	}
}

/* decode page test digits */
static void receive_page_imts(imts_t *imts, int tone)
{
	switch (tone) {
	case TONE_IDLE:
		/* pulse detected */
		imts->rx_page_pulse++;
		PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Detected page test pulse #%d.\n", imts->rx_page_pulse);
		if (imts->rx_page_pulse > 10) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received too many pulses!\n");
		}
		timer_start(&imts->timer, PAGE_PULSE_TO); // use for page test timeout
		break;
	case TONE_SEIZE:
		/* pulse completed */
		timer_start(&imts->timer, PAGE_PULSE_TO); // use for page test timeout
		break;
	default:
		;
	}
}

/* decode page test digits */
static void receive_page_mts(imts_t *imts, int tone)
{
	if (tone == TONE_600 || tone == TONE_1500) {
		/* pulse detected */
		imts->rx_page_pulse++;
		PDEBUG_CHAN(DIMTS, DEBUG_DEBUG, "Detected page test pulse #%d.\n", imts->rx_page_pulse);
		if (imts->rx_page_pulse > 10) {
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received too many pulses!\n");
		}
		timer_start(&imts->timer, PAGE_PULSE_TO); // use for page test timeout
	}
}

/* A tone was detected or is gone. */
void imts_receive_tone(imts_t *imts, int tone, double elapsed, double amplitude)
{
	/* used for several states */
	imts_receive_disconnect(imts, tone, elapsed, amplitude);

	switch (imts->state) {
	case IMTS_IDLE:
		imts_receive_seize(imts, tone);
		break;
	case IMTS_ANI:
		imts_receive_ani(imts, tone);
		break;
	case IMTS_DIALING:
		imts_receive_dialing(imts, tone);
		break;
	case IMTS_CONVERSATION:
		break;
	case IMTS_PAGING_TEST:
		if (imts->mode == MODE_IMTS)
			receive_page_imts(imts, tone);
		else
			receive_page_mts(imts, tone);
		break;
	default:
		;
	}

	/* remember last tone, also store amplitude of last signaling tone */
	imts->last_tone = tone;
	if (imts->last_tone < NUM_SIG_TONES)
		imts->last_sigtone_amplitude = amplitude;
}

void imts_lost_tone(imts_t *imts, int tone, double elapsed)
{
	switch (imts->state) {
	case IMTS_IDLE:
		timer_stop(&imts->timer);
		imts->rx_guard_duration = elapsed;
		break;
	case IMTS_PAGING:
		if (elapsed >= 0.300 && tone == TONE_GUARD && timer_running(&imts->timer)) {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received acknowledge (Guard tone) from mobile phone (level %.0f%%).\n", imts->last_sigtone_amplitude * 100.0);
			call_up_alerting(imts->callref);
			imts_ringing(imts);
			break;
		}
		break;
	case IMTS_RINGING:
		if (elapsed >= 0.190 && tone == TONE_CONNECT) {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received answer (Connect tone) from mobile phone (level %.0f%%).\n", imts->last_sigtone_amplitude * 100.0);
			call_up_answer(imts->callref, imts->station_id);
			imts_answer(imts);
			break;
		}
		break;
	default:
		;
	}
}

static void ani_after_digit(imts_t *imts)
{
	/* timeout after pulses: digit complete
	 * timeout after digit: ANI in complete
	 */
	if (imts->rx_ani_pulse) {
		if (imts->rx_ani_pulse == 10)
			imts->rx_ani_pulse = 0;
		imts->station_id[imts->rx_ani_index] = imts->rx_ani_pulse + '0';
		imts->rx_ani_pulse = 0;
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received ANI digit '%c' from mobile phone (level %.0f%%).\n", imts->station_id[imts->rx_ani_index], imts->last_sigtone_amplitude * 100.0);
		imts->station_id[++imts->rx_ani_index] = '\0';
		/* update status while receiving station ID */
		imts_display_status();
		/* if all digits have been received */
		if (imts->rx_ani_index == imts->station_length) {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "ANI '%s' complete, sending dial tone.\n", imts->station_id);
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_DIALTONE, 0.0, 0);
			timer_start(&imts->timer, DIALTONE_TO);
			imts->dial_number[0] = '\0';
			imts->rx_dial_index = 0;
			imts->rx_dial_pulse = 0;
			imts_new_state(imts, IMTS_DIALING);
			return;
		}
		timer_start(&imts->timer, ANI_TO);
	} else {
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Timeout receiving ANI from mobile phone, releasing!\n");
		imts_release(imts);
	}
}

static void dial_after_digit(imts_t *imts)
{
	/* special case where nothing happens after dial tone */
	if (!imts->rx_dial_pulse && !imts->rx_dial_index) {
		PDEBUG_CHAN(DANETZ, DEBUG_NOTICE, "Mobile phone does not start dialing, releasing!\n");
		imts_release(imts);
		return;
	}

	/* timeout after pulses: digit complete
	 * timeout after digit: number complete
	 */
	if (imts->rx_dial_pulse) {
		if (imts->rx_dial_index == sizeof(imts->dial_number) - 1) {
			PDEBUG_CHAN(DANETZ, DEBUG_NOTICE, "Mobile phone dials too many digits, releasing!\n");
			imts_release(imts);
			return;
		}
		if (imts->rx_dial_pulse == 10)
			imts->rx_dial_pulse = 0;
		imts->dial_number[imts->rx_dial_index] = imts->rx_dial_pulse + '0';
		imts->rx_dial_pulse = 0;
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received dial digit '%c' from mobile phone. (level %.0f%%)\n", imts->dial_number[imts->rx_dial_index], imts->last_sigtone_amplitude * 100.0);
		imts->dial_number[++imts->rx_dial_index] = '\0';
		timer_start(&imts->timer, DIALING_TO);
	} else {
		int callref = ++new_callref;
		int rc;

		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Timeout receiving dialing from mobile phone, number complete.\n");
		rc = call_up_setup(callref, imts->station_id, imts->dial_number);
		if (rc < 0) {
			PDEBUG_CHAN(DANETZ, DEBUG_NOTICE, "Call rejected (cause %d), releasing!\n", -rc);
			imts_release(imts);
			return;
		}
		imts->callref = callref;
		imts_new_state(imts, IMTS_CONVERSATION);
		imts_set_dsp_mode(imts, DSP_MODE_AUDIO, 0, 0.0, 0);
		imts->rx_disc_pulse = 0;
	}
}

static void page_after_digit(imts_t *imts)
{
	char digit;
	double delay;

	/* timeout after pulses: digit complete */
	if (imts->rx_page_pulse) {
		if (imts->rx_page_pulse < 11) {
			if (imts->rx_page_pulse == 10)
				imts->rx_page_pulse = 0;
			digit = imts->rx_page_pulse + '0';
			delay = get_time() - imts->tx_page_timestamp - PAGE_PULSE_TO;
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Received paging test digit '%c' (level %.0f%%  delay %.0f ms).\n", digit, imts->last_sigtone_amplitude * 100.0, delay * 1000.0);
		}
		imts->rx_page_pulse = 0;
	}
}

/* Timeout handling */
static void imts_timeout(struct timer *timer)
{
	imts_t *imts = (imts_t *)timer->priv;

	switch (imts->state) {
	case IMTS_IDLE:
		switch (imts->last_tone) {
		case TONE_IDLE:
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received idle tone (level of %.0f%%), loopback?\n", imts->last_sigtone_amplitude * 100.0);
			/* trigger reset of decoder to force detection again and again */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, 0.0, 1);
			break;
		case TONE_600:
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received 600 Hz tone with level of %.0f%%, loopback?\n", imts->last_sigtone_amplitude * 100.0);
			/* trigger reset of decoder to force detection again and again */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_600, 0.0, 1);
			break;
		case TONE_GUARD:
			PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Received Guard tone, turning off IDLE tone\n");
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, 0.5, 0);
			break;
		}
		break;
	case IMTS_PAGING:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "No response from mobile phone.\n");
		imts_go_idle(imts);
		call_up_release(imts->callref, CAUSE_OUTOFORDER);
		imts->callref = 0;
		break;
	case IMTS_RINGING:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "No answer from mobile phone's user, releasing.\n");
		imts_release(imts);
		call_up_release(imts->callref, CAUSE_NOANSWER);
		imts->callref = 0;
		break;
	case IMTS_RELEASE:
		imts_go_idle(imts);
		break;
	case IMTS_SEIZE:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending Seize to mobile phone.\n");
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, SEIZE_LENGTH, 0);
		timer_start(&imts->timer, SEIZE_LENGTH + ANI_TO);
		imts->station_id[0] = '\0';
		imts->rx_ani_index = 0;
		imts->rx_ani_pulse = 0;
		imts->rx_ani_totpulses = 0;
		imts_new_state(imts, IMTS_ANI);
		break;
	case IMTS_ANI:
		ani_after_digit(imts);
		break;
	case IMTS_DIALING:
		dial_after_digit(imts);
		break;
	case IMTS_PAGING_TEST:
		page_after_digit(imts);
		break;
	default:
		;
	}
}

/* generate pulse sequence to page phone */
static void paging_pulses_imts(imts_t *imts, int tone)
{
	double duration;
	int pulses;

	pulses = imts->station_id[imts->tx_page_index] - '0';
	if (pulses == 0)
		pulses = 10;

	if (tone == TONE_SEIZE) {
		if (imts->tx_page_pulse == 0)
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Sending paging digit '%c' as pulses.\n", imts->station_id[imts->tx_page_index]);
		/* send mark (pulse start) */
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, PAGE_MARK, 0);
		imts->tx_page_pulse++;
	} else {
		if (imts->tx_page_pulse <= pulses) {
			/* send space (pulse end), use long space after last pulse */
			if (imts->tx_page_pulse < pulses)
				duration = PAGE_SPACE;
			else {
				imts->tx_page_pulse = 0;
				imts->tx_page_index++;
				imts->tx_page_timestamp = get_time();
				/* restart test pattern */
				if (!imts->station_id[imts->tx_page_index] && imts->state == IMTS_PAGING_TEST)
					imts->tx_page_index = 0;
				if (imts->station_id[imts->tx_page_index])
					duration = PAGE_PAUSE;
				else {
					duration = 0;
					timer_start(&imts->timer, PAGING_TO);
				}
			}
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, duration, 0);
		}
	}
}
static void paging_pulses_mts(imts_t *imts, int tone)
{
	double duration;
	int pulses;

	pulses = imts->station_id[imts->tx_page_index] - '0';
	if (pulses == 0)
		pulses = 10;

	if (tone == TONE_1500)
		tone = TONE_600;
	else
		tone = TONE_1500;

	if (imts->tx_page_pulse == 0) {
		PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Sending paging digit '%c' as pulses.\n", imts->station_id[imts->tx_page_index]);
	}
	imts->tx_page_pulse++;
	if (imts->tx_page_pulse < pulses)
		duration = PAGE_PULSE;
	else {
		imts->tx_page_pulse = 0;
		imts->tx_page_index++;
		imts->tx_page_timestamp = get_time();
		/* restart test pattern */
		if (!imts->station_id[imts->tx_page_index] && imts->state == IMTS_PAGING_TEST)
			imts->tx_page_index = 0;
		if (imts->station_id[imts->tx_page_index])
			duration = PAGE_PAUSE;
		else {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Digits complete, assuming the phone is ringing.\n");
			duration = 0;
			imts_new_state(imts, IMTS_RINGING);
			call_up_alerting(imts->callref);
		}
	}
	imts_set_dsp_mode(imts, DSP_MODE_TONE, tone, duration, 0);
}

/* generate pulse sequence to ring phone */
static void ringing_pulses(imts_t *imts, int tone)
{
	if (tone == TONE_IDLE) {
		if (imts->tx_ring_pulse == 0)
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Sending ringing signal as pulses.\n");
		/* send space (pulse end) */
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, RING_SPACE, 0);
		imts->tx_ring_pulse++;
	} else {
		if (imts->tx_ring_pulse < RING_PULSES) {
			/* send mark (pulse start) */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, RING_MARK, 0);
		} else {
			PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Sending pause after ringing.\n");
			/* send long space after last pulse */
			imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, RING_OFF, 0);
			imts->tx_ring_pulse = 0;
		}
	}
}

/* after sending Seize tone switch to silence and await ANI */
static void seize_sent(imts_t *imts, int tone)
{
	if (tone == TONE_SEIZE) {
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, 0.0, 0);
	}
}

/* send test pattern (cycle through tones, skip if length is 0) */
static void detector_test_imts(imts_t *imts, int tone)
{
	switch (tone) {
	case TONE_SILENCE:
tone_idle:
		if (imts->detector_test_length_1 <= 0)
			goto tone_seize;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs IDLE tone.\n", imts->detector_test_length_1);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, imts->detector_test_length_1, 0);
		break;
	case TONE_IDLE:
tone_seize:
		if (imts->detector_test_length_2 <= 0)
			goto tone_silence;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs SEIZE tone.\n", imts->detector_test_length_2);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SEIZE, imts->detector_test_length_2, 0);
		break;
	case TONE_SEIZE:
tone_silence:
		if (imts->detector_test_length_3 <= 0)
			goto tone_idle;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs SILENCE.\n", imts->detector_test_length_3);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, imts->detector_test_length_3, 0);
		break;
	}
}
static void detector_test_mts(imts_t *imts, int tone)
{
	switch (tone) {
	case TONE_SILENCE:
tone_idle:
		if (imts->detector_test_length_1 <= 0)
			goto tone_seize;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs 600 Hz tone.\n", imts->detector_test_length_1);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_600, imts->detector_test_length_1, 0);
		break;
	case TONE_600:
tone_seize:
		if (imts->detector_test_length_2 <= 0)
			goto tone_silence;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs 1500 Hz tone.\n", imts->detector_test_length_2);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_1500, imts->detector_test_length_2, 0);
		break;
	case TONE_1500:
tone_silence:
		if (imts->detector_test_length_3 <= 0)
			goto tone_idle;
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Sending %.3fs SILENCE.\n", imts->detector_test_length_3);
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_SILENCE, imts->detector_test_length_3, 0);
		break;
	}
}

/* whenever a tone has been sent (only for tones with given duration) */
void imts_tone_sent(imts_t *imts, int tone)
{
	switch (imts->state) {
	case IMTS_IDLE:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "No Seize tone after Guard tone, turning on IDLE tone\n");
		imts_set_dsp_mode(imts, DSP_MODE_TONE, TONE_IDLE, 0.0, 0);
		break;
	case IMTS_ANI:
		seize_sent(imts, tone);
		break;
	case IMTS_PAGING:
	case IMTS_PAGING_TEST:
		if (imts->mode == MODE_IMTS)
			paging_pulses_imts(imts, tone);
		else
			paging_pulses_mts(imts, tone);
		break;
	case IMTS_RINGING:
		ringing_pulses(imts, tone);
		break;
	case IMTS_RELEASE:
		imts_release(imts);
		break;
	case IMTS_DETECTOR_TEST:
		if (imts->mode == MODE_IMTS)
			detector_test_imts(imts, tone);
		else
			detector_test_mts(imts, tone);
		break;
	default:
		;
	}
}

/* Call control starts call towards mobile station. */
int call_down_setup(int callref, const char __attribute__((unused)) *caller_id, enum number_type __attribute__((unused)) caller_type, const char *dialing)
{
	char number[8];
	sender_t *sender;
	imts_t *imts;
	int i;

	/* 1. check if given number is already in a call, return BUSY */
	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		if (!strcmp(imts->station_id, dialing))
			break;
	}
	if (sender) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "Outgoing call to busy number, rejecting!\n");
		return -CAUSE_BUSY;
	}

	/* 2. check if all channels are busy, return NOCHANNEL */
	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		if (imts->state == IMTS_IDLE)
			break;
	}
	if (!sender) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "Outgoing call, but no free channel, rejecting!\n");
		return -CAUSE_NOCHANNEL;
	}

	/* 3. check if number is invalid, return INVALNUMBER */
	if (strlen(dialing) == 12 && !strncmp(dialing, "+1", 2))
		dialing += 2;
	if (strlen(dialing) == 11 && !strncmp(dialing, "1", 1))
		dialing += 1;
	if (strlen(dialing) == 10 && imts->station_length == 7) {
		strncpy(number, dialing, 3);
		strcpy(number + 3, dialing + 6);
		dialing = number;
	}
	if (strlen(dialing) == 10 && imts->station_length == 5)
		dialing += 5;
	if ((int)strlen(dialing) != imts->station_length) {
inval:
		PDEBUG(DIMTS, DEBUG_NOTICE, "Outgoing call to invalid number '%s', rejecting!\n", dialing);
		return -CAUSE_INVALNUMBER;
	}
	for (i = 0; i < (int)strlen(dialing); i++) {
		if (dialing[i] < '0' || dialing[i] > '9')
			goto inval;
	}

	PDEBUG_CHAN(DIMTS, DEBUG_INFO, "Call to mobile station, paging number: %s\n", dialing);

	/* 4. trying to page mobile station */
	imts->callref = callref;
	imts_paging(imts, dialing, 0);

	return 0;
}

void call_down_answer(int __attribute__((unused)) callref)
{
}

/* Call control sends disconnect (with tones).
 * An active call stays active, so tones and annoucements can be received
 * by mobile station.
 */
void call_down_disconnect(int callref, int cause)
{
	sender_t *sender;
	imts_t *imts;

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

	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		if (imts->callref == callref)
			break;
	}
	if (!sender) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "Outgoing disconnect, but no callref!\n");
		call_up_release(callref, CAUSE_INVALCALLREF);
		return;
	}

	/* Release when not active */
	if (imts->state == IMTS_CONVERSATION)
		return;
	switch (imts->state) {
	case IMTS_PAGING:
	case IMTS_RINGING:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Outgoing disconnect, during paging/alerting, releasing!\n");
	 	imts_release(imts);
		break;
	default:
		break;
	}

	call_up_release(callref, cause);

	imts->callref = 0;

}

/* Call control releases call toward mobile station. */
void call_down_release(int callref, __attribute__((unused)) int cause)
{
	sender_t *sender;
	imts_t *imts;

	PDEBUG(DIMTS, DEBUG_INFO, "Call has been released by network, releasing call.\n");

	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		if (imts->callref == callref)
			break;
	}
	if (!sender) {
		PDEBUG(DIMTS, DEBUG_NOTICE, "Outgoing release, but no callref!\n");
		/* don't send release, because caller already released */
		return;
	}

	imts->callref = 0;

	switch (imts->state) {
	case IMTS_CONVERSATION:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Outgoing release, during call, releasing!\n");
	 	imts_release(imts);
		break;
	case IMTS_PAGING:
	case IMTS_RINGING:
		PDEBUG_CHAN(DIMTS, DEBUG_NOTICE, "Outgoing release, during paging/alerting, releasing!\n");
	 	imts_release(imts);
		break;
	default:
		break;
	}
}

/* Receive audio from call instance. */
void call_down_audio(int callref, sample_t *samples, int count)
{
	sender_t *sender;
	imts_t *imts;

	for (sender = sender_head; sender; sender = sender->next) {
		imts = (imts_t *) sender;
		if (imts->callref == callref)
			break;
	}
	if (!sender)
		return;

	if (imts->dsp_mode == DSP_MODE_AUDIO) {
		sample_t up[(int)((double)count * imts->sender.srstate.factor + 0.5) + 10];
		count = samplerate_upsample(&imts->sender.srstate, samples, count, up);
		jitter_save(&imts->sender.dejitter, up, count);
	}
}

void dump_info(void) {}