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 line=%d] %s", call_text, ast_text, line, 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, call->callerinfo.id);
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
643 /* set stored call information */
644 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
645 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
647 if (call->cid_num[0])
648 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
649 if (call->cid_name[0])
650 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
651 if (call->cid_rdnis[0]) {
652 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
653 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
654 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
656 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
657 case AST_PRES_RESTRICTED:
658 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
660 case AST_PRES_UNAVAILABLE:
661 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
663 case AST_PRES_ALLOWED:
665 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
667 switch(ast->cid.cid_ton) {
669 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
672 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
675 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
678 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
681 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
682 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
684 // ast->transfercapability = atoi(tmp);
685 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
686 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
688 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
690 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
691 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
693 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
694 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
695 send_message(MESSAGE_SETUP, call->ref, &newparam);
697 /* change to outgoing setup state */
698 call->state = CHAN_LCR_STATE_OUT_SETUP;
702 * send dialing info to LCR
703 * this function is called, when setup acknowledge is received and dialing
706 static void send_dialque_to_lcr(struct chan_call *call)
708 union parameter newparam;
710 if (!call->ast || !call->ref || !call->dialque[0])
713 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
715 /* send setup message to LCR */
716 memset(&newparam, 0, sizeof(union parameter));
718 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
720 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
721 call->dialque[0] = '\0';
722 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
726 * in case of a bridge, the unsupported message can be forwarded directly
727 * to the remote call.
729 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
733 if (!call->bridge_call) return;
734 CDEBUG(call, NULL, "Sending message due bridging.\n");
735 send_message(message_type, call->bridge_call->ref, param);
739 * send release message to LCR and import bchannel if exported
741 static void send_release_and_import(struct chan_call *call, int cause, int location)
743 union parameter newparam;
745 /* importing channel */
746 if (call->bchannel) {
747 memset(&newparam, 0, sizeof(union parameter));
748 newparam.bchannel.type = BCHANNEL_RELEASE;
749 newparam.bchannel.handle = call->bchannel->handle;
750 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
752 /* sending release */
753 memset(&newparam, 0, sizeof(union parameter));
754 newparam.disconnectinfo.cause = cause;
755 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
756 send_message(MESSAGE_RELEASE, call->ref, &newparam);
760 * check if extension matches and start asterisk
761 * if it can match, proceed
764 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
767 union parameter newparam;
768 char *exten = ast->exten;
772 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
776 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
777 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
781 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
782 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);
786 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
787 /* send setup acknowledge to lcr */
788 memset(&newparam, 0, sizeof(union parameter));
789 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
792 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
797 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
798 /* send setup acknowledge to lcr */
799 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
800 memset(&newparam, 0, sizeof(union parameter));
801 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
805 call->state = CHAN_LCR_STATE_IN_DIALING;
807 /* if match, start pbx */
808 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
809 CDEBUG(call, ast, "Extensions matches.\n");
814 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
820 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
828 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
829 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
831 /* release asterisk */
832 ast->hangupcause = call->cause;
833 /* change to release state */
834 call->state = CHAN_LCR_STATE_RELEASE;
835 ast_hangup(ast); // call will be destroyed here
839 /* send setup to asterisk */
840 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
842 #ifdef LCR_FOR_CALLWEAVER
844 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
847 ret = ast_pbx_start(ast);
849 cause = (ret==-2)?34:27;
852 call->pbx_started = 1;
853 ast_setstate(ast, AST_STATE_RING);
857 * incoming setup from LCR
859 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
861 struct ast_channel *ast;
863 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
865 /* create asterisk channel instrance */
867 #ifdef LCR_FOR_CALLWEAVER
868 ast = ast_channel_alloc(1);
871 #ifdef LCR_FOR_ASTERISK
872 #ifdef AST_1_8_OR_HIGHER
873 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
875 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
881 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
882 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
889 ast->tech_pvt = call;
890 ast->tech = &lcr_tech;
891 ast->fds[0] = call->pipe[0];
893 /* fill setup information */
894 if (param->setup.dialinginfo.id)
895 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
896 if (param->setup.context[0])
897 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
899 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
903 #ifdef AST_1_8_OR_HIGHER
904 if (param->setup.callerinfo.id[0]) {
905 ast->caller.id.number.valid = 1;
906 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
907 if (!param->setup.callerinfo.id[0]) {
908 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
909 ast->caller.id.number.plan = (0 << 4) | 1;
911 switch (param->setup.callerinfo.present) {
912 case INFO_PRESENT_ALLOWED:
913 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
915 case INFO_PRESENT_RESTRICTED:
916 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
919 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
921 switch (param->setup.callerinfo.screen) {
922 case INFO_SCREEN_USER:
923 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
925 case INFO_SCREEN_USER_VERIFIED_PASSED:
926 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
928 case INFO_SCREEN_USER_VERIFIED_FAILED:
929 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
932 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
934 switch (param->setup.callerinfo.ntype) {
935 case INFO_NTYPE_SUBSCRIBER:
936 ast->caller.id.number.plan = (4 << 4) | 1;
938 case INFO_NTYPE_NATIONAL:
939 ast->caller.id.number.plan = (2 << 4) | 1;
941 case INFO_NTYPE_INTERNATIONAL:
942 ast->caller.id.number.plan = (1 << 4) | 1;
945 ast->caller.id.number.plan = (0 << 4) | 1;
948 if (param->setup.callerinfo.id2[0]) {
949 ast->caller.ani.number.valid = 1;
950 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
951 switch (param->setup.callerinfo.present2) {
952 case INFO_PRESENT_ALLOWED:
953 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
955 case INFO_PRESENT_RESTRICTED:
956 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
959 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
961 switch (param->setup.callerinfo.screen2) {
962 case INFO_SCREEN_USER:
963 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
965 case INFO_SCREEN_USER_VERIFIED_PASSED:
966 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
968 case INFO_SCREEN_USER_VERIFIED_FAILED:
969 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
972 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
974 switch (param->setup.callerinfo.ntype2) {
975 case INFO_NTYPE_SUBSCRIBER:
976 ast->caller.ani.number.plan = (4 << 4) | 1;
978 case INFO_NTYPE_NATIONAL:
979 ast->caller.ani.number.plan = (2 << 4) | 1;
981 case INFO_NTYPE_INTERNATIONAL:
982 ast->caller.ani.number.plan = (1 << 4) | 1;
985 ast->caller.ani.number.plan = (0 << 4) | 1;
988 if (param->setup.callerinfo.name[0]) {
989 ast->caller.id.name.valid = 1;
990 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
992 if (param->setup.redirinfo.id[0]) {
993 ast->redirecting.from.number.valid = 1;
994 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
995 switch (param->setup.redirinfo.present) {
996 case INFO_PRESENT_ALLOWED:
997 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
999 case INFO_PRESENT_RESTRICTED:
1000 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1003 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1005 switch (param->setup.redirinfo.screen) {
1006 case INFO_SCREEN_USER:
1007 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1009 case INFO_SCREEN_USER_VERIFIED_PASSED:
1010 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1012 case INFO_SCREEN_USER_VERIFIED_FAILED:
1013 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1016 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1018 switch (param->setup.redirinfo.ntype) {
1019 case INFO_NTYPE_SUBSCRIBER:
1020 ast->redirecting.from.number.plan = (4 << 4) | 1;
1022 case INFO_NTYPE_NATIONAL:
1023 ast->redirecting.from.number.plan = (2 << 4) | 1;
1025 case INFO_NTYPE_INTERNATIONAL:
1026 ast->redirecting.from.number.plan = (1 << 4) | 1;
1029 ast->redirecting.from.number.plan = (0 << 4) | 1;
1033 memset(&ast->cid, 0, sizeof(ast->cid));
1034 if (param->setup.callerinfo.id[0])
1035 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1036 if (param->setup.callerinfo.id2[0])
1037 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1038 if (param->setup.callerinfo.name[0])
1039 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1040 if (param->setup.redirinfo.id[0])
1041 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1042 switch (param->setup.callerinfo.present) {
1043 case INFO_PRESENT_ALLOWED:
1044 ast->cid.cid_pres = AST_PRES_ALLOWED;
1046 case INFO_PRESENT_RESTRICTED:
1047 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1050 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1052 switch (param->setup.callerinfo.ntype) {
1053 case INFO_NTYPE_SUBSCRIBER:
1054 ast->cid.cid_ton = 4;
1056 case INFO_NTYPE_NATIONAL:
1057 ast->cid.cid_ton = 2;
1059 case INFO_NTYPE_INTERNATIONAL:
1060 ast->cid.cid_ton = 1;
1063 ast->cid.cid_ton = 0;
1067 ast->transfercapability = param->setup.capainfo.bearer_capa;
1068 /* enable hdlc if transcap is data */
1069 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1071 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1073 /* configure channel */
1074 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1075 ast->readformat = ast->rawreadformat = ast->nativeformats;
1076 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1078 ast->hangupcause = 0;
1081 call->state = CHAN_LCR_STATE_IN_SETUP;
1083 if (!call->pbx_started)
1084 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1088 * incoming setup acknowledge from LCR
1090 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1092 if (!call->ast) return;
1094 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1096 /* send pending digits in dialque */
1097 if (call->dialque[0])
1098 send_dialque_to_lcr(call);
1099 /* change to overlap state */
1100 call->state = CHAN_LCR_STATE_OUT_DIALING;
1104 * incoming proceeding from LCR
1106 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1108 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1111 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1112 /* queue event for asterisk */
1113 if (call->ast && call->pbx_started) {
1117 write(wake_pipe[1], &byte, 1);
1119 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1125 * incoming alerting from LCR
1127 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1129 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1132 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1133 /* queue event to asterisk */
1134 if (call->ast && call->pbx_started) {
1138 write(wake_pipe[1], &byte, 1);
1140 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1145 * incoming connect from LCR
1147 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1149 union parameter newparam;
1151 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1154 call->state = CHAN_LCR_STATE_CONNECT;
1155 /* request bchannel */
1156 if (!call->bchannel) {
1157 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1158 memset(&newparam, 0, sizeof(union parameter));
1159 newparam.bchannel.type = BCHANNEL_REQUEST;
1160 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1162 /* copy connectinfo */
1163 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1164 /* queue event to asterisk */
1165 if (call->ast && call->pbx_started) {
1169 write(wake_pipe[1], &byte, 1);
1171 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1176 * incoming disconnect from LCR
1178 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1180 struct ast_channel *ast = call->ast;
1182 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1185 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1187 call->cause = param->disconnectinfo.cause;
1188 call->location = param->disconnectinfo.location;
1189 /* if bridge, forward disconnect and return */
1192 if (call->bridge_call) {
1193 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1194 bridge_message_if_bridged(call, message_type, param);
1198 /* release lcr with same cause */
1199 send_release_and_import(call, call->cause, call->location);
1201 /* change to release state */
1202 call->state = CHAN_LCR_STATE_RELEASE;
1203 /* queue release asterisk */
1205 ast->hangupcause = call->cause;
1206 if (call->pbx_started) {
1210 write(wake_pipe[1], &byte, 1);
1212 strcpy(call->queue_string, "H"); // overwrite other indications
1214 ast_hangup(ast); // call will be destroyed here
1220 * incoming release from LCR
1222 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1224 struct ast_channel *ast = call->ast;
1226 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1230 /* change to release state */
1231 call->state = CHAN_LCR_STATE_RELEASE;
1232 /* copy release info */
1234 call->cause = param->disconnectinfo.cause;
1235 call->location = param->disconnectinfo.location;
1237 /* if we have an asterisk instance, queue hangup, else we are done */
1239 ast->hangupcause = call->cause;
1240 if (call->pbx_started) {
1244 write(wake_pipe[1], &byte, 1);
1246 strcpy(call->queue_string, "H");
1248 ast_hangup(ast); // call will be destroyed here
1257 * incoming information from LCR
1259 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1261 struct ast_channel *ast = call->ast;
1263 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1267 /* pbx not started */
1268 if (!call->pbx_started) {
1269 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1270 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1271 lcr_start_pbx(call, ast, param->information.sending_complete);
1275 /* change dailing state after setup */
1276 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1277 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1278 call->state = CHAN_LCR_STATE_IN_DIALING;
1279 // ast_setstate(ast, AST_STATE_DIALING);
1283 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1287 write(wake_pipe[1], &byte, 1);
1289 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1292 /* use bridge to forware message not supported by asterisk */
1293 if (call->state == CHAN_LCR_STATE_CONNECT) {
1294 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1295 bridge_message_if_bridged(call, message_type, param);
1300 * incoming information from LCR
1302 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1304 union parameter newparam;
1306 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1308 /* request bchannel, if call is resumed and we don't have it */
1309 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1310 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1311 memset(&newparam, 0, sizeof(union parameter));
1312 newparam.bchannel.type = BCHANNEL_REQUEST;
1313 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1316 if (!call->ast) return;
1318 /* use bridge to forware message not supported by asterisk */
1319 bridge_message_if_bridged(call, message_type, param);
1323 * incoming information from LCR
1325 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1327 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1329 if (!call->ast) return;
1331 /* use bridge to forware message not supported by asterisk */
1332 bridge_message_if_bridged(call, message_type, param);
1336 * incoming pattern from LCR
1338 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1340 union parameter newparam;
1342 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1344 if (!call->ast) return;
1346 /* pattern are indicated only once */
1347 if (call->has_pattern)
1349 call->has_pattern = 1;
1351 /* request bchannel */
1352 if (!call->bchannel) {
1353 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1354 memset(&newparam, 0, sizeof(union parameter));
1355 newparam.bchannel.type = BCHANNEL_REQUEST;
1356 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1358 /* queue PROGRESS, because tones are available */
1359 if (call->ast && call->pbx_started) {
1363 write(wake_pipe[1], &byte, 1);
1365 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1370 * got dtmf from bchannel (locked state)
1372 void lcr_in_dtmf(struct chan_call *call, int val)
1374 struct ast_channel *ast = call->ast;
1379 if (!call->pbx_started)
1382 if (!call->dsp_dtmf) {
1383 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1387 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1393 write(wake_pipe[1], &byte, 1);
1395 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1399 * message received from LCR
1401 int receive_message(int message_type, unsigned int ref, union parameter *param)
1403 struct bchannel *bchannel;
1404 struct chan_call *call;
1405 union parameter newparam;
1407 memset(&newparam, 0, sizeof(union parameter));
1409 /* handle bchannel message*/
1410 if (message_type == MESSAGE_BCHANNEL) {
1411 switch(param->bchannel.type) {
1412 case BCHANNEL_ASSIGN:
1413 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1414 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1415 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1418 /* create bchannel */
1419 bchannel = alloc_bchannel(param->bchannel.handle);
1421 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1425 /* configure channel */
1426 bchannel->b_tx_gain = param->bchannel.tx_gain;
1427 bchannel->b_rx_gain = param->bchannel.rx_gain;
1428 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1429 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1430 bchannel->b_bf_len = param->bchannel.crypt_len;
1431 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1433 bchannel->b_txdata = 0;
1434 bchannel->b_tx_dejitter = 1;
1436 /* in case, ref is not set, this bchannel instance must
1437 * be created until it is removed again by LCR */
1439 call = find_call_ref(ref);
1441 bchannel->call = call;
1442 call->bchannel = bchannel;
1444 bchannel_dtmf(bchannel, 1);
1446 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1447 if (call->pipeline[0])
1448 bchannel_pipeline(bchannel, call->pipeline);
1450 bchannel_gain(bchannel, call->rx_gain, 0);
1452 bchannel_gain(bchannel, call->tx_gain, 1);
1453 if (call->bridge_id) {
1454 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1455 bchannel_join(bchannel, call->bridge_id);
1457 /* ignore all dsp features, if it is a loopback interface */
1458 if (param->bchannel.isloopback)
1461 /* create only, if call exists, othewhise it bchannel is freed below... */
1462 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1463 bchannel_activate(bchannel, 1);
1466 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1467 newparam.bchannel.handle = param->bchannel.handle;
1468 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1469 /* if call has released before bchannel is assigned */
1471 newparam.bchannel.type = BCHANNEL_RELEASE;
1472 newparam.bchannel.handle = param->bchannel.handle;
1473 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1478 case BCHANNEL_REMOVE:
1479 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1480 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1481 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1484 /* unklink from call and destroy bchannel */
1485 free_bchannel(bchannel);
1488 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1489 newparam.bchannel.handle = param->bchannel.handle;
1490 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1495 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1500 /* handle new ref */
1501 if (message_type == MESSAGE_NEWREF) {
1502 if (param->newref.direction) {
1503 /* new ref from lcr */
1504 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1505 if (!ref || find_call_ref(ref)) {
1506 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1509 /* allocate new call instance */
1510 call = alloc_call();
1512 call->state = CHAN_LCR_STATE_IN_PREPARE;
1515 call->ref_was_assigned = 1;
1516 /* set dtmf (default, use option 'n' to disable */
1518 /* wait for setup (or release from asterisk) */
1520 /* new ref, as requested from this remote application */
1521 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1522 call = find_call_ref(0);
1524 /* send release, if ref does not exist */
1525 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1530 call->ref_was_assigned = 1;
1531 /* set dtmf (default, use option 'n' to disable */
1533 /* send pending setup info */
1534 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1535 send_setup_to_lcr(call);
1536 /* release if asterisk has signed off */
1537 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1540 send_release_and_import(call, call->cause, call->location);
1542 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1553 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1556 call = find_call_ref(ref);
1558 /* ignore ref that is not used (anymore) */
1559 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1563 /* handle messages */
1564 switch(message_type) {
1566 lcr_in_setup(call, message_type, param);
1569 case MESSAGE_OVERLAP:
1570 lcr_in_overlap(call, message_type, param);
1573 case MESSAGE_PROCEEDING:
1574 lcr_in_proceeding(call, message_type, param);
1577 case MESSAGE_ALERTING:
1578 lcr_in_alerting(call, message_type, param);
1581 case MESSAGE_CONNECT:
1582 lcr_in_connect(call, message_type, param);
1585 case MESSAGE_DISCONNECT:
1586 lcr_in_disconnect(call, message_type, param);
1589 case MESSAGE_RELEASE:
1590 lcr_in_release(call, message_type, param);
1593 case MESSAGE_INFORMATION:
1594 lcr_in_information(call, message_type, param);
1597 case MESSAGE_NOTIFY:
1598 lcr_in_notify(call, message_type, param);
1601 case MESSAGE_FACILITY:
1602 lcr_in_facility(call, message_type, param);
1605 case MESSAGE_PATTERN: // audio available from LCR
1606 if (!call->has_pattern)
1607 lcr_in_pattern(call, message_type, param);
1610 case MESSAGE_NOPATTERN: // audio not available from LCR
1613 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1614 call->audiopath = param->audiopath;
1618 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1625 * release all calls (due to broken socket)
1627 static void release_all_calls(void)
1629 struct chan_call *call;
1634 /* no ast, so we may directly free call */
1636 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1640 /* already in release process */
1641 if (call->state == CHAN_LCR_STATE_RELEASE) {
1645 /* release or queue release */
1647 call->state = CHAN_LCR_STATE_RELEASE;
1648 if (!call->pbx_started) {
1649 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1650 ast_hangup(call->ast); // call will be destroyed here
1653 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1657 write(wake_pipe[1], &byte, 1);
1659 strcpy(call->queue_string, "H");
1663 /* release all bchannels */
1664 while(bchannel_first)
1665 free_bchannel(bchannel_first);
1668 void close_socket(void);
1671 * warning! not thread safe
1672 * returns -1 for socket error, 0 for no work, 1 for work
1674 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1677 struct admin_list *admin;
1678 struct admin_message msg;
1680 if ((what & LCR_FD_READ)) {
1681 /* read from socket */
1682 len = read(lcr_sock, &msg, sizeof(msg));
1684 CERROR(NULL, NULL, "Socket closed.(read)\n");
1686 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1688 release_all_calls();
1689 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1693 if (len != sizeof(msg)) {
1694 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1697 if (msg.message != ADMIN_MESSAGE) {
1698 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1701 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1703 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1708 if ((what & LCR_FD_WRITE)) {
1709 /* write to socket */
1711 socket_fd.when &= ~LCR_FD_WRITE;
1714 admin = admin_first;
1715 len = write(lcr_sock, &admin->msg, sizeof(msg));
1717 CERROR(NULL, NULL, "Socket closed.(write)\n");
1721 if (len != sizeof(msg)) {
1722 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1726 admin_first = admin->next;
1730 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1739 * open and close socket and thread
1741 int open_socket(void)
1744 struct sockaddr_un sock_address;
1745 union parameter param;
1748 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1749 CERROR(NULL, NULL, "Failed to create socket.\n");
1753 /* set socket address and name */
1754 memset(&sock_address, 0, sizeof(sock_address));
1755 sock_address.sun_family = PF_UNIX;
1756 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1758 /* connect socket */
1759 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1762 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1766 /* register socket fd */
1767 memset(&socket_fd, 0, sizeof(socket_fd));
1768 socket_fd.fd = lcr_sock;
1769 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1771 /* enque hello message */
1772 memset(¶m, 0, sizeof(param));
1773 strcpy(param.hello.application, "asterisk");
1774 send_message(MESSAGE_HELLO, 0, ¶m);
1779 void close_socket(void)
1781 struct admin_list *admin, *temp;
1783 unregister_fd(&socket_fd);
1785 /* flush pending messages */
1786 admin = admin_first;
1789 admin = admin->next;
1802 /* sending queue to asterisk */
1803 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1807 read(wake_pipe[0], &byte, 1);
1814 static void handle_queue()
1816 struct chan_call *call;
1817 struct ast_channel *ast;
1818 struct ast_frame fr;
1824 p = call->queue_string;
1827 if (ast_channel_trylock(ast)) {
1828 ast_mutex_unlock(&chan_lock);
1830 ast_mutex_lock(&chan_lock);
1836 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1837 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1840 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1841 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1844 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1845 ast_queue_control(ast, AST_CONTROL_RINGING);
1846 ast_setstate(ast, AST_STATE_RINGING);
1849 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1850 ast_queue_control(ast, AST_CONTROL_ANSWER);
1853 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1854 ast_queue_hangup(ast);
1856 case '1': case '2': case '3': case 'A':
1857 case '4': case '5': case '6': case 'B':
1858 case '7': case '8': case '9': case 'C':
1859 case '*': case '0': case '#': case 'D':
1860 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1861 /* send digit to asterisk */
1862 memset(&fr, 0, sizeof(fr));
1864 #ifdef LCR_FOR_ASTERISK
1865 fr.frametype = AST_FRAME_DTMF_BEGIN;
1868 #ifdef LCR_FOR_CALLWEAVER
1869 fr.frametype = AST_FRAME_DTMF;
1872 #ifdef AST_1_8_OR_HIGHER
1873 fr.subclass.integer = *p;
1877 fr.delivery = ast_tv(0, 0);
1878 ast_queue_frame(ast, &fr);
1880 #ifdef LCR_FOR_ASTERISK
1881 fr.frametype = AST_FRAME_DTMF_END;
1882 ast_queue_frame(ast, &fr);
1887 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1891 call->queue_string[0] = '\0';
1892 ast_channel_unlock(ast);
1898 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1900 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1901 if (open_socket() < 0)
1902 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1907 void lock_chan(void)
1909 ast_mutex_lock(&chan_lock);
1912 void unlock_chan(void)
1914 ast_mutex_unlock(&chan_lock);
1917 /* chan_lcr thread */
1918 static void *chan_thread(void *arg)
1920 if (pipe(wake_pipe) < 0) {
1921 CERROR(NULL, NULL, "Failed to open pipe.\n");
1924 memset(&wake_fd, 0, sizeof(wake_fd));
1925 wake_fd.fd = wake_pipe[0];
1926 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1928 memset(&socket_retry, 0, sizeof(socket_retry));
1929 add_timer(&socket_retry, handle_retry, NULL, 0);
1931 bchannel_pid = getpid();
1933 /* open socket the first time */
1934 handle_retry(NULL, NULL, 0);
1936 ast_mutex_lock(&chan_lock);
1940 select_main(0, &global_change, lock_chan, unlock_chan);
1945 del_timer(&socket_retry);
1947 unregister_fd(&wake_fd);
1948 close(wake_pipe[0]);
1949 close(wake_pipe[1]);
1951 CERROR(NULL, NULL, "Thread exit.\n");
1953 ast_mutex_unlock(&chan_lock);
1959 * new asterisk instance
1962 #ifdef AST_1_8_OR_HIGHER
1963 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
1965 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1968 char exten[256], *dial, *interface, *opt;
1969 struct ast_channel *ast;
1970 struct chan_call *call;
1972 ast_mutex_lock(&chan_lock);
1973 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1975 /* if socket is closed */
1977 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1978 ast_mutex_unlock(&chan_lock);
1982 /* create call instance */
1983 call = alloc_call();
1985 /* failed to create instance */
1986 ast_mutex_unlock(&chan_lock);
1990 /* create asterisk channel instrance */
1992 #ifdef LCR_FOR_ASTERISK
1993 #ifdef AST_1_8_OR_HIGHER
1994 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
1996 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2000 #ifdef LCR_FOR_CALLWEAVER
2001 ast = ast_channel_alloc(1);
2005 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2007 /* failed to create instance */
2008 ast_mutex_unlock(&chan_lock);
2011 ast->tech = &lcr_tech;
2012 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2013 /* configure channel */
2014 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2015 ast->readformat = ast->rawreadformat = ast->nativeformats;
2016 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2018 ast->hangupcause = 0;
2022 ast->tech_pvt = call;
2023 ast->fds[0] = call->pipe[0];
2024 call->pbx_started = 0;
2026 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2029 * Extract interface, dialstring, options from data.
2032 * <interface>/<dialstring>
2033 * <interface>/<dialstring>/options
2035 strncpy(exten, (char *)data, sizeof(exten)-1);
2036 exten[sizeof(exten)-1] = '\0';
2037 if ((dial = strchr(exten, '/'))) {
2040 if ((opt = strchr(dial, '/')))
2049 strncpy(call->interface, interface, sizeof(call->interface)-1);
2050 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2051 apply_opt(call, (char *)opt);
2053 #ifdef AST_1_8_OR_HIGHER
2054 // clone_variables(requestor, ast);
2057 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2058 if (requestor->caller.ani.number.valid)
2059 if (requestor->caller.ani.number.str)
2060 if (requestor->caller.ani.number.str[0])
2061 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2062 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2063 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2065 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2066 if (requestor->caller.ani.name.valid)
2067 if (requestor->caller.ani.name.str)
2068 if (requestor->caller.ani.name.str[0])
2069 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2070 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2072 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2073 if (requestor->caller.ani.subaddress.valid)
2074 if (requestor->caller.ani.subaddress.str)
2075 if (requestor->caller.ani.subaddress.str[0])
2076 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2077 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2079 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2080 if (requestor->caller.id.number.valid)
2081 if (requestor->caller.id.number.str)
2082 if (requestor->caller.id.number.str[0])
2083 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2084 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2085 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2087 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2088 if (requestor->caller.id.name.valid)
2089 if (requestor->caller.id.name.str)
2090 if (requestor->caller.id.name.str[0])
2091 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2092 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2094 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2095 if (requestor->caller.id.subaddress.valid)
2096 if (requestor->caller.id.subaddress.str)
2097 if (requestor->caller.id.subaddress.str[0])
2098 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2099 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2101 if (requestor->dialed.number.str)
2102 if (requestor->dialed.number.str[0])
2103 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2104 ast->dialed.number.plan= requestor->dialed.number.plan;
2106 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2107 if (requestor->dialed.subaddress.valid)
2108 if (requestor->dialed.subaddress.str)
2109 if (requestor->dialed.subaddress.str[0])
2110 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2111 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2113 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2114 ast->redirecting.count= requestor->redirecting.count;
2115 ast->redirecting.reason= requestor->redirecting.reason;
2117 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2118 if (requestor->redirecting.from.number.valid)
2119 if (requestor->redirecting.from.number.str)
2120 if (requestor->redirecting.from.number.str[0])
2121 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2122 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2123 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2125 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2126 if (requestor->redirecting.to.number.valid)
2127 if (requestor->redirecting.to.number.str)
2128 if (requestor->redirecting.to.number.str[0])
2129 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2130 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2131 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2133 /* store call information for setup */
2136 if (requestor->caller.id.number.valid) {
2137 if (requestor->caller.id.number.str)
2138 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2139 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2140 case AST_PRES_RESTRICTED:
2141 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2143 case AST_PRES_UNAVAILABLE:
2144 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2146 case AST_PRES_ALLOWED:
2148 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2150 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2151 case AST_PRES_USER_NUMBER_UNSCREENED:
2152 call->callerinfo.screen = INFO_SCREEN_USER;
2154 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2155 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2157 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2158 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2161 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2163 switch((requestor->caller.id.number.plan >> 4) & 7) {
2165 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2168 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2171 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2174 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2177 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2180 if (requestor->caller.ani.number.valid) {
2181 if (requestor->caller.ani.number.str)
2182 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2183 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2184 case AST_PRES_RESTRICTED:
2185 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2187 case AST_PRES_UNAVAILABLE:
2188 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2190 case AST_PRES_ALLOWED:
2192 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2194 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2195 case AST_PRES_USER_NUMBER_UNSCREENED:
2196 call->callerinfo.screen2 = INFO_SCREEN_USER;
2198 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2199 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2201 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2202 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2205 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2207 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2209 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2212 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2215 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2218 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2221 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2224 if (requestor->caller.id.name.valid) {
2225 if (requestor->caller.id.name.str)
2226 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2230 if (requestor->redirecting.from.number.valid) {
2231 call->redirinfo.itype = INFO_ITYPE_CHAN;
2232 if (requestor->redirecting.from.number.str)
2233 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2234 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2235 case AST_PRES_RESTRICTED:
2236 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2238 case AST_PRES_UNAVAILABLE:
2239 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2241 case AST_PRES_ALLOWED:
2243 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2245 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2246 case AST_PRES_USER_NUMBER_UNSCREENED:
2247 call->redirinfo.screen = INFO_SCREEN_USER;
2249 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2250 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2252 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2253 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2256 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2258 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2260 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2263 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2266 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2269 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2274 ast_mutex_unlock(&chan_lock);
2279 * call from asterisk
2281 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2283 union parameter newparam;
2284 struct chan_call *call;
2286 ast_mutex_lock(&chan_lock);
2287 call = ast->tech_pvt;
2289 #ifdef LCR_FOR_CALLWEAVER
2291 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
2295 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2296 ast_mutex_unlock(&chan_lock);
2300 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2302 /* pbx process is started */
2303 call->pbx_started = 1;
2304 /* send MESSAGE_NEWREF */
2305 memset(&newparam, 0, sizeof(union parameter));
2306 newparam.newref.direction = 0; /* request from app */
2307 if (!strcmp(call->interface, "pbx"))
2308 newparam.newref.mode = 1;
2309 send_message(MESSAGE_NEWREF, 0, &newparam);
2311 /* set hdlc if capability requires hdlc */
2312 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2313 || ast->transfercapability == INFO_BC_DATARESTRICTED
2314 || ast->transfercapability == INFO_BC_VIDEO)
2316 /* if hdlc is forced by option, we change transcap to data */
2318 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2319 && ast->transfercapability != INFO_BC_DATARESTRICTED
2320 && ast->transfercapability != INFO_BC_VIDEO)
2321 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2323 #ifndef AST_1_8_OR_HIGHER
2324 call->cid_num[0] = 0;
2325 call->cid_name[0] = 0;
2326 call->cid_rdnis[0] = 0;
2328 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2329 strncpy(call->cid_num, ast->cid.cid_num,
2330 sizeof(call->cid_num)-1);
2331 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2332 strncpy(call->cid_name, ast->cid.cid_name,
2333 sizeof(call->cid_name)-1);
2334 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2335 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2336 sizeof(call->cid_rdnis)-1);
2339 ast_mutex_unlock(&chan_lock);
2343 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2345 static const char* dtmf_tones[] = {
2346 "!941+1336/100,!0/100", /* 0 */
2347 "!697+1209/100,!0/100", /* 1 */
2348 "!697+1336/100,!0/100", /* 2 */
2349 "!697+1477/100,!0/100", /* 3 */
2350 "!770+1209/100,!0/100", /* 4 */
2351 "!770+1336/100,!0/100", /* 5 */
2352 "!770+1477/100,!0/100", /* 6 */
2353 "!852+1209/100,!0/100", /* 7 */
2354 "!852+1336/100,!0/100", /* 8 */
2355 "!852+1477/100,!0/100", /* 9 */
2356 "!697+1633/100,!0/100", /* A */
2357 "!770+1633/100,!0/100", /* B */
2358 "!852+1633/100,!0/100", /* C */
2359 "!941+1633/100,!0/100", /* D */
2360 "!941+1209/100,!0/100", /* * */
2361 "!941+1477/100,!0/100" }; /* # */
2363 if (digit >= '0' && digit <='9')
2364 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2365 else if (digit >= 'A' && digit <= 'D')
2366 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2367 else if (digit == '*')
2368 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2369 else if (digit == '#')
2370 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2373 CDEBUG(NULL, ast, "Unable to handle DTMF tone "
2374 "'%c' for '%s'\n", digit, ast->name);
2378 #ifdef LCR_FOR_ASTERISK
2379 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2381 #ifdef LCR_FOR_CALLWEAVER
2382 static int lcr_digit(struct ast_channel *ast, char digit)
2385 struct chan_call *call;
2386 union parameter newparam;
2389 #ifdef LCR_FOR_CALLWEAVER
2390 int inband_dtmf = 0;
2393 /* only pass IA5 number space */
2394 if (digit > 126 || digit < 32)
2397 ast_mutex_lock(&chan_lock);
2398 call = ast->tech_pvt;
2400 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2401 ast_mutex_unlock(&chan_lock);
2405 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2407 /* send information or queue them */
2408 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2409 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2410 memset(&newparam, 0, sizeof(union parameter));
2412 newparam.information.keypad[0] = digit;
2413 newparam.information.keypad[1] = '\0';
2415 newparam.information.id[0] = digit;
2416 newparam.information.id[1] = '\0';
2418 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2421 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2422 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2424 strncat(call->dialque, buf, strlen(call->dialque)-1);
2427 ast_mutex_unlock(&chan_lock);
2429 #ifdef LCR_FOR_ASTERISK
2433 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2435 int inband_dtmf = 0;
2436 struct chan_call *call;
2439 ast_mutex_lock(&chan_lock);
2441 call = ast->tech_pvt;
2445 "Received digit from Asterisk, "
2446 "but no call instance exists.\n");
2447 ast_mutex_unlock(&chan_lock);
2451 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2453 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2457 ast_mutex_unlock(&chan_lock);
2460 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2461 send_digit_to_chan(ast, digit);
2467 static int lcr_answer(struct ast_channel *ast)
2469 union parameter newparam;
2470 struct chan_call *call;
2472 ast_mutex_lock(&chan_lock);
2473 call = ast->tech_pvt;
2475 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2476 ast_mutex_unlock(&chan_lock);
2480 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2482 /* copy connectinfo, if bridged */
2483 if (call->bridge_call)
2484 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2485 /* send connect message to lcr */
2486 if (call->state != CHAN_LCR_STATE_CONNECT) {
2487 memset(&newparam, 0, sizeof(union parameter));
2488 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2489 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2490 call->state = CHAN_LCR_STATE_CONNECT;
2493 /* request bchannel */
2494 if (!call->bchannel) {
2495 CDEBUG(call, ast, "Requesting B-channel.\n");
2496 memset(&newparam, 0, sizeof(union parameter));
2497 newparam.bchannel.type = BCHANNEL_REQUEST;
2498 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2501 // memset(&newparam, 0, sizeof(union parameter));
2502 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2504 ast_mutex_unlock(&chan_lock);
2508 static int lcr_hangup(struct ast_channel *ast)
2510 struct chan_call *call;
2511 pthread_t tid = pthread_self();
2513 if (!pthread_equal(tid, chan_tid)) {
2514 ast_mutex_lock(&chan_lock);
2516 call = ast->tech_pvt;
2518 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2519 if (!pthread_equal(tid, chan_tid)) {
2520 ast_mutex_unlock(&chan_lock);
2525 if (!pthread_equal(tid, chan_tid))
2526 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2528 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2530 /* disconnect asterisk, maybe not required */
2531 ast->tech_pvt = NULL;
2535 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2536 if (ast->hangupcause > 0)
2537 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2539 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2542 if (!pthread_equal(tid, chan_tid)) {
2543 ast_mutex_unlock(&chan_lock);
2547 /* ref is not set, due to prepare setup or release */
2548 if (call->state == CHAN_LCR_STATE_RELEASE) {
2549 /* we get the response to our release */
2550 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2553 /* during prepare, we change to release state */
2554 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2555 call->state = CHAN_LCR_STATE_RELEASE;
2559 if (!pthread_equal(tid, chan_tid)) {
2560 ast_mutex_unlock(&chan_lock);
2565 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2567 struct chan_call *call;
2568 struct ast_frame * f = fr;
2570 #ifdef AST_1_8_OR_HIGHER
2571 if (!f->subclass.integer)
2575 CDEBUG(NULL, ast, "No subclass\n");
2576 #ifdef AST_1_8_OR_HIGHER
2577 if (!(f->subclass.integer & ast->nativeformats)) {
2579 if (!(f->subclass & ast->nativeformats)) {
2582 "Unexpected format. "
2583 "Activating emergency conversion...\n");
2585 #ifdef AST_1_8_OR_HIGHER
2586 ast_set_write_format(ast, f->subclass.integer);
2588 ast_set_write_format(ast, f->subclass);
2590 f = (ast->writetrans) ? ast_translate(
2591 ast->writetrans, fr, 0) : fr;
2594 ast_mutex_lock(&chan_lock);
2595 call = ast->tech_pvt;
2597 ast_mutex_unlock(&chan_lock);
2603 if (call->bchannel && f->samples)
2604 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2605 ast_mutex_unlock(&chan_lock);
2613 static struct ast_frame *lcr_read(struct ast_channel *ast)
2615 struct chan_call *call;
2618 ast_mutex_lock(&chan_lock);
2619 call = ast->tech_pvt;
2621 ast_mutex_unlock(&chan_lock);
2624 if (call->pipe[0] > -1) {
2625 if (call->rebuffer && !call->hdlc) {
2626 /* Make sure we have a complete 20ms (160byte) frame */
2627 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2629 call->framepos += len;
2632 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2634 if (len < 0 && errno == EAGAIN) {
2635 ast_mutex_unlock(&chan_lock);
2637 #ifdef LCR_FOR_ASTERISK
2638 return &ast_null_frame;
2641 #ifdef LCR_FOR_CALLWEAVER
2647 close(call->pipe[0]);
2650 ast_mutex_unlock(&chan_lock);
2652 } else if (call->rebuffer && call->framepos < 160) {
2653 /* Not a complete frame, so we send a null-frame */
2654 ast_mutex_unlock(&chan_lock);
2655 return &ast_null_frame;
2659 call->read_fr.frametype = AST_FRAME_VOICE;
2660 #ifdef AST_1_8_OR_HIGHER
2661 call->read_fr.subclass.integer = ast->nativeformats;
2663 call->read_fr.subclass = ast->nativeformats;
2665 if (call->rebuffer) {
2666 call->read_fr.datalen = call->framepos;
2667 call->read_fr.samples = call->framepos;
2670 call->read_fr.datalen = len;
2671 call->read_fr.samples = len;
2673 call->read_fr.delivery = ast_tv(0,0);
2674 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2675 ast_mutex_unlock(&chan_lock);
2677 return &call->read_fr;
2680 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2682 union parameter newparam;
2684 struct chan_call *call;
2685 const struct tone_zone_sound *ts = NULL;
2687 ast_mutex_lock(&chan_lock);
2688 call = ast->tech_pvt;
2690 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2691 ast_mutex_unlock(&chan_lock);
2696 case AST_CONTROL_BUSY:
2697 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2698 ast_setstate(ast, AST_STATE_BUSY);
2699 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2700 /* send message to lcr */
2701 memset(&newparam, 0, sizeof(union parameter));
2702 newparam.disconnectinfo.cause = 17;
2703 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2704 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2706 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2708 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2709 ts = ast_get_indication_tone(ast->zone, "busy");
2712 case AST_CONTROL_CONGESTION:
2713 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2714 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2715 /* send message to lcr */
2716 memset(&newparam, 0, sizeof(union parameter));
2717 newparam.disconnectinfo.cause = ast->hangupcause;
2718 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2719 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2721 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2723 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2724 ts = ast_get_indication_tone(ast->zone, "congestion");
2727 case AST_CONTROL_PROCEEDING:
2728 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2729 if (call->state == CHAN_LCR_STATE_IN_SETUP
2730 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2731 /* send message to lcr */
2732 memset(&newparam, 0, sizeof(union parameter));
2733 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2735 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2738 case AST_CONTROL_RINGING:
2739 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2740 ast_setstate(ast, AST_STATE_RING);
2741 if (call->state == CHAN_LCR_STATE_IN_SETUP
2742 || call->state == CHAN_LCR_STATE_IN_DIALING
2743 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2744 /* send message to lcr */
2745 memset(&newparam, 0, sizeof(union parameter));
2746 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2748 call->state = CHAN_LCR_STATE_IN_ALERTING;
2750 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2751 ts = ast_get_indication_tone(ast->zone, "ring");
2754 case AST_CONTROL_PROGRESS:
2755 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2756 /* request bchannel */
2757 if (!call->bchannel) {
2758 CDEBUG(call, ast, "Requesting B-channel.\n");
2759 memset(&newparam, 0, sizeof(union parameter));
2760 newparam.bchannel.type = BCHANNEL_REQUEST;
2761 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2765 CDEBUG(call, ast, "Received indicate -1.\n");
2766 ast_playtones_stop(ast);
2770 case AST_CONTROL_VIDUPDATE:
2771 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2774 case AST_CONTROL_HOLD:
2775 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2776 /* send message to lcr */
2777 memset(&newparam, 0, sizeof(union parameter));
2778 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2779 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2781 /*start music onhold*/
2782 #ifdef LCR_FOR_ASTERISK
2783 ast_moh_start(ast,data,ast->musicclass);
2786 #ifdef LCR_FOR_CALLWEAVER
2787 ast_moh_start(ast, NULL);
2792 case AST_CONTROL_UNHOLD:
2793 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2794 /* send message to lcr */
2795 memset(&newparam, 0, sizeof(union parameter));
2796 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2797 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2803 #ifdef AST_CONTROL_SRCUPDATE
2804 case AST_CONTROL_SRCUPDATE:
2808 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2811 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2816 if (ts && ts->data[0]) {
2817 ast_playtones_start(ast, 0, ts->data, 1);
2821 ast_mutex_unlock(&chan_lock);
2828 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2830 struct chan_call *call;
2836 ast_mutex_lock(&chan_lock);
2837 call = ast->tech_pvt;
2839 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2840 ast_mutex_unlock(&chan_lock);
2844 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2846 ast_mutex_unlock(&chan_lock);
2851 * send_text asterisk
2853 static int lcr_send_text(struct ast_channel *ast, const char *text)
2855 struct chan_call *call;
2856 union parameter newparam;
2858 ast_mutex_lock(&chan_lock);
2859 call = ast->tech_pvt;
2861 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2862 ast_mutex_unlock(&chan_lock);
2866 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2867 memset(&newparam, 0, sizeof(union parameter));
2868 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2869 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2870 ast_mutex_unlock(&chan_lock);
2877 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2878 struct ast_channel *ast2, int flags,
2879 struct ast_frame **fo,
2880 struct ast_channel **rc, int timeoutms)
2883 struct chan_call *call1, *call2;
2884 struct ast_channel *carr[2], *who;
2886 struct ast_frame *f;
2889 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2894 /* join via dsp (if the channels are currently open) */
2895 ast_mutex_lock(&chan_lock);
2896 call1 = ast1->tech_pvt;
2897 call2 = ast2->tech_pvt;
2898 if (!call1 || !call2) {
2899 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2900 ast_mutex_unlock(&chan_lock);
2901 return AST_BRIDGE_COMPLETE;
2904 /* join, if both call instances uses dsp
2905 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2907 if (!call1->nodsp && !call2->nodsp) {
2908 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2910 /* get bridge id and join */
2911 bridge_id = new_bridge_id();
2913 call1->bridge_id = bridge_id;
2914 if (call1->bchannel)
2915 bchannel_join(call1->bchannel, bridge_id);
2917 call2->bridge_id = bridge_id;
2918 if (call2->bchannel)
2919 bchannel_join(call2->bchannel, bridge_id);
2921 if (call1->nodsp && call2->nodsp)
2922 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2924 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2925 call1->bridge_call = call2;
2926 call2->bridge_call = call1;
2928 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2929 || call1->state == CHAN_LCR_STATE_IN_DIALING
2930 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2931 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2932 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2935 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2936 || call2->state == CHAN_LCR_STATE_IN_DIALING
2937 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2938 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2939 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2943 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2944 so let's do it for them. Hmpf.
2947 if (call1->on_hold) {
2948 union parameter newparam;
2950 memset(&newparam, 0, sizeof(union parameter));
2951 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2952 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2957 if (call2->on_hold) {
2958 union parameter newparam;
2960 memset(&newparam, 0, sizeof(union parameter));
2961 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2962 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2967 ast_mutex_unlock(&chan_lock);
2971 who = ast_waitfor_n(carr, 2, &to);
2974 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2979 if (!f || f->frametype == AST_FRAME_CONTROL) {
2981 CDEBUG(NULL, NULL, "Got hangup.\n");
2983 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2990 if ( f->frametype == AST_FRAME_DTMF ) {
2991 CDEBUG(NULL, NULL, "Got DTMF.\n");
3007 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3009 /* split channels */
3010 ast_mutex_lock(&chan_lock);
3011 call1 = ast1->tech_pvt;
3012 call2 = ast2->tech_pvt;
3013 if (call1 && call1->bridge_id) {
3014 call1->bridge_id = 0;
3015 if (call1->bchannel)
3016 bchannel_join(call1->bchannel, 0);
3017 if (call1->bridge_call)
3018 call1->bridge_call->bridge_call = NULL;
3020 if (call2 && call1->bridge_id) {
3021 call2->bridge_id = 0;
3022 if (call2->bchannel)
3023 bchannel_join(call2->bchannel, 0);
3024 if (call2->bridge_call)
3025 call2->bridge_call->bridge_call = NULL;
3027 call1->bridge_call = NULL;
3028 call2->bridge_call = NULL;
3030 ast_mutex_unlock(&chan_lock);
3031 return AST_BRIDGE_COMPLETE;
3033 static struct ast_channel_tech lcr_tech = {
3035 .description = "Channel driver for connecting to Linux-Call-Router",
3036 .capabilities = AST_FORMAT_ALAW,
3037 .requester = lcr_request,
3039 #ifdef LCR_FOR_ASTERISK
3040 .send_digit_begin = lcr_digit_begin,
3041 .send_digit_end = lcr_digit_end,
3044 #ifdef LCR_FOR_CALLWEAVER
3045 .send_digit = lcr_digit,
3049 .bridge = lcr_bridge,
3050 .hangup = lcr_hangup,
3051 .answer = lcr_answer,
3054 .indicate = lcr_indicate,
3056 .send_text = lcr_send_text,
3065 static int lcr_show_lcr (int fd, int argc, char *argv[])
3070 static int lcr_show_calls (int fd, int argc, char *argv[])
3075 static int lcr_reload_routing (int fd, int argc, char *argv[])
3080 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3085 static int lcr_port_block (int fd, int argc, char *argv[])
3090 static int lcr_port_unblock (int fd, int argc, char *argv[])
3095 static int lcr_port_unload (int fd, int argc, char *argv[])
3100 static struct ast_cli_entry cli_show_lcr =
3101 { {"lcr", "show", "lcr", NULL},
3103 "Shows current states of LCR core",
3104 "Usage: lcr show lcr\n",
3107 static struct ast_cli_entry cli_show_calls =
3108 { {"lcr", "show", "calls", NULL},
3110 "Shows current calls made by LCR and Asterisk",
3111 "Usage: lcr show calls\n",
3114 static struct ast_cli_entry cli_reload_routing =
3115 { {"lcr", "reload", "routing", NULL},
3117 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3118 "Usage: lcr reload routing\n",
3121 static struct ast_cli_entry cli_reload_interfaces =
3122 { {"lcr", "reload", "interfaces", NULL},
3123 lcr_reload_interfaces,
3124 "Reloads interfaces conf of LCR",
3125 "Usage: lcr reload interfaces\n",
3128 static struct ast_cli_entry cli_port_block =
3129 { {"lcr", "port", "block", NULL},
3131 "Blocks LCR port for further calls",
3132 "Usage: lcr port block \"<port>\"\n",
3135 static struct ast_cli_entry cli_port_unblock =
3136 { {"lcr", "port", "unblock", NULL},
3138 "Unblocks or loads LCR port, port is opened my mISDN",
3139 "Usage: lcr port unblock \"<port>\"\n",
3142 static struct ast_cli_entry cli_port_unload =
3143 { {"lcr", "port", "unload", NULL},
3145 "Unloads LCR port, port is closes by mISDN",
3146 "Usage: lcr port unload \"<port>\"\n",
3151 #ifdef LCR_FOR_ASTERISK
3152 #ifdef AST_1_8_OR_HIGHER
3153 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3155 static int lcr_config_exec(struct ast_channel *ast, void *data)
3159 #ifdef LCR_FOR_CALLWEAVER
3160 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3163 struct chan_call *call;
3165 ast_mutex_lock(&chan_lock);
3167 #ifdef LCR_FOR_ASTERISK
3168 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3171 #ifdef LCR_FOR_CALLWEAVER
3172 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3178 if (call->ast == ast)
3184 #ifdef LCR_FOR_ASTERISK
3185 apply_opt(call, (char *)data);
3188 #ifdef LCR_FOR_CALLWEAVER
3189 apply_opt(call, (char *)argv[0]);
3193 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3195 ast_mutex_unlock(&chan_lock);
3200 * module loading and destruction
3202 int load_module(void)
3205 char options_error[256];
3207 for (i = 0; i < 256; i++) {
3208 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3209 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3212 if (read_options(options_error) == 0) {
3213 CERROR(NULL, NULL, "%s", options_error);
3215 #ifdef LCR_FOR_ASTERISK
3216 return AST_MODULE_LOAD_DECLINE;
3219 #ifdef LCR_FOR_CALLWEAVER
3225 ast_mutex_init(&chan_lock);
3226 ast_mutex_init(&log_lock);
3228 if (bchannel_initialize()) {
3229 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3232 #ifdef LCR_FOR_ASTERISK
3233 return AST_MODULE_LOAD_DECLINE;
3236 #ifdef LCR_FOR_CALLWEAVER
3242 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3243 if (ast_channel_register(&lcr_tech)) {
3244 CERROR(NULL, NULL, "Unable to register channel class\n");
3245 bchannel_deinitialize();
3248 #ifdef LCR_FOR_ASTERISK
3249 return AST_MODULE_LOAD_DECLINE;
3252 #ifdef LCR_FOR_CALLWEAVER
3257 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3259 #ifdef LCR_FOR_ASTERISK
3260 "lcr_config(<opt><optarg>:<opt>:...)\n"
3263 #ifdef LCR_FOR_CALLWEAVER
3264 "lcr_config(<opt><optarg>:<opt>:...)\n",
3267 "Sets LCR opts. and optargs\n"
3269 "The available options are:\n"
3270 " d - Send display text on called phone, text is the optarg.\n"
3271 " n - Don't detect dtmf tones on called channel.\n"
3272 " h - Force data call (HDLC).\n"
3273 " t - Disable mISDN_dsp features (required for fax application).\n"
3274 " q - Add queue to make fax stream seamless (required for fax app).\n"
3275 " Use queue size in miliseconds for optarg. (try 250)\n"
3276 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3277 " Use time to detect for optarg.\n"
3278 " c - Make crypted outgoing call, optarg is keyindex.\n"
3279 " e - Perform echo cancelation on this channel.\n"
3280 " Takes mISDN pipeline option as optarg.\n"
3281 " s - Send Non Inband DTMF as inband.\n"
3282 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3283 " vr - rxgain control\n"
3284 " vt - txgain control\n"
3285 " Volume changes at factor 2 ^ optarg.\n"
3286 " k - use keypad to dial this call.\n"
3288 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3289 " -> use 16 for fax (3.1k audio)\n"
3291 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3292 "options: \"n:t:q250\" for seamless audio transmission.\n"
3297 ast_cli_register(&cli_show_lcr);
3298 ast_cli_register(&cli_show_calls);
3299 ast_cli_register(&cli_reload_routing);
3300 ast_cli_register(&cli_reload_interfaces);
3301 ast_cli_register(&cli_port_block);
3302 ast_cli_register(&cli_port_unblock);
3303 ast_cli_register(&cli_port_unload);
3307 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3308 /* failed to create thread */
3309 bchannel_deinitialize();
3311 ast_channel_unregister(&lcr_tech);
3313 #ifdef LCR_FOR_ASTERISK
3314 return AST_MODULE_LOAD_DECLINE;
3317 #ifdef LCR_FOR_CALLWEAVER
3325 int unload_module(void)
3327 /* First, take us out of the channel loop */
3328 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
3331 pthread_join(chan_tid, NULL);
3333 ast_channel_unregister(&lcr_tech);
3335 ast_unregister_application("lcr_config");
3338 if (mISDN_created) {
3339 bchannel_deinitialize();
3343 if (lcr_sock >= 0) {
3351 int reload_module(void)
3357 #ifdef LCR_FOR_ASTERISK
3358 #define AST_MODULE "chan_lcr"
3361 #ifdef LCR_FOR_CALLWEAVER
3366 ast_mutex_lock(&usecnt_lock);
3368 ast_mutex_unlock(&usecnt_lock);
3373 #ifdef LCR_FOR_ASTERISK
3374 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3375 .load = load_module,
3376 .unload = unload_module,
3377 .reload = reload_module,
3381 #ifdef LCR_FOR_CALLWEAVER
3382 char *description(void)