-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmISDN.cpp
2384 lines (2097 loc) · 68.2 KB
/
mISDN.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
/*****************************************************************************\
** **
** Linux Call Router **
** **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg **
** **
** mISDN port abstraction for dss1 **
** **
\*****************************************************************************/
#include "main.h"
#include "myisdn.h"
#include <mISDN/q931.h>
#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#endif
// timeouts if activating/deactivating response from mISDN got lost
#define B_TIMER_ACTIVATING 1 // seconds
#define B_TIMER_DEACTIVATING 1 // seconds
/* list of mISDN ports */
struct mISDNport *mISDNport_first;
/* noise randomizer */
unsigned char mISDN_rand[256];
int mISDN_rand_count = 0;
#ifdef OLD_MT_ASSIGN
unsigned int mt_assign_pid = ~0;
#endif
int mISDNsocket = -1;
static int upqueue_pipe[2];
static struct lcr_fd upqueue_fd;
int upqueue_avail = 0;
static int mISDN_upqueue(struct lcr_fd *fd, unsigned int what, void *instance, int i);
static int mISDN_timeout(struct lcr_timer *timer, void *instance, int i);
static int my_mISDNlib_debug(const char *file, int line, const char *func, int level, const char *fmt, va_list va)
{
int ret = 0;
if (debug_fp > 0)
ret = vfprintf(debug_fp, fmt, va);
return ret;
}
static struct mi_ext_fn_s myfn;
int mISDN_initialize(void)
{
char filename[256];
int ver;
/* try to open raw socket to check kernel */
mISDNsocket = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
if (mISDNsocket < 0) {
fprintf(stderr, "Cannot open mISDN due to '%s'. (Does your Kernel support socket based mISDN? Protocol family is %d.)\n", strerror(errno), PF_ISDN);
return(-1);
}
/* init mlayer3 */
// set debug printout function
myfn.prt_debug = my_mISDNlib_debug;
ver = init_layer3(4, &myfn); // buffer of 4
/* open debug, if enabled and not only stack debugging */
if (options.deb) {
SPRINT(filename, "%s/debug.log", LOG_DIR);
debug_fp = fopen(filename, "a");
}
if (options.deb & DEBUG_STACK)
mISDN_set_debug_level(0xfffffeff);
else
mISDN_set_debug_level(0);
if (pipe(upqueue_pipe) < 0)
FATAL("Failed to open pipe\n");
memset(&upqueue_fd, 0, sizeof(upqueue_fd));
upqueue_fd.fd = upqueue_pipe[0];
register_fd(&upqueue_fd, LCR_FD_READ, mISDN_upqueue, NULL, 0);
return(0);
}
void mISDN_deinitialize(void)
{
cleanup_layer3();
if (debug_fp)
fclose(debug_fp);
debug_fp = NULL;
if (mISDNsocket > -1)
close(mISDNsocket);
if (upqueue_fd.inuse) {
unregister_fd(&upqueue_fd);
close(upqueue_pipe[0]);
close(upqueue_pipe[1]);
}
upqueue_avail = 0;
}
static int load_timer(struct lcr_timer *timer, void *instance, int index);
/*
* constructor
*/
PmISDN::PmISDN(int type, mISDNport *mISDNport, char *portname, struct port_settings *settings, struct interface *interface, int channel, int exclusive, int mode) : Port(type, portname, settings, interface)
{
p_m_mISDNport = mISDNport;
p_m_portnum = mISDNport->portnum;
p_m_b_index = -1;
p_m_b_channel = 0;
p_m_b_exclusive = 0;
p_m_b_reserve = 0;
p_m_b_mode = mode;
p_m_hold = 0;
p_m_tx_gain = mISDNport->ifport->interface->tx_gain;
p_m_rx_gain = mISDNport->ifport->interface->rx_gain;
p_m_conf = 0;
p_m_mute = 0;
p_m_txdata = 0;
p_m_delay = 0;
p_m_tx_dejitter = 0;
p_m_preload = ISDN_LOAD;
p_m_disable_dejitter = 0;
p_m_echo = 0;
p_m_tone = 0;
p_m_rxoff = 0;
p_m_inband_send_on = 0;
p_m_inband_receive_on = 0;
p_m_dtmf = !mISDNport->ifport->nodtmf;
p_m_dtmf_threshold = mISDNport->ifport->dtmf_threshold;
memset(&p_m_timeout, 0, sizeof(p_m_timeout));
add_timer(&p_m_timeout, mISDN_timeout, this, 0);
SCPY(p_m_pipeline, mISDNport->ifport->interface->pipeline);
/* audio */
memset(&p_m_loadtimer, 0, sizeof(p_m_loadtimer));
add_timer(&p_m_loadtimer, load_timer, this, 0);
p_m_load = 0;
p_m_last_tv_sec = 0;
/* crypt */
p_m_crypt = 0;
p_m_crypt_listen = 0;
p_m_crypt_msg_loops = 0;
p_m_crypt_msg_loops = 0;
p_m_crypt_msg_len = 0;
p_m_crypt_msg[0] = '\0';
p_m_crypt_msg_current = 0;
p_m_crypt_key_len = 0;
p_m_crypt_listen = 0;
p_m_crypt_listen_state = 0;
p_m_crypt_listen_len = 0;
p_m_crypt_listen_msg[0] = '\0';
p_m_crypt_listen_crc = 0;
if (mISDNport->ifport->interface->bf_len >= 4 && mISDNport->ifport->interface->bf_len <= 56) {
memcpy(p_m_crypt_key, mISDNport->ifport->interface->bf_key, p_m_crypt_key_len);
p_m_crypt_key_len = mISDNport->ifport->interface->bf_len;
p_m_crypt = 1;
}
/* if any channel requested by constructor */
if (channel == CHANNEL_ANY) {
/* reserve channel */
p_m_b_reserve = 1;
mISDNport->b_reserved++;
}
/* reserve channel */
if (channel > 0) // only if constructor was called with a channel resevation
seize_bchannel(channel, exclusive);
/* we increase the number of objects: */
mISDNport->use++;
PDEBUG(DEBUG_ISDN, "Created new mISDNPort(%s). Currently %d objects use, port #%d\n", portname, mISDNport->use, p_m_portnum);
//inband_receive_on();
}
/*
* destructor
*/
PmISDN::~PmISDN()
{
struct lcr_msg *message;
del_timer(&p_m_timeout);
del_timer(&p_m_loadtimer);
/* remove bchannel relation */
drop_bchannel();
/* release epoint */
while (p_epointlist) {
PDEBUG(DEBUG_ISDN, "destroy mISDNPort(%s). endpoint still exists, releaseing.\n", p_name);
message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
message->param.disconnectinfo.cause = 16;
message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
message_put(message);
/* remove from list */
free_epointlist(p_epointlist);
}
/* we decrease the number of objects: */
p_m_mISDNport->use--;
PDEBUG(DEBUG_ISDN, "destroyed mISDNPort(%s). Currently %d objects\n", p_name, p_m_mISDNport->use);
}
/*
* trace
*/
void chan_trace_header(struct mISDNport *mISDNport, class PmISDN *port, const char *msgtext, int direction)
{
/* 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,
direction,
CATEGORY_CH,
port?port->p_serial:0,
msgtext);
}
/*
* layer trace header
*/
static struct isdn_message {
const char *name;
unsigned int value;
} isdn_message[] = {
{"PH_ACTIVATE", L1_ACTIVATE_REQ},
{"PH_DEACTIVATE", L1_DEACTIVATE_REQ},
{"DL_ESTABLISH", L2_ESTABLISH_REQ},
{"DL_RELEASE", L2_RELEASE_REQ},
{"UNKNOWN", L3_UNKNOWN_REQ},
{"MT_TIMEOUT", L3_TIMEOUT_REQ},
{"MT_SETUP", L3_SETUP_REQ},
{"MT_SETUP_ACK", L3_SETUP_ACKNOWLEDGE_REQ},
{"MT_PROCEEDING", L3_PROCEEDING_REQ},
{"MT_ALERTING", L3_ALERTING_REQ},
{"MT_CONNECT", L3_CONNECT_REQ},
{"MT_CONNECT_ACK", L3_CONNECT_ACKNOWLEDGE_REQ},
{"MT_DISCONNECT", L3_DISCONNECT_REQ},
{"MT_RELEASE", L3_RELEASE_REQ},
{"MT_RELEASE_COMP", L3_RELEASE_COMPLETE_REQ},
{"MT_INFORMATION", L3_INFORMATION_REQ},
{"MT_PROGRESS", L3_PROGRESS_REQ},
{"MT_NOTIFY", L3_NOTIFY_REQ},
{"MT_SUSPEND", L3_SUSPEND_REQ},
{"MT_SUSPEND_ACK", L3_SUSPEND_ACKNOWLEDGE_REQ},
{"MT_SUSPEND_REJ", L3_SUSPEND_REJECT_REQ},
{"MT_RESUME", L3_RESUME_REQ},
{"MT_RESUME_ACK", L3_RESUME_ACKNOWLEDGE_REQ},
{"MT_RESUME_REJ", L3_RESUME_REJECT_REQ},
{"MT_HOLD", L3_HOLD_REQ},
{"MT_HOLD_ACK", L3_HOLD_ACKNOWLEDGE_REQ},
{"MT_HOLD_REJ", L3_HOLD_REJECT_REQ},
{"MT_RETRIEVE", L3_RETRIEVE_REQ},
{"MT_RETRIEVE_ACK", L3_RETRIEVE_ACKNOWLEDGE_REQ},
{"MT_RETRIEVE_REJ", L3_RETRIEVE_REJECT_REQ},
{"MT_FACILITY", L3_FACILITY_REQ},
{"MT_STATUS", L3_STATUS_REQ},
{"MT_RESTART", L3_RESTART_REQ},
{"MT_NEW_L3ID", L3_NEW_L3ID_REQ},
{"MT_RELEASE_L3ID", L3_RELEASE_L3ID_REQ},
{NULL, 0},
};
static const char *isdn_prim[4] = {
" REQUEST",
" CONFIRM",
" INDICATION",
" RESPONSE",
};
void l1l2l3_trace_header(struct mISDNport *mISDNport, class PmISDN *port, unsigned int msg, int direction)
{
int i;
char msgtext[64];
SCPY(msgtext, "<<UNKNOWN MESSAGE>>");
/* select message and primitive text */
i = 0;
while(isdn_message[i].name) {
// if (msg == L3_NOTIFY_REQ) printf("val = %x %s\n", isdn_message[i].value, isdn_message[i].name);
if (isdn_message[i].value == (msg&0xffffff00)) {
SCPY(msgtext, isdn_message[i].name);
break;
}
i++;
}
SCAT(msgtext, isdn_prim[msg&0x00000003]);
/* add direction */
if (direction && (msg&0xffffff00)!=L3_NEW_L3ID_REQ && (msg&0xffffff00)!=L3_RELEASE_L3ID_REQ) {
if (mISDNport) {
if (mISDNport->ntmode) {
if (direction == DIRECTION_OUT)
SCAT(msgtext, " N->U");
else
SCAT(msgtext, " N<-U");
} else {
if (direction == DIRECTION_OUT)
SCAT(msgtext, " U->N");
else
SCAT(msgtext, " U<-N");
}
}
}
/* 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,
direction,
CATEGORY_CH,
port?port->p_serial:0,
msgtext);
}
/*
* send control information to the channel (dsp-module)
*/
void ph_control(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, unsigned int c2, const char *trace_name, int trace_value)
{
unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
int ret;
int len = 8;
if (sock < 0)
return;
if (c1 == DTMF_TONE_START && c2 == 0) {
len = 4;
}
ctrl->prim = PH_CONTROL_REQ;
ctrl->id = 0;
*d++ = c1;
*d++ = c2;
ret = sendto(sock, buffer, MISDN_HEADER_LEN+len, 0, NULL, 0);
if (ret <= 0)
PERROR("Failed to send to socket %d\n", sock);
chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
if (c1 == DSP_CONF_JOIN)
add_trace(trace_name, NULL, "0x%08x", trace_value);
else
add_trace(trace_name, NULL, "%d", trace_value);
end_trace();
}
void ph_control_block(struct mISDNport *mISDNport, class PmISDN *isdnport, int sock, unsigned int c1, void *c2, int c2_len, const char *trace_name, int trace_value)
{
unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
unsigned int *d = (unsigned int *)(buffer+MISDN_HEADER_LEN);
int ret;
if (sock < 0)
return;
ctrl->prim = PH_CONTROL_REQ;
ctrl->id = 0;
*d++ = c1;
memcpy(d, c2, c2_len);
ret = sendto(sock, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
if (ret <= 0)
PERROR("Failed to send to socket %d\n", sock);
chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
add_trace(trace_name, NULL, "%d", trace_value);
end_trace();
}
static int b_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int i);
/*
* subfunction for bchannel_event
* create stack
*/
static int _bchannel_create(struct mISDNport *mISDNport, int i)
{
int ret;
struct sockaddr_mISDN addr;
if (mISDNport->b_sock[i].inuse) {
PERROR("Error: Socket already created for index %d\n", i);
return(0);
}
/* open socket */
//#warning testing without DSP
// mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_HDLC:ISDN_P_B_RAW);
mISDNport->b_sock[i].fd = socket(PF_ISDN, SOCK_DGRAM, (mISDNport->b_mode[i]==B_MODE_HDLC)?ISDN_P_B_L2DSPHDLC:ISDN_P_B_L2DSP);
if (mISDNport->b_sock[i].fd < 0) {
PERROR("Error: Failed to open bchannel-socket for index %d with mISDN-DSP layer. Did you load mISDN_dsp.ko?\n", i);
return(0);
}
/* register callback for read */
register_fd(&mISDNport->b_sock[i], LCR_FD_READ, b_sock_callback, mISDNport, i);
/* bind socket to bchannel */
addr.family = AF_ISDN;
addr.dev = mISDNport->portnum;
addr.channel = i+1+(i>=15);
ret = bind(mISDNport->b_sock[i].fd, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
PERROR("Error: Failed to bind bchannel-socket for index %d with mISDN-DSP layer (errno=%d). Did you load mISDN_dsp.ko?\n", i, errno);
close(mISDNport->b_sock[i].fd);
unregister_fd(&mISDNport->b_sock[i]);
return(0);
}
chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
add_trace("channel", NULL, "%d", i+1+(i>=15));
add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
end_trace();
return(1);
}
/*
* subfunction for bchannel_event
* activate / deactivate request
*/
static void _bchannel_activate(struct mISDNport *mISDNport, int i, int activate, int timeout)
{
struct mISDNhead act;
int ret;
if (!mISDNport->b_sock[i].inuse)
return;
act.prim = (activate)?PH_ACTIVATE_REQ:PH_DEACTIVATE_REQ;
act.id = 0;
ret = sendto(mISDNport->b_sock[i].fd, &act, MISDN_HEADER_LEN, 0, NULL, 0);
if (ret <= 0)
PERROR("Failed to send to socket %d\n", mISDNport->b_sock[i].fd);
/* trace */
chan_trace_header(mISDNport, mISDNport->b_port[i], activate ? "BCHANNEL activate" : "BCHANNEL deactivate", DIRECTION_OUT);
add_trace("channel", NULL, "%d", i+1+(i>=15));
if (timeout)
add_trace("event", NULL, "timeout recovery");
end_trace();
}
/*
* subfunction for bchannel_event
* set features
*/
static void _bchannel_configure(struct mISDNport *mISDNport, int i)
{
struct PmISDN *port;
int handle, mode;
if (!mISDNport->b_sock[i].inuse)
return;
handle = mISDNport->b_sock[i].fd;
port = mISDNport->b_port[i];
mode = mISDNport->b_mode[i];
if (!port) {
PERROR("bchannel index i=%d not associated with a port object\n", i);
return;
}
/* set dsp features */
if (port->p_m_txdata)
ph_control(mISDNport, port, handle, (port->p_m_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", port->p_m_txdata);
if (port->p_m_delay && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DSP_DELAY, port->p_m_delay, "DSP-DELAY", port->p_m_delay);
if (port->p_m_tx_dejitter && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DSP_TX_DEJITTER, port->p_m_tx_dejitter, "DSP-TX_DEJITTER", port->p_m_tx_dejitter);
if (port->p_m_tx_gain && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_TX, port->p_m_tx_gain, "DSP-TX_GAIN", port->p_m_tx_gain);
if (port->p_m_rx_gain && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DSP_VOL_CHANGE_RX, port->p_m_rx_gain, "DSP-RX_GAIN", port->p_m_rx_gain);
if (port->p_m_pipeline[0] && mode == B_MODE_TRANSPARENT)
ph_control_block(mISDNport, port, handle, DSP_PIPELINE_CFG, port->p_m_pipeline, strlen(port->p_m_pipeline)+1, "DSP-PIPELINE", 0);
if (port->p_m_conf && !port->p_m_mute)
ph_control(mISDNport, port, handle, DSP_CONF_JOIN, port->p_m_conf, "DSP-CONF", port->p_m_conf);
if (port->p_m_echo)
ph_control(mISDNport, port, handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
if (port->p_m_tone && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DSP_TONE_PATT_ON, port->p_m_tone, "DSP-TONE", port->p_m_tone);
if (port->p_m_rxoff)
ph_control(mISDNport, port, handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
// if (port->p_m_txmix && mode == B_MODE_TRANSPARENT)
// ph_control(mISDNport, port, handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
if (port->p_m_dtmf && mode == B_MODE_TRANSPARENT)
ph_control(mISDNport, port, handle, DTMF_TONE_START, port->p_m_dtmf_threshold, "DSP-DTMF", 1);
if (port->p_m_crypt && mode == B_MODE_TRANSPARENT)
ph_control_block(mISDNport, port, handle, DSP_BF_ENABLE_KEY, port->p_m_crypt_key, port->p_m_crypt_key_len, "DSP-CRYPT", port->p_m_crypt_key_len);
}
void PmISDN::set_conf(int oldconf, int newconf)
{
if (oldconf != newconf) {
PDEBUG(DEBUG_BCHANNEL, "we change conference from conf=%d to conf=%d.\n", oldconf, newconf);
if (p_m_b_index > -1)
if (p_m_mISDNport->b_state[p_m_b_index] == B_STATE_ACTIVE)
ph_control(p_m_mISDNport, this, p_m_mISDNport->b_sock[p_m_b_index].fd, (newconf)?DSP_CONF_JOIN:DSP_CONF_SPLIT, newconf, "DSP-CONF", newconf);
} else
PDEBUG(DEBUG_BCHANNEL, "we already have conf=%d.\n", newconf);
}
/*
* subfunction for bchannel_event
* destroy stack
*/
static void _bchannel_destroy(struct mISDNport *mISDNport, int i)
{
if (!mISDNport->b_sock[i].inuse)
return;
chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
add_trace("channel", NULL, "%d", i+1+(i>=15));
add_trace("socket", NULL, "%d", mISDNport->b_sock[i].fd);
end_trace();
close(mISDNport->b_sock[i].fd);
unregister_fd(&mISDNport->b_sock[i]);
}
/*
bchannel procedure
------------------
A bchannel goes through the following states in this order:
- B_STATE_IDLE
No one is using the bchannel.
It is available and not linked to Port class, nor reserved.
- B_STATE_ACTIVATING
The bchannel stack is created and an activation request is sent.
It MAY be linked to Port class, but already unlinked due to Port class removal.
- B_STATE_ACTIVE
The bchannel is active and cofigured to the Port class needs.
Also it is linked to a Port class, otherwhise it would be deactivated.
- B_STATE_DEACTIVATING
The bchannel is in deactivating state, due to deactivation request.
It may be linked to a Port class, that likes to reactivate it.
- B_STATE_IDLE
See above.
After deactivating bchannel, and if not used, the bchannel becomes idle again.
A bchannel can have the following events:
- B_EVENT_USE
A bchannel is required by a Port class.
- B_EVENT_ACTIVATED
The bchannel beomes active.
- B_EVENT_DROP
The bchannel is not required by Port class anymore
- B_EVENT_DEACTIVATED
The bchannel becomes inactive.
All actions taken on these events depend on the current bchannel's state and if it is linked to a Port class.
*/
/*
* process bchannel events
* - mISDNport is a pointer to the port's structure
* - i is the index of the bchannel
* - event is the B_EVENT_* value
* - port is the PmISDN class pointer
*/
void bchannel_event(struct mISDNport *mISDNport, int i, int event)
{
class PmISDN *b_port = mISDNport->b_port[i];
int state = mISDNport->b_state[i];
int timer = -1; // no change
int p_m_tx_gain = 0;
int p_m_rx_gain = 0;
char *p_m_pipeline = NULL;
unsigned char *p_m_crypt_key = NULL;
int p_m_crypt_key_len = 0;
int p_m_crypt_key_type = 0;
if (b_port) {
p_m_tx_gain = b_port->p_m_tx_gain;
p_m_rx_gain = b_port->p_m_rx_gain;
p_m_pipeline = b_port->p_m_pipeline;
p_m_crypt_key = b_port->p_m_crypt_key;
p_m_crypt_key_len = b_port->p_m_crypt_key_len;
p_m_crypt_key_type = /*b_port->p_m_crypt_key_type*/1;
}
switch(event) {
case B_EVENT_USE:
/* port must be linked in order to allow activation */
if (!b_port)
FATAL("bchannel must be linked to a Port class\n");
switch(state) {
case B_STATE_IDLE:
/* create stack and send activation request */
if (_bchannel_create(mISDNport, i)) {
_bchannel_activate(mISDNport, i, 1, 0);
state = B_STATE_ACTIVATING;
timer = B_TIMER_ACTIVATING;
}
break;
case B_STATE_ACTIVATING:
/* do nothing, because it is already activating */
break;
default:
/* problems that might ocurr:
* B_EVENT_USE is received when channel already in use.
*/
PERROR("Illegal event %d at state %d, please correct.\n", event, state);
}
break;
case B_EVENT_ACTIVATED:
timer = 0;
switch(state) {
case B_STATE_ACTIVATING:
if (b_port) {
/* bchannel is active and used by Port class, so we configure bchannel */
_bchannel_configure(mISDNport, i);
state = B_STATE_ACTIVE;
b_port->p_m_load = 0;
b_port->update_load();
} else {
/* bchannel is active, but not used anymore (or has wrong stack config), so we deactivate */
_bchannel_activate(mISDNport, i, 0, 0);
state = B_STATE_DEACTIVATING;
timer = B_TIMER_DEACTIVATING;
}
break;
default:
PERROR("Illegal event %d at state %d, please correct.\n", event, state);
}
break;
case B_EVENT_DROP:
if (!b_port)
FATAL("bchannel must be linked to a Port class\n");
switch(state) {
case B_STATE_IDLE:
/* bchannel is idle due to an error, so we do nothing */
break;
case B_STATE_ACTIVATING:
/* do nothing because we must wait until bchanenl is active before deactivating */
break;
case B_STATE_ACTIVE:
/* bchannel is active, so we deactivate */
_bchannel_activate(mISDNport, i, 0, 0);
state = B_STATE_DEACTIVATING;
timer = B_TIMER_DEACTIVATING;
break;
case B_STATE_DEACTIVATING:
/* we may have taken an already deactivating bchannel, but do not require it anymore, so we do nothing */
break;
default:
PERROR("Illegal event %d at state %d, please correct.\n", event, state);
}
break;
case B_EVENT_DEACTIVATED:
timer = 0;
switch(state) {
case B_STATE_IDLE:
/* ignore due to deactivation confirm after unloading */
break;
case B_STATE_DEACTIVATING:
_bchannel_destroy(mISDNport, i);
state = B_STATE_IDLE;
if (b_port) {
/* bchannel is now deactivate, but is requied by Port class, so we reactivate */
if (_bchannel_create(mISDNport, i)) {
_bchannel_activate(mISDNport, i, 1, 0);
state = B_STATE_ACTIVATING;
timer = B_TIMER_ACTIVATING;
}
}
break;
default:
PERROR("Illegal event %d at state %d, please correct.\n", event, state);
}
break;
case B_EVENT_TIMEOUT:
timer = 0;
switch(state) {
case B_STATE_IDLE:
/* ignore due to deactivation confirm after unloading */
break;
case B_STATE_ACTIVATING:
_bchannel_activate(mISDNport, i, 1, 1);
timer = B_TIMER_ACTIVATING;
break;
case B_STATE_DEACTIVATING:
_bchannel_activate(mISDNport, i, 0, 1);
timer = B_TIMER_DEACTIVATING;
break;
default:
PERROR("Illegal event %d at state %d, please correct.\n", event, state);
}
break;
default:
PERROR("Illegal event %d, please correct.\n", event);
}
mISDNport->b_state[i] = state;
if (timer == 0)
unsched_timer(&mISDNport->b_timer[i]);
else if (timer > 0)
schedule_timer(&mISDNport->b_timer[i], timer, 0);
}
/*
* check for available channel and reserve+set it.
* give channel number or SEL_CHANNEL_ANY or SEL_CHANNEL_NO
* give exclusiv flag
* returns -(cause value) or x = channel x or 0 = no channel
* NOTE: no activation is done here
*/
int PmISDN::seize_bchannel(int channel, int exclusive)
{
int i;
/* the channel is what we have */
if (p_m_b_channel == channel)
return(channel);
/* if channel already in use, release it */
if (p_m_b_channel)
drop_bchannel();
/* if CHANNEL_NO */
if (channel==CHANNEL_NO || channel==0)
return(0);
/* is channel in range ? */
if (channel==16
|| (channel>p_m_mISDNport->b_num && channel<16)
|| ((channel-1)>p_m_mISDNport->b_num && channel>16)) /* channel-1 because channel 16 is not counted */
return(-6); /* channel unacceptable */
/* request exclusive channel */
if (exclusive && channel>0) {
i = channel-1-(channel>16);
if (p_m_mISDNport->b_port[i])
return(-44); /* requested channel not available */
goto seize;
}
/* ask for channel */
if (channel>0) {
i = channel-1-(channel>16);
if (p_m_mISDNport->b_port[i] == NULL)
goto seize;
}
/* search for channel */
i = 0;
while(i < p_m_mISDNport->b_num) {
if (!p_m_mISDNport->b_port[i]) {
channel = i+1+(i>=15);
goto seize;
}
i++;
}
return(-34); /* no free channel */
seize:
PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) seizing bchannel %d (index %d)\n", p_name, channel, i);
/* link Port, set parameters */
p_m_mISDNport->b_port[i] = this;
p_m_b_index = i;
p_m_b_channel = channel;
p_m_b_exclusive = exclusive;
p_m_mISDNport->b_mode[i] = p_m_b_mode;
/* reserve channel */
if (!p_m_b_reserve) {
p_m_b_reserve = 1;
p_m_mISDNport->b_reserved++;
}
return(channel);
}
/*
* drop reserved channel and unset it.
* deactivation is also done
*/
void PmISDN::drop_bchannel(void)
{
/* unreserve channel */
if (p_m_b_reserve)
p_m_mISDNport->b_reserved--;
p_m_b_reserve = 0;
/* if not in use */
if (p_m_b_index < 0)
return;
if (!p_m_b_channel)
return;
PDEBUG(DEBUG_BCHANNEL, "PmISDN(%s) dropping bchannel\n", p_name);
if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_IDLE)
bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_DROP);
p_m_mISDNport->b_port[p_m_b_index] = NULL;
p_m_mISDNport->b_mode[p_m_b_index] = 0;
p_m_b_index = -1;
p_m_b_channel = 0;
p_m_b_exclusive = 0;
}
/*
* handler
audio transmission procedure:
-----------------------------
* priority
three sources of audio transmission:
- crypto-data high priority
- tones high priority (also high)
- remote-data low priority
* elapsed
a variable that temporarily shows the number of samples elapsed since last transmission process.
p_m_last_tv_* is used to store that last timestamp. this is used to calculate the time elapsed.
* load
a variable that is increased whenever data is transmitted.
it is decreased while time elapses. it stores the number of samples that
are currently loaded to dsp module.
since clock in dsp module is the same clock for user space process, these
times have no skew.
* levels
there are two levels:
p_m_preload will give the load that have to be kept in dsp.
ISDN_MAXLOAD (2*p_m_preload) will give the maximum load before dropping.
* procedure for low priority data
see txfromup() for procedure
in short: remote data is ignored during high priority tones
* procedure for high priority data
whenever load is below p_m_preload, load is filled up to p_m_preload
if no more data is available, load becomes empty again.
'load' variable:
0 p_m_preload ISDN_MAXLOAD
+--------------------+----------------------+
| | |
+--------------------+----------------------+
on empty load or on load below p_m_preload, the load is inceased to p_m_preload:
0 p_m_preload ISDN_MAXLOAD
+--------------------+----------------------+
|TTTTTTTTTTTTTTTTTTTT| |
+--------------------+----------------------+
on empty load, remote-audio causes the load with the remote audio to be increased to p_m_preload.
0 p_m_preload ISDN_MAXLOAD
+--------------------+----------------------+
|TTTTTTTTTTTTTTTTTTTTRRRRR |
+--------------------+----------------------+
*/
void PmISDN::update_load(void)
{
/* don't trigger load event if event already active */
if (p_m_loadtimer.active)
return;
schedule_timer(&p_m_loadtimer, 0, 0); /* no delay the first time */
}
static int load_timer(struct lcr_timer *timer, void *instance, int index)
{
class PmISDN *isdnport = (class PmISDN *)instance;
isdnport->load_tx();
return 0;
}
void PmISDN::load_tx(void)
{
int elapsed = 0;
int ret;
struct timeval current_time;
/* get elapsed */
gettimeofday(¤t_time, NULL);
if (p_m_last_tv_sec) {
elapsed = 8000 * (current_time.tv_sec - p_m_last_tv_sec)
+ 8 * (current_time.tv_usec/1000 - p_m_last_tv_msec);
}
/* set clock of last process! */
p_m_last_tv_sec = current_time.tv_sec;
p_m_last_tv_msec = current_time.tv_usec/1000;
/* process only if we have samples and we are active */
if (p_m_mISDNport->b_state[p_m_b_index] != B_STATE_ACTIVE)
return;
if (elapsed) {
/* update load */
if (elapsed < p_m_load)
p_m_load -= elapsed;
else
p_m_load = 0;
/* to send data, tone must be on */
if ((p_tone_name[0] || p_m_crypt_msg_loops || p_m_inband_send_on) /* what tones? */
&& (p_m_load < p_m_preload) /* not too much load? */
&& (p_state==PORT_STATE_CONNECT || p_m_mISDNport->tones || p_m_inband_send_on)) { /* connected or inband-tones? */
int tosend = p_m_preload - p_m_load, length;
unsigned char buf[MISDN_HEADER_LEN+tosend];
struct mISDNhead *frm = (struct mISDNhead *)buf;
unsigned char *p = buf+MISDN_HEADER_LEN;
/* copy inband signalling (e.g. used by ss5) */
if (p_m_inband_send_on && tosend) {
tosend -= inband_send(p, tosend);
}
/* copy crypto loops */
while (p_m_crypt_msg_loops && tosend) {
/* how much do we have to send */
length = p_m_crypt_msg_len - p_m_crypt_msg_current;
/* clip tosend */
if (length > tosend)
length = tosend;
/* copy message (part) to buffer */
memcpy(p, p_m_crypt_msg+p_m_crypt_msg_current, length);
/* new position */
p_m_crypt_msg_current += length;
if (p_m_crypt_msg_current == p_m_crypt_msg_len) {
/* next loop */
p_m_crypt_msg_current = 0;
p_m_crypt_msg_loops--;
if (!p_m_crypt_msg_loops)
update_rxoff();
// puts("eine loop weniger");