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;
226 void lock_debug(char *text)
228 pthread_t tid = pthread_self();
229 // printf("%s|%03x\n", text, ((tid>>6) | (tid>>3) | tid) & 0xfff); fflush(stdout);
230 printf("%s|%x\n", text, tid); fflush(stdout);
236 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, ...)
239 char call_text[128] = "NULL";
240 char ast_text[128] = "NULL";
244 ast_mutex_lock(&log_lock);
247 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
248 buffer[sizeof(buffer)-1]=0;
252 sprintf(call_text, "%d", call->ref);
254 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
255 ast_text[sizeof(ast_text)-1] = '\0';
257 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
259 ast_mutex_unlock(&log_lock);
264 * channel and call instances
266 struct chan_call *call_first;
270 * special case: 0: find new ref, that has not been assigned a ref yet
273 struct chan_call *find_call_ref(unsigned int ref)
275 struct chan_call *call = call_first;
276 int assigned = (ref > 0);
279 if (call->ref == ref && call->ref_was_assigned == assigned)
286 void free_call(struct chan_call *call)
288 struct chan_call **temp = &call_first;
292 *temp = (*temp)->next;
293 if (call->pipe[0] > -1)
294 close(call->pipe[0]);
295 if (call->pipe[1] > -1)
296 close(call->pipe[1]);
297 if (call->bchannel) {
298 if (call->bchannel->call != call)
299 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
300 call->bchannel->call = NULL;
302 if (call->bridge_call) {
303 if (call->bridge_call->bridge_call != call)
304 CERROR(call, NULL, "Linked call structure has no link to us.\n");
305 call->bridge_call->bridge_call = NULL;
308 ast_translator_free_path(call->trans);
310 ast_dsp_free(call->dsp);
311 CDEBUG(call, NULL, "Call instance freed.\n");
316 temp = &((*temp)->next);
318 CERROR(call, NULL, "Call instance not found in list.\n");
321 struct chan_call *alloc_call(void)
323 struct chan_call **callp = &call_first;
326 callp = &((*callp)->next);
328 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
330 memset(*callp, 0, sizeof(struct chan_call));
331 if (pipe((*callp)->pipe) < 0) {
332 CERROR(*callp, NULL, "Failed to create pipe.\n");
336 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
337 CDEBUG(*callp, NULL, "Call instance allocated.\n");
341 unsigned short new_bridge_id(void)
343 struct chan_call *call;
344 unsigned short id = 1;
346 /* search for lowest bridge id that is not in use and not 0 */
350 if (call->bridge_id == id)
358 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
363 * enque message to LCR
365 int send_message(int message_type, unsigned int ref, union parameter *param)
367 struct admin_list *admin, **adminp;
370 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
373 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
375 adminp = &admin_first;
377 adminp = &((*adminp)->next);
378 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
380 CERROR(NULL, NULL, "No memory for message to LCR.\n");
385 admin->msg.message = ADMIN_MESSAGE;
386 admin->msg.u.msg.type = message_type;
387 admin->msg.u.msg.ref = ref;
388 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
389 socket_fd.when |= LCR_FD_WRITE;
393 write(wake_pipe[1], &byte, 1);
400 * apply options (in locked state)
402 void apply_opt(struct chan_call *call, char *data)
404 union parameter newparam;
405 char string[1024], *p = string, *opt, *key;
411 strncpy(string, data, sizeof(string)-1);
412 string[sizeof(string)-1] = '\0';
415 while((opt = strsep(&p, ":"))) {
418 if (opt[1] == '\0') {
419 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
422 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
423 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
424 strncpy(call->display, opt+1, sizeof(call->display)-1);
426 memset(&newparam, 0, sizeof(union parameter));
427 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
428 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
432 if (opt[1] != '\0') {
433 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
436 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
437 if (call->dsp_dtmf) {
440 bchannel_dtmf(call->bchannel, 0);
444 if (opt[1] == '\0') {
445 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
449 /* check for 0xXXXX... type of key */
450 if (!!strncmp((char *)key, "0x", 2)) {
451 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
455 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
456 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
461 if (*key>='0' && *key<='9')
462 call->bf_key[i] = (*key-'0') << 8;
463 else if (*key>='a' && *key<='f')
464 call->bf_key[i] = (*key-'a'+10) << 8;
465 else if (*key>='A' && *key<='F')
466 call->bf_key[i] = (*key-'A'+10) << 8;
470 if (*key>='0' && *key<='9')
471 call->bf_key[i] += (*key - '0');
472 else if (*key>='a' && *key<='f')
473 call->bf_key[i] += (*key - 'a' + 10);
474 else if (*key>='A' && *key<='F')
475 call->bf_key[i] += (*key - 'A' + 10);
482 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
486 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
488 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
491 if (opt[1] != '\0') {
492 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
495 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
500 if (opt[1] != '\0') {
501 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
504 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
509 if (opt[1] == '\0') {
510 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
513 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
514 call->nodsp_queue = atoi(opt+1);
517 if (opt[1] == '\0') {
518 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
521 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
522 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
524 bchannel_pipeline(call->bchannel, call->pipeline);
527 if (opt[1] == '\0') {
528 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
531 call->faxdetect=atoi(opt+1);
533 call->dsp=ast_dsp_new();
535 #ifdef LCR_FOR_CALLWEAVER
536 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
538 #ifdef LCR_FOR_ASTERISK
539 #ifdef DSP_FEATURE_DTMF_DETECT
540 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
542 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
547 #ifdef LCR_FOR_CALLWEAVER
548 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
550 #ifdef LCR_FOR_ASTERISK
551 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
554 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
557 if (opt[1] != '\0') {
558 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
561 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
566 if (opt[1] != '\0') {
567 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
570 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
571 call->inband_dtmf = 1;
574 if (opt[1] != 'r' && opt[1] != 't') {
575 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
579 if (gain < -8 || gain >8) {
580 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
583 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
585 call->rx_gain = gain;
587 bchannel_gain(call->bchannel, call->rx_gain, 0);
589 call->tx_gain = gain;
591 bchannel_gain(call->bchannel, call->tx_gain, 1);
595 if (opt[1] != '\0') {
596 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
599 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
604 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
608 /* re-open, if bchannel is created */
609 if (call->bchannel && call->bchannel->b_sock > -1) {
610 bchannel_destroy(call->bchannel);
611 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
612 bchannel_activate(call->bchannel, 1);
617 * send setup info to LCR
618 * this function is called, when asterisk call is received and ref is received
620 static void send_setup_to_lcr(struct chan_call *call)
622 union parameter newparam;
623 struct ast_channel *ast = call->ast;
626 if (!call->ast || !call->ref)
629 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
631 /* send setup message to LCR */
632 memset(&newparam, 0, sizeof(union parameter));
633 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
634 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
636 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
638 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
639 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
640 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
641 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
642 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
643 call->display[0] = '\0';
644 if (call->cid_num[0])
645 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
646 if (call->cid_name[0])
647 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
648 if (call->cid_rdnis[0]) {
649 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
650 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
651 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
653 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
654 case AST_PRES_RESTRICTED:
655 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
657 case AST_PRES_UNAVAILABLE:
658 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
660 case AST_PRES_ALLOWED:
662 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
664 switch(ast->cid.cid_ton) {
666 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
669 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
672 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
675 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
677 tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
679 ast->transfercapability = atoi(tmp);
680 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
681 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
683 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
685 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
686 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
688 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
689 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
690 send_message(MESSAGE_SETUP, call->ref, &newparam);
692 /* change to outgoing setup state */
693 call->state = CHAN_LCR_STATE_OUT_SETUP;
697 * send dialing info to LCR
698 * this function is called, when setup acknowledge is received and dialing
701 static void send_dialque_to_lcr(struct chan_call *call)
703 union parameter newparam;
705 if (!call->ast || !call->ref || !call->dialque[0])
708 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
710 /* send setup message to LCR */
711 memset(&newparam, 0, sizeof(union parameter));
713 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
715 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
716 call->dialque[0] = '\0';
717 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
721 * in case of a bridge, the unsupported message can be forwarded directly
722 * to the remote call.
724 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
728 if (!call->bridge_call) return;
729 CDEBUG(call, NULL, "Sending message due bridging.\n");
730 send_message(message_type, call->bridge_call->ref, param);
734 * send release message to LCR and import bchannel if exported
736 static void send_release_and_import(struct chan_call *call, int cause, int location)
738 union parameter newparam;
740 /* importing channel */
741 if (call->bchannel) {
742 memset(&newparam, 0, sizeof(union parameter));
743 newparam.bchannel.type = BCHANNEL_RELEASE;
744 newparam.bchannel.handle = call->bchannel->handle;
745 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
747 /* sending release */
748 memset(&newparam, 0, sizeof(union parameter));
749 newparam.disconnectinfo.cause = cause;
750 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
751 send_message(MESSAGE_RELEASE, call->ref, &newparam);
755 * check if extension matches and start asterisk
756 * if it can match, proceed
759 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
762 union parameter newparam;
763 char *exten = ast->exten;
767 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
771 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
772 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
776 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
777 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);
781 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
782 /* send setup acknowledge to lcr */
783 memset(&newparam, 0, sizeof(union parameter));
784 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
787 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
792 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
793 /* send setup acknowledge to lcr */
794 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
795 memset(&newparam, 0, sizeof(union parameter));
796 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
800 call->state = CHAN_LCR_STATE_IN_DIALING;
802 /* if match, start pbx */
803 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
804 CDEBUG(call, ast, "Extensions matches.\n");
809 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
815 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
823 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
824 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
826 /* release asterisk */
827 ast->hangupcause = call->cause;
828 /* change to release state */
829 call->state = CHAN_LCR_STATE_RELEASE;
830 ast_hangup(ast); // call will be destroyed here
834 /* send setup to asterisk */
835 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
837 #ifdef LCR_FOR_CALLWEAVER
839 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
842 ret = ast_pbx_start(ast);
844 cause = (ret==-2)?34:27;
847 call->pbx_started = 1;
848 ast_setstate(ast, AST_STATE_RING);
852 * incoming setup from LCR
854 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
856 struct ast_channel *ast;
858 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
860 /* create asterisk channel instrance */
862 #ifdef LCR_FOR_CALLWEAVER
863 ast = ast_channel_alloc(1);
866 #ifdef LCR_FOR_ASTERISK
867 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
872 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
873 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
880 ast->tech_pvt = call;
881 ast->tech = &lcr_tech;
882 ast->fds[0] = call->pipe[0];
884 /* fill setup information */
885 if (param->setup.dialinginfo.id)
886 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
887 if (param->setup.context[0])
888 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
890 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
891 if (param->setup.callerinfo.id[0])
892 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
893 if (param->setup.callerinfo.name[0])
894 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
895 if (param->setup.redirinfo.id[0])
896 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
897 switch (param->setup.callerinfo.present) {
898 case INFO_PRESENT_ALLOWED:
899 ast->cid.cid_pres = AST_PRES_ALLOWED;
901 case INFO_PRESENT_RESTRICTED:
902 ast->cid.cid_pres = AST_PRES_RESTRICTED;
905 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
907 switch (param->setup.callerinfo.ntype) {
908 case INFO_NTYPE_SUBSCRIBER:
909 ast->cid.cid_ton = 4;
911 case INFO_NTYPE_NATIONAL:
912 ast->cid.cid_ton = 2;
914 case INFO_NTYPE_INTERNATIONAL:
915 ast->cid.cid_ton = 1;
918 ast->cid.cid_ton = 0;
920 ast->transfercapability = param->setup.capainfo.bearer_capa;
921 /* enable hdlc if transcap is data */
922 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
924 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
926 /* configure channel */
927 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
928 ast->readformat = ast->rawreadformat = ast->nativeformats;
929 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
931 ast->hangupcause = 0;
934 call->state = CHAN_LCR_STATE_IN_SETUP;
936 if (!call->pbx_started)
937 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
941 * incoming setup acknowledge from LCR
943 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
945 if (!call->ast) return;
947 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
949 /* send pending digits in dialque */
950 if (call->dialque[0])
951 send_dialque_to_lcr(call);
952 /* change to overlap state */
953 call->state = CHAN_LCR_STATE_OUT_DIALING;
957 * incoming proceeding from LCR
959 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
961 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
964 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
965 /* queue event for asterisk */
966 if (call->ast && call->pbx_started) {
970 write(wake_pipe[1], &byte, 1);
972 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
978 * incoming alerting from LCR
980 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
982 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
985 call->state = CHAN_LCR_STATE_OUT_ALERTING;
986 /* queue event to asterisk */
987 if (call->ast && call->pbx_started) {
991 write(wake_pipe[1], &byte, 1);
993 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
998 * incoming connect from LCR
1000 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1002 union parameter newparam;
1004 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1007 call->state = CHAN_LCR_STATE_CONNECT;
1008 /* request bchannel */
1009 if (!call->bchannel) {
1010 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1011 memset(&newparam, 0, sizeof(union parameter));
1012 newparam.bchannel.type = BCHANNEL_REQUEST;
1013 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1015 /* copy connectinfo */
1016 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1017 /* queue event to asterisk */
1018 if (call->ast && call->pbx_started) {
1022 write(wake_pipe[1], &byte, 1);
1024 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1029 * incoming disconnect from LCR
1031 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1033 struct ast_channel *ast = call->ast;
1035 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1038 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1040 call->cause = param->disconnectinfo.cause;
1041 call->location = param->disconnectinfo.location;
1042 /* if bridge, forward disconnect and return */
1045 if (call->bridge_call) {
1046 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1047 bridge_message_if_bridged(call, message_type, param);
1051 /* release lcr with same cause */
1052 send_release_and_import(call, call->cause, call->location);
1054 /* change to release state */
1055 call->state = CHAN_LCR_STATE_RELEASE;
1056 /* queue release asterisk */
1058 ast->hangupcause = call->cause;
1059 if (call->pbx_started) {
1063 write(wake_pipe[1], &byte, 1);
1065 strcpy(call->queue_string, "H"); // overwrite other indications
1067 ast_hangup(ast); // call will be destroyed here
1073 * incoming release from LCR
1075 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1077 struct ast_channel *ast = call->ast;
1079 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1083 /* change to release state */
1084 call->state = CHAN_LCR_STATE_RELEASE;
1085 /* copy release info */
1087 call->cause = param->disconnectinfo.cause;
1088 call->location = param->disconnectinfo.location;
1090 /* if we have an asterisk instance, queue hangup, else we are done */
1092 ast->hangupcause = call->cause;
1093 if (call->pbx_started) {
1097 write(wake_pipe[1], &byte, 1);
1099 strcpy(call->queue_string, "H");
1101 ast_hangup(ast); // call will be destroyed here
1110 * incoming information from LCR
1112 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1114 struct ast_channel *ast = call->ast;
1116 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1120 /* pbx not started */
1121 if (!call->pbx_started) {
1122 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1123 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1124 lcr_start_pbx(call, ast, param->information.sending_complete);
1128 /* change dailing state after setup */
1129 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1130 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1131 call->state = CHAN_LCR_STATE_IN_DIALING;
1132 // ast_setstate(ast, AST_STATE_DIALING);
1136 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1140 write(wake_pipe[1], &byte, 1);
1142 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1145 /* use bridge to forware message not supported by asterisk */
1146 if (call->state == CHAN_LCR_STATE_CONNECT) {
1147 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1148 bridge_message_if_bridged(call, message_type, param);
1153 * incoming information from LCR
1155 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1157 union parameter newparam;
1159 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1161 /* request bchannel, if call is resumed and we don't have it */
1162 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1163 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1164 memset(&newparam, 0, sizeof(union parameter));
1165 newparam.bchannel.type = BCHANNEL_REQUEST;
1166 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1169 if (!call->ast) return;
1171 /* use bridge to forware message not supported by asterisk */
1172 bridge_message_if_bridged(call, message_type, param);
1176 * incoming information from LCR
1178 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1180 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1182 if (!call->ast) return;
1184 /* use bridge to forware message not supported by asterisk */
1185 bridge_message_if_bridged(call, message_type, param);
1189 * incoming pattern from LCR
1191 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1193 union parameter newparam;
1195 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1197 if (!call->ast) return;
1199 /* pattern are indicated only once */
1200 if (call->has_pattern)
1202 call->has_pattern = 1;
1204 /* request bchannel */
1205 if (!call->bchannel) {
1206 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1207 memset(&newparam, 0, sizeof(union parameter));
1208 newparam.bchannel.type = BCHANNEL_REQUEST;
1209 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1211 /* queue PROGRESS, because tones are available */
1212 if (call->ast && call->pbx_started) {
1216 write(wake_pipe[1], &byte, 1);
1218 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1223 * got dtmf from bchannel (locked state)
1225 void lcr_in_dtmf(struct chan_call *call, int val)
1227 struct ast_channel *ast = call->ast;
1232 if (!call->pbx_started)
1235 if (!call->dsp_dtmf) {
1236 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1240 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1246 write(wake_pipe[1], &byte, 1);
1248 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1252 * message received from LCR
1254 int receive_message(int message_type, unsigned int ref, union parameter *param)
1256 struct bchannel *bchannel;
1257 struct chan_call *call;
1258 union parameter newparam;
1260 memset(&newparam, 0, sizeof(union parameter));
1262 /* handle bchannel message*/
1263 if (message_type == MESSAGE_BCHANNEL) {
1264 switch(param->bchannel.type) {
1265 case BCHANNEL_ASSIGN:
1266 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1267 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1268 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1271 /* create bchannel */
1272 bchannel = alloc_bchannel(param->bchannel.handle);
1274 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1278 /* configure channel */
1279 bchannel->b_tx_gain = param->bchannel.tx_gain;
1280 bchannel->b_rx_gain = param->bchannel.rx_gain;
1281 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1282 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1283 bchannel->b_bf_len = param->bchannel.crypt_len;
1284 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1286 bchannel->b_txdata = 0;
1287 bchannel->b_tx_dejitter = 1;
1289 /* in case, ref is not set, this bchannel instance must
1290 * be created until it is removed again by LCR */
1292 call = find_call_ref(ref);
1294 bchannel->call = call;
1295 call->bchannel = bchannel;
1297 bchannel_dtmf(bchannel, 1);
1299 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1300 if (call->pipeline[0])
1301 bchannel_pipeline(bchannel, call->pipeline);
1303 bchannel_gain(bchannel, call->rx_gain, 0);
1305 bchannel_gain(bchannel, call->tx_gain, 1);
1306 if (call->bridge_id) {
1307 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1308 bchannel_join(bchannel, call->bridge_id);
1310 /* create only, if call exists, othewhise it bchannel is freed below... */
1311 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1312 bchannel_activate(bchannel, 1);
1315 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1316 newparam.bchannel.handle = param->bchannel.handle;
1317 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1318 /* if call has released before bchannel is assigned */
1320 newparam.bchannel.type = BCHANNEL_RELEASE;
1321 newparam.bchannel.handle = param->bchannel.handle;
1322 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1327 case BCHANNEL_REMOVE:
1328 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1329 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1330 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1333 /* unklink from call and destroy bchannel */
1334 free_bchannel(bchannel);
1337 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1338 newparam.bchannel.handle = param->bchannel.handle;
1339 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1344 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1349 /* handle new ref */
1350 if (message_type == MESSAGE_NEWREF) {
1351 if (param->direction) {
1352 /* new ref from lcr */
1353 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1354 if (!ref || find_call_ref(ref)) {
1355 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1358 /* allocate new call instance */
1359 call = alloc_call();
1361 call->state = CHAN_LCR_STATE_IN_PREPARE;
1364 call->ref_was_assigned = 1;
1365 /* set dtmf (default, use option 'n' to disable */
1367 /* wait for setup (or release from asterisk) */
1369 /* new ref, as requested from this remote application */
1370 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1371 call = find_call_ref(0);
1373 /* send release, if ref does not exist */
1374 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1375 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1380 call->ref_was_assigned = 1;
1381 /* set dtmf (default, use option 'n' to disable */
1383 /* send pending setup info */
1384 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1385 send_setup_to_lcr(call);
1386 /* release if asterisk has signed off */
1387 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1390 send_release_and_import(call, call->cause, call->location);
1392 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1403 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1406 call = find_call_ref(ref);
1408 /* ignore ref that is not used (anymore) */
1409 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1413 /* handle messages */
1414 switch(message_type) {
1416 lcr_in_setup(call, message_type, param);
1419 case MESSAGE_OVERLAP:
1420 lcr_in_overlap(call, message_type, param);
1423 case MESSAGE_PROCEEDING:
1424 lcr_in_proceeding(call, message_type, param);
1427 case MESSAGE_ALERTING:
1428 lcr_in_alerting(call, message_type, param);
1431 case MESSAGE_CONNECT:
1432 lcr_in_connect(call, message_type, param);
1435 case MESSAGE_DISCONNECT:
1436 lcr_in_disconnect(call, message_type, param);
1439 case MESSAGE_RELEASE:
1440 lcr_in_release(call, message_type, param);
1443 case MESSAGE_INFORMATION:
1444 lcr_in_information(call, message_type, param);
1447 case MESSAGE_NOTIFY:
1448 lcr_in_notify(call, message_type, param);
1451 case MESSAGE_FACILITY:
1452 lcr_in_facility(call, message_type, param);
1455 case MESSAGE_PATTERN: // audio available from LCR
1456 if (!call->has_pattern)
1457 lcr_in_pattern(call, message_type, param);
1460 case MESSAGE_NOPATTERN: // audio not available from LCR
1463 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1464 call->audiopath = param->audiopath;
1468 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1475 * release all calls (due to broken socket)
1477 static void release_all_calls(void)
1479 struct chan_call *call;
1484 /* no ast, so we may directly free call */
1486 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1490 /* already in release process */
1491 if (call->state == CHAN_LCR_STATE_RELEASE) {
1495 /* release or queue release */
1497 call->state = CHAN_LCR_STATE_RELEASE;
1498 if (!call->pbx_started) {
1499 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1500 ast_hangup(call->ast); // call will be destroyed here
1503 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1507 write(wake_pipe[1], &byte, 1);
1509 strcpy(call->queue_string, "H");
1513 /* release all bchannels */
1514 while(bchannel_first)
1515 free_bchannel(bchannel_first);
1518 void close_socket(void);
1521 * warning! not thread safe
1522 * returns -1 for socket error, 0 for no work, 1 for work
1524 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1527 struct admin_list *admin;
1528 struct admin_message msg;
1530 if ((what & LCR_FD_READ)) {
1531 /* read from socket */
1532 len = read(lcr_sock, &msg, sizeof(msg));
1534 CERROR(NULL, NULL, "Socket closed.\n");
1536 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1538 release_all_calls();
1539 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1543 if (len != sizeof(msg)) {
1544 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1547 if (msg.message != ADMIN_MESSAGE) {
1548 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1551 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1553 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1558 if ((what & LCR_FD_WRITE)) {
1559 /* write to socket */
1561 socket_fd.when &= ~LCR_FD_WRITE;
1564 admin = admin_first;
1565 len = write(lcr_sock, &admin->msg, sizeof(msg));
1567 CERROR(NULL, NULL, "Socket closed.\n");
1571 if (len != sizeof(msg)) {
1572 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1576 admin_first = admin->next;
1580 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1589 * open and close socket and thread
1591 int open_socket(void)
1594 struct sockaddr_un sock_address;
1595 union parameter param;
1598 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1599 CERROR(NULL, NULL, "Failed to create socket.\n");
1603 /* set socket address and name */
1604 memset(&sock_address, 0, sizeof(sock_address));
1605 sock_address.sun_family = PF_UNIX;
1606 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1608 /* connect socket */
1609 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1612 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1616 /* register socket fd */
1617 memset(&socket_fd, 0, sizeof(socket_fd));
1618 socket_fd.fd = lcr_sock;
1619 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1621 /* enque hello message */
1622 memset(¶m, 0, sizeof(param));
1623 strcpy(param.hello.application, "asterisk");
1624 send_message(MESSAGE_HELLO, 0, ¶m);
1629 void close_socket(void)
1631 struct admin_list *admin, *temp;
1633 unregister_fd(&socket_fd);
1635 /* flush pending messages */
1636 admin = admin_first;
1639 admin = admin->next;
1652 /* sending queue to asterisk */
1653 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1657 read(wake_pipe[0], &byte, 1);
1664 static void handle_queue()
1666 struct chan_call *call;
1667 struct ast_channel *ast;
1668 struct ast_frame fr;
1673 p = call->queue_string;
1677 ast_channel_lock(ast);
1681 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1682 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1685 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1686 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1689 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1690 ast_queue_control(ast, AST_CONTROL_RINGING);
1691 ast_setstate(ast, AST_STATE_RINGING);
1694 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1695 ast_queue_control(ast, AST_CONTROL_ANSWER);
1698 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1699 ast_queue_hangup(ast);
1701 case '1': case '2': case '3': case 'A':
1702 case '4': case '5': case '6': case 'B':
1703 case '7': case '8': case '9': case 'C':
1704 case '*': case '0': case '#': case 'D':
1705 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1706 /* send digit to asterisk */
1707 memset(&fr, 0, sizeof(fr));
1709 #ifdef LCR_FOR_ASTERISK
1710 fr.frametype = AST_FRAME_DTMF_BEGIN;
1713 #ifdef LCR_FOR_CALLWEAVER
1714 fr.frametype = AST_FRAME_DTMF;
1718 fr.delivery = ast_tv(0, 0);
1719 ast_queue_frame(ast, &fr);
1721 #ifdef LCR_FOR_ASTERISK
1722 fr.frametype = AST_FRAME_DTMF_END;
1723 ast_queue_frame(ast, &fr);
1728 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1732 call->queue_string[0] = '\0';
1733 ast_channel_unlock(ast);
1740 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1742 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1743 if (open_socket() < 0)
1744 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1749 void lock_chan(void)
1752 ast_mutex_lock(&chan_lock);
1755 void unlock_chan(void)
1757 ast_mutex_unlock(&chan_lock);
1761 /* chan_lcr thread */
1762 static void *chan_thread(void *arg)
1764 if (pipe(wake_pipe) < 0) {
1765 CERROR(NULL, NULL, "Failed to open pipe.\n");
1768 memset(&wake_fd, 0, sizeof(wake_fd));
1769 wake_fd.fd = wake_pipe[0];
1770 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1772 memset(&socket_retry, 0, sizeof(socket_retry));
1773 add_timer(&socket_retry, handle_retry, NULL, 0);
1775 bchannel_pid = getpid();
1777 /* open socket the first time */
1778 handle_retry(NULL, NULL, 0);
1781 ast_mutex_lock(&chan_lock);
1785 select_main(0, &global_change, lock_chan, unlock_chan);
1790 del_timer(&socket_retry);
1792 unregister_fd(&wake_fd);
1793 close(wake_pipe[0]);
1794 close(wake_pipe[1]);
1796 CERROR(NULL, NULL, "Thread exit.\n");
1798 ast_mutex_unlock(&chan_lock);
1805 * new asterisk instance
1808 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1810 char exten[256], *dial, *interface, *opt;
1811 struct ast_channel *ast;
1812 struct chan_call *call;
1815 ast_mutex_lock(&chan_lock);
1816 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1818 /* if socket is closed */
1820 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1821 ast_mutex_unlock(&chan_lock);
1826 /* create call instance */
1827 call = alloc_call();
1829 /* failed to create instance */
1830 ast_mutex_unlock(&chan_lock);
1835 /* create asterisk channel instrance */
1837 #ifdef LCR_FOR_ASTERISK
1838 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1841 #ifdef LCR_FOR_CALLWEAVER
1842 ast = ast_channel_alloc(1);
1846 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1848 /* failed to create instance */
1849 ast_mutex_unlock(&chan_lock);
1853 ast->tech = &lcr_tech;
1854 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1855 /* configure channel */
1856 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1857 ast->readformat = ast->rawreadformat = ast->nativeformats;
1858 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1860 ast->hangupcause = 0;
1864 ast->tech_pvt = call;
1865 ast->fds[0] = call->pipe[0];
1866 call->pbx_started = 0;
1868 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1871 * Extract interface, dialstring, options from data.
1874 * <interface>/<dialstring>
1875 * <interface>/<dialstring>/options
1877 strncpy(exten, (char *)data, sizeof(exten)-1);
1878 exten[sizeof(exten)-1] = '\0';
1879 if ((dial = strchr(exten, '/'))) {
1882 if ((opt = strchr(dial, '/')))
1891 strncpy(call->interface, interface, sizeof(call->interface)-1);
1892 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1893 apply_opt(call, (char *)opt);
1895 ast_mutex_unlock(&chan_lock);
1901 * call from asterisk
1903 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1905 union parameter newparam;
1906 struct chan_call *call;
1909 ast_mutex_lock(&chan_lock);
1910 call = ast->tech_pvt;
1912 #ifdef LCR_FOR_CALLWEAVER
1914 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1918 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1919 ast_mutex_unlock(&chan_lock);
1924 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1926 /* pbx process is started */
1927 call->pbx_started = 1;
1928 /* send MESSAGE_NEWREF */
1929 memset(&newparam, 0, sizeof(union parameter));
1930 newparam.direction = 0; /* request from app */
1931 send_message(MESSAGE_NEWREF, 0, &newparam);
1933 /* set hdlc if capability requires hdlc */
1934 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1935 || ast->transfercapability == INFO_BC_DATARESTRICTED
1936 || ast->transfercapability == INFO_BC_VIDEO)
1938 /* if hdlc is forced by option, we change transcap to data */
1940 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1941 && ast->transfercapability != INFO_BC_DATARESTRICTED
1942 && ast->transfercapability != INFO_BC_VIDEO)
1943 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1945 call->cid_num[0] = 0;
1946 call->cid_name[0] = 0;
1947 call->cid_rdnis[0] = 0;
1949 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1950 strncpy(call->cid_num, ast->cid.cid_num,
1951 sizeof(call->cid_num)-1);
1953 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1954 strncpy(call->cid_name, ast->cid.cid_name,
1955 sizeof(call->cid_name)-1);
1956 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1957 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1958 sizeof(call->cid_rdnis)-1);
1960 ast_mutex_unlock(&chan_lock);
1965 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1967 static const char* dtmf_tones[] = {
1968 "!941+1336/100,!0/100", /* 0 */
1969 "!697+1209/100,!0/100", /* 1 */
1970 "!697+1336/100,!0/100", /* 2 */
1971 "!697+1477/100,!0/100", /* 3 */
1972 "!770+1209/100,!0/100", /* 4 */
1973 "!770+1336/100,!0/100", /* 5 */
1974 "!770+1477/100,!0/100", /* 6 */
1975 "!852+1209/100,!0/100", /* 7 */
1976 "!852+1336/100,!0/100", /* 8 */
1977 "!852+1477/100,!0/100", /* 9 */
1978 "!697+1633/100,!0/100", /* A */
1979 "!770+1633/100,!0/100", /* B */
1980 "!852+1633/100,!0/100", /* C */
1981 "!941+1633/100,!0/100", /* D */
1982 "!941+1209/100,!0/100", /* * */
1983 "!941+1477/100,!0/100" }; /* # */
1985 if (digit >= '0' && digit <='9')
1986 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1987 else if (digit >= 'A' && digit <= 'D')
1988 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1989 else if (digit == '*')
1990 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1991 else if (digit == '#')
1992 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1995 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1996 "'%c' for '%s'\n", digit, ast->name);
2000 #ifdef LCR_FOR_ASTERISK
2001 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2003 #ifdef LCR_FOR_CALLWEAVER
2004 static int lcr_digit(struct ast_channel *ast, char digit)
2007 struct chan_call *call;
2008 union parameter newparam;
2011 #ifdef LCR_FOR_CALLWEAVER
2012 int inband_dtmf = 0;
2015 /* only pass IA5 number space */
2016 if (digit > 126 || digit < 32)
2020 ast_mutex_lock(&chan_lock);
2021 call = ast->tech_pvt;
2023 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2024 ast_mutex_unlock(&chan_lock);
2029 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2031 /* send information or queue them */
2032 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2033 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2034 memset(&newparam, 0, sizeof(union parameter));
2036 newparam.information.keypad[0] = digit;
2037 newparam.information.keypad[1] = '\0';
2039 newparam.information.id[0] = digit;
2040 newparam.information.id[1] = '\0';
2042 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2045 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2046 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2048 strncat(call->dialque, buf, strlen(call->dialque)-1);
2051 ast_mutex_unlock(&chan_lock);
2054 #ifdef LCR_FOR_ASTERISK
2058 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2060 int inband_dtmf = 0;
2061 struct chan_call *call;
2065 ast_mutex_lock(&chan_lock);
2067 call = ast->tech_pvt;
2071 "Received digit from Asterisk, "
2072 "but no call instance exists.\n");
2073 ast_mutex_unlock(&chan_lock);
2078 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2080 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2084 ast_mutex_unlock(&chan_lock);
2088 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2089 send_digit_to_chan(ast, digit);
2095 static int lcr_answer(struct ast_channel *ast)
2097 union parameter newparam;
2098 struct chan_call *call;
2101 ast_mutex_lock(&chan_lock);
2102 call = ast->tech_pvt;
2104 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2105 ast_mutex_unlock(&chan_lock);
2110 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2112 /* copy connectinfo, if bridged */
2113 if (call->bridge_call)
2114 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2115 /* send connect message to lcr */
2116 if (call->state != CHAN_LCR_STATE_CONNECT) {
2117 memset(&newparam, 0, sizeof(union parameter));
2118 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2119 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2120 call->state = CHAN_LCR_STATE_CONNECT;
2123 /* request bchannel */
2124 if (!call->bchannel) {
2125 CDEBUG(call, ast, "Requesting B-channel.\n");
2126 memset(&newparam, 0, sizeof(union parameter));
2127 newparam.bchannel.type = BCHANNEL_REQUEST;
2128 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2131 // memset(&newparam, 0, sizeof(union parameter));
2132 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2134 ast_mutex_unlock(&chan_lock);
2139 static int lcr_hangup(struct ast_channel *ast)
2141 struct chan_call *call;
2142 pthread_t tid = pthread_self();
2144 if (!pthread_equal(tid, chan_tid)) {
2146 ast_mutex_lock(&chan_lock);
2148 call = ast->tech_pvt;
2150 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2151 if (!pthread_equal(tid, chan_tid)) {
2152 ast_mutex_unlock(&chan_lock);
2158 if (!pthread_equal(tid, chan_tid))
2159 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2161 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2163 /* disconnect asterisk, maybe not required */
2164 ast->tech_pvt = NULL;
2168 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2169 if (ast->hangupcause > 0)
2170 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2172 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2175 if (!pthread_equal(tid, chan_tid)) {
2176 ast_mutex_unlock(&chan_lock);
2181 /* ref is not set, due to prepare setup or release */
2182 if (call->state == CHAN_LCR_STATE_RELEASE) {
2183 /* we get the response to our release */
2184 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2187 /* during prepare, we change to release state */
2188 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2189 call->state = CHAN_LCR_STATE_RELEASE;
2193 if (!pthread_equal(tid, chan_tid)) {
2194 ast_mutex_unlock(&chan_lock);
2200 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2202 struct chan_call *call;
2205 CDEBUG(NULL, ast, "No subclass\n");
2206 if (!(f->subclass & ast->nativeformats))
2207 CDEBUG(NULL, ast, "Unexpected format.\n");
2210 ast_mutex_lock(&chan_lock);
2211 call = ast->tech_pvt;
2213 ast_mutex_unlock(&chan_lock);
2217 if (call->bchannel && f->samples)
2218 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2219 ast_mutex_unlock(&chan_lock);
2225 static struct ast_frame *lcr_read(struct ast_channel *ast)
2227 struct chan_call *call;
2231 ast_mutex_lock(&chan_lock);
2232 call = ast->tech_pvt;
2234 ast_mutex_unlock(&chan_lock);
2238 if (call->pipe[0] > -1) {
2239 if (call->rebuffer && !call->hdlc) {
2240 /* Make sure we have a complete 20ms (160byte) frame */
2241 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2243 call->framepos += len;
2246 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2248 if (len < 0 && errno == EAGAIN) {
2249 ast_mutex_unlock(&chan_lock);
2252 #ifdef LCR_FOR_ASTERISK
2253 return &ast_null_frame;
2256 #ifdef LCR_FOR_CALLWEAVER
2262 close(call->pipe[0]);
2265 ast_mutex_unlock(&chan_lock);
2268 } else if (call->rebuffer && call->framepos < 160) {
2269 /* Not a complete frame, so we send a null-frame */
2270 ast_mutex_unlock(&chan_lock);
2272 return &ast_null_frame;
2276 call->read_fr.frametype = AST_FRAME_VOICE;
2277 call->read_fr.subclass = ast->nativeformats;
2278 if (call->rebuffer) {
2279 call->read_fr.datalen = call->framepos;
2280 call->read_fr.samples = call->framepos;
2283 call->read_fr.datalen = len;
2284 call->read_fr.samples = len;
2286 call->read_fr.delivery = ast_tv(0,0);
2287 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2288 ast_mutex_unlock(&chan_lock);
2291 return &call->read_fr;
2294 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2296 union parameter newparam;
2298 struct chan_call *call;
2299 const struct tone_zone_sound *ts = NULL;
2302 ast_mutex_lock(&chan_lock);
2303 call = ast->tech_pvt;
2305 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2306 ast_mutex_unlock(&chan_lock);
2312 case AST_CONTROL_BUSY:
2313 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2314 ast_setstate(ast, AST_STATE_BUSY);
2315 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2316 /* send message to lcr */
2317 memset(&newparam, 0, sizeof(union parameter));
2318 newparam.disconnectinfo.cause = 17;
2319 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2320 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2322 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2324 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2325 ts = ast_get_indication_tone(ast->zone, "busy");
2328 case AST_CONTROL_CONGESTION:
2329 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2330 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2331 /* send message to lcr */
2332 memset(&newparam, 0, sizeof(union parameter));
2333 newparam.disconnectinfo.cause = ast->hangupcause;
2334 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2335 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2337 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2339 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2340 ts = ast_get_indication_tone(ast->zone, "congestion");
2343 case AST_CONTROL_PROCEEDING:
2344 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2345 if (call->state == CHAN_LCR_STATE_IN_SETUP
2346 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2347 /* send message to lcr */
2348 memset(&newparam, 0, sizeof(union parameter));
2349 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2351 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2354 case AST_CONTROL_RINGING:
2355 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2356 ast_setstate(ast, AST_STATE_RING);
2357 if (call->state == CHAN_LCR_STATE_IN_SETUP
2358 || call->state == CHAN_LCR_STATE_IN_DIALING
2359 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2360 /* send message to lcr */
2361 memset(&newparam, 0, sizeof(union parameter));
2362 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2364 call->state = CHAN_LCR_STATE_IN_ALERTING;
2366 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2367 ts = ast_get_indication_tone(ast->zone, "ring");
2370 case AST_CONTROL_PROGRESS:
2371 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2372 /* request bchannel */
2373 if (!call->bchannel) {
2374 CDEBUG(call, ast, "Requesting B-channel.\n");
2375 memset(&newparam, 0, sizeof(union parameter));
2376 newparam.bchannel.type = BCHANNEL_REQUEST;
2377 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2381 CDEBUG(call, ast, "Received indicate -1.\n");
2382 ast_playtones_stop(ast);
2386 case AST_CONTROL_VIDUPDATE:
2387 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2390 case AST_CONTROL_HOLD:
2391 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2392 /* send message to lcr */
2393 memset(&newparam, 0, sizeof(union parameter));
2394 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2395 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2397 /*start music onhold*/
2398 #ifdef LCR_FOR_ASTERISK
2399 ast_moh_start(ast,data,ast->musicclass);
2402 #ifdef LCR_FOR_CALLWEAVER
2403 ast_moh_start(ast, NULL);
2408 case AST_CONTROL_UNHOLD:
2409 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2410 /* send message to lcr */
2411 memset(&newparam, 0, sizeof(union parameter));
2412 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2413 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2419 #ifdef AST_CONTROL_SRCUPDATE
2420 case AST_CONTROL_SRCUPDATE:
2424 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2427 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2432 if (ts && ts->data[0]) {
2433 ast_playtones_start(ast, 0, ts->data, 1);
2437 ast_mutex_unlock(&chan_lock);
2445 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2447 struct chan_call *call;
2454 ast_mutex_lock(&chan_lock);
2455 call = ast->tech_pvt;
2457 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2458 ast_mutex_unlock(&chan_lock);
2463 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2465 ast_mutex_unlock(&chan_lock);
2471 * send_text asterisk
2473 static int lcr_send_text(struct ast_channel *ast, const char *text)
2475 struct chan_call *call;
2476 union parameter newparam;
2479 ast_mutex_lock(&chan_lock);
2480 call = ast->tech_pvt;
2482 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2483 ast_mutex_unlock(&chan_lock);
2488 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2489 memset(&newparam, 0, sizeof(union parameter));
2490 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2491 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2492 ast_mutex_unlock(&chan_lock);
2500 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2501 struct ast_channel *ast2, int flags,
2502 struct ast_frame **fo,
2503 struct ast_channel **rc, int timeoutms)
2506 struct chan_call *call1, *call2;
2507 struct ast_channel *carr[2], *who;
2509 struct ast_frame *f;
2512 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2517 /* join via dsp (if the channels are currently open) */
2519 ast_mutex_lock(&chan_lock);
2520 call1 = ast1->tech_pvt;
2521 call2 = ast2->tech_pvt;
2522 if (!call1 || !call2) {
2523 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2524 ast_mutex_unlock(&chan_lock);
2526 return AST_BRIDGE_COMPLETE;
2529 /* join, if both call instances uses dsp
2530 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2532 if (!call1->nodsp && !call2->nodsp) {
2533 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2535 /* get bridge id and join */
2536 bridge_id = new_bridge_id();
2538 call1->bridge_id = bridge_id;
2539 if (call1->bchannel)
2540 bchannel_join(call1->bchannel, bridge_id);
2542 call2->bridge_id = bridge_id;
2543 if (call2->bchannel)
2544 bchannel_join(call2->bchannel, bridge_id);
2546 if (call1->nodsp && call2->nodsp)
2547 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2549 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2550 call1->bridge_call = call2;
2551 call2->bridge_call = call1;
2553 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2554 || call1->state == CHAN_LCR_STATE_IN_DIALING
2555 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2556 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2557 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2560 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2561 || call2->state == CHAN_LCR_STATE_IN_DIALING
2562 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2563 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2564 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2568 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2569 so let's do it for them. Hmpf.
2572 if (call1->on_hold) {
2573 union parameter newparam;
2575 memset(&newparam, 0, sizeof(union parameter));
2576 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2577 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2582 if (call2->on_hold) {
2583 union parameter newparam;
2585 memset(&newparam, 0, sizeof(union parameter));
2586 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2587 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2592 ast_mutex_unlock(&chan_lock);
2597 who = ast_waitfor_n(carr, 2, &to);
2600 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2605 if (!f || f->frametype == AST_FRAME_CONTROL) {
2607 CDEBUG(NULL, NULL, "Got hangup.\n");
2609 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2616 if ( f->frametype == AST_FRAME_DTMF ) {
2617 CDEBUG(NULL, NULL, "Got DTMF.\n");
2633 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2635 /* split channels */
2637 ast_mutex_lock(&chan_lock);
2638 call1 = ast1->tech_pvt;
2639 call2 = ast2->tech_pvt;
2640 if (call1 && call1->bridge_id) {
2641 call1->bridge_id = 0;
2642 if (call1->bchannel)
2643 bchannel_join(call1->bchannel, 0);
2644 if (call1->bridge_call)
2645 call1->bridge_call->bridge_call = NULL;
2647 if (call2 && call1->bridge_id) {
2648 call2->bridge_id = 0;
2649 if (call2->bchannel)
2650 bchannel_join(call2->bchannel, 0);
2651 if (call2->bridge_call)
2652 call2->bridge_call->bridge_call = NULL;
2654 call1->bridge_call = NULL;
2655 call2->bridge_call = NULL;
2657 ast_mutex_unlock(&chan_lock);
2659 return AST_BRIDGE_COMPLETE;
2661 static struct ast_channel_tech lcr_tech = {
2663 .description = "Channel driver for connecting to Linux-Call-Router",
2664 .capabilities = AST_FORMAT_ALAW,
2665 .requester = lcr_request,
2667 #ifdef LCR_FOR_ASTERISK
2668 .send_digit_begin = lcr_digit_begin,
2669 .send_digit_end = lcr_digit_end,
2672 #ifdef LCR_FOR_CALLWEAVER
2673 .send_digit = lcr_digit,
2677 .bridge = lcr_bridge,
2678 .hangup = lcr_hangup,
2679 .answer = lcr_answer,
2682 .indicate = lcr_indicate,
2684 .send_text = lcr_send_text,
2693 static int lcr_show_lcr (int fd, int argc, char *argv[])
2698 static int lcr_show_calls (int fd, int argc, char *argv[])
2703 static int lcr_reload_routing (int fd, int argc, char *argv[])
2708 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2713 static int lcr_port_block (int fd, int argc, char *argv[])
2718 static int lcr_port_unblock (int fd, int argc, char *argv[])
2723 static int lcr_port_unload (int fd, int argc, char *argv[])
2728 static struct ast_cli_entry cli_show_lcr =
2729 { {"lcr", "show", "lcr", NULL},
2731 "Shows current states of LCR core",
2732 "Usage: lcr show lcr\n",
2735 static struct ast_cli_entry cli_show_calls =
2736 { {"lcr", "show", "calls", NULL},
2738 "Shows current calls made by LCR and Asterisk",
2739 "Usage: lcr show calls\n",
2742 static struct ast_cli_entry cli_reload_routing =
2743 { {"lcr", "reload", "routing", NULL},
2745 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2746 "Usage: lcr reload routing\n",
2749 static struct ast_cli_entry cli_reload_interfaces =
2750 { {"lcr", "reload", "interfaces", NULL},
2751 lcr_reload_interfaces,
2752 "Reloads interfaces conf of LCR",
2753 "Usage: lcr reload interfaces\n",
2756 static struct ast_cli_entry cli_port_block =
2757 { {"lcr", "port", "block", NULL},
2759 "Blocks LCR port for further calls",
2760 "Usage: lcr port block \"<port>\"\n",
2763 static struct ast_cli_entry cli_port_unblock =
2764 { {"lcr", "port", "unblock", NULL},
2766 "Unblocks or loads LCR port, port is opened my mISDN",
2767 "Usage: lcr port unblock \"<port>\"\n",
2770 static struct ast_cli_entry cli_port_unload =
2771 { {"lcr", "port", "unload", NULL},
2773 "Unloads LCR port, port is closes by mISDN",
2774 "Usage: lcr port unload \"<port>\"\n",
2779 #ifdef LCR_FOR_ASTERISK
2780 static int lcr_config_exec(struct ast_channel *ast, void *data)
2783 #ifdef LCR_FOR_CALLWEAVER
2784 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2787 struct chan_call *call;
2790 ast_mutex_lock(&chan_lock);
2792 #ifdef LCR_FOR_ASTERISK
2793 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2796 #ifdef LCR_FOR_CALLWEAVER
2797 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2803 if (call->ast == ast)
2809 #ifdef LCR_FOR_ASTERISK
2810 apply_opt(call, (char *)data);
2813 #ifdef LCR_FOR_CALLWEAVER
2814 apply_opt(call, (char *)argv[0]);
2818 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2820 ast_mutex_unlock(&chan_lock);
2826 * module loading and destruction
2828 int load_module(void)
2831 char options_error[256];
2833 for (i = 0; i < 256; i++) {
2834 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2835 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2838 if (read_options(options_error) == 0) {
2839 CERROR(NULL, NULL, "%s", options_error);
2841 #ifdef LCR_FOR_ASTERISK
2842 return AST_MODULE_LOAD_DECLINE;
2845 #ifdef LCR_FOR_CALLWEAVER
2851 ast_mutex_init(&chan_lock);
2852 ast_mutex_init(&log_lock);
2854 if (bchannel_initialize()) {
2855 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2858 #ifdef LCR_FOR_ASTERISK
2859 return AST_MODULE_LOAD_DECLINE;
2862 #ifdef LCR_FOR_CALLWEAVER
2868 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2869 if (ast_channel_register(&lcr_tech)) {
2870 CERROR(NULL, NULL, "Unable to register channel class\n");
2871 bchannel_deinitialize();
2874 #ifdef LCR_FOR_ASTERISK
2875 return AST_MODULE_LOAD_DECLINE;
2878 #ifdef LCR_FOR_CALLWEAVER
2883 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2885 #ifdef LCR_FOR_ASTERISK
2886 "lcr_config(<opt><optarg>:<opt>:...)\n"
2889 #ifdef LCR_FOR_CALLWEAVER
2890 "lcr_config(<opt><optarg>:<opt>:...)\n",
2893 "Sets LCR opts. and optargs\n"
2895 "The available options are:\n"
2896 " d - Send display text on called phone, text is the optarg.\n"
2897 " n - Don't detect dtmf tones on called channel.\n"
2898 " h - Force data call (HDLC).\n"
2899 " t - Disable mISDN_dsp features (required for fax application).\n"
2900 " q - Add queue to make fax stream seamless (required for fax app).\n"
2901 " Use queue size in miliseconds for optarg. (try 250)\n"
2902 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2903 " Use time to detect for optarg.\n"
2904 " c - Make crypted outgoing call, optarg is keyindex.\n"
2905 " e - Perform echo cancelation on this channel.\n"
2906 " Takes mISDN pipeline option as optarg.\n"
2907 " s - Send Non Inband DTMF as inband.\n"
2908 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2909 " vr - rxgain control\n"
2910 " vt - txgain control\n"
2911 " Volume changes at factor 2 ^ optarg.\n"
2912 " k - use keypad to dial this call.\n"
2914 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
2915 " -> use 16 for fax (3.1k audio)\n"
2917 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
2918 "options: \"n:t:q250\" for seamless audio transmission.\n"
2923 ast_cli_register(&cli_show_lcr);
2924 ast_cli_register(&cli_show_calls);
2925 ast_cli_register(&cli_reload_routing);
2926 ast_cli_register(&cli_reload_interfaces);
2927 ast_cli_register(&cli_port_block);
2928 ast_cli_register(&cli_port_unblock);
2929 ast_cli_register(&cli_port_unload);
2933 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
2934 /* failed to create thread */
2935 bchannel_deinitialize();
2937 ast_channel_unregister(&lcr_tech);
2939 #ifdef LCR_FOR_ASTERISK
2940 return AST_MODULE_LOAD_DECLINE;
2943 #ifdef LCR_FOR_CALLWEAVER
2951 int unload_module(void)
2953 /* First, take us out of the channel loop */
2954 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2957 pthread_join(chan_tid, NULL);
2959 ast_channel_unregister(&lcr_tech);
2961 ast_unregister_application("lcr_config");
2964 if (mISDN_created) {
2965 bchannel_deinitialize();
2969 if (lcr_sock >= 0) {
2977 int reload_module(void)
2983 #ifdef LCR_FOR_ASTERISK
2984 #define AST_MODULE "chan_lcr"
2987 #ifdef LCR_FOR_CALLWEAVER
2993 ast_mutex_lock(&usecnt_lock);
2995 ast_mutex_unlock(&usecnt_lock);
3001 #ifdef LCR_FOR_ASTERISK
3002 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3003 .load = load_module,
3004 .unload = unload_module,
3005 .reload = reload_module,
3009 #ifdef LCR_FOR_CALLWEAVER
3010 char *description(void)