summaryrefslogtreecommitdiffstats
path: root/ss5.cpp
blob: 09f97eb13b7363c13b20bfc710b6e7adfba7160d (plain) (blame)
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
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
/*****************************************************************************\
**                                                                           **
** LCR                                                                       **
**                                                                           **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg                                              **
**                                                                           **
** mISDN ss5                                                                 **
**                                                                           **
\*****************************************************************************/ 

/*
 * STATES:
 *
 * there are three types of states
 *
 * - the port state (p_state): used for current call state
 * - the ss5 state (p_m_s_state): used for current tone
 * - the ss5 signal state (p_m_s_signal): used for current signal state of current tone
 *
 * the port state differs from isdn state:
 * 
 * - PORT_STATE_IDLE: used until number is complete. outgoing overlap dialing is received in this state.
 * - PORT_STATE_OUT_SETUP: the seizing procedure is started.
 * - PORT_STATE_OUT_OVERLAP: the transmitter is sending the digits.
 * - PORT_STATE_OUT_PROCEEDING: the digits are sent, we wait until someone answers.
 * - PORT_STATE_CONNECT: a call is answered on either side.
 * - PORT_STATE_IN_SETUP: the seizing is received, we wait for the first digit.
 * - PORT_STATE_IN_OVERLAP: the digits are received.
 * - PORT_STATE_IN_PROCEEDING: the number is complete, a SETUP is indicated.
 * - PORT_STATE_IN_DISCONNECT: a clear-back was received, an DISCONNECT is indicated.
 * - PORT_STATE_RELEASE: the clear forward procedure is started.
 *
 */

#include "main.h"

//#define DEBUG_DETECT

/* ss5 signal states */
enum {
	SS5_STATE_IDLE,			/* no signal */
	SS5_STATE_SEIZING,		/* seizing */
	SS5_STATE_PROCEED_TO_SEND,	/* proceed-to-send */
	SS5_STATE_BUSY_FLASH,		/* busy-flash / clear back */
	SS5_STATE_ACK_BUSY_FLASH,	/* acknowledge of busy/answer/clear-back */
	SS5_STATE_ANSWER,		/* answer */
	SS5_STATE_ACK_ANSWER,		/* acknowledge of busy/answer/clear-back */
	SS5_STATE_FORWARD_TRANSFER,	/* forward transfer */
	SS5_STATE_CLEAR_BACK,		/* clear-back */
	SS5_STATE_ACK_CLEAR_BACK,	/* acknowledge of busy/answer/clear-back */
	SS5_STATE_CLEAR_FORWARD,	/* clear-forward */
	SS5_STATE_RELEASE_GUARD,	/* release-guard */
	SS5_STATE_DIAL_OUT,		/* dialing state (transmitter) */
	SS5_STATE_DIAL_IN,		/* dialing state (receiver) */
	SS5_STATE_DIAL_IN_PULSE,	/* dialing state (receiver with pulses) */
	SS5_STATE_DELAY,		/* after signal wait until next signal can be sent */
	SS5_STATE_DOUBLE_SEIZE,		/* in case of a double seize, we make the remote size recognize it */
};
const char *ss5_state_name[] = {
	"STATE_IDLE",
	"STATE_SEIZING",
	"STATE_PROCEED_TO_SEND",
	"STATE_BUSY_FLASH",
	"STATE_ACK_BUSY_FLASH",
	"STATE_ANSWER",
	"STATE_ACK_ANSWER",
	"STATE_FORWARD_TRANSFER",
	"STATE_CLEAR_BACK",
	"STATE_ACK_CLEAR_BACK",
	"STATE_CLEAR_FORWARD",
	"STATE_RELEASE_GUARD",
	"STATE_DIAL_OUT",
	"STATE_DIAL_IN",
	"STATE_DIAL_IN_PULSE",
	"STATE_DELAY",
	"STATE_DOUBLE_SEIZE",
};

enum {
	SS5_SIGNAL_NULL,
	/* sending signal states */
	SS5_SIGNAL_SEND_ON,		/* sending signal, waiting for acknowledge */
	SS5_SIGNAL_SEND_ON_RECOG,	/* sending signal, receiving ack, waiting for recogition timer */
	SS5_SIGNAL_SEND_OFF,		/* silence, receiving ack, waiting for stop */
	/* receiving signal states */
	SS5_SIGNAL_RECEIVE_RECOG,	/* receiving signal, waiting for recognition timer */
	SS5_SIGNAL_RECEIVE,		/* receiving signal / send ack, waiting for stop */
	SS5_SIGNAL_DELAY,		/* delay after release guard to prevent ping-pong */
	/* sending / receiving digit states */
	SS5_SIGNAL_DIGIT_PAUSE,		/* pausing before sending (next) digit */
	SS5_SIGNAL_DIGIT_ON,		/* sending digit */
	SS5_SIGNAL_PULSE_OFF,		/* make */
	SS5_SIGNAL_PULSE_ON,		/* break */
};
const char *ss5_signal_name[] = {
	"NULL",
	"SIGNAL_SEND_ON",
	"SIGNAL_SEND_ON_RECOG",
	"SIGNAL_SEND_OFF",
	"SIGNAL_RECEIVE_RECOG",
	"SIGNAL_RECEIVE",
	"SIGNAL_DELAY",
	"SIGNAL_DIGIT_PAUSE",
	"SIGNAL_DIGIT_ON",
	"SIGNAL_PULSE_OFF",
	"SIGNAL_PULSE_ON",
};

/* ss5 signal timers (in samples) */
#define	SS5_TIMER_AFTER_SIGNAL	(100*8)	/* wait after signal is terminated */
#define SS5_TIMER_KP		(100*8)	/* duration of KP1 or KP2 digit */
#define SS5_TIMER_DIGIT		(55*8)	/* duration of all other digits */
#define SS5_TIMER_PAUSE		(55*8)	/* pause between digits */
#define SS5_TIMER_FORWARD	(850*8)	/* forward transfer length */
#define SS5_TIMER_RECOG_SEIZE	(40*8)	/* recognition time of seizing / proceed-to-send signal */
#define SS5_TIMER_RECOG_OTHER	(125*8)	/* recognition time of all other f1/f2 signals */
#define SS5_TIMER_SIGNAL_LOSS	(15*8)	/* minimum time of signal loss for a continous signal */
#define SS5_TIMER_DOUBLE_SEIZE	(850*8)	/* double seize length */
#define SS5_TIMER_RELEASE_GUARD	(850*8)	/* be sure to release after clear-forward */
#define SS5_TIMER_RELEASE_MAX	(2000*8)/* maximum time for release guard to prevent 'double-releasing' */
#define	SS5_TIMER_RELEASE_DELAY	(4000*8)/* wait after release guard to prevent ping-pong */
#define BELL_TIMER_BREAK	(50*8)	/* loop open, tone */
#define BELL_TIMER_MAKE		(50*8)	/* loop closed, no tone */
#define BELL_TIMER_PAUSE	(800*8)	/* interdigit delay */
#define BELL_TIMER_RECOG_HANGUP	(200*8) /* time to recognize hangup */
#define BELL_TIMER_RECOG_END	(300*8) /* recognize end of digit */

/* ss5 timers */
#define SS5_TIMER_OVERLAP	5.0	/* timeout for overlap digits received on outgoing exchange */


/*
 * ss5 trace header
 */
