-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathss5.cpp
1984 lines (1822 loc) · 57.4 KB
/
ss5.cpp
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
/*****************************************************************************\
** **
** 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, mISDNport->ifport->interface, 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, struct interface *interface, int channel, int exclusive, int mode) : PmISDN(type, mISDNport, portname, settings, interface, 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 ? */