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) ", text, (int)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);
248 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
249 buffer[sizeof(buffer)-1]=0;
253 sprintf(call_text, "%d", call->ref);
255 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
256 ast_text[sizeof(ast_text)-1] = '\0';
258 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
260 ast_mutex_unlock(&log_lock);
265 * channel and call instances
267 struct chan_call *call_first;
271 * special case: 0: find new ref, that has not been assigned a ref yet
274 struct chan_call *find_call_ref(unsigned int ref)
276 struct chan_call *call = call_first;
277 int assigned = (ref > 0);
280 if (call->ref == ref && call->ref_was_assigned == assigned)
287 void free_call(struct chan_call *call)
289 struct chan_call **temp = &call_first;
293 *temp = (*temp)->next;
294 if (call->pipe[0] > -1)
295 close(call->pipe[0]);
296 if (call->pipe[1] > -1)
297 close(call->pipe[1]);
298 if (call->bchannel) {
299 if (call->bchannel->call != call)
300 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
301 call->bchannel->call = NULL;
303 if (call->bridge_call) {
304 if (call->bridge_call->bridge_call != call)
305 CERROR(call, NULL, "Linked call structure has no link to us.\n");
306 call->bridge_call->bridge_call = NULL;
309 ast_translator_free_path(call->trans);
311 ast_dsp_free(call->dsp);
312 CDEBUG(call, NULL, "Call instance freed.\n");
317 temp = &((*temp)->next);
319 CERROR(call, NULL, "Call instance not found in list.\n");
322 struct chan_call *alloc_call(void)
324 struct chan_call **callp = &call_first;
327 callp = &((*callp)->next);
329 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
331 memset(*callp, 0, sizeof(struct chan_call));
332 if (pipe((*callp)->pipe) < 0) {
333 CERROR(*callp, NULL, "Failed to create pipe.\n");
337 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
338 CDEBUG(*callp, NULL, "Call instance allocated.\n");
342 unsigned short new_bridge_id(void)
344 struct chan_call *call;
345 unsigned short id = 1;
347 /* search for lowest bridge id that is not in use and not 0 */
351 if (call->bridge_id == id)
359 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
364 * enque message to LCR
366 int send_message(int message_type, unsigned int ref, union parameter *param)
368 struct admin_list *admin, **adminp;
371 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
374 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
376 adminp = &admin_first;
378 adminp = &((*adminp)->next);
379 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
381 CERROR(NULL, NULL, "No memory for message to LCR.\n");
386 admin->msg.message = ADMIN_MESSAGE;
387 admin->msg.u.msg.type = message_type;
388 admin->msg.u.msg.ref = ref;
389 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
390 socket_fd.when |= LCR_FD_WRITE;
394 write(wake_pipe[1], &byte, 1);
401 * apply options (in locked state)
403 void apply_opt(struct chan_call *call, char *data)
405 union parameter newparam;
406 char string[1024], *p = string, *opt, *key;
412 strncpy(string, data, sizeof(string)-1);
413 string[sizeof(string)-1] = '\0';
416 while((opt = strsep(&p, ":"))) {
419 if (opt[1] == '\0') {
420 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
423 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
424 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
425 strncpy(call->display, opt+1, sizeof(call->display)-1);
427 memset(&newparam, 0, sizeof(union parameter));
428 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
429 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
433 if (opt[1] != '\0') {
434 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
437 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
438 if (call->dsp_dtmf) {
441 bchannel_dtmf(call->bchannel, 0);
445 if (opt[1] == '\0') {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
450 /* check for 0xXXXX... type of key */
451 if (!!strncmp((char *)key, "0x", 2)) {
452 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
456 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
457 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
462 if (*key>='0' && *key<='9')
463 call->bf_key[i] = (*key-'0') << 8;
464 else if (*key>='a' && *key<='f')
465 call->bf_key[i] = (*key-'a'+10) << 8;
466 else if (*key>='A' && *key<='F')
467 call->bf_key[i] = (*key-'A'+10) << 8;
471 if (*key>='0' && *key<='9')
472 call->bf_key[i] += (*key - '0');
473 else if (*key>='a' && *key<='f')
474 call->bf_key[i] += (*key - 'a' + 10);
475 else if (*key>='A' && *key<='F')
476 call->bf_key[i] += (*key - 'A' + 10);
483 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
487 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
489 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
492 if (opt[1] != '\0') {
493 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
496 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
501 if (opt[1] != '\0') {
502 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
505 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
510 if (opt[1] == '\0') {
511 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
514 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
515 call->nodsp_queue = atoi(opt+1);
518 if (opt[1] == '\0') {
519 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
522 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
523 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
525 bchannel_pipeline(call->bchannel, call->pipeline);
528 if (opt[1] == '\0') {
529 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
532 call->faxdetect=atoi(opt+1);
534 call->dsp=ast_dsp_new();
536 #ifdef LCR_FOR_CALLWEAVER
537 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
539 #ifdef LCR_FOR_ASTERISK
540 #ifdef DSP_FEATURE_DTMF_DETECT
541 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
543 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
548 #ifdef LCR_FOR_CALLWEAVER
549 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
551 #ifdef LCR_FOR_ASTERISK
552 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
555 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
558 if (opt[1] != '\0') {
559 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
562 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
567 if (opt[1] != '\0') {
568 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
571 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
572 call->inband_dtmf = 1;
575 if (opt[1] != 'r' && opt[1] != 't') {
576 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
580 if (gain < -8 || gain >8) {
581 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
584 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
586 call->rx_gain = gain;
588 bchannel_gain(call->bchannel, call->rx_gain, 0);
590 call->tx_gain = gain;
592 bchannel_gain(call->bchannel, call->tx_gain, 1);
596 if (opt[1] != '\0') {
597 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
600 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
605 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
609 /* re-open, if bchannel is created */
610 if (call->bchannel && call->bchannel->b_sock > -1) {
611 bchannel_destroy(call->bchannel);
612 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
613 bchannel_activate(call->bchannel, 1);
618 * send setup info to LCR
619 * this function is called, when asterisk call is received and ref is received
621 static void send_setup_to_lcr(struct chan_call *call)
623 union parameter newparam;
624 struct ast_channel *ast = call->ast;
627 if (!call->ast || !call->ref)
630 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
632 /* send setup message to LCR */
633 memset(&newparam, 0, sizeof(union parameter));
634 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
635 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
637 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
639 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
640 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
641 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
642 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
643 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
644 call->display[0] = '\0';
645 if (call->cid_num[0])
646 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
647 if (call->cid_name[0])
648 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
649 if (call->cid_rdnis[0]) {
650 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
651 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
652 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
654 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
655 case AST_PRES_RESTRICTED:
656 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
658 case AST_PRES_UNAVAILABLE:
659 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
661 case AST_PRES_ALLOWED:
663 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
665 switch(ast->cid.cid_ton) {
667 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
670 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
673 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
676 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
678 tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
680 ast->transfercapability = atoi(tmp);
681 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
682 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
684 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
686 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
687 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
689 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
690 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
691 send_message(MESSAGE_SETUP, call->ref, &newparam);
693 /* change to outgoing setup state */
694 call->state = CHAN_LCR_STATE_OUT_SETUP;
698 * send dialing info to LCR
699 * this function is called, when setup acknowledge is received and dialing
702 static void send_dialque_to_lcr(struct chan_call *call)
704 union parameter newparam;
706 if (!call->ast || !call->ref || !call->dialque[0])
709 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
711 /* send setup message to LCR */
712 memset(&newparam, 0, sizeof(union parameter));
714 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
716 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
717 call->dialque[0] = '\0';
718 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
722 * in case of a bridge, the unsupported message can be forwarded directly
723 * to the remote call.
725 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
729 if (!call->bridge_call) return;
730 CDEBUG(call, NULL, "Sending message due bridging.\n");
731 send_message(message_type, call->bridge_call->ref, param);
735 * send release message to LCR and import bchannel if exported
737 static void send_release_and_import(struct chan_call *call, int cause, int location)
739 union parameter newparam;
741 /* importing channel */
742 if (call->bchannel) {
743 memset(&newparam, 0, sizeof(union parameter));
744 newparam.bchannel.type = BCHANNEL_RELEASE;
745 newparam.bchannel.handle = call->bchannel->handle;
746 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
748 /* sending release */
749 memset(&newparam, 0, sizeof(union parameter));
750 newparam.disconnectinfo.cause = cause;
751 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
752 send_message(MESSAGE_RELEASE, call->ref, &newparam);
756 * check if extension matches and start asterisk
757 * if it can match, proceed
760 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
763 union parameter newparam;
764 char *exten = ast->exten;
768 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
772 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
773 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
777 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
778 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);
782 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
783 /* send setup acknowledge to lcr */
784 memset(&newparam, 0, sizeof(union parameter));
785 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
788 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
793 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
794 /* send setup acknowledge to lcr */
795 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
796 memset(&newparam, 0, sizeof(union parameter));
797 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
801 call->state = CHAN_LCR_STATE_IN_DIALING;
803 /* if match, start pbx */
804 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
805 CDEBUG(call, ast, "Extensions matches.\n");
810 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
816 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
824 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
825 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
827 /* release asterisk */
828 ast->hangupcause = call->cause;
829 /* change to release state */
830 call->state = CHAN_LCR_STATE_RELEASE;
831 ast_hangup(ast); // call will be destroyed here
835 /* send setup to asterisk */
836 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
838 #ifdef LCR_FOR_CALLWEAVER
840 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
843 ret = ast_pbx_start(ast);
845 cause = (ret==-2)?34:27;
848 call->pbx_started = 1;
849 ast_setstate(ast, AST_STATE_RING);
853 * incoming setup from LCR
855 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
857 struct ast_channel *ast;
859 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
861 /* create asterisk channel instrance */
863 #ifdef LCR_FOR_CALLWEAVER
864 ast = ast_channel_alloc(1);
867 #ifdef LCR_FOR_ASTERISK
868 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
873 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
874 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
881 ast->tech_pvt = call;
882 ast->tech = &lcr_tech;
883 ast->fds[0] = call->pipe[0];
885 /* fill setup information */
886 if (param->setup.dialinginfo.id)
887 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
888 if (param->setup.context[0])
889 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
891 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
892 if (param->setup.callerinfo.id[0])
893 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
894 if (param->setup.callerinfo.name[0])
895 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
896 if (param->setup.redirinfo.id[0])
897 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
898 switch (param->setup.callerinfo.present) {
899 case INFO_PRESENT_ALLOWED:
900 ast->cid.cid_pres = AST_PRES_ALLOWED;
902 case INFO_PRESENT_RESTRICTED:
903 ast->cid.cid_pres = AST_PRES_RESTRICTED;
906 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
908 switch (param->setup.callerinfo.ntype) {
909 case INFO_NTYPE_SUBSCRIBER:
910 ast->cid.cid_ton = 4;
912 case INFO_NTYPE_NATIONAL:
913 ast->cid.cid_ton = 2;
915 case INFO_NTYPE_INTERNATIONAL:
916 ast->cid.cid_ton = 1;
919 ast->cid.cid_ton = 0;
921 ast->transfercapability = param->setup.capainfo.bearer_capa;
922 /* enable hdlc if transcap is data */
923 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
925 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
927 /* configure channel */
928 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
929 ast->readformat = ast->rawreadformat = ast->nativeformats;
930 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
932 ast->hangupcause = 0;
935 call->state = CHAN_LCR_STATE_IN_SETUP;
937 if (!call->pbx_started)
938 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
942 * incoming setup acknowledge from LCR
944 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
946 if (!call->ast) return;
948 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
950 /* send pending digits in dialque */
951 if (call->dialque[0])
952 send_dialque_to_lcr(call);
953 /* change to overlap state */
954 call->state = CHAN_LCR_STATE_OUT_DIALING;
958 * incoming proceeding from LCR
960 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
962 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
965 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
966 /* queue event for asterisk */
967 if (call->ast && call->pbx_started) {
971 write(wake_pipe[1], &byte, 1);
973 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
979 * incoming alerting from LCR
981 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
983 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
986 call->state = CHAN_LCR_STATE_OUT_ALERTING;
987 /* queue event to asterisk */
988 if (call->ast && call->pbx_started) {
992 write(wake_pipe[1], &byte, 1);
994 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
999 * incoming connect from LCR
1001 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1003 union parameter newparam;
1005 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1008 call->state = CHAN_LCR_STATE_CONNECT;
1009 /* request bchannel */
1010 if (!call->bchannel) {
1011 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1012 memset(&newparam, 0, sizeof(union parameter));
1013 newparam.bchannel.type = BCHANNEL_REQUEST;
1014 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1016 /* copy connectinfo */
1017 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1018 /* queue event to asterisk */
1019 if (call->ast && call->pbx_started) {
1023 write(wake_pipe[1], &byte, 1);
1025 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1030 * incoming disconnect from LCR
1032 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1034 struct ast_channel *ast = call->ast;
1036 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1039 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1041 call->cause = param->disconnectinfo.cause;
1042 call->location = param->disconnectinfo.location;
1043 /* if bridge, forward disconnect and return */
1046 if (call->bridge_call) {
1047 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1048 bridge_message_if_bridged(call, message_type, param);
1052 /* release lcr with same cause */
1053 send_release_and_import(call, call->cause, call->location);
1055 /* change to release state */
1056 call->state = CHAN_LCR_STATE_RELEASE;
1057 /* queue release asterisk */
1059 ast->hangupcause = call->cause;
1060 if (call->pbx_started) {
1064 write(wake_pipe[1], &byte, 1);
1066 strcpy(call->queue_string, "H"); // overwrite other indications
1068 ast_hangup(ast); // call will be destroyed here
1074 * incoming release from LCR
1076 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1078 struct ast_channel *ast = call->ast;
1080 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1084 /* change to release state */
1085 call->state = CHAN_LCR_STATE_RELEASE;
1086 /* copy release info */
1088 call->cause = param->disconnectinfo.cause;
1089 call->location = param->disconnectinfo.location;
1091 /* if we have an asterisk instance, queue hangup, else we are done */
1093 ast->hangupcause = call->cause;
1094 if (call->pbx_started) {
1098 write(wake_pipe[1], &byte, 1);
1100 strcpy(call->queue_string, "H");
1102 ast_hangup(ast); // call will be destroyed here
1111 * incoming information from LCR
1113 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1115 struct ast_channel *ast = call->ast;
1117 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1121 /* pbx not started */
1122 if (!call->pbx_started) {
1123 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1124 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1125 lcr_start_pbx(call, ast, param->information.sending_complete);
1129 /* change dailing state after setup */
1130 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1131 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1132 call->state = CHAN_LCR_STATE_IN_DIALING;
1133 // ast_setstate(ast, AST_STATE_DIALING);
1137 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1141 write(wake_pipe[1], &byte, 1);
1143 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1146 /* use bridge to forware message not supported by asterisk */
1147 if (call->state == CHAN_LCR_STATE_CONNECT) {
1148 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1149 bridge_message_if_bridged(call, message_type, param);
1154 * incoming information from LCR
1156 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1158 union parameter newparam;
1160 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1162 /* request bchannel, if call is resumed and we don't have it */
1163 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1164 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1165 memset(&newparam, 0, sizeof(union parameter));
1166 newparam.bchannel.type = BCHANNEL_REQUEST;
1167 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1170 if (!call->ast) return;
1172 /* use bridge to forware message not supported by asterisk */
1173 bridge_message_if_bridged(call, message_type, param);
1177 * incoming information from LCR
1179 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1181 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1183 if (!call->ast) return;
1185 /* use bridge to forware message not supported by asterisk */
1186 bridge_message_if_bridged(call, message_type, param);
1190 * incoming pattern from LCR
1192 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1194 union parameter newparam;
1196 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1198 if (!call->ast) return;
1200 /* pattern are indicated only once */
1201 if (call->has_pattern)
1203 call->has_pattern = 1;
1205 /* request bchannel */
1206 if (!call->bchannel) {
1207 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1208 memset(&newparam, 0, sizeof(union parameter));
1209 newparam.bchannel.type = BCHANNEL_REQUEST;
1210 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1212 /* queue PROGRESS, because tones are available */
1213 if (call->ast && call->pbx_started) {
1217 write(wake_pipe[1], &byte, 1);
1219 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1224 * got dtmf from bchannel (locked state)
1226 void lcr_in_dtmf(struct chan_call *call, int val)
1228 struct ast_channel *ast = call->ast;
1233 if (!call->pbx_started)
1236 if (!call->dsp_dtmf) {
1237 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1241 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1247 write(wake_pipe[1], &byte, 1);
1249 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1253 * message received from LCR
1255 int receive_message(int message_type, unsigned int ref, union parameter *param)
1257 struct bchannel *bchannel;
1258 struct chan_call *call;
1259 union parameter newparam;
1261 memset(&newparam, 0, sizeof(union parameter));
1263 /* handle bchannel message*/
1264 if (message_type == MESSAGE_BCHANNEL) {
1265 switch(param->bchannel.type) {
1266 case BCHANNEL_ASSIGN:
1268 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1269 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1270 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1273 /* create bchannel */
1274 bchannel = alloc_bchannel(param->bchannel.handle);
1276 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1280 /* configure channel */
1281 bchannel->b_tx_gain = param->bchannel.tx_gain;
1282 bchannel->b_rx_gain = param->bchannel.rx_gain;
1283 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1284 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1285 bchannel->b_bf_len = param->bchannel.crypt_len;
1286 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1288 bchannel->b_txdata = 0;
1289 bchannel->b_tx_dejitter = 1;
1291 /* in case, ref is not set, this bchannel instance must
1292 * be created until it is removed again by LCR */
1294 call = find_call_ref(ref);
1296 bchannel->call = call;
1297 call->bchannel = bchannel;
1299 bchannel_dtmf(bchannel, 1);
1301 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1302 if (call->pipeline[0])
1303 bchannel_pipeline(bchannel, call->pipeline);
1305 bchannel_gain(bchannel, call->rx_gain, 0);
1307 bchannel_gain(bchannel, call->tx_gain, 1);
1308 if (call->bridge_id) {
1309 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1310 bchannel_join(bchannel, call->bridge_id);
1312 /* create only, if call exists, othewhise it bchannel is freed below... */
1313 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1314 bchannel_activate(bchannel, 1);
1317 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1318 newparam.bchannel.handle = param->bchannel.handle;
1319 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1320 /* if call has released before bchannel is assigned */
1322 newparam.bchannel.type = BCHANNEL_RELEASE;
1323 newparam.bchannel.handle = param->bchannel.handle;
1324 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1329 case BCHANNEL_REMOVE:
1330 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1331 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1332 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1335 /* unklink from call and destroy bchannel */
1336 free_bchannel(bchannel);
1339 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1340 newparam.bchannel.handle = param->bchannel.handle;
1341 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1346 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1351 /* handle new ref */
1352 if (message_type == MESSAGE_NEWREF) {
1353 if (param->direction) {
1354 /* new ref from lcr */
1355 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1356 if (!ref || find_call_ref(ref)) {
1357 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1360 /* allocate new call instance */
1361 call = alloc_call();
1363 call->state = CHAN_LCR_STATE_IN_PREPARE;
1366 call->ref_was_assigned = 1;
1367 /* set dtmf (default, use option 'n' to disable */
1369 /* wait for setup (or release from asterisk) */
1371 /* new ref, as requested from this remote application */
1372 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1373 call = find_call_ref(0);
1375 /* send release, if ref does not exist */
1376 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1377 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1382 call->ref_was_assigned = 1;
1383 /* set dtmf (default, use option 'n' to disable */
1385 /* send pending setup info */
1386 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1387 send_setup_to_lcr(call);
1388 /* release if asterisk has signed off */
1389 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1392 send_release_and_import(call, call->cause, call->location);
1394 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1405 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1408 call = find_call_ref(ref);
1410 /* ignore ref that is not used (anymore) */
1411 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1415 /* handle messages */
1416 switch(message_type) {
1418 lcr_in_setup(call, message_type, param);
1421 case MESSAGE_OVERLAP:
1422 lcr_in_overlap(call, message_type, param);
1425 case MESSAGE_PROCEEDING:
1426 lcr_in_proceeding(call, message_type, param);
1429 case MESSAGE_ALERTING:
1430 lcr_in_alerting(call, message_type, param);
1433 case MESSAGE_CONNECT:
1434 lcr_in_connect(call, message_type, param);
1437 case MESSAGE_DISCONNECT:
1438 lcr_in_disconnect(call, message_type, param);
1441 case MESSAGE_RELEASE:
1442 lcr_in_release(call, message_type, param);
1445 case MESSAGE_INFORMATION:
1446 lcr_in_information(call, message_type, param);
1449 case MESSAGE_NOTIFY:
1450 lcr_in_notify(call, message_type, param);
1453 case MESSAGE_FACILITY:
1454 lcr_in_facility(call, message_type, param);
1457 case MESSAGE_PATTERN: // audio available from LCR
1458 if (!call->has_pattern)
1459 lcr_in_pattern(call, message_type, param);
1462 case MESSAGE_NOPATTERN: // audio not available from LCR
1465 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1466 call->audiopath = param->audiopath;
1470 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1477 * release all calls (due to broken socket)
1479 static void release_all_calls(void)
1481 struct chan_call *call;
1486 /* no ast, so we may directly free call */
1488 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1492 /* already in release process */
1493 if (call->state == CHAN_LCR_STATE_RELEASE) {
1497 /* release or queue release */
1499 call->state = CHAN_LCR_STATE_RELEASE;
1500 if (!call->pbx_started) {
1501 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1502 ast_hangup(call->ast); // call will be destroyed here
1505 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1509 write(wake_pipe[1], &byte, 1);
1511 strcpy(call->queue_string, "H");
1515 /* release all bchannels */
1516 while(bchannel_first)
1517 free_bchannel(bchannel_first);
1520 void close_socket(void);
1523 * warning! not thread safe
1524 * returns -1 for socket error, 0 for no work, 1 for work
1526 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1529 struct admin_list *admin;
1530 struct admin_message msg;
1532 lock_debug("handle+");
1533 if ((what & LCR_FD_READ)) {
1534 /* read from socket */
1535 lock_debug("handle1");
1536 len = read(lcr_sock, &msg, sizeof(msg));
1537 lock_debug("handle2");
1539 CERROR(NULL, NULL, "Socket closed.\n");
1541 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1542 lock_debug("handle3");
1544 lock_debug("handle4");
1545 release_all_calls();
1546 lock_debug("handle5");
1547 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1548 lock_debug("handle-");
1552 if (len != sizeof(msg)) {
1553 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1556 if (msg.message != ADMIN_MESSAGE) {
1557 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1560 lock_debug("handleX");
1561 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1562 lock_debug("handleY");
1564 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1569 if ((what & LCR_FD_WRITE)) {
1570 /* write to socket */
1572 socket_fd.when &= ~LCR_FD_WRITE;
1573 lock_debug("handle-");
1576 lock_debug("handle6");
1577 admin = admin_first;
1578 len = write(lcr_sock, &admin->msg, sizeof(msg));
1579 lock_debug("handle7");
1581 CERROR(NULL, NULL, "Socket closed.\n");
1585 if (len != sizeof(msg)) {
1586 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1590 lock_debug("handle8");
1591 admin_first = admin->next;
1595 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1600 lock_debug("handle-");
1605 * open and close socket and thread
1607 int open_socket(void)
1610 struct sockaddr_un sock_address;
1611 union parameter param;
1614 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1615 CERROR(NULL, NULL, "Failed to create socket.\n");
1619 /* set socket address and name */
1620 memset(&sock_address, 0, sizeof(sock_address));
1621 sock_address.sun_family = PF_UNIX;
1622 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1624 /* connect socket */
1625 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1628 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1632 /* register socket fd */
1633 memset(&socket_fd, 0, sizeof(socket_fd));
1634 socket_fd.fd = lcr_sock;
1635 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1637 /* enque hello message */
1638 memset(¶m, 0, sizeof(param));
1639 strcpy(param.hello.application, "asterisk");
1640 send_message(MESSAGE_HELLO, 0, ¶m);
1645 void close_socket(void)
1647 struct admin_list *admin, *temp;
1649 unregister_fd(&socket_fd);
1651 /* flush pending messages */
1652 admin = admin_first;
1655 admin = admin->next;
1668 /* sending queue to asterisk */
1669 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1673 lock_debug("wake+");
1674 read(wake_pipe[0], &byte, 1);
1677 lock_debug("wake-");
1682 static void handle_queue()
1684 struct chan_call *call;
1685 struct ast_channel *ast;
1686 struct ast_frame fr;
1691 p = call->queue_string;
1695 // ast_channel_lock(ast);
1700 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1701 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1704 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1705 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1708 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1709 ast_queue_control(ast, AST_CONTROL_RINGING);
1710 ast_setstate(ast, AST_STATE_RINGING);
1713 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1714 ast_queue_control(ast, AST_CONTROL_ANSWER);
1717 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1718 ast_queue_hangup(ast);
1720 case '1': case '2': case '3': case 'A':
1721 case '4': case '5': case '6': case 'B':
1722 case '7': case '8': case '9': case 'C':
1723 case '*': case '0': case '#': case 'D':
1724 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1725 /* send digit to asterisk */
1726 memset(&fr, 0, sizeof(fr));
1728 #ifdef LCR_FOR_ASTERISK
1729 fr.frametype = AST_FRAME_DTMF_BEGIN;
1732 #ifdef LCR_FOR_CALLWEAVER
1733 fr.frametype = AST_FRAME_DTMF;
1737 fr.delivery = ast_tv(0, 0);
1738 ast_queue_frame(ast, &fr);
1740 #ifdef LCR_FOR_ASTERISK
1741 fr.frametype = AST_FRAME_DTMF_END;
1742 ast_queue_frame(ast, &fr);
1747 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1751 call->queue_string[0] = '\0';
1752 // ast_channel_unlock(ast);
1759 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1761 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1762 if (open_socket() < 0)
1763 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1768 void lock_chan(void)
1771 ast_mutex_lock(&chan_lock);
1775 void unlock_chan(void)
1777 ast_mutex_unlock(&chan_lock);
1781 /* chan_lcr thread */
1782 static void *chan_thread(void *arg)
1784 if (pipe(wake_pipe) < 0) {
1785 CERROR(NULL, NULL, "Failed to open pipe.\n");
1788 memset(&wake_fd, 0, sizeof(wake_fd));
1789 wake_fd.fd = wake_pipe[0];
1790 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1792 memset(&socket_retry, 0, sizeof(socket_retry));
1793 add_timer(&socket_retry, handle_retry, NULL, 0);
1795 bchannel_pid = getpid();
1797 /* open socket the first time */
1798 handle_retry(NULL, NULL, 0);
1801 ast_mutex_lock(&chan_lock);
1806 select_main(0, &global_change, lock_chan, unlock_chan);
1811 del_timer(&socket_retry);
1813 unregister_fd(&wake_fd);
1814 close(wake_pipe[0]);
1815 close(wake_pipe[1]);
1817 CERROR(NULL, NULL, "Thread exit.\n");
1819 ast_mutex_unlock(&chan_lock);
1826 * new asterisk instance
1829 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1831 char exten[256], *dial, *interface, *opt;
1832 struct ast_channel *ast;
1833 struct chan_call *call;
1836 ast_mutex_lock(&chan_lock);
1838 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1840 /* if socket is closed */
1842 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1843 ast_mutex_unlock(&chan_lock);
1848 /* create call instance */
1849 call = alloc_call();
1851 /* failed to create instance */
1852 ast_mutex_unlock(&chan_lock);
1857 /* create asterisk channel instrance */
1859 #ifdef LCR_FOR_ASTERISK
1860 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1863 #ifdef LCR_FOR_CALLWEAVER
1864 ast = ast_channel_alloc(1);
1868 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1870 /* failed to create instance */
1871 ast_mutex_unlock(&chan_lock);
1875 ast->tech = &lcr_tech;
1876 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1877 /* configure channel */
1878 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1879 ast->readformat = ast->rawreadformat = ast->nativeformats;
1880 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1882 ast->hangupcause = 0;
1886 ast->tech_pvt = call;
1887 ast->fds[0] = call->pipe[0];
1888 call->pbx_started = 0;
1890 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1893 * Extract interface, dialstring, options from data.
1896 * <interface>/<dialstring>
1897 * <interface>/<dialstring>/options
1899 strncpy(exten, (char *)data, sizeof(exten)-1);
1900 exten[sizeof(exten)-1] = '\0';
1901 if ((dial = strchr(exten, '/'))) {
1904 if ((opt = strchr(dial, '/')))
1913 strncpy(call->interface, interface, sizeof(call->interface)-1);
1914 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1915 apply_opt(call, (char *)opt);
1917 ast_mutex_unlock(&chan_lock);
1923 * call from asterisk
1925 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1927 union parameter newparam;
1928 struct chan_call *call;
1931 ast_mutex_lock(&chan_lock);
1933 call = ast->tech_pvt;
1935 #ifdef LCR_FOR_CALLWEAVER
1937 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1941 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1942 ast_mutex_unlock(&chan_lock);
1947 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1949 /* pbx process is started */
1950 call->pbx_started = 1;
1951 /* send MESSAGE_NEWREF */
1952 memset(&newparam, 0, sizeof(union parameter));
1953 newparam.direction = 0; /* request from app */
1954 send_message(MESSAGE_NEWREF, 0, &newparam);
1956 /* set hdlc if capability requires hdlc */
1957 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1958 || ast->transfercapability == INFO_BC_DATARESTRICTED
1959 || ast->transfercapability == INFO_BC_VIDEO)
1961 /* if hdlc is forced by option, we change transcap to data */
1963 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1964 && ast->transfercapability != INFO_BC_DATARESTRICTED
1965 && ast->transfercapability != INFO_BC_VIDEO)
1966 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1968 call->cid_num[0] = 0;
1969 call->cid_name[0] = 0;
1970 call->cid_rdnis[0] = 0;
1972 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1973 strncpy(call->cid_num, ast->cid.cid_num,
1974 sizeof(call->cid_num)-1);
1976 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1977 strncpy(call->cid_name, ast->cid.cid_name,
1978 sizeof(call->cid_name)-1);
1979 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1980 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1981 sizeof(call->cid_rdnis)-1);
1983 ast_mutex_unlock(&chan_lock);
1988 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1990 static const char* dtmf_tones[] = {
1991 "!941+1336/100,!0/100", /* 0 */
1992 "!697+1209/100,!0/100", /* 1 */
1993 "!697+1336/100,!0/100", /* 2 */
1994 "!697+1477/100,!0/100", /* 3 */
1995 "!770+1209/100,!0/100", /* 4 */
1996 "!770+1336/100,!0/100", /* 5 */
1997 "!770+1477/100,!0/100", /* 6 */
1998 "!852+1209/100,!0/100", /* 7 */
1999 "!852+1336/100,!0/100", /* 8 */
2000 "!852+1477/100,!0/100", /* 9 */
2001 "!697+1633/100,!0/100", /* A */
2002 "!770+1633/100,!0/100", /* B */
2003 "!852+1633/100,!0/100", /* C */
2004 "!941+1633/100,!0/100", /* D */
2005 "!941+1209/100,!0/100", /* * */
2006 "!941+1477/100,!0/100" }; /* # */
2008 if (digit >= '0' && digit <='9')
2009 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2010 else if (digit >= 'A' && digit <= 'D')
2011 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2012 else if (digit == '*')
2013 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2014 else if (digit == '#')
2015 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2018 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
2019 "'%c' for '%s'\n", digit, ast->name);
2023 #ifdef LCR_FOR_ASTERISK
2024 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2026 #ifdef LCR_FOR_CALLWEAVER
2027 static int lcr_digit(struct ast_channel *ast, char digit)
2030 struct chan_call *call;
2031 union parameter newparam;
2034 #ifdef LCR_FOR_CALLWEAVER
2035 int inband_dtmf = 0;
2038 /* only pass IA5 number space */
2039 if (digit > 126 || digit < 32)
2043 ast_mutex_lock(&chan_lock);
2045 call = ast->tech_pvt;
2047 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2048 ast_mutex_unlock(&chan_lock);
2053 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2055 /* send information or queue them */
2056 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2057 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2058 memset(&newparam, 0, sizeof(union parameter));
2060 newparam.information.keypad[0] = digit;
2061 newparam.information.keypad[1] = '\0';
2063 newparam.information.id[0] = digit;
2064 newparam.information.id[1] = '\0';
2066 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2069 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2070 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2072 strncat(call->dialque, buf, strlen(call->dialque)-1);
2075 ast_mutex_unlock(&chan_lock);
2078 #ifdef LCR_FOR_ASTERISK
2082 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2084 int inband_dtmf = 0;
2085 struct chan_call *call;
2089 ast_mutex_lock(&chan_lock);
2092 call = ast->tech_pvt;
2096 "Received digit from Asterisk, "
2097 "but no call instance exists.\n");
2098 ast_mutex_unlock(&chan_lock);
2103 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2105 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2109 ast_mutex_unlock(&chan_lock);
2113 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2114 send_digit_to_chan(ast, digit);
2120 static int lcr_answer(struct ast_channel *ast)
2122 union parameter newparam;
2123 struct chan_call *call;
2126 ast_mutex_lock(&chan_lock);
2128 call = ast->tech_pvt;
2130 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2131 ast_mutex_unlock(&chan_lock);
2136 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2138 /* copy connectinfo, if bridged */
2139 if (call->bridge_call)
2140 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2141 /* send connect message to lcr */
2142 if (call->state != CHAN_LCR_STATE_CONNECT) {
2143 memset(&newparam, 0, sizeof(union parameter));
2144 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2145 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2146 call->state = CHAN_LCR_STATE_CONNECT;
2149 /* request bchannel */
2150 if (!call->bchannel) {
2151 CDEBUG(call, ast, "Requesting B-channel.\n");
2152 memset(&newparam, 0, sizeof(union parameter));
2153 newparam.bchannel.type = BCHANNEL_REQUEST;
2154 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2157 // memset(&newparam, 0, sizeof(union parameter));
2158 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2160 ast_mutex_unlock(&chan_lock);
2165 static int lcr_hangup(struct ast_channel *ast)
2167 struct chan_call *call;
2168 pthread_t tid = pthread_self();
2170 if (!pthread_equal(tid, chan_tid)) {
2172 ast_mutex_lock(&chan_lock);
2175 call = ast->tech_pvt;
2177 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2178 if (!pthread_equal(tid, chan_tid)) {
2179 ast_mutex_unlock(&chan_lock);
2185 if (!pthread_equal(tid, chan_tid))
2186 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2188 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2190 /* disconnect asterisk, maybe not required */
2191 ast->tech_pvt = NULL;
2195 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2196 if (ast->hangupcause > 0)
2197 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2199 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2202 if (!pthread_equal(tid, chan_tid)) {
2203 ast_mutex_unlock(&chan_lock);
2208 /* ref is not set, due to prepare setup or release */
2209 if (call->state == CHAN_LCR_STATE_RELEASE) {
2210 /* we get the response to our release */
2211 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2214 /* during prepare, we change to release state */
2215 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2216 call->state = CHAN_LCR_STATE_RELEASE;
2220 if (!pthread_equal(tid, chan_tid)) {
2221 ast_mutex_unlock(&chan_lock);
2227 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2229 struct chan_call *call;
2232 CDEBUG(NULL, ast, "No subclass\n");
2233 if (!(f->subclass & ast->nativeformats))
2234 CDEBUG(NULL, ast, "Unexpected format.\n");
2237 ast_mutex_lock(&chan_lock);
2239 call = ast->tech_pvt;
2241 ast_mutex_unlock(&chan_lock);
2245 if (call->bchannel && f->samples)
2246 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2247 ast_mutex_unlock(&chan_lock);
2253 static struct ast_frame *lcr_read(struct ast_channel *ast)
2255 struct chan_call *call;
2259 ast_mutex_lock(&chan_lock);
2261 call = ast->tech_pvt;
2263 ast_mutex_unlock(&chan_lock);
2267 if (call->pipe[0] > -1) {
2268 if (call->rebuffer && !call->hdlc) {
2269 /* Make sure we have a complete 20ms (160byte) frame */
2271 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2273 call->framepos += len;
2277 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2280 if (len < 0 && errno == EAGAIN) {
2281 ast_mutex_unlock(&chan_lock);
2284 #ifdef LCR_FOR_ASTERISK
2285 return &ast_null_frame;
2288 #ifdef LCR_FOR_CALLWEAVER
2294 close(call->pipe[0]);
2297 ast_mutex_unlock(&chan_lock);
2300 } else if (call->rebuffer && call->framepos < 160) {
2301 /* Not a complete frame, so we send a null-frame */
2302 ast_mutex_unlock(&chan_lock);
2304 return &ast_null_frame;
2308 call->read_fr.frametype = AST_FRAME_VOICE;
2309 call->read_fr.subclass = ast->nativeformats;
2310 if (call->rebuffer) {
2311 call->read_fr.datalen = call->framepos;
2312 call->read_fr.samples = call->framepos;
2315 call->read_fr.datalen = len;
2316 call->read_fr.samples = len;
2318 call->read_fr.delivery = ast_tv(0,0);
2319 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2320 ast_mutex_unlock(&chan_lock);
2323 return &call->read_fr;
2326 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2328 union parameter newparam;
2330 struct chan_call *call;
2331 const struct tone_zone_sound *ts = NULL;
2334 ast_mutex_lock(&chan_lock);
2336 call = ast->tech_pvt;
2338 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2339 ast_mutex_unlock(&chan_lock);
2345 case AST_CONTROL_BUSY:
2346 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2347 ast_setstate(ast, AST_STATE_BUSY);
2348 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2349 /* send message to lcr */
2350 memset(&newparam, 0, sizeof(union parameter));
2351 newparam.disconnectinfo.cause = 17;
2352 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2353 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2355 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2357 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2358 ts = ast_get_indication_tone(ast->zone, "busy");
2361 case AST_CONTROL_CONGESTION:
2362 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2363 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2364 /* send message to lcr */
2365 memset(&newparam, 0, sizeof(union parameter));
2366 newparam.disconnectinfo.cause = ast->hangupcause;
2367 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2368 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2370 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2372 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2373 ts = ast_get_indication_tone(ast->zone, "congestion");
2376 case AST_CONTROL_PROCEEDING:
2377 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2378 if (call->state == CHAN_LCR_STATE_IN_SETUP
2379 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2380 /* send message to lcr */
2381 memset(&newparam, 0, sizeof(union parameter));
2382 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2384 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2387 case AST_CONTROL_RINGING:
2388 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2389 ast_setstate(ast, AST_STATE_RING);
2390 if (call->state == CHAN_LCR_STATE_IN_SETUP
2391 || call->state == CHAN_LCR_STATE_IN_DIALING
2392 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2393 /* send message to lcr */
2394 memset(&newparam, 0, sizeof(union parameter));
2395 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2397 call->state = CHAN_LCR_STATE_IN_ALERTING;
2399 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2400 ts = ast_get_indication_tone(ast->zone, "ring");
2403 case AST_CONTROL_PROGRESS:
2404 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2405 /* request bchannel */
2406 if (!call->bchannel) {
2407 CDEBUG(call, ast, "Requesting B-channel.\n");
2408 memset(&newparam, 0, sizeof(union parameter));
2409 newparam.bchannel.type = BCHANNEL_REQUEST;
2410 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2414 CDEBUG(call, ast, "Received indicate -1.\n");
2415 ast_playtones_stop(ast);
2419 case AST_CONTROL_VIDUPDATE:
2420 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2423 case AST_CONTROL_HOLD:
2424 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2425 /* send message to lcr */
2426 memset(&newparam, 0, sizeof(union parameter));
2427 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2428 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2430 /*start music onhold*/
2431 #ifdef LCR_FOR_ASTERISK
2432 ast_moh_start(ast,data,ast->musicclass);
2435 #ifdef LCR_FOR_CALLWEAVER
2436 ast_moh_start(ast, NULL);
2441 case AST_CONTROL_UNHOLD:
2442 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2443 /* send message to lcr */
2444 memset(&newparam, 0, sizeof(union parameter));
2445 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2446 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2452 #ifdef AST_CONTROL_SRCUPDATE
2453 case AST_CONTROL_SRCUPDATE:
2457 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2460 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2465 if (ts && ts->data[0]) {
2466 ast_playtones_start(ast, 0, ts->data, 1);
2470 ast_mutex_unlock(&chan_lock);
2478 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2480 struct chan_call *call;
2487 ast_mutex_lock(&chan_lock);
2489 call = ast->tech_pvt;
2491 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2492 ast_mutex_unlock(&chan_lock);
2497 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2499 ast_mutex_unlock(&chan_lock);
2505 * send_text asterisk
2507 static int lcr_send_text(struct ast_channel *ast, const char *text)
2509 struct chan_call *call;
2510 union parameter newparam;
2513 ast_mutex_lock(&chan_lock);
2515 call = ast->tech_pvt;
2517 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2518 ast_mutex_unlock(&chan_lock);
2523 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2524 memset(&newparam, 0, sizeof(union parameter));
2525 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2526 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2527 ast_mutex_unlock(&chan_lock);
2535 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2536 struct ast_channel *ast2, int flags,
2537 struct ast_frame **fo,
2538 struct ast_channel **rc, int timeoutms)
2541 struct chan_call *call1, *call2;
2542 struct ast_channel *carr[2], *who;
2544 struct ast_frame *f;
2547 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2552 /* join via dsp (if the channels are currently open) */
2554 ast_mutex_lock(&chan_lock);
2556 call1 = ast1->tech_pvt;
2557 call2 = ast2->tech_pvt;
2558 if (!call1 || !call2) {
2559 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2560 ast_mutex_unlock(&chan_lock);
2562 return AST_BRIDGE_COMPLETE;
2565 /* join, if both call instances uses dsp
2566 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2568 if (!call1->nodsp && !call2->nodsp) {
2569 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2571 /* get bridge id and join */
2572 bridge_id = new_bridge_id();
2574 call1->bridge_id = bridge_id;
2575 if (call1->bchannel)
2576 bchannel_join(call1->bchannel, bridge_id);
2578 call2->bridge_id = bridge_id;
2579 if (call2->bchannel)
2580 bchannel_join(call2->bchannel, bridge_id);
2582 if (call1->nodsp && call2->nodsp)
2583 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2585 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2586 call1->bridge_call = call2;
2587 call2->bridge_call = call1;
2589 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2590 || call1->state == CHAN_LCR_STATE_IN_DIALING
2591 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2592 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2593 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2596 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2597 || call2->state == CHAN_LCR_STATE_IN_DIALING
2598 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2599 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2600 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2604 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2605 so let's do it for them. Hmpf.
2608 if (call1->on_hold) {
2609 union parameter newparam;
2611 memset(&newparam, 0, sizeof(union parameter));
2612 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2613 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2618 if (call2->on_hold) {
2619 union parameter newparam;
2621 memset(&newparam, 0, sizeof(union parameter));
2622 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2623 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2628 ast_mutex_unlock(&chan_lock);
2633 who = ast_waitfor_n(carr, 2, &to);
2636 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2641 if (!f || f->frametype == AST_FRAME_CONTROL) {
2643 CDEBUG(NULL, NULL, "Got hangup.\n");
2645 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2652 if ( f->frametype == AST_FRAME_DTMF ) {
2653 CDEBUG(NULL, NULL, "Got DTMF.\n");
2669 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2671 /* split channels */
2673 ast_mutex_lock(&chan_lock);
2675 call1 = ast1->tech_pvt;
2676 call2 = ast2->tech_pvt;
2677 if (call1 && call1->bridge_id) {
2678 call1->bridge_id = 0;
2679 if (call1->bchannel)
2680 bchannel_join(call1->bchannel, 0);
2681 if (call1->bridge_call)
2682 call1->bridge_call->bridge_call = NULL;
2684 if (call2 && call1->bridge_id) {
2685 call2->bridge_id = 0;
2686 if (call2->bchannel)
2687 bchannel_join(call2->bchannel, 0);
2688 if (call2->bridge_call)
2689 call2->bridge_call->bridge_call = NULL;
2691 call1->bridge_call = NULL;
2692 call2->bridge_call = NULL;
2694 ast_mutex_unlock(&chan_lock);
2696 return AST_BRIDGE_COMPLETE;
2698 static struct ast_channel_tech lcr_tech = {
2700 .description = "Channel driver for connecting to Linux-Call-Router",
2701 .capabilities = AST_FORMAT_ALAW,
2702 .requester = lcr_request,
2704 #ifdef LCR_FOR_ASTERISK
2705 .send_digit_begin = lcr_digit_begin,
2706 .send_digit_end = lcr_digit_end,
2709 #ifdef LCR_FOR_CALLWEAVER
2710 .send_digit = lcr_digit,
2714 .bridge = lcr_bridge,
2715 .hangup = lcr_hangup,
2716 .answer = lcr_answer,
2719 .indicate = lcr_indicate,
2721 .send_text = lcr_send_text,
2730 static int lcr_show_lcr (int fd, int argc, char *argv[])
2735 static int lcr_show_calls (int fd, int argc, char *argv[])
2740 static int lcr_reload_routing (int fd, int argc, char *argv[])
2745 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2750 static int lcr_port_block (int fd, int argc, char *argv[])
2755 static int lcr_port_unblock (int fd, int argc, char *argv[])
2760 static int lcr_port_unload (int fd, int argc, char *argv[])
2765 static struct ast_cli_entry cli_show_lcr =
2766 { {"lcr", "show", "lcr", NULL},
2768 "Shows current states of LCR core",
2769 "Usage: lcr show lcr\n",
2772 static struct ast_cli_entry cli_show_calls =
2773 { {"lcr", "show", "calls", NULL},
2775 "Shows current calls made by LCR and Asterisk",
2776 "Usage: lcr show calls\n",
2779 static struct ast_cli_entry cli_reload_routing =
2780 { {"lcr", "reload", "routing", NULL},
2782 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2783 "Usage: lcr reload routing\n",
2786 static struct ast_cli_entry cli_reload_interfaces =
2787 { {"lcr", "reload", "interfaces", NULL},
2788 lcr_reload_interfaces,
2789 "Reloads interfaces conf of LCR",
2790 "Usage: lcr reload interfaces\n",
2793 static struct ast_cli_entry cli_port_block =
2794 { {"lcr", "port", "block", NULL},
2796 "Blocks LCR port for further calls",
2797 "Usage: lcr port block \"<port>\"\n",
2800 static struct ast_cli_entry cli_port_unblock =
2801 { {"lcr", "port", "unblock", NULL},
2803 "Unblocks or loads LCR port, port is opened my mISDN",
2804 "Usage: lcr port unblock \"<port>\"\n",
2807 static struct ast_cli_entry cli_port_unload =
2808 { {"lcr", "port", "unload", NULL},
2810 "Unloads LCR port, port is closes by mISDN",
2811 "Usage: lcr port unload \"<port>\"\n",
2816 #ifdef LCR_FOR_ASTERISK
2817 static int lcr_config_exec(struct ast_channel *ast, void *data)
2820 #ifdef LCR_FOR_CALLWEAVER
2821 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2824 struct chan_call *call;
2827 ast_mutex_lock(&chan_lock);
2830 #ifdef LCR_FOR_ASTERISK
2831 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2834 #ifdef LCR_FOR_CALLWEAVER
2835 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2841 if (call->ast == ast)
2847 #ifdef LCR_FOR_ASTERISK
2848 apply_opt(call, (char *)data);
2851 #ifdef LCR_FOR_CALLWEAVER
2852 apply_opt(call, (char *)argv[0]);
2856 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2858 ast_mutex_unlock(&chan_lock);
2864 * module loading and destruction
2866 int load_module(void)
2869 char options_error[256];
2871 for (i = 0; i < 256; i++) {
2872 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2873 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2876 if (read_options(options_error) == 0) {
2877 CERROR(NULL, NULL, "%s", options_error);
2879 #ifdef LCR_FOR_ASTERISK
2880 return AST_MODULE_LOAD_DECLINE;
2883 #ifdef LCR_FOR_CALLWEAVER
2889 ast_mutex_init(&chan_lock);
2890 ast_mutex_init(&log_lock);
2892 if (bchannel_initialize()) {
2893 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2896 #ifdef LCR_FOR_ASTERISK
2897 return AST_MODULE_LOAD_DECLINE;
2900 #ifdef LCR_FOR_CALLWEAVER
2906 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2907 if (ast_channel_register(&lcr_tech)) {
2908 CERROR(NULL, NULL, "Unable to register channel class\n");
2909 bchannel_deinitialize();
2912 #ifdef LCR_FOR_ASTERISK
2913 return AST_MODULE_LOAD_DECLINE;
2916 #ifdef LCR_FOR_CALLWEAVER
2921 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2923 #ifdef LCR_FOR_ASTERISK
2924 "lcr_config(<opt><optarg>:<opt>:...)\n"
2927 #ifdef LCR_FOR_CALLWEAVER
2928 "lcr_config(<opt><optarg>:<opt>:...)\n",
2931 "Sets LCR opts. and optargs\n"
2933 "The available options are:\n"
2934 " d - Send display text on called phone, text is the optarg.\n"
2935 " n - Don't detect dtmf tones on called channel.\n"
2936 " h - Force data call (HDLC).\n"
2937 " t - Disable mISDN_dsp features (required for fax application).\n"
2938 " q - Add queue to make fax stream seamless (required for fax app).\n"
2939 " Use queue size in miliseconds for optarg. (try 250)\n"
2940 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2941 " Use time to detect for optarg.\n"
2942 " c - Make crypted outgoing call, optarg is keyindex.\n"
2943 " e - Perform echo cancelation on this channel.\n"
2944 " Takes mISDN pipeline option as optarg.\n"
2945 " s - Send Non Inband DTMF as inband.\n"
2946 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2947 " vr - rxgain control\n"
2948 " vt - txgain control\n"
2949 " Volume changes at factor 2 ^ optarg.\n"
2950 " k - use keypad to dial this call.\n"
2952 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
2953 " -> use 16 for fax (3.1k audio)\n"
2955 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
2956 "options: \"n:t:q250\" for seamless audio transmission.\n"
2961 ast_cli_register(&cli_show_lcr);
2962 ast_cli_register(&cli_show_calls);
2963 ast_cli_register(&cli_reload_routing);
2964 ast_cli_register(&cli_reload_interfaces);
2965 ast_cli_register(&cli_port_block);
2966 ast_cli_register(&cli_port_unblock);
2967 ast_cli_register(&cli_port_unload);
2971 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
2972 /* failed to create thread */
2973 bchannel_deinitialize();
2975 ast_channel_unregister(&lcr_tech);
2977 #ifdef LCR_FOR_ASTERISK
2978 return AST_MODULE_LOAD_DECLINE;
2981 #ifdef LCR_FOR_CALLWEAVER
2989 int unload_module(void)
2991 /* First, take us out of the channel loop */
2992 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2995 pthread_join(chan_tid, NULL);
2997 ast_channel_unregister(&lcr_tech);
2999 ast_unregister_application("lcr_config");
3002 if (mISDN_created) {
3003 bchannel_deinitialize();
3007 if (lcr_sock >= 0) {
3015 int reload_module(void)
3021 #ifdef LCR_FOR_ASTERISK
3022 #define AST_MODULE "chan_lcr"
3025 #ifdef LCR_FOR_CALLWEAVER
3031 ast_mutex_lock(&usecnt_lock);
3034 ast_mutex_unlock(&usecnt_lock);
3040 #ifdef LCR_FOR_ASTERISK
3041 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3042 .load = load_module,
3043 .unload = unload_module,
3044 .reload = reload_module,
3048 #ifdef LCR_FOR_CALLWEAVER
3049 char *description(void)