enum { /* even values are indications, odd values are requests */
	SS5_SEIZING_IND,
	SS5_SEIZING_REQ,
	SS5_PROCEED_TO_SEND_IND,
	SS5_PROCEED_TO_SEND_REQ,
	SS5_BUSY_FLASH_IND,
	SS5_BUSY_FLASH_REQ,
	SS5_ANSWER_IND,
	SS5_ANSWER_REQ,
	SS5_CLEAR_BACK_IND,
	SS5_CLEAR_BACK_REQ,
	SS5_CLEAR_FORWARD_IND,
	SS5_CLEAR_FORWARD_REQ,
	SS5_RELEASE_GUARD_IND,
	SS5_RELEASE_GUARD_REQ,
	SS5_ACKNOWLEDGE_IND,
	SS5_ACKNOWLEDGE_REQ,
	SS5_DOUBLE_SEIZURE_IND,
	SS5_DOUBLE_SEIZURE_REQ,
	SS5_DIALING_IND,
	SS5_DIALING_REQ,
	SS5_FORWARD_TRANSFER_IND,
	SS5_FORWARD_TRANSFER_REQ,
};
static struct isdn_message {
	const char *name;
	unsigned int value;
} ss5_message[] = {
	{"SEIZING RECEIVED", SS5_SEIZING_IND},
	{"SEIZING SENDING", SS5_SEIZING_REQ},
	{"PROCEED-TO-SEND RECEIVED", SS5_PROCEED_TO_SEND_IND},
	{"PROCEED-TO-SEND SENDING", SS5_PROCEED_TO_SEND_REQ},
	{"BUSY-FLASH RECEIVED", SS5_BUSY_FLASH_IND},
	{"BUSY-FLASH SENDING", SS5_BUSY_FLASH_REQ},
	{"ANSWER RECEIVED", SS5_ANSWER_IND},
	{"ANSWER SENDING", SS5_ANSWER_REQ},
	{"CLEAR-BACK RECEIVED", SS5_CLEAR_BACK_IND},
	{"CLEAR-BACK SENDING", SS5_CLEAR_BACK_REQ},
	{"CLEAR-FORWARD RECEIVED", SS5_CLEAR_FORWARD_IND},
	{"CLEAR-FORWARD SENDING", SS5_CLEAR_FORWARD_REQ},
	{"RELEASE-GUARD RECEIVED", SS5_RELEASE_GUARD_IND},
	{"RELEASE-GUARD SENDING", SS5_RELEASE_GUARD_REQ},
	{"ACKNOWLEDGE RECEIVED", SS5_ACKNOWLEDGE_IND},
	{"ACKNOWLEDGE SENDING", SS5_ACKNOWLEDGE_REQ},
	{"DOUBLE-SEIZURE RECEIVED", SS5_DOUBLE_SEIZURE_IND},
	{"DOUBLE-SEIZURE SENDING", SS5_DOUBLE_SEIZURE_REQ},
	{"DIALING RECEIVED", SS5_DIALING_IND},
	{"DIALING SENDING", SS5_DIALING_REQ},
	{"FORWARD-TRANSFER RECEIVED", SS5_FORWARD_TRANSFER_IND},
	{"FORWARD-TRANSFER SENDING", SS5_FORWARD_TRANSFER_REQ},
	{NULL, 0},
};
static void ss5_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg, int channel)
{
	int i;
	char msgtext[64];

	SCPY(msgtext, "<<UNKNOWN MESSAGE>>");
	/* select message and primitive text */
	i = 0;
	while(ss5_message[i].name) {
//		if (msg == L3_NOTIFY_REQ) printf("val = %x %s\n", isdn_message[i].value, isdn_message[i].name);
		if (ss5_message[i].value == msg) {
			SCPY(msgtext, ss5_message[i].name);
			break;
		}
		i++;
	}

	/* init trace with given values */
	start_trace(mISDNport?mISDNport->portnum:-1,
		    mISDNport?(mISDNport->ifport?mISDNport->ifport->interface:NULL):NULL,
		    port?numberrize_callerinfo(port->p_callerinfo.id, port->p_callerinfo.ntype, options.national, options.international):NULL,
		    port?port->p_dialinginfo.id:NULL,
		    (msg&1)?DIRECTION_OUT:DIRECTION_IN,
		    CATEGORY_CH,
		    port?port->p_serial:0,
		    msgtext);
	add_trace("channel", NULL, "%d", channel);
	switch (port->p_type) {
		case PORT_TYPE_SS5_OUT:
		add_trace("state", NULL, "outgoing");
		break;
		case PORT_TYPE_SS5_IN:
		add_trace("state", NULL, "incomming");
		break;
		default:
		add_trace("state", NULL, "idle");
		break;
	}
}


/*
 * changes release tone into silence
 * this makes the line sound more authentic
 */
void Pss5::set_tone(const char *dir, const char *name)
{
	if (name && !strcmp(name, "cause_10"))
		name = NULL;

	PmISDN::set_tone(dir, name);
}

/*
 * creation of static channels
 */
void ss5_create_channel(struct mISDNport *mISDNport, int i)
{
	class Pss5		*ss5port;
	char			portname[32];
	struct port_settings	port_settings;

	SPRINT(portname, "%s-%d", mISDNport->name, i+1);

	memset(&port_settings, 0, sizeof(port_settings));
	SCPY(port_settings.tones_dir, options.tones_dir);

	ss5port = new Pss5(PORT_TYPE_SS5_IDLE, mISDNport, portname, &port_settings, i + (i>=15) + 1, 1, B_MODE_TRANSPARENT);
	if (!ss5port)
		FATAL("No memory for Pss5 class.\n");
	if (!ss5port->p_m_b_channel)
		FATAL("No bchannel on given index.\n");

	/* connect channel */
	bchannel_event(mISDNport, ss5port->p_m_b_index, B_EVENT_USE);

}


/*
 * hunt for a free line
 * this function returns a port object in idle state.
 */
class Pss5 *ss5_hunt_line(struct mISDNport *mISDNport)
{
	int i;
	class Port	*port;
	class Pss5	*ss5port = NULL;
	struct select_channel *selchannel; 

	PDEBUG(DEBUG_SS5, "Entered name=%s\n", mISDNport->name);
	selchannel = mISDNport->ifport->out_channel;
	while(selchannel) {
		switch(selchannel->channel) {
			case CHANNEL_FREE: /* free channel */
			case CHANNEL_ANY: /* any channel */
			for (i = 0; i < mISDNport->b_num; i++) {
				port = mISDNport->b_port[i];
				PDEBUG(DEBUG_SS5, "Checking port %p on index\n", port, i);
				if (!port)
					continue;
				if (port->p_type == PORT_TYPE_SS5_IN || port->p_type == PORT_TYPE_SS5_OUT)
					PDEBUG(DEBUG_SS5, "Checking port %s: channel %d not available, because port not idle type.\n",
						mISDNport->name, i);
				if (port->p_type != PORT_TYPE_SS5_IDLE)
					continue;
				ss5port = (class Pss5 *)port;
				/* is really idle ? */
				if (ss5port->p_state == PORT_STATE_IDLE
				 && ss5port->p_m_s_state == SS5_STATE_IDLE)
					return ss5port;
				PDEBUG(DEBUG_SS5, "Checking port %s: channel %d not available, because p_state=%d, ss5_state=%d.\n",
					mISDNport->name, i, ss5port->p_state,ss5port->p_m_s_state);
			}
			PDEBUG(DEBUG_SS5, "no free interface\n");
			return NULL;

			case CHANNEL_NO:
			break;

			default:
			if (selchannel->channel<1 || selchannel->channel==16)
				break;
			i = selchannel->channel-1-(selchannel->channel>=17);
			if (i >= mISDNport->b_num)
				break;
			port = mISDNport->b_port[i];
			if (!port)
				break;
			if (port->p_type == PORT_TYPE_SS5_IN || port->p_type == PORT_TYPE_SS5_OUT)
				PDEBUG(DEBUG_SS5, "Checking port %s: channel %d not available, because port not idle type.\n",
					mISDNport->name, i);
			if (port->p_type != PORT_TYPE_SS5_IDLE)
				break;
			ss5port = (class Pss5 *)port;
			/* is really idle ? */
			if (ss5port->p_state == PORT_STATE_IDLE
			 && ss5port->p_m_s_state == SS5_STATE_IDLE)
				return ss5port;
			PDEBUG(DEBUG_SS5, "Checking port %s: channel %d not available, because p_state=%d, ss5_state=%d.\n",
				mISDNport->name, i, ss5port->p_state,ss5port->p_m_s_state);
		}
		selchannel = selchannel->next;
	}
	PDEBUG(DEBUG_SS5, "no free interface in channel list\n");
	return NULL;
}

int queue_event(struct lcr_work *work, void *instance, int index);

/*
 * constructor
 */
Pss5::Pss5(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
{
	p_callerinfo.itype = (mISDNport->ifport->interface->extension)?INFO_ITYPE_ISDN_EXTENSION:INFO_ITYPE_ISDN;
	p_m_s_state = SS5_STATE_IDLE;
	p_m_s_signal = SS5_SIGNAL_NULL;
	p_m_s_dial[0] = '\0';
	p_m_s_digit_i = 0;
	p_m_s_pulsecount = 0;
	p_m_s_last_digit = ' ';
	p_m_s_last_digit_used = ' ';
	p_m_s_signal_loss = 0;
	p_m_s_decoder_count = 0;
	//p_m_s_decoder_buffer;
	p_m_s_sample_nr = 0;
	p_m_s_recog = 0;
	memset(&p_m_s_queue, 0, sizeof(p_m_s_queue));
	add_work(&p_m_s_queue, queue_event, this, 0);
	p_m_s_answer = 0;
	p_m_s_busy_flash = 0;
	p_m_s_clear_back = 0;
	memset(p_m_s_delay_digits, ' ', sizeof(p_m_s_delay_digits));
	memset(p_m_s_delay_mute, ' ', sizeof(p_m_s_delay_mute));

	/* turn on signalling receiver */
	inband_receive_on();

	PDEBUG(DEBUG_SS5, "Created new mISDNPort(%s). Currently %d objects use.\n", portname, mISDNport->use);
}


/*
 * destructor
 */
Pss5::~Pss5()
{
	del_work(&p_m_s_queue);
}


/*
 * change ss5 states
 */
void Pss5::_new_ss5_state(int state, const char *func, int line)
{
	PDEBUG(DEBUG_SS5, "%s(%s:%d): changing SS5 state from %s to %s\n", p_name, func, line, ss5_state_name[p_m_s_state], ss5_state_name[state]);
	p_m_s_state = state;
	p_m_s_signal = SS5_SIGNAL_NULL;

	if (p_m_s_state == SS5_STATE_IDLE && (p_m_s_answer || p_m_s_busy_flash || p_m_s_clear_back))
		trigger_work(&p_m_s_queue);
}

int queue_event(struct lcr_work *work, void *instance, int index)
{
	class Pss5 *ss5port = (class Pss5 *)instance;

	if (ss5port->p_m_s_state == SS5_STATE_IDLE) {
		/* if answer signal is queued */
		if (ss5port->p_m_s_answer) {
			ss5port->p_m_s_answer = 0;
			/* start answer */
			ss5_trace_header(ss5port->p_m_mISDNport, ss5port, SS5_ANSWER_REQ, ss5port->p_m_b_channel);
			end_trace();
			ss5port->start_signal(SS5_STATE_ANSWER);
		}

		/* if busy-flash signal is queued */
		if (ss5port->p_m_s_busy_flash) {
			ss5port->p_m_s_busy_flash = 0;
			/* start busy-flash */
			ss5_trace_header(ss5port->p_m_mISDNport, ss5port, SS5_BUSY_FLASH_REQ, ss5port->p_m_b_channel);
			end_trace();
			ss5port->start_signal(SS5_STATE_BUSY_FLASH);
		}

		/* if clear-back signal is queued */
		if (ss5port->p_m_s_clear_back) {
			ss5port->p_m_s_clear_back = 0;
			/* start clear-back */
			ss5_trace_header(ss5port->p_m_mISDNport, ss5port, SS5_CLEAR_BACK_REQ, ss5port->p_m_b_channel);
			end_trace();
			ss5port->start_signal(SS5_STATE_CLEAR_BACK);
		}
	}

	return 0;
}

void Pss5::_new_ss5_signal(int signal, const char *func, int line)
{
	if (p_m_s_signal)
		PDEBUG(DEBUG_SS5, "%s(%s:%d): changing SS5 signal state from %s to %s\n", p_name, func, line, ss5_signal_name[p_m_s_signal], ss5_signal_name[signal]);
	else
		PDEBUG(DEBUG_SS5, "%s(%s:%d): changing SS5 signal state to %s\n", p_name, func, line, ss5_signal_name[signal]);
	p_m_s_signal = signal;
}


/*
 * signalling receiver
 *
 * this function will be called for every audio received.
 */
void Pss5::inband_receive(unsigned char *buffer, int len)
{
	int count = 0, tocopy, space;
	char digit;

	again:
	/* how much to copy ? */
	tocopy = len - count;
	space = SS5_DECODER_NPOINTS - p_m_s_decoder_count;
	if (space < 0)
		FATAL("p_m_s_decoder_count overflows\n");
	if (space < tocopy)
		tocopy = space;
	/* copy an count */
	memcpy(p_m_s_decoder_buffer+p_m_s_decoder_count, buffer+count, tocopy);
	p_m_s_decoder_count += tocopy;
	count += tocopy;
	/* decoder buffer not completely filled ? */
	if (tocopy < space)
		return;

	/* decode one frame */
	digit = ss5_decode(p_m_s_decoder_buffer, SS5_DECODER_NPOINTS);
	p_m_s_decoder_count = 0;

#ifdef DEBUG_DETECT
	if (p_m_s_last_digit != digit && digit != ' ')
		PDEBUG(DEBUG_SS5, "%s: detecting signal '%c' start (state=%s signal=%s)\n", p_name, digit, ss5_state_name[p_m_s_state], ss5_signal_name[p_m_s_signal]);
#endif

	/* ignore short loss of signal, or change within one decode window */
	if (p_m_s_signal_loss) {
		if (digit == ' ') {
			/* still lost */
			if (p_m_s_signal_loss >= SS5_TIMER_SIGNAL_LOSS) {
#ifdef DEBUG_DETECT
				PDEBUG(DEBUG_SS5, "%s: signal '%c' lost too long\n", p_name, p_m_s_last_digit);
#endif
				/* long enough, we stop loss-timer */
				p_m_s_signal_loss = 0;
			} else {
				/* not long enough, so we use last signal */
				p_m_s_signal_loss += SS5_DECODER_NPOINTS;
				digit = p_m_s_last_digit;
			}
		} else {
			/* signal is back, we stop timer and store */
#ifdef DEBUG_DETECT
			PDEBUG(DEBUG_SS5, "%s: signal '%c' lost, but continues with '%c'\n", p_name, p_m_s_last_digit, digit);
#endif
			p_m_s_signal_loss = 0;
			p_m_s_last_digit = digit;
		}
	} else {
		if (p_m_s_last_digit != ' ' && digit == ' ') {
#ifdef DEBUG_DETECT
			PDEBUG(DEBUG_SS5, "%s: signal '%c' lost\n", p_name, p_m_s_last_digit);
#endif
			/* restore last digit until signal is really lost */
			p_m_s_last_digit = digit;
			/* starting to loose signal */
			p_m_s_signal_loss = SS5_DECODER_NPOINTS;
		} else if (digit != p_m_s_last_digit) {
			/* digit changes, but we keep old digit until it is detected twice */
#ifdef DEBUG_DETECT
			PDEBUG(DEBUG_SS5, "%s: signal '%c' changes to '%c'\n", p_name, p_m_s_last_digit, digit);
#endif
			p_m_s_last_digit = digit;
			digit = p_m_s_last_digit_used;
		} else {
			/* storing last signal, in case it is lost */
			p_m_s_last_digit = digit;
		}
	}
	p_m_s_last_digit_used = digit;

	/* update mute */
	if ((p_m_mISDNport->ss5 & SS5_FEATURE_SUPPRESS)) {
		int mdigit;
		memcpy(p_m_s_delay_mute, p_m_s_delay_mute+1, sizeof(p_m_s_delay_mute)-1);
		p_m_s_delay_mute[sizeof(p_m_s_delay_mute)-1] = digit;
		mdigit = p_m_s_delay_mute[0];
		if (p_m_mute) {
			/* mute is on */
			if (mdigit != 'A' && mdigit != 'B' && mdigit != 'C')
				mute_off();
		} else {
			/* mute is off */
			if (mdigit == 'A' || mdigit == 'B' || mdigit == 'C')
				mute_on();
		}
	}

	/* delay decoded tones */
	if ((p_m_mISDNport->ss5 & SS5_FEATURE_DELAY)) {
		/* shift buffer */
		memcpy(p_m_s_delay_digits, p_m_s_delay_digits+1, sizeof(p_m_s_delay_digits)-1);
		/* first in */
		p_m_s_delay_digits[sizeof(p_m_s_delay_digits)-1] = digit;
		/* first out */
		digit = p_m_s_delay_digits[0];
	}

	/* clear forward is always recognized */
	if (digit == 'C' && p_m_s_state != SS5_STATE_CLEAR_FORWARD && p_m_s_state != SS5_STATE_RELEASE_GUARD) {
		switch (p_type) {
			case PORT_TYPE_SS5_OUT:
			PDEBUG(DEBUG_SS5, "%s: received release-guard, waiting for recognition\n", p_name);
			break;
			case PORT_TYPE_SS5_IN:
			PDEBUG(DEBUG_SS5, "%s: received clear-forward, waiting for recognition\n", p_name);
			break;
			default:
			PDEBUG(DEBUG_SS5, "%s: received clear-forward in idle state, waiting for recognition\n", p_name);
			break;
		}
		new_ss5_state(SS5_STATE_RELEASE_GUARD);
		new_ss5_signal(SS5_SIGNAL_RECEIVE_RECOG);
		p_m_s_recog = 0;
	} else
	switch(p_m_s_state) {
		case SS5_STATE_IDLE:
		/* seizing only recognized in port idle state */
		if (p_state == PORT_STATE_IDLE) {
			if (digit != 'A')
				break;
			seize:
			PDEBUG(DEBUG_SS5, "%s: received seize, waiting for recognition\n", p_name);
			p_type = PORT_TYPE_SS5_IN;
			new_ss5_state(SS5_STATE_PROCEED_TO_SEND);
			new_ss5_signal(SS5_SIGNAL_RECEIVE_RECOG);
			p_m_s_recog = 0;
			break;
		}
		/* other signals */
		if (digit == 'A') {
			if (p_type != PORT_TYPE_SS5_OUT)
				break;
			PDEBUG(DEBUG_SS5, "%s: received answer, waiting for recognition\n", p_name);
			new_ss5_state(SS5_STATE_ACK_ANSWER);
			new_ss5_signal(SS5_SIGNAL_RECEIVE_RECOG);
			p_m_s_recog = 0;
			break;
		}
		if (digit == 'B') {
			if (p_type == PORT_TYPE_SS5_IN) {
				if ((p_m_mISDNport->ss5 & SS5_FEATURE_BELL)) {
					new_ss5_state(SS5_STATE_DIAL_IN_PULSE); /* go pulsing state */
					new_ss5_signal(SS5_SIGNAL_PULSE_OFF); /* we are starting with pulse off */
					p_m_s_pulsecount = 0; /* init pulse counter */
					p_m_s_dial[0] = '\0'; /* init dial string */
					pulse_ind(1); /* also inits recogition timer... */
					break;
				}
				PDEBUG(DEBUG_SS5, "%s: received forward-transfer, waiting for recognition\n", p_name);
				/* forward transfer on incomming lines */
				new_ss5_state(SS5_STATE_FORWARD_TRANSFER);
				new_ss5_signal(SS5_SIGNAL_RECEIVE_RECOG);
				p_m_s_recog = 0;
				break;
			}
			if (p_state == PORT_STATE_CONNECT) {
				PDEBUG(DEBUG_SS5, "%s: received clear-back, waiting for recognition\n", p_name);
				new_ss5_state(SS5_STATE_ACK_CLEAR_BACK);
			} else {
				PDEBUG(DEBUG_SS5, "%s: received busy-flash, waiting for recognition\n", p_name);
				new_ss5_state(SS5_STATE_ACK_BUSY_FLASH);
			}
			new_ss5_signal(SS5_SIGNAL_RECEIVE_RECOG);
			p_m_s_recog = 0;
			break;
		}
		/* dialing only allowed in incomming setup state */
		if (p_state == PORT_STATE_IN_SETUP) {
			if (!strchr("1234567890*#abc", digit))
				break;
			PDEBUG(DEBUG_SS5, "%s: received dialing start with '%c'\n", p_name, digit);
			new_ss5_state(SS5_STATE_DIAL_IN);
			new_ss5_signal(SS5_SIGNAL_DIGIT_ON);
			p_m_s_dial[0] = '\0';
			digit_ind(digit);
			break;
		}
		break;
		/* sending seizing */
		case SS5_STATE_SEIZING:
		switch (p_m_s_signal) {
			case SS5_SIGNAL_SEND_ON:
			if (digit == 'A') { /* double seize */
				PDEBUG(DEBUG_SS5, "%s: received double seizure\n", p_name, digit);
				double_seizure_ind();
				break;
			}
			if (digit == 'B') {
				PDEBUG(DEBUG_SS5, "%s: received answer to outgoing seize, waiting for recognition\n", p_name);
				/* set recognition timer */
				new_ss5_signal(SS5_SIGNAL_SEND_ON_RECOG);
				p_m_s_recog = 0;
			}
			break;
			case SS5_SIGNAL_SEND_ON_RECOG:
			if (digit != 'B') { /* seize */
				PDEBUG(DEBUG_SS5, "%s: answer to outgoing seize is gone before recognition\n", p_name);
				new_ss5_signal(SS5_SIGNAL_SEND_ON);
//				p_m_s_sample_nr = 0;
//				inband_send_on();
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_SEIZE)
				break;
			PDEBUG(DEBUG_SS5, "%s: answer to outgoing seize recognized, turning off, waiting for recognition\n", p_name);
			new_ss5_signal(SS5_SIGNAL_SEND_OFF);
			break;
			case SS5_SIGNAL_SEND_OFF:
			if (digit == 'B')
				break;
			PDEBUG(DEBUG_SS5, "%s: outgoing seizure is complete, proceeding...\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			proceed_to_send_ind();
			break;
		}
		break;
		/* answer to seize */
		case SS5_STATE_PROCEED_TO_SEND:
		if (p_m_s_signal == SS5_SIGNAL_RECEIVE_RECOG) {
			if (digit != 'A') {
				PDEBUG(DEBUG_SS5, "%s: incomming seize is gone before recognition\n", p_name);
				new_ss5_state(SS5_STATE_IDLE);
				p_type = PORT_TYPE_SS5_IDLE;
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_SEIZE)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming seize is recognized, responding...\n", p_name);
			new_ss5_signal(SS5_SIGNAL_RECEIVE);
			p_m_s_sample_nr = 0;
			inband_send_on();
			break;
		}
		if (digit != 'A') {
			PDEBUG(DEBUG_SS5, "%s: incomming seize is gone after responding\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			seizing_ind();
		}
		break;
		/* sending busy flash / answer / clear-back */
		case SS5_STATE_BUSY_FLASH:
		case SS5_STATE_ANSWER:
		case SS5_STATE_CLEAR_BACK:
		switch (p_m_s_signal) {
			case SS5_SIGNAL_SEND_ON:
			if (digit == 'A') {
				PDEBUG(DEBUG_SS5, "%s: received acknowledge, waiting for recognition\n", p_name);
				/* set recognition timer */
				new_ss5_signal(SS5_SIGNAL_SEND_ON_RECOG);
				p_m_s_recog = 0;
			}
			break;
			case SS5_SIGNAL_SEND_ON_RECOG:
			if (digit != 'A') {
				PDEBUG(DEBUG_SS5, "%s: acknowledge is gone before recognition\n", p_name);
				new_ss5_signal(SS5_SIGNAL_SEND_ON);
//				p_m_s_sample_nr = 0;
//				inband_send_on();
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			PDEBUG(DEBUG_SS5, "%s: acknowledge recognized, turning off, waiting for recognition\n", p_name);
			new_ss5_signal(SS5_SIGNAL_SEND_OFF);
			break;
			case SS5_SIGNAL_SEND_OFF:
			if (digit == 'A')
				break;
			PDEBUG(DEBUG_SS5, "%s: outgoing signal is complete\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			break;
		}
		break;
		/* answer to busy-flash / clear back */
		case SS5_STATE_ACK_BUSY_FLASH:
		case SS5_STATE_ACK_CLEAR_BACK:
		if (p_m_s_signal == SS5_SIGNAL_RECEIVE_RECOG) {
			if (digit != 'B') {
				PDEBUG(DEBUG_SS5, "%s: incomming clear-back/busy-flash is gone before recognition\n", p_name);
				new_ss5_state(SS5_STATE_IDLE);
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming clear-back/busy-flash is recognized, responding...\n", p_name);
			new_ss5_signal(SS5_SIGNAL_RECEIVE);
			p_m_s_sample_nr = 0;
			inband_send_on();
			break;
		}
		if (digit != 'B') {
			PDEBUG(DEBUG_SS5, "%s: incomming clear-back/busy-flash is gone after responding\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			if (p_m_s_state == SS5_STATE_ACK_BUSY_FLASH)
				busy_flash_ind();
			else
				clear_back_ind();
		}
		break;
		/* answer to answer */
		case SS5_STATE_ACK_ANSWER:
		if (p_m_s_signal == SS5_SIGNAL_RECEIVE_RECOG) {
			if (digit != 'A') {
				PDEBUG(DEBUG_SS5, "%s: incomming answer is gone before recognition\n", p_name);
				new_ss5_state(SS5_STATE_IDLE);
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming answer is recognized, responding...\n", p_name);
			new_ss5_signal(SS5_SIGNAL_RECEIVE);
			p_m_s_sample_nr = 0;
			inband_send_on();
			break;
		}
		if (digit != 'A') {
			PDEBUG(DEBUG_SS5, "%s: incomming answer is gone after responding\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			answer_ind();
		}
		break;
		/* sending clear-forward */
		case SS5_STATE_CLEAR_FORWARD:
		switch (p_m_s_signal) {
			case SS5_SIGNAL_SEND_ON:
			if (digit == 'C') {
				PDEBUG(DEBUG_SS5, "%s: received answer to clear-forward, waiting for recognition\n", p_name);
				/* set recognition timer */
				new_ss5_signal(SS5_SIGNAL_SEND_ON_RECOG);
				p_m_s_recog = 0;
			}
			break;
			case SS5_SIGNAL_SEND_ON_RECOG:
			if (digit != 'C') {
				PDEBUG(DEBUG_SS5, "%s: answer to clear-forward is gone before recognition\n", p_name);
				new_ss5_signal(SS5_SIGNAL_SEND_ON);
//				p_m_s_sample_nr = 0;
//				inband_send_on();
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			PDEBUG(DEBUG_SS5, "%s: answer to clear-forward recognized, turning off, waiting for recognition\n", p_name);
			new_ss5_signal(SS5_SIGNAL_SEND_OFF);
			break;
			case SS5_SIGNAL_SEND_OFF:
			if (digit == 'A') {
				PDEBUG(DEBUG_SS5, "%s: received seize right after clear-forward answer, continue with seize\n", p_name);
				new_state(PORT_STATE_IDLE);
				goto seize;
			}
			if (digit == 'C')
				break;
			PDEBUG(DEBUG_SS5, "%s: answer to clear-forward is complete\n", p_name);
			release_guard_ind();
			new_ss5_signal(SS5_SIGNAL_DELAY);
			p_m_s_recog = 0; /* use recog to delay */
			PDEBUG(DEBUG_SS5, "%s: answer to clear-forward on outgoing interface starting delay to prevent ping-pong\n", p_name);
			break;
			case SS5_SIGNAL_DELAY:
			if (digit == 'A') {
				PDEBUG(DEBUG_SS5, "%s: received seize right after clear-forward answer, continue with seize\n", p_name);
				new_state(PORT_STATE_IDLE);
				goto seize;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RELEASE_DELAY)
				break;
			PDEBUG(DEBUG_SS5, "%s: delay time over, going idle\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			new_state(PORT_STATE_IDLE);
			p_type = PORT_TYPE_SS5_IDLE;
			break;
		}
		break;
		/* answer to release-guard*/
		case SS5_STATE_RELEASE_GUARD:
		switch (p_m_s_signal) {
			case SS5_SIGNAL_RECEIVE_RECOG:
			if (digit != 'C') {
				if (p_type == PORT_TYPE_SS5_OUT)
					PDEBUG(DEBUG_SS5, "%s: incomming release-guard is gone before recognition\n", p_name);
				else
					PDEBUG(DEBUG_SS5, "%s: incomming clear forward is gone before recognition\n", p_name);
				new_ss5_state(SS5_STATE_IDLE);
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			if (p_type == PORT_TYPE_SS5_OUT)
				PDEBUG(DEBUG_SS5, "%s: incomming release-guard is recognized, responding...\n", p_name);
			else
				PDEBUG(DEBUG_SS5, "%s: incomming clear-forward is recognized, responding...\n", p_name);
			new_state(PORT_STATE_RELEASE);
			new_ss5_signal(SS5_SIGNAL_RECEIVE);
			p_m_s_sample_nr = 0;
			inband_send_on();
			break;
			case SS5_SIGNAL_RECEIVE:
			if (digit == 'C'
			 || p_m_s_sample_nr < 256) /* small hack to keep answer for at least some time */
			 break;
#if 0
			if (digit == 'A') {
				PDEBUG(DEBUG_SS5, "%s: received seize right after clear-forward is received\n", p_name);
				new_state(PORT_STATE_IDLE);
				goto seize;
			}
#endif
			/* if clear forward stops right after recognition on the incomming side,
			 * the release guard signal stops and may be too short to be recognized at the outgoing side.
			 * to prevent this, a timer can be started to force a release guard that is long
			 * enough to be recognized on the outgoing side.
			 * this will prevent braking via blueboxing (other tricks may still be possible).
			 */
			if ((p_m_mISDNport->ss5 & SS5_FEATURE_RELEASEGUARDTIMER)
			 && p_m_s_sample_nr < SS5_TIMER_RELEASE_GUARD)
				break;
			if (p_type == PORT_TYPE_SS5_OUT)
				PDEBUG(DEBUG_SS5, "%s: incomming release-guard is gone after responding\n", p_name);
			else
				PDEBUG(DEBUG_SS5, "%s: incomming clear-forward is gone after responding\n", p_name);
			if (p_type == PORT_TYPE_SS5_OUT) {
				release_guard_ind();
				new_ss5_signal(SS5_SIGNAL_DELAY);
				p_m_s_recog = 0; /* use recog to delay */
				PDEBUG(DEBUG_SS5, "%s: incomming release-guard on outgoing interface starting delay to prevent ping-pong\n", p_name);
			} else {
				clear_forward_ind();
				new_ss5_state(SS5_STATE_IDLE);
			}
			break;
			case SS5_SIGNAL_DELAY:
			if (digit == 'A') {
				PDEBUG(DEBUG_SS5, "%s: received seize right after release guard is gone, continue with seize\n", p_name);
				new_state(PORT_STATE_IDLE);
				goto seize;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RELEASE_DELAY)
				break;
			PDEBUG(DEBUG_SS5, "%s: delay time over, going idle\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			new_state(PORT_STATE_IDLE);
			p_type = PORT_TYPE_SS5_IDLE;
			break;
		}
		break;
		/* wait time to recognize forward transfer */
		case SS5_STATE_FORWARD_TRANSFER:
		if (p_m_s_signal == SS5_SIGNAL_RECEIVE_RECOG) {
			if (digit != 'B') {
				PDEBUG(DEBUG_SS5, "%s: incomming forward-transfer is gone before recognition\n", p_name);
				new_ss5_state(SS5_STATE_IDLE);
				break;
			}
			p_m_s_recog += SS5_DECODER_NPOINTS;
			if (p_m_s_recog < SS5_TIMER_RECOG_OTHER)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming forward-transfer is recognized, responding, if BELL feature was selected...\n", p_name);
			new_ss5_signal(SS5_SIGNAL_RECEIVE);
#if 0
			p_m_s_sample_nr = 0;
			inband_send_on();
#endif
			break;
		}
		if (digit != 'B') {
			PDEBUG(DEBUG_SS5, "%s: incomming forward-transfer is gone after recognition\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			forward_transfer_ind();
			break;
		}
		break;
		/* dialing is received */
		case SS5_STATE_DIAL_IN:
		if (strchr("1234567890*#abc", digit)) {
			if (p_m_s_signal != SS5_SIGNAL_DIGIT_PAUSE)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming digit '%c' is recognized\n", p_name, digit);
			new_ss5_signal(SS5_SIGNAL_DIGIT_ON);
			digit_ind(digit);
		} else {
			if (p_m_s_signal != SS5_SIGNAL_DIGIT_ON)
				break;
			PDEBUG(DEBUG_SS5, "%s: incomming digit is gone after recognition\n", p_name);
			new_ss5_signal(SS5_SIGNAL_DIGIT_PAUSE);
		}
		break;
		case SS5_STATE_DIAL_IN_PULSE:
		if (digit == 'B')
			pulse_ind(1);
		else
			pulse_ind(0);
		break;
	}

	/* something more to decode ? */
	if (count != len)
		goto again;
}


/*
 * signalling sender
 *
 * this function generates tones and assembles dial string with digits and pause
 * the result is sent to mISDN. it uses the ss5_encode() function.
 * except for dialing and forward-transfer, tones are continuous and will not change state.
 */
int Pss5::inband_send(unsigned char *buffer, int len)
{
	int count = 0; /* sample counter */
	int duration;
	char digit;
	int tocode, tosend;

	switch(p_m_s_state) {
		/* turn off transmitter in idle state */
		case SS5_STATE_IDLE:
		inband_send_off();
		break;

		case SS5_STATE_SEIZING:
		if (p_m_s_signal != SS5_SIGNAL_SEND_ON
		 && p_m_s_signal != SS5_SIGNAL_SEND_ON_RECOG)
		 	break;
		duration = -1; /* continuous */
		digit = 'A';
		send:
		/* how much samples do we have left */
		if (duration < 0)
			tocode = len;
		else
			tocode = duration - p_m_s_sample_nr;
		if (tocode > 0) {
			if (tocode > len)
				tocode = len;
			ss5_encode(buffer, tocode, digit, p_m_s_sample_nr);
			/* increase counters */
			p_m_s_sample_nr += tocode;
			count += tocode;
		}
		/* more to come ? */
		if (duration > 0 && p_m_s_sample_nr >= duration) {
			PDEBUG(DEBUG_SS5, "%s: sending tone '%c' complete, starting delay\n", p_name, digit);
			if (p_m_s_state == SS5_STATE_DOUBLE_SEIZE) {
				do_release(CAUSE_NOCHANNEL, LOCATION_PRIVATE_LOCAL);
				break;
			}
			new_ss5_state(SS5_STATE_DELAY);
			p_m_s_sample_nr = 0;
		}
#if 0
		/* stop sending if too long */
		if (duration < 0 && p_m_s_sample_nr >= SS5_TIMER_MAX_SIGNAL) {
			PDEBUG(DEBUG_SS5, "%s: sending tone '%c' too long, stopping\n", p_name, digit);
			inband_send_off();
			break;
		}
#endif
		break;
		/* incomming seizing */
		case SS5_STATE_PROCEED_TO_SEND:
		if (p_m_s_signal != SS5_SIGNAL_RECEIVE)
		 	break;
		duration = -1; /* continuous */
		digit = 'B';
		goto send;

		case SS5_STATE_BUSY_FLASH:
		case SS5_STATE_CLEAR_BACK:
		if (p_m_s_signal != SS5_SIGNAL_SEND_ON
		 && p_m_s_signal != SS5_SIGNAL_SEND_ON_RECOG)
		 	break;
		duration = -1; /* continuous */
		digit = 'B';
		goto send;

		case SS5_STATE_ANSWER:
		if (p_m_s_signal != SS5_SIGNAL_SEND_ON
		 && p_m_s_signal != SS5_SIGNAL_SEND_ON_RECOG)
		 	break;
		duration = -1; /* continuous */
		digit = 'A';
		goto send;

		case SS5_STATE_ACK_BUSY_FLASH:
		case SS5_STATE_ACK_ANSWER:
		case SS5_STATE_ACK_CLEAR_BACK:
		if (p_m_s_signal != SS5_SIGNAL_RECEIVE)
		 	break;
		duration = -1; /* continuous */
		digit = 'A';
		goto send;

#if 0
		case SS5_STATE_FORWARD_TRANSFER:
		if (p_m_s_signal != SS5_SIGNAL_RECEIVE)
		 	break;
		/* only on bell systems continue and acknowledge tone */
		if (!(p_m_mISDNport->ss5 & SS5_FEATURE_BELL))
			break;
		duration = SS5_TIMER_FORWARD;
		digit = 'B';
		goto send;
#endif

		case SS5_STATE_CLEAR_FORWARD:
		if (p_m_s_signal != SS5_SIGNAL_SEND_ON
		 && p_m_s_signal != SS5_SIGNAL_SEND_ON_RECOG)
		 	break;
		duration = -1; /* continuous */
		digit = 'C';
		goto send;

		case SS5_STATE_RELEASE_GUARD:
		if (p_m_s_signal != SS5_SIGNAL_RECEIVE
		 && p_m_s_signal != SS5_SIGNAL_DELAY)
		 	break;
		/* prevent from sending release guard too long */
		if (p_m_s_sample_nr >= SS5_TIMER_RELEASE_MAX)
			break;
		duration = -1; /* continuous */
		digit = 'C';
		goto send;

		case SS5_STATE_DIAL_OUT:
		if ((p_m_mISDNport->ss5 & SS5_FEATURE_PULSEDIALING))
			count = inband_dial_pulse(buffer, len, count);
		else
			count = inband_dial_mf(buffer, len, count);
		break;
		break;

		case SS5_STATE_DELAY:
		tosend = len - count;
		memset(buffer+count, audio_s16_to_law[0], tosend);
		p_m_s_sample_nr += tosend;
		count += tosend;
		if (p_m_s_sample_nr >= SS5_TIMER_AFTER_SIGNAL) {
			PDEBUG(DEBUG_SS5, "%s: delay done, ready for next signal\n", p_name);
			new_ss5_state(SS5_STATE_IDLE);
			inband_send_off();
		}
		break;

		case SS5_STATE_DOUBLE_SEIZE:
		duration = SS5_TIMER_DOUBLE_SEIZE;
		digit = 'A';
		goto send;

		/* nothing to send */
		default:
		PERROR("inband signalling is turned on, but no signal is processed here.");
		new_ss5_state(SS5_STATE_IDLE);
		inband_send_off();
		return 0;
	}

	/* return (partly) filled buffer */
	return count;
}


int Pss5::inband_dial_mf(unsigned char *buffer, int len, int count)
{
	int duration;
	int tocode, tosend;
	char digit;

	/* dialing
	 *
	 * p_m_s_dial: digits to be dialed
	 * p_m_s_digit_i: current digit counter
	 * p_m_s_signal: current signal state
	 * p_m_s_sample_nr: current sample number
	 */
	again:
	/* get digit and duration */
	digit = p_m_s_dial[p_m_s_digit_i];
	if (!digit) { /* if end of string reached */
		new_ss5_state(SS5_STATE_DELAY);
		p_m_s_sample_nr = 0;
		return count;
	}
	if (p_m_s_signal == SS5_SIGNAL_DIGIT_ON) {
		if (!p_m_s_digit_i) // first digit
			duration = SS5_TIMER_KP;
		else
			duration = SS5_TIMER_DIGIT;
	} else {
		duration = SS5_TIMER_PAUSE;
	}
	/* end of digit/pause ? */
	if (p_m_s_sample_nr >= duration) {
		p_m_s_sample_nr = 0;
		if (p_m_s_signal == SS5_SIGNAL_DIGIT_PAUSE)
			new_ss5_signal(SS5_SIGNAL_DIGIT_ON);
		else {
			new_ss5_signal(SS5_SIGNAL_DIGIT_PAUSE);
			p_m_s_digit_i++;
			goto again;
		}
	}
	/* how much samples do we have left */
	tosend = len - count;
	tocode = duration - p_m_s_sample_nr;
	if (tocode < 0)
		FATAL("sample_nr overrun duration");
	if (tosend < tocode)
		tocode = tosend;
	/* digit or pause */
	if (p_m_s_signal == SS5_SIGNAL_DIGIT_PAUSE) {
		memset(buffer+count, audio_s16_to_law[0], tocode);
//		printf("coding pause %d bytes\n", tocode);
	} else {
		ss5_encode(buffer+count, tocode, digit, p_m_s_sample_nr);
//		printf("coding digit '%c' %d bytes\n", digit, tocode);
	}
	/* increase counters */
	p_m_s_sample_nr += tocode;
	count += tocode;
	/* can we take more ? */
	if (len != count)
		goto again;
	return count;
}


int Pss5::inband_dial_pulse(unsigned char *buffer, int len, int count)
{
	int tocode, tosend;
	int duration;
	char digit;

	/* dialing
	 *
	 * p_m_s_dial: digits to be dialed
	 * p_m_s_digit_i: current digit counter
	 * p_m_s_signal: current signal state
	 * p_m_s_sample_nr: current sample number
	 */
	again:
	/* get digit */
	digit = p_m_s_dial[p_m_s_digit_i];
	if (!digit) { /* if end of string reached */
		new_ss5_state(SS5_STATE_DELAY);
		p_m_s_sample_nr = 0;
		return count;
	}
	/* convert digit to pulse */
	switch (digit) {
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		digit -= '0';
		break;
		case '0':
		digit = 10;
		break;
		case '*':
		digit = 11;
		break;
		case '#':
		digit = 12;
		break;
		default:
		p_m_s_digit_i++;
		goto again;
	}
	/* get duration */
	if (p_m_s_signal == SS5_SIGNAL_DIGIT_ON) {
		if (p_m_s_pulsecount & 1)
			duration = BELL_TIMER_MAKE; /* loop closed */
		else
			duration = BELL_TIMER_BREAK; /* loop open, tone */
	} else {
		duration = BELL_TIMER_PAUSE;
	}
	/* end of digit/pause ? */
	if (p_m_s_sample_nr >= duration) {
		p_m_s_sample_nr = 0;
		if (p_m_s_signal == SS5_SIGNAL_DIGIT_PAUSE) {
			new_ss5_signal(SS5_SIGNAL_DIGIT_ON);
			PDEBUG(DEBUG_SS5, "%s: starting pusling digit '%c'\n", p_name, digit);
		} else {
			p_m_s_pulsecount++; /* toggle pulse */
			if (!(p_m_s_pulsecount & 1)) {
				/* pulse now on again, but if end is reached... */
				if (p_m_s_pulsecount == (digit<<1)) {
					new_ss5_signal(SS5_SIGNAL_DIGIT_PAUSE);
					p_m_s_pulsecount = 0;
					p_m_s_digit_i++;
					goto again;
				}
			}
		}
	}
	/* how much samples do we have left */
	tosend = len - count;
	tocode = duration - p_m_s_sample_nr;
	if (tocode < 0)
		FATAL("sample_nr overrun duration");
	if (tosend < tocode)
		tocode = tosend;
	/* digit or pause */
	if (p_m_s_signal == SS5_SIGNAL_DIGIT_PAUSE
	 || (p_m_s_pulsecount&1)) /* ...or currently on and no pulse */
		memset(buffer+count, audio_s16_to_law[0], tocode);
	else
		ss5_encode(buffer+count, tocode, 'B', p_m_s_sample_nr);
	/* increase counters */
	p_m_s_sample_nr += tocode;
	count += tocode;
	/* can we take more ? */
	if (len != count)
		goto again;
	return count;
}


/*
 * start signal
 */ 
void Pss5::start_signal(int state)
{
	PDEBUG(DEBUG_SS5, "%s: starting singal '%s'\n", p_name, ss5_state_name[state]);
	/* start signal */
	new_ss5_state(state);
	if (state == SS5_STATE_DIAL_OUT) {
		p_m_s_digit_i = 0;
		p_m_s_pulsecount = 0;
		new_ss5_signal(SS5_SIGNAL_DIGIT_ON);
	} else
		new_ss5_signal(SS5_SIGNAL_SEND_ON);

	/* double seize must continue the current seize tone, so don't reset sample_nr */
	if (state != SS5_STATE_DOUBLE_SEIZE) {
		/* (re)set sound phase to 0 */
		p_m_s_sample_nr = 0;
	}

	/* turn on inband transmitter */
	inband_send_on();
}


/*
 * handles all indications
 */
void Pss5::seizing_ind(void)
{
	ss5_trace_header(p_m_mISDNport, this, SS5_SEIZING_IND, p_m_b_channel);
	end_trace();

	new_state(PORT_STATE_IN_SETUP);
	set_tone("", "noise");
}

void Pss5::digit_ind(char digit)
{
	int i;
	char string[128] = "", dial[128] = "";
	int dash, first_digit, last_was_digit;

	/* add digit */
	SCCAT(p_m_s_dial, digit);

	if (p_state == PORT_STATE_IN_SETUP)
		new_state(PORT_STATE_IN_OVERLAP);

	/* not last digit ? */
	if (digit != 'c')
		return;

	/* parse string */
	dash = 0; /* dash must be used next time */
	first_digit = 1;
	last_was_digit = 0;
	p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
	for (i = 0; p_m_s_dial[i]; i++) {
		if (dash || (last_was_digit && (p_m_s_dial[i]<'0' || p_m_s_dial[i]>'9')))
			SCCAT(string, '-');
		dash = 0;
		last_was_digit = 0;
		switch(p_m_s_dial[i]) {
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '0':
			if (first_digit)
				dash = 1;
			first_digit = 0;
			last_was_digit = 1;
			SCCAT(string, p_m_s_dial[i]);
			SCCAT(dial, p_m_s_dial[i]);
			break;
			case '*':
			SCAT(string, "C11");
			SCCAT(dial, p_m_s_dial[i]);
			dash = 1;
			break;
			case '#':
			SCAT(string, "C12");
			SCCAT(dial, p_m_s_dial[i]);
			dash = 1;
			break;
			case 'a':
			SCAT(string, "KP1");
			SCCAT(dial, p_m_s_dial[i]);
			dash = 1;
			break;
			case 'b':
			SCAT(string, "KP2");
			SCCAT(dial, p_m_s_dial[i]);
			dash = 1;
			break;
			case 'c':
			SCAT(string, "ST");
			dash = 1;
			break;
			default:
			break;
		}
	}
	ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_IND, p_m_b_channel);
	add_trace("string", NULL, "%s", string);
	add_trace("number", NULL, "%s", dial);
	end_trace();
	new_ss5_state(SS5_STATE_IDLE);

	do_setup(dial, 1);
	new_state(PORT_STATE_IN_PROCEEDING);
}

void Pss5::pulse_ind(int on)
{
	struct lcr_msg *message;
	char dial[3] = "a.";

	if (p_m_s_signal == SS5_SIGNAL_PULSE_OFF) {
		if (on) {
			/* pulse turns on */
			p_m_s_recog = 0;
			new_ss5_signal(SS5_SIGNAL_PULSE_ON);
			/* pulse turns of, count it */
			p_m_s_pulsecount++;
			PDEBUG(DEBUG_SS5, "%s: pulse turns on, counting\n", p_name);
		} else {
			/* pulse remains off */
			p_m_s_recog += SS5_DECODER_NPOINTS;
			/* not recognized end of digit, we wait... */
			if (p_m_s_recog < BELL_TIMER_RECOG_END)
				return;
			PDEBUG(DEBUG_SS5, "%s: pulse remains off, counted %d pulses\n", p_name, p_m_s_pulsecount);
			if (p_m_s_pulsecount >= 12)
				dial[1] = '#';
			else if (p_m_s_pulsecount == 11)
				dial[1] = '*';
			else if (p_m_s_pulsecount == 10)
				dial[1] = '0';
			else
				dial[1] = p_m_s_pulsecount + '0';
			ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_IND, p_m_b_channel);
			add_trace("digit", NULL, "%s", dial+1);
			add_trace("pulses", NULL, "%d", p_m_s_pulsecount);
			end_trace();
			/* special star release feature */
			if ((p_m_mISDNport->ss5 & SS5_FEATURE_STAR_RELEASE) && dial[1] == '*') {
				ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_IND, p_m_b_channel);
				add_trace("star", NULL, "releases call");
				end_trace();
				goto star_release;
			}
			if (p_state == PORT_STATE_IN_SETUP) {
				/* sending digit as setup */
				do_setup(dial, 0); /* include 'a' == KP1 */
				new_state(PORT_STATE_IN_OVERLAP);
			} else {
				/* sending digit as information */
				message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_INFORMATION);
				SCPY(message->param.information.id, dial+1);
				message_put(message);
			}
				new_ss5_state(SS5_STATE_IDLE);
			/* done rx pulses, return to idle */
			new_ss5_state(SS5_STATE_IDLE);
		}
	} else {
		if (on) {
			/* pulse remains on */
			p_m_s_recog += SS5_DECODER_NPOINTS;
		} else {
			/* pulse turns off */
			if (p_m_s_recog >= BELL_TIMER_RECOG_HANGUP) {
				PDEBUG(DEBUG_SS5, "%s: long pulse turns off, releasing\n", p_name);
				ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_IND, p_m_b_channel);
				add_trace("longtone", NULL, "releases call");
				end_trace();
				star_release:
				/* long pulse is gone, release current connection, if any */
				while(p_epointlist) {
					message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
					message->param.disconnectinfo.location = LOCATION_BEYOND;
					message->param.disconnectinfo.cause = CAUSE_NORMAL;
					message_put(message);
					free_epointlist(p_epointlist);
				}
				set_tone("", NULL);
				/* return to setup state */
				new_state(PORT_STATE_IN_SETUP);
				new_ss5_state(SS5_STATE_IDLE);
				return;
			}
			PDEBUG(DEBUG_SS5, "%s: short pulse turns off, releasing\n", p_name);
			p_m_s_recog = 0;
			new_ss5_signal(SS5_SIGNAL_PULSE_OFF);
		}
	}
}

void Pss5::proceed_to_send_ind(void)
{
	ss5_trace_header(p_m_mISDNport, this, SS5_PROCEED_TO_SEND_IND, p_m_b_channel);
	end_trace();

	SCPY(p_m_s_dial, p_dialinginfo.id);
	start_signal(SS5_STATE_DIAL_OUT);

	new_state(PORT_STATE_OUT_OVERLAP);
}

void Pss5::busy_flash_ind(void)
{
	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_BUSY_FLASH_IND, p_m_b_channel);
	end_trace();

	/* busy before dialing ? */
	if (!p_epointlist)
		return;

	if (!(p_m_mISDNport->ss5 & SS5_FEATURE_NODISCONNECT)) {
		message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
		message->param.disconnectinfo.location = LOCATION_BEYOND;
		message->param.disconnectinfo.cause = CAUSE_BUSY;
		message_put(message);
	}

	new_state(PORT_STATE_IN_DISCONNECT);
}

void Pss5::answer_ind(void)
{
	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_ANSWER_IND, p_m_b_channel);
	end_trace();

	/* answer before dialing ? */
	if (!p_epointlist)
		return;

	/* already connected */
	if (!(p_m_mISDNport->ss5 & SS5_FEATURE_CONNECT)) {
		message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
		message_put(message);
	}

	new_state(PORT_STATE_CONNECT);
}

void Pss5::forward_transfer_ind(void)
{
//	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_FORWARD_TRANSFER_IND, p_m_b_channel);
	end_trace();

#if 0
	/* if BELL flavor bluebox flag is set, use it to seize a new line */
	if (!(p_m_mISDNport->ss5 & SS5_FEATURE_BELL))
		return;

	/* special BELL flavor hack to clear a line and seize a new one */
	while(p_epointlist) {
		message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
		message->param.disconnectinfo.location = LOCATION_BEYOND;
		message->param.disconnectinfo.cause = CAUSE_NORMAL;
		message_put(message);
		free_epointlist(p_epointlist);
	}
	set_tone("", NULL);
	new_state(PORT_STATE_IN_SETUP);
#endif
}

void Pss5::clear_back_ind(void)
{
	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_CLEAR_BACK_IND, p_m_b_channel);
	end_trace();

	/* nobody? */
	if (!p_epointlist)
		return;

	if (!(p_m_mISDNport->ss5 & SS5_FEATURE_NODISCONNECT)) {
		message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_DISCONNECT);
		message->param.disconnectinfo.location = LOCATION_BEYOND;
		message->param.disconnectinfo.cause = CAUSE_NORMAL;
		message_put(message);
	}

	new_state(PORT_STATE_IN_DISCONNECT);
}

void Pss5::clear_forward_ind(void)
{
	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_CLEAR_FORWARD_IND, p_m_b_channel);
	end_trace();

	new_state(PORT_STATE_IDLE);
	set_tone("", NULL);
	p_type = PORT_TYPE_SS5_IDLE;

	/* someone ? */
	if (!p_epointlist)
		return;

	message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
	message->param.disconnectinfo.location = LOCATION_BEYOND;
	message->param.disconnectinfo.cause = CAUSE_NORMAL;
	message_put(message);
	free_epointlist(p_epointlist);

}

void Pss5::release_guard_ind(void)
{
	struct lcr_msg *message;

	ss5_trace_header(p_m_mISDNport, this, SS5_RELEASE_GUARD_IND, p_m_b_channel);
	end_trace();

	set_tone("", NULL);

	/* someone ? */
	if (!p_epointlist)
		return;

	message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
	message->param.disconnectinfo.location = LOCATION_BEYOND;
	message->param.disconnectinfo.cause = CAUSE_NORMAL;
	message_put(message);
	free_epointlist(p_epointlist);
}

void Pss5::double_seizure_ind(void)
{
	ss5_trace_header(p_m_mISDNport, this, SS5_DOUBLE_SEIZURE_IND, p_m_b_channel);
	end_trace();
	ss5_trace_header(p_m_mISDNport, this, SS5_DOUBLE_SEIZURE_REQ, p_m_b_channel);
	end_trace();

	/* start double seizure sequence, so remote exchange will recognize it */
	start_signal(SS5_STATE_DOUBLE_SEIZE);
}


/*
 * shuts down by sending a clear forward and releasing endpoint
 */
void Pss5::do_release(int cause, int location)
{
	struct lcr_msg *message;

	/* sending release to endpoint */
	while(p_epointlist) {
		message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
		message->param.disconnectinfo.location = location;
		message->param.disconnectinfo.cause = cause;
		message_put(message);
		free_epointlist(p_epointlist);
	}

	/* start clear-forward */
	ss5_trace_header(p_m_mISDNport, this, SS5_CLEAR_FORWARD_REQ, p_m_b_channel);
	end_trace();
	start_signal(SS5_STATE_CLEAR_FORWARD);

	new_state(PORT_STATE_RELEASE);
}


/*
 * create endpoint and send setup
 */
void Pss5::do_setup(char *dial, int complete)
{
	class Endpoint *epoint;
	struct lcr_msg *message;

	SCPY(p_dialinginfo.id, dial);
	p_dialinginfo.sending_complete = complete;
	p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
	p_callerinfo.screen = INFO_SCREEN_NETWORK;
	p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
	p_callerinfo.isdn_port = p_m_portnum;
	SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);

	p_capainfo.bearer_capa = INFO_BC_AUDIO;
	p_capainfo.bearer_info1 = (options.law=='a')?3:2;
	p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
	p_capainfo.hlc = INFO_HLC_NONE;
	p_capainfo.exthlc = INFO_HLC_NONE;
	p_capainfo.source_mode = B_MODE_TRANSPARENT;

	/* create endpoint */
	if (p_epointlist)
		FATAL("Incoming call but already got an endpoint.\n");
	if (!(epoint = new Endpoint(p_serial, 0)))
		FATAL("No memory for Endpoint instance\n");
	if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
		FATAL("No memory for Endpoint Application instance\n");
	epointlist_new(epoint->ep_serial);

	/* send setup message to endpoit */
	message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
	message->param.setup.isdn_port = p_m_portnum;
	message->param.setup.port_type = p_type;
//	message->param.setup.dtmf = !p_m_mISDNport->ifport->nodtmf;
	memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
	memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
	memcpy(&message->param.setup.redirinfo, &p_redirinfo, sizeof(struct redir_info));
	memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
	message_put(message);

}



/*
 * handles all messages from endpoint
 */

/* MESSAGE_SETUP */
void Pss5::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
{
	struct lcr_msg *message;
	int i;
	char string[128] = "", dial[128] = "";
	int dash, first_digit, last_was_digit;

	if (p_epointlist) {
		PERROR("endpoint already exist.\n");
		message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
		message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
		message->param.disconnectinfo.cause = CAUSE_UNSPECIFIED;
		message_put(message);
		return;
	}

	/* copy setup infos to port */
	memcpy(&p_callerinfo, &param->setup.callerinfo, sizeof(p_callerinfo));
	memcpy(&p_dialinginfo, &param->setup.dialinginfo, sizeof(p_dialinginfo));
	memcpy(&p_capainfo, &param->setup.capainfo, sizeof(p_capainfo));
	memcpy(&p_redirinfo, &param->setup.redirinfo, sizeof(p_redirinfo));
	/* screen outgoing caller id */
	do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, p_m_mISDNport->ifport->interface);
	do_screen(1, p_callerinfo.id2, sizeof(p_callerinfo.id2), &p_callerinfo.ntype2, &p_callerinfo.present2, p_m_mISDNport->ifport->interface);

	/* parse dial string  */
	dash = 0; /* dash must be used next time */
	first_digit = 1;
	last_was_digit = 0;
	for (i = 0; p_dialinginfo.id[i]; i++) {
		if (dash || (last_was_digit && (p_dialinginfo.id[i]<'0' || p_dialinginfo.id[i]>'9')))
			SCCAT(string, '-');
		dash = 0;
		last_was_digit = 0;
		switch(p_dialinginfo.id[i]) {
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '0':
			if (i && first_digit)
				dash = 1;
			first_digit = 0;
			last_was_digit = 1;
			SCCAT(string, p_dialinginfo.id[i]);
			SCCAT(dial, p_dialinginfo.id[i]);
			break;
			case '*':
			SCAT(string, "C11");
			SCCAT(dial, '*');
			dash = 1;
			break;
			case '#':
			SCAT(string, "C12");
			SCCAT(dial, '#');
			dash = 1;
			break;
			case 'a':
			SCAT(string, "KP1");
			SCCAT(dial, 'a');
			dash = 1;
			break;
			case 'b':
			SCAT(string, "KP2");
			SCCAT(dial, 'b');
			dash = 1;
			break;
			case 'c':
			SCAT(string, "ST");
			SCCAT(dial, 'c');
			dash = 1;
			case 'K':
			i++;
			if (p_dialinginfo.id[i] != 'P')
				goto dial_error;
			i++;
			if (p_dialinginfo.id[i] == '1') {
				SCAT(string, "KP1");
				SCCAT(dial, 'a');
				dash = 1;
				break;
			}
			if (p_dialinginfo.id[i] == '2') {
				SCAT(string, "KP2");
				SCCAT(dial, 'b');
				dash = 1;
				break;
			}
			goto dial_error;
			case 'C':
			i++;
			if (p_dialinginfo.id[i] != '1')
				goto dial_error;
			i++;
			if (p_dialinginfo.id[i] == '1') {
				SCAT(string, "C11");
				SCCAT(dial, 'a');
				dash = 1;
				break;
			}
			if (p_dialinginfo.id[i] == '2') {
				SCAT(string, "C12");
				SCCAT(dial, 'b');
				dash = 1;
				break;
			}
			goto dial_error;
			case 'S':
			i++;
			if (p_dialinginfo.id[i] != 'T')
				goto dial_error;
			SCAT(string, "ST");
			SCCAT(dial, 'c');
			dash = 1;
			break;
			default:
			break;
		}
		/* stop, if ST */
		if (dial[0] && dial[strlen(dial)-1] == 'c')
			break;
	}
	/* terminate */
	if (dial[0] && dial[strlen(dial)-1]!='c') {
		SCCAT(string, '-');
		SCAT(string, "ST");
		SCCAT(dial, 'c');
	}

	/* error in dial string */
	if (!dial[0]) {
		dial_error:
		ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_REQ, p_m_b_channel);
		add_trace("string", NULL, "%s", p_dialinginfo.id);
		if (!dial[0])
			add_trace("error", NULL, "no number", dial);
		else if (dial[0]!='a' && dial[0]!='b')
			add_trace("error", NULL, "number must start with KP1/KP2", dial);
		else
			add_trace("error", NULL, "illegal format", dial);
		end_trace();
		message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
		message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
		message->param.disconnectinfo.cause = CAUSE_INVALID;
		message_put(message);
		return;
	}

	/* copy new dial string */
	SCPY(p_dialinginfo.id, dial);

	/* attach only if not already */
	epointlist_new(epoint_id);

	ss5_trace_header(p_m_mISDNport, this, SS5_DIALING_REQ, p_m_b_channel);
	add_trace("string", NULL, "%s", string);
	add_trace("type", NULL, "%s", (dial[0]=='b')?"international":"national");
	add_trace("number", NULL, "%s", dial);
	end_trace();
	/* connect auto path */
	if ((p_m_mISDNport->ss5 & SS5_FEATURE_CONNECT)) {
		message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
		message_put(message);
	} else {
		message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
		message_put(message);
	}

	/* start seizing */
	ss5_trace_header(p_m_mISDNport, this, SS5_SEIZING_REQ, p_m_b_channel);
	end_trace();
	new_ss5_state(SS5_STATE_SEIZING);
	new_ss5_signal(SS5_SIGNAL_SEND_ON);
	p_m_s_sample_nr = 0;
	inband_send_on();

	p_type = PORT_TYPE_SS5_OUT;
	new_state(PORT_STATE_OUT_SETUP);
}

/* MESSAGE_CONNECT */
void Pss5::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
{
	memcpy(&p_connectinfo, &param->connectinfo, sizeof(p_connectinfo));

	if (p_state != PORT_STATE_CONNECT) {
		new_state(PORT_STATE_CONNECT);
		p_m_s_answer = 1;
		trigger_work(&p_m_s_queue);
	}

	set_tone("", NULL);
}

/* MESSAGE_DISCONNECT */
void Pss5::message_disconnect(unsigned int epoint_id, int message_id, union parameter *param)
{
	/* disconnect and clear forward (release guard) */
//	if ((p_type==PORT_TYPE_SS5_IN && !p_m_mISDNport->tones) /* incomming exchange with no tones */
if (0	 || p_type==PORT_TYPE_SS5_OUT) { /* outgoing exchange */
		do_release(param->disconnectinfo.cause, param->disconnectinfo.location);
		return;
	}

	ss5_trace_header(p_m_mISDNport, this, SS5_CLEAR_BACK_REQ, p_m_b_channel);
	end_trace();
	start_signal(SS5_STATE_CLEAR_BACK);

	new_state(PORT_STATE_OUT_DISCONNECT);
}

/* MESSAGE_RELEASE */
void Pss5::message_release(unsigned int epoint_id, int message_id, union parameter *param)
{
	do_release(param->disconnectinfo.cause, param->disconnectinfo.location);
}

void Pss5::register_timeout(void)
{
	do_release(CAUSE_UNSPECIFIED, LOCATION_PRIVATE_LOCAL);
}

/*
 * endpoint sends messages to the port
 */
int Pss5::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
{
	if (PmISDN::message_epoint(epoint_id, message_id, param))
		return(1);

	switch(message_id) {
		case MESSAGE_SETUP: /* dial-out command received from epoint */
		if (p_state!=PORT_STATE_IDLE) {
			PERROR("Pss5(%s) ignoring setup because isdn port is not in idle state (or connected for sending display info).\n", p_name);
			break;
		}
		if (p_epointlist && p_state==PORT_STATE_IDLE)
			FATAL("Pss5(%s): epoint pointer is set in idle state, how bad!!\n", p_name);
		message_setup(epoint_id, message_id, param);
		break;

		case MESSAGE_CONNECT: /* call of endpoint is connected */
		message_connect(epoint_id, message_id, param);
		break;

		case MESSAGE_DISCONNECT: /* call has been disconnected */
		message_disconnect(epoint_id, message_id, param);
		break;

		case MESSAGE_RELEASE: /* release isdn port */
		message_release(epoint_id, message_id, param);
		break;

		default:
		PDEBUG(DEBUG_SS5, "Pss5(%s) ss5 port with (caller id %s) received an unhandled message: %d\n", p_name, p_callerinfo.id, message_id);
	}

	return(1);
}