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"
170 #include "bchannel.h"
172 #include "chan_lcr.h"
174 CHAN_LCR_STATE // state description structure
175 MESSAGES // message text
177 #ifdef LCR_FOR_CALLWEAVER
178 AST_MUTEX_DEFINE_STATIC(rand_lock);
181 unsigned char flip_bits[256];
183 #ifdef LCR_FOR_CALLWEAVER
184 static struct ast_frame nullframe = { AST_FRAME_NULL, };
190 char lcr_type[]="lcr";
192 #ifdef LCR_FOR_CALLWEAVER
193 static ast_mutex_t usecnt_lock;
195 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
199 ast_mutex_t chan_lock; /* global lock */
200 ast_mutex_t log_lock; /* logging log */
203 int glob_channel = 0;
208 struct admin_list *next;
209 struct admin_message msg;
210 } *admin_first = NULL;
212 static struct ast_channel_tech lcr_tech;
217 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, ...)
220 char call_text[128] = "NULL";
221 char ast_text[128] = "NULL";
224 ast_mutex_lock(&log_lock);
227 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
228 buffer[sizeof(buffer)-1]=0;
232 sprintf(call_text, "%d", call->ref);
234 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
235 ast_text[sizeof(ast_text)-1] = '\0';
237 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
239 ast_mutex_unlock(&log_lock);
243 * channel and call instances
245 struct chan_call *call_first;
249 * special case: 0: find new ref, that has not been assigned a ref yet
252 struct chan_call *find_call_ref(unsigned int ref)
254 struct chan_call *call = call_first;
255 int assigned = (ref > 0);
258 if (call->ref == ref && call->ref_was_assigned == assigned)
265 void free_call(struct chan_call *call)
267 struct chan_call **temp = &call_first;
271 *temp = (*temp)->next;
272 if (call->pipe[0] > -1)
273 close(call->pipe[0]);
274 if (call->pipe[1] > -1)
275 close(call->pipe[1]);
276 if (call->bchannel) {
277 if (call->bchannel->call != call)
278 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
279 call->bchannel->call = NULL;
281 if (call->bridge_call) {
282 if (call->bridge_call->bridge_call != call)
283 CERROR(call, NULL, "Linked call structure has no link to us.\n");
284 call->bridge_call->bridge_call = NULL;
287 ast_translator_free_path(call->trans);
289 ast_dsp_free(call->dsp);
290 CDEBUG(call, NULL, "Call instance freed.\n");
294 temp = &((*temp)->next);
296 CERROR(call, NULL, "Call instance not found in list.\n");
299 struct chan_call *alloc_call(void)
301 struct chan_call **callp = &call_first;
304 callp = &((*callp)->next);
306 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
308 memset(*callp, 0, sizeof(struct chan_call));
309 if (pipe((*callp)->pipe) < 0) {
310 CERROR(*callp, NULL, "Failed to create pipe.\n");
314 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
315 CDEBUG(*callp, NULL, "Call instance allocated.\n");
319 unsigned short new_bridge_id(void)
321 struct chan_call *call;
322 unsigned short id = 1;
324 /* search for lowest bridge id that is not in use and not 0 */
328 if (call->bridge_id == id)
336 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
341 * enque message to LCR
343 int send_message(int message_type, unsigned int ref, union parameter *param)
345 struct admin_list *admin, **adminp;
348 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
351 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
353 adminp = &admin_first;
355 adminp = &((*adminp)->next);
356 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
358 CERROR(NULL, NULL, "No memory for message to LCR.\n");
363 admin->msg.message = ADMIN_MESSAGE;
364 admin->msg.u.msg.type = message_type;
365 admin->msg.u.msg.ref = ref;
366 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
372 * apply options (in locked state)
374 void apply_opt(struct chan_call *call, char *data)
376 union parameter newparam;
377 char string[1024], *p = string, *opt, *key;
383 strncpy(string, data, sizeof(string)-1);
384 string[sizeof(string)-1] = '\0';
387 while((opt = strsep(&p, ":"))) {
390 if (opt[1] == '\0') {
391 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
394 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
395 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
396 strncpy(call->display, opt+1, sizeof(call->display)-1);
398 memset(&newparam, 0, sizeof(union parameter));
399 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
400 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
404 if (opt[1] != '\0') {
405 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
408 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
409 if (call->dsp_dtmf) {
412 bchannel_dtmf(call->bchannel, 0);
416 if (opt[1] == '\0') {
417 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
421 /* check for 0xXXXX... type of key */
422 if (!!strncmp((char *)key, "0x", 2)) {
423 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
427 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
428 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
433 if (*key>='0' && *key<='9')
434 call->bf_key[i] = (*key-'0') << 8;
435 else if (*key>='a' && *key<='f')
436 call->bf_key[i] = (*key-'a'+10) << 8;
437 else if (*key>='A' && *key<='F')
438 call->bf_key[i] = (*key-'A'+10) << 8;
442 if (*key>='0' && *key<='9')
443 call->bf_key[i] += (*key - '0');
444 else if (*key>='a' && *key<='f')
445 call->bf_key[i] += (*key - 'a' + 10);
446 else if (*key>='A' && *key<='F')
447 call->bf_key[i] += (*key - 'A' + 10);
454 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
458 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
460 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
463 if (opt[1] != '\0') {
464 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
467 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
472 if (opt[1] != '\0') {
473 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
476 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
481 if (opt[1] == '\0') {
482 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
485 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
486 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
488 bchannel_pipeline(call->bchannel, call->pipeline);
491 if (opt[1] == '\0') {
492 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
495 call->faxdetect=atoi(opt+1);
497 call->dsp=ast_dsp_new();
499 #ifdef LCR_FOR_CALLWEAVER
500 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
502 #ifdef LCR_FOR_ASTERISK
503 #ifdef DSP_FEATURE_DTMF_DETECT
504 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
506 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
511 #ifdef LCR_FOR_CALLWEAVER
512 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
514 #ifdef LCR_FOR_ASTERISK
515 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
518 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
521 if (opt[1] != '\0') {
522 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
525 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
530 if (opt[1] != '\0') {
531 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
534 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
535 call->inband_dtmf = 1;
538 if (opt[1] != 'r' && opt[1] != 't') {
539 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
543 if (gain < -8 || gain >8) {
544 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
547 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
549 call->rx_gain = gain;
551 bchannel_gain(call->bchannel, call->rx_gain, 0);
553 call->tx_gain = gain;
555 bchannel_gain(call->bchannel, call->tx_gain, 1);
559 if (opt[1] != '\0') {
560 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
563 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
568 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
572 /* re-open, if bchannel is created */
573 if (call->bchannel && call->bchannel->b_sock > -1) {
574 bchannel_destroy(call->bchannel);
575 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0)))
576 bchannel_activate(call->bchannel, 1);
581 * send setup info to LCR
582 * this function is called, when asterisk call is received and ref is received
584 static void send_setup_to_lcr(struct chan_call *call)
586 union parameter newparam;
587 struct ast_channel *ast = call->ast;
589 if (!call->ast || !call->ref)
592 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
594 /* send setup message to LCR */
595 memset(&newparam, 0, sizeof(union parameter));
596 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
597 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
599 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
601 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
602 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
603 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
604 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
605 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
606 call->display[0] = '\0';
607 if (call->cid_num[0])
608 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
609 if (call->cid_name[0])
610 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
611 if (call->cid_rdnis[0]) {
612 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
613 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
614 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
616 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
617 case AST_PRES_RESTRICTED:
618 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
620 case AST_PRES_UNAVAILABLE:
621 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
623 case AST_PRES_ALLOWED:
625 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
627 switch(ast->cid.cid_ton) {
629 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
632 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
635 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
638 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
640 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
641 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
643 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
645 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
646 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
648 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
649 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
650 send_message(MESSAGE_SETUP, call->ref, &newparam);
652 /* change to outgoing setup state */
653 call->state = CHAN_LCR_STATE_OUT_SETUP;
657 * send dialing info to LCR
658 * this function is called, when setup acknowledge is received and dialing
661 static void send_dialque_to_lcr(struct chan_call *call)
663 union parameter newparam;
665 if (!call->ast || !call->ref || !call->dialque[0])
668 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
670 /* send setup message to LCR */
671 memset(&newparam, 0, sizeof(union parameter));
673 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
675 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
676 call->dialque[0] = '\0';
677 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
681 * in case of a bridge, the unsupported message can be forwarded directly
682 * to the remote call.
684 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
688 if (!call->bridge_call) return;
689 CDEBUG(call, NULL, "Sending message due bridging.\n");
690 send_message(message_type, call->bridge_call->ref, param);
694 * send release message to LCR and import bchannel if exported
696 static void send_release_and_import(struct chan_call *call, int cause, int location)
698 union parameter newparam;
700 /* importing channel */
701 if (call->bchannel) {
702 memset(&newparam, 0, sizeof(union parameter));
703 newparam.bchannel.type = BCHANNEL_RELEASE;
704 newparam.bchannel.handle = call->bchannel->handle;
705 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
707 /* sending release */
708 memset(&newparam, 0, sizeof(union parameter));
709 newparam.disconnectinfo.cause = cause;
710 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
711 send_message(MESSAGE_RELEASE, call->ref, &newparam);
715 * check if extension matches and start asterisk
716 * if it can match, proceed
719 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
722 union parameter newparam;
723 char *exten = ast->exten;
727 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
731 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
732 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
736 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
737 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);
741 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
742 /* send setup acknowledge to lcr */
743 memset(&newparam, 0, sizeof(union parameter));
744 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
747 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
752 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
753 /* send setup acknowledge to lcr */
754 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
755 memset(&newparam, 0, sizeof(union parameter));
756 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
760 call->state = CHAN_LCR_STATE_IN_DIALING;
762 /* if match, start pbx */
763 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
764 CDEBUG(call, ast, "Extensions matches.\n");
769 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
775 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
783 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
784 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
786 /* release asterisk */
787 ast->hangupcause = call->cause;
788 /* change to release state */
789 call->state = CHAN_LCR_STATE_RELEASE;
790 ast_hangup(ast); // call will be destroyed here
794 /* send setup to asterisk */
795 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
797 #ifdef LCR_FOR_CALLWEAVER
799 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
802 ret = ast_pbx_start(ast);
804 cause = (ret==-2)?34:27;
807 call->pbx_started = 1;
808 ast_setstate(ast, AST_STATE_RING);
812 * incoming setup from LCR
814 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
816 struct ast_channel *ast;
818 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
820 /* create asterisk channel instrance */
822 #ifdef LCR_FOR_CALLWEAVER
823 ast = ast_channel_alloc(1);
826 #ifdef LCR_FOR_ASTERISK
827 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
832 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
833 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
840 ast->tech_pvt = call;
841 ast->tech = &lcr_tech;
842 ast->fds[0] = call->pipe[0];
844 /* fill setup information */
845 if (param->setup.dialinginfo.id)
846 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
847 if (param->setup.context[0])
848 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
850 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
851 if (param->setup.callerinfo.id[0])
852 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
853 if (param->setup.callerinfo.name[0])
854 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
855 if (param->setup.redirinfo.id[0])
856 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
857 switch (param->setup.callerinfo.present) {
858 case INFO_PRESENT_ALLOWED:
859 ast->cid.cid_pres = AST_PRES_ALLOWED;
861 case INFO_PRESENT_RESTRICTED:
862 ast->cid.cid_pres = AST_PRES_RESTRICTED;
865 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
867 switch (param->setup.callerinfo.ntype) {
868 case INFO_NTYPE_SUBSCRIBER:
869 ast->cid.cid_ton = 4;
871 case INFO_NTYPE_NATIONAL:
872 ast->cid.cid_ton = 2;
874 case INFO_NTYPE_INTERNATIONAL:
875 ast->cid.cid_ton = 1;
878 ast->cid.cid_ton = 0;
880 ast->transfercapability = param->setup.capainfo.bearer_capa;
881 /* enable hdlc if transcap is data */
882 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
884 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
886 /* configure channel */
887 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
888 ast->readformat = ast->rawreadformat = ast->nativeformats;
889 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
891 ast->hangupcause = 0;
894 call->state = CHAN_LCR_STATE_IN_SETUP;
896 if (!call->pbx_started)
897 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
901 * incoming setup acknowledge from LCR
903 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
905 if (!call->ast) return;
907 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
909 /* send pending digits in dialque */
910 if (call->dialque[0])
911 send_dialque_to_lcr(call);
912 /* change to overlap state */
913 call->state = CHAN_LCR_STATE_OUT_DIALING;
917 * incoming proceeding from LCR
919 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
921 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
924 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
925 /* queue event for asterisk */
926 if (call->ast && call->pbx_started)
927 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
931 * incoming alerting from LCR
933 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
935 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
938 call->state = CHAN_LCR_STATE_OUT_ALERTING;
939 /* queue event to asterisk */
940 if (call->ast && call->pbx_started)
941 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
945 * incoming connect from LCR
947 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
949 union parameter newparam;
951 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
954 call->state = CHAN_LCR_STATE_CONNECT;
955 /* request bchannel */
956 if (!call->bchannel) {
957 CDEBUG(call, call->ast, "Requesting B-channel.\n");
958 memset(&newparam, 0, sizeof(union parameter));
959 newparam.bchannel.type = BCHANNEL_REQUEST;
960 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
962 /* copy connectinfo */
963 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
964 /* queue event to asterisk */
965 if (call->ast && call->pbx_started)
966 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
970 * incoming disconnect from LCR
972 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
974 struct ast_channel *ast = call->ast;
976 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
979 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
981 call->cause = param->disconnectinfo.cause;
982 call->location = param->disconnectinfo.location;
983 /* if bridge, forward disconnect and return */
986 if (call->bridge_call) {
987 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
988 bridge_message_if_bridged(call, message_type, param);
992 /* release lcr with same cause */
993 send_release_and_import(call, call->cause, call->location);
995 /* change to release state */
996 call->state = CHAN_LCR_STATE_RELEASE;
997 /* queue release asterisk */
999 ast->hangupcause = call->cause;
1000 if (call->pbx_started)
1001 strcpy(call->queue_string, "H"); // overwrite other indications
1003 ast_hangup(ast); // call will be destroyed here
1009 * incoming release from LCR
1011 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1013 struct ast_channel *ast = call->ast;
1015 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1019 /* change to release state */
1020 call->state = CHAN_LCR_STATE_RELEASE;
1021 /* copy release info */
1023 call->cause = param->disconnectinfo.cause;
1024 call->location = param->disconnectinfo.location;
1026 /* if we have an asterisk instance, queue hangup, else we are done */
1028 ast->hangupcause = call->cause;
1029 if (call->pbx_started)
1030 strcpy(call->queue_string, "H");
1032 ast_hangup(ast); // call will be destroyed here
1041 * incoming information from LCR
1043 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1045 struct ast_channel *ast = call->ast;
1047 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1051 /* pbx not started */
1052 if (!call->pbx_started) {
1053 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1054 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1055 lcr_start_pbx(call, ast, param->information.sending_complete);
1059 /* change dailing state after setup */
1060 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1061 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1062 call->state = CHAN_LCR_STATE_IN_DIALING;
1063 // ast_setstate(ast, AST_STATE_DIALING);
1067 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
1068 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1070 /* use bridge to forware message not supported by asterisk */
1071 if (call->state == CHAN_LCR_STATE_CONNECT) {
1072 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1073 bridge_message_if_bridged(call, message_type, param);
1078 * incoming information from LCR
1080 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1082 union parameter newparam;
1084 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1086 /* request bchannel, if call is resumed and we don't have it */
1087 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1088 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1089 memset(&newparam, 0, sizeof(union parameter));
1090 newparam.bchannel.type = BCHANNEL_REQUEST;
1091 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1094 if (!call->ast) return;
1096 /* use bridge to forware message not supported by asterisk */
1097 bridge_message_if_bridged(call, message_type, param);
1101 * incoming information from LCR
1103 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1105 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1107 if (!call->ast) return;
1109 /* use bridge to forware message not supported by asterisk */
1110 bridge_message_if_bridged(call, message_type, param);
1114 * incoming pattern from LCR
1116 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1118 union parameter newparam;
1120 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1122 if (!call->ast) return;
1124 /* pattern are indicated only once */
1125 if (call->has_pattern)
1127 call->has_pattern = 1;
1129 /* request bchannel */
1130 if (!call->bchannel) {
1131 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1132 memset(&newparam, 0, sizeof(union parameter));
1133 newparam.bchannel.type = BCHANNEL_REQUEST;
1134 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1136 /* queue PROGRESS, because tones are available */
1137 if (call->ast && call->pbx_started)
1138 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1142 * got dtmf from bchannel (locked state)
1144 void lcr_in_dtmf(struct chan_call *call, int val)
1146 struct ast_channel *ast = call->ast;
1151 if (!call->pbx_started)
1154 if (!call->dsp_dtmf) {
1155 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1159 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1162 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1166 * message received from LCR
1168 int receive_message(int message_type, unsigned int ref, union parameter *param)
1170 struct bchannel *bchannel;
1171 struct chan_call *call;
1172 union parameter newparam;
1174 memset(&newparam, 0, sizeof(union parameter));
1176 /* handle bchannel message*/
1177 if (message_type == MESSAGE_BCHANNEL) {
1178 switch(param->bchannel.type) {
1179 case BCHANNEL_ASSIGN:
1180 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1181 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1182 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1185 /* create bchannel */
1186 bchannel = alloc_bchannel(param->bchannel.handle);
1188 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1192 /* configure channel */
1193 bchannel->b_tx_gain = param->bchannel.tx_gain;
1194 bchannel->b_rx_gain = param->bchannel.rx_gain;
1195 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1196 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1197 bchannel->b_bf_len = param->bchannel.crypt_len;
1198 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1200 bchannel->b_txdata = 0;
1201 bchannel->b_tx_dejitter = 1;
1203 /* in case, ref is not set, this bchannel instance must
1204 * be created until it is removed again by LCR */
1206 call = find_call_ref(ref);
1208 bchannel->call = call;
1209 call->bchannel = bchannel;
1211 bchannel_dtmf(bchannel, 1);
1213 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1214 if (call->pipeline[0])
1215 bchannel_pipeline(bchannel, call->pipeline);
1217 bchannel_gain(bchannel, call->rx_gain, 0);
1219 bchannel_gain(bchannel, call->tx_gain, 1);
1220 if (call->bridge_id) {
1221 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1222 bchannel_join(bchannel, call->bridge_id);
1224 /* create only, if call exists, othewhise it bchannel is freed below... */
1225 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0)))
1226 bchannel_activate(bchannel, 1);
1229 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1230 newparam.bchannel.handle = param->bchannel.handle;
1231 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1232 /* if call has released before bchannel is assigned */
1234 newparam.bchannel.type = BCHANNEL_RELEASE;
1235 newparam.bchannel.handle = param->bchannel.handle;
1236 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1241 case BCHANNEL_REMOVE:
1242 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1243 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1244 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1247 /* unklink from call and destroy bchannel */
1248 free_bchannel(bchannel);
1251 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1252 newparam.bchannel.handle = param->bchannel.handle;
1253 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1258 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1263 /* handle new ref */
1264 if (message_type == MESSAGE_NEWREF) {
1265 if (param->direction) {
1266 /* new ref from lcr */
1267 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1268 if (!ref || find_call_ref(ref)) {
1269 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1272 /* allocate new call instance */
1273 call = alloc_call();
1275 call->state = CHAN_LCR_STATE_IN_PREPARE;
1278 call->ref_was_assigned = 1;
1279 /* set dtmf (default, use option 'n' to disable */
1281 /* wait for setup (or release from asterisk) */
1283 /* new ref, as requested from this remote application */
1284 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1285 call = find_call_ref(0);
1287 /* send release, if ref does not exist */
1288 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1289 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1294 call->ref_was_assigned = 1;
1295 /* send pending setup info */
1296 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1297 send_setup_to_lcr(call);
1298 /* release if asterisk has signed off */
1299 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1302 send_release_and_import(call, call->cause, call->location);
1304 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1315 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1318 call = find_call_ref(ref);
1320 /* ignore ref that is not used (anymore) */
1321 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1325 /* handle messages */
1326 switch(message_type) {
1328 lcr_in_setup(call, message_type, param);
1331 case MESSAGE_OVERLAP:
1332 lcr_in_overlap(call, message_type, param);
1335 case MESSAGE_PROCEEDING:
1336 lcr_in_proceeding(call, message_type, param);
1339 case MESSAGE_ALERTING:
1340 lcr_in_alerting(call, message_type, param);
1343 case MESSAGE_CONNECT:
1344 lcr_in_connect(call, message_type, param);
1347 case MESSAGE_DISCONNECT:
1348 lcr_in_disconnect(call, message_type, param);
1351 case MESSAGE_RELEASE:
1352 lcr_in_release(call, message_type, param);
1355 case MESSAGE_INFORMATION:
1356 lcr_in_information(call, message_type, param);
1359 case MESSAGE_NOTIFY:
1360 lcr_in_notify(call, message_type, param);
1363 case MESSAGE_FACILITY:
1364 lcr_in_facility(call, message_type, param);
1367 case MESSAGE_PATTERN: // audio available from LCR
1368 if (!call->has_pattern)
1369 lcr_in_pattern(call, message_type, param);
1372 case MESSAGE_NOPATTERN: // audio not available from LCR
1375 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1376 call->audiopath = param->audiopath;
1380 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1387 * release all calls (due to broken socket)
1389 static void release_all_calls(void)
1391 struct chan_call *call;
1396 /* no ast, so we may directly free call */
1398 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1402 /* already in release process */
1403 if (call->state == CHAN_LCR_STATE_RELEASE) {
1407 /* release or queue release */
1409 call->state = CHAN_LCR_STATE_RELEASE;
1410 if (!call->pbx_started) {
1411 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1412 ast_hangup(call->ast); // call will be destroyed here
1415 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1416 strcpy(call->queue_string, "H");
1420 /* release all bchannels */
1421 while(bchannel_first)
1422 free_bchannel(bchannel_first);
1427 * warning! not thread safe
1428 * returns -1 for socket error, 0 for no work, 1 for work
1430 int handle_socket(void)
1434 struct admin_list *admin;
1435 struct admin_message msg;
1437 /* read from socket */
1438 len = read(lcr_sock, &msg, sizeof(msg));
1440 CERROR(NULL, NULL, "Socket closed.\n");
1441 return(-1); // socket closed
1444 if (len != sizeof(msg)) {
1445 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1446 return(-1); // socket error
1448 if (msg.message != ADMIN_MESSAGE) {
1449 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1452 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1455 if (errno != EWOULDBLOCK) {
1456 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1461 /* write to socket */
1464 admin = admin_first;
1465 len = write(lcr_sock, &admin->msg, sizeof(msg));
1467 CERROR(NULL, NULL, "Socket closed.\n");
1468 return(-1); // socket closed
1471 if (len != sizeof(msg)) {
1472 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1473 return(-1); // socket error
1476 admin_first = admin->next;
1481 if (errno != EWOULDBLOCK) {
1482 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1491 * open and close socket and thread
1493 int open_socket(void)
1497 struct sockaddr_un sock_address;
1498 unsigned int on = 1;
1499 union parameter param;
1502 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1503 CERROR(NULL, NULL, "Failed to create socket.\n");
1507 /* set socket address and name */
1508 memset(&sock_address, 0, sizeof(sock_address));
1509 sock_address.sun_family = PF_UNIX;
1510 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1512 /* connect socket */
1513 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1516 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1520 /* set non-blocking io */
1521 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0) {
1524 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1528 /* enque hello message */
1529 memset(¶m, 0, sizeof(param));
1530 strcpy(param.hello.application, "asterisk");
1531 send_message(MESSAGE_HELLO, 0, ¶m);
1536 void close_socket(void)
1538 struct admin_list *admin, *temp;
1540 /* flush pending messages */
1541 admin = admin_first;
1544 admin = admin->next;
1556 /* sending queue to asterisk */
1557 static int queue_send(void)
1560 struct chan_call *call;
1561 struct ast_channel *ast;
1562 struct ast_frame fr;
1567 p = call->queue_string;
1570 /* there is something to queue */
1571 if (!ast_channel_trylock(ast)) { /* succeed */
1575 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1576 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1579 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1580 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1583 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1584 ast_queue_control(ast, AST_CONTROL_RINGING);
1585 ast_setstate(ast, AST_STATE_RINGING);
1588 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1589 ast_queue_control(ast, AST_CONTROL_ANSWER);
1592 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1593 ast_queue_hangup(ast);
1595 case '1': case '2': case '3': case 'A':
1596 case '4': case '5': case '6': case 'B':
1597 case '7': case '8': case '9': case 'C':
1598 case '*': case '0': case '#': case 'D':
1599 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1600 /* send digit to asterisk */
1601 memset(&fr, 0, sizeof(fr));
1603 #ifdef LCR_FOR_ASTERISK
1604 fr.frametype = AST_FRAME_DTMF_BEGIN;
1607 #ifdef LCR_FOR_CALLWEAVER
1608 fr.frametype = AST_FRAME_DTMF;
1612 fr.delivery = ast_tv(0, 0);
1613 ast_queue_frame(ast, &fr);
1615 #ifdef LCR_FOR_ASTERISK
1616 fr.frametype = AST_FRAME_DTMF_END;
1617 ast_queue_frame(ast, &fr);
1622 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1626 call->queue_string[0] = '\0';
1627 ast_channel_unlock(ast);
1637 /* signal handler */
1638 void sighandler(int sigset)
1642 /* chan_lcr thread */
1643 static void *chan_thread(void *arg)
1647 union parameter param;
1648 time_t retry = 0, now;
1650 bchannel_pid = getpid();
1652 // signal(SIGPIPE, sighandler);
1654 memset(¶m, 0, sizeof(union parameter));
1658 ast_mutex_lock(&chan_lock);
1665 ret = handle_socket();
1667 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1669 release_all_calls();
1676 if (retry && now-retry > 5) {
1677 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1679 if (open_socket() < 0) {
1688 ret = bchannel_handle();
1692 /* handle messages to asterisk */
1697 /* delay if no work done */
1699 ast_mutex_unlock(&chan_lock);
1701 #ifdef LCR_FOR_ASTERISK
1705 #ifdef LCR_FOR_CALLWEAVER
1709 ast_mutex_lock(&chan_lock);
1715 CERROR(NULL, NULL, "Thread exit.\n");
1717 ast_mutex_unlock(&chan_lock);
1719 // signal(SIGPIPE, SIG_DFL);
1725 * new asterisk instance
1728 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1730 char exten[256], *dial, *interface, *opt;
1731 struct ast_channel *ast;
1732 struct chan_call *call;
1734 ast_mutex_lock(&chan_lock);
1735 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1737 /* if socket is closed */
1739 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1740 ast_mutex_unlock(&chan_lock);
1744 /* create call instance */
1745 call = alloc_call();
1747 /* failed to create instance */
1748 ast_mutex_unlock(&chan_lock);
1752 /* create asterisk channel instrance */
1754 #ifdef LCR_FOR_ASTERISK
1755 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1758 #ifdef LCR_FOR_CALLWEAVER
1759 ast = ast_channel_alloc(1);
1763 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1765 /* failed to create instance */
1766 ast_mutex_unlock(&chan_lock);
1769 ast->tech = &lcr_tech;
1770 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1771 /* configure channel */
1772 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1773 ast->readformat = ast->rawreadformat = ast->nativeformats;
1774 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1776 ast->hangupcause = 0;
1780 ast->tech_pvt = call;
1781 ast->fds[0] = call->pipe[0];
1782 call->pbx_started = 0;
1784 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1787 * Extract interface, dialstring, options from data.
1790 * <interface>/<dialstring>
1791 * <interface>/<dialstring>/options
1793 strncpy(exten, (char *)data, sizeof(exten)-1);
1794 exten[sizeof(exten)-1] = '\0';
1795 if ((dial = strchr(exten, '/'))) {
1798 if ((opt = strchr(dial, '/')))
1807 strncpy(call->interface, interface, sizeof(call->interface)-1);
1808 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1809 apply_opt(call, (char *)opt);
1811 ast_mutex_unlock(&chan_lock);
1816 * call from asterisk
1818 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1820 union parameter newparam;
1821 struct chan_call *call;
1823 ast_mutex_lock(&chan_lock);
1824 call = ast->tech_pvt;
1826 #ifdef LCR_FOR_CALLWEAVER
1828 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1832 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1833 ast_mutex_unlock(&chan_lock);
1837 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1839 /* pbx process is started */
1840 call->pbx_started = 1;
1841 /* send MESSAGE_NEWREF */
1842 memset(&newparam, 0, sizeof(union parameter));
1843 newparam.direction = 0; /* request from app */
1844 send_message(MESSAGE_NEWREF, 0, &newparam);
1846 /* set hdlc if capability requires hdlc */
1847 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1848 || ast->transfercapability == INFO_BC_DATARESTRICTED
1849 || ast->transfercapability == INFO_BC_VIDEO)
1851 /* if hdlc is forced by option, we change transcap to data */
1853 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1854 && ast->transfercapability != INFO_BC_DATARESTRICTED
1855 && ast->transfercapability != INFO_BC_VIDEO)
1856 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1858 call->cid_num[0] = 0;
1859 call->cid_name[0] = 0;
1860 call->cid_rdnis[0] = 0;
1862 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1863 strncpy(call->cid_num, ast->cid.cid_num,
1864 sizeof(call->cid_num)-1);
1866 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1867 strncpy(call->cid_name, ast->cid.cid_name,
1868 sizeof(call->cid_name)-1);
1869 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1870 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1871 sizeof(call->cid_rdnis)-1);
1873 ast_mutex_unlock(&chan_lock);
1877 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1879 static const char* dtmf_tones[] = {
1880 "!941+1336/100,!0/100", /* 0 */
1881 "!697+1209/100,!0/100", /* 1 */
1882 "!697+1336/100,!0/100", /* 2 */
1883 "!697+1477/100,!0/100", /* 3 */
1884 "!770+1209/100,!0/100", /* 4 */
1885 "!770+1336/100,!0/100", /* 5 */
1886 "!770+1477/100,!0/100", /* 6 */
1887 "!852+1209/100,!0/100", /* 7 */
1888 "!852+1336/100,!0/100", /* 8 */
1889 "!852+1477/100,!0/100", /* 9 */
1890 "!697+1633/100,!0/100", /* A */
1891 "!770+1633/100,!0/100", /* B */
1892 "!852+1633/100,!0/100", /* C */
1893 "!941+1633/100,!0/100", /* D */
1894 "!941+1209/100,!0/100", /* * */
1895 "!941+1477/100,!0/100" }; /* # */
1897 if (digit >= '0' && digit <='9')
1898 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1899 else if (digit >= 'A' && digit <= 'D')
1900 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1901 else if (digit == '*')
1902 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1903 else if (digit == '#')
1904 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1907 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1908 "'%c' for '%s'\n", digit, ast->name);
1912 #ifdef LCR_FOR_ASTERISK
1913 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1915 #ifdef LCR_FOR_CALLWEAVER
1916 static int lcr_digit(struct ast_channel *ast, char digit)
1919 struct chan_call *call;
1920 union parameter newparam;
1923 #ifdef LCR_FOR_CALLWEAVER
1924 int inband_dtmf = 0;
1927 /* only pass IA5 number space */
1928 if (digit > 126 || digit < 32)
1931 ast_mutex_lock(&chan_lock);
1932 call = ast->tech_pvt;
1934 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1935 ast_mutex_unlock(&chan_lock);
1939 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1941 /* send information or queue them */
1942 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
1943 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1944 memset(&newparam, 0, sizeof(union parameter));
1946 newparam.information.keypad[0] = digit;
1947 newparam.information.keypad[1] = '\0';
1949 newparam.information.id[0] = digit;
1950 newparam.information.id[1] = '\0';
1952 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1955 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
1956 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1958 strncat(call->dialque, buf, strlen(call->dialque)-1);
1961 ast_mutex_unlock(&chan_lock);
1963 #ifdef LCR_FOR_ASTERISK
1967 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1969 int inband_dtmf = 0;
1970 struct chan_call *call;
1973 ast_mutex_lock(&chan_lock);
1975 call = ast->tech_pvt;
1979 "Received digit from Asterisk, "
1980 "but no call instance exists.\n");
1981 ast_mutex_unlock(&chan_lock);
1985 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
1987 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
1991 ast_mutex_unlock(&chan_lock);
1994 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
1995 send_digit_to_chan(ast, digit);
2001 static int lcr_answer(struct ast_channel *ast)
2003 union parameter newparam;
2004 struct chan_call *call;
2006 ast_mutex_lock(&chan_lock);
2007 call = ast->tech_pvt;
2009 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2010 ast_mutex_unlock(&chan_lock);
2014 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2016 /* copy connectinfo, if bridged */
2017 if (call->bridge_call)
2018 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2019 /* send connect message to lcr */
2020 if (call->state != CHAN_LCR_STATE_CONNECT) {
2021 memset(&newparam, 0, sizeof(union parameter));
2022 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2023 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2024 call->state = CHAN_LCR_STATE_CONNECT;
2027 /* request bchannel */
2028 if (!call->bchannel) {
2029 CDEBUG(call, ast, "Requesting B-channel.\n");
2030 memset(&newparam, 0, sizeof(union parameter));
2031 newparam.bchannel.type = BCHANNEL_REQUEST;
2032 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2035 // memset(&newparam, 0, sizeof(union parameter));
2036 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2038 ast_mutex_unlock(&chan_lock);
2042 static int lcr_hangup(struct ast_channel *ast)
2044 struct chan_call *call;
2045 pthread_t tid = pthread_self();
2047 if (!pthread_equal(tid, chan_tid))
2048 ast_mutex_lock(&chan_lock);
2049 call = ast->tech_pvt;
2051 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2052 if (!pthread_equal(tid, chan_tid))
2053 ast_mutex_unlock(&chan_lock);
2057 if (!pthread_equal(tid, chan_tid))
2058 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2060 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2062 /* disconnect asterisk, maybe not required */
2063 ast->tech_pvt = NULL;
2067 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2068 if (ast->hangupcause > 0)
2069 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2071 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2074 if (!pthread_equal(tid, chan_tid))
2075 ast_mutex_unlock(&chan_lock);
2078 /* ref is not set, due to prepare setup or release */
2079 if (call->state == CHAN_LCR_STATE_RELEASE) {
2080 /* we get the response to our release */
2081 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2084 /* during prepare, we change to release state */
2085 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2086 call->state = CHAN_LCR_STATE_RELEASE;
2090 if (!pthread_equal(tid, chan_tid))
2091 ast_mutex_unlock(&chan_lock);
2095 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2097 struct chan_call *call;
2100 CDEBUG(NULL, ast, "No subclass\n");
2101 if (!(f->subclass & ast->nativeformats))
2102 CDEBUG(NULL, ast, "Unexpected format.\n");
2104 ast_mutex_lock(&chan_lock);
2105 call = ast->tech_pvt;
2107 ast_mutex_unlock(&chan_lock);
2110 if (call->bchannel && f->samples)
2111 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2112 ast_mutex_unlock(&chan_lock);
2117 static struct ast_frame *lcr_read(struct ast_channel *ast)
2119 struct chan_call *call;
2122 ast_mutex_lock(&chan_lock);
2123 call = ast->tech_pvt;
2125 ast_mutex_unlock(&chan_lock);
2128 if (call->pipe[0] > -1) {
2129 if (call->rebuffer && !call->hdlc) {
2130 /* Make sure we have a complete 20ms (160byte) frame */
2131 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2133 call->framepos += len;
2136 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2138 if (len < 0 && errno == EAGAIN) {
2139 ast_mutex_unlock(&chan_lock);
2141 #ifdef LCR_FOR_ASTERISK
2142 return &ast_null_frame;
2145 #ifdef LCR_FOR_CALLWEAVER
2151 close(call->pipe[0]);
2153 ast_mutex_unlock(&chan_lock);
2155 } else if (call->rebuffer && call->framepos < 160) {
2156 /* Not a complete frame, so we send a null-frame */
2157 ast_mutex_unlock(&chan_lock);
2158 return &ast_null_frame;
2162 call->read_fr.frametype = AST_FRAME_VOICE;
2163 call->read_fr.subclass = ast->nativeformats;
2164 if (call->rebuffer) {
2165 call->read_fr.datalen = call->framepos;
2166 call->read_fr.samples = call->framepos;
2169 call->read_fr.datalen = len;
2170 call->read_fr.samples = len;
2172 call->read_fr.delivery = ast_tv(0,0);
2173 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2174 ast_mutex_unlock(&chan_lock);
2176 return &call->read_fr;
2179 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2181 union parameter newparam;
2183 struct chan_call *call;
2185 ast_mutex_lock(&chan_lock);
2186 call = ast->tech_pvt;
2188 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2189 ast_mutex_unlock(&chan_lock);
2194 case AST_CONTROL_BUSY:
2195 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2196 ast_setstate(ast, AST_STATE_BUSY);
2197 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2198 /* send message to lcr */
2199 memset(&newparam, 0, sizeof(union parameter));
2200 newparam.disconnectinfo.cause = 17;
2201 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2202 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2204 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2207 case AST_CONTROL_CONGESTION:
2208 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2209 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2210 /* send message to lcr */
2211 memset(&newparam, 0, sizeof(union parameter));
2212 newparam.disconnectinfo.cause = ast->hangupcause;
2213 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2214 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2216 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2219 case AST_CONTROL_PROCEEDING:
2220 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2221 if (call->state == CHAN_LCR_STATE_IN_SETUP
2222 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2223 /* send message to lcr */
2224 memset(&newparam, 0, sizeof(union parameter));
2225 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2227 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2230 case AST_CONTROL_RINGING:
2231 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2232 ast_setstate(ast, AST_STATE_RING);
2233 if (call->state == CHAN_LCR_STATE_IN_SETUP
2234 || call->state == CHAN_LCR_STATE_IN_DIALING
2235 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2236 /* send message to lcr */
2237 memset(&newparam, 0, sizeof(union parameter));
2238 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2240 call->state = CHAN_LCR_STATE_IN_ALERTING;
2243 case AST_CONTROL_PROGRESS:
2244 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2245 /* request bchannel */
2246 if (!call->bchannel) {
2247 CDEBUG(call, ast, "Requesting B-channel.\n");
2248 memset(&newparam, 0, sizeof(union parameter));
2249 newparam.bchannel.type = BCHANNEL_REQUEST;
2250 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2254 CDEBUG(call, ast, "Received indicate -1.\n");
2258 case AST_CONTROL_VIDUPDATE:
2259 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2262 case AST_CONTROL_HOLD:
2263 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2264 /* send message to lcr */
2265 memset(&newparam, 0, sizeof(union parameter));
2266 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2267 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2269 /*start music onhold*/
2270 #ifdef LCR_FOR_ASTERISK
2271 ast_moh_start(ast,data,ast->musicclass);
2274 #ifdef LCR_FOR_CALLWEAVER
2275 ast_moh_start(ast, NULL);
2280 case AST_CONTROL_UNHOLD:
2281 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2282 /* send message to lcr */
2283 memset(&newparam, 0, sizeof(union parameter));
2284 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2285 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2291 #ifdef AST_CONTROL_SRCUPDATE
2292 case AST_CONTROL_SRCUPDATE:
2293 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2297 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2303 ast_mutex_unlock(&chan_lock);
2310 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2312 struct chan_call *call;
2318 ast_mutex_lock(&chan_lock);
2319 call = ast->tech_pvt;
2321 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2322 ast_mutex_unlock(&chan_lock);
2326 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2328 ast_mutex_unlock(&chan_lock);
2333 * send_text asterisk
2335 static int lcr_send_text(struct ast_channel *ast, const char *text)
2337 struct chan_call *call;
2338 union parameter newparam;
2340 ast_mutex_lock(&chan_lock);
2341 call = ast->tech_pvt;
2343 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2344 ast_mutex_unlock(&chan_lock);
2348 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2349 memset(&newparam, 0, sizeof(union parameter));
2350 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2351 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2352 ast_mutex_lock(&chan_lock);
2359 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2360 struct ast_channel *ast2, int flags,
2361 struct ast_frame **fo,
2362 struct ast_channel **rc, int timeoutms)
2365 struct chan_call *call1, *call2;
2366 struct ast_channel *carr[2], *who;
2368 struct ast_frame *f;
2371 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2376 /* join via dsp (if the channels are currently open) */
2377 ast_mutex_lock(&chan_lock);
2378 call1 = ast1->tech_pvt;
2379 call2 = ast2->tech_pvt;
2380 if (!call1 || !call2) {
2381 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2382 ast_mutex_unlock(&chan_lock);
2383 return AST_BRIDGE_COMPLETE;
2386 /* join, if both call instances uses dsp
2387 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2389 if (!call1->nodsp && !call2->nodsp) {
2390 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2392 /* get bridge id and join */
2393 bridge_id = new_bridge_id();
2395 call1->bridge_id = bridge_id;
2396 if (call1->bchannel)
2397 bchannel_join(call1->bchannel, bridge_id);
2399 call2->bridge_id = bridge_id;
2400 if (call2->bchannel)
2401 bchannel_join(call2->bchannel, bridge_id);
2403 if (call1->nodsp && call2->nodsp)
2404 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2406 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2407 call1->bridge_call = call2;
2408 call2->bridge_call = call1;
2410 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2411 || call1->state == CHAN_LCR_STATE_IN_DIALING
2412 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2413 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2414 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2417 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2418 || call2->state == CHAN_LCR_STATE_IN_DIALING
2419 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2420 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2421 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2425 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2426 so let's do it for them. Hmpf.
2429 if (call1->on_hold) {
2430 union parameter newparam;
2432 memset(&newparam, 0, sizeof(union parameter));
2433 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2434 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2439 if (call2->on_hold) {
2440 union parameter newparam;
2442 memset(&newparam, 0, sizeof(union parameter));
2443 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2444 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2449 ast_mutex_unlock(&chan_lock);
2453 who = ast_waitfor_n(carr, 2, &to);
2456 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2461 if (!f || f->frametype == AST_FRAME_CONTROL) {
2463 CDEBUG(NULL, NULL, "Got hangup.\n");
2465 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2472 if ( f->frametype == AST_FRAME_DTMF ) {
2473 CDEBUG(NULL, NULL, "Got DTMF.\n");
2489 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2491 /* split channels */
2492 ast_mutex_lock(&chan_lock);
2493 call1 = ast1->tech_pvt;
2494 call2 = ast2->tech_pvt;
2495 if (call1 && call1->bridge_id) {
2496 call1->bridge_id = 0;
2497 if (call1->bchannel)
2498 bchannel_join(call1->bchannel, 0);
2499 if (call1->bridge_call)
2500 call1->bridge_call->bridge_call = NULL;
2502 if (call2 && call1->bridge_id) {
2503 call2->bridge_id = 0;
2504 if (call2->bchannel)
2505 bchannel_join(call2->bchannel, 0);
2506 if (call2->bridge_call)
2507 call2->bridge_call->bridge_call = NULL;
2509 call1->bridge_call = NULL;
2510 call2->bridge_call = NULL;
2512 ast_mutex_unlock(&chan_lock);
2513 return AST_BRIDGE_COMPLETE;
2515 static struct ast_channel_tech lcr_tech = {
2517 .description = "Channel driver for connecting to Linux-Call-Router",
2518 .capabilities = AST_FORMAT_ALAW,
2519 .requester = lcr_request,
2521 #ifdef LCR_FOR_ASTERISK
2522 .send_digit_begin = lcr_digit_begin,
2523 .send_digit_end = lcr_digit_end,
2526 #ifdef LCR_FOR_CALLWEAVER
2527 .send_digit = lcr_digit,
2531 .bridge = lcr_bridge,
2532 .hangup = lcr_hangup,
2533 .answer = lcr_answer,
2536 .indicate = lcr_indicate,
2538 .send_text = lcr_send_text,
2547 static int lcr_show_lcr (int fd, int argc, char *argv[])
2552 static int lcr_show_calls (int fd, int argc, char *argv[])
2557 static int lcr_reload_routing (int fd, int argc, char *argv[])
2562 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2567 static int lcr_port_block (int fd, int argc, char *argv[])
2572 static int lcr_port_unblock (int fd, int argc, char *argv[])
2577 static int lcr_port_unload (int fd, int argc, char *argv[])
2582 static struct ast_cli_entry cli_show_lcr =
2583 { {"lcr", "show", "lcr", NULL},
2585 "Shows current states of LCR core",
2586 "Usage: lcr show lcr\n",
2589 static struct ast_cli_entry cli_show_calls =
2590 { {"lcr", "show", "calls", NULL},
2592 "Shows current calls made by LCR and Asterisk",
2593 "Usage: lcr show calls\n",
2596 static struct ast_cli_entry cli_reload_routing =
2597 { {"lcr", "reload", "routing", NULL},
2599 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2600 "Usage: lcr reload routing\n",
2603 static struct ast_cli_entry cli_reload_interfaces =
2604 { {"lcr", "reload", "interfaces", NULL},
2605 lcr_reload_interfaces,
2606 "Reloads interfaces conf of LCR",
2607 "Usage: lcr reload interfaces\n",
2610 static struct ast_cli_entry cli_port_block =
2611 { {"lcr", "port", "block", NULL},
2613 "Blocks LCR port for further calls",
2614 "Usage: lcr port block \"<port>\"\n",
2617 static struct ast_cli_entry cli_port_unblock =
2618 { {"lcr", "port", "unblock", NULL},
2620 "Unblocks or loads LCR port, port is opened my mISDN",
2621 "Usage: lcr port unblock \"<port>\"\n",
2624 static struct ast_cli_entry cli_port_unload =
2625 { {"lcr", "port", "unload", NULL},
2627 "Unloads LCR port, port is closes by mISDN",
2628 "Usage: lcr port unload \"<port>\"\n",
2633 #ifdef LCR_FOR_ASTERISK
2634 static int lcr_config_exec(struct ast_channel *ast, void *data)
2637 #ifdef LCR_FOR_CALLWEAVER
2638 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2641 struct chan_call *call;
2643 ast_mutex_lock(&chan_lock);
2645 #ifdef LCR_FOR_ASTERISK
2646 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2649 #ifdef LCR_FOR_CALLWEAVER
2650 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2656 if (call->ast == ast)
2662 #ifdef LCR_FOR_ASTERISK
2663 apply_opt(call, (char *)data);
2666 #ifdef LCR_FOR_CALLWEAVER
2667 apply_opt(call, (char *)argv[0]);
2671 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2673 ast_mutex_unlock(&chan_lock);
2678 * module loading and destruction
2680 int load_module(void)
2684 for (i = 0; i < 256; i++) {
2685 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2686 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2689 if (read_options() == 0) {
2690 CERROR(NULL, NULL, "%s", options_error);
2692 #ifdef LCR_FOR_ASTERISK
2693 return AST_MODULE_LOAD_DECLINE;
2696 #ifdef LCR_FOR_CALLWEAVER
2702 ast_mutex_init(&chan_lock);
2703 ast_mutex_init(&log_lock);
2705 if (open_socket() < 0) {
2706 /* continue with closed socket */
2709 if (bchannel_initialize()) {
2710 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2713 #ifdef LCR_FOR_ASTERISK
2714 return AST_MODULE_LOAD_DECLINE;
2717 #ifdef LCR_FOR_CALLWEAVER
2723 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2724 if (ast_channel_register(&lcr_tech)) {
2725 CERROR(NULL, NULL, "Unable to register channel class\n");
2726 bchannel_deinitialize();
2729 #ifdef LCR_FOR_ASTERISK
2730 return AST_MODULE_LOAD_DECLINE;
2733 #ifdef LCR_FOR_CALLWEAVER
2738 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2740 #ifdef LCR_FOR_ASTERISK
2741 "lcr_config(<opt><optarg>:<opt>:...)\n"
2744 #ifdef LCR_FOR_CALLWEAVER
2745 "lcr_config(<opt><optarg>:<opt>:...)\n",
2748 "Sets LCR opts. and optargs\n"
2750 "The available options are:\n"
2751 " d - Send display text on called phone, text is the optarg.\n"
2752 " n - Don't detect dtmf tones on called channel.\n"
2753 " h - Force data call (HDLC).\n"
2754 " t - Disable mISDN_dsp features (required for fax application).\n"
2755 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2756 " Use time to detect for optarg.\n"
2757 " c - Make crypted outgoing call, optarg is keyindex.\n"
2758 " e - Perform echo cancelation on this channel.\n"
2759 " Takes mISDN pipeline option as optarg.\n"
2760 " s - Send Non Inband DTMF as inband.\n"
2761 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2762 " vr - rxgain control\n"
2763 " vt - txgain control\n"
2764 " Volume changes at factor 2 ^ optarg.\n"
2765 " k - use keypad to dial this call.\n"
2770 ast_cli_register(&cli_show_lcr);
2771 ast_cli_register(&cli_show_calls);
2772 ast_cli_register(&cli_reload_routing);
2773 ast_cli_register(&cli_reload_interfaces);
2774 ast_cli_register(&cli_port_block);
2775 ast_cli_register(&cli_port_unblock);
2776 ast_cli_register(&cli_port_unload);
2780 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
2781 /* failed to create thread */
2782 bchannel_deinitialize();
2784 ast_channel_unregister(&lcr_tech);
2786 #ifdef LCR_FOR_ASTERISK
2787 return AST_MODULE_LOAD_DECLINE;
2790 #ifdef LCR_FOR_CALLWEAVER
2798 int unload_module(void)
2800 /* First, take us out of the channel loop */
2801 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2804 pthread_join(chan_tid, NULL);
2806 ast_channel_unregister(&lcr_tech);
2808 ast_unregister_application("lcr_config");
2811 if (mISDN_created) {
2812 bchannel_deinitialize();
2816 if (lcr_sock >= 0) {
2824 int reload_module(void)
2830 #ifdef LCR_FOR_ASTERISK
2831 #define AST_MODULE "chan_lcr"
2834 #ifdef LCR_FOR_CALLWEAVER
2838 ast_mutex_lock(&usecnt_lock);
2840 ast_mutex_unlock(&usecnt_lock);
2845 #ifdef LCR_FOR_ASTERISK
2846 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2847 .load = load_module,
2848 .unload = unload_module,
2849 .reload = reload_module,
2853 #ifdef LCR_FOR_CALLWEAVER
2854 char *description(void)