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 printf("%s", text); fflush(stdout);
234 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, ...)
237 char call_text[128] = "NULL";
238 char ast_text[128] = "NULL";
242 ast_mutex_lock(&log_lock);
245 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
246 buffer[sizeof(buffer)-1]=0;
250 sprintf(call_text, "%d", call->ref);
252 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
253 ast_text[sizeof(ast_text)-1] = '\0';
255 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
257 ast_mutex_unlock(&log_lock);
262 * channel and call instances
264 struct chan_call *call_first;
268 * special case: 0: find new ref, that has not been assigned a ref yet
271 struct chan_call *find_call_ref(unsigned int ref)
273 struct chan_call *call = call_first;
274 int assigned = (ref > 0);
277 if (call->ref == ref && call->ref_was_assigned == assigned)
284 void free_call(struct chan_call *call)
286 struct chan_call **temp = &call_first;
290 *temp = (*temp)->next;
291 if (call->pipe[0] > -1)
292 close(call->pipe[0]);
293 if (call->pipe[1] > -1)
294 close(call->pipe[1]);
295 if (call->bchannel) {
296 if (call->bchannel->call != call)
297 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
298 call->bchannel->call = NULL;
300 if (call->bridge_call) {
301 if (call->bridge_call->bridge_call != call)
302 CERROR(call, NULL, "Linked call structure has no link to us.\n");
303 call->bridge_call->bridge_call = NULL;
306 ast_translator_free_path(call->trans);
308 ast_dsp_free(call->dsp);
309 CDEBUG(call, NULL, "Call instance freed.\n");
314 temp = &((*temp)->next);
316 CERROR(call, NULL, "Call instance not found in list.\n");
319 struct chan_call *alloc_call(void)
321 struct chan_call **callp = &call_first;
324 callp = &((*callp)->next);
326 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
328 memset(*callp, 0, sizeof(struct chan_call));
329 if (pipe((*callp)->pipe) < 0) {
330 CERROR(*callp, NULL, "Failed to create pipe.\n");
334 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
335 CDEBUG(*callp, NULL, "Call instance allocated.\n");
339 unsigned short new_bridge_id(void)
341 struct chan_call *call;
342 unsigned short id = 1;
344 /* search for lowest bridge id that is not in use and not 0 */
348 if (call->bridge_id == id)
356 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
361 * enque message to LCR
363 int send_message(int message_type, unsigned int ref, union parameter *param)
365 struct admin_list *admin, **adminp;
368 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
371 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
373 adminp = &admin_first;
375 adminp = &((*adminp)->next);
376 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
378 CERROR(NULL, NULL, "No memory for message to LCR.\n");
383 admin->msg.message = ADMIN_MESSAGE;
384 admin->msg.u.msg.type = message_type;
385 admin->msg.u.msg.ref = ref;
386 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
387 socket_fd.when |= LCR_FD_WRITE;
391 write(wake_pipe[1], &byte, 1);
398 * apply options (in locked state)
400 void apply_opt(struct chan_call *call, char *data)
402 union parameter newparam;
403 char string[1024], *p = string, *opt, *key;
409 strncpy(string, data, sizeof(string)-1);
410 string[sizeof(string)-1] = '\0';
413 while((opt = strsep(&p, ":"))) {
416 if (opt[1] == '\0') {
417 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
420 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
421 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
422 strncpy(call->display, opt+1, sizeof(call->display)-1);
424 memset(&newparam, 0, sizeof(union parameter));
425 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
426 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
430 if (opt[1] != '\0') {
431 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
434 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
435 if (call->dsp_dtmf) {
438 bchannel_dtmf(call->bchannel, 0);
442 if (opt[1] == '\0') {
443 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
447 /* check for 0xXXXX... type of key */
448 if (!!strncmp((char *)key, "0x", 2)) {
449 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
453 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
454 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
459 if (*key>='0' && *key<='9')
460 call->bf_key[i] = (*key-'0') << 8;
461 else if (*key>='a' && *key<='f')
462 call->bf_key[i] = (*key-'a'+10) << 8;
463 else if (*key>='A' && *key<='F')
464 call->bf_key[i] = (*key-'A'+10) << 8;
468 if (*key>='0' && *key<='9')
469 call->bf_key[i] += (*key - '0');
470 else if (*key>='a' && *key<='f')
471 call->bf_key[i] += (*key - 'a' + 10);
472 else if (*key>='A' && *key<='F')
473 call->bf_key[i] += (*key - 'A' + 10);
480 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
484 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
486 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
489 if (opt[1] != '\0') {
490 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
493 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
498 if (opt[1] != '\0') {
499 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
502 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
507 if (opt[1] == '\0') {
508 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
511 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
512 call->nodsp_queue = atoi(opt+1);
515 if (opt[1] == '\0') {
516 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
519 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
520 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
522 bchannel_pipeline(call->bchannel, call->pipeline);
525 if (opt[1] == '\0') {
526 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
529 call->faxdetect=atoi(opt+1);
531 call->dsp=ast_dsp_new();
533 #ifdef LCR_FOR_CALLWEAVER
534 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
536 #ifdef LCR_FOR_ASTERISK
537 #ifdef DSP_FEATURE_DTMF_DETECT
538 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
540 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
545 #ifdef LCR_FOR_CALLWEAVER
546 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
548 #ifdef LCR_FOR_ASTERISK
549 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
552 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
555 if (opt[1] != '\0') {
556 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
559 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
564 if (opt[1] != '\0') {
565 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
568 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
569 call->inband_dtmf = 1;
572 if (opt[1] != 'r' && opt[1] != 't') {
573 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
577 if (gain < -8 || gain >8) {
578 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
581 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
583 call->rx_gain = gain;
585 bchannel_gain(call->bchannel, call->rx_gain, 0);
587 call->tx_gain = gain;
589 bchannel_gain(call->bchannel, call->tx_gain, 1);
593 if (opt[1] != '\0') {
594 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
597 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
602 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
606 /* re-open, if bchannel is created */
607 if (call->bchannel && call->bchannel->b_sock > -1) {
608 bchannel_destroy(call->bchannel);
609 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
610 bchannel_activate(call->bchannel, 1);
615 * send setup info to LCR
616 * this function is called, when asterisk call is received and ref is received
618 static void send_setup_to_lcr(struct chan_call *call)
620 union parameter newparam;
621 struct ast_channel *ast = call->ast;
624 if (!call->ast || !call->ref)
627 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
629 /* send setup message to LCR */
630 memset(&newparam, 0, sizeof(union parameter));
631 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
632 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
634 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
636 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
637 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
638 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
639 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
640 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
641 call->display[0] = '\0';
642 if (call->cid_num[0])
643 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
644 if (call->cid_name[0])
645 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
646 if (call->cid_rdnis[0]) {
647 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
648 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
649 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
651 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
652 case AST_PRES_RESTRICTED:
653 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
655 case AST_PRES_UNAVAILABLE:
656 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
658 case AST_PRES_ALLOWED:
660 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
662 switch(ast->cid.cid_ton) {
664 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
667 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
670 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
673 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
675 tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
677 ast->transfercapability = atoi(tmp);
678 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
679 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
681 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
683 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
684 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
686 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
687 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
688 send_message(MESSAGE_SETUP, call->ref, &newparam);
690 /* change to outgoing setup state */
691 call->state = CHAN_LCR_STATE_OUT_SETUP;
695 * send dialing info to LCR
696 * this function is called, when setup acknowledge is received and dialing
699 static void send_dialque_to_lcr(struct chan_call *call)
701 union parameter newparam;
703 if (!call->ast || !call->ref || !call->dialque[0])
706 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
708 /* send setup message to LCR */
709 memset(&newparam, 0, sizeof(union parameter));
711 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
713 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
714 call->dialque[0] = '\0';
715 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
719 * in case of a bridge, the unsupported message can be forwarded directly
720 * to the remote call.
722 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
726 if (!call->bridge_call) return;
727 CDEBUG(call, NULL, "Sending message due bridging.\n");
728 send_message(message_type, call->bridge_call->ref, param);
732 * send release message to LCR and import bchannel if exported
734 static void send_release_and_import(struct chan_call *call, int cause, int location)
736 union parameter newparam;
738 /* importing channel */
739 if (call->bchannel) {
740 memset(&newparam, 0, sizeof(union parameter));
741 newparam.bchannel.type = BCHANNEL_RELEASE;
742 newparam.bchannel.handle = call->bchannel->handle;
743 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
745 /* sending release */
746 memset(&newparam, 0, sizeof(union parameter));
747 newparam.disconnectinfo.cause = cause;
748 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
749 send_message(MESSAGE_RELEASE, call->ref, &newparam);
753 * check if extension matches and start asterisk
754 * if it can match, proceed
757 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
760 union parameter newparam;
761 char *exten = ast->exten;
765 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
769 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
770 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
774 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
775 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);
779 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
780 /* send setup acknowledge to lcr */
781 memset(&newparam, 0, sizeof(union parameter));
782 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
785 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
790 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
791 /* send setup acknowledge to lcr */
792 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
793 memset(&newparam, 0, sizeof(union parameter));
794 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
798 call->state = CHAN_LCR_STATE_IN_DIALING;
800 /* if match, start pbx */
801 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
802 CDEBUG(call, ast, "Extensions matches.\n");
807 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
813 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
821 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
822 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
824 /* release asterisk */
825 ast->hangupcause = call->cause;
826 /* change to release state */
827 call->state = CHAN_LCR_STATE_RELEASE;
828 ast_hangup(ast); // call will be destroyed here
832 /* send setup to asterisk */
833 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
835 #ifdef LCR_FOR_CALLWEAVER
837 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
840 ret = ast_pbx_start(ast);
842 cause = (ret==-2)?34:27;
845 call->pbx_started = 1;
846 ast_setstate(ast, AST_STATE_RING);
850 * incoming setup from LCR
852 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
854 struct ast_channel *ast;
856 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
858 /* create asterisk channel instrance */
860 #ifdef LCR_FOR_CALLWEAVER
861 ast = ast_channel_alloc(1);
864 #ifdef LCR_FOR_ASTERISK
865 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
870 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
871 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
878 ast->tech_pvt = call;
879 ast->tech = &lcr_tech;
880 ast->fds[0] = call->pipe[0];
882 /* fill setup information */
883 if (param->setup.dialinginfo.id)
884 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
885 if (param->setup.context[0])
886 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
888 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
889 if (param->setup.callerinfo.id[0])
890 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
891 if (param->setup.callerinfo.name[0])
892 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
893 if (param->setup.redirinfo.id[0])
894 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
895 switch (param->setup.callerinfo.present) {
896 case INFO_PRESENT_ALLOWED:
897 ast->cid.cid_pres = AST_PRES_ALLOWED;
899 case INFO_PRESENT_RESTRICTED:
900 ast->cid.cid_pres = AST_PRES_RESTRICTED;
903 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
905 switch (param->setup.callerinfo.ntype) {
906 case INFO_NTYPE_SUBSCRIBER:
907 ast->cid.cid_ton = 4;
909 case INFO_NTYPE_NATIONAL:
910 ast->cid.cid_ton = 2;
912 case INFO_NTYPE_INTERNATIONAL:
913 ast->cid.cid_ton = 1;
916 ast->cid.cid_ton = 0;
918 ast->transfercapability = param->setup.capainfo.bearer_capa;
919 /* enable hdlc if transcap is data */
920 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
922 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
924 /* configure channel */
925 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
926 ast->readformat = ast->rawreadformat = ast->nativeformats;
927 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
929 ast->hangupcause = 0;
932 call->state = CHAN_LCR_STATE_IN_SETUP;
934 if (!call->pbx_started)
935 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
939 * incoming setup acknowledge from LCR
941 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
943 if (!call->ast) return;
945 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
947 /* send pending digits in dialque */
948 if (call->dialque[0])
949 send_dialque_to_lcr(call);
950 /* change to overlap state */
951 call->state = CHAN_LCR_STATE_OUT_DIALING;
955 * incoming proceeding from LCR
957 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
959 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
962 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
963 /* queue event for asterisk */
964 if (call->ast && call->pbx_started) {
968 write(wake_pipe[1], &byte, 1);
970 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
976 * incoming alerting from LCR
978 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
980 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
983 call->state = CHAN_LCR_STATE_OUT_ALERTING;
984 /* queue event to asterisk */
985 if (call->ast && call->pbx_started) {
989 write(wake_pipe[1], &byte, 1);
991 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
996 * incoming connect from LCR
998 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1000 union parameter newparam;
1002 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1005 call->state = CHAN_LCR_STATE_CONNECT;
1006 /* request bchannel */
1007 if (!call->bchannel) {
1008 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1009 memset(&newparam, 0, sizeof(union parameter));
1010 newparam.bchannel.type = BCHANNEL_REQUEST;
1011 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1013 /* copy connectinfo */
1014 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1015 /* queue event to asterisk */
1016 if (call->ast && call->pbx_started) {
1020 write(wake_pipe[1], &byte, 1);
1022 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1027 * incoming disconnect from LCR
1029 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1031 struct ast_channel *ast = call->ast;
1033 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1036 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1038 call->cause = param->disconnectinfo.cause;
1039 call->location = param->disconnectinfo.location;
1040 /* if bridge, forward disconnect and return */
1043 if (call->bridge_call) {
1044 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1045 bridge_message_if_bridged(call, message_type, param);
1049 /* release lcr with same cause */
1050 send_release_and_import(call, call->cause, call->location);
1052 /* change to release state */
1053 call->state = CHAN_LCR_STATE_RELEASE;
1054 /* queue release asterisk */
1056 ast->hangupcause = call->cause;
1057 if (call->pbx_started) {
1061 write(wake_pipe[1], &byte, 1);
1063 strcpy(call->queue_string, "H"); // overwrite other indications
1065 ast_hangup(ast); // call will be destroyed here
1071 * incoming release from LCR
1073 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1075 struct ast_channel *ast = call->ast;
1077 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1081 /* change to release state */
1082 call->state = CHAN_LCR_STATE_RELEASE;
1083 /* copy release info */
1085 call->cause = param->disconnectinfo.cause;
1086 call->location = param->disconnectinfo.location;
1088 /* if we have an asterisk instance, queue hangup, else we are done */
1090 ast->hangupcause = call->cause;
1091 if (call->pbx_started) {
1095 write(wake_pipe[1], &byte, 1);
1097 strcpy(call->queue_string, "H");
1099 ast_hangup(ast); // call will be destroyed here
1108 * incoming information from LCR
1110 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1112 struct ast_channel *ast = call->ast;
1114 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1118 /* pbx not started */
1119 if (!call->pbx_started) {
1120 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1121 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1122 lcr_start_pbx(call, ast, param->information.sending_complete);
1126 /* change dailing state after setup */
1127 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1128 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1129 call->state = CHAN_LCR_STATE_IN_DIALING;
1130 // ast_setstate(ast, AST_STATE_DIALING);
1134 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1138 write(wake_pipe[1], &byte, 1);
1140 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1143 /* use bridge to forware message not supported by asterisk */
1144 if (call->state == CHAN_LCR_STATE_CONNECT) {
1145 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1146 bridge_message_if_bridged(call, message_type, param);
1151 * incoming information from LCR
1153 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1155 union parameter newparam;
1157 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1159 /* request bchannel, if call is resumed and we don't have it */
1160 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1161 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1162 memset(&newparam, 0, sizeof(union parameter));
1163 newparam.bchannel.type = BCHANNEL_REQUEST;
1164 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1167 if (!call->ast) return;
1169 /* use bridge to forware message not supported by asterisk */
1170 bridge_message_if_bridged(call, message_type, param);
1174 * incoming information from LCR
1176 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1178 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1180 if (!call->ast) return;
1182 /* use bridge to forware message not supported by asterisk */
1183 bridge_message_if_bridged(call, message_type, param);
1187 * incoming pattern from LCR
1189 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1191 union parameter newparam;
1193 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1195 if (!call->ast) return;
1197 /* pattern are indicated only once */
1198 if (call->has_pattern)
1200 call->has_pattern = 1;
1202 /* request bchannel */
1203 if (!call->bchannel) {
1204 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1205 memset(&newparam, 0, sizeof(union parameter));
1206 newparam.bchannel.type = BCHANNEL_REQUEST;
1207 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1209 /* queue PROGRESS, because tones are available */
1210 if (call->ast && call->pbx_started) {
1214 write(wake_pipe[1], &byte, 1);
1216 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1221 * got dtmf from bchannel (locked state)
1223 void lcr_in_dtmf(struct chan_call *call, int val)
1225 struct ast_channel *ast = call->ast;
1230 if (!call->pbx_started)
1233 if (!call->dsp_dtmf) {
1234 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1238 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1244 write(wake_pipe[1], &byte, 1);
1246 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1250 * message received from LCR
1252 int receive_message(int message_type, unsigned int ref, union parameter *param)
1254 struct bchannel *bchannel;
1255 struct chan_call *call;
1256 union parameter newparam;
1258 memset(&newparam, 0, sizeof(union parameter));
1260 /* handle bchannel message*/
1261 if (message_type == MESSAGE_BCHANNEL) {
1262 switch(param->bchannel.type) {
1263 case BCHANNEL_ASSIGN:
1264 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1265 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1266 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1269 /* create bchannel */
1270 bchannel = alloc_bchannel(param->bchannel.handle);
1272 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1276 /* configure channel */
1277 bchannel->b_tx_gain = param->bchannel.tx_gain;
1278 bchannel->b_rx_gain = param->bchannel.rx_gain;
1279 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1280 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1281 bchannel->b_bf_len = param->bchannel.crypt_len;
1282 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1284 bchannel->b_txdata = 0;
1285 bchannel->b_tx_dejitter = 1;
1287 /* in case, ref is not set, this bchannel instance must
1288 * be created until it is removed again by LCR */
1290 call = find_call_ref(ref);
1292 bchannel->call = call;
1293 call->bchannel = bchannel;
1295 bchannel_dtmf(bchannel, 1);
1297 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1298 if (call->pipeline[0])
1299 bchannel_pipeline(bchannel, call->pipeline);
1301 bchannel_gain(bchannel, call->rx_gain, 0);
1303 bchannel_gain(bchannel, call->tx_gain, 1);
1304 if (call->bridge_id) {
1305 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1306 bchannel_join(bchannel, call->bridge_id);
1308 /* create only, if call exists, othewhise it bchannel is freed below... */
1309 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1310 bchannel_activate(bchannel, 1);
1313 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1314 newparam.bchannel.handle = param->bchannel.handle;
1315 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1316 /* if call has released before bchannel is assigned */
1318 newparam.bchannel.type = BCHANNEL_RELEASE;
1319 newparam.bchannel.handle = param->bchannel.handle;
1320 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1325 case BCHANNEL_REMOVE:
1326 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1327 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1328 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1331 /* unklink from call and destroy bchannel */
1332 free_bchannel(bchannel);
1335 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1336 newparam.bchannel.handle = param->bchannel.handle;
1337 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1342 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1347 /* handle new ref */
1348 if (message_type == MESSAGE_NEWREF) {
1349 if (param->direction) {
1350 /* new ref from lcr */
1351 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1352 if (!ref || find_call_ref(ref)) {
1353 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1356 /* allocate new call instance */
1357 call = alloc_call();
1359 call->state = CHAN_LCR_STATE_IN_PREPARE;
1362 call->ref_was_assigned = 1;
1363 /* set dtmf (default, use option 'n' to disable */
1365 /* wait for setup (or release from asterisk) */
1367 /* new ref, as requested from this remote application */
1368 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1369 call = find_call_ref(0);
1371 /* send release, if ref does not exist */
1372 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1373 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1378 call->ref_was_assigned = 1;
1379 /* set dtmf (default, use option 'n' to disable */
1381 /* send pending setup info */
1382 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1383 send_setup_to_lcr(call);
1384 /* release if asterisk has signed off */
1385 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1388 send_release_and_import(call, call->cause, call->location);
1390 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1401 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1404 call = find_call_ref(ref);
1406 /* ignore ref that is not used (anymore) */
1407 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1411 /* handle messages */
1412 switch(message_type) {
1414 lcr_in_setup(call, message_type, param);
1417 case MESSAGE_OVERLAP:
1418 lcr_in_overlap(call, message_type, param);
1421 case MESSAGE_PROCEEDING:
1422 lcr_in_proceeding(call, message_type, param);
1425 case MESSAGE_ALERTING:
1426 lcr_in_alerting(call, message_type, param);
1429 case MESSAGE_CONNECT:
1430 lcr_in_connect(call, message_type, param);
1433 case MESSAGE_DISCONNECT:
1434 lcr_in_disconnect(call, message_type, param);
1437 case MESSAGE_RELEASE:
1438 lcr_in_release(call, message_type, param);
1441 case MESSAGE_INFORMATION:
1442 lcr_in_information(call, message_type, param);
1445 case MESSAGE_NOTIFY:
1446 lcr_in_notify(call, message_type, param);
1449 case MESSAGE_FACILITY:
1450 lcr_in_facility(call, message_type, param);
1453 case MESSAGE_PATTERN: // audio available from LCR
1454 if (!call->has_pattern)
1455 lcr_in_pattern(call, message_type, param);
1458 case MESSAGE_NOPATTERN: // audio not available from LCR
1461 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1462 call->audiopath = param->audiopath;
1466 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1473 * release all calls (due to broken socket)
1475 static void release_all_calls(void)
1477 struct chan_call *call;
1482 /* no ast, so we may directly free call */
1484 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1488 /* already in release process */
1489 if (call->state == CHAN_LCR_STATE_RELEASE) {
1493 /* release or queue release */
1495 call->state = CHAN_LCR_STATE_RELEASE;
1496 if (!call->pbx_started) {
1497 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1498 ast_hangup(call->ast); // call will be destroyed here
1501 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1505 write(wake_pipe[1], &byte, 1);
1507 strcpy(call->queue_string, "H");
1511 /* release all bchannels */
1512 while(bchannel_first)
1513 free_bchannel(bchannel_first);
1516 void close_socket(void);
1519 * warning! not thread safe
1520 * returns -1 for socket error, 0 for no work, 1 for work
1522 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1525 struct admin_list *admin;
1526 struct admin_message msg;
1528 if ((what & LCR_FD_READ)) {
1529 /* read from socket */
1530 len = read(lcr_sock, &msg, sizeof(msg));
1532 CERROR(NULL, NULL, "Socket closed.\n");
1534 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1536 release_all_calls();
1537 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1541 if (len != sizeof(msg)) {
1542 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1545 if (msg.message != ADMIN_MESSAGE) {
1546 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1549 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1551 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1556 if ((what & LCR_FD_WRITE)) {
1557 /* write to socket */
1559 socket_fd.when &= ~LCR_FD_WRITE;
1562 admin = admin_first;
1563 len = write(lcr_sock, &admin->msg, sizeof(msg));
1565 CERROR(NULL, NULL, "Socket closed.\n");
1569 if (len != sizeof(msg)) {
1570 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1574 admin_first = admin->next;
1578 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1587 * open and close socket and thread
1589 int open_socket(void)
1592 struct sockaddr_un sock_address;
1593 union parameter param;
1596 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1597 CERROR(NULL, NULL, "Failed to create socket.\n");
1601 /* set socket address and name */
1602 memset(&sock_address, 0, sizeof(sock_address));
1603 sock_address.sun_family = PF_UNIX;
1604 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1606 /* connect socket */
1607 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1610 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1614 /* register socket fd */
1615 memset(&socket_fd, 0, sizeof(socket_fd));
1616 socket_fd.fd = lcr_sock;
1617 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1619 /* enque hello message */
1620 memset(¶m, 0, sizeof(param));
1621 strcpy(param.hello.application, "asterisk");
1622 send_message(MESSAGE_HELLO, 0, ¶m);
1627 void close_socket(void)
1629 struct admin_list *admin, *temp;
1631 unregister_fd(&socket_fd);
1633 /* flush pending messages */
1634 admin = admin_first;
1637 admin = admin->next;
1650 /* sending queue to asterisk */
1651 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1655 read(wake_pipe[0], &byte, 1);
1662 static void handle_queue()
1664 struct chan_call *call;
1665 struct ast_channel *ast;
1666 struct ast_frame fr;
1671 p = call->queue_string;
1675 ast_channel_lock(ast);
1679 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1680 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1683 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1684 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1687 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1688 ast_queue_control(ast, AST_CONTROL_RINGING);
1689 ast_setstate(ast, AST_STATE_RINGING);
1692 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1693 ast_queue_control(ast, AST_CONTROL_ANSWER);
1696 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1697 ast_queue_hangup(ast);
1699 case '1': case '2': case '3': case 'A':
1700 case '4': case '5': case '6': case 'B':
1701 case '7': case '8': case '9': case 'C':
1702 case '*': case '0': case '#': case 'D':
1703 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1704 /* send digit to asterisk */
1705 memset(&fr, 0, sizeof(fr));
1707 #ifdef LCR_FOR_ASTERISK
1708 fr.frametype = AST_FRAME_DTMF_BEGIN;
1711 #ifdef LCR_FOR_CALLWEAVER
1712 fr.frametype = AST_FRAME_DTMF;
1716 fr.delivery = ast_tv(0, 0);
1717 ast_queue_frame(ast, &fr);
1719 #ifdef LCR_FOR_ASTERISK
1720 fr.frametype = AST_FRAME_DTMF_END;
1721 ast_queue_frame(ast, &fr);
1726 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1730 call->queue_string[0] = '\0';
1731 ast_channel_unlock(ast);
1738 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1740 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1741 if (open_socket() < 0)
1742 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1747 void lock_chan(void)
1750 ast_mutex_lock(&chan_lock);
1753 void unlock_chan(void)
1755 ast_mutex_unlock(&chan_lock);
1759 /* chan_lcr thread */
1760 static void *chan_thread(void *arg)
1762 if (pipe(wake_pipe) < 0) {
1763 CERROR(NULL, NULL, "Failed to open pipe.\n");
1766 memset(&wake_fd, 0, sizeof(wake_fd));
1767 wake_fd.fd = wake_pipe[0];
1768 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1770 memset(&socket_retry, 0, sizeof(socket_retry));
1771 add_timer(&socket_retry, handle_retry, NULL, 0);
1773 bchannel_pid = getpid();
1775 /* open socket the first time */
1776 handle_retry(NULL, NULL, 0);
1779 ast_mutex_lock(&chan_lock);
1783 select_main(0, &global_change, lock_chan, unlock_chan);
1788 del_timer(&socket_retry);
1790 unregister_fd(&wake_fd);
1791 close(wake_pipe[0]);
1792 close(wake_pipe[1]);
1794 CERROR(NULL, NULL, "Thread exit.\n");
1796 ast_mutex_unlock(&chan_lock);
1803 * new asterisk instance
1806 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1808 char exten[256], *dial, *interface, *opt;
1809 struct ast_channel *ast;
1810 struct chan_call *call;
1813 ast_mutex_lock(&chan_lock);
1814 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1816 /* if socket is closed */
1818 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1819 ast_mutex_unlock(&chan_lock);
1824 /* create call instance */
1825 call = alloc_call();
1827 /* failed to create instance */
1828 ast_mutex_unlock(&chan_lock);
1833 /* create asterisk channel instrance */
1835 #ifdef LCR_FOR_ASTERISK
1836 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1839 #ifdef LCR_FOR_CALLWEAVER
1840 ast = ast_channel_alloc(1);
1844 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1846 /* failed to create instance */
1847 ast_mutex_unlock(&chan_lock);
1851 ast->tech = &lcr_tech;
1852 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1853 /* configure channel */
1854 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1855 ast->readformat = ast->rawreadformat = ast->nativeformats;
1856 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1858 ast->hangupcause = 0;
1862 ast->tech_pvt = call;
1863 ast->fds[0] = call->pipe[0];
1864 call->pbx_started = 0;
1866 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1869 * Extract interface, dialstring, options from data.
1872 * <interface>/<dialstring>
1873 * <interface>/<dialstring>/options
1875 strncpy(exten, (char *)data, sizeof(exten)-1);
1876 exten[sizeof(exten)-1] = '\0';
1877 if ((dial = strchr(exten, '/'))) {
1880 if ((opt = strchr(dial, '/')))
1889 strncpy(call->interface, interface, sizeof(call->interface)-1);
1890 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1891 apply_opt(call, (char *)opt);
1893 ast_mutex_unlock(&chan_lock);
1899 * call from asterisk
1901 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1903 union parameter newparam;
1904 struct chan_call *call;
1907 ast_mutex_lock(&chan_lock);
1908 call = ast->tech_pvt;
1910 #ifdef LCR_FOR_CALLWEAVER
1912 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1916 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1917 ast_mutex_unlock(&chan_lock);
1922 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1924 /* pbx process is started */
1925 call->pbx_started = 1;
1926 /* send MESSAGE_NEWREF */
1927 memset(&newparam, 0, sizeof(union parameter));
1928 newparam.direction = 0; /* request from app */
1929 send_message(MESSAGE_NEWREF, 0, &newparam);
1931 /* set hdlc if capability requires hdlc */
1932 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1933 || ast->transfercapability == INFO_BC_DATARESTRICTED
1934 || ast->transfercapability == INFO_BC_VIDEO)
1936 /* if hdlc is forced by option, we change transcap to data */
1938 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1939 && ast->transfercapability != INFO_BC_DATARESTRICTED
1940 && ast->transfercapability != INFO_BC_VIDEO)
1941 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1943 call->cid_num[0] = 0;
1944 call->cid_name[0] = 0;
1945 call->cid_rdnis[0] = 0;
1947 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1948 strncpy(call->cid_num, ast->cid.cid_num,
1949 sizeof(call->cid_num)-1);
1951 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1952 strncpy(call->cid_name, ast->cid.cid_name,
1953 sizeof(call->cid_name)-1);
1954 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1955 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1956 sizeof(call->cid_rdnis)-1);
1958 ast_mutex_unlock(&chan_lock);
1963 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1965 static const char* dtmf_tones[] = {
1966 "!941+1336/100,!0/100", /* 0 */
1967 "!697+1209/100,!0/100", /* 1 */
1968 "!697+1336/100,!0/100", /* 2 */
1969 "!697+1477/100,!0/100", /* 3 */
1970 "!770+1209/100,!0/100", /* 4 */
1971 "!770+1336/100,!0/100", /* 5 */
1972 "!770+1477/100,!0/100", /* 6 */
1973 "!852+1209/100,!0/100", /* 7 */
1974 "!852+1336/100,!0/100", /* 8 */
1975 "!852+1477/100,!0/100", /* 9 */
1976 "!697+1633/100,!0/100", /* A */
1977 "!770+1633/100,!0/100", /* B */
1978 "!852+1633/100,!0/100", /* C */
1979 "!941+1633/100,!0/100", /* D */
1980 "!941+1209/100,!0/100", /* * */
1981 "!941+1477/100,!0/100" }; /* # */
1983 if (digit >= '0' && digit <='9')
1984 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1985 else if (digit >= 'A' && digit <= 'D')
1986 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1987 else if (digit == '*')
1988 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1989 else if (digit == '#')
1990 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1993 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1994 "'%c' for '%s'\n", digit, ast->name);
1998 #ifdef LCR_FOR_ASTERISK
1999 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2001 #ifdef LCR_FOR_CALLWEAVER
2002 static int lcr_digit(struct ast_channel *ast, char digit)
2005 struct chan_call *call;
2006 union parameter newparam;
2009 #ifdef LCR_FOR_CALLWEAVER
2010 int inband_dtmf = 0;
2013 /* only pass IA5 number space */
2014 if (digit > 126 || digit < 32)
2018 ast_mutex_lock(&chan_lock);
2019 call = ast->tech_pvt;
2021 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2022 ast_mutex_unlock(&chan_lock);
2027 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2029 /* send information or queue them */
2030 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2031 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2032 memset(&newparam, 0, sizeof(union parameter));
2034 newparam.information.keypad[0] = digit;
2035 newparam.information.keypad[1] = '\0';
2037 newparam.information.id[0] = digit;
2038 newparam.information.id[1] = '\0';
2040 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2043 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2044 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2046 strncat(call->dialque, buf, strlen(call->dialque)-1);
2049 ast_mutex_unlock(&chan_lock);
2052 #ifdef LCR_FOR_ASTERISK
2056 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2058 int inband_dtmf = 0;
2059 struct chan_call *call;
2063 ast_mutex_lock(&chan_lock);
2065 call = ast->tech_pvt;
2069 "Received digit from Asterisk, "
2070 "but no call instance exists.\n");
2071 ast_mutex_unlock(&chan_lock);
2076 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2078 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2082 ast_mutex_unlock(&chan_lock);
2086 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2087 send_digit_to_chan(ast, digit);
2093 static int lcr_answer(struct ast_channel *ast)
2095 union parameter newparam;
2096 struct chan_call *call;
2099 ast_mutex_lock(&chan_lock);
2100 call = ast->tech_pvt;
2102 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2103 ast_mutex_unlock(&chan_lock);
2108 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2110 /* copy connectinfo, if bridged */
2111 if (call->bridge_call)
2112 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2113 /* send connect message to lcr */
2114 if (call->state != CHAN_LCR_STATE_CONNECT) {
2115 memset(&newparam, 0, sizeof(union parameter));
2116 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2117 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2118 call->state = CHAN_LCR_STATE_CONNECT;
2121 /* request bchannel */
2122 if (!call->bchannel) {
2123 CDEBUG(call, ast, "Requesting B-channel.\n");
2124 memset(&newparam, 0, sizeof(union parameter));
2125 newparam.bchannel.type = BCHANNEL_REQUEST;
2126 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2129 // memset(&newparam, 0, sizeof(union parameter));
2130 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2132 ast_mutex_unlock(&chan_lock);
2137 static int lcr_hangup(struct ast_channel *ast)
2139 struct chan_call *call;
2140 pthread_t tid = pthread_self();
2142 if (!pthread_equal(tid, chan_tid)) {
2144 ast_mutex_lock(&chan_lock);
2146 call = ast->tech_pvt;
2148 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2149 if (!pthread_equal(tid, chan_tid)) {
2150 ast_mutex_unlock(&chan_lock);
2156 if (!pthread_equal(tid, chan_tid))
2157 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2159 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2161 /* disconnect asterisk, maybe not required */
2162 ast->tech_pvt = NULL;
2166 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2167 if (ast->hangupcause > 0)
2168 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2170 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2173 if (!pthread_equal(tid, chan_tid)) {
2174 ast_mutex_unlock(&chan_lock);
2179 /* ref is not set, due to prepare setup or release */
2180 if (call->state == CHAN_LCR_STATE_RELEASE) {
2181 /* we get the response to our release */
2182 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2185 /* during prepare, we change to release state */
2186 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2187 call->state = CHAN_LCR_STATE_RELEASE;
2191 if (!pthread_equal(tid, chan_tid)) {
2192 ast_mutex_unlock(&chan_lock);
2198 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2200 struct chan_call *call;
2203 CDEBUG(NULL, ast, "No subclass\n");
2204 if (!(f->subclass & ast->nativeformats))
2205 CDEBUG(NULL, ast, "Unexpected format.\n");
2208 ast_mutex_lock(&chan_lock);
2209 call = ast->tech_pvt;
2211 ast_mutex_unlock(&chan_lock);
2215 if (call->bchannel && f->samples)
2216 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2217 ast_mutex_unlock(&chan_lock);
2223 static struct ast_frame *lcr_read(struct ast_channel *ast)
2225 struct chan_call *call;
2229 ast_mutex_lock(&chan_lock);
2230 call = ast->tech_pvt;
2232 ast_mutex_unlock(&chan_lock);
2236 if (call->pipe[0] > -1) {
2237 if (call->rebuffer && !call->hdlc) {
2238 /* Make sure we have a complete 20ms (160byte) frame */
2239 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2241 call->framepos += len;
2244 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2246 if (len < 0 && errno == EAGAIN) {
2247 ast_mutex_unlock(&chan_lock);
2250 #ifdef LCR_FOR_ASTERISK
2251 return &ast_null_frame;
2254 #ifdef LCR_FOR_CALLWEAVER
2260 close(call->pipe[0]);
2263 ast_mutex_unlock(&chan_lock);
2266 } else if (call->rebuffer && call->framepos < 160) {
2267 /* Not a complete frame, so we send a null-frame */
2268 ast_mutex_unlock(&chan_lock);
2270 return &ast_null_frame;
2274 call->read_fr.frametype = AST_FRAME_VOICE;
2275 call->read_fr.subclass = ast->nativeformats;
2276 if (call->rebuffer) {
2277 call->read_fr.datalen = call->framepos;
2278 call->read_fr.samples = call->framepos;
2281 call->read_fr.datalen = len;
2282 call->read_fr.samples = len;
2284 call->read_fr.delivery = ast_tv(0,0);
2285 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2286 ast_mutex_unlock(&chan_lock);
2289 return &call->read_fr;
2292 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2294 union parameter newparam;
2296 struct chan_call *call;
2297 const struct tone_zone_sound *ts = NULL;
2300 ast_mutex_lock(&chan_lock);
2301 call = ast->tech_pvt;
2303 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2304 ast_mutex_unlock(&chan_lock);
2310 case AST_CONTROL_BUSY:
2311 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2312 ast_setstate(ast, AST_STATE_BUSY);
2313 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2314 /* send message to lcr */
2315 memset(&newparam, 0, sizeof(union parameter));
2316 newparam.disconnectinfo.cause = 17;
2317 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2318 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2320 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2322 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2323 ts = ast_get_indication_tone(ast->zone, "busy");
2326 case AST_CONTROL_CONGESTION:
2327 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2328 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2329 /* send message to lcr */
2330 memset(&newparam, 0, sizeof(union parameter));
2331 newparam.disconnectinfo.cause = ast->hangupcause;
2332 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2333 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2335 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2337 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2338 ts = ast_get_indication_tone(ast->zone, "congestion");
2341 case AST_CONTROL_PROCEEDING:
2342 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2343 if (call->state == CHAN_LCR_STATE_IN_SETUP
2344 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2345 /* send message to lcr */
2346 memset(&newparam, 0, sizeof(union parameter));
2347 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2349 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2352 case AST_CONTROL_RINGING:
2353 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2354 ast_setstate(ast, AST_STATE_RING);
2355 if (call->state == CHAN_LCR_STATE_IN_SETUP
2356 || call->state == CHAN_LCR_STATE_IN_DIALING
2357 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2358 /* send message to lcr */
2359 memset(&newparam, 0, sizeof(union parameter));
2360 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2362 call->state = CHAN_LCR_STATE_IN_ALERTING;
2364 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2365 ts = ast_get_indication_tone(ast->zone, "ring");
2368 case AST_CONTROL_PROGRESS:
2369 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2370 /* request bchannel */
2371 if (!call->bchannel) {
2372 CDEBUG(call, ast, "Requesting B-channel.\n");
2373 memset(&newparam, 0, sizeof(union parameter));
2374 newparam.bchannel.type = BCHANNEL_REQUEST;
2375 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2379 CDEBUG(call, ast, "Received indicate -1.\n");
2380 ast_playtones_stop(ast);
2384 case AST_CONTROL_VIDUPDATE:
2385 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2388 case AST_CONTROL_HOLD:
2389 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2390 /* send message to lcr */
2391 memset(&newparam, 0, sizeof(union parameter));
2392 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2393 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2395 /*start music onhold*/
2396 #ifdef LCR_FOR_ASTERISK
2397 ast_moh_start(ast,data,ast->musicclass);
2400 #ifdef LCR_FOR_CALLWEAVER
2401 ast_moh_start(ast, NULL);
2406 case AST_CONTROL_UNHOLD:
2407 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2408 /* send message to lcr */
2409 memset(&newparam, 0, sizeof(union parameter));
2410 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2411 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2417 #ifdef AST_CONTROL_SRCUPDATE
2418 case AST_CONTROL_SRCUPDATE:
2422 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2425 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2430 if (ts && ts->data[0]) {
2431 ast_playtones_start(ast, 0, ts->data, 1);
2435 ast_mutex_unlock(&chan_lock);
2443 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2445 struct chan_call *call;
2452 ast_mutex_lock(&chan_lock);
2453 call = ast->tech_pvt;
2455 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2456 ast_mutex_unlock(&chan_lock);
2461 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2463 ast_mutex_unlock(&chan_lock);
2469 * send_text asterisk
2471 static int lcr_send_text(struct ast_channel *ast, const char *text)
2473 struct chan_call *call;
2474 union parameter newparam;
2477 ast_mutex_lock(&chan_lock);
2478 call = ast->tech_pvt;
2480 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2481 ast_mutex_unlock(&chan_lock);
2486 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2487 memset(&newparam, 0, sizeof(union parameter));
2488 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2489 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2490 ast_mutex_unlock(&chan_lock);
2498 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2499 struct ast_channel *ast2, int flags,
2500 struct ast_frame **fo,
2501 struct ast_channel **rc, int timeoutms)
2504 struct chan_call *call1, *call2;
2505 struct ast_channel *carr[2], *who;
2507 struct ast_frame *f;
2510 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2515 /* join via dsp (if the channels are currently open) */
2517 ast_mutex_lock(&chan_lock);
2518 call1 = ast1->tech_pvt;
2519 call2 = ast2->tech_pvt;
2520 if (!call1 || !call2) {
2521 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2522 ast_mutex_unlock(&chan_lock);
2524 return AST_BRIDGE_COMPLETE;
2527 /* join, if both call instances uses dsp
2528 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2530 if (!call1->nodsp && !call2->nodsp) {
2531 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2533 /* get bridge id and join */
2534 bridge_id = new_bridge_id();
2536 call1->bridge_id = bridge_id;
2537 if (call1->bchannel)
2538 bchannel_join(call1->bchannel, bridge_id);
2540 call2->bridge_id = bridge_id;
2541 if (call2->bchannel)
2542 bchannel_join(call2->bchannel, bridge_id);
2544 if (call1->nodsp && call2->nodsp)
2545 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2547 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2548 call1->bridge_call = call2;
2549 call2->bridge_call = call1;
2551 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2552 || call1->state == CHAN_LCR_STATE_IN_DIALING
2553 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2554 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2555 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2558 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2559 || call2->state == CHAN_LCR_STATE_IN_DIALING
2560 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2561 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2562 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2566 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2567 so let's do it for them. Hmpf.
2570 if (call1->on_hold) {
2571 union parameter newparam;
2573 memset(&newparam, 0, sizeof(union parameter));
2574 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2575 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2580 if (call2->on_hold) {
2581 union parameter newparam;
2583 memset(&newparam, 0, sizeof(union parameter));
2584 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2585 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2590 ast_mutex_unlock(&chan_lock);
2595 who = ast_waitfor_n(carr, 2, &to);
2598 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2603 if (!f || f->frametype == AST_FRAME_CONTROL) {
2605 CDEBUG(NULL, NULL, "Got hangup.\n");
2607 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2614 if ( f->frametype == AST_FRAME_DTMF ) {
2615 CDEBUG(NULL, NULL, "Got DTMF.\n");
2631 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2633 /* split channels */
2635 ast_mutex_lock(&chan_lock);
2636 call1 = ast1->tech_pvt;
2637 call2 = ast2->tech_pvt;
2638 if (call1 && call1->bridge_id) {
2639 call1->bridge_id = 0;
2640 if (call1->bchannel)
2641 bchannel_join(call1->bchannel, 0);
2642 if (call1->bridge_call)
2643 call1->bridge_call->bridge_call = NULL;
2645 if (call2 && call1->bridge_id) {
2646 call2->bridge_id = 0;
2647 if (call2->bchannel)
2648 bchannel_join(call2->bchannel, 0);
2649 if (call2->bridge_call)
2650 call2->bridge_call->bridge_call = NULL;
2652 call1->bridge_call = NULL;
2653 call2->bridge_call = NULL;
2655 ast_mutex_unlock(&chan_lock);
2657 return AST_BRIDGE_COMPLETE;
2659 static struct ast_channel_tech lcr_tech = {
2661 .description = "Channel driver for connecting to Linux-Call-Router",
2662 .capabilities = AST_FORMAT_ALAW,
2663 .requester = lcr_request,
2665 #ifdef LCR_FOR_ASTERISK
2666 .send_digit_begin = lcr_digit_begin,
2667 .send_digit_end = lcr_digit_end,
2670 #ifdef LCR_FOR_CALLWEAVER
2671 .send_digit = lcr_digit,
2675 .bridge = lcr_bridge,
2676 .hangup = lcr_hangup,
2677 .answer = lcr_answer,
2680 .indicate = lcr_indicate,
2682 .send_text = lcr_send_text,
2691 static int lcr_show_lcr (int fd, int argc, char *argv[])
2696 static int lcr_show_calls (int fd, int argc, char *argv[])
2701 static int lcr_reload_routing (int fd, int argc, char *argv[])
2706 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2711 static int lcr_port_block (int fd, int argc, char *argv[])
2716 static int lcr_port_unblock (int fd, int argc, char *argv[])
2721 static int lcr_port_unload (int fd, int argc, char *argv[])
2726 static struct ast_cli_entry cli_show_lcr =
2727 { {"lcr", "show", "lcr", NULL},
2729 "Shows current states of LCR core",
2730 "Usage: lcr show lcr\n",
2733 static struct ast_cli_entry cli_show_calls =
2734 { {"lcr", "show", "calls", NULL},
2736 "Shows current calls made by LCR and Asterisk",
2737 "Usage: lcr show calls\n",
2740 static struct ast_cli_entry cli_reload_routing =
2741 { {"lcr", "reload", "routing", NULL},
2743 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2744 "Usage: lcr reload routing\n",
2747 static struct ast_cli_entry cli_reload_interfaces =
2748 { {"lcr", "reload", "interfaces", NULL},
2749 lcr_reload_interfaces,
2750 "Reloads interfaces conf of LCR",
2751 "Usage: lcr reload interfaces\n",
2754 static struct ast_cli_entry cli_port_block =
2755 { {"lcr", "port", "block", NULL},
2757 "Blocks LCR port for further calls",
2758 "Usage: lcr port block \"<port>\"\n",
2761 static struct ast_cli_entry cli_port_unblock =
2762 { {"lcr", "port", "unblock", NULL},
2764 "Unblocks or loads LCR port, port is opened my mISDN",
2765 "Usage: lcr port unblock \"<port>\"\n",
2768 static struct ast_cli_entry cli_port_unload =
2769 { {"lcr", "port", "unload", NULL},
2771 "Unloads LCR port, port is closes by mISDN",
2772 "Usage: lcr port unload \"<port>\"\n",
2777 #ifdef LCR_FOR_ASTERISK
2778 static int lcr_config_exec(struct ast_channel *ast, void *data)
2781 #ifdef LCR_FOR_CALLWEAVER
2782 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2785 struct chan_call *call;
2788 ast_mutex_lock(&chan_lock);
2790 #ifdef LCR_FOR_ASTERISK
2791 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2794 #ifdef LCR_FOR_CALLWEAVER
2795 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2801 if (call->ast == ast)
2807 #ifdef LCR_FOR_ASTERISK
2808 apply_opt(call, (char *)data);
2811 #ifdef LCR_FOR_CALLWEAVER
2812 apply_opt(call, (char *)argv[0]);
2816 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2818 ast_mutex_unlock(&chan_lock);
2824 * module loading and destruction
2826 int load_module(void)
2829 char options_error[256];
2831 for (i = 0; i < 256; i++) {
2832 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2833 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2836 if (read_options(options_error) == 0) {
2837 CERROR(NULL, NULL, "%s", options_error);
2839 #ifdef LCR_FOR_ASTERISK
2840 return AST_MODULE_LOAD_DECLINE;
2843 #ifdef LCR_FOR_CALLWEAVER
2849 ast_mutex_init(&chan_lock);
2850 ast_mutex_init(&log_lock);
2852 if (bchannel_initialize()) {
2853 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2856 #ifdef LCR_FOR_ASTERISK
2857 return AST_MODULE_LOAD_DECLINE;
2860 #ifdef LCR_FOR_CALLWEAVER
2866 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2867 if (ast_channel_register(&lcr_tech)) {
2868 CERROR(NULL, NULL, "Unable to register channel class\n");
2869 bchannel_deinitialize();
2872 #ifdef LCR_FOR_ASTERISK
2873 return AST_MODULE_LOAD_DECLINE;
2876 #ifdef LCR_FOR_CALLWEAVER
2881 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2883 #ifdef LCR_FOR_ASTERISK
2884 "lcr_config(<opt><optarg>:<opt>:...)\n"
2887 #ifdef LCR_FOR_CALLWEAVER
2888 "lcr_config(<opt><optarg>:<opt>:...)\n",
2891 "Sets LCR opts. and optargs\n"
2893 "The available options are:\n"
2894 " d - Send display text on called phone, text is the optarg.\n"
2895 " n - Don't detect dtmf tones on called channel.\n"
2896 " h - Force data call (HDLC).\n"
2897 " t - Disable mISDN_dsp features (required for fax application).\n"
2898 " q - Add queue to make fax stream seamless (required for fax app).\n"
2899 " Use queue size in miliseconds for optarg. (try 250)\n"
2900 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2901 " Use time to detect for optarg.\n"
2902 " c - Make crypted outgoing call, optarg is keyindex.\n"
2903 " e - Perform echo cancelation on this channel.\n"
2904 " Takes mISDN pipeline option as optarg.\n"
2905 " s - Send Non Inband DTMF as inband.\n"
2906 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2907 " vr - rxgain control\n"
2908 " vt - txgain control\n"
2909 " Volume changes at factor 2 ^ optarg.\n"
2910 " k - use keypad to dial this call.\n"
2912 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
2913 " -> use 16 for fax (3.1k audio)\n"
2915 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
2916 "options: \"n:t:q250\" for seamless audio transmission.\n"
2921 ast_cli_register(&cli_show_lcr);
2922 ast_cli_register(&cli_show_calls);
2923 ast_cli_register(&cli_reload_routing);
2924 ast_cli_register(&cli_reload_interfaces);
2925 ast_cli_register(&cli_port_block);
2926 ast_cli_register(&cli_port_unblock);
2927 ast_cli_register(&cli_port_unload);
2931 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
2932 /* failed to create thread */
2933 bchannel_deinitialize();
2935 ast_channel_unregister(&lcr_tech);
2937 #ifdef LCR_FOR_ASTERISK
2938 return AST_MODULE_LOAD_DECLINE;
2941 #ifdef LCR_FOR_CALLWEAVER
2949 int unload_module(void)
2951 /* First, take us out of the channel loop */
2952 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2955 pthread_join(chan_tid, NULL);
2957 ast_channel_unregister(&lcr_tech);
2959 ast_unregister_application("lcr_config");
2962 if (mISDN_created) {
2963 bchannel_deinitialize();
2967 if (lcr_sock >= 0) {
2975 int reload_module(void)
2981 #ifdef LCR_FOR_ASTERISK
2982 #define AST_MODULE "chan_lcr"
2985 #ifdef LCR_FOR_CALLWEAVER
2991 ast_mutex_lock(&usecnt_lock);
2993 ast_mutex_unlock(&usecnt_lock);
2999 #ifdef LCR_FOR_ASTERISK
3000 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3001 .load = load_module,
3002 .unload = unload_module,
3003 .reload = reload_module,
3007 #ifdef LCR_FOR_CALLWEAVER
3008 char *description(void)