1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
139 #include <asterisk/module.h>
140 #include <asterisk/channel.h>
141 #include <asterisk/config.h>
142 #include <asterisk/logger.h>
143 #include <asterisk/pbx.h>
144 #include <asterisk/options.h>
145 #include <asterisk/io.h>
146 #include <asterisk/frame.h>
147 #include <asterisk/translate.h>
148 #include <asterisk/cli.h>
149 #include <asterisk/musiconhold.h>
150 #include <asterisk/dsp.h>
151 #include <asterisk/translate.h>
152 #include <asterisk/file.h>
153 #ifdef LCR_FOR_ASTERISK
154 #include <asterisk/callerid.h>
156 #ifdef LCR_FOR_CALLWEAVER
157 #include <asterisk/phone_no_utils.h>
160 #include <asterisk/indications.h>
161 #include <asterisk/app.h>
162 #include <asterisk/features.h>
163 #include <asterisk/sched.h>
165 #include "extension.h"
167 #include "callerid.h"
168 #include "lcrsocket.h"
171 #include "bchannel.h"
173 #include "chan_lcr.h"
175 CHAN_LCR_STATE // state description structure
176 MESSAGES // message text
178 #ifdef LCR_FOR_CALLWEAVER
179 AST_MUTEX_DEFINE_STATIC(rand_lock);
182 unsigned char flip_bits[256];
184 #ifdef LCR_FOR_CALLWEAVER
185 static struct ast_frame nullframe = { AST_FRAME_NULL, };
191 char lcr_type[]="lcr";
193 #ifdef LCR_FOR_CALLWEAVER
194 static ast_mutex_t usecnt_lock;
196 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
200 ast_mutex_t chan_lock; /* global lock */
201 ast_mutex_t log_lock; /* logging log */
203 * used to indicate change in file descriptors, so select function's result may
206 int global_change = 0;
209 struct lcr_fd wake_fd;
213 int glob_channel = 0;
216 struct lcr_fd socket_fd;
217 struct lcr_timer socket_retry;
220 struct admin_list *next;
221 struct admin_message msg;
222 } *admin_first = NULL;
224 static struct ast_channel_tech lcr_tech;
229 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
232 char call_text[128] = "NULL";
233 char ast_text[128] = "NULL";
236 ast_mutex_lock(&log_lock);
239 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
240 buffer[sizeof(buffer)-1]=0;
244 sprintf(call_text, "%d", call->ref);
246 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
247 ast_text[sizeof(ast_text)-1] = '\0';
249 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
250 printf("[call=%s ast=%s] %s", call_text, ast_text, buffer);
252 ast_mutex_unlock(&log_lock);
256 * channel and call instances
258 struct chan_call *call_first;
262 * special case: 0: find new ref, that has not been assigned a ref yet
265 struct chan_call *find_call_ref(unsigned int ref)
267 struct chan_call *call = call_first;
268 int assigned = (ref > 0);
271 if (call->ref == ref && call->ref_was_assigned == assigned)
278 void free_call(struct chan_call *call)
280 struct chan_call **temp = &call_first;
284 *temp = (*temp)->next;
285 if (call->pipe[0] > -1)
286 close(call->pipe[0]);
287 if (call->pipe[1] > -1)
288 close(call->pipe[1]);
289 if (call->bchannel) {
290 if (call->bchannel->call != call)
291 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
292 call->bchannel->call = NULL;
294 if (call->bridge_call) {
295 if (call->bridge_call->bridge_call != call)
296 CERROR(call, NULL, "Linked call structure has no link to us.\n");
297 call->bridge_call->bridge_call = NULL;
300 ast_translator_free_path(call->trans);
302 ast_dsp_free(call->dsp);
303 CDEBUG(call, NULL, "Call instance freed.\n");
308 temp = &((*temp)->next);
310 CERROR(call, NULL, "Call instance not found in list.\n");
313 struct chan_call *alloc_call(void)
315 struct chan_call **callp = &call_first;
318 callp = &((*callp)->next);
320 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
322 memset(*callp, 0, sizeof(struct chan_call));
323 if (pipe((*callp)->pipe) < 0) {
324 CERROR(*callp, NULL, "Failed to create pipe.\n");
328 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
329 CDEBUG(*callp, NULL, "Call instance allocated.\n");
333 unsigned short new_bridge_id(void)
335 struct chan_call *call;
336 unsigned short id = 1;
338 /* search for lowest bridge id that is not in use and not 0 */
342 if (call->bridge_id == id)
350 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
355 * enque message to LCR
357 int send_message(int message_type, unsigned int ref, union parameter *param)
359 struct admin_list *admin, **adminp;
362 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
365 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
367 adminp = &admin_first;
369 adminp = &((*adminp)->next);
370 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
372 CERROR(NULL, NULL, "No memory for message to LCR.\n");
377 admin->msg.message = ADMIN_MESSAGE;
378 admin->msg.u.msg.type = message_type;
379 admin->msg.u.msg.ref = ref;
380 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
381 socket_fd.when |= LCR_FD_WRITE;
385 write(wake_pipe[1], &byte, 1);
392 * apply options (in locked state)
394 void apply_opt(struct chan_call *call, char *data)
396 union parameter newparam;
397 char string[1024], *p = string, *opt, *key;
403 strncpy(string, data, sizeof(string)-1);
404 string[sizeof(string)-1] = '\0';
407 while((opt = strsep(&p, ":"))) {
410 if (opt[1] == '\0') {
411 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
414 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
415 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
416 strncpy(call->display, opt+1, sizeof(call->display)-1);
418 memset(&newparam, 0, sizeof(union parameter));
419 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
420 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
424 if (opt[1] != '\0') {
425 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
428 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
429 if (call->dsp_dtmf) {
432 bchannel_dtmf(call->bchannel, 0);
436 if (opt[1] == '\0') {
437 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
441 /* check for 0xXXXX... type of key */
442 if (!!strncmp((char *)key, "0x", 2)) {
443 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
447 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
448 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
453 if (*key>='0' && *key<='9')
454 call->bf_key[i] = (*key-'0') << 8;
455 else if (*key>='a' && *key<='f')
456 call->bf_key[i] = (*key-'a'+10) << 8;
457 else if (*key>='A' && *key<='F')
458 call->bf_key[i] = (*key-'A'+10) << 8;
462 if (*key>='0' && *key<='9')
463 call->bf_key[i] += (*key - '0');
464 else if (*key>='a' && *key<='f')
465 call->bf_key[i] += (*key - 'a' + 10);
466 else if (*key>='A' && *key<='F')
467 call->bf_key[i] += (*key - 'A' + 10);
474 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
478 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
480 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
483 if (opt[1] != '\0') {
484 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
487 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
492 if (opt[1] != '\0') {
493 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
496 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
501 if (opt[1] == '\0') {
502 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
505 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
506 call->nodsp_queue = atoi(opt+1);
509 if (opt[1] == '\0') {
510 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
513 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
514 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
516 bchannel_pipeline(call->bchannel, call->pipeline);
519 if (opt[1] == '\0') {
520 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
523 call->faxdetect=atoi(opt+1);
525 call->dsp=ast_dsp_new();
527 #ifdef LCR_FOR_CALLWEAVER
528 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
530 #ifdef LCR_FOR_ASTERISK
531 #ifdef DSP_FEATURE_DTMF_DETECT
532 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
534 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
539 #ifdef LCR_FOR_CALLWEAVER
540 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
542 #ifdef LCR_FOR_ASTERISK
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
546 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
549 if (opt[1] != '\0') {
550 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
553 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
558 if (opt[1] != '\0') {
559 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
562 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
563 call->inband_dtmf = 1;
566 if (opt[1] != 'r' && opt[1] != 't') {
567 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
571 if (gain < -8 || gain >8) {
572 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
575 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
577 call->rx_gain = gain;
579 bchannel_gain(call->bchannel, call->rx_gain, 0);
581 call->tx_gain = gain;
583 bchannel_gain(call->bchannel, call->tx_gain, 1);
587 if (opt[1] != '\0') {
588 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
591 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
596 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
600 /* re-open, if bchannel is created */
601 if (call->bchannel && call->bchannel->b_sock > -1) {
602 bchannel_destroy(call->bchannel);
603 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
604 bchannel_activate(call->bchannel, 1);
609 * send setup info to LCR
610 * this function is called, when asterisk call is received and ref is received
612 static void send_setup_to_lcr(struct chan_call *call)
614 union parameter newparam;
615 struct ast_channel *ast = call->ast;
618 if (!call->ast || !call->ref)
621 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
623 /* send setup message to LCR */
624 memset(&newparam, 0, sizeof(union parameter));
625 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
626 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
628 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
630 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
631 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
632 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
633 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
634 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
635 call->display[0] = '\0';
636 if (call->cid_num[0])
637 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
638 if (call->cid_name[0])
639 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
640 if (call->cid_rdnis[0]) {
641 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
642 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
643 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
645 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
646 case AST_PRES_RESTRICTED:
647 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
649 case AST_PRES_UNAVAILABLE:
650 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
652 case AST_PRES_ALLOWED:
654 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
656 switch(ast->cid.cid_ton) {
658 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
661 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
664 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
667 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
669 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
670 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
672 // ast->transfercapability = atoi(tmp);
673 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
674 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
676 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
678 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
679 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
681 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
682 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
683 send_message(MESSAGE_SETUP, call->ref, &newparam);
685 /* change to outgoing setup state */
686 call->state = CHAN_LCR_STATE_OUT_SETUP;
690 * send dialing info to LCR
691 * this function is called, when setup acknowledge is received and dialing
694 static void send_dialque_to_lcr(struct chan_call *call)
696 union parameter newparam;
698 if (!call->ast || !call->ref || !call->dialque[0])
701 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
703 /* send setup message to LCR */
704 memset(&newparam, 0, sizeof(union parameter));
706 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
708 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
709 call->dialque[0] = '\0';
710 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
714 * in case of a bridge, the unsupported message can be forwarded directly
715 * to the remote call.
717 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
721 if (!call->bridge_call) return;
722 CDEBUG(call, NULL, "Sending message due bridging.\n");
723 send_message(message_type, call->bridge_call->ref, param);
727 * send release message to LCR and import bchannel if exported
729 static void send_release_and_import(struct chan_call *call, int cause, int location)
731 union parameter newparam;
733 /* importing channel */
734 if (call->bchannel) {
735 memset(&newparam, 0, sizeof(union parameter));
736 newparam.bchannel.type = BCHANNEL_RELEASE;
737 newparam.bchannel.handle = call->bchannel->handle;
738 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
740 /* sending release */
741 memset(&newparam, 0, sizeof(union parameter));
742 newparam.disconnectinfo.cause = cause;
743 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
744 send_message(MESSAGE_RELEASE, call->ref, &newparam);
748 * check if extension matches and start asterisk
749 * if it can match, proceed
752 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
755 union parameter newparam;
756 char *exten = ast->exten;
760 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
764 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
765 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
769 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
770 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);
774 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
775 /* send setup acknowledge to lcr */
776 memset(&newparam, 0, sizeof(union parameter));
777 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
780 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
785 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
786 /* send setup acknowledge to lcr */
787 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
788 memset(&newparam, 0, sizeof(union parameter));
789 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
793 call->state = CHAN_LCR_STATE_IN_DIALING;
795 /* if match, start pbx */
796 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
797 CDEBUG(call, ast, "Extensions matches.\n");
802 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
808 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
816 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
817 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
819 /* release asterisk */
820 ast->hangupcause = call->cause;
821 /* change to release state */
822 call->state = CHAN_LCR_STATE_RELEASE;
823 ast_hangup(ast); // call will be destroyed here
827 /* send setup to asterisk */
828 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
830 #ifdef LCR_FOR_CALLWEAVER
832 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
835 ret = ast_pbx_start(ast);
837 cause = (ret==-2)?34:27;
840 call->pbx_started = 1;
841 ast_setstate(ast, AST_STATE_RING);
845 * incoming setup from LCR
847 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
849 struct ast_channel *ast;
851 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
853 /* create asterisk channel instrance */
855 #ifdef LCR_FOR_CALLWEAVER
856 ast = ast_channel_alloc(1);
859 #ifdef LCR_FOR_ASTERISK
860 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
865 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
866 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
873 ast->tech_pvt = call;
874 ast->tech = &lcr_tech;
875 ast->fds[0] = call->pipe[0];
877 /* fill setup information */
878 if (param->setup.dialinginfo.id)
879 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
880 if (param->setup.context[0])
881 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
883 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
884 if (param->setup.callerinfo.id[0])
885 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
886 if (param->setup.callerinfo.name[0])
887 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
888 if (param->setup.redirinfo.id[0])
889 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
890 switch (param->setup.callerinfo.present) {
891 case INFO_PRESENT_ALLOWED:
892 ast->cid.cid_pres = AST_PRES_ALLOWED;
894 case INFO_PRESENT_RESTRICTED:
895 ast->cid.cid_pres = AST_PRES_RESTRICTED;
898 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
900 switch (param->setup.callerinfo.ntype) {
901 case INFO_NTYPE_SUBSCRIBER:
902 ast->cid.cid_ton = 4;
904 case INFO_NTYPE_NATIONAL:
905 ast->cid.cid_ton = 2;
907 case INFO_NTYPE_INTERNATIONAL:
908 ast->cid.cid_ton = 1;
911 ast->cid.cid_ton = 0;
913 ast->transfercapability = param->setup.capainfo.bearer_capa;
914 /* enable hdlc if transcap is data */
915 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
917 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
919 /* configure channel */
920 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
921 ast->readformat = ast->rawreadformat = ast->nativeformats;
922 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
924 ast->hangupcause = 0;
927 call->state = CHAN_LCR_STATE_IN_SETUP;
929 if (!call->pbx_started)
930 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
934 * incoming setup acknowledge from LCR
936 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
938 if (!call->ast) return;
940 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
942 /* send pending digits in dialque */
943 if (call->dialque[0])
944 send_dialque_to_lcr(call);
945 /* change to overlap state */
946 call->state = CHAN_LCR_STATE_OUT_DIALING;
950 * incoming proceeding from LCR
952 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
954 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
957 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
958 /* queue event for asterisk */
959 if (call->ast && call->pbx_started) {
963 write(wake_pipe[1], &byte, 1);
965 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
971 * incoming alerting from LCR
973 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
975 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
978 call->state = CHAN_LCR_STATE_OUT_ALERTING;
979 /* queue event to asterisk */
980 if (call->ast && call->pbx_started) {
984 write(wake_pipe[1], &byte, 1);
986 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
991 * incoming connect from LCR
993 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
995 union parameter newparam;
997 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1000 call->state = CHAN_LCR_STATE_CONNECT;
1001 /* request bchannel */
1002 if (!call->bchannel) {
1003 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1004 memset(&newparam, 0, sizeof(union parameter));
1005 newparam.bchannel.type = BCHANNEL_REQUEST;
1006 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1008 /* copy connectinfo */
1009 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1010 /* queue event to asterisk */
1011 if (call->ast && call->pbx_started) {
1015 write(wake_pipe[1], &byte, 1);
1017 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1022 * incoming disconnect from LCR
1024 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1026 struct ast_channel *ast = call->ast;
1028 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1031 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1033 call->cause = param->disconnectinfo.cause;
1034 call->location = param->disconnectinfo.location;
1035 /* if bridge, forward disconnect and return */
1038 if (call->bridge_call) {
1039 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1040 bridge_message_if_bridged(call, message_type, param);
1044 /* release lcr with same cause */
1045 send_release_and_import(call, call->cause, call->location);
1047 /* change to release state */
1048 call->state = CHAN_LCR_STATE_RELEASE;
1049 /* queue release asterisk */
1051 ast->hangupcause = call->cause;
1052 if (call->pbx_started) {
1056 write(wake_pipe[1], &byte, 1);
1058 strcpy(call->queue_string, "H"); // overwrite other indications
1060 ast_hangup(ast); // call will be destroyed here
1066 * incoming release from LCR
1068 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1070 struct ast_channel *ast = call->ast;
1072 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1076 /* change to release state */
1077 call->state = CHAN_LCR_STATE_RELEASE;
1078 /* copy release info */
1080 call->cause = param->disconnectinfo.cause;
1081 call->location = param->disconnectinfo.location;
1083 /* if we have an asterisk instance, queue hangup, else we are done */
1085 ast->hangupcause = call->cause;
1086 if (call->pbx_started) {
1090 write(wake_pipe[1], &byte, 1);
1092 strcpy(call->queue_string, "H");
1094 ast_hangup(ast); // call will be destroyed here
1103 * incoming information from LCR
1105 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1107 struct ast_channel *ast = call->ast;
1109 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1113 /* pbx not started */
1114 if (!call->pbx_started) {
1115 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1116 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1117 lcr_start_pbx(call, ast, param->information.sending_complete);
1121 /* change dailing state after setup */
1122 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1123 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1124 call->state = CHAN_LCR_STATE_IN_DIALING;
1125 // ast_setstate(ast, AST_STATE_DIALING);
1129 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1133 write(wake_pipe[1], &byte, 1);
1135 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1138 /* use bridge to forware message not supported by asterisk */
1139 if (call->state == CHAN_LCR_STATE_CONNECT) {
1140 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1141 bridge_message_if_bridged(call, message_type, param);
1146 * incoming information from LCR
1148 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1150 union parameter newparam;
1152 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1154 /* request bchannel, if call is resumed and we don't have it */
1155 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1156 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1157 memset(&newparam, 0, sizeof(union parameter));
1158 newparam.bchannel.type = BCHANNEL_REQUEST;
1159 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1162 if (!call->ast) return;
1164 /* use bridge to forware message not supported by asterisk */
1165 bridge_message_if_bridged(call, message_type, param);
1169 * incoming information from LCR
1171 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1173 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1175 if (!call->ast) return;
1177 /* use bridge to forware message not supported by asterisk */
1178 bridge_message_if_bridged(call, message_type, param);
1182 * incoming pattern from LCR
1184 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1186 union parameter newparam;
1188 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1190 if (!call->ast) return;
1192 /* pattern are indicated only once */
1193 if (call->has_pattern)
1195 call->has_pattern = 1;
1197 /* request bchannel */
1198 if (!call->bchannel) {
1199 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1200 memset(&newparam, 0, sizeof(union parameter));
1201 newparam.bchannel.type = BCHANNEL_REQUEST;
1202 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1204 /* queue PROGRESS, because tones are available */
1205 if (call->ast && call->pbx_started) {
1209 write(wake_pipe[1], &byte, 1);
1211 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1216 * got dtmf from bchannel (locked state)
1218 void lcr_in_dtmf(struct chan_call *call, int val)
1220 struct ast_channel *ast = call->ast;
1225 if (!call->pbx_started)
1228 if (!call->dsp_dtmf) {
1229 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1233 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1239 write(wake_pipe[1], &byte, 1);
1241 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1245 * message received from LCR
1247 int receive_message(int message_type, unsigned int ref, union parameter *param)
1249 struct bchannel *bchannel;
1250 struct chan_call *call;
1251 union parameter newparam;
1253 memset(&newparam, 0, sizeof(union parameter));
1255 /* handle bchannel message*/
1256 if (message_type == MESSAGE_BCHANNEL) {
1257 switch(param->bchannel.type) {
1258 case BCHANNEL_ASSIGN:
1259 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1260 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1261 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1264 /* create bchannel */
1265 bchannel = alloc_bchannel(param->bchannel.handle);
1267 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1271 /* configure channel */
1272 bchannel->b_tx_gain = param->bchannel.tx_gain;
1273 bchannel->b_rx_gain = param->bchannel.rx_gain;
1274 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1275 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1276 bchannel->b_bf_len = param->bchannel.crypt_len;
1277 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1279 bchannel->b_txdata = 0;
1280 bchannel->b_tx_dejitter = 1;
1282 /* in case, ref is not set, this bchannel instance must
1283 * be created until it is removed again by LCR */
1285 call = find_call_ref(ref);
1287 bchannel->call = call;
1288 call->bchannel = bchannel;
1290 bchannel_dtmf(bchannel, 1);
1292 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1293 if (call->pipeline[0])
1294 bchannel_pipeline(bchannel, call->pipeline);
1296 bchannel_gain(bchannel, call->rx_gain, 0);
1298 bchannel_gain(bchannel, call->tx_gain, 1);
1299 if (call->bridge_id) {
1300 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1301 bchannel_join(bchannel, call->bridge_id);
1303 /* create only, if call exists, othewhise it bchannel is freed below... */
1304 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1305 bchannel_activate(bchannel, 1);
1308 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1309 newparam.bchannel.handle = param->bchannel.handle;
1310 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1311 /* if call has released before bchannel is assigned */
1313 newparam.bchannel.type = BCHANNEL_RELEASE;
1314 newparam.bchannel.handle = param->bchannel.handle;
1315 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1320 case BCHANNEL_REMOVE:
1321 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1322 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1323 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1326 /* unklink from call and destroy bchannel */
1327 free_bchannel(bchannel);
1330 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1331 newparam.bchannel.handle = param->bchannel.handle;
1332 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1337 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1342 /* handle new ref */
1343 if (message_type == MESSAGE_NEWREF) {
1344 if (param->direction) {
1345 /* new ref from lcr */
1346 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1347 if (!ref || find_call_ref(ref)) {
1348 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1351 /* allocate new call instance */
1352 call = alloc_call();
1354 call->state = CHAN_LCR_STATE_IN_PREPARE;
1357 call->ref_was_assigned = 1;
1358 /* set dtmf (default, use option 'n' to disable */
1360 /* wait for setup (or release from asterisk) */
1362 /* new ref, as requested from this remote application */
1363 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1364 call = find_call_ref(0);
1366 /* send release, if ref does not exist */
1367 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1368 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1373 call->ref_was_assigned = 1;
1374 /* set dtmf (default, use option 'n' to disable */
1376 /* send pending setup info */
1377 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1378 send_setup_to_lcr(call);
1379 /* release if asterisk has signed off */
1380 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1383 send_release_and_import(call, call->cause, call->location);
1385 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1396 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1399 call = find_call_ref(ref);
1401 /* ignore ref that is not used (anymore) */
1402 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1406 /* handle messages */
1407 switch(message_type) {
1409 lcr_in_setup(call, message_type, param);
1412 case MESSAGE_OVERLAP:
1413 lcr_in_overlap(call, message_type, param);
1416 case MESSAGE_PROCEEDING:
1417 lcr_in_proceeding(call, message_type, param);
1420 case MESSAGE_ALERTING:
1421 lcr_in_alerting(call, message_type, param);
1424 case MESSAGE_CONNECT:
1425 lcr_in_connect(call, message_type, param);
1428 case MESSAGE_DISCONNECT:
1429 lcr_in_disconnect(call, message_type, param);
1432 case MESSAGE_RELEASE:
1433 lcr_in_release(call, message_type, param);
1436 case MESSAGE_INFORMATION:
1437 lcr_in_information(call, message_type, param);
1440 case MESSAGE_NOTIFY:
1441 lcr_in_notify(call, message_type, param);
1444 case MESSAGE_FACILITY:
1445 lcr_in_facility(call, message_type, param);
1448 case MESSAGE_PATTERN: // audio available from LCR
1449 if (!call->has_pattern)
1450 lcr_in_pattern(call, message_type, param);
1453 case MESSAGE_NOPATTERN: // audio not available from LCR
1456 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1457 call->audiopath = param->audiopath;
1461 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1468 * release all calls (due to broken socket)
1470 static void release_all_calls(void)
1472 struct chan_call *call;
1477 /* no ast, so we may directly free call */
1479 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1483 /* already in release process */
1484 if (call->state == CHAN_LCR_STATE_RELEASE) {
1488 /* release or queue release */
1490 call->state = CHAN_LCR_STATE_RELEASE;
1491 if (!call->pbx_started) {
1492 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1493 ast_hangup(call->ast); // call will be destroyed here
1496 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1500 write(wake_pipe[1], &byte, 1);
1502 strcpy(call->queue_string, "H");
1506 /* release all bchannels */
1507 while(bchannel_first)
1508 free_bchannel(bchannel_first);
1511 void close_socket(void);
1514 * warning! not thread safe
1515 * returns -1 for socket error, 0 for no work, 1 for work
1517 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1520 struct admin_list *admin;
1521 struct admin_message msg;
1523 if ((what & LCR_FD_READ)) {
1524 /* read from socket */
1525 len = read(lcr_sock, &msg, sizeof(msg));
1527 CERROR(NULL, NULL, "Socket closed.\n");
1529 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1531 release_all_calls();
1532 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1536 if (len != sizeof(msg)) {
1537 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1540 if (msg.message != ADMIN_MESSAGE) {
1541 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1544 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1546 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1551 if ((what & LCR_FD_WRITE)) {
1552 /* write to socket */
1554 socket_fd.when &= ~LCR_FD_WRITE;
1557 admin = admin_first;
1558 len = write(lcr_sock, &admin->msg, sizeof(msg));
1560 CERROR(NULL, NULL, "Socket closed.\n");
1564 if (len != sizeof(msg)) {
1565 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1569 admin_first = admin->next;
1573 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1582 * open and close socket and thread
1584 int open_socket(void)
1587 struct sockaddr_un sock_address;
1588 union parameter param;
1591 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1592 CERROR(NULL, NULL, "Failed to create socket.\n");
1596 /* set socket address and name */
1597 memset(&sock_address, 0, sizeof(sock_address));
1598 sock_address.sun_family = PF_UNIX;
1599 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1601 /* connect socket */
1602 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1605 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1609 /* register socket fd */
1610 memset(&socket_fd, 0, sizeof(socket_fd));
1611 socket_fd.fd = lcr_sock;
1612 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1614 /* enque hello message */
1615 memset(¶m, 0, sizeof(param));
1616 strcpy(param.hello.application, "asterisk");
1617 send_message(MESSAGE_HELLO, 0, ¶m);
1622 void close_socket(void)
1624 struct admin_list *admin, *temp;
1626 unregister_fd(&socket_fd);
1628 /* flush pending messages */
1629 admin = admin_first;
1632 admin = admin->next;
1645 /* sending queue to asterisk */
1646 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1650 read(wake_pipe[0], &byte, 1);
1657 static void handle_queue()
1659 struct chan_call *call;
1660 struct ast_channel *ast;
1661 struct ast_frame fr;
1667 p = call->queue_string;
1670 if (ast_channel_trylock(ast)) {
1671 ast_mutex_unlock(&chan_lock);
1673 ast_mutex_lock(&chan_lock);
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);
1737 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1739 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1740 if (open_socket() < 0)
1741 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1746 void lock_chan(void)
1748 ast_mutex_lock(&chan_lock);
1751 void unlock_chan(void)
1753 ast_mutex_unlock(&chan_lock);
1756 /* chan_lcr thread */
1757 static void *chan_thread(void *arg)
1759 if (pipe(wake_pipe) < 0) {
1760 CERROR(NULL, NULL, "Failed to open pipe.\n");
1763 memset(&wake_fd, 0, sizeof(wake_fd));
1764 wake_fd.fd = wake_pipe[0];
1765 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1767 memset(&socket_retry, 0, sizeof(socket_retry));
1768 add_timer(&socket_retry, handle_retry, NULL, 0);
1770 bchannel_pid = getpid();
1772 /* open socket the first time */
1773 handle_retry(NULL, NULL, 0);
1775 ast_mutex_lock(&chan_lock);
1779 select_main(0, &global_change, lock_chan, unlock_chan);
1784 del_timer(&socket_retry);
1786 unregister_fd(&wake_fd);
1787 close(wake_pipe[0]);
1788 close(wake_pipe[1]);
1790 CERROR(NULL, NULL, "Thread exit.\n");
1792 ast_mutex_unlock(&chan_lock);
1798 * new asterisk instance
1801 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1803 char exten[256], *dial, *interface, *opt;
1804 struct ast_channel *ast;
1805 struct chan_call *call;
1807 ast_mutex_lock(&chan_lock);
1808 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1810 /* if socket is closed */
1812 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1813 ast_mutex_unlock(&chan_lock);
1817 /* create call instance */
1818 call = alloc_call();
1820 /* failed to create instance */
1821 ast_mutex_unlock(&chan_lock);
1825 /* create asterisk channel instrance */
1827 #ifdef LCR_FOR_ASTERISK
1828 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1831 #ifdef LCR_FOR_CALLWEAVER
1832 ast = ast_channel_alloc(1);
1836 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1838 /* failed to create instance */
1839 ast_mutex_unlock(&chan_lock);
1842 ast->tech = &lcr_tech;
1843 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1844 /* configure channel */
1845 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1846 ast->readformat = ast->rawreadformat = ast->nativeformats;
1847 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1849 ast->hangupcause = 0;
1853 ast->tech_pvt = call;
1854 ast->fds[0] = call->pipe[0];
1855 call->pbx_started = 0;
1857 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1860 * Extract interface, dialstring, options from data.
1863 * <interface>/<dialstring>
1864 * <interface>/<dialstring>/options
1866 strncpy(exten, (char *)data, sizeof(exten)-1);
1867 exten[sizeof(exten)-1] = '\0';
1868 if ((dial = strchr(exten, '/'))) {
1871 if ((opt = strchr(dial, '/')))
1880 strncpy(call->interface, interface, sizeof(call->interface)-1);
1881 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1882 apply_opt(call, (char *)opt);
1884 ast_mutex_unlock(&chan_lock);
1889 * call from asterisk
1891 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1893 union parameter newparam;
1894 struct chan_call *call;
1896 ast_mutex_lock(&chan_lock);
1897 call = ast->tech_pvt;
1899 #ifdef LCR_FOR_CALLWEAVER
1901 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1905 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1906 ast_mutex_unlock(&chan_lock);
1910 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1912 /* pbx process is started */
1913 call->pbx_started = 1;
1914 /* send MESSAGE_NEWREF */
1915 memset(&newparam, 0, sizeof(union parameter));
1916 newparam.direction = 0; /* request from app */
1917 send_message(MESSAGE_NEWREF, 0, &newparam);
1919 /* set hdlc if capability requires hdlc */
1920 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1921 || ast->transfercapability == INFO_BC_DATARESTRICTED
1922 || ast->transfercapability == INFO_BC_VIDEO)
1924 /* if hdlc is forced by option, we change transcap to data */
1926 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1927 && ast->transfercapability != INFO_BC_DATARESTRICTED
1928 && ast->transfercapability != INFO_BC_VIDEO)
1929 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1931 call->cid_num[0] = 0;
1932 call->cid_name[0] = 0;
1933 call->cid_rdnis[0] = 0;
1935 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1936 strncpy(call->cid_num, ast->cid.cid_num,
1937 sizeof(call->cid_num)-1);
1939 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1940 strncpy(call->cid_name, ast->cid.cid_name,
1941 sizeof(call->cid_name)-1);
1942 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1943 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1944 sizeof(call->cid_rdnis)-1);
1946 ast_mutex_unlock(&chan_lock);
1950 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1952 static const char* dtmf_tones[] = {
1953 "!941+1336/100,!0/100", /* 0 */
1954 "!697+1209/100,!0/100", /* 1 */
1955 "!697+1336/100,!0/100", /* 2 */
1956 "!697+1477/100,!0/100", /* 3 */
1957 "!770+1209/100,!0/100", /* 4 */
1958 "!770+1336/100,!0/100", /* 5 */
1959 "!770+1477/100,!0/100", /* 6 */
1960 "!852+1209/100,!0/100", /* 7 */
1961 "!852+1336/100,!0/100", /* 8 */
1962 "!852+1477/100,!0/100", /* 9 */
1963 "!697+1633/100,!0/100", /* A */
1964 "!770+1633/100,!0/100", /* B */
1965 "!852+1633/100,!0/100", /* C */
1966 "!941+1633/100,!0/100", /* D */
1967 "!941+1209/100,!0/100", /* * */
1968 "!941+1477/100,!0/100" }; /* # */
1970 if (digit >= '0' && digit <='9')
1971 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1972 else if (digit >= 'A' && digit <= 'D')
1973 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1974 else if (digit == '*')
1975 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1976 else if (digit == '#')
1977 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1980 CDEBUG(NULL, ast, "Unable to handle DTMF tone "
1981 "'%c' for '%s'\n", digit, ast->name);
1985 #ifdef LCR_FOR_ASTERISK
1986 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1988 #ifdef LCR_FOR_CALLWEAVER
1989 static int lcr_digit(struct ast_channel *ast, char digit)
1992 struct chan_call *call;
1993 union parameter newparam;
1996 #ifdef LCR_FOR_CALLWEAVER
1997 int inband_dtmf = 0;
2000 /* only pass IA5 number space */
2001 if (digit > 126 || digit < 32)
2004 ast_mutex_lock(&chan_lock);
2005 call = ast->tech_pvt;
2007 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2008 ast_mutex_unlock(&chan_lock);
2012 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2014 /* send information or queue them */
2015 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2016 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2017 memset(&newparam, 0, sizeof(union parameter));
2019 newparam.information.keypad[0] = digit;
2020 newparam.information.keypad[1] = '\0';
2022 newparam.information.id[0] = digit;
2023 newparam.information.id[1] = '\0';
2025 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2028 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2029 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2031 strncat(call->dialque, buf, strlen(call->dialque)-1);
2034 ast_mutex_unlock(&chan_lock);
2036 #ifdef LCR_FOR_ASTERISK
2040 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2042 int inband_dtmf = 0;
2043 struct chan_call *call;
2046 ast_mutex_lock(&chan_lock);
2048 call = ast->tech_pvt;
2052 "Received digit from Asterisk, "
2053 "but no call instance exists.\n");
2054 ast_mutex_unlock(&chan_lock);
2058 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2060 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2064 ast_mutex_unlock(&chan_lock);
2067 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2068 send_digit_to_chan(ast, digit);
2074 static int lcr_answer(struct ast_channel *ast)
2076 union parameter newparam;
2077 struct chan_call *call;
2079 ast_mutex_lock(&chan_lock);
2080 call = ast->tech_pvt;
2082 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2083 ast_mutex_unlock(&chan_lock);
2087 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2089 /* copy connectinfo, if bridged */
2090 if (call->bridge_call)
2091 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2092 /* send connect message to lcr */
2093 if (call->state != CHAN_LCR_STATE_CONNECT) {
2094 memset(&newparam, 0, sizeof(union parameter));
2095 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2096 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2097 call->state = CHAN_LCR_STATE_CONNECT;
2100 /* request bchannel */
2101 if (!call->bchannel) {
2102 CDEBUG(call, ast, "Requesting B-channel.\n");
2103 memset(&newparam, 0, sizeof(union parameter));
2104 newparam.bchannel.type = BCHANNEL_REQUEST;
2105 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2108 // memset(&newparam, 0, sizeof(union parameter));
2109 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2111 ast_mutex_unlock(&chan_lock);
2115 static int lcr_hangup(struct ast_channel *ast)
2117 struct chan_call *call;
2118 pthread_t tid = pthread_self();
2120 if (!pthread_equal(tid, chan_tid)) {
2121 ast_mutex_lock(&chan_lock);
2123 call = ast->tech_pvt;
2125 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2126 if (!pthread_equal(tid, chan_tid)) {
2127 ast_mutex_unlock(&chan_lock);
2132 if (!pthread_equal(tid, chan_tid))
2133 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2135 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2137 /* disconnect asterisk, maybe not required */
2138 ast->tech_pvt = NULL;
2142 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2143 if (ast->hangupcause > 0)
2144 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2146 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2149 if (!pthread_equal(tid, chan_tid)) {
2150 ast_mutex_unlock(&chan_lock);
2154 /* ref is not set, due to prepare setup or release */
2155 if (call->state == CHAN_LCR_STATE_RELEASE) {
2156 /* we get the response to our release */
2157 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2160 /* during prepare, we change to release state */
2161 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2162 call->state = CHAN_LCR_STATE_RELEASE;
2166 if (!pthread_equal(tid, chan_tid)) {
2167 ast_mutex_unlock(&chan_lock);
2172 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2174 struct chan_call *call;
2177 CDEBUG(NULL, ast, "No subclass\n");
2178 if (!(f->subclass & ast->nativeformats))
2179 CDEBUG(NULL, ast, "Unexpected format.\n");
2181 ast_mutex_lock(&chan_lock);
2182 call = ast->tech_pvt;
2184 ast_mutex_unlock(&chan_lock);
2187 if (call->bchannel && f->samples)
2188 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2189 ast_mutex_unlock(&chan_lock);
2194 static struct ast_frame *lcr_read(struct ast_channel *ast)
2196 struct chan_call *call;
2199 ast_mutex_lock(&chan_lock);
2200 call = ast->tech_pvt;
2202 ast_mutex_unlock(&chan_lock);
2205 if (call->pipe[0] > -1) {
2206 if (call->rebuffer && !call->hdlc) {
2207 /* Make sure we have a complete 20ms (160byte) frame */
2208 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2210 call->framepos += len;
2213 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2215 if (len < 0 && errno == EAGAIN) {
2216 ast_mutex_unlock(&chan_lock);
2218 #ifdef LCR_FOR_ASTERISK
2219 return &ast_null_frame;
2222 #ifdef LCR_FOR_CALLWEAVER
2228 close(call->pipe[0]);
2231 ast_mutex_unlock(&chan_lock);
2233 } else if (call->rebuffer && call->framepos < 160) {
2234 /* Not a complete frame, so we send a null-frame */
2235 ast_mutex_unlock(&chan_lock);
2236 return &ast_null_frame;
2240 call->read_fr.frametype = AST_FRAME_VOICE;
2241 call->read_fr.subclass = ast->nativeformats;
2242 if (call->rebuffer) {
2243 call->read_fr.datalen = call->framepos;
2244 call->read_fr.samples = call->framepos;
2247 call->read_fr.datalen = len;
2248 call->read_fr.samples = len;
2250 call->read_fr.delivery = ast_tv(0,0);
2251 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2252 ast_mutex_unlock(&chan_lock);
2254 return &call->read_fr;
2257 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2259 union parameter newparam;
2261 struct chan_call *call;
2262 const struct tone_zone_sound *ts = NULL;
2264 ast_mutex_lock(&chan_lock);
2265 call = ast->tech_pvt;
2267 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2268 ast_mutex_unlock(&chan_lock);
2273 case AST_CONTROL_BUSY:
2274 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2275 ast_setstate(ast, AST_STATE_BUSY);
2276 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2277 /* send message to lcr */
2278 memset(&newparam, 0, sizeof(union parameter));
2279 newparam.disconnectinfo.cause = 17;
2280 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2281 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2283 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2285 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2286 ts = ast_get_indication_tone(ast->zone, "busy");
2289 case AST_CONTROL_CONGESTION:
2290 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2291 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2292 /* send message to lcr */
2293 memset(&newparam, 0, sizeof(union parameter));
2294 newparam.disconnectinfo.cause = ast->hangupcause;
2295 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2296 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2298 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2300 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2301 ts = ast_get_indication_tone(ast->zone, "congestion");
2304 case AST_CONTROL_PROCEEDING:
2305 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2306 if (call->state == CHAN_LCR_STATE_IN_SETUP
2307 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2308 /* send message to lcr */
2309 memset(&newparam, 0, sizeof(union parameter));
2310 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2312 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2315 case AST_CONTROL_RINGING:
2316 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2317 ast_setstate(ast, AST_STATE_RING);
2318 if (call->state == CHAN_LCR_STATE_IN_SETUP
2319 || call->state == CHAN_LCR_STATE_IN_DIALING
2320 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2321 /* send message to lcr */
2322 memset(&newparam, 0, sizeof(union parameter));
2323 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2325 call->state = CHAN_LCR_STATE_IN_ALERTING;
2327 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2328 ts = ast_get_indication_tone(ast->zone, "ring");
2331 case AST_CONTROL_PROGRESS:
2332 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2333 /* request bchannel */
2334 if (!call->bchannel) {
2335 CDEBUG(call, ast, "Requesting B-channel.\n");
2336 memset(&newparam, 0, sizeof(union parameter));
2337 newparam.bchannel.type = BCHANNEL_REQUEST;
2338 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2342 CDEBUG(call, ast, "Received indicate -1.\n");
2343 ast_playtones_stop(ast);
2347 case AST_CONTROL_VIDUPDATE:
2348 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2351 case AST_CONTROL_HOLD:
2352 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2353 /* send message to lcr */
2354 memset(&newparam, 0, sizeof(union parameter));
2355 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2356 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2358 /*start music onhold*/
2359 #ifdef LCR_FOR_ASTERISK
2360 ast_moh_start(ast,data,ast->musicclass);
2363 #ifdef LCR_FOR_CALLWEAVER
2364 ast_moh_start(ast, NULL);
2369 case AST_CONTROL_UNHOLD:
2370 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2371 /* send message to lcr */
2372 memset(&newparam, 0, sizeof(union parameter));
2373 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2374 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2380 #ifdef AST_CONTROL_SRCUPDATE
2381 case AST_CONTROL_SRCUPDATE:
2385 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2388 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2393 if (ts && ts->data[0]) {
2394 ast_playtones_start(ast, 0, ts->data, 1);
2398 ast_mutex_unlock(&chan_lock);
2405 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2407 struct chan_call *call;
2413 ast_mutex_lock(&chan_lock);
2414 call = ast->tech_pvt;
2416 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2417 ast_mutex_unlock(&chan_lock);
2421 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2423 ast_mutex_unlock(&chan_lock);
2428 * send_text asterisk
2430 static int lcr_send_text(struct ast_channel *ast, const char *text)
2432 struct chan_call *call;
2433 union parameter newparam;
2435 ast_mutex_lock(&chan_lock);
2436 call = ast->tech_pvt;
2438 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2439 ast_mutex_unlock(&chan_lock);
2443 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2444 memset(&newparam, 0, sizeof(union parameter));
2445 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2446 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2447 ast_mutex_unlock(&chan_lock);
2454 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2455 struct ast_channel *ast2, int flags,
2456 struct ast_frame **fo,
2457 struct ast_channel **rc, int timeoutms)
2460 struct chan_call *call1, *call2;
2461 struct ast_channel *carr[2], *who;
2463 struct ast_frame *f;
2466 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2471 /* join via dsp (if the channels are currently open) */
2472 ast_mutex_lock(&chan_lock);
2473 call1 = ast1->tech_pvt;
2474 call2 = ast2->tech_pvt;
2475 if (!call1 || !call2) {
2476 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2477 ast_mutex_unlock(&chan_lock);
2478 return AST_BRIDGE_COMPLETE;
2481 /* join, if both call instances uses dsp
2482 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2484 if (!call1->nodsp && !call2->nodsp) {
2485 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2487 /* get bridge id and join */
2488 bridge_id = new_bridge_id();
2490 call1->bridge_id = bridge_id;
2491 if (call1->bchannel)
2492 bchannel_join(call1->bchannel, bridge_id);
2494 call2->bridge_id = bridge_id;
2495 if (call2->bchannel)
2496 bchannel_join(call2->bchannel, bridge_id);
2498 if (call1->nodsp && call2->nodsp)
2499 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2501 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2502 call1->bridge_call = call2;
2503 call2->bridge_call = call1;
2505 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2506 || call1->state == CHAN_LCR_STATE_IN_DIALING
2507 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2508 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2509 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2512 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2513 || call2->state == CHAN_LCR_STATE_IN_DIALING
2514 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2515 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2516 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2520 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2521 so let's do it for them. Hmpf.
2524 if (call1->on_hold) {
2525 union parameter newparam;
2527 memset(&newparam, 0, sizeof(union parameter));
2528 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2529 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2534 if (call2->on_hold) {
2535 union parameter newparam;
2537 memset(&newparam, 0, sizeof(union parameter));
2538 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2539 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2544 ast_mutex_unlock(&chan_lock);
2548 who = ast_waitfor_n(carr, 2, &to);
2551 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2556 if (!f || f->frametype == AST_FRAME_CONTROL) {
2558 CDEBUG(NULL, NULL, "Got hangup.\n");
2560 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2567 if ( f->frametype == AST_FRAME_DTMF ) {
2568 CDEBUG(NULL, NULL, "Got DTMF.\n");
2584 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2586 /* split channels */
2587 ast_mutex_lock(&chan_lock);
2588 call1 = ast1->tech_pvt;
2589 call2 = ast2->tech_pvt;
2590 if (call1 && call1->bridge_id) {
2591 call1->bridge_id = 0;
2592 if (call1->bchannel)
2593 bchannel_join(call1->bchannel, 0);
2594 if (call1->bridge_call)
2595 call1->bridge_call->bridge_call = NULL;
2597 if (call2 && call1->bridge_id) {
2598 call2->bridge_id = 0;
2599 if (call2->bchannel)
2600 bchannel_join(call2->bchannel, 0);
2601 if (call2->bridge_call)
2602 call2->bridge_call->bridge_call = NULL;
2604 call1->bridge_call = NULL;
2605 call2->bridge_call = NULL;
2607 ast_mutex_unlock(&chan_lock);
2608 return AST_BRIDGE_COMPLETE;
2610 static struct ast_channel_tech lcr_tech = {
2612 .description = "Channel driver for connecting to Linux-Call-Router",
2613 .capabilities = AST_FORMAT_ALAW,
2614 .requester = lcr_request,
2616 #ifdef LCR_FOR_ASTERISK
2617 .send_digit_begin = lcr_digit_begin,
2618 .send_digit_end = lcr_digit_end,
2621 #ifdef LCR_FOR_CALLWEAVER
2622 .send_digit = lcr_digit,
2626 .bridge = lcr_bridge,
2627 .hangup = lcr_hangup,
2628 .answer = lcr_answer,
2631 .indicate = lcr_indicate,
2633 .send_text = lcr_send_text,
2642 static int lcr_show_lcr (int fd, int argc, char *argv[])
2647 static int lcr_show_calls (int fd, int argc, char *argv[])
2652 static int lcr_reload_routing (int fd, int argc, char *argv[])
2657 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2662 static int lcr_port_block (int fd, int argc, char *argv[])
2667 static int lcr_port_unblock (int fd, int argc, char *argv[])
2672 static int lcr_port_unload (int fd, int argc, char *argv[])
2677 static struct ast_cli_entry cli_show_lcr =
2678 { {"lcr", "show", "lcr", NULL},
2680 "Shows current states of LCR core",
2681 "Usage: lcr show lcr\n",
2684 static struct ast_cli_entry cli_show_calls =
2685 { {"lcr", "show", "calls", NULL},
2687 "Shows current calls made by LCR and Asterisk",
2688 "Usage: lcr show calls\n",
2691 static struct ast_cli_entry cli_reload_routing =
2692 { {"lcr", "reload", "routing", NULL},
2694 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2695 "Usage: lcr reload routing\n",
2698 static struct ast_cli_entry cli_reload_interfaces =
2699 { {"lcr", "reload", "interfaces", NULL},
2700 lcr_reload_interfaces,
2701 "Reloads interfaces conf of LCR",
2702 "Usage: lcr reload interfaces\n",
2705 static struct ast_cli_entry cli_port_block =
2706 { {"lcr", "port", "block", NULL},
2708 "Blocks LCR port for further calls",
2709 "Usage: lcr port block \"<port>\"\n",
2712 static struct ast_cli_entry cli_port_unblock =
2713 { {"lcr", "port", "unblock", NULL},
2715 "Unblocks or loads LCR port, port is opened my mISDN",
2716 "Usage: lcr port unblock \"<port>\"\n",
2719 static struct ast_cli_entry cli_port_unload =
2720 { {"lcr", "port", "unload", NULL},
2722 "Unloads LCR port, port is closes by mISDN",
2723 "Usage: lcr port unload \"<port>\"\n",
2728 #ifdef LCR_FOR_ASTERISK
2729 static int lcr_config_exec(struct ast_channel *ast, void *data)
2732 #ifdef LCR_FOR_CALLWEAVER
2733 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2736 struct chan_call *call;
2738 ast_mutex_lock(&chan_lock);
2740 #ifdef LCR_FOR_ASTERISK
2741 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2744 #ifdef LCR_FOR_CALLWEAVER
2745 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2751 if (call->ast == ast)
2757 #ifdef LCR_FOR_ASTERISK
2758 apply_opt(call, (char *)data);
2761 #ifdef LCR_FOR_CALLWEAVER
2762 apply_opt(call, (char *)argv[0]);
2766 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2768 ast_mutex_unlock(&chan_lock);
2773 * module loading and destruction
2775 int load_module(void)
2778 char options_error[256];
2780 for (i = 0; i < 256; i++) {
2781 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2782 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2785 if (read_options(options_error) == 0) {
2786 CERROR(NULL, NULL, "%s", options_error);
2788 #ifdef LCR_FOR_ASTERISK
2789 return AST_MODULE_LOAD_DECLINE;
2792 #ifdef LCR_FOR_CALLWEAVER
2798 ast_mutex_init(&chan_lock);
2799 ast_mutex_init(&log_lock);
2801 if (bchannel_initialize()) {
2802 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2805 #ifdef LCR_FOR_ASTERISK
2806 return AST_MODULE_LOAD_DECLINE;
2809 #ifdef LCR_FOR_CALLWEAVER
2815 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2816 if (ast_channel_register(&lcr_tech)) {
2817 CERROR(NULL, NULL, "Unable to register channel class\n");
2818 bchannel_deinitialize();
2821 #ifdef LCR_FOR_ASTERISK
2822 return AST_MODULE_LOAD_DECLINE;
2825 #ifdef LCR_FOR_CALLWEAVER
2830 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2832 #ifdef LCR_FOR_ASTERISK
2833 "lcr_config(<opt><optarg>:<opt>:...)\n"
2836 #ifdef LCR_FOR_CALLWEAVER
2837 "lcr_config(<opt><optarg>:<opt>:...)\n",
2840 "Sets LCR opts. and optargs\n"
2842 "The available options are:\n"
2843 " d - Send display text on called phone, text is the optarg.\n"
2844 " n - Don't detect dtmf tones on called channel.\n"
2845 " h - Force data call (HDLC).\n"
2846 " t - Disable mISDN_dsp features (required for fax application).\n"
2847 " q - Add queue to make fax stream seamless (required for fax app).\n"
2848 " Use queue size in miliseconds for optarg. (try 250)\n"
2849 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2850 " Use time to detect for optarg.\n"
2851 " c - Make crypted outgoing call, optarg is keyindex.\n"
2852 " e - Perform echo cancelation on this channel.\n"
2853 " Takes mISDN pipeline option as optarg.\n"
2854 " s - Send Non Inband DTMF as inband.\n"
2855 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2856 " vr - rxgain control\n"
2857 " vt - txgain control\n"
2858 " Volume changes at factor 2 ^ optarg.\n"
2859 " k - use keypad to dial this call.\n"
2861 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
2862 " -> use 16 for fax (3.1k audio)\n"
2864 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
2865 "options: \"n:t:q250\" for seamless audio transmission.\n"
2870 ast_cli_register(&cli_show_lcr);
2871 ast_cli_register(&cli_show_calls);
2872 ast_cli_register(&cli_reload_routing);
2873 ast_cli_register(&cli_reload_interfaces);
2874 ast_cli_register(&cli_port_block);
2875 ast_cli_register(&cli_port_unblock);
2876 ast_cli_register(&cli_port_unload);
2880 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
2881 /* failed to create thread */
2882 bchannel_deinitialize();
2884 ast_channel_unregister(&lcr_tech);
2886 #ifdef LCR_FOR_ASTERISK
2887 return AST_MODULE_LOAD_DECLINE;
2890 #ifdef LCR_FOR_CALLWEAVER
2898 int unload_module(void)
2900 /* First, take us out of the channel loop */
2901 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2904 pthread_join(chan_tid, NULL);
2906 ast_channel_unregister(&lcr_tech);
2908 ast_unregister_application("lcr_config");
2911 if (mISDN_created) {
2912 bchannel_deinitialize();
2916 if (lcr_sock >= 0) {
2924 int reload_module(void)
2930 #ifdef LCR_FOR_ASTERISK
2931 #define AST_MODULE "chan_lcr"
2934 #ifdef LCR_FOR_CALLWEAVER
2939 ast_mutex_lock(&usecnt_lock);
2941 ast_mutex_unlock(&usecnt_lock);
2946 #ifdef LCR_FOR_ASTERISK
2947 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2948 .load = load_module,
2949 .unload = unload_module,
2950 .reload = reload_module,
2954 #ifdef LCR_FOR_CALLWEAVER
2955 char *description(void)