-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchan_lcr.c
3678 lines (3262 loc) · 101 KB
/
chan_lcr.c
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 **
** **
** Asterisk socket client **
** **
\*****************************************************************************/
/*
Registering to LCR:
To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
the application name. This name is unique an can be used for routing calls.
Now the channel driver is linked to LCR and can receive and make calls.
Call is initiated by LCR:
If a call is received from LCR, a MESSAGE_NEWREF is received first.
The ref_was_assigned ist set to 1.
A new chan_call instance is created. The call reference (ref) is given by
the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
using ast_channel_alloc(1). The setup information is given to asterisk.
The new Asterisk instance pointer (ast) is stored to chan_call structure.
The state changes to CHAN_LCR_STATE_IN_SETUP.
Call is initiated by Asterisk:
If a call is requested from Asterisk, a new chan_call instance is created.
The new Asterisk instance pointer (ast) is stored to chan_call structure.
The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
a new call reference (ref).
The ref_was_assigned ist set to 1.
Further dialing information is queued.
After the new callref is received by special MESSAGE_NEWREF reply, new ref
is stored in the chan_call structure.
The setup information is sent to LCR using MESSAGE_SETUP.
The state changes to CHAN_LCR_STATE_OUT_SETUP.
Call is in process:
During call process, messages are received and sent.
The state changes accordingly.
Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
If a MESSAGE_OVERLAP is received, further dialing is required.
Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
Call is released by LCR:
A MESSAGE_RELEASE is received with the call reference (ref) to be released.
The current ref is set to 0, to indicate released reference.
The ref_was_assigned==1 shows that there is no other ref to be assigned.
The state changes to CHAN_LCR_STATE_RELEASE.
ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
the chan_call instance is destroyed.
After lcr_hangup() is called-back by Asterisk, the chan_call instance
is destroyed, because the current ref is set to 0 and the state equals
CHAN_LCR_STATE_RELEASE.
If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
"Call is released by Asterisk".
Call is released by Asterisk:
lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
set to CHAN_LCR_STATE_RELEASE.
The ref_was_assigned==0 shows that a ref is still requested.
Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
LCR and the chan_call instance is destroyed.
If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
"Call is released by LCR".
Locking issues:
The deadlocking problem:
- chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
to be unlocked.
- ast_channel thread locks ast_channel and calls a tech function and waits
there for chan_lock to be unlocked.
The solution:
Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
ast_channel can be locked. All messages to asterisk are queued inside call
instance and will be handled using a try-lock to get ast_channel lock.
If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
if the lock is incremented and decremented there.
Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
it is called from ast_channel process which has already locked ast_channel.
*/
/* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
#define LCR_FOR_ASTERISK
/* #define LCR_FOR_CALLWEAVER */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
//#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <semaphore.h>
#define HAVE_ATTRIBUTE_always_inline 1
#define HAVE_ARPA_INET_H 1
#define HAVE_TIMERSUB 1
#define HAVE_STRTOQ 1
#define HAVE_INET_ATON 1
#include <asterisk/compiler.h>
#ifdef LCR_FOR_ASTERISK
#include <asterisk/buildopts.h>
#endif
/*
* Fwd declare struct ast_channel to get rid of gcc warning about
* incompatible pointer type passed to ast_register_application2.
*/
struct ast_channel;
#include <asterisk/module.h>
#include <asterisk/channel.h>
#include <asterisk/config.h>
#include <asterisk/logger.h>
#include <asterisk/pbx.h>
#include <asterisk/options.h>
#include <asterisk/io.h>
#include <asterisk/frame.h>
#include <asterisk/translate.h>
#include <asterisk/cli.h>
#include <asterisk/musiconhold.h>
#include <asterisk/dsp.h>
#include <asterisk/translate.h>
#include <asterisk/file.h>
#ifdef LCR_FOR_ASTERISK
#include <asterisk/callerid.h>
#endif
#ifdef LCR_FOR_CALLWEAVER
#include <asterisk/phone_no_utils.h>
#endif
#include <asterisk/indications.h>
#include <asterisk/app.h>
#include <asterisk/features.h>
#include <asterisk/sched.h>
#if ASTERISK_VERSION_NUM < 110000
#include <asterisk/version.h>
#endif
#include "extension.h"
#include "message.h"
#include "callerid.h"
#include "lcrsocket.h"
#include "cause.h"
#include "select.h"
#include "options.h"
#include "chan_lcr.h"
CHAN_LCR_STATE // state description structure
MESSAGES // message text
#ifdef LCR_FOR_CALLWEAVER
AST_MUTEX_DEFINE_STATIC(rand_lock);
#endif
unsigned char flip_bits[256];
#ifdef LCR_FOR_CALLWEAVER
static struct ast_frame nullframe = { AST_FRAME_NULL, };
#endif
int lcr_debug=1;
char lcr_type[]="lcr";
#ifdef LCR_FOR_CALLWEAVER
static ast_mutex_t usecnt_lock;
static int usecnt=0;
static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
#endif
pthread_t chan_tid;
ast_mutex_t chan_lock; /* global lock */
ast_mutex_t log_lock; /* logging log */
/* global_change:
* used to indicate change in file descriptors, so select function's result may
* be obsolete.
*/
int global_change = 0;
int wake_global = 0;
int wake_pipe[2];
struct lcr_fd wake_fd;
int glob_channel = 0;
int lcr_sock = -1;
struct lcr_fd socket_fd;
struct lcr_timer socket_retry;
struct admin_list {
struct admin_list *next;
struct admin_message msg;
} *admin_first = NULL;
static struct ast_channel_tech lcr_tech;
/*
* logging
*/
void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
{
char buffer[1024];
char call_text[128] = "NULL";
char ast_text[128] = "NULL";
va_list args;
if (!option_debug) return;
ast_mutex_lock(&log_lock);
va_start(args,fmt);
vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
buffer[sizeof(buffer)-1]=0;
va_end(args);
if (call)
sprintf(call_text, "%d", call->ref);
if (ast)
#if ASTERISK_VERSION_NUM < 110000
strncpy(ast_text, ast->name, sizeof(ast_text)-1);
#else
strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
#endif
ast_text[sizeof(ast_text)-1] = '\0';
ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
// printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
ast_mutex_unlock(&log_lock);
}
/*
* channel and call instances
*/
struct chan_call *call_first;
/*
* find call by ref
* special case: 0: find new ref, that has not been assigned a ref yet
*/
struct chan_call *find_call_ref(unsigned int ref)
{
struct chan_call *call = call_first;
int assigned = (ref > 0);
while(call) {
if (call->ref == ref && call->ref_was_assigned == assigned)
break;
call = call->next;
}
return call;
}
void free_call(struct chan_call *call)
{
struct chan_call **temp = &call_first;
while(*temp) {
if (*temp == call) {
*temp = (*temp)->next;
if (call->pipe[0] > -1)
close(call->pipe[0]);
if (call->pipe[1] > -1)
close(call->pipe[1]);
if (call->bridge_call) {
if (call->bridge_call->bridge_call != call)
CERROR(call, NULL, "Linked call structure has no link to us.\n");
call->bridge_call->bridge_call = NULL;
}
if (call->trans)
ast_translator_free_path(call->trans);
if (call->dsp)
ast_dsp_free(call->dsp);
CDEBUG(call, NULL, "Call instance freed.\n");
free(call);
global_change = 1;
return;
}
temp = &((*temp)->next);
}
CERROR(call, NULL, "Call instance not found in list.\n");
}
struct chan_call *alloc_call(void)
{
struct chan_call **callp = &call_first;
while(*callp)
callp = &((*callp)->next);
*callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
if (*callp)
memset(*callp, 0, sizeof(struct chan_call));
if (pipe((*callp)->pipe) < 0) {
CERROR(*callp, NULL, "Failed to create pipe.\n");
free_call(*callp);
return NULL;
}
fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
CDEBUG(*callp, NULL, "Call instance allocated.\n");
/* unset dtmf (default, use option 'd' to enable) */
(*callp)->dsp_dtmf = 0;
return *callp;
}
unsigned short new_bridge_id(void)
{
struct chan_call *call;
unsigned short id = 1;
/* search for lowest bridge id that is not in use and not 0 */
while(id) {
call = call_first;
while(call) {
if (call->bridge_id == id)
break;
call = call->next;
}
if (!call)
break;
id++;
}
CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
return id;
}
/*
* enque message to LCR
*/
int send_message(int message_type, unsigned int ref, union parameter *param)
{
struct admin_list *admin, **adminp;
if (lcr_sock < 0) {
CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
return -1;
}
if (message_type != MESSAGE_TRAFFIC)
CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
adminp = &admin_first;
while(*adminp)
adminp = &((*adminp)->next);
admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
if (!admin) {
CERROR(NULL, NULL, "No memory for message to LCR.\n");
return -1;
}
*adminp = admin;
admin->msg.message = ADMIN_MESSAGE;
admin->msg.u.msg.type = message_type;
admin->msg.u.msg.ref = ref;
memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
socket_fd.when |= LCR_FD_WRITE;
if (!wake_global) {
wake_global = 1;
char byte = 0;
int rc;
rc = write(wake_pipe[1], &byte, 1);
}
return 0;
}
/*
* apply options (in locked state)
*/
void apply_opt(struct chan_call *call, char *data)
{
union parameter newparam;
char string[1024], *p = string, *opt;//, *key;
// int gain, i;
if (!data[0])
return; // no opts
strncpy(string, data, sizeof(string)-1);
string[sizeof(string)-1] = '\0';
/* parse options */
while((opt = strsep(&p, ":"))) {
switch(opt[0]) {
case 'd':
if (opt[1] == '\0') {
CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
strncpy(call->display, opt+1, sizeof(call->display)-1);
else {
memset(&newparam, 0, sizeof(union parameter));
strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
send_message(MESSAGE_NOTIFY, call->ref, &newparam);
}
break;
case 'D':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'D' (DTMF) expects no parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'D' (DTMF).\n");
call->dsp_dtmf = 1;
break;
#if 0
case 'c':
if (opt[1] == '\0') {
CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
break;
}
key = opt+1;
/* check for 0xXXXX... type of key */
if (!!strncmp((char *)key, "0x", 2)) {
CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
break;
}
key+=2;
if (strlen(key) > 56*2 || (strlen(key) % 1)) {
CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
break;
}
i = 0;
while(*key) {
if (*key>='0' && *key<='9')
call->bf_key[i] = (*key-'0') << 8;
else if (*key>='a' && *key<='f')
call->bf_key[i] = (*key-'a'+10) << 8;
else if (*key>='A' && *key<='F')
call->bf_key[i] = (*key-'A'+10) << 8;
else
break;
key++;
if (*key>='0' && *key<='9')
call->bf_key[i] += (*key - '0');
else if (*key>='a' && *key<='f')
call->bf_key[i] += (*key - 'a' + 10);
else if (*key>='A' && *key<='F')
call->bf_key[i] += (*key - 'A' + 10);
else
break;
key++;
i++;
}
if (*key) {
CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
break;
}
call->bf_len = i;
CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
if (call->bchannel)
bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
break;
#endif
case 'h':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
if (!call->hdlc)
call->hdlc = 1;
break;
case 'q':
if (opt[1] == '\0') {
CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'q' (queue).\n");
call->tx_queue = atoi(opt+1);
break;
#if 0
case 'e':
if (opt[1] == '\0') {
CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
if (call->bchannel)
bchannel_pipeline(call->bchannel, call->pipeline);
break;
#endif
case 'f':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'f' (faxdetect) expects no parameter.\n", opt);
break;
}
call->faxdetect = 1;
call->dsp_dtmf = 0;
CDEBUG(call, call->ast, "Option 'f' (faxdetect).\n");
break;
case 'a':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'a' (asterisk DTMF) expects no parameter.\n", opt);
break;
}
call->ast_dsp = 1;
call->dsp_dtmf = 0;
CDEBUG(call, call->ast, "Option 'a' (Asterisk DTMF detection).\n");
break;
case 'r':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
call->rebuffer = 1;
call->framepos = 0;
break;
case 's':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
call->inband_dtmf = 1;
break;
#if 0
case 'v':
if (opt[1] != 'r' && opt[1] != 't') {
CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
break;
}
gain = atoi(opt+2);
if (gain < -8 || gain >8) {
CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
break;
}
CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
if (opt[1] == 'r') {
call->rx_gain = gain;
if (call->bchannel)
bchannel_gain(call->bchannel, call->rx_gain, 0);
} else {
call->tx_gain = gain;
if (call->bchannel)
bchannel_gain(call->bchannel, call->tx_gain, 1);
}
break;
#endif
case 'k':
if (opt[1] != '\0') {
CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
break;
}
CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
if (!call->keypad)
call->keypad = 1;
break;
default:
CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
}
}
if (call->faxdetect || call->ast_dsp) {
if (!call->dsp)
call->dsp=ast_dsp_new();
if (call->dsp) {
#ifdef LCR_FOR_CALLWEAVER
ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_CNG_DETECT : 0));
#endif
#ifdef LCR_FOR_ASTERISK
#ifdef DSP_FEATURE_DTMF_DETECT
ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
#else
ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
#endif
#endif
if (!call->trans) {
#ifdef LCR_FOR_CALLWEAVER
call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
#endif
#ifdef LCR_FOR_ASTERISK
#if ASTERISK_VERSION_NUM < 100000
call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
// #else
// struct ast_format src;
// struct ast_format dst;
// ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
// ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
// call->trans=ast_translator_build_path(&dst, &src);
#endif
#endif
}
}
}
}
/*
* send setup info to LCR
* this function is called, when asterisk call is received and ref is received
*/
static void send_setup_to_lcr(struct chan_call *call)
{
union parameter newparam;
struct ast_channel *ast = call->ast;
// const char *tmp;
if (!call->ast || !call->ref)
return;
#ifdef AST_1_8_OR_HIGHER
CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
#else
CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
#endif
/* send setup message to LCR */
memset(&newparam, 0, sizeof(union parameter));
newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
if (call->keypad)
strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
else
strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
call->display[0] = '\0';
#ifdef AST_1_8_OR_HIGHER
/* set stored call information */
memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
#else
if (call->cid_num[0])
strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
if (call->cid_name[0])
strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
if (call->cid_rdnis[0]) {
strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
}
switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
case AST_PRES_RESTRICTED:
newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
break;
case AST_PRES_UNAVAILABLE:
newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
break;
case AST_PRES_ALLOWED:
default:
newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
}
switch(ast->cid.cid_ton) {
case 4:
newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
break;
case 2:
newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
break;
case 1:
newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
break;
default:
newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
}
#endif
#warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
// tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
// if (tmp && *tmp)
#if ASTERISK_VERSION_NUM < 110000
// ast->transfercapability = atoi(tmp);
newparam.setup.capainfo.bearer_capa = ast->transfercapability;
#else
// ast_channel_transfercapability_set(ast, atoi(tmp));
newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
#endif
newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
if (call->hdlc)
newparam.setup.capainfo.source_mode = B_MODE_HDLC;
else {
newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
}
newparam.setup.capainfo.hlc = INFO_HLC_NONE;
newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
send_message(MESSAGE_SETUP, call->ref, &newparam);
if (call->tx_queue) {
memset(&newparam, 0, sizeof(union parameter));
newparam.queue = call->tx_queue * 8;
send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
}
/* change to outgoing setup state */
call->state = CHAN_LCR_STATE_OUT_SETUP;
}
/*
* send dialing info to LCR
* this function is called, when setup acknowledge is received and dialing
* info is available.
*/
static void send_dialque_to_lcr(struct chan_call *call)
{
union parameter newparam;
if (!call->ast || !call->ref || !call->dialque[0])
return;
CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
/* send setup message to LCR */
memset(&newparam, 0, sizeof(union parameter));
if (call->keypad)
strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
else
strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
call->dialque[0] = '\0';
send_message(MESSAGE_INFORMATION, call->ref, &newparam);
}
/*
* in case of a bridge, the unsupported message can be forwarded directly
* to the remote call.
*/
static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
{
/* check bridge */
if (!call) return;
if (!call->bridge_call) return;
CDEBUG(call, NULL, "Sending message due bridging.\n");
send_message(message_type, call->bridge_call->ref, param);
}
/*
* send release message to LCR
*/
static void send_release(struct chan_call *call, int cause, int location)
{
union parameter newparam;
/* sending release */
memset(&newparam, 0, sizeof(union parameter));
newparam.disconnectinfo.cause = cause;
newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
send_message(MESSAGE_RELEASE, call->ref, &newparam);
}
/*
* check if extension matches and start asterisk
* if it can match, proceed
* if not, release
*/
static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
{
int cause, ret;
union parameter newparam;
#if ASTERISK_VERSION_NUM < 110000
char *exten = ast->exten;
#else
char s_exten[AST_MAX_EXTENSION];
char *exten=s_exten;
strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
#endif
if (!*exten)
exten = "s";
#if ASTERISK_VERSION_NUM < 110000
CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
#else
CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
#endif
if (complete) {
/* if not match */
#if ASTERISK_VERSION_NUM < 110000
if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
#else
if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
#endif
cause = 1;
goto release;
}
#if ASTERISK_VERSION_NUM < 110000
if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast->context);
#else
if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast_channel_context(ast));
#endif
cause = 28;
goto release;
}
CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
/* send setup acknowledge to lcr */
memset(&newparam, 0, sizeof(union parameter));
send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
/* change state */
call->state = CHAN_LCR_STATE_IN_PROCEEDING;
goto start;
}
#if ASTERISK_VERSION_NUM < 110000
if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
#else
if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
#endif
/* send setup acknowledge to lcr */
if (call->state != CHAN_LCR_STATE_IN_DIALING) {
memset(&newparam, 0, sizeof(union parameter));
send_message(MESSAGE_OVERLAP, call->ref, &newparam);
}
/* change state */
call->state = CHAN_LCR_STATE_IN_DIALING;
/* if match, start pbx */
#if ASTERISK_VERSION_NUM < 110000
if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
#else
if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
#endif
CDEBUG(call, ast, "Extensions matches.\n");
goto start;
}
/* send setup acknowledge to lcr */
if (call->state != CHAN_LCR_STATE_IN_DIALING) {
memset(&newparam, 0, sizeof(union parameter));
send_message(MESSAGE_OVERLAP, call->ref, &newparam);
}
/* change state */
call->state = CHAN_LCR_STATE_IN_DIALING;
/* if can match */
CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
return;
}
#if ASTERISK_VERSION_NUM < 110000
if (!*ast->exten) {
#else
if (!*ast_channel_exten(ast)) {
#endif
/* send setup acknowledge to lcr */
if (call->state != CHAN_LCR_STATE_IN_DIALING) {
memset(&newparam, 0, sizeof(union parameter));
send_message(MESSAGE_OVERLAP, call->ref, &newparam);
}
/* change state */
call->state = CHAN_LCR_STATE_IN_DIALING;
/* if can match */
CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
return;
}
/* if not match */
cause = 1;
release:
/* release lcr */
CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
send_release(call, cause, LOCATION_PRIVATE_LOCAL);
call->ref = 0;
/* release asterisk */
#if ASTERISK_VERSION_NUM < 110000
ast->hangupcause = call->cause;
#else
ast_channel_hangupcause_set(ast, call->cause);
#endif
/* change to release state */
call->state = CHAN_LCR_STATE_RELEASE;
ast_hangup(ast); // call will be destroyed here
return;
start:
/* send setup to asterisk */
CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
#ifdef LCR_FOR_CALLWEAVER
ast->type = "LCR";
snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
#endif
ret = ast_pbx_start(ast);
if (ret < 0) {
cause = (ret==-2)?34:27;
goto release;
}
call->pbx_started = 1;
ast_setstate(ast, AST_STATE_RING);
}
/*
* incoming setup from LCR
*/
static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
{
struct ast_channel *ast;
#ifdef AST_1_8_OR_HIGHER
struct ast_party_redirecting *ast_redir;
struct ast_party_caller *ast_caller;
#else
struct ast_callerid *ast_caller;
#endif
#if ASTERISK_VERSION_NUM >= 110000
struct ast_party_redirecting s_ast_redir;
struct ast_party_caller s_ast_caller;
ast_party_redirecting_init(&s_ast_redir);
ast_party_caller_init(&s_ast_caller);
#endif
CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
/* create asterisk channel instrance */
#ifdef LCR_FOR_CALLWEAVER
ast = ast_channel_alloc(1);
#endif
#ifdef LCR_FOR_ASTERISK
#ifdef AST_1_8_OR_HIGHER
ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
#else
ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
#endif
#endif
#if ASTERISK_VERSION_NUM < 110000
#ifdef AST_1_8_OR_HIGHER
ast_redir = &ast->redirecting;
ast_caller = &ast->caller;
#else
ast_caller = &ast->cid;
#endif
#else
ast_redir = &s_ast_redir;
ast_caller = &s_ast_caller;
#endif
if (!ast) {
/* release */
CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
send_release(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
/* remove call */
free_call(call);
return;
}
/* link together */
call->ast = ast;
#if ASTERISK_VERSION_NUM < 110000
ast->tech_pvt = call;
ast->tech = &lcr_tech;
ast->fds[0] = call->pipe[0];
#else
ast_channel_tech_pvt_set(ast, call);
ast_channel_tech_set(ast, &lcr_tech);
ast_channel_set_fd(ast, 0, call->pipe[0]);
#endif
/* fill setup information */
if (param->setup.dialinginfo.id)