1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
139 #include <asterisk/module.h>
140 #include <asterisk/channel.h>
141 #include <asterisk/config.h>
142 #include <asterisk/logger.h>
143 #include <asterisk/pbx.h>
144 #include <asterisk/options.h>
145 #include <asterisk/io.h>
146 #include <asterisk/frame.h>
147 #include <asterisk/translate.h>
148 #include <asterisk/cli.h>
149 #include <asterisk/musiconhold.h>
150 #include <asterisk/dsp.h>
151 #include <asterisk/translate.h>
152 #include <asterisk/file.h>
153 #ifdef LCR_FOR_ASTERISK
154 #include <asterisk/callerid.h>
156 #ifdef LCR_FOR_CALLWEAVER
157 #include <asterisk/phone_no_utils.h>
160 #include <asterisk/indications.h>
161 #include <asterisk/app.h>
162 #include <asterisk/features.h>
163 #include <asterisk/sched.h>
165 #include "extension.h"
167 #include "callerid.h"
168 #include "lcrsocket.h"
171 #include "bchannel.h"
173 #include "chan_lcr.h"
175 CHAN_LCR_STATE // state description structure
176 MESSAGES // message text
178 #ifdef LCR_FOR_CALLWEAVER
179 AST_MUTEX_DEFINE_STATIC(rand_lock);
182 unsigned char flip_bits[256];
184 #ifdef LCR_FOR_CALLWEAVER
185 static struct ast_frame nullframe = { AST_FRAME_NULL, };
191 char lcr_type[]="lcr";
193 #ifdef LCR_FOR_CALLWEAVER
194 static ast_mutex_t usecnt_lock;
196 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
200 ast_mutex_t chan_lock; /* global lock */
201 ast_mutex_t log_lock; /* logging log */
203 * used to indicate change in file descriptors, so select function's result may
206 int global_change = 0;
209 struct lcr_fd wake_fd;
213 int glob_channel = 0;
216 struct lcr_fd socket_fd;
217 struct lcr_timer socket_retry;
220 struct admin_list *next;
221 struct admin_message msg;
222 } *admin_first = NULL;
224 static struct ast_channel_tech lcr_tech;
229 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, ...)
232 char call_text[128] = "NULL";
233 char ast_text[128] = "NULL";
236 ast_mutex_lock(&log_lock);
239 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
240 buffer[sizeof(buffer)-1]=0;
244 sprintf(call_text, "%d", call->ref);
246 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
247 ast_text[sizeof(ast_text)-1] = '\0';
249 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
250 printf("[call=%s ast=%s] %s", call_text, ast_text, buffer);
252 ast_mutex_unlock(&log_lock);
256 * channel and call instances
258 struct chan_call *call_first;
262 * special case: 0: find new ref, that has not been assigned a ref yet
265 struct chan_call *find_call_ref(unsigned int ref)
267 struct chan_call *call = call_first;
268 int assigned = (ref > 0);
271 if (call->ref == ref && call->ref_was_assigned == assigned)
278 void free_call(struct chan_call *call)
280 struct chan_call **temp = &call_first;
284 *temp = (*temp)->next;
285 if (call->pipe[0] > -1)
286 close(call->pipe[0]);
287 if (call->pipe[1] > -1)
288 close(call->pipe[1]);
289 if (call->bchannel) {
290 if (call->bchannel->call != call)
291 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
292 call->bchannel->call = NULL;
294 if (call->bridge_call) {
295 if (call->bridge_call->bridge_call != call)
296 CERROR(call, NULL, "Linked call structure has no link to us.\n");
297 call->bridge_call->bridge_call = NULL;
300 ast_translator_free_path(call->trans);
302 ast_dsp_free(call->dsp);
303 CDEBUG(call, NULL, "Call instance freed.\n");
308 temp = &((*temp)->next);
310 CERROR(call, NULL, "Call instance not found in list.\n");
313 struct chan_call *alloc_call(void)
315 struct chan_call **callp = &call_first;
318 callp = &((*callp)->next);
320 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
322 memset(*callp, 0, sizeof(struct chan_call));
323 if (pipe((*callp)->pipe) < 0) {
324 CERROR(*callp, NULL, "Failed to create pipe.\n");
328 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
329 CDEBUG(*callp, NULL, "Call instance allocated.\n");
333 unsigned short new_bridge_id(void)
335 struct chan_call *call;
336 unsigned short id = 1;
338 /* search for lowest bridge id that is not in use and not 0 */
342 if (call->bridge_id == id)
350 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
355 * enque message to LCR
357 int send_message(int message_type, unsigned int ref, union parameter *param)
359 struct admin_list *admin, **adminp;
362 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
365 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
367 adminp = &admin_first;
369 adminp = &((*adminp)->next);
370 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
372 CERROR(NULL, NULL, "No memory for message to LCR.\n");
377 admin->msg.message = ADMIN_MESSAGE;
378 admin->msg.u.msg.type = message_type;
379 admin->msg.u.msg.ref = ref;
380 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
381 socket_fd.when |= LCR_FD_WRITE;
385 write(wake_pipe[1], &byte, 1);
392 * apply options (in locked state)
394 void apply_opt(struct chan_call *call, char *data)
396 union parameter newparam;
397 char string[1024], *p = string, *opt, *key;
403 strncpy(string, data, sizeof(string)-1);
404 string[sizeof(string)-1] = '\0';
407 while((opt = strsep(&p, ":"))) {
410 if (opt[1] == '\0') {
411 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
414 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
415 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
416 strncpy(call->display, opt+1, sizeof(call->display)-1);
418 memset(&newparam, 0, sizeof(union parameter));
419 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
420 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
424 if (opt[1] != '\0') {
425 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
428 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
429 if (call->dsp_dtmf) {
432 bchannel_dtmf(call->bchannel, 0);
436 if (opt[1] == '\0') {
437 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
441 /* check for 0xXXXX... type of key */
442 if (!!strncmp((char *)key, "0x", 2)) {
443 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
447 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
448 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
453 if (*key>='0' && *key<='9')
454 call->bf_key[i] = (*key-'0') << 8;
455 else if (*key>='a' && *key<='f')
456 call->bf_key[i] = (*key-'a'+10) << 8;
457 else if (*key>='A' && *key<='F')
458 call->bf_key[i] = (*key-'A'+10) << 8;
462 if (*key>='0' && *key<='9')
463 call->bf_key[i] += (*key - '0');
464 else if (*key>='a' && *key<='f')
465 call->bf_key[i] += (*key - 'a' + 10);
466 else if (*key>='A' && *key<='F')
467 call->bf_key[i] += (*key - 'A' + 10);
474 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
478 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
480 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
483 if (opt[1] != '\0') {
484 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
487 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
492 if (opt[1] != '\0') {
493 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
496 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
501 if (opt[1] == '\0') {
502 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
505 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
506 call->nodsp_queue = atoi(opt+1);
509 if (opt[1] == '\0') {
510 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
513 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
514 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
516 bchannel_pipeline(call->bchannel, call->pipeline);
519 if (opt[1] == '\0') {
520 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
523 call->faxdetect=atoi(opt+1);
525 call->dsp=ast_dsp_new();
527 #ifdef LCR_FOR_CALLWEAVER
528 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
530 #ifdef LCR_FOR_ASTERISK
531 #ifdef DSP_FEATURE_DTMF_DETECT
532 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
534 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
539 #ifdef LCR_FOR_CALLWEAVER
540 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
542 #ifdef LCR_FOR_ASTERISK
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
546 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
549 if (opt[1] != '\0') {
550 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
553 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
558 if (opt[1] != '\0') {
559 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
562 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
563 call->inband_dtmf = 1;
566 if (opt[1] != 'r' && opt[1] != 't') {
567 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
571 if (gain < -8 || gain >8) {
572 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
575 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
577 call->rx_gain = gain;
579 bchannel_gain(call->bchannel, call->rx_gain, 0);
581 call->tx_gain = gain;
583 bchannel_gain(call->bchannel, call->tx_gain, 1);
587 if (opt[1] != '\0') {
588 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
591 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
596 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
600 /* re-open, if bchannel is created */
601 if (call->bchannel && call->bchannel->b_sock > -1) {
602 bchannel_destroy(call->bchannel);
603 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
604 bchannel_activate(call->bchannel, 1);
609 * send setup info to LCR
610 * this function is called, when asterisk call is received and ref is received
612 static void send_setup_to_lcr(struct chan_call *call)
614 union parameter newparam;
615 struct ast_channel *ast = call->ast;
618 if (!call->ast || !call->ref)
621 #ifdef AST_1_8_OR_HIGHER
622 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, ast->caller.id.number.str);
624 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
627 /* send setup message to LCR */
628 memset(&newparam, 0, sizeof(union parameter));
629 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
630 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
632 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
634 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
635 if (!!strcmp(call->interface, "pbx"))
636 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
637 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
638 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
639 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
640 call->display[0] = '\0';
642 #ifdef AST_1_8_OR_HIGHER
644 if (ast->caller.id.number.valid) {
645 if (ast->caller.id.number.str)
646 strncpy(newparam.setup.callerinfo.id, ast->caller.id.number.str, sizeof(newparam.setup.callerinfo.id)-1);
647 switch(ast->caller.id.number.presentation & AST_PRES_RESTRICTION) {
648 case AST_PRES_RESTRICTED:
649 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
651 case AST_PRES_UNAVAILABLE:
652 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
654 case AST_PRES_ALLOWED:
656 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
658 switch(ast->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
659 case AST_PRES_USER_NUMBER_UNSCREENED:
660 newparam.setup.callerinfo.screen = INFO_SCREEN_USER;
662 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
663 newparam.setup.callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
665 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
666 newparam.setup.callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
669 newparam.setup.callerinfo.screen = INFO_SCREEN_NETWORK;
671 switch((ast->caller.id.number.plan >> 4) & 7) {
673 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
676 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
679 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
682 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
685 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
688 if (ast->caller.ani.number.valid) {
689 if (ast->caller.ani.number.str)
690 strncpy(newparam.setup.callerinfo.id2, ast->caller.ani.number.str, sizeof(newparam.setup.callerinfo.id2)-1);
691 switch(ast->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
692 case AST_PRES_RESTRICTED:
693 newparam.setup.callerinfo.present2 = INFO_PRESENT_RESTRICTED;
695 case AST_PRES_UNAVAILABLE:
696 newparam.setup.callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
698 case AST_PRES_ALLOWED:
700 newparam.setup.callerinfo.present2 = INFO_PRESENT_ALLOWED;
702 switch(ast->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
703 case AST_PRES_USER_NUMBER_UNSCREENED:
704 newparam.setup.callerinfo.screen2 = INFO_SCREEN_USER;
706 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
707 newparam.setup.callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
709 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
710 newparam.setup.callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
713 newparam.setup.callerinfo.screen2 = INFO_SCREEN_NETWORK;
715 switch((ast->caller.ani.number.plan >> 4) & 7) {
717 newparam.setup.callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
720 newparam.setup.callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
723 newparam.setup.callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
726 newparam.setup.callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
729 newparam.setup.callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
732 if (ast->caller.id.name.valid) {
733 if (ast->caller.id.name.str)
734 strncpy(newparam.setup.callerinfo.name, ast->caller.id.name.str, sizeof(newparam.setup.callerinfo.name)-1);
738 if (ast->redirecting.from.number.valid) {
739 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
740 if (ast->redirecting.from.number.str)
741 strncpy(newparam.setup.redirinfo.id, ast->redirecting.from.number.str, sizeof(newparam.setup.redirinfo.id)-1);
742 switch(ast->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
743 case AST_PRES_RESTRICTED:
744 newparam.setup.redirinfo.present = INFO_PRESENT_RESTRICTED;
746 case AST_PRES_UNAVAILABLE:
747 newparam.setup.redirinfo.present = INFO_PRESENT_NOTAVAIL;
749 case AST_PRES_ALLOWED:
751 newparam.setup.redirinfo.present = INFO_PRESENT_ALLOWED;
753 switch(ast->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
754 case AST_PRES_USER_NUMBER_UNSCREENED:
755 newparam.setup.redirinfo.screen = INFO_SCREEN_USER;
757 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
758 newparam.setup.redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
760 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
761 newparam.setup.redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
764 newparam.setup.redirinfo.screen = INFO_SCREEN_NETWORK;
766 switch((ast->redirecting.from.number.plan >> 4) & 7) {
768 newparam.setup.redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
771 newparam.setup.redirinfo.ntype = INFO_NTYPE_NATIONAL;
774 newparam.setup.redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
777 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
781 if (call->cid_num[0])
782 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
783 if (call->cid_name[0])
784 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
785 if (call->cid_rdnis[0]) {
786 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
787 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
788 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
790 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
791 case AST_PRES_RESTRICTED:
792 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
794 case AST_PRES_UNAVAILABLE:
795 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
797 case AST_PRES_ALLOWED:
799 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
801 switch(ast->cid.cid_ton) {
803 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
806 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
809 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
812 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
815 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
816 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
818 // ast->transfercapability = atoi(tmp);
819 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
820 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
822 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
824 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
825 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
827 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
828 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
829 send_message(MESSAGE_SETUP, call->ref, &newparam);
831 /* change to outgoing setup state */
832 call->state = CHAN_LCR_STATE_OUT_SETUP;
836 * send dialing info to LCR
837 * this function is called, when setup acknowledge is received and dialing
840 static void send_dialque_to_lcr(struct chan_call *call)
842 union parameter newparam;
844 if (!call->ast || !call->ref || !call->dialque[0])
847 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
849 /* send setup message to LCR */
850 memset(&newparam, 0, sizeof(union parameter));
852 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
854 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
855 call->dialque[0] = '\0';
856 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
860 * in case of a bridge, the unsupported message can be forwarded directly
861 * to the remote call.
863 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
867 if (!call->bridge_call) return;
868 CDEBUG(call, NULL, "Sending message due bridging.\n");
869 send_message(message_type, call->bridge_call->ref, param);
873 * send release message to LCR and import bchannel if exported
875 static void send_release_and_import(struct chan_call *call, int cause, int location)
877 union parameter newparam;
879 /* importing channel */
880 if (call->bchannel) {
881 memset(&newparam, 0, sizeof(union parameter));
882 newparam.bchannel.type = BCHANNEL_RELEASE;
883 newparam.bchannel.handle = call->bchannel->handle;
884 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
886 /* sending release */
887 memset(&newparam, 0, sizeof(union parameter));
888 newparam.disconnectinfo.cause = cause;
889 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
890 send_message(MESSAGE_RELEASE, call->ref, &newparam);
894 * check if extension matches and start asterisk
895 * if it can match, proceed
898 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
901 union parameter newparam;
902 char *exten = ast->exten;
906 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
910 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
911 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
915 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
916 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);
920 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
921 /* send setup acknowledge to lcr */
922 memset(&newparam, 0, sizeof(union parameter));
923 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
926 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
931 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
932 /* send setup acknowledge to lcr */
933 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
934 memset(&newparam, 0, sizeof(union parameter));
935 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
939 call->state = CHAN_LCR_STATE_IN_DIALING;
941 /* if match, start pbx */
942 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
943 CDEBUG(call, ast, "Extensions matches.\n");
948 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
954 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
962 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
963 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
965 /* release asterisk */
966 ast->hangupcause = call->cause;
967 /* change to release state */
968 call->state = CHAN_LCR_STATE_RELEASE;
969 ast_hangup(ast); // call will be destroyed here
973 /* send setup to asterisk */
974 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
976 #ifdef LCR_FOR_CALLWEAVER
978 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
981 ret = ast_pbx_start(ast);
983 cause = (ret==-2)?34:27;
986 call->pbx_started = 1;
987 ast_setstate(ast, AST_STATE_RING);
991 * incoming setup from LCR
993 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
995 struct ast_channel *ast;
997 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
999 /* create asterisk channel instrance */
1001 #ifdef LCR_FOR_CALLWEAVER
1002 ast = ast_channel_alloc(1);
1005 #ifdef LCR_FOR_ASTERISK
1006 #ifdef AST_1_8_OR_HIGHER
1007 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
1009 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1015 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
1016 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
1023 ast->tech_pvt = call;
1024 ast->tech = &lcr_tech;
1025 ast->fds[0] = call->pipe[0];
1027 /* fill setup information */
1028 if (param->setup.dialinginfo.id)
1029 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
1030 if (param->setup.context[0])
1031 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
1033 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
1037 #ifdef AST_1_8_OR_HIGHER
1038 if (param->setup.callerinfo.id[0]) {
1039 ast->caller.id.number.valid = 1;
1040 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
1041 if (!param->setup.callerinfo.id[0]) {
1042 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
1043 ast->caller.id.number.plan = (0 << 4) | 1;
1045 switch (param->setup.callerinfo.present) {
1046 case INFO_PRESENT_ALLOWED:
1047 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
1049 case INFO_PRESENT_RESTRICTED:
1050 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
1053 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
1055 switch (param->setup.callerinfo.screen) {
1056 case INFO_SCREEN_USER:
1057 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1059 case INFO_SCREEN_USER_VERIFIED_PASSED:
1060 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1062 case INFO_SCREEN_USER_VERIFIED_FAILED:
1063 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1066 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1068 switch (param->setup.callerinfo.ntype) {
1069 case INFO_NTYPE_SUBSCRIBER:
1070 ast->caller.id.number.plan = (4 << 4) | 1;
1072 case INFO_NTYPE_NATIONAL:
1073 ast->caller.id.number.plan = (2 << 4) | 1;
1075 case INFO_NTYPE_INTERNATIONAL:
1076 ast->caller.id.number.plan = (1 << 4) | 1;
1079 ast->caller.id.number.plan = (0 << 4) | 1;
1082 if (param->setup.callerinfo.id2[0]) {
1083 ast->caller.ani.number.valid = 1;
1084 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
1085 switch (param->setup.callerinfo.present2) {
1086 case INFO_PRESENT_ALLOWED:
1087 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
1089 case INFO_PRESENT_RESTRICTED:
1090 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
1093 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
1095 switch (param->setup.callerinfo.screen2) {
1096 case INFO_SCREEN_USER:
1097 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1099 case INFO_SCREEN_USER_VERIFIED_PASSED:
1100 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1102 case INFO_SCREEN_USER_VERIFIED_FAILED:
1103 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1106 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1108 switch (param->setup.callerinfo.ntype2) {
1109 case INFO_NTYPE_SUBSCRIBER:
1110 ast->caller.ani.number.plan = (4 << 4) | 1;
1112 case INFO_NTYPE_NATIONAL:
1113 ast->caller.ani.number.plan = (2 << 4) | 1;
1115 case INFO_NTYPE_INTERNATIONAL:
1116 ast->caller.ani.number.plan = (1 << 4) | 1;
1119 ast->caller.ani.number.plan = (0 << 4) | 1;
1122 if (param->setup.callerinfo.name[0]) {
1123 ast->caller.id.name.valid = 1;
1124 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
1126 if (param->setup.redirinfo.id[0]) {
1127 ast->redirecting.from.number.valid = 1;
1128 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
1129 switch (param->setup.redirinfo.present) {
1130 case INFO_PRESENT_ALLOWED:
1131 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
1133 case INFO_PRESENT_RESTRICTED:
1134 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1137 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1139 switch (param->setup.redirinfo.screen) {
1140 case INFO_SCREEN_USER:
1141 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1143 case INFO_SCREEN_USER_VERIFIED_PASSED:
1144 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1146 case INFO_SCREEN_USER_VERIFIED_FAILED:
1147 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1150 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1152 switch (param->setup.redirinfo.ntype) {
1153 case INFO_NTYPE_SUBSCRIBER:
1154 ast->redirecting.from.number.plan = (4 << 4) | 1;
1156 case INFO_NTYPE_NATIONAL:
1157 ast->redirecting.from.number.plan = (2 << 4) | 1;
1159 case INFO_NTYPE_INTERNATIONAL:
1160 ast->redirecting.from.number.plan = (1 << 4) | 1;
1163 ast->redirecting.from.number.plan = (0 << 4) | 1;
1167 memset(&ast->cid, 0, sizeof(ast->cid));
1168 if (param->setup.callerinfo.id[0])
1169 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1170 if (param->setup.callerinfo.id2[0])
1171 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1172 if (param->setup.callerinfo.name[0])
1173 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1174 if (param->setup.redirinfo.id[0])
1175 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1176 switch (param->setup.callerinfo.present) {
1177 case INFO_PRESENT_ALLOWED:
1178 ast->cid.cid_pres = AST_PRES_ALLOWED;
1180 case INFO_PRESENT_RESTRICTED:
1181 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1184 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1186 switch (param->setup.callerinfo.ntype) {
1187 case INFO_NTYPE_SUBSCRIBER:
1188 ast->cid.cid_ton = 4;
1190 case INFO_NTYPE_NATIONAL:
1191 ast->cid.cid_ton = 2;
1193 case INFO_NTYPE_INTERNATIONAL:
1194 ast->cid.cid_ton = 1;
1197 ast->cid.cid_ton = 0;
1201 ast->transfercapability = param->setup.capainfo.bearer_capa;
1202 /* enable hdlc if transcap is data */
1203 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1205 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1207 /* configure channel */
1208 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1209 ast->readformat = ast->rawreadformat = ast->nativeformats;
1210 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1212 ast->hangupcause = 0;
1215 call->state = CHAN_LCR_STATE_IN_SETUP;
1217 if (!call->pbx_started)
1218 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1222 * incoming setup acknowledge from LCR
1224 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1226 if (!call->ast) return;
1228 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1230 /* send pending digits in dialque */
1231 if (call->dialque[0])
1232 send_dialque_to_lcr(call);
1233 /* change to overlap state */
1234 call->state = CHAN_LCR_STATE_OUT_DIALING;
1238 * incoming proceeding from LCR
1240 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1242 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1245 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1246 /* queue event for asterisk */
1247 if (call->ast && call->pbx_started) {
1251 write(wake_pipe[1], &byte, 1);
1253 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1259 * incoming alerting from LCR
1261 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1263 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1266 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1267 /* queue event to asterisk */
1268 if (call->ast && call->pbx_started) {
1272 write(wake_pipe[1], &byte, 1);
1274 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1279 * incoming connect from LCR
1281 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1283 union parameter newparam;
1285 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1288 call->state = CHAN_LCR_STATE_CONNECT;
1289 /* request bchannel */
1290 if (!call->bchannel) {
1291 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1292 memset(&newparam, 0, sizeof(union parameter));
1293 newparam.bchannel.type = BCHANNEL_REQUEST;
1294 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1296 /* copy connectinfo */
1297 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1298 /* queue event to asterisk */
1299 if (call->ast && call->pbx_started) {
1303 write(wake_pipe[1], &byte, 1);
1305 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1310 * incoming disconnect from LCR
1312 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1314 struct ast_channel *ast = call->ast;
1316 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1319 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1321 call->cause = param->disconnectinfo.cause;
1322 call->location = param->disconnectinfo.location;
1323 /* if bridge, forward disconnect and return */
1326 if (call->bridge_call) {
1327 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1328 bridge_message_if_bridged(call, message_type, param);
1332 /* release lcr with same cause */
1333 send_release_and_import(call, call->cause, call->location);
1335 /* change to release state */
1336 call->state = CHAN_LCR_STATE_RELEASE;
1337 /* queue release asterisk */
1339 ast->hangupcause = call->cause;
1340 if (call->pbx_started) {
1344 write(wake_pipe[1], &byte, 1);
1346 strcpy(call->queue_string, "H"); // overwrite other indications
1348 ast_hangup(ast); // call will be destroyed here
1354 * incoming release from LCR
1356 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1358 struct ast_channel *ast = call->ast;
1360 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1364 /* change to release state */
1365 call->state = CHAN_LCR_STATE_RELEASE;
1366 /* copy release info */
1368 call->cause = param->disconnectinfo.cause;
1369 call->location = param->disconnectinfo.location;
1371 /* if we have an asterisk instance, queue hangup, else we are done */
1373 ast->hangupcause = call->cause;
1374 if (call->pbx_started) {
1378 write(wake_pipe[1], &byte, 1);
1380 strcpy(call->queue_string, "H");
1382 ast_hangup(ast); // call will be destroyed here
1391 * incoming information from LCR
1393 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1395 struct ast_channel *ast = call->ast;
1397 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1401 /* pbx not started */
1402 if (!call->pbx_started) {
1403 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1404 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1405 lcr_start_pbx(call, ast, param->information.sending_complete);
1409 /* change dailing state after setup */
1410 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1411 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1412 call->state = CHAN_LCR_STATE_IN_DIALING;
1413 // ast_setstate(ast, AST_STATE_DIALING);
1417 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1421 write(wake_pipe[1], &byte, 1);
1423 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1426 /* use bridge to forware message not supported by asterisk */
1427 if (call->state == CHAN_LCR_STATE_CONNECT) {
1428 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1429 bridge_message_if_bridged(call, message_type, param);
1434 * incoming information from LCR
1436 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1438 union parameter newparam;
1440 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1442 /* request bchannel, if call is resumed and we don't have it */
1443 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1444 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1445 memset(&newparam, 0, sizeof(union parameter));
1446 newparam.bchannel.type = BCHANNEL_REQUEST;
1447 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1450 if (!call->ast) return;
1452 /* use bridge to forware message not supported by asterisk */
1453 bridge_message_if_bridged(call, message_type, param);
1457 * incoming information from LCR
1459 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1461 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1463 if (!call->ast) return;
1465 /* use bridge to forware message not supported by asterisk */
1466 bridge_message_if_bridged(call, message_type, param);
1470 * incoming pattern from LCR
1472 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1474 union parameter newparam;
1476 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1478 if (!call->ast) return;
1480 /* pattern are indicated only once */
1481 if (call->has_pattern)
1483 call->has_pattern = 1;
1485 /* request bchannel */
1486 if (!call->bchannel) {
1487 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1488 memset(&newparam, 0, sizeof(union parameter));
1489 newparam.bchannel.type = BCHANNEL_REQUEST;
1490 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1492 /* queue PROGRESS, because tones are available */
1493 if (call->ast && call->pbx_started) {
1497 write(wake_pipe[1], &byte, 1);
1499 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1504 * got dtmf from bchannel (locked state)
1506 void lcr_in_dtmf(struct chan_call *call, int val)
1508 struct ast_channel *ast = call->ast;
1513 if (!call->pbx_started)
1516 if (!call->dsp_dtmf) {
1517 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1521 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1527 write(wake_pipe[1], &byte, 1);
1529 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1533 * message received from LCR
1535 int receive_message(int message_type, unsigned int ref, union parameter *param)
1537 struct bchannel *bchannel;
1538 struct chan_call *call;
1539 union parameter newparam;
1541 memset(&newparam, 0, sizeof(union parameter));
1543 /* handle bchannel message*/
1544 if (message_type == MESSAGE_BCHANNEL) {
1545 switch(param->bchannel.type) {
1546 case BCHANNEL_ASSIGN:
1547 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1548 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1549 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1552 /* create bchannel */
1553 bchannel = alloc_bchannel(param->bchannel.handle);
1555 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1559 /* configure channel */
1560 bchannel->b_tx_gain = param->bchannel.tx_gain;
1561 bchannel->b_rx_gain = param->bchannel.rx_gain;
1562 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1563 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1564 bchannel->b_bf_len = param->bchannel.crypt_len;
1565 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1567 bchannel->b_txdata = 0;
1568 bchannel->b_tx_dejitter = 1;
1570 /* in case, ref is not set, this bchannel instance must
1571 * be created until it is removed again by LCR */
1573 call = find_call_ref(ref);
1575 bchannel->call = call;
1576 call->bchannel = bchannel;
1578 bchannel_dtmf(bchannel, 1);
1580 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1581 if (call->pipeline[0])
1582 bchannel_pipeline(bchannel, call->pipeline);
1584 bchannel_gain(bchannel, call->rx_gain, 0);
1586 bchannel_gain(bchannel, call->tx_gain, 1);
1587 if (call->bridge_id) {
1588 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1589 bchannel_join(bchannel, call->bridge_id);
1591 /* ignore all dsp features, if it is a loopback interface */
1592 if (param->bchannel.isloopback)
1595 /* create only, if call exists, othewhise it bchannel is freed below... */
1596 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1597 bchannel_activate(bchannel, 1);
1600 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1601 newparam.bchannel.handle = param->bchannel.handle;
1602 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1603 /* if call has released before bchannel is assigned */
1605 newparam.bchannel.type = BCHANNEL_RELEASE;
1606 newparam.bchannel.handle = param->bchannel.handle;
1607 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1612 case BCHANNEL_REMOVE:
1613 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1614 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1615 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1618 /* unklink from call and destroy bchannel */
1619 free_bchannel(bchannel);
1622 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1623 newparam.bchannel.handle = param->bchannel.handle;
1624 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1629 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1634 /* handle new ref */
1635 if (message_type == MESSAGE_NEWREF) {
1636 if (param->newref.direction) {
1637 /* new ref from lcr */
1638 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1639 if (!ref || find_call_ref(ref)) {
1640 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1643 /* allocate new call instance */
1644 call = alloc_call();
1646 call->state = CHAN_LCR_STATE_IN_PREPARE;
1649 call->ref_was_assigned = 1;
1650 /* set dtmf (default, use option 'n' to disable */
1652 /* wait for setup (or release from asterisk) */
1654 /* new ref, as requested from this remote application */
1655 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1656 call = find_call_ref(0);
1658 /* send release, if ref does not exist */
1659 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1660 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1665 call->ref_was_assigned = 1;
1666 /* set dtmf (default, use option 'n' to disable */
1668 /* send pending setup info */
1669 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1670 send_setup_to_lcr(call);
1671 /* release if asterisk has signed off */
1672 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1675 send_release_and_import(call, call->cause, call->location);
1677 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1688 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1691 call = find_call_ref(ref);
1693 /* ignore ref that is not used (anymore) */
1694 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1698 /* handle messages */
1699 switch(message_type) {
1701 lcr_in_setup(call, message_type, param);
1704 case MESSAGE_OVERLAP:
1705 lcr_in_overlap(call, message_type, param);
1708 case MESSAGE_PROCEEDING:
1709 lcr_in_proceeding(call, message_type, param);
1712 case MESSAGE_ALERTING:
1713 lcr_in_alerting(call, message_type, param);
1716 case MESSAGE_CONNECT:
1717 lcr_in_connect(call, message_type, param);
1720 case MESSAGE_DISCONNECT:
1721 lcr_in_disconnect(call, message_type, param);
1724 case MESSAGE_RELEASE:
1725 lcr_in_release(call, message_type, param);
1728 case MESSAGE_INFORMATION:
1729 lcr_in_information(call, message_type, param);
1732 case MESSAGE_NOTIFY:
1733 lcr_in_notify(call, message_type, param);
1736 case MESSAGE_FACILITY:
1737 lcr_in_facility(call, message_type, param);
1740 case MESSAGE_PATTERN: // audio available from LCR
1741 if (!call->has_pattern)
1742 lcr_in_pattern(call, message_type, param);
1745 case MESSAGE_NOPATTERN: // audio not available from LCR
1748 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1749 call->audiopath = param->audiopath;
1753 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1760 * release all calls (due to broken socket)
1762 static void release_all_calls(void)
1764 struct chan_call *call;
1769 /* no ast, so we may directly free call */
1771 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1775 /* already in release process */
1776 if (call->state == CHAN_LCR_STATE_RELEASE) {
1780 /* release or queue release */
1782 call->state = CHAN_LCR_STATE_RELEASE;
1783 if (!call->pbx_started) {
1784 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1785 ast_hangup(call->ast); // call will be destroyed here
1788 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1792 write(wake_pipe[1], &byte, 1);
1794 strcpy(call->queue_string, "H");
1798 /* release all bchannels */
1799 while(bchannel_first)
1800 free_bchannel(bchannel_first);
1803 void close_socket(void);
1806 * warning! not thread safe
1807 * returns -1 for socket error, 0 for no work, 1 for work
1809 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1812 struct admin_list *admin;
1813 struct admin_message msg;
1815 if ((what & LCR_FD_READ)) {
1816 /* read from socket */
1817 len = read(lcr_sock, &msg, sizeof(msg));
1819 CERROR(NULL, NULL, "Socket closed.(read)\n");
1821 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1823 release_all_calls();
1824 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1828 if (len != sizeof(msg)) {
1829 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1832 if (msg.message != ADMIN_MESSAGE) {
1833 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1836 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1838 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1843 if ((what & LCR_FD_WRITE)) {
1844 /* write to socket */
1846 socket_fd.when &= ~LCR_FD_WRITE;
1849 admin = admin_first;
1850 len = write(lcr_sock, &admin->msg, sizeof(msg));
1852 CERROR(NULL, NULL, "Socket closed.(write)\n");
1856 if (len != sizeof(msg)) {
1857 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1861 admin_first = admin->next;
1865 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1874 * open and close socket and thread
1876 int open_socket(void)
1879 struct sockaddr_un sock_address;
1880 union parameter param;
1883 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1884 CERROR(NULL, NULL, "Failed to create socket.\n");
1888 /* set socket address and name */
1889 memset(&sock_address, 0, sizeof(sock_address));
1890 sock_address.sun_family = PF_UNIX;
1891 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1893 /* connect socket */
1894 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1897 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1901 /* register socket fd */
1902 memset(&socket_fd, 0, sizeof(socket_fd));
1903 socket_fd.fd = lcr_sock;
1904 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1906 /* enque hello message */
1907 memset(¶m, 0, sizeof(param));
1908 strcpy(param.hello.application, "asterisk");
1909 send_message(MESSAGE_HELLO, 0, ¶m);
1914 void close_socket(void)
1916 struct admin_list *admin, *temp;
1918 unregister_fd(&socket_fd);
1920 /* flush pending messages */
1921 admin = admin_first;
1924 admin = admin->next;
1937 /* sending queue to asterisk */
1938 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1942 read(wake_pipe[0], &byte, 1);
1949 static void handle_queue()
1951 struct chan_call *call;
1952 struct ast_channel *ast;
1953 struct ast_frame fr;
1959 p = call->queue_string;
1962 if (ast_channel_trylock(ast)) {
1963 ast_mutex_unlock(&chan_lock);
1965 ast_mutex_lock(&chan_lock);
1971 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1972 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1975 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1976 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1979 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1980 ast_queue_control(ast, AST_CONTROL_RINGING);
1981 ast_setstate(ast, AST_STATE_RINGING);
1984 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1985 ast_queue_control(ast, AST_CONTROL_ANSWER);
1988 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1989 ast_queue_hangup(ast);
1991 case '1': case '2': case '3': case 'A':
1992 case '4': case '5': case '6': case 'B':
1993 case '7': case '8': case '9': case 'C':
1994 case '*': case '0': case '#': case 'D':
1995 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1996 /* send digit to asterisk */
1997 memset(&fr, 0, sizeof(fr));
1999 #ifdef LCR_FOR_ASTERISK
2000 fr.frametype = AST_FRAME_DTMF_BEGIN;
2003 #ifdef LCR_FOR_CALLWEAVER
2004 fr.frametype = AST_FRAME_DTMF;
2007 #ifdef AST_1_8_OR_HIGHER
2008 fr.subclass.integer = *p;
2012 fr.delivery = ast_tv(0, 0);
2013 ast_queue_frame(ast, &fr);
2015 #ifdef LCR_FOR_ASTERISK
2016 fr.frametype = AST_FRAME_DTMF_END;
2017 ast_queue_frame(ast, &fr);
2022 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
2026 call->queue_string[0] = '\0';
2027 ast_channel_unlock(ast);
2033 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
2035 CDEBUG(NULL, NULL, "Retry to open socket.\n");
2036 if (open_socket() < 0)
2037 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
2042 void lock_chan(void)
2044 ast_mutex_lock(&chan_lock);
2047 void unlock_chan(void)
2049 ast_mutex_unlock(&chan_lock);
2052 /* chan_lcr thread */
2053 static void *chan_thread(void *arg)
2055 if (pipe(wake_pipe) < 0) {
2056 CERROR(NULL, NULL, "Failed to open pipe.\n");
2059 memset(&wake_fd, 0, sizeof(wake_fd));
2060 wake_fd.fd = wake_pipe[0];
2061 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2063 memset(&socket_retry, 0, sizeof(socket_retry));
2064 add_timer(&socket_retry, handle_retry, NULL, 0);
2066 bchannel_pid = getpid();
2068 /* open socket the first time */
2069 handle_retry(NULL, NULL, 0);
2071 ast_mutex_lock(&chan_lock);
2075 select_main(0, &global_change, lock_chan, unlock_chan);
2080 del_timer(&socket_retry);
2082 unregister_fd(&wake_fd);
2083 close(wake_pipe[0]);
2084 close(wake_pipe[1]);
2086 CERROR(NULL, NULL, "Thread exit.\n");
2088 ast_mutex_unlock(&chan_lock);
2094 * new asterisk instance
2097 #ifdef AST_1_8_OR_HIGHER
2098 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2100 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2103 char exten[256], *dial, *interface, *opt;
2104 struct ast_channel *ast;
2105 struct chan_call *call;
2107 ast_mutex_lock(&chan_lock);
2108 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2110 /* if socket is closed */
2112 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2113 ast_mutex_unlock(&chan_lock);
2117 /* create call instance */
2118 call = alloc_call();
2120 /* failed to create instance */
2121 ast_mutex_unlock(&chan_lock);
2125 /* create asterisk channel instrance */
2127 #ifdef LCR_FOR_ASTERISK
2128 #ifdef AST_1_8_OR_HIGHER
2129 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2131 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2135 #ifdef LCR_FOR_CALLWEAVER
2136 ast = ast_channel_alloc(1);
2140 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2142 /* failed to create instance */
2143 ast_mutex_unlock(&chan_lock);
2146 ast->tech = &lcr_tech;
2147 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2148 /* configure channel */
2149 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2150 ast->readformat = ast->rawreadformat = ast->nativeformats;
2151 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2153 ast->hangupcause = 0;
2157 ast->tech_pvt = call;
2158 ast->fds[0] = call->pipe[0];
2159 call->pbx_started = 0;
2161 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2164 * Extract interface, dialstring, options from data.
2167 * <interface>/<dialstring>
2168 * <interface>/<dialstring>/options
2170 strncpy(exten, (char *)data, sizeof(exten)-1);
2171 exten[sizeof(exten)-1] = '\0';
2172 if ((dial = strchr(exten, '/'))) {
2175 if ((opt = strchr(dial, '/')))
2184 strncpy(call->interface, interface, sizeof(call->interface)-1);
2185 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2186 apply_opt(call, (char *)opt);
2188 #ifdef AST_1_8_OR_HIGHER
2189 // clone_variables(requestor, ast);
2191 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2192 if (requestor->caller.ani.number.valid)
2193 if (requestor->caller.ani.number.str)
2194 if (requestor->caller.ani.number.str[0])
2195 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2196 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2197 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2199 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2200 if (requestor->caller.ani.name.valid)
2201 if (requestor->caller.ani.name.str)
2202 if (requestor->caller.ani.name.str[0])
2203 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2204 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2206 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2207 if (requestor->caller.ani.subaddress.valid)
2208 if (requestor->caller.ani.subaddress.str)
2209 if (requestor->caller.ani.subaddress.str[0])
2210 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2211 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2213 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2214 if (requestor->caller.id.number.valid)
2215 if (requestor->caller.id.number.str)
2216 if (requestor->caller.id.number.str[0])
2217 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2218 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2219 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2221 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2222 if (requestor->caller.id.name.valid)
2223 if (requestor->caller.id.name.str)
2224 if (requestor->caller.id.name.str[0])
2225 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2226 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2228 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2229 if (requestor->caller.id.subaddress.valid)
2230 if (requestor->caller.id.subaddress.str)
2231 if (requestor->caller.id.subaddress.str[0])
2232 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2233 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2235 if (requestor->dialed.number.str)
2236 if (requestor->dialed.number.str[0])
2237 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2238 ast->dialed.number.plan= requestor->dialed.number.plan;
2240 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2241 if (requestor->dialed.subaddress.valid)
2242 if (requestor->dialed.subaddress.str)
2243 if (requestor->dialed.subaddress.str[0])
2244 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2245 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2247 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2248 ast->redirecting.count= requestor->redirecting.count;
2249 ast->redirecting.reason= requestor->redirecting.reason;
2251 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2252 if (requestor->redirecting.from.number.valid)
2253 if (requestor->redirecting.from.number.str)
2254 if (requestor->redirecting.from.number.str[0])
2255 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2256 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2257 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2259 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2260 if (requestor->redirecting.to.number.valid)
2261 if (requestor->redirecting.to.number.str)
2262 if (requestor->redirecting.to.number.str[0])
2263 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2264 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2265 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2268 ast_mutex_unlock(&chan_lock);
2273 * call from asterisk
2275 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2277 union parameter newparam;
2278 struct chan_call *call;
2280 ast_mutex_lock(&chan_lock);
2281 call = ast->tech_pvt;
2283 #ifdef LCR_FOR_CALLWEAVER
2285 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
2289 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2290 ast_mutex_unlock(&chan_lock);
2294 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2296 /* pbx process is started */
2297 call->pbx_started = 1;
2298 /* send MESSAGE_NEWREF */
2299 memset(&newparam, 0, sizeof(union parameter));
2300 newparam.newref.direction = 0; /* request from app */
2301 if (!strcmp(call->interface, "pbx"))
2302 newparam.newref.mode = 1;
2303 send_message(MESSAGE_NEWREF, 0, &newparam);
2305 /* set hdlc if capability requires hdlc */
2306 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2307 || ast->transfercapability == INFO_BC_DATARESTRICTED
2308 || ast->transfercapability == INFO_BC_VIDEO)
2310 /* if hdlc is forced by option, we change transcap to data */
2312 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2313 && ast->transfercapability != INFO_BC_DATARESTRICTED
2314 && ast->transfercapability != INFO_BC_VIDEO)
2315 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2317 #ifndef AST_1_8_OR_HIGHER
2318 call->cid_num[0] = 0;
2319 call->cid_name[0] = 0;
2320 call->cid_rdnis[0] = 0;
2322 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2323 strncpy(call->cid_num, ast->cid.cid_num,
2324 sizeof(call->cid_num)-1);
2325 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2326 strncpy(call->cid_name, ast->cid.cid_name,
2327 sizeof(call->cid_name)-1);
2328 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2329 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2330 sizeof(call->cid_rdnis)-1);
2333 ast_mutex_unlock(&chan_lock);
2337 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2339 static const char* dtmf_tones[] = {
2340 "!941+1336/100,!0/100", /* 0 */
2341 "!697+1209/100,!0/100", /* 1 */
2342 "!697+1336/100,!0/100", /* 2 */
2343 "!697+1477/100,!0/100", /* 3 */
2344 "!770+1209/100,!0/100", /* 4 */
2345 "!770+1336/100,!0/100", /* 5 */
2346 "!770+1477/100,!0/100", /* 6 */
2347 "!852+1209/100,!0/100", /* 7 */
2348 "!852+1336/100,!0/100", /* 8 */
2349 "!852+1477/100,!0/100", /* 9 */
2350 "!697+1633/100,!0/100", /* A */
2351 "!770+1633/100,!0/100", /* B */
2352 "!852+1633/100,!0/100", /* C */
2353 "!941+1633/100,!0/100", /* D */
2354 "!941+1209/100,!0/100", /* * */
2355 "!941+1477/100,!0/100" }; /* # */
2357 if (digit >= '0' && digit <='9')
2358 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2359 else if (digit >= 'A' && digit <= 'D')
2360 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2361 else if (digit == '*')
2362 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2363 else if (digit == '#')
2364 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2367 CDEBUG(NULL, ast, "Unable to handle DTMF tone "
2368 "'%c' for '%s'\n", digit, ast->name);
2372 #ifdef LCR_FOR_ASTERISK
2373 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2375 #ifdef LCR_FOR_CALLWEAVER
2376 static int lcr_digit(struct ast_channel *ast, char digit)
2379 struct chan_call *call;
2380 union parameter newparam;
2383 #ifdef LCR_FOR_CALLWEAVER
2384 int inband_dtmf = 0;
2387 /* only pass IA5 number space */
2388 if (digit > 126 || digit < 32)
2391 ast_mutex_lock(&chan_lock);
2392 call = ast->tech_pvt;
2394 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2395 ast_mutex_unlock(&chan_lock);
2399 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2401 /* send information or queue them */
2402 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2403 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2404 memset(&newparam, 0, sizeof(union parameter));
2406 newparam.information.keypad[0] = digit;
2407 newparam.information.keypad[1] = '\0';
2409 newparam.information.id[0] = digit;
2410 newparam.information.id[1] = '\0';
2412 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2415 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2416 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2418 strncat(call->dialque, buf, strlen(call->dialque)-1);
2421 ast_mutex_unlock(&chan_lock);
2423 #ifdef LCR_FOR_ASTERISK
2427 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2429 int inband_dtmf = 0;
2430 struct chan_call *call;
2433 ast_mutex_lock(&chan_lock);
2435 call = ast->tech_pvt;
2439 "Received digit from Asterisk, "
2440 "but no call instance exists.\n");
2441 ast_mutex_unlock(&chan_lock);
2445 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2447 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2451 ast_mutex_unlock(&chan_lock);
2454 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2455 send_digit_to_chan(ast, digit);
2461 static int lcr_answer(struct ast_channel *ast)
2463 union parameter newparam;
2464 struct chan_call *call;
2466 ast_mutex_lock(&chan_lock);
2467 call = ast->tech_pvt;
2469 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2470 ast_mutex_unlock(&chan_lock);
2474 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2476 /* copy connectinfo, if bridged */
2477 if (call->bridge_call)
2478 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2479 /* send connect message to lcr */
2480 if (call->state != CHAN_LCR_STATE_CONNECT) {
2481 memset(&newparam, 0, sizeof(union parameter));
2482 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2483 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2484 call->state = CHAN_LCR_STATE_CONNECT;
2487 /* request bchannel */
2488 if (!call->bchannel) {
2489 CDEBUG(call, ast, "Requesting B-channel.\n");
2490 memset(&newparam, 0, sizeof(union parameter));
2491 newparam.bchannel.type = BCHANNEL_REQUEST;
2492 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2495 // memset(&newparam, 0, sizeof(union parameter));
2496 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2498 ast_mutex_unlock(&chan_lock);
2502 static int lcr_hangup(struct ast_channel *ast)
2504 struct chan_call *call;
2505 pthread_t tid = pthread_self();
2507 if (!pthread_equal(tid, chan_tid)) {
2508 ast_mutex_lock(&chan_lock);
2510 call = ast->tech_pvt;
2512 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2513 if (!pthread_equal(tid, chan_tid)) {
2514 ast_mutex_unlock(&chan_lock);
2519 if (!pthread_equal(tid, chan_tid))
2520 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2522 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2524 /* disconnect asterisk, maybe not required */
2525 ast->tech_pvt = NULL;
2529 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2530 if (ast->hangupcause > 0)
2531 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2533 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2536 if (!pthread_equal(tid, chan_tid)) {
2537 ast_mutex_unlock(&chan_lock);
2541 /* ref is not set, due to prepare setup or release */
2542 if (call->state == CHAN_LCR_STATE_RELEASE) {
2543 /* we get the response to our release */
2544 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2547 /* during prepare, we change to release state */
2548 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2549 call->state = CHAN_LCR_STATE_RELEASE;
2553 if (!pthread_equal(tid, chan_tid)) {
2554 ast_mutex_unlock(&chan_lock);
2559 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2561 struct chan_call *call;
2562 struct ast_frame * f = fr;
2564 #ifdef AST_1_8_OR_HIGHER
2565 if (!f->subclass.integer)
2569 CDEBUG(NULL, ast, "No subclass\n");
2570 #ifdef AST_1_8_OR_HIGHER
2571 if (!(f->subclass.integer & ast->nativeformats)) {
2573 if (!(f->subclass & ast->nativeformats)) {
2576 "Unexpected format. "
2577 "Activating emergency conversion...\n");
2579 #ifdef AST_1_8_OR_HIGHER
2580 ast_set_write_format(ast, f->subclass.integer);
2582 ast_set_write_format(ast, f->subclass);
2584 f = (ast->writetrans) ? ast_translate(
2585 ast->writetrans, fr, 0) : fr;
2588 ast_mutex_lock(&chan_lock);
2589 call = ast->tech_pvt;
2591 ast_mutex_unlock(&chan_lock);
2597 if (call->bchannel && f->samples)
2598 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2599 ast_mutex_unlock(&chan_lock);
2607 static struct ast_frame *lcr_read(struct ast_channel *ast)
2609 struct chan_call *call;
2612 ast_mutex_lock(&chan_lock);
2613 call = ast->tech_pvt;
2615 ast_mutex_unlock(&chan_lock);
2618 if (call->pipe[0] > -1) {
2619 if (call->rebuffer && !call->hdlc) {
2620 /* Make sure we have a complete 20ms (160byte) frame */
2621 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2623 call->framepos += len;
2626 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2628 if (len < 0 && errno == EAGAIN) {
2629 ast_mutex_unlock(&chan_lock);
2631 #ifdef LCR_FOR_ASTERISK
2632 return &ast_null_frame;
2635 #ifdef LCR_FOR_CALLWEAVER
2641 close(call->pipe[0]);
2644 ast_mutex_unlock(&chan_lock);
2646 } else if (call->rebuffer && call->framepos < 160) {
2647 /* Not a complete frame, so we send a null-frame */
2648 ast_mutex_unlock(&chan_lock);
2649 return &ast_null_frame;
2653 call->read_fr.frametype = AST_FRAME_VOICE;
2654 #ifdef AST_1_8_OR_HIGHER
2655 call->read_fr.subclass.integer = ast->nativeformats;
2657 call->read_fr.subclass = ast->nativeformats;
2659 if (call->rebuffer) {
2660 call->read_fr.datalen = call->framepos;
2661 call->read_fr.samples = call->framepos;
2664 call->read_fr.datalen = len;
2665 call->read_fr.samples = len;
2667 call->read_fr.delivery = ast_tv(0,0);
2668 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2669 ast_mutex_unlock(&chan_lock);
2671 return &call->read_fr;
2674 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2676 union parameter newparam;
2678 struct chan_call *call;
2679 const struct tone_zone_sound *ts = NULL;
2681 ast_mutex_lock(&chan_lock);
2682 call = ast->tech_pvt;
2684 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2685 ast_mutex_unlock(&chan_lock);
2690 case AST_CONTROL_BUSY:
2691 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2692 ast_setstate(ast, AST_STATE_BUSY);
2693 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2694 /* send message to lcr */
2695 memset(&newparam, 0, sizeof(union parameter));
2696 newparam.disconnectinfo.cause = 17;
2697 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2698 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2700 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2702 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2703 ts = ast_get_indication_tone(ast->zone, "busy");
2706 case AST_CONTROL_CONGESTION:
2707 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2708 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2709 /* send message to lcr */
2710 memset(&newparam, 0, sizeof(union parameter));
2711 newparam.disconnectinfo.cause = ast->hangupcause;
2712 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2713 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2715 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2717 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2718 ts = ast_get_indication_tone(ast->zone, "congestion");
2721 case AST_CONTROL_PROCEEDING:
2722 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2723 if (call->state == CHAN_LCR_STATE_IN_SETUP
2724 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2725 /* send message to lcr */
2726 memset(&newparam, 0, sizeof(union parameter));
2727 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2729 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2732 case AST_CONTROL_RINGING:
2733 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2734 ast_setstate(ast, AST_STATE_RING);
2735 if (call->state == CHAN_LCR_STATE_IN_SETUP
2736 || call->state == CHAN_LCR_STATE_IN_DIALING
2737 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2738 /* send message to lcr */
2739 memset(&newparam, 0, sizeof(union parameter));
2740 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2742 call->state = CHAN_LCR_STATE_IN_ALERTING;
2744 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2745 ts = ast_get_indication_tone(ast->zone, "ring");
2748 case AST_CONTROL_PROGRESS:
2749 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2750 /* request bchannel */
2751 if (!call->bchannel) {
2752 CDEBUG(call, ast, "Requesting B-channel.\n");
2753 memset(&newparam, 0, sizeof(union parameter));
2754 newparam.bchannel.type = BCHANNEL_REQUEST;
2755 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2759 CDEBUG(call, ast, "Received indicate -1.\n");
2760 ast_playtones_stop(ast);
2764 case AST_CONTROL_VIDUPDATE:
2765 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2768 case AST_CONTROL_HOLD:
2769 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2770 /* send message to lcr */
2771 memset(&newparam, 0, sizeof(union parameter));
2772 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2773 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2775 /*start music onhold*/
2776 #ifdef LCR_FOR_ASTERISK
2777 ast_moh_start(ast,data,ast->musicclass);
2780 #ifdef LCR_FOR_CALLWEAVER
2781 ast_moh_start(ast, NULL);
2786 case AST_CONTROL_UNHOLD:
2787 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2788 /* send message to lcr */
2789 memset(&newparam, 0, sizeof(union parameter));
2790 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2791 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2797 #ifdef AST_CONTROL_SRCUPDATE
2798 case AST_CONTROL_SRCUPDATE:
2802 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2805 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2810 if (ts && ts->data[0]) {
2811 ast_playtones_start(ast, 0, ts->data, 1);
2815 ast_mutex_unlock(&chan_lock);
2822 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2824 struct chan_call *call;
2830 ast_mutex_lock(&chan_lock);
2831 call = ast->tech_pvt;
2833 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2834 ast_mutex_unlock(&chan_lock);
2838 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2840 ast_mutex_unlock(&chan_lock);
2845 * send_text asterisk
2847 static int lcr_send_text(struct ast_channel *ast, const char *text)
2849 struct chan_call *call;
2850 union parameter newparam;
2852 ast_mutex_lock(&chan_lock);
2853 call = ast->tech_pvt;
2855 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2856 ast_mutex_unlock(&chan_lock);
2860 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2861 memset(&newparam, 0, sizeof(union parameter));
2862 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2863 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2864 ast_mutex_unlock(&chan_lock);
2871 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2872 struct ast_channel *ast2, int flags,
2873 struct ast_frame **fo,
2874 struct ast_channel **rc, int timeoutms)
2877 struct chan_call *call1, *call2;
2878 struct ast_channel *carr[2], *who;
2880 struct ast_frame *f;
2883 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2888 /* join via dsp (if the channels are currently open) */
2889 ast_mutex_lock(&chan_lock);
2890 call1 = ast1->tech_pvt;
2891 call2 = ast2->tech_pvt;
2892 if (!call1 || !call2) {
2893 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2894 ast_mutex_unlock(&chan_lock);
2895 return AST_BRIDGE_COMPLETE;
2898 /* join, if both call instances uses dsp
2899 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2901 if (!call1->nodsp && !call2->nodsp) {
2902 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2904 /* get bridge id and join */
2905 bridge_id = new_bridge_id();
2907 call1->bridge_id = bridge_id;
2908 if (call1->bchannel)
2909 bchannel_join(call1->bchannel, bridge_id);
2911 call2->bridge_id = bridge_id;
2912 if (call2->bchannel)
2913 bchannel_join(call2->bchannel, bridge_id);
2915 if (call1->nodsp && call2->nodsp)
2916 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2918 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2919 call1->bridge_call = call2;
2920 call2->bridge_call = call1;
2922 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2923 || call1->state == CHAN_LCR_STATE_IN_DIALING
2924 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2925 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2926 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2929 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2930 || call2->state == CHAN_LCR_STATE_IN_DIALING
2931 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2932 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2933 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2937 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2938 so let's do it for them. Hmpf.
2941 if (call1->on_hold) {
2942 union parameter newparam;
2944 memset(&newparam, 0, sizeof(union parameter));
2945 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2946 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2951 if (call2->on_hold) {
2952 union parameter newparam;
2954 memset(&newparam, 0, sizeof(union parameter));
2955 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2956 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2961 ast_mutex_unlock(&chan_lock);
2965 who = ast_waitfor_n(carr, 2, &to);
2968 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2973 if (!f || f->frametype == AST_FRAME_CONTROL) {
2975 CDEBUG(NULL, NULL, "Got hangup.\n");
2977 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2984 if ( f->frametype == AST_FRAME_DTMF ) {
2985 CDEBUG(NULL, NULL, "Got DTMF.\n");
3001 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3003 /* split channels */
3004 ast_mutex_lock(&chan_lock);
3005 call1 = ast1->tech_pvt;
3006 call2 = ast2->tech_pvt;
3007 if (call1 && call1->bridge_id) {
3008 call1->bridge_id = 0;
3009 if (call1->bchannel)
3010 bchannel_join(call1->bchannel, 0);
3011 if (call1->bridge_call)
3012 call1->bridge_call->bridge_call = NULL;
3014 if (call2 && call1->bridge_id) {
3015 call2->bridge_id = 0;
3016 if (call2->bchannel)
3017 bchannel_join(call2->bchannel, 0);
3018 if (call2->bridge_call)
3019 call2->bridge_call->bridge_call = NULL;
3021 call1->bridge_call = NULL;
3022 call2->bridge_call = NULL;
3024 ast_mutex_unlock(&chan_lock);
3025 return AST_BRIDGE_COMPLETE;
3027 static struct ast_channel_tech lcr_tech = {
3029 .description = "Channel driver for connecting to Linux-Call-Router",
3030 .capabilities = AST_FORMAT_ALAW,
3031 .requester = lcr_request,
3033 #ifdef LCR_FOR_ASTERISK
3034 .send_digit_begin = lcr_digit_begin,
3035 .send_digit_end = lcr_digit_end,
3038 #ifdef LCR_FOR_CALLWEAVER
3039 .send_digit = lcr_digit,
3043 .bridge = lcr_bridge,
3044 .hangup = lcr_hangup,
3045 .answer = lcr_answer,
3048 .indicate = lcr_indicate,
3050 .send_text = lcr_send_text,
3059 static int lcr_show_lcr (int fd, int argc, char *argv[])
3064 static int lcr_show_calls (int fd, int argc, char *argv[])
3069 static int lcr_reload_routing (int fd, int argc, char *argv[])
3074 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3079 static int lcr_port_block (int fd, int argc, char *argv[])
3084 static int lcr_port_unblock (int fd, int argc, char *argv[])
3089 static int lcr_port_unload (int fd, int argc, char *argv[])
3094 static struct ast_cli_entry cli_show_lcr =
3095 { {"lcr", "show", "lcr", NULL},
3097 "Shows current states of LCR core",
3098 "Usage: lcr show lcr\n",
3101 static struct ast_cli_entry cli_show_calls =
3102 { {"lcr", "show", "calls", NULL},
3104 "Shows current calls made by LCR and Asterisk",
3105 "Usage: lcr show calls\n",
3108 static struct ast_cli_entry cli_reload_routing =
3109 { {"lcr", "reload", "routing", NULL},
3111 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3112 "Usage: lcr reload routing\n",
3115 static struct ast_cli_entry cli_reload_interfaces =
3116 { {"lcr", "reload", "interfaces", NULL},
3117 lcr_reload_interfaces,
3118 "Reloads interfaces conf of LCR",
3119 "Usage: lcr reload interfaces\n",
3122 static struct ast_cli_entry cli_port_block =
3123 { {"lcr", "port", "block", NULL},
3125 "Blocks LCR port for further calls",
3126 "Usage: lcr port block \"<port>\"\n",
3129 static struct ast_cli_entry cli_port_unblock =
3130 { {"lcr", "port", "unblock", NULL},
3132 "Unblocks or loads LCR port, port is opened my mISDN",
3133 "Usage: lcr port unblock \"<port>\"\n",
3136 static struct ast_cli_entry cli_port_unload =
3137 { {"lcr", "port", "unload", NULL},
3139 "Unloads LCR port, port is closes by mISDN",
3140 "Usage: lcr port unload \"<port>\"\n",
3145 #ifdef LCR_FOR_ASTERISK
3146 #ifdef AST_1_8_OR_HIGHER
3147 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3149 static int lcr_config_exec(struct ast_channel *ast, void *data)
3153 #ifdef LCR_FOR_CALLWEAVER
3154 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3157 struct chan_call *call;
3159 ast_mutex_lock(&chan_lock);
3161 #ifdef LCR_FOR_ASTERISK
3162 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3165 #ifdef LCR_FOR_CALLWEAVER
3166 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3172 if (call->ast == ast)
3178 #ifdef LCR_FOR_ASTERISK
3179 apply_opt(call, (char *)data);
3182 #ifdef LCR_FOR_CALLWEAVER
3183 apply_opt(call, (char *)argv[0]);
3187 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3189 ast_mutex_unlock(&chan_lock);
3194 * module loading and destruction
3196 int load_module(void)
3199 char options_error[256];
3201 for (i = 0; i < 256; i++) {
3202 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3203 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3206 if (read_options(options_error) == 0) {
3207 CERROR(NULL, NULL, "%s", options_error);
3209 #ifdef LCR_FOR_ASTERISK
3210 return AST_MODULE_LOAD_DECLINE;
3213 #ifdef LCR_FOR_CALLWEAVER
3219 ast_mutex_init(&chan_lock);
3220 ast_mutex_init(&log_lock);
3222 if (bchannel_initialize()) {
3223 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3226 #ifdef LCR_FOR_ASTERISK
3227 return AST_MODULE_LOAD_DECLINE;
3230 #ifdef LCR_FOR_CALLWEAVER
3236 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3237 if (ast_channel_register(&lcr_tech)) {
3238 CERROR(NULL, NULL, "Unable to register channel class\n");
3239 bchannel_deinitialize();
3242 #ifdef LCR_FOR_ASTERISK
3243 return AST_MODULE_LOAD_DECLINE;
3246 #ifdef LCR_FOR_CALLWEAVER
3251 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3253 #ifdef LCR_FOR_ASTERISK
3254 "lcr_config(<opt><optarg>:<opt>:...)\n"
3257 #ifdef LCR_FOR_CALLWEAVER
3258 "lcr_config(<opt><optarg>:<opt>:...)\n",
3261 "Sets LCR opts. and optargs\n"
3263 "The available options are:\n"
3264 " d - Send display text on called phone, text is the optarg.\n"
3265 " n - Don't detect dtmf tones on called channel.\n"
3266 " h - Force data call (HDLC).\n"
3267 " t - Disable mISDN_dsp features (required for fax application).\n"
3268 " q - Add queue to make fax stream seamless (required for fax app).\n"
3269 " Use queue size in miliseconds for optarg. (try 250)\n"
3270 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3271 " Use time to detect for optarg.\n"
3272 " c - Make crypted outgoing call, optarg is keyindex.\n"
3273 " e - Perform echo cancelation on this channel.\n"
3274 " Takes mISDN pipeline option as optarg.\n"
3275 " s - Send Non Inband DTMF as inband.\n"
3276 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3277 " vr - rxgain control\n"
3278 " vt - txgain control\n"
3279 " Volume changes at factor 2 ^ optarg.\n"
3280 " k - use keypad to dial this call.\n"
3282 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3283 " -> use 16 for fax (3.1k audio)\n"
3285 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3286 "options: \"n:t:q250\" for seamless audio transmission.\n"
3291 ast_cli_register(&cli_show_lcr);
3292 ast_cli_register(&cli_show_calls);
3293 ast_cli_register(&cli_reload_routing);
3294 ast_cli_register(&cli_reload_interfaces);
3295 ast_cli_register(&cli_port_block);
3296 ast_cli_register(&cli_port_unblock);
3297 ast_cli_register(&cli_port_unload);
3301 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3302 /* failed to create thread */
3303 bchannel_deinitialize();
3305 ast_channel_unregister(&lcr_tech);
3307 #ifdef LCR_FOR_ASTERISK
3308 return AST_MODULE_LOAD_DECLINE;
3311 #ifdef LCR_FOR_CALLWEAVER
3319 int unload_module(void)
3321 /* First, take us out of the channel loop */
3322 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
3325 pthread_join(chan_tid, NULL);
3327 ast_channel_unregister(&lcr_tech);
3329 ast_unregister_application("lcr_config");
3332 if (mISDN_created) {
3333 bchannel_deinitialize();
3337 if (lcr_sock >= 0) {
3345 int reload_module(void)
3351 #ifdef LCR_FOR_ASTERISK
3352 #define AST_MODULE "chan_lcr"
3355 #ifdef LCR_FOR_CALLWEAVER
3360 ast_mutex_lock(&usecnt_lock);
3362 ast_mutex_unlock(&usecnt_lock);
3367 #ifdef LCR_FOR_ASTERISK
3368 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3369 .load = load_module,
3370 .unload = unload_module,
3371 .reload = reload_module,
3375 #ifdef LCR_FOR_CALLWEAVER
3376 char *description(void)