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);
259 if (call->ref == ref && call->ref_was_assigned == assigned)
266 void free_call(struct chan_call *call)
268 struct chan_call **temp = &call_first;
274 *temp = (*temp)->next;
275 if (call->pipe[0] > -1)
276 close(call->pipe[0]);
277 if (call->pipe[1] > -1)
278 close(call->pipe[1]);
281 if (call->bchannel->call != call)
282 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
283 call->bchannel->call = NULL;
285 if (call->bridge_call)
287 if (call->bridge_call->bridge_call != call)
288 CERROR(call, NULL, "Linked call structure has no link to us.\n");
289 call->bridge_call->bridge_call = NULL;
292 ast_translator_free_path(call->trans);
294 ast_dsp_free(call->dsp);
295 CDEBUG(call, NULL, "Call instance freed.\n");
299 temp = &((*temp)->next);
301 CERROR(call, NULL, "Call instance not found in list.\n");
304 struct chan_call *alloc_call(void)
306 struct chan_call **callp = &call_first;
309 callp = &((*callp)->next);
311 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
313 memset(*callp, 0, sizeof(struct chan_call));
314 if (pipe((*callp)->pipe) < 0) {
315 CERROR(*callp, NULL, "Failed to create pipe.\n");
319 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
320 CDEBUG(*callp, NULL, "Call instance allocated.\n");
324 unsigned short new_bridge_id(void)
326 struct chan_call *call;
327 unsigned short id = 1;
329 /* search for lowest bridge id that is not in use and not 0 */
335 if (call->bridge_id == id)
343 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
348 * enque message to LCR
350 int send_message(int message_type, unsigned int ref, union parameter *param)
352 struct admin_list *admin, **adminp;
355 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
358 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
360 adminp = &admin_first;
362 adminp = &((*adminp)->next);
363 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
365 CERROR(NULL, NULL, "No memory for message to LCR.\n");
370 admin->msg.message = ADMIN_MESSAGE;
371 admin->msg.u.msg.type = message_type;
372 admin->msg.u.msg.ref = ref;
373 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
379 * apply options (in locked state)
381 void apply_opt(struct chan_call *call, char *data)
383 union parameter newparam;
384 char string[1024], *p = string, *opt, *key;
390 strncpy(string, data, sizeof(string)-1);
391 string[sizeof(string)-1] = '\0';
394 while((opt = strsep(&p, ":")))
398 if (opt[1] == '\0') {
399 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
402 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
403 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
404 strncpy(call->display, opt+1, sizeof(call->display)-1);
406 memset(&newparam, 0, sizeof(union parameter));
407 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
408 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
412 if (opt[1] != '\0') {
413 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
416 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
417 if (call->dsp_dtmf) {
420 bchannel_dtmf(call->bchannel, 0);
424 if (opt[1] == '\0') {
425 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
429 /* check for 0xXXXX... type of key */
430 if (!!strncmp((char *)key, "0x", 2)) {
431 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
435 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
436 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
442 if (*key>='0' && *key<='9')
443 call->bf_key[i] = (*key-'0') << 8;
444 else if (*key>='a' && *key<='f')
445 call->bf_key[i] = (*key-'a'+10) << 8;
446 else if (*key>='A' && *key<='F')
447 call->bf_key[i] = (*key-'A'+10) << 8;
451 if (*key>='0' && *key<='9')
452 call->bf_key[i] += (*key - '0');
453 else if (*key>='a' && *key<='f')
454 call->bf_key[i] += (*key - 'a' + 10);
455 else if (*key>='A' && *key<='F')
456 call->bf_key[i] += (*key - 'A' + 10);
463 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
467 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
469 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
472 if (opt[1] != '\0') {
473 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
476 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
481 if (opt[1] != '\0') {
482 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
485 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
490 if (opt[1] == '\0') {
491 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
494 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
495 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
497 bchannel_pipeline(call->bchannel, call->pipeline);
500 if (opt[1] == '\0') {
501 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
504 call->faxdetect=atoi(opt+1);
506 call->dsp=ast_dsp_new();
508 #ifdef LCR_FOR_CALLWEAVER
509 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
511 #ifdef LCR_FOR_ASTERISK
512 #ifdef DSP_FEATURE_DTMF_DETECT
513 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
515 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
520 #ifdef LCR_FOR_CALLWEAVER
521 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
523 #ifdef LCR_FOR_ASTERISK
524 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
527 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
530 if (opt[1] != '\0') {
531 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
534 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
539 if (opt[1] != '\0') {
540 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
543 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
544 call->inband_dtmf = 1;
547 if (opt[1] != 'r' && opt[1] != 't') {
548 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
552 if (gain < -8 || gain >8) {
553 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
556 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
558 call->rx_gain = gain;
560 bchannel_gain(call->bchannel, call->rx_gain, 0);
562 call->tx_gain = gain;
564 bchannel_gain(call->bchannel, call->tx_gain, 1);
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;
598 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
599 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
600 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
601 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
602 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
603 call->display[0] = '\0';
604 if (call->cid_num[0])
605 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
606 if (call->cid_name[0])
607 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
608 if (call->cid_rdnis[0])
610 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
611 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
612 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
614 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
616 case AST_PRES_RESTRICTED:
617 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
619 case AST_PRES_UNAVAILABLE:
620 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
622 case AST_PRES_ALLOWED:
624 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
626 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));
672 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
673 call->dialque[0] = '\0';
674 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
678 * in case of a bridge, the unsupported message can be forwarded directly
679 * to the remote call.
681 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
685 if (!call->bridge_call) return;
686 CDEBUG(call, NULL, "Sending message due bridging.\n");
687 send_message(message_type, call->bridge_call->ref, param);
691 * send release message to LCR and import bchannel if exported
693 static void send_release_and_import(struct chan_call *call, int cause, int location)
695 union parameter newparam;
697 /* importing channel */
698 if (call->bchannel) {
699 memset(&newparam, 0, sizeof(union parameter));
700 newparam.bchannel.type = BCHANNEL_RELEASE;
701 newparam.bchannel.handle = call->bchannel->handle;
702 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
704 /* sending release */
705 memset(&newparam, 0, sizeof(union parameter));
706 newparam.disconnectinfo.cause = cause;
707 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
708 send_message(MESSAGE_RELEASE, call->ref, &newparam);
712 * check if extension matches and start asterisk
713 * if it can match, proceed
716 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
719 union parameter newparam;
720 char *exten = ast->exten;
724 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
729 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad))
731 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
735 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))
754 /* send setup acknowledge to lcr */
755 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
756 memset(&newparam, 0, sizeof(union parameter));
757 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
761 call->state = CHAN_LCR_STATE_IN_DIALING;
763 /* if match, start pbx */
764 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
765 CDEBUG(call, ast, "Extensions matches.\n");
770 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
776 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
784 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
785 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
787 /* release asterisk */
788 ast->hangupcause = call->cause;
789 /* change to release state */
790 call->state = CHAN_LCR_STATE_RELEASE;
791 ast_hangup(ast); // call will be destroyed here
795 /* send setup to asterisk */
796 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
798 #ifdef LCR_FOR_CALLWEAVER
800 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",ast->cid.cid_num, ast_random() & 0xffff);
803 ret = ast_pbx_start(ast);
806 cause = (ret==-2)?34:27;
809 call->pbx_started = 1;
810 ast_setstate(ast, AST_STATE_RING);
814 * incoming setup from LCR
816 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
818 struct ast_channel *ast;
820 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
822 /* create asterisk channel instrance */
824 #ifdef LCR_FOR_CALLWEAVER
825 ast = ast_channel_alloc(1);
828 #ifdef LCR_FOR_ASTERISK
829 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
835 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
836 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
843 ast->tech_pvt = call;
844 ast->tech = &lcr_tech;
845 ast->fds[0] = call->pipe[0];
847 /* fill setup information */
848 if (param->setup.dialinginfo.id)
849 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
850 if (param->setup.context[0])
851 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
853 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
854 if (param->setup.callerinfo.id[0])
855 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
856 if (param->setup.callerinfo.name[0])
857 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
858 if (param->setup.redirinfo.id[0])
859 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
860 switch (param->setup.callerinfo.present)
862 case INFO_PRESENT_ALLOWED:
863 ast->cid.cid_pres = AST_PRES_ALLOWED;
865 case INFO_PRESENT_RESTRICTED:
866 ast->cid.cid_pres = AST_PRES_RESTRICTED;
869 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
871 switch (param->setup.callerinfo.ntype)
873 case INFO_NTYPE_SUBSCRIBER:
874 ast->cid.cid_ton = 4;
876 case INFO_NTYPE_NATIONAL:
877 ast->cid.cid_ton = 2;
879 case INFO_NTYPE_INTERNATIONAL:
880 ast->cid.cid_ton = 1;
883 ast->cid.cid_ton = 0;
885 ast->transfercapability = param->setup.capainfo.bearer_capa;
886 /* enable hdlc if transcap is data */
887 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
889 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
891 /* configure channel */
892 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
893 ast->readformat = ast->rawreadformat = ast->nativeformats;
894 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
896 ast->hangupcause = 0;
899 call->state = CHAN_LCR_STATE_IN_SETUP;
901 if (!call->pbx_started)
902 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
906 * incoming setup acknowledge from LCR
908 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
910 if (!call->ast) return;
912 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
914 /* send pending digits in dialque */
915 if (call->dialque[0])
916 send_dialque_to_lcr(call);
917 /* change to overlap state */
918 call->state = CHAN_LCR_STATE_OUT_DIALING;
922 * incoming proceeding from LCR
924 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
926 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
929 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
930 /* queue event for asterisk */
931 if (call->ast && call->pbx_started)
932 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
936 * incoming alerting from LCR
938 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
940 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
943 call->state = CHAN_LCR_STATE_OUT_ALERTING;
944 /* queue event to asterisk */
945 if (call->ast && call->pbx_started)
946 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
950 * incoming connect from LCR
952 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
954 union parameter newparam;
956 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
959 call->state = CHAN_LCR_STATE_CONNECT;
960 /* request bchannel */
961 if (!call->bchannel) {
962 CDEBUG(call, call->ast, "Requesting B-channel.\n");
963 memset(&newparam, 0, sizeof(union parameter));
964 newparam.bchannel.type = BCHANNEL_REQUEST;
965 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
967 /* copy connectinfo */
968 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
969 /* queue event to asterisk */
970 if (call->ast && call->pbx_started)
971 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
975 * incoming disconnect from LCR
977 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
979 struct ast_channel *ast = call->ast;
981 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
984 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
986 call->cause = param->disconnectinfo.cause;
987 call->location = param->disconnectinfo.location;
988 /* if bridge, forward disconnect and return */
991 if (call->bridge_call)
993 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
994 bridge_message_if_bridged(call, message_type, param);
998 /* release lcr with same cause */
999 send_release_and_import(call, call->cause, call->location);
1001 /* change to release state */
1002 call->state = CHAN_LCR_STATE_RELEASE;
1003 /* queue release asterisk */
1006 ast->hangupcause = call->cause;
1007 if (call->pbx_started)
1008 strcpy(call->queue_string, "H"); // overwrite other indications
1010 ast_hangup(ast); // call will be destroyed here
1016 * incoming release from LCR
1018 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1020 struct ast_channel *ast = call->ast;
1022 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1026 /* change to release state */
1027 call->state = CHAN_LCR_STATE_RELEASE;
1028 /* copy release info */
1031 call->cause = param->disconnectinfo.cause;
1032 call->location = param->disconnectinfo.location;
1034 /* if we have an asterisk instance, queue hangup, else we are done */
1037 ast->hangupcause = call->cause;
1038 if (call->pbx_started)
1039 strcpy(call->queue_string, "H");
1041 ast_hangup(ast); // call will be destroyed here
1051 * incoming information from LCR
1053 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1055 struct ast_channel *ast = call->ast;
1057 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1061 /* pbx not started */
1062 if (!call->pbx_started)
1064 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1065 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1066 lcr_start_pbx(call, ast, param->information.sending_complete);
1070 /* change dailing state after setup */
1071 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1072 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1073 call->state = CHAN_LCR_STATE_IN_DIALING;
1074 // ast_setstate(ast, AST_STATE_DIALING);
1078 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
1079 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1081 /* use bridge to forware message not supported by asterisk */
1082 if (call->state == CHAN_LCR_STATE_CONNECT) {
1083 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1084 bridge_message_if_bridged(call, message_type, param);
1089 * incoming information from LCR
1091 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1093 union parameter newparam;
1095 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1097 /* request bchannel, if call is resumed and we don't have it */
1098 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1099 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1100 memset(&newparam, 0, sizeof(union parameter));
1101 newparam.bchannel.type = BCHANNEL_REQUEST;
1102 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1105 if (!call->ast) return;
1107 /* use bridge to forware message not supported by asterisk */
1108 bridge_message_if_bridged(call, message_type, param);
1112 * incoming information from LCR
1114 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1116 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1118 if (!call->ast) return;
1120 /* use bridge to forware message not supported by asterisk */
1121 bridge_message_if_bridged(call, message_type, param);
1125 * incoming pattern from LCR
1127 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1129 union parameter newparam;
1131 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1133 if (!call->ast) return;
1135 /* pattern are indicated only once */
1136 if (call->has_pattern)
1138 call->has_pattern = 1;
1140 /* request bchannel */
1141 if (!call->bchannel) {
1142 CDEBUG(call, call->ast, "Requesting B-channel.\n");
1143 memset(&newparam, 0, sizeof(union parameter));
1144 newparam.bchannel.type = BCHANNEL_REQUEST;
1145 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1147 /* queue PROGRESS, because tones are available */
1148 if (call->ast && call->pbx_started)
1149 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1153 * got dtmf from bchannel (locked state)
1155 void lcr_in_dtmf(struct chan_call *call, int val)
1157 struct ast_channel *ast = call->ast;
1162 if (!call->pbx_started)
1165 if (!call->dsp_dtmf) {
1166 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1170 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1173 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1177 * message received from LCR
1179 int receive_message(int message_type, unsigned int ref, union parameter *param)
1181 struct bchannel *bchannel;
1182 struct chan_call *call;
1183 union parameter newparam;
1185 memset(&newparam, 0, sizeof(union parameter));
1187 /* handle bchannel message*/
1188 if (message_type == MESSAGE_BCHANNEL)
1190 switch(param->bchannel.type)
1192 case BCHANNEL_ASSIGN:
1193 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1194 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1196 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1199 /* create bchannel */
1200 bchannel = alloc_bchannel(param->bchannel.handle);
1203 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1207 /* configure channel */
1208 bchannel->b_tx_gain = param->bchannel.tx_gain;
1209 bchannel->b_rx_gain = param->bchannel.rx_gain;
1210 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1211 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1213 bchannel->b_bf_len = param->bchannel.crypt_len;
1214 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1216 bchannel->b_txdata = 0;
1217 bchannel->b_tx_dejitter = 1;
1219 /* in case, ref is not set, this bchannel instance must
1220 * be created until it is removed again by LCR */
1222 call = find_call_ref(ref);
1225 bchannel->call = call;
1226 call->bchannel = bchannel;
1228 bchannel_dtmf(bchannel, 1);
1230 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1231 if (call->pipeline[0])
1232 bchannel_pipeline(bchannel, call->pipeline);
1234 bchannel_gain(bchannel, call->rx_gain, 0);
1236 bchannel_gain(bchannel, call->tx_gain, 1);
1237 if (call->bridge_id) {
1238 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1239 bchannel_join(bchannel, call->bridge_id);
1241 /* create only, if call exists, othewhise it bchannel is freed below... */
1242 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0)))
1243 bchannel_activate(bchannel, 1);
1246 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1247 newparam.bchannel.handle = param->bchannel.handle;
1248 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1249 /* if call has released before bchannel is assigned */
1251 newparam.bchannel.type = BCHANNEL_RELEASE;
1252 newparam.bchannel.handle = param->bchannel.handle;
1253 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1258 case BCHANNEL_REMOVE:
1259 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1260 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1262 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1265 /* unklink from call and destroy bchannel */
1266 free_bchannel(bchannel);
1269 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1270 newparam.bchannel.handle = param->bchannel.handle;
1271 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1276 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1281 /* handle new ref */
1282 if (message_type == MESSAGE_NEWREF)
1284 if (param->direction)
1286 /* new ref from lcr */
1287 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1288 if (!ref || find_call_ref(ref))
1290 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1293 /* allocate new call instance */
1294 call = alloc_call();
1296 call->state = CHAN_LCR_STATE_IN_PREPARE;
1299 call->ref_was_assigned = 1;
1300 /* set dtmf (default, use option 'n' to disable */
1302 /* wait for setup (or release from asterisk) */
1305 /* new ref, as requested from this remote application */
1306 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1307 call = find_call_ref(0);
1310 /* send release, if ref does not exist */
1311 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1312 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1317 call->ref_was_assigned = 1;
1318 /* send pending setup info */
1319 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1320 send_setup_to_lcr(call);
1321 /* release if asterisk has signed off */
1322 else if (call->state == CHAN_LCR_STATE_RELEASE)
1326 send_release_and_import(call, call->cause, call->location);
1328 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1340 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1343 call = find_call_ref(ref);
1346 /* ignore ref that is not used (anymore) */
1347 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1351 /* handle messages */
1352 switch(message_type)
1355 lcr_in_setup(call, message_type, param);
1358 case MESSAGE_OVERLAP:
1359 lcr_in_overlap(call, message_type, param);
1362 case MESSAGE_PROCEEDING:
1363 lcr_in_proceeding(call, message_type, param);
1366 case MESSAGE_ALERTING:
1367 lcr_in_alerting(call, message_type, param);
1370 case MESSAGE_CONNECT:
1371 lcr_in_connect(call, message_type, param);
1374 case MESSAGE_DISCONNECT:
1375 lcr_in_disconnect(call, message_type, param);
1378 case MESSAGE_RELEASE:
1379 lcr_in_release(call, message_type, param);
1382 case MESSAGE_INFORMATION:
1383 lcr_in_information(call, message_type, param);
1386 case MESSAGE_NOTIFY:
1387 lcr_in_notify(call, message_type, param);
1390 case MESSAGE_FACILITY:
1391 lcr_in_facility(call, message_type, param);
1394 case MESSAGE_PATTERN: // audio available from LCR
1395 if (!call->has_pattern)
1396 lcr_in_pattern(call, message_type, param);
1399 case MESSAGE_NOPATTERN: // audio not available from LCR
1402 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1403 call->audiopath = param->audiopath;
1407 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1414 * release all calls (due to broken socket)
1416 static void release_all_calls(void)
1418 struct chan_call *call;
1423 /* no ast, so we may directly free call */
1425 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1429 /* already in release process */
1430 if (call->state == CHAN_LCR_STATE_RELEASE) {
1434 /* release or queue release */
1436 call->state = CHAN_LCR_STATE_RELEASE;
1437 if (!call->pbx_started) {
1438 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1439 ast_hangup(call->ast); // call will be destroyed here
1442 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1443 strcpy(call->queue_string, "H");
1447 /* release all bchannels */
1448 while(bchannel_first)
1449 free_bchannel(bchannel_first);
1454 * warning! not thread safe
1455 * returns -1 for socket error, 0 for no work, 1 for work
1457 int handle_socket(void)
1461 struct admin_list *admin;
1462 struct admin_message msg;
1464 /* read from socket */
1465 len = read(lcr_sock, &msg, sizeof(msg));
1468 CERROR(NULL, NULL, "Socket closed.\n");
1469 return(-1); // socket closed
1473 if (len != sizeof(msg))
1475 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1476 return(-1); // socket error
1478 if (msg.message != ADMIN_MESSAGE)
1480 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1483 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1487 if (errno != EWOULDBLOCK)
1489 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1494 /* write to socket */
1497 admin = admin_first;
1498 len = write(lcr_sock, &admin->msg, sizeof(msg));
1501 CERROR(NULL, NULL, "Socket closed.\n");
1502 return(-1); // socket closed
1506 if (len != sizeof(msg))
1508 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1509 return(-1); // socket error
1512 admin_first = admin->next;
1518 if (errno != EWOULDBLOCK)
1520 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1529 * open and close socket and thread
1531 int open_socket(void)
1535 struct sockaddr_un sock_address;
1536 unsigned int on = 1;
1537 union parameter param;
1540 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1542 CERROR(NULL, NULL, "Failed to create socket.\n");
1546 /* set socket address and name */
1547 memset(&sock_address, 0, sizeof(sock_address));
1548 sock_address.sun_family = PF_UNIX;
1549 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1551 /* connect socket */
1552 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1556 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1560 /* set non-blocking io */
1561 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1565 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1569 /* enque hello message */
1570 memset(¶m, 0, sizeof(param));
1571 strcpy(param.hello.application, "asterisk");
1572 send_message(MESSAGE_HELLO, 0, ¶m);
1577 void close_socket(void)
1579 struct admin_list *admin, *temp;
1581 /* flush pending messages */
1582 admin = admin_first;
1585 admin = admin->next;
1597 /* sending queue to asterisk */
1598 static int queue_send(void)
1601 struct chan_call *call;
1602 struct ast_channel *ast;
1603 struct ast_frame fr;
1608 p = call->queue_string;
1611 /* there is something to queue */
1612 if (!ast_channel_trylock(ast)) { /* succeed */
1616 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1617 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1620 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1621 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1624 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1625 ast_queue_control(ast, AST_CONTROL_RINGING);
1626 ast_setstate(ast, AST_STATE_RINGING);
1629 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1630 ast_queue_control(ast, AST_CONTROL_ANSWER);
1633 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1634 ast_queue_hangup(ast);
1636 case '1': case '2': case '3': case 'A':
1637 case '4': case '5': case '6': case 'B':
1638 case '7': case '8': case '9': case 'C':
1639 case '*': case '0': case '#': case 'D':
1640 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1641 /* send digit to asterisk */
1642 memset(&fr, 0, sizeof(fr));
1644 #ifdef LCR_FOR_ASTERISK
1645 fr.frametype = AST_FRAME_DTMF_BEGIN;
1648 #ifdef LCR_FOR_CALLWEAVER
1649 fr.frametype = AST_FRAME_DTMF;
1653 fr.delivery = ast_tv(0, 0);
1654 ast_queue_frame(ast, &fr);
1656 #ifdef LCR_FOR_ASTERISK
1657 fr.frametype = AST_FRAME_DTMF_END;
1658 ast_queue_frame(ast, &fr);
1663 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1667 call->queue_string[0] = '\0';
1668 ast_channel_unlock(ast);
1678 /* signal handler */
1679 void sighandler(int sigset)
1683 /* chan_lcr thread */
1684 static void *chan_thread(void *arg)
1688 union parameter param;
1689 time_t retry = 0, now;
1691 bchannel_pid = getpid();
1693 // signal(SIGPIPE, sighandler);
1695 memset(¶m, 0, sizeof(union parameter));
1699 ast_mutex_lock(&chan_lock);
1706 ret = handle_socket();
1708 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1710 release_all_calls();
1717 if (retry && now-retry > 5) {
1718 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1720 if (open_socket() < 0) {
1729 ret = bchannel_handle();
1733 /* handle messages to asterisk */
1738 /* delay if no work done */
1740 ast_mutex_unlock(&chan_lock);
1742 #ifdef LCR_FOR_ASTERISK
1746 #ifdef LCR_FOR_CALLWEAVER
1750 ast_mutex_lock(&chan_lock);
1756 CERROR(NULL, NULL, "Thread exit.\n");
1758 ast_mutex_unlock(&chan_lock);
1760 // signal(SIGPIPE, SIG_DFL);
1766 * new asterisk instance
1769 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1771 char exten[256], *dial, *interface, *opt;
1772 struct ast_channel *ast;
1773 struct chan_call *call;
1775 ast_mutex_lock(&chan_lock);
1776 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1778 /* if socket is closed */
1781 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1782 ast_mutex_unlock(&chan_lock);
1786 /* create call instance */
1787 call = alloc_call();
1790 /* failed to create instance */
1791 ast_mutex_unlock(&chan_lock);
1795 /* create asterisk channel instrance */
1797 #ifdef LCR_FOR_ASTERISK
1798 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1801 #ifdef LCR_FOR_CALLWEAVER
1802 ast = ast_channel_alloc(1);
1807 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1809 /* failed to create instance */
1810 ast_mutex_unlock(&chan_lock);
1813 ast->tech = &lcr_tech;
1814 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1815 /* configure channel */
1816 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1817 ast->readformat = ast->rawreadformat = ast->nativeformats;
1818 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1820 ast->hangupcause = 0;
1824 ast->tech_pvt = call;
1825 ast->fds[0] = call->pipe[0];
1826 call->pbx_started = 0;
1828 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1831 * Extract interface, dialstring, options from data.
1834 * <interface>/<dialstring>
1835 * <interface>/<dialstring>/options
1837 strncpy(exten, (char *)data, sizeof(exten)-1);
1838 exten[sizeof(exten)-1] = '\0';
1839 if ((dial = strchr(exten, '/'))) {
1842 if ((opt = strchr(dial, '/')))
1851 strncpy(call->interface, interface, sizeof(call->interface)-1);
1852 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1853 apply_opt(call, (char *)opt);
1855 ast_mutex_unlock(&chan_lock);
1860 * call from asterisk
1862 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1864 union parameter newparam;
1865 struct chan_call *call;
1867 ast_mutex_lock(&chan_lock);
1868 call = ast->tech_pvt;
1870 #ifdef LCR_FOR_CALLWEAVER
1872 snprintf(ast->name, sizeof(ast->name), "LCR/%s-%04x",call->dialstring, ast_random() & 0xffff);
1876 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1877 ast_mutex_unlock(&chan_lock);
1881 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1883 /* pbx process is started */
1884 call->pbx_started = 1;
1885 /* send MESSAGE_NEWREF */
1886 memset(&newparam, 0, sizeof(union parameter));
1887 newparam.direction = 0; /* request from app */
1888 send_message(MESSAGE_NEWREF, 0, &newparam);
1890 /* set hdlc if capability requires hdlc */
1891 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1892 || ast->transfercapability == INFO_BC_DATARESTRICTED
1893 || ast->transfercapability == INFO_BC_VIDEO)
1895 /* if hdlc is forced by option, we change transcap to data */
1897 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1898 && ast->transfercapability != INFO_BC_DATARESTRICTED
1899 && ast->transfercapability != INFO_BC_VIDEO)
1900 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1902 call->cid_num[0] = 0;
1903 call->cid_name[0] = 0;
1904 call->cid_rdnis[0] = 0;
1906 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1907 strncpy(call->cid_num, ast->cid.cid_num,
1908 sizeof(call->cid_num)-1);
1910 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1911 strncpy(call->cid_name, ast->cid.cid_name,
1912 sizeof(call->cid_name)-1);
1913 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1914 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1915 sizeof(call->cid_rdnis)-1);
1917 ast_mutex_unlock(&chan_lock);
1921 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1923 static const char* dtmf_tones[] = {
1924 "!941+1336/100,!0/100", /* 0 */
1925 "!697+1209/100,!0/100", /* 1 */
1926 "!697+1336/100,!0/100", /* 2 */
1927 "!697+1477/100,!0/100", /* 3 */
1928 "!770+1209/100,!0/100", /* 4 */
1929 "!770+1336/100,!0/100", /* 5 */
1930 "!770+1477/100,!0/100", /* 6 */
1931 "!852+1209/100,!0/100", /* 7 */
1932 "!852+1336/100,!0/100", /* 8 */
1933 "!852+1477/100,!0/100", /* 9 */
1934 "!697+1633/100,!0/100", /* A */
1935 "!770+1633/100,!0/100", /* B */
1936 "!852+1633/100,!0/100", /* C */
1937 "!941+1633/100,!0/100", /* D */
1938 "!941+1209/100,!0/100", /* * */
1939 "!941+1477/100,!0/100" }; /* # */
1941 if (digit >= '0' && digit <='9')
1942 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1943 else if (digit >= 'A' && digit <= 'D')
1944 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1945 else if (digit == '*')
1946 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1947 else if (digit == '#')
1948 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1951 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1952 "'%c' for '%s'\n", digit, ast->name);
1956 #ifdef LCR_FOR_ASTERISK
1957 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1959 #ifdef LCR_FOR_CALLWEAVER
1960 static int lcr_digit(struct ast_channel *ast, char digit)
1963 struct chan_call *call;
1964 union parameter newparam;
1967 #ifdef LCR_FOR_CALLWEAVER
1968 int inband_dtmf = 0;
1971 /* only pass IA5 number space */
1972 if (digit > 126 || digit < 32)
1975 ast_mutex_lock(&chan_lock);
1976 call = ast->tech_pvt;
1978 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1979 ast_mutex_unlock(&chan_lock);
1983 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1985 /* send information or queue them */
1986 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1988 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1989 memset(&newparam, 0, sizeof(union parameter));
1990 newparam.information.id[0] = digit;
1991 newparam.information.id[1] = '\0';
1992 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1995 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1997 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1999 strncat(call->dialque, buf, strlen(call->dialque)-1);
2002 ast_mutex_unlock(&chan_lock);
2004 #ifdef LCR_FOR_ASTERISK
2008 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2010 int inband_dtmf = 0;
2011 struct chan_call *call;
2014 ast_mutex_lock(&chan_lock);
2016 call = ast->tech_pvt;
2020 "Received digit from Asterisk, "
2021 "but no call instance exists.\n");
2022 ast_mutex_unlock(&chan_lock);
2026 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2028 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2032 ast_mutex_unlock(&chan_lock);
2035 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2036 send_digit_to_chan(ast, digit);
2042 static int lcr_answer(struct ast_channel *ast)
2044 union parameter newparam;
2045 struct chan_call *call;
2047 ast_mutex_lock(&chan_lock);
2048 call = ast->tech_pvt;
2050 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2051 ast_mutex_unlock(&chan_lock);
2055 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2057 /* copy connectinfo, if bridged */
2058 if (call->bridge_call)
2059 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2060 /* send connect message to lcr */
2061 if (call->state != CHAN_LCR_STATE_CONNECT) {
2062 memset(&newparam, 0, sizeof(union parameter));
2063 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2064 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2065 call->state = CHAN_LCR_STATE_CONNECT;
2068 /* request bchannel */
2069 if (!call->bchannel) {
2070 CDEBUG(call, ast, "Requesting B-channel.\n");
2071 memset(&newparam, 0, sizeof(union parameter));
2072 newparam.bchannel.type = BCHANNEL_REQUEST;
2073 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2076 // memset(&newparam, 0, sizeof(union parameter));
2077 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2079 ast_mutex_unlock(&chan_lock);
2083 static int lcr_hangup(struct ast_channel *ast)
2085 struct chan_call *call;
2086 pthread_t tid = pthread_self();
2088 if (!pthread_equal(tid, chan_tid))
2089 ast_mutex_lock(&chan_lock);
2090 call = ast->tech_pvt;
2092 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2093 if (!pthread_equal(tid, chan_tid))
2094 ast_mutex_unlock(&chan_lock);
2098 if (!pthread_equal(tid, chan_tid))
2099 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2101 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2103 /* disconnect asterisk, maybe not required */
2104 ast->tech_pvt = NULL;
2109 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2110 if (ast->hangupcause > 0)
2111 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2113 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2116 if (!pthread_equal(tid, chan_tid))
2117 ast_mutex_unlock(&chan_lock);
2121 /* ref is not set, due to prepare setup or release */
2122 if (call->state == CHAN_LCR_STATE_RELEASE)
2124 /* we get the response to our release */
2125 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2129 /* during prepare, we change to release state */
2130 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2131 call->state = CHAN_LCR_STATE_RELEASE;
2135 if (!pthread_equal(tid, chan_tid))
2136 ast_mutex_unlock(&chan_lock);
2140 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
2142 struct chan_call *call;
2145 CDEBUG(NULL, ast, "No subclass\n");
2146 if (!(f->subclass & ast->nativeformats))
2147 CDEBUG(NULL, ast, "Unexpected format.\n");
2149 ast_mutex_lock(&chan_lock);
2150 call = ast->tech_pvt;
2152 ast_mutex_unlock(&chan_lock);
2155 if (call->bchannel && f->samples)
2156 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2157 ast_mutex_unlock(&chan_lock);
2162 static struct ast_frame *lcr_read(struct ast_channel *ast)
2164 struct chan_call *call;
2167 ast_mutex_lock(&chan_lock);
2168 call = ast->tech_pvt;
2170 ast_mutex_unlock(&chan_lock);
2173 if (call->pipe[0] > -1) {
2174 if (call->rebuffer && !call->hdlc) {
2175 /* Make sure we have a complete 20ms (160byte) frame */
2176 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2178 call->framepos += len;
2181 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2183 if (len < 0 && errno == EAGAIN) {
2184 ast_mutex_unlock(&chan_lock);
2186 #ifdef LCR_FOR_ASTERISK
2187 return &ast_null_frame;
2190 #ifdef LCR_FOR_CALLWEAVER
2196 close(call->pipe[0]);
2198 ast_mutex_unlock(&chan_lock);
2200 } else if (call->rebuffer && call->framepos < 160) {
2201 /* Not a complete frame, so we send a null-frame */
2202 ast_mutex_unlock(&chan_lock);
2203 return &ast_null_frame;
2207 call->read_fr.frametype = AST_FRAME_VOICE;
2208 call->read_fr.subclass = ast->nativeformats;
2209 if (call->rebuffer) {
2210 call->read_fr.datalen = call->framepos;
2211 call->read_fr.samples = call->framepos;
2214 call->read_fr.datalen = len;
2215 call->read_fr.samples = len;
2217 call->read_fr.delivery = ast_tv(0,0);
2218 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2219 ast_mutex_unlock(&chan_lock);
2221 return &call->read_fr;
2224 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2226 union parameter newparam;
2228 struct chan_call *call;
2230 ast_mutex_lock(&chan_lock);
2231 call = ast->tech_pvt;
2233 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2234 ast_mutex_unlock(&chan_lock);
2239 case AST_CONTROL_BUSY:
2240 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2241 ast_setstate(ast, AST_STATE_BUSY);
2242 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2243 /* send message to lcr */
2244 memset(&newparam, 0, sizeof(union parameter));
2245 newparam.disconnectinfo.cause = 17;
2246 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2247 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2249 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2252 case AST_CONTROL_CONGESTION:
2253 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2254 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2255 /* send message to lcr */
2256 memset(&newparam, 0, sizeof(union parameter));
2257 newparam.disconnectinfo.cause = ast->hangupcause;
2258 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2259 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2261 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2264 case AST_CONTROL_PROCEEDING:
2265 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2266 if (call->state == CHAN_LCR_STATE_IN_SETUP
2267 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2268 /* send message to lcr */
2269 memset(&newparam, 0, sizeof(union parameter));
2270 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2272 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2275 case AST_CONTROL_RINGING:
2276 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2277 ast_setstate(ast, AST_STATE_RINGING);
2278 if (call->state == CHAN_LCR_STATE_IN_SETUP
2279 || call->state == CHAN_LCR_STATE_IN_DIALING
2280 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2281 /* send message to lcr */
2282 memset(&newparam, 0, sizeof(union parameter));
2283 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2285 call->state = CHAN_LCR_STATE_IN_ALERTING;
2288 case AST_CONTROL_PROGRESS:
2289 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2290 /* request bchannel */
2291 if (!call->bchannel) {
2292 CDEBUG(call, ast, "Requesting B-channel.\n");
2293 memset(&newparam, 0, sizeof(union parameter));
2294 newparam.bchannel.type = BCHANNEL_REQUEST;
2295 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2299 CDEBUG(call, ast, "Received indicate -1.\n");
2303 case AST_CONTROL_VIDUPDATE:
2304 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2307 case AST_CONTROL_HOLD:
2308 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2309 /* send message to lcr */
2310 memset(&newparam, 0, sizeof(union parameter));
2311 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2312 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2314 /*start music onhold*/
2315 #ifdef LCR_FOR_ASTERISK
2316 ast_moh_start(ast,data,ast->musicclass);
2319 #ifdef LCR_FOR_CALLWEAVER
2320 ast_moh_start(ast, NULL);
2325 case AST_CONTROL_UNHOLD:
2326 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2327 /* send message to lcr */
2328 memset(&newparam, 0, sizeof(union parameter));
2329 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2330 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2336 #ifdef AST_CONTROL_SRCUPDATE
2337 case AST_CONTROL_SRCUPDATE:
2338 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2342 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2348 ast_mutex_unlock(&chan_lock);
2355 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2357 struct chan_call *call;
2363 ast_mutex_lock(&chan_lock);
2364 call = ast->tech_pvt;
2366 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2367 ast_mutex_unlock(&chan_lock);
2371 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2373 ast_mutex_unlock(&chan_lock);
2378 * send_text asterisk
2380 static int lcr_send_text(struct ast_channel *ast, const char *text)
2382 struct chan_call *call;
2383 union parameter newparam;
2385 ast_mutex_lock(&chan_lock);
2386 call = ast->tech_pvt;
2388 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2389 ast_mutex_unlock(&chan_lock);
2393 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2394 memset(&newparam, 0, sizeof(union parameter));
2395 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2396 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2397 ast_mutex_lock(&chan_lock);
2404 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2405 struct ast_channel *ast2, int flags,
2406 struct ast_frame **fo,
2407 struct ast_channel **rc, int timeoutms)
2410 struct chan_call *call1, *call2;
2411 struct ast_channel *carr[2], *who;
2413 struct ast_frame *f;
2416 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2421 /* join via dsp (if the channels are currently open) */
2422 ast_mutex_lock(&chan_lock);
2423 call1 = ast1->tech_pvt;
2424 call2 = ast2->tech_pvt;
2425 if (!call1 || !call2) {
2426 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2427 ast_mutex_unlock(&chan_lock);
2428 return AST_BRIDGE_COMPLETE;
2431 /* join, if both call instances uses dsp
2432 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2434 if (!call1->nodsp && !call2->nodsp) {
2435 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2437 /* get bridge id and join */
2438 bridge_id = new_bridge_id();
2440 call1->bridge_id = bridge_id;
2441 if (call1->bchannel)
2442 bchannel_join(call1->bchannel, bridge_id);
2444 call2->bridge_id = bridge_id;
2445 if (call2->bchannel)
2446 bchannel_join(call2->bchannel, bridge_id);
2448 if (call1->nodsp && call2->nodsp)
2449 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2451 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2452 call1->bridge_call = call2;
2453 call2->bridge_call = call1;
2455 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2456 || call1->state == CHAN_LCR_STATE_IN_DIALING
2457 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2458 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2459 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2462 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2463 || call2->state == CHAN_LCR_STATE_IN_DIALING
2464 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2465 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2466 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2470 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2471 so let's do it for them. Hmpf.
2474 if (call1->on_hold) {
2475 union parameter newparam;
2477 memset(&newparam, 0, sizeof(union parameter));
2478 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2479 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2484 if (call2->on_hold) {
2485 union parameter newparam;
2487 memset(&newparam, 0, sizeof(union parameter));
2488 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2489 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2494 ast_mutex_unlock(&chan_lock);
2498 who = ast_waitfor_n(carr, 2, &to);
2501 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2506 if (!f || f->frametype == AST_FRAME_CONTROL) {
2508 CDEBUG(NULL, NULL, "Got hangup.\n");
2510 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2517 if ( f->frametype == AST_FRAME_DTMF ) {
2518 CDEBUG(NULL, NULL, "Got DTMF.\n");
2534 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2536 /* split channels */
2537 ast_mutex_lock(&chan_lock);
2538 call1 = ast1->tech_pvt;
2539 call2 = ast2->tech_pvt;
2540 if (call1 && call1->bridge_id)
2542 call1->bridge_id = 0;
2543 if (call1->bchannel)
2544 bchannel_join(call1->bchannel, 0);
2545 if (call1->bridge_call)
2546 call1->bridge_call->bridge_call = NULL;
2548 if (call2 && call1->bridge_id)
2550 call2->bridge_id = 0;
2551 if (call2->bchannel)
2552 bchannel_join(call2->bchannel, 0);
2553 if (call2->bridge_call)
2554 call2->bridge_call->bridge_call = NULL;
2556 call1->bridge_call = NULL;
2557 call2->bridge_call = NULL;
2559 ast_mutex_unlock(&chan_lock);
2560 return AST_BRIDGE_COMPLETE;
2562 static struct ast_channel_tech lcr_tech = {
2564 .description = "Channel driver for connecting to Linux-Call-Router",
2565 .capabilities = AST_FORMAT_ALAW,
2566 .requester = lcr_request,
2568 #ifdef LCR_FOR_ASTERISK
2569 .send_digit_begin = lcr_digit_begin,
2570 .send_digit_end = lcr_digit_end,
2573 #ifdef LCR_FOR_CALLWEAVER
2574 .send_digit = lcr_digit,
2578 .bridge = lcr_bridge,
2579 .hangup = lcr_hangup,
2580 .answer = lcr_answer,
2583 .indicate = lcr_indicate,
2585 .send_text = lcr_send_text,
2594 static int lcr_show_lcr (int fd, int argc, char *argv[])
2599 static int lcr_show_calls (int fd, int argc, char *argv[])
2604 static int lcr_reload_routing (int fd, int argc, char *argv[])
2609 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2614 static int lcr_port_block (int fd, int argc, char *argv[])
2619 static int lcr_port_unblock (int fd, int argc, char *argv[])
2624 static int lcr_port_unload (int fd, int argc, char *argv[])
2629 static struct ast_cli_entry cli_show_lcr =
2630 { {"lcr", "show", "lcr", NULL},
2632 "Shows current states of LCR core",
2633 "Usage: lcr show lcr\n",
2636 static struct ast_cli_entry cli_show_calls =
2637 { {"lcr", "show", "calls", NULL},
2639 "Shows current calls made by LCR and Asterisk",
2640 "Usage: lcr show calls\n",
2643 static struct ast_cli_entry cli_reload_routing =
2644 { {"lcr", "reload", "routing", NULL},
2646 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2647 "Usage: lcr reload routing\n",
2650 static struct ast_cli_entry cli_reload_interfaces =
2651 { {"lcr", "reload", "interfaces", NULL},
2652 lcr_reload_interfaces,
2653 "Reloads interfaces conf of LCR",
2654 "Usage: lcr reload interfaces\n",
2657 static struct ast_cli_entry cli_port_block =
2658 { {"lcr", "port", "block", NULL},
2660 "Blocks LCR port for further calls",
2661 "Usage: lcr port block \"<port>\"\n",
2664 static struct ast_cli_entry cli_port_unblock =
2665 { {"lcr", "port", "unblock", NULL},
2667 "Unblocks or loads LCR port, port is opened my mISDN",
2668 "Usage: lcr port unblock \"<port>\"\n",
2671 static struct ast_cli_entry cli_port_unload =
2672 { {"lcr", "port", "unload", NULL},
2674 "Unloads LCR port, port is closes by mISDN",
2675 "Usage: lcr port unload \"<port>\"\n",
2680 #ifdef LCR_FOR_ASTERISK
2681 static int lcr_config_exec(struct ast_channel *ast, void *data)
2684 #ifdef LCR_FOR_CALLWEAVER
2685 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
2688 struct chan_call *call;
2690 ast_mutex_lock(&chan_lock);
2692 #ifdef LCR_FOR_ASTERISK
2693 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2696 #ifdef LCR_FOR_CALLWEAVER
2697 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
2703 if (call->ast == ast)
2709 #ifdef LCR_FOR_ASTERISK
2710 apply_opt(call, (char *)data);
2713 #ifdef LCR_FOR_CALLWEAVER
2714 apply_opt(call, (char *)argv[0]);
2718 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2720 ast_mutex_unlock(&chan_lock);
2725 * module loading and destruction
2727 int load_module(void)
2731 for (i = 0; i < 256; i++) {
2732 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2733 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2736 if (read_options() == 0) {
2737 CERROR(NULL, NULL, "%s", options_error);
2739 #ifdef LCR_FOR_ASTERISK
2740 return AST_MODULE_LOAD_DECLINE;
2743 #ifdef LCR_FOR_CALLWEAVER
2749 ast_mutex_init(&chan_lock);
2750 ast_mutex_init(&log_lock);
2752 if (open_socket() < 0) {
2753 /* continue with closed socket */
2756 if (bchannel_initialize()) {
2757 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2760 #ifdef LCR_FOR_ASTERISK
2761 return AST_MODULE_LOAD_DECLINE;
2764 #ifdef LCR_FOR_CALLWEAVER
2770 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2771 if (ast_channel_register(&lcr_tech)) {
2772 CERROR(NULL, NULL, "Unable to register channel class\n");
2773 bchannel_deinitialize();
2776 #ifdef LCR_FOR_ASTERISK
2777 return AST_MODULE_LOAD_DECLINE;
2780 #ifdef LCR_FOR_CALLWEAVER
2785 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2787 #ifdef LCR_FOR_ASTERISK
2788 "lcr_config(<opt><optarg>:<opt>:...)\n"
2791 #ifdef LCR_FOR_CALLWEAVER
2792 "lcr_config(<opt><optarg>:<opt>:...)\n",
2795 "Sets LCR opts. and optargs\n"
2797 "The available options are:\n"
2798 " d - Send display text on called phone, text is the optarg.\n"
2799 " n - Don't detect dtmf tones on called channel.\n"
2800 " h - Force data call (HDLC).\n"
2801 " t - Disable mISDN_dsp features (required for fax application).\n"
2802 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
2803 " Use time to detect for optarg.\n"
2804 " c - Make crypted outgoing call, optarg is keyindex.\n"
2805 " e - Perform echo cancelation on this channel.\n"
2806 " Takes mISDN pipeline option as optarg.\n"
2807 " s - Send Non Inband DTMF as inband.\n"
2808 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
2809 " vr - rxgain control\n"
2810 " vt - txgain control\n"
2811 " Volume changes at factor 2 ^ optarg.\n"
2816 ast_cli_register(&cli_show_lcr);
2817 ast_cli_register(&cli_show_calls);
2818 ast_cli_register(&cli_reload_routing);
2819 ast_cli_register(&cli_reload_interfaces);
2820 ast_cli_register(&cli_port_block);
2821 ast_cli_register(&cli_port_unblock);
2822 ast_cli_register(&cli_port_unload);
2826 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2828 /* failed to create thread */
2829 bchannel_deinitialize();
2831 ast_channel_unregister(&lcr_tech);
2833 #ifdef LCR_FOR_ASTERISK
2834 return AST_MODULE_LOAD_DECLINE;
2837 #ifdef LCR_FOR_CALLWEAVER
2845 int unload_module(void)
2847 /* First, take us out of the channel loop */
2848 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2851 pthread_join(chan_tid, NULL);
2853 ast_channel_unregister(&lcr_tech);
2855 ast_unregister_application("lcr_config");
2858 if (mISDN_created) {
2859 bchannel_deinitialize();
2863 if (lcr_sock >= 0) {
2871 int reload_module(void)
2877 #ifdef LCR_FOR_ASTERISK
2878 #define AST_MODULE "chan_lcr"
2881 #ifdef LCR_FOR_CALLWEAVER
2885 ast_mutex_lock(&usecnt_lock);
2887 ast_mutex_unlock(&usecnt_lock);
2892 #ifdef LCR_FOR_ASTERISK
2893 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2894 .load = load_module,
2895 .unload = unload_module,
2896 .reload = reload_module,
2900 #ifdef LCR_FOR_CALLWEAVER
2901 char *description(void)