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;
211 int glob_channel = 0;
214 struct lcr_fd socket_fd;
215 struct lcr_timer socket_retry;
218 struct admin_list *next;
219 struct admin_message msg;
220 } *admin_first = NULL;
222 static struct ast_channel_tech lcr_tech;
227 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, ...)
230 char call_text[128] = "NULL";
231 char ast_text[128] = "NULL";
234 ast_mutex_lock(&log_lock);
237 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
238 buffer[sizeof(buffer)-1]=0;
242 sprintf(call_text, "%d", call->ref);
244 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
245 ast_text[sizeof(ast_text)-1] = '\0';
247 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
248 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
250 ast_mutex_unlock(&log_lock);
254 * channel and call instances
256 struct chan_call *call_first;
260 * special case: 0: find new ref, that has not been assigned a ref yet
263 struct chan_call *find_call_ref(unsigned int ref)
265 struct chan_call *call = call_first;
266 int assigned = (ref > 0);
269 if (call->ref == ref && call->ref_was_assigned == assigned)
276 void free_call(struct chan_call *call)
278 struct chan_call **temp = &call_first;
282 *temp = (*temp)->next;
283 if (call->pipe[0] > -1)
284 close(call->pipe[0]);
285 if (call->pipe[1] > -1)
286 close(call->pipe[1]);
287 if (call->bchannel) {
288 if (call->bchannel->call != call)
289 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
290 call->bchannel->call = NULL;
292 if (call->bridge_call) {
293 if (call->bridge_call->bridge_call != call)
294 CERROR(call, NULL, "Linked call structure has no link to us.\n");
295 call->bridge_call->bridge_call = NULL;
298 ast_translator_free_path(call->trans);
300 ast_dsp_free(call->dsp);
301 CDEBUG(call, NULL, "Call instance freed.\n");
306 temp = &((*temp)->next);
308 CERROR(call, NULL, "Call instance not found in list.\n");
311 struct chan_call *alloc_call(void)
313 struct chan_call **callp = &call_first;
316 callp = &((*callp)->next);
318 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
320 memset(*callp, 0, sizeof(struct chan_call));
321 if (pipe((*callp)->pipe) < 0) {
322 CERROR(*callp, NULL, "Failed to create pipe.\n");
326 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
327 CDEBUG(*callp, NULL, "Call instance allocated.\n");
331 unsigned short new_bridge_id(void)
333 struct chan_call *call;
334 unsigned short id = 1;
336 /* search for lowest bridge id that is not in use and not 0 */
340 if (call->bridge_id == id)
348 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
353 * enque message to LCR
355 int send_message(int message_type, unsigned int ref, union parameter *param)
357 struct admin_list *admin, **adminp;
360 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
363 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
365 adminp = &admin_first;
367 adminp = &((*adminp)->next);
368 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
370 CERROR(NULL, NULL, "No memory for message to LCR.\n");
375 admin->msg.message = ADMIN_MESSAGE;
376 admin->msg.u.msg.type = message_type;
377 admin->msg.u.msg.ref = ref;
378 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
379 socket_fd.when |= LCR_FD_WRITE;
383 write(wake_pipe[1], &byte, 1);
390 * apply options (in locked state)
392 void apply_opt(struct chan_call *call, char *data)
394 union parameter newparam;
395 char string[1024], *p = string, *opt, *key;
401 strncpy(string, data, sizeof(string)-1);
402 string[sizeof(string)-1] = '\0';
405 while((opt = strsep(&p, ":"))) {
408 if (opt[1] == '\0') {
409 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
412 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
413 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
414 strncpy(call->display, opt+1, sizeof(call->display)-1);
416 memset(&newparam, 0, sizeof(union parameter));
417 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
418 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
422 if (opt[1] != '\0') {
423 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
426 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
427 if (call->dsp_dtmf) {
430 bchannel_dtmf(call->bchannel, 0);
434 if (opt[1] == '\0') {
435 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
439 /* check for 0xXXXX... type of key */
440 if (!!strncmp((char *)key, "0x", 2)) {
441 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
445 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
451 if (*key>='0' && *key<='9')
452 call->bf_key[i] = (*key-'0') << 8;
453 else if (*key>='a' && *key<='f')
454 call->bf_key[i] = (*key-'a'+10) << 8;
455 else if (*key>='A' && *key<='F')
456 call->bf_key[i] = (*key-'A'+10) << 8;
460 if (*key>='0' && *key<='9')
461 call->bf_key[i] += (*key - '0');
462 else if (*key>='a' && *key<='f')
463 call->bf_key[i] += (*key - 'a' + 10);
464 else if (*key>='A' && *key<='F')
465 call->bf_key[i] += (*key - 'A' + 10);
472 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
476 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
478 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
481 if (opt[1] != '\0') {
482 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
485 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
490 if (opt[1] != '\0') {
491 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
494 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
499 if (opt[1] == '\0') {
500 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
503 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
504 call->nodsp_queue = atoi(opt+1);
507 if (opt[1] == '\0') {
508 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
511 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
512 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
514 bchannel_pipeline(call->bchannel, call->pipeline);
517 if (opt[1] == '\0') {
518 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
521 call->faxdetect=atoi(opt+1);
523 call->dsp=ast_dsp_new();
525 #ifdef LCR_FOR_CALLWEAVER
526 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
528 #ifdef LCR_FOR_ASTERISK
529 #ifdef DSP_FEATURE_DTMF_DETECT
530 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
532 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
537 #ifdef LCR_FOR_CALLWEAVER
538 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
540 #ifdef LCR_FOR_ASTERISK
541 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
544 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
547 if (opt[1] != '\0') {
548 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
551 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
556 if (opt[1] != '\0') {
557 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
560 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
561 call->inband_dtmf = 1;
564 if (opt[1] != 'r' && opt[1] != 't') {
565 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
569 if (gain < -8 || gain >8) {
570 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
573 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
575 call->rx_gain = gain;
577 bchannel_gain(call->bchannel, call->rx_gain, 0);
579 call->tx_gain = gain;
581 bchannel_gain(call->bchannel, call->tx_gain, 1);
585 if (opt[1] != '\0') {
586 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
589 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
594 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
598 /* re-open, if bchannel is created */
599 if (call->bchannel && call->bchannel->b_sock > -1) {
600 bchannel_destroy(call->bchannel);
601 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
602 bchannel_activate(call->bchannel, 1);
607 * send setup info to LCR
608 * this function is called, when asterisk call is received and ref is received
610 static void send_setup_to_lcr(struct chan_call *call)
612 union parameter newparam;
613 struct ast_channel *ast = call->ast;
616 if (!call->ast || !call->ref)
619 #ifdef AST_1_8_OR_HIGHER
620 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
622 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
625 /* send setup message to LCR */
626 memset(&newparam, 0, sizeof(union parameter));
627 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
628 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
630 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
632 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
633 if (!!strcmp(call->interface, "pbx"))
634 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
635 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
636 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
637 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
638 call->display[0] = '\0';
640 #ifdef AST_1_8_OR_HIGHER
641 /* set stored call information */
642 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
643 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
645 if (call->cid_num[0])
646 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
647 if (call->cid_name[0])
648 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
649 if (call->cid_rdnis[0]) {
650 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
651 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
652 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
654 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
655 case AST_PRES_RESTRICTED:
656 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
658 case AST_PRES_UNAVAILABLE:
659 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
661 case AST_PRES_ALLOWED:
663 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
665 switch(ast->cid.cid_ton) {
667 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
670 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
673 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
676 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
679 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
680 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
682 // ast->transfercapability = atoi(tmp);
683 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
684 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
686 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
688 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
689 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
691 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
692 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
693 send_message(MESSAGE_SETUP, call->ref, &newparam);
695 /* change to outgoing setup state */
696 call->state = CHAN_LCR_STATE_OUT_SETUP;
700 * send dialing info to LCR
701 * this function is called, when setup acknowledge is received and dialing
704 static void send_dialque_to_lcr(struct chan_call *call)
706 union parameter newparam;
708 if (!call->ast || !call->ref || !call->dialque[0])
711 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
713 /* send setup message to LCR */
714 memset(&newparam, 0, sizeof(union parameter));
716 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
718 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
719 call->dialque[0] = '\0';
720 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
724 * in case of a bridge, the unsupported message can be forwarded directly
725 * to the remote call.
727 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
731 if (!call->bridge_call) return;
732 CDEBUG(call, NULL, "Sending message due bridging.\n");
733 send_message(message_type, call->bridge_call->ref, param);
737 * send release message to LCR and import bchannel if exported
739 static void send_release_and_import(struct chan_call *call, int cause, int location)
741 union parameter newparam;
743 /* importing channel */
744 if (call->bchannel) {
745 memset(&newparam, 0, sizeof(union parameter));
746 newparam.bchannel.type = BCHANNEL_RELEASE;
747 newparam.bchannel.handle = call->bchannel->handle;
748 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
750 /* sending release */
751 memset(&newparam, 0, sizeof(union parameter));
752 newparam.disconnectinfo.cause = cause;
753 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
754 send_message(MESSAGE_RELEASE, call->ref, &newparam);
758 * check if extension matches and start asterisk
759 * if it can match, proceed
762 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
765 union parameter newparam;
766 char *exten = ast->exten;
770 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
774 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
775 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
779 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
780 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);
784 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
785 /* send setup acknowledge to lcr */
786 memset(&newparam, 0, sizeof(union parameter));
787 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
790 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
795 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
796 /* send setup acknowledge to lcr */
797 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
798 memset(&newparam, 0, sizeof(union parameter));
799 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
803 call->state = CHAN_LCR_STATE_IN_DIALING;
805 /* if match, start pbx */
806 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
807 CDEBUG(call, ast, "Extensions matches.\n");
812 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
818 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
826 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
827 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
829 /* release asterisk */
830 ast->hangupcause = call->cause;
831 /* change to release state */
832 call->state = CHAN_LCR_STATE_RELEASE;
833 ast_hangup(ast); // call will be destroyed here
837 /* send setup to asterisk */
838 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
840 #ifdef LCR_FOR_CALLWEAVER
842 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
845 ret = ast_pbx_start(ast);
847 cause = (ret==-2)?34:27;
850 call->pbx_started = 1;
851 ast_setstate(ast, AST_STATE_RING);
855 * incoming setup from LCR
857 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
859 struct ast_channel *ast;
861 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
863 /* create asterisk channel instrance */
865 #ifdef LCR_FOR_CALLWEAVER
866 ast = ast_channel_alloc(1);
869 #ifdef LCR_FOR_ASTERISK
870 #ifdef AST_1_8_OR_HIGHER
871 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
873 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
879 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
880 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
887 ast->tech_pvt = call;
888 ast->tech = &lcr_tech;
889 ast->fds[0] = call->pipe[0];
891 /* fill setup information */
892 if (param->setup.dialinginfo.id)
893 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
894 if (param->setup.context[0])
895 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
897 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
901 #ifdef AST_1_8_OR_HIGHER
902 if (param->setup.callerinfo.id[0]) {
903 ast->caller.id.number.valid = 1;
904 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
905 if (!param->setup.callerinfo.id[0]) {
906 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
907 ast->caller.id.number.plan = (0 << 4) | 1;
909 switch (param->setup.callerinfo.present) {
910 case INFO_PRESENT_ALLOWED:
911 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
913 case INFO_PRESENT_RESTRICTED:
914 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
917 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
919 switch (param->setup.callerinfo.screen) {
920 case INFO_SCREEN_USER:
921 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
923 case INFO_SCREEN_USER_VERIFIED_PASSED:
924 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
926 case INFO_SCREEN_USER_VERIFIED_FAILED:
927 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
930 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
932 switch (param->setup.callerinfo.ntype) {
933 case INFO_NTYPE_SUBSCRIBER:
934 ast->caller.id.number.plan = (4 << 4) | 1;
936 case INFO_NTYPE_NATIONAL:
937 ast->caller.id.number.plan = (2 << 4) | 1;
939 case INFO_NTYPE_INTERNATIONAL:
940 ast->caller.id.number.plan = (1 << 4) | 1;
943 ast->caller.id.number.plan = (0 << 4) | 1;
946 if (param->setup.callerinfo.id2[0]) {
947 ast->caller.ani.number.valid = 1;
948 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
949 switch (param->setup.callerinfo.present2) {
950 case INFO_PRESENT_ALLOWED:
951 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
953 case INFO_PRESENT_RESTRICTED:
954 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
957 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
959 switch (param->setup.callerinfo.screen2) {
960 case INFO_SCREEN_USER:
961 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
963 case INFO_SCREEN_USER_VERIFIED_PASSED:
964 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
966 case INFO_SCREEN_USER_VERIFIED_FAILED:
967 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
970 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
972 switch (param->setup.callerinfo.ntype2) {
973 case INFO_NTYPE_SUBSCRIBER:
974 ast->caller.ani.number.plan = (4 << 4) | 1;
976 case INFO_NTYPE_NATIONAL:
977 ast->caller.ani.number.plan = (2 << 4) | 1;
979 case INFO_NTYPE_INTERNATIONAL:
980 ast->caller.ani.number.plan = (1 << 4) | 1;
983 ast->caller.ani.number.plan = (0 << 4) | 1;
986 if (param->setup.callerinfo.name[0]) {
987 ast->caller.id.name.valid = 1;
988 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
990 if (param->setup.redirinfo.id[0]) {
991 ast->redirecting.from.number.valid = 1;
992 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
993 switch (param->setup.redirinfo.present) {
994 case INFO_PRESENT_ALLOWED:
995 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
997 case INFO_PRESENT_RESTRICTED:
998 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1001 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1003 switch (param->setup.redirinfo.screen) {
1004 case INFO_SCREEN_USER:
1005 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1007 case INFO_SCREEN_USER_VERIFIED_PASSED:
1008 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1010 case INFO_SCREEN_USER_VERIFIED_FAILED:
1011 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1014 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1016 switch (param->setup.redirinfo.ntype) {
1017 case INFO_NTYPE_SUBSCRIBER:
1018 ast->redirecting.from.number.plan = (4 << 4) | 1;
1020 case INFO_NTYPE_NATIONAL:
1021 ast->redirecting.from.number.plan = (2 << 4) | 1;
1023 case INFO_NTYPE_INTERNATIONAL:
1024 ast->redirecting.from.number.plan = (1 << 4) | 1;
1027 ast->redirecting.from.number.plan = (0 << 4) | 1;
1031 memset(&ast->cid, 0, sizeof(ast->cid));
1032 if (param->setup.callerinfo.id[0])
1033 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1034 if (param->setup.callerinfo.id2[0])
1035 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1036 if (param->setup.callerinfo.name[0])
1037 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1038 if (param->setup.redirinfo.id[0])
1039 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1040 switch (param->setup.callerinfo.present) {
1041 case INFO_PRESENT_ALLOWED:
1042 ast->cid.cid_pres = AST_PRES_ALLOWED;
1044 case INFO_PRESENT_RESTRICTED:
1045 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1048 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1050 switch (param->setup.callerinfo.ntype) {
1051 case INFO_NTYPE_SUBSCRIBER:
1052 ast->cid.cid_ton = 4;
1054 case INFO_NTYPE_NATIONAL:
1055 ast->cid.cid_ton = 2;
1057 case INFO_NTYPE_INTERNATIONAL:
1058 ast->cid.cid_ton = 1;
1061 ast->cid.cid_ton = 0;
1065 ast->transfercapability = param->setup.capainfo.bearer_capa;
1066 /* enable hdlc if transcap is data */
1067 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1069 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1071 /* configure channel */
1072 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1073 ast->readformat = ast->rawreadformat = ast->nativeformats;
1074 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1076 ast->hangupcause = 0;
1079 call->state = CHAN_LCR_STATE_IN_SETUP;
1081 if (!call->pbx_started)
1082 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1086 * incoming setup acknowledge from LCR
1088 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1090 if (!call->ast) return;
1092 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1094 /* send pending digits in dialque */
1095 if (call->dialque[0])
1096 send_dialque_to_lcr(call);
1097 /* change to overlap state */
1098 call->state = CHAN_LCR_STATE_OUT_DIALING;
1102 * incoming proceeding from LCR
1104 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1106 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1109 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1110 /* queue event for asterisk */
1111 if (call->ast && call->pbx_started) {
1115 write(wake_pipe[1], &byte, 1);
1117 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1123 * incoming alerting from LCR
1125 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1127 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1130 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1131 /* queue event to asterisk */
1132 if (call->ast && call->pbx_started) {
1136 write(wake_pipe[1], &byte, 1);
1138 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1143 * incoming connect from LCR
1145 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1147 union parameter newparam;
1149 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1152 call->state = CHAN_LCR_STATE_CONNECT;
1153 /* request bchannel */
1154 if (!call->bchannel) {
1155 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1156 memset(&newparam, 0, sizeof(union parameter));
1157 newparam.bchannel.type = BCHANNEL_REQUEST;
1158 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1160 /* copy connectinfo */
1161 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1162 /* queue event to asterisk */
1163 if (call->ast && call->pbx_started) {
1167 write(wake_pipe[1], &byte, 1);
1169 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1174 * incoming disconnect from LCR
1176 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1178 struct ast_channel *ast = call->ast;
1180 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1183 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1185 call->cause = param->disconnectinfo.cause;
1186 call->location = param->disconnectinfo.location;
1187 /* if bridge, forward disconnect and return */
1190 if (call->bridge_call) {
1191 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1192 bridge_message_if_bridged(call, message_type, param);
1196 /* release lcr with same cause */
1197 send_release_and_import(call, call->cause, call->location);
1199 /* change to release state */
1200 call->state = CHAN_LCR_STATE_RELEASE;
1201 /* queue release asterisk */
1203 ast->hangupcause = call->cause;
1204 if (call->pbx_started) {
1208 write(wake_pipe[1], &byte, 1);
1210 strcpy(call->queue_string, "H"); // overwrite other indications
1212 ast_hangup(ast); // call will be destroyed here
1218 * incoming release from LCR
1220 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1222 struct ast_channel *ast = call->ast;
1224 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1228 /* change to release state */
1229 call->state = CHAN_LCR_STATE_RELEASE;
1230 /* copy release info */
1232 call->cause = param->disconnectinfo.cause;
1233 call->location = param->disconnectinfo.location;
1235 /* if we have an asterisk instance, queue hangup, else we are done */
1237 ast->hangupcause = call->cause;
1238 if (call->pbx_started) {
1242 write(wake_pipe[1], &byte, 1);
1244 strcpy(call->queue_string, "H");
1246 ast_hangup(ast); // call will be destroyed here
1255 * incoming information from LCR
1257 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1259 struct ast_channel *ast = call->ast;
1261 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1265 /* pbx not started */
1266 if (!call->pbx_started) {
1267 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1268 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1269 lcr_start_pbx(call, ast, param->information.sending_complete);
1273 /* change dailing state after setup */
1274 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1275 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1276 call->state = CHAN_LCR_STATE_IN_DIALING;
1277 // ast_setstate(ast, AST_STATE_DIALING);
1281 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1285 write(wake_pipe[1], &byte, 1);
1287 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1290 /* use bridge to forware message not supported by asterisk */
1291 if (call->state == CHAN_LCR_STATE_CONNECT) {
1292 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1293 bridge_message_if_bridged(call, message_type, param);
1298 * incoming information from LCR
1300 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1302 union parameter newparam;
1304 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1306 /* request bchannel, if call is resumed and we don't have it */
1307 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1308 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1309 memset(&newparam, 0, sizeof(union parameter));
1310 newparam.bchannel.type = BCHANNEL_REQUEST;
1311 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1314 if (!call->ast) return;
1316 /* use bridge to forware message not supported by asterisk */
1317 bridge_message_if_bridged(call, message_type, param);
1321 * incoming information from LCR
1323 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1325 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1327 if (!call->ast) return;
1329 /* use bridge to forware message not supported by asterisk */
1330 bridge_message_if_bridged(call, message_type, param);
1334 * incoming pattern from LCR
1336 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1338 union parameter newparam;
1340 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1342 if (!call->ast) return;
1344 /* pattern are indicated only once */
1345 if (call->has_pattern)
1347 call->has_pattern = 1;
1349 /* request bchannel */
1350 if (!call->bchannel) {
1351 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1352 memset(&newparam, 0, sizeof(union parameter));
1353 newparam.bchannel.type = BCHANNEL_REQUEST;
1354 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1356 /* queue PROGRESS, because tones are available */
1357 if (call->ast && call->pbx_started) {
1361 write(wake_pipe[1], &byte, 1);
1363 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1368 * got dtmf from bchannel (locked state)
1370 void lcr_in_dtmf(struct chan_call *call, int val)
1372 struct ast_channel *ast = call->ast;
1377 if (!call->pbx_started)
1380 if (!call->dsp_dtmf) {
1381 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1385 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1391 write(wake_pipe[1], &byte, 1);
1393 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1397 * message received from LCR
1399 int receive_message(int message_type, unsigned int ref, union parameter *param)
1401 struct bchannel *bchannel;
1402 struct chan_call *call;
1403 union parameter newparam;
1405 memset(&newparam, 0, sizeof(union parameter));
1407 /* handle bchannel message*/
1408 if (message_type == MESSAGE_BCHANNEL) {
1409 switch(param->bchannel.type) {
1410 case BCHANNEL_ASSIGN:
1411 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1412 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1413 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1416 /* create bchannel */
1417 bchannel = alloc_bchannel(param->bchannel.handle);
1419 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1423 /* configure channel */
1424 bchannel->b_tx_gain = param->bchannel.tx_gain;
1425 bchannel->b_rx_gain = param->bchannel.rx_gain;
1426 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1427 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1428 bchannel->b_bf_len = param->bchannel.crypt_len;
1429 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1431 bchannel->b_txdata = 0;
1432 bchannel->b_tx_dejitter = 1;
1434 /* in case, ref is not set, this bchannel instance must
1435 * be created until it is removed again by LCR */
1437 call = find_call_ref(ref);
1439 bchannel->call = call;
1440 call->bchannel = bchannel;
1442 bchannel_dtmf(bchannel, 1);
1444 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1445 if (call->pipeline[0])
1446 bchannel_pipeline(bchannel, call->pipeline);
1448 bchannel_gain(bchannel, call->rx_gain, 0);
1450 bchannel_gain(bchannel, call->tx_gain, 1);
1451 if (call->bridge_id) {
1452 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1453 bchannel_join(bchannel, call->bridge_id);
1455 /* ignore all dsp features, if it is a loopback interface */
1456 if (param->bchannel.isloopback)
1459 /* create only, if call exists, othewhise it bchannel is freed below... */
1460 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1461 bchannel_activate(bchannel, 1);
1464 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1465 newparam.bchannel.handle = param->bchannel.handle;
1466 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1467 /* if call has released before bchannel is assigned */
1469 newparam.bchannel.type = BCHANNEL_RELEASE;
1470 newparam.bchannel.handle = param->bchannel.handle;
1471 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1476 case BCHANNEL_REMOVE:
1477 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1478 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1479 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1482 /* unklink from call and destroy bchannel */
1483 free_bchannel(bchannel);
1486 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1487 newparam.bchannel.handle = param->bchannel.handle;
1488 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1493 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1498 /* handle new ref */
1499 if (message_type == MESSAGE_NEWREF) {
1500 if (param->newref.direction) {
1501 /* new ref from lcr */
1502 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1503 if (!ref || find_call_ref(ref)) {
1504 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1507 /* allocate new call instance */
1508 call = alloc_call();
1510 call->state = CHAN_LCR_STATE_IN_PREPARE;
1513 call->ref_was_assigned = 1;
1514 /* set dtmf (default, use option 'n' to disable */
1516 /* wait for setup (or release from asterisk) */
1518 /* new ref, as requested from this remote application */
1519 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1520 call = find_call_ref(0);
1522 /* send release, if ref does not exist */
1523 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1528 call->ref_was_assigned = 1;
1529 /* set dtmf (default, use option 'n' to disable */
1531 /* send pending setup info */
1532 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1533 send_setup_to_lcr(call);
1534 /* release if asterisk has signed off */
1535 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1538 send_release_and_import(call, call->cause, call->location);
1540 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1551 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1554 call = find_call_ref(ref);
1556 /* ignore ref that is not used (anymore) */
1557 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1561 /* handle messages */
1562 switch(message_type) {
1564 lcr_in_setup(call, message_type, param);
1567 case MESSAGE_OVERLAP:
1568 lcr_in_overlap(call, message_type, param);
1571 case MESSAGE_PROCEEDING:
1572 lcr_in_proceeding(call, message_type, param);
1575 case MESSAGE_ALERTING:
1576 lcr_in_alerting(call, message_type, param);
1579 case MESSAGE_CONNECT:
1580 lcr_in_connect(call, message_type, param);
1583 case MESSAGE_DISCONNECT:
1584 lcr_in_disconnect(call, message_type, param);
1587 case MESSAGE_RELEASE:
1588 lcr_in_release(call, message_type, param);
1591 case MESSAGE_INFORMATION:
1592 lcr_in_information(call, message_type, param);
1595 case MESSAGE_NOTIFY:
1596 lcr_in_notify(call, message_type, param);
1599 case MESSAGE_FACILITY:
1600 lcr_in_facility(call, message_type, param);
1603 case MESSAGE_PATTERN: // audio available from LCR
1604 if (!call->has_pattern)
1605 lcr_in_pattern(call, message_type, param);
1608 case MESSAGE_NOPATTERN: // audio not available from LCR
1611 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1612 call->audiopath = param->audiopath;
1616 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1623 * release all calls (due to broken socket)
1625 static void release_all_calls(void)
1627 struct chan_call *call;
1632 /* no ast, so we may directly free call */
1634 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1638 /* already in release process */
1639 if (call->state == CHAN_LCR_STATE_RELEASE) {
1643 /* release or queue release */
1645 call->state = CHAN_LCR_STATE_RELEASE;
1646 if (!call->pbx_started) {
1647 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1648 ast_hangup(call->ast); // call will be destroyed here
1651 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1655 write(wake_pipe[1], &byte, 1);
1657 strcpy(call->queue_string, "H");
1661 /* release all bchannels */
1662 while(bchannel_first)
1663 free_bchannel(bchannel_first);
1666 void close_socket(void);
1669 * warning! not thread safe
1670 * returns -1 for socket error, 0 for no work, 1 for work
1672 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1675 struct admin_list *admin;
1676 struct admin_message msg;
1678 if ((what & LCR_FD_READ)) {
1679 /* read from socket */
1680 len = read(lcr_sock, &msg, sizeof(msg));
1682 CERROR(NULL, NULL, "Socket closed.(read)\n");
1684 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1686 release_all_calls();
1687 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1691 if (len != sizeof(msg)) {
1692 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1695 if (msg.message != ADMIN_MESSAGE) {
1696 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1699 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1701 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1706 if ((what & LCR_FD_WRITE)) {
1707 /* write to socket */
1709 socket_fd.when &= ~LCR_FD_WRITE;
1712 admin = admin_first;
1713 len = write(lcr_sock, &admin->msg, sizeof(msg));
1715 CERROR(NULL, NULL, "Socket closed.(write)\n");
1719 if (len != sizeof(msg)) {
1720 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1724 admin_first = admin->next;
1728 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1737 * open and close socket and thread
1739 int open_socket(void)
1742 struct sockaddr_un sock_address;
1743 union parameter param;
1746 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1747 CERROR(NULL, NULL, "Failed to create socket.\n");
1751 /* set socket address and name */
1752 memset(&sock_address, 0, sizeof(sock_address));
1753 sock_address.sun_family = PF_UNIX;
1754 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1756 /* connect socket */
1757 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1760 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1764 /* register socket fd */
1765 memset(&socket_fd, 0, sizeof(socket_fd));
1766 socket_fd.fd = lcr_sock;
1767 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1769 /* enque hello message */
1770 memset(¶m, 0, sizeof(param));
1771 strcpy(param.hello.application, "asterisk");
1772 send_message(MESSAGE_HELLO, 0, ¶m);
1777 void close_socket(void)
1779 struct admin_list *admin, *temp;
1781 unregister_fd(&socket_fd);
1783 /* flush pending messages */
1784 admin = admin_first;
1787 admin = admin->next;
1800 /* sending queue to asterisk */
1801 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1805 read(wake_pipe[0], &byte, 1);
1812 static void handle_queue()
1814 struct chan_call *call;
1815 struct ast_channel *ast;
1816 struct ast_frame fr;
1822 p = call->queue_string;
1825 if (ast_channel_trylock(ast)) {
1826 ast_mutex_unlock(&chan_lock);
1828 ast_mutex_lock(&chan_lock);
1834 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1835 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1838 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1839 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1842 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1843 ast_queue_control(ast, AST_CONTROL_RINGING);
1844 ast_setstate(ast, AST_STATE_RINGING);
1847 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1848 ast_queue_control(ast, AST_CONTROL_ANSWER);
1851 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1852 ast_queue_hangup(ast);
1854 case '1': case '2': case '3': case 'A':
1855 case '4': case '5': case '6': case 'B':
1856 case '7': case '8': case '9': case 'C':
1857 case '*': case '0': case '#': case 'D':
1858 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1859 /* send digit to asterisk */
1860 memset(&fr, 0, sizeof(fr));
1862 #ifdef LCR_FOR_ASTERISK
1863 fr.frametype = AST_FRAME_DTMF_BEGIN;
1866 #ifdef LCR_FOR_CALLWEAVER
1867 fr.frametype = AST_FRAME_DTMF;
1870 #ifdef AST_1_8_OR_HIGHER
1871 fr.subclass.integer = *p;
1875 fr.delivery = ast_tv(0, 0);
1876 ast_queue_frame(ast, &fr);
1878 #ifdef LCR_FOR_ASTERISK
1879 fr.frametype = AST_FRAME_DTMF_END;
1880 ast_queue_frame(ast, &fr);
1885 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1889 call->queue_string[0] = '\0';
1890 ast_channel_unlock(ast);
1896 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1898 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1899 if (open_socket() < 0)
1900 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1905 void lock_chan(void)
1907 ast_mutex_lock(&chan_lock);
1910 void unlock_chan(void)
1912 ast_mutex_unlock(&chan_lock);
1915 /* chan_lcr thread */
1916 static void *chan_thread(void *arg)
1918 if (pipe(wake_pipe) < 0) {
1919 CERROR(NULL, NULL, "Failed to open pipe.\n");
1922 memset(&wake_fd, 0, sizeof(wake_fd));
1923 wake_fd.fd = wake_pipe[0];
1924 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1926 memset(&socket_retry, 0, sizeof(socket_retry));
1927 add_timer(&socket_retry, handle_retry, NULL, 0);
1929 bchannel_pid = getpid();
1931 /* open socket the first time */
1932 handle_retry(NULL, NULL, 0);
1934 ast_mutex_lock(&chan_lock);
1938 select_main(0, &global_change, lock_chan, unlock_chan);
1945 * new asterisk instance
1948 #ifdef AST_1_8_OR_HIGHER
1949 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
1951 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1954 char exten[256], *dial, *interface, *opt;
1955 struct ast_channel *ast;
1956 struct chan_call *call;
1958 ast_mutex_lock(&chan_lock);
1959 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1961 /* if socket is closed */
1963 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1964 ast_mutex_unlock(&chan_lock);
1968 /* create call instance */
1969 call = alloc_call();
1971 /* failed to create instance */
1972 ast_mutex_unlock(&chan_lock);
1976 /* create asterisk channel instrance */
1978 #ifdef LCR_FOR_ASTERISK
1979 #ifdef AST_1_8_OR_HIGHER
1980 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
1982 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1986 #ifdef LCR_FOR_CALLWEAVER
1987 ast = ast_channel_alloc(1);
1991 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1993 /* failed to create instance */
1994 ast_mutex_unlock(&chan_lock);
1997 ast->tech = &lcr_tech;
1998 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1999 /* configure channel */
2000 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2001 ast->readformat = ast->rawreadformat = ast->nativeformats;
2002 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2004 ast->hangupcause = 0;
2008 ast->tech_pvt = call;
2009 ast->fds[0] = call->pipe[0];
2010 call->pbx_started = 0;
2012 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2015 * Extract interface, dialstring, options from data.
2018 * <interface>/<dialstring>
2019 * <interface>/<dialstring>/options
2021 strncpy(exten, (char *)data, sizeof(exten)-1);
2022 exten[sizeof(exten)-1] = '\0';
2023 if ((dial = strchr(exten, '/'))) {
2026 if ((opt = strchr(dial, '/')))
2035 strncpy(call->interface, interface, sizeof(call->interface)-1);
2036 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2037 apply_opt(call, (char *)opt);
2039 #ifdef AST_1_8_OR_HIGHER
2040 // clone_variables(requestor, ast);
2043 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2044 if (requestor->caller.ani.number.valid)
2045 if (requestor->caller.ani.number.str)
2046 if (requestor->caller.ani.number.str[0])
2047 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2048 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2049 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2051 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2052 if (requestor->caller.ani.name.valid)
2053 if (requestor->caller.ani.name.str)
2054 if (requestor->caller.ani.name.str[0])
2055 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2056 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2058 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2059 if (requestor->caller.ani.subaddress.valid)
2060 if (requestor->caller.ani.subaddress.str)
2061 if (requestor->caller.ani.subaddress.str[0])
2062 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2063 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2065 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2066 if (requestor->caller.id.number.valid)
2067 if (requestor->caller.id.number.str)
2068 if (requestor->caller.id.number.str[0])
2069 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2070 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2071 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2073 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2074 if (requestor->caller.id.name.valid)
2075 if (requestor->caller.id.name.str)
2076 if (requestor->caller.id.name.str[0])
2077 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2078 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2080 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2081 if (requestor->caller.id.subaddress.valid)
2082 if (requestor->caller.id.subaddress.str)
2083 if (requestor->caller.id.subaddress.str[0])
2084 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2085 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2087 if (requestor->dialed.number.str)
2088 if (requestor->dialed.number.str[0])
2089 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2090 ast->dialed.number.plan= requestor->dialed.number.plan;
2092 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2093 if (requestor->dialed.subaddress.valid)
2094 if (requestor->dialed.subaddress.str)
2095 if (requestor->dialed.subaddress.str[0])
2096 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2097 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2099 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2100 ast->redirecting.count= requestor->redirecting.count;
2101 ast->redirecting.reason= requestor->redirecting.reason;
2103 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2104 if (requestor->redirecting.from.number.valid)
2105 if (requestor->redirecting.from.number.str)
2106 if (requestor->redirecting.from.number.str[0])
2107 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2108 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2109 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2111 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2112 if (requestor->redirecting.to.number.valid)
2113 if (requestor->redirecting.to.number.str)
2114 if (requestor->redirecting.to.number.str[0])
2115 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2116 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2117 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2119 /* store call information for setup */
2122 if (requestor->caller.id.number.valid) {
2123 if (requestor->caller.id.number.str)
2124 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2125 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2126 case AST_PRES_RESTRICTED:
2127 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2129 case AST_PRES_UNAVAILABLE:
2130 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2132 case AST_PRES_ALLOWED:
2134 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2136 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2137 case AST_PRES_USER_NUMBER_UNSCREENED:
2138 call->callerinfo.screen = INFO_SCREEN_USER;
2140 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2141 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2143 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2144 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2147 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2149 switch((requestor->caller.id.number.plan >> 4) & 7) {
2151 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2154 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2157 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2160 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2163 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2166 if (requestor->caller.ani.number.valid) {
2167 if (requestor->caller.ani.number.str)
2168 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2169 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2170 case AST_PRES_RESTRICTED:
2171 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2173 case AST_PRES_UNAVAILABLE:
2174 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2176 case AST_PRES_ALLOWED:
2178 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2180 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2181 case AST_PRES_USER_NUMBER_UNSCREENED:
2182 call->callerinfo.screen2 = INFO_SCREEN_USER;
2184 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2185 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2187 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2188 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2191 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2193 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2195 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2198 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2201 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2204 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2207 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2210 if (requestor->caller.id.name.valid) {
2211 if (requestor->caller.id.name.str)
2212 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2216 if (requestor->redirecting.from.number.valid) {
2217 call->redirinfo.itype = INFO_ITYPE_CHAN;
2218 if (requestor->redirecting.from.number.str)
2219 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2220 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2221 case AST_PRES_RESTRICTED:
2222 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2224 case AST_PRES_UNAVAILABLE:
2225 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2227 case AST_PRES_ALLOWED:
2229 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2231 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2232 case AST_PRES_USER_NUMBER_UNSCREENED:
2233 call->redirinfo.screen = INFO_SCREEN_USER;
2235 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2236 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2238 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2239 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2242 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2244 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2246 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2249 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2252 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2255 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2260 ast_mutex_unlock(&chan_lock);
2265 * call from asterisk
2267 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2269 union parameter newparam;
2270 struct chan_call *call;
2272 ast_mutex_lock(&chan_lock);
2273 call = ast->tech_pvt;
2275 #ifdef LCR_FOR_CALLWEAVER
2277 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
2281 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2282 ast_mutex_unlock(&chan_lock);
2286 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2288 /* pbx process is started */
2289 call->pbx_started = 1;
2290 /* send MESSAGE_NEWREF */
2291 memset(&newparam, 0, sizeof(union parameter));
2292 newparam.newref.direction = 0; /* request from app */
2293 if (!strcmp(call->interface, "pbx"))
2294 newparam.newref.mode = 1;
2295 send_message(MESSAGE_NEWREF, 0, &newparam);
2297 /* set hdlc if capability requires hdlc */
2298 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2299 || ast->transfercapability == INFO_BC_DATARESTRICTED
2300 || ast->transfercapability == INFO_BC_VIDEO)
2302 /* if hdlc is forced by option, we change transcap to data */
2304 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2305 && ast->transfercapability != INFO_BC_DATARESTRICTED
2306 && ast->transfercapability != INFO_BC_VIDEO)
2307 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2309 #ifndef AST_1_8_OR_HIGHER
2310 call->cid_num[0] = 0;
2311 call->cid_name[0] = 0;
2312 call->cid_rdnis[0] = 0;
2314 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2315 strncpy(call->cid_num, ast->cid.cid_num,
2316 sizeof(call->cid_num)-1);
2317 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2318 strncpy(call->cid_name, ast->cid.cid_name,
2319 sizeof(call->cid_name)-1);
2320 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2321 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2322 sizeof(call->cid_rdnis)-1);
2325 ast_mutex_unlock(&chan_lock);
2329 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2331 static const char* dtmf_tones[] = {
2332 "!941+1336/100,!0/100", /* 0 */
2333 "!697+1209/100,!0/100", /* 1 */
2334 "!697+1336/100,!0/100", /* 2 */
2335 "!697+1477/100,!0/100", /* 3 */
2336 "!770+1209/100,!0/100", /* 4 */
2337 "!770+1336/100,!0/100", /* 5 */
2338 "!770+1477/100,!0/100", /* 6 */
2339 "!852+1209/100,!0/100", /* 7 */
2340 "!852+1336/100,!0/100", /* 8 */
2341 "!852+1477/100,!0/100", /* 9 */
2342 "!697+1633/100,!0/100", /* A */
2343 "!770+1633/100,!0/100", /* B */
2344 "!852+1633/100,!0/100", /* C */
2345 "!941+1633/100,!0/100", /* D */
2346 "!941+1209/100,!0/100", /* * */
2347 "!941+1477/100,!0/100" }; /* # */
2349 if (digit >= '0' && digit <='9')
2350 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2351 else if (digit >= 'A' && digit <= 'D')
2352 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2353 else if (digit == '*')
2354 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2355 else if (digit == '#')
2356 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2359 CDEBUG(NULL, ast, "Unable to handle DTMF tone "
2360 "'%c' for '%s'\n", digit, ast->name);
2364 #ifdef LCR_FOR_ASTERISK
2365 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2367 #ifdef LCR_FOR_CALLWEAVER
2368 static int lcr_digit(struct ast_channel *ast, char digit)
2371 struct chan_call *call;
2372 union parameter newparam;
2375 #ifdef LCR_FOR_CALLWEAVER
2376 int inband_dtmf = 0;
2379 /* only pass IA5 number space */
2380 if (digit > 126 || digit < 32)
2383 ast_mutex_lock(&chan_lock);
2384 call = ast->tech_pvt;
2386 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2387 ast_mutex_unlock(&chan_lock);
2391 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2393 /* send information or queue them */
2394 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2395 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2396 memset(&newparam, 0, sizeof(union parameter));
2398 newparam.information.keypad[0] = digit;
2399 newparam.information.keypad[1] = '\0';
2401 newparam.information.id[0] = digit;
2402 newparam.information.id[1] = '\0';
2404 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2407 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2408 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2410 strncat(call->dialque, buf, strlen(call->dialque)-1);
2413 ast_mutex_unlock(&chan_lock);
2415 #ifdef LCR_FOR_ASTERISK
2419 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2421 int inband_dtmf = 0;
2422 struct chan_call *call;
2425 ast_mutex_lock(&chan_lock);
2427 call = ast->tech_pvt;
2431 "Received digit from Asterisk, "
2432 "but no call instance exists.\n");
2433 ast_mutex_unlock(&chan_lock);
2437 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2439 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2443 ast_mutex_unlock(&chan_lock);
2446 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2447 send_digit_to_chan(ast, digit);
2453 static int lcr_answer(struct ast_channel *ast)
2455 union parameter newparam;
2456 struct chan_call *call;
2458 ast_mutex_lock(&chan_lock);
2459 call = ast->tech_pvt;
2461 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2462 ast_mutex_unlock(&chan_lock);
2466 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2468 /* copy connectinfo, if bridged */
2469 if (call->bridge_call)
2470 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2471 /* send connect message to lcr */
2472 if (call->state != CHAN_LCR_STATE_CONNECT) {
2473 memset(&newparam, 0, sizeof(union parameter));
2474 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2475 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2476 call->state = CHAN_LCR_STATE_CONNECT;
2479 /* request bchannel */
2480 if (!call->bchannel) {
2481 CDEBUG(call, ast, "Requesting B-channel.\n");
2482 memset(&newparam, 0, sizeof(union parameter));
2483 newparam.bchannel.type = BCHANNEL_REQUEST;
2484 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2487 // memset(&newparam, 0, sizeof(union parameter));
2488 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2490 ast_mutex_unlock(&chan_lock);
2494 static int lcr_hangup(struct ast_channel *ast)
2496 struct chan_call *call;
2497 pthread_t tid = pthread_self();
2499 if (!pthread_equal(tid, chan_tid)) {
2500 ast_mutex_lock(&chan_lock);
2502 call = ast->tech_pvt;
2504 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2505 if (!pthread_equal(tid, chan_tid)) {
2506 ast_mutex_unlock(&chan_lock);
2511 if (!pthread_equal(tid, chan_tid))
2512 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2514 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2516 /* disconnect asterisk, maybe not required */
2517 ast->tech_pvt = NULL;
2521 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2522 if (ast->hangupcause > 0)
2523 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2525 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2528 if (!pthread_equal(tid, chan_tid)) {
2529 ast_mutex_unlock(&chan_lock);
2533 /* ref is not set, due to prepare setup or release */
2534 if (call->state == CHAN_LCR_STATE_RELEASE) {
2535 /* we get the response to our release */
2536 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2539 /* during prepare, we change to release state */
2540 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2541 call->state = CHAN_LCR_STATE_RELEASE;
2545 if (!pthread_equal(tid, chan_tid)) {
2546 ast_mutex_unlock(&chan_lock);
2551 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2553 struct chan_call *call;
2554 struct ast_frame * f = fr;
2556 #ifdef AST_1_8_OR_HIGHER
2557 if (!f->subclass.integer)
2561 CDEBUG(NULL, ast, "No subclass\n");
2562 #ifdef AST_1_8_OR_HIGHER
2563 if (!(f->subclass.integer & ast->nativeformats)) {
2565 if (!(f->subclass & ast->nativeformats)) {
2568 "Unexpected format. "
2569 "Activating emergency conversion...\n");
2571 #ifdef AST_1_8_OR_HIGHER
2572 ast_set_write_format(ast, f->subclass.integer);
2574 ast_set_write_format(ast, f->subclass);
2576 f = (ast->writetrans) ? ast_translate(
2577 ast->writetrans, fr, 0) : fr;
2580 ast_mutex_lock(&chan_lock);
2581 call = ast->tech_pvt;
2583 ast_mutex_unlock(&chan_lock);
2589 if (call->bchannel && f->samples)
2590 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2591 ast_mutex_unlock(&chan_lock);
2599 static struct ast_frame *lcr_read(struct ast_channel *ast)
2601 struct chan_call *call;
2604 ast_mutex_lock(&chan_lock);
2605 call = ast->tech_pvt;
2607 ast_mutex_unlock(&chan_lock);
2610 if (call->pipe[0] > -1) {
2611 if (call->rebuffer && !call->hdlc) {
2612 /* Make sure we have a complete 20ms (160byte) frame */
2613 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2615 call->framepos += len;
2618 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2620 if (len < 0 && errno == EAGAIN) {
2621 ast_mutex_unlock(&chan_lock);
2623 #ifdef LCR_FOR_ASTERISK
2624 return &ast_null_frame;
2627 #ifdef LCR_FOR_CALLWEAVER
2633 close(call->pipe[0]);
2636 ast_mutex_unlock(&chan_lock);
2638 } else if (call->rebuffer && call->framepos < 160) {
2639 /* Not a complete frame, so we send a null-frame */
2640 ast_mutex_unlock(&chan_lock);
2641 return &ast_null_frame;
2645 call->read_fr.frametype = AST_FRAME_VOICE;
2646 #ifdef AST_1_8_OR_HIGHER
2647 call->read_fr.subclass.integer = ast->nativeformats;
2649 call->read_fr.subclass = ast->nativeformats;
2651 if (call->rebuffer) {
2652 call->read_fr.datalen = call->framepos;
2653 call->read_fr.samples = call->framepos;
2656 call->read_fr.datalen = len;
2657 call->read_fr.samples = len;
2659 call->read_fr.delivery = ast_tv(0,0);
2660 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2661 ast_mutex_unlock(&chan_lock);
2663 return &call->read_fr;
2666 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2668 union parameter newparam;
2670 struct chan_call *call;
2671 const struct tone_zone_sound *ts = NULL;
2673 ast_mutex_lock(&chan_lock);
2674 call = ast->tech_pvt;
2676 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2677 ast_mutex_unlock(&chan_lock);
2682 case AST_CONTROL_BUSY:
2683 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2684 ast_setstate(ast, AST_STATE_BUSY);
2685 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2686 /* send message to lcr */
2687 memset(&newparam, 0, sizeof(union parameter));
2688 newparam.disconnectinfo.cause = 17;
2689 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2690 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2692 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2694 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2695 ts = ast_get_indication_tone(ast->zone, "busy");
2698 case AST_CONTROL_CONGESTION:
2699 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2700 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2701 /* send message to lcr */
2702 memset(&newparam, 0, sizeof(union parameter));
2703 newparam.disconnectinfo.cause = ast->hangupcause;
2704 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2705 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2707 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2709 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2710 ts = ast_get_indication_tone(ast->zone, "congestion");
2713 case AST_CONTROL_PROCEEDING:
2714 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2715 if (call->state == CHAN_LCR_STATE_IN_SETUP
2716 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2717 /* send message to lcr */
2718 memset(&newparam, 0, sizeof(union parameter));
2719 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2721 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2724 case AST_CONTROL_RINGING:
2725 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2726 ast_setstate(ast, AST_STATE_RING);
2727 if (call->state == CHAN_LCR_STATE_IN_SETUP
2728 || call->state == CHAN_LCR_STATE_IN_DIALING
2729 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2730 /* send message to lcr */
2731 memset(&newparam, 0, sizeof(union parameter));
2732 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2734 call->state = CHAN_LCR_STATE_IN_ALERTING;
2736 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2737 ts = ast_get_indication_tone(ast->zone, "ring");
2740 case AST_CONTROL_PROGRESS:
2741 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2742 /* request bchannel */
2743 if (!call->bchannel) {
2744 CDEBUG(call, ast, "Requesting B-channel.\n");
2745 memset(&newparam, 0, sizeof(union parameter));
2746 newparam.bchannel.type = BCHANNEL_REQUEST;
2747 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2751 CDEBUG(call, ast, "Received indicate -1.\n");
2752 ast_playtones_stop(ast);
2756 case AST_CONTROL_VIDUPDATE:
2757 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2760 case AST_CONTROL_HOLD:
2761 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2762 /* send message to lcr */
2763 memset(&newparam, 0, sizeof(union parameter));
2764 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2765 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2767 /*start music onhold*/
2768 #ifdef LCR_FOR_ASTERISK
2769 ast_moh_start(ast,data,ast->musicclass);
2772 #ifdef LCR_FOR_CALLWEAVER
2773 ast_moh_start(ast, NULL);
2778 case AST_CONTROL_UNHOLD:
2779 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2780 /* send message to lcr */
2781 memset(&newparam, 0, sizeof(union parameter));
2782 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2783 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2789 #ifdef AST_CONTROL_SRCUPDATE
2790 case AST_CONTROL_SRCUPDATE:
2794 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2797 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2802 if (ts && ts->data[0]) {
2803 ast_playtones_start(ast, 0, ts->data, 1);
2807 ast_mutex_unlock(&chan_lock);
2814 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2816 struct chan_call *call;
2822 ast_mutex_lock(&chan_lock);
2823 call = ast->tech_pvt;
2825 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2826 ast_mutex_unlock(&chan_lock);
2830 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2832 ast_mutex_unlock(&chan_lock);
2837 * send_text asterisk
2839 static int lcr_send_text(struct ast_channel *ast, const char *text)
2841 struct chan_call *call;
2842 union parameter newparam;
2844 ast_mutex_lock(&chan_lock);
2845 call = ast->tech_pvt;
2847 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2848 ast_mutex_unlock(&chan_lock);
2852 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2853 memset(&newparam, 0, sizeof(union parameter));
2854 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2855 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2856 ast_mutex_unlock(&chan_lock);
2863 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2864 struct ast_channel *ast2, int flags,
2865 struct ast_frame **fo,
2866 struct ast_channel **rc, int timeoutms)
2869 struct chan_call *call1, *call2;
2870 struct ast_channel *carr[2], *who;
2872 struct ast_frame *f;
2875 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2880 /* join via dsp (if the channels are currently open) */
2881 ast_mutex_lock(&chan_lock);
2882 call1 = ast1->tech_pvt;
2883 call2 = ast2->tech_pvt;
2884 if (!call1 || !call2) {
2885 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2886 ast_mutex_unlock(&chan_lock);
2887 return AST_BRIDGE_COMPLETE;
2890 /* join, if both call instances uses dsp
2891 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2893 if (!call1->nodsp && !call2->nodsp) {
2894 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2896 /* get bridge id and join */
2897 bridge_id = new_bridge_id();
2899 call1->bridge_id = bridge_id;
2900 if (call1->bchannel)
2901 bchannel_join(call1->bchannel, bridge_id);
2903 call2->bridge_id = bridge_id;
2904 if (call2->bchannel)
2905 bchannel_join(call2->bchannel, bridge_id);
2907 if (call1->nodsp && call2->nodsp)
2908 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2910 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2911 call1->bridge_call = call2;
2912 call2->bridge_call = call1;
2914 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2915 || call1->state == CHAN_LCR_STATE_IN_DIALING
2916 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2917 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2918 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2921 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2922 || call2->state == CHAN_LCR_STATE_IN_DIALING
2923 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2924 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2925 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2929 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2930 so let's do it for them. Hmpf.
2933 if (call1->on_hold) {
2934 union parameter newparam;
2936 memset(&newparam, 0, sizeof(union parameter));
2937 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2938 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2943 if (call2->on_hold) {
2944 union parameter newparam;
2946 memset(&newparam, 0, sizeof(union parameter));
2947 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2948 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2953 ast_mutex_unlock(&chan_lock);
2957 who = ast_waitfor_n(carr, 2, &to);
2960 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2965 if (!f || f->frametype == AST_FRAME_CONTROL) {
2967 CDEBUG(NULL, NULL, "Got hangup.\n");
2969 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2976 if ( f->frametype == AST_FRAME_DTMF ) {
2977 CDEBUG(NULL, NULL, "Got DTMF.\n");
2993 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2995 /* split channels */
2996 ast_mutex_lock(&chan_lock);
2997 call1 = ast1->tech_pvt;
2998 call2 = ast2->tech_pvt;
2999 if (call1 && call1->bridge_id) {
3000 call1->bridge_id = 0;
3001 if (call1->bchannel)
3002 bchannel_join(call1->bchannel, 0);
3003 if (call1->bridge_call)
3004 call1->bridge_call->bridge_call = NULL;
3006 if (call2 && call1->bridge_id) {
3007 call2->bridge_id = 0;
3008 if (call2->bchannel)
3009 bchannel_join(call2->bchannel, 0);
3010 if (call2->bridge_call)
3011 call2->bridge_call->bridge_call = NULL;
3013 call1->bridge_call = NULL;
3014 call2->bridge_call = NULL;
3016 ast_mutex_unlock(&chan_lock);
3017 return AST_BRIDGE_COMPLETE;
3019 static struct ast_channel_tech lcr_tech = {
3021 .description = "Channel driver for connecting to Linux-Call-Router",
3022 .capabilities = AST_FORMAT_ALAW,
3023 .requester = lcr_request,
3025 #ifdef LCR_FOR_ASTERISK
3026 .send_digit_begin = lcr_digit_begin,
3027 .send_digit_end = lcr_digit_end,
3030 #ifdef LCR_FOR_CALLWEAVER
3031 .send_digit = lcr_digit,
3035 .bridge = lcr_bridge,
3036 .hangup = lcr_hangup,
3037 .answer = lcr_answer,
3040 .indicate = lcr_indicate,
3042 .send_text = lcr_send_text,
3051 static int lcr_show_lcr (int fd, int argc, char *argv[])
3056 static int lcr_show_calls (int fd, int argc, char *argv[])
3061 static int lcr_reload_routing (int fd, int argc, char *argv[])
3066 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3071 static int lcr_port_block (int fd, int argc, char *argv[])
3076 static int lcr_port_unblock (int fd, int argc, char *argv[])
3081 static int lcr_port_unload (int fd, int argc, char *argv[])
3086 static struct ast_cli_entry cli_show_lcr =
3087 { {"lcr", "show", "lcr", NULL},
3089 "Shows current states of LCR core",
3090 "Usage: lcr show lcr\n",
3093 static struct ast_cli_entry cli_show_calls =
3094 { {"lcr", "show", "calls", NULL},
3096 "Shows current calls made by LCR and Asterisk",
3097 "Usage: lcr show calls\n",
3100 static struct ast_cli_entry cli_reload_routing =
3101 { {"lcr", "reload", "routing", NULL},
3103 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3104 "Usage: lcr reload routing\n",
3107 static struct ast_cli_entry cli_reload_interfaces =
3108 { {"lcr", "reload", "interfaces", NULL},
3109 lcr_reload_interfaces,
3110 "Reloads interfaces conf of LCR",
3111 "Usage: lcr reload interfaces\n",
3114 static struct ast_cli_entry cli_port_block =
3115 { {"lcr", "port", "block", NULL},
3117 "Blocks LCR port for further calls",
3118 "Usage: lcr port block \"<port>\"\n",
3121 static struct ast_cli_entry cli_port_unblock =
3122 { {"lcr", "port", "unblock", NULL},
3124 "Unblocks or loads LCR port, port is opened my mISDN",
3125 "Usage: lcr port unblock \"<port>\"\n",
3128 static struct ast_cli_entry cli_port_unload =
3129 { {"lcr", "port", "unload", NULL},
3131 "Unloads LCR port, port is closes by mISDN",
3132 "Usage: lcr port unload \"<port>\"\n",
3137 #ifdef LCR_FOR_ASTERISK
3138 #ifdef AST_1_8_OR_HIGHER
3139 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3141 static int lcr_config_exec(struct ast_channel *ast, void *data)
3145 #ifdef LCR_FOR_CALLWEAVER
3146 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3149 struct chan_call *call;
3151 ast_mutex_lock(&chan_lock);
3153 #ifdef LCR_FOR_ASTERISK
3154 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3157 #ifdef LCR_FOR_CALLWEAVER
3158 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3164 if (call->ast == ast)
3170 #ifdef LCR_FOR_ASTERISK
3171 apply_opt(call, (char *)data);
3174 #ifdef LCR_FOR_CALLWEAVER
3175 apply_opt(call, (char *)argv[0]);
3179 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3181 ast_mutex_unlock(&chan_lock);
3186 * module loading and destruction
3188 int load_module(void)
3191 char options_error[256];
3193 for (i = 0; i < 256; i++) {
3194 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3195 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3198 if (read_options(options_error) == 0) {
3199 CERROR(NULL, NULL, "%s", options_error);
3201 #ifdef LCR_FOR_ASTERISK
3202 return AST_MODULE_LOAD_DECLINE;
3205 #ifdef LCR_FOR_CALLWEAVER
3211 ast_mutex_init(&chan_lock);
3212 ast_mutex_init(&log_lock);
3214 if (bchannel_initialize()) {
3215 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3218 #ifdef LCR_FOR_ASTERISK
3219 return AST_MODULE_LOAD_DECLINE;
3222 #ifdef LCR_FOR_CALLWEAVER
3228 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3229 if (ast_channel_register(&lcr_tech)) {
3230 CERROR(NULL, NULL, "Unable to register channel class\n");
3231 bchannel_deinitialize();
3234 #ifdef LCR_FOR_ASTERISK
3235 return AST_MODULE_LOAD_DECLINE;
3238 #ifdef LCR_FOR_CALLWEAVER
3243 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3245 #ifdef LCR_FOR_ASTERISK
3246 "lcr_config(<opt><optarg>:<opt>:...)\n"
3249 #ifdef LCR_FOR_CALLWEAVER
3250 "lcr_config(<opt><optarg>:<opt>:...)\n",
3253 "Sets LCR opts. and optargs\n"
3255 "The available options are:\n"
3256 " d - Send display text on called phone, text is the optarg.\n"
3257 " n - Don't detect dtmf tones on called channel.\n"
3258 " h - Force data call (HDLC).\n"
3259 " t - Disable mISDN_dsp features (required for fax application).\n"
3260 " q - Add queue to make fax stream seamless (required for fax app).\n"
3261 " Use queue size in miliseconds for optarg. (try 250)\n"
3262 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3263 " Use time to detect for optarg.\n"
3264 " c - Make crypted outgoing call, optarg is keyindex.\n"
3265 " e - Perform echo cancelation on this channel.\n"
3266 " Takes mISDN pipeline option as optarg.\n"
3267 " s - Send Non Inband DTMF as inband.\n"
3268 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3269 " vr - rxgain control\n"
3270 " vt - txgain control\n"
3271 " Volume changes at factor 2 ^ optarg.\n"
3272 " k - use keypad to dial this call.\n"
3274 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3275 " -> use 16 for fax (3.1k audio)\n"
3277 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3278 "options: \"n:t:q250\" for seamless audio transmission.\n"
3283 ast_cli_register(&cli_show_lcr);
3284 ast_cli_register(&cli_show_calls);
3285 ast_cli_register(&cli_reload_routing);
3286 ast_cli_register(&cli_reload_interfaces);
3287 ast_cli_register(&cli_port_block);
3288 ast_cli_register(&cli_port_unblock);
3289 ast_cli_register(&cli_port_unload);
3292 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3293 /* failed to create thread */
3294 bchannel_deinitialize();
3296 ast_channel_unregister(&lcr_tech);
3298 #ifdef LCR_FOR_ASTERISK
3299 return AST_MODULE_LOAD_DECLINE;
3302 #ifdef LCR_FOR_CALLWEAVER
3310 int unload_module(void)
3312 /* First, take us out of the channel loop */
3313 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3315 pthread_cancel(chan_tid);
3319 del_timer(&socket_retry);
3321 unregister_fd(&wake_fd);
3322 close(wake_pipe[0]);
3323 close(wake_pipe[1]);
3325 // ast_mutex_unlock(&chan_lock);
3327 ast_channel_unregister(&lcr_tech);
3329 ast_unregister_application("lcr_config");
3331 if (mISDN_created) {
3332 bchannel_deinitialize();
3336 if (lcr_sock >= 0) {
3344 int reload_module(void)
3350 #ifdef LCR_FOR_ASTERISK
3351 #define AST_MODULE "chan_lcr"
3354 #ifdef LCR_FOR_CALLWEAVER
3359 ast_mutex_lock(&usecnt_lock);
3361 ast_mutex_unlock(&usecnt_lock);
3366 #ifdef LCR_FOR_ASTERISK
3367 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3368 .load = load_module,
3369 .unload = unload_module,
3370 .reload = reload_module,
3374 #ifdef LCR_FOR_CALLWEAVER
3375 char *description(void)