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>
164 #include <asterisk/version.h>
166 #include "extension.h"
168 #include "callerid.h"
169 #include "lcrsocket.h"
172 #include "bchannel.h"
174 #include "chan_lcr.h"
176 CHAN_LCR_STATE // state description structure
177 MESSAGES // message text
179 #ifdef LCR_FOR_CALLWEAVER
180 AST_MUTEX_DEFINE_STATIC(rand_lock);
183 unsigned char flip_bits[256];
185 #ifdef LCR_FOR_CALLWEAVER
186 static struct ast_frame nullframe = { AST_FRAME_NULL, };
192 char lcr_type[]="lcr";
194 #ifdef LCR_FOR_CALLWEAVER
195 static ast_mutex_t usecnt_lock;
197 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
201 ast_mutex_t chan_lock; /* global lock */
202 ast_mutex_t log_lock; /* logging log */
204 * used to indicate change in file descriptors, so select function's result may
207 int global_change = 0;
210 struct lcr_fd wake_fd;
212 int glob_channel = 0;
215 struct lcr_fd socket_fd;
216 struct lcr_timer socket_retry;
219 struct admin_list *next;
220 struct admin_message msg;
221 } *admin_first = NULL;
223 static struct ast_channel_tech lcr_tech;
228 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, ...)
231 char call_text[128] = "NULL";
232 char ast_text[128] = "NULL";
235 ast_mutex_lock(&log_lock);
238 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
239 buffer[sizeof(buffer)-1]=0;
243 sprintf(call_text, "%d", call->ref);
245 #if ASTERISK_VERSION_NUM < 110000
246 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
248 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
250 ast_text[sizeof(ast_text)-1] = '\0';
252 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
253 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
255 ast_mutex_unlock(&log_lock);
259 * channel and call instances
261 struct chan_call *call_first;
265 * special case: 0: find new ref, that has not been assigned a ref yet
268 struct chan_call *find_call_ref(unsigned int ref)
270 struct chan_call *call = call_first;
271 int assigned = (ref > 0);
274 if (call->ref == ref && call->ref_was_assigned == assigned)
281 void free_call(struct chan_call *call)
283 struct chan_call **temp = &call_first;
287 *temp = (*temp)->next;
288 if (call->pipe[0] > -1)
289 close(call->pipe[0]);
290 if (call->pipe[1] > -1)
291 close(call->pipe[1]);
292 if (call->bchannel) {
293 if (call->bchannel->call != call)
294 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
295 call->bchannel->call = NULL;
297 if (call->bridge_call) {
298 if (call->bridge_call->bridge_call != call)
299 CERROR(call, NULL, "Linked call structure has no link to us.\n");
300 call->bridge_call->bridge_call = NULL;
303 ast_translator_free_path(call->trans);
305 ast_dsp_free(call->dsp);
306 CDEBUG(call, NULL, "Call instance freed.\n");
311 temp = &((*temp)->next);
313 CERROR(call, NULL, "Call instance not found in list.\n");
316 struct chan_call *alloc_call(void)
318 struct chan_call **callp = &call_first;
321 callp = &((*callp)->next);
323 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
325 memset(*callp, 0, sizeof(struct chan_call));
326 if (pipe((*callp)->pipe) < 0) {
327 CERROR(*callp, NULL, "Failed to create pipe.\n");
331 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
332 CDEBUG(*callp, NULL, "Call instance allocated.\n");
336 unsigned short new_bridge_id(void)
338 struct chan_call *call;
339 unsigned short id = 1;
341 /* search for lowest bridge id that is not in use and not 0 */
345 if (call->bridge_id == id)
353 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
358 * enque message to LCR
360 int send_message(int message_type, unsigned int ref, union parameter *param)
362 struct admin_list *admin, **adminp;
365 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
368 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
370 adminp = &admin_first;
372 adminp = &((*adminp)->next);
373 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
375 CERROR(NULL, NULL, "No memory for message to LCR.\n");
380 admin->msg.message = ADMIN_MESSAGE;
381 admin->msg.u.msg.type = message_type;
382 admin->msg.u.msg.ref = ref;
383 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
384 socket_fd.when |= LCR_FD_WRITE;
388 write(wake_pipe[1], &byte, 1);
395 * apply options (in locked state)
397 void apply_opt(struct chan_call *call, char *data)
399 union parameter newparam;
400 char string[1024], *p = string, *opt, *key;
406 strncpy(string, data, sizeof(string)-1);
407 string[sizeof(string)-1] = '\0';
410 while((opt = strsep(&p, ":"))) {
413 if (opt[1] == '\0') {
414 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
417 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
418 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
419 strncpy(call->display, opt+1, sizeof(call->display)-1);
421 memset(&newparam, 0, sizeof(union parameter));
422 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
423 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
427 if (opt[1] != '\0') {
428 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
431 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
432 if (call->dsp_dtmf) {
435 bchannel_dtmf(call->bchannel, 0);
439 if (opt[1] == '\0') {
440 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
444 /* check for 0xXXXX... type of key */
445 if (!!strncmp((char *)key, "0x", 2)) {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
450 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
451 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
456 if (*key>='0' && *key<='9')
457 call->bf_key[i] = (*key-'0') << 8;
458 else if (*key>='a' && *key<='f')
459 call->bf_key[i] = (*key-'a'+10) << 8;
460 else if (*key>='A' && *key<='F')
461 call->bf_key[i] = (*key-'A'+10) << 8;
465 if (*key>='0' && *key<='9')
466 call->bf_key[i] += (*key - '0');
467 else if (*key>='a' && *key<='f')
468 call->bf_key[i] += (*key - 'a' + 10);
469 else if (*key>='A' && *key<='F')
470 call->bf_key[i] += (*key - 'A' + 10);
477 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
481 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
483 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
486 if (opt[1] != '\0') {
487 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
490 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
495 if (opt[1] != '\0') {
496 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
499 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
504 if (opt[1] == '\0') {
505 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
508 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
509 call->nodsp_queue = atoi(opt+1);
512 if (opt[1] == '\0') {
513 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
516 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
517 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
519 bchannel_pipeline(call->bchannel, call->pipeline);
522 if (opt[1] == '\0') {
523 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
526 call->faxdetect=atoi(opt+1);
528 call->dsp=ast_dsp_new();
530 #ifdef LCR_FOR_CALLWEAVER
531 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
533 #ifdef LCR_FOR_ASTERISK
534 #ifdef DSP_FEATURE_DTMF_DETECT
535 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
537 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
542 #ifdef LCR_FOR_CALLWEAVER
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
545 #ifdef LCR_FOR_ASTERISK
546 #if ASTERISK_VERSION_NUM < 100000
547 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
549 struct ast_format src;
550 struct ast_format dst;
551 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
552 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
553 call->trans=ast_translator_build_path(&dst, &src);
558 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
561 if (opt[1] != '\0') {
562 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
565 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
570 if (opt[1] != '\0') {
571 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
574 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
575 call->inband_dtmf = 1;
578 if (opt[1] != 'r' && opt[1] != 't') {
579 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
583 if (gain < -8 || gain >8) {
584 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
587 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
589 call->rx_gain = gain;
591 bchannel_gain(call->bchannel, call->rx_gain, 0);
593 call->tx_gain = gain;
595 bchannel_gain(call->bchannel, call->tx_gain, 1);
599 if (opt[1] != '\0') {
600 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
603 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
608 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
612 /* re-open, if bchannel is created */
613 if (call->bchannel && call->bchannel->b_sock > -1) {
614 bchannel_destroy(call->bchannel);
615 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
616 bchannel_activate(call->bchannel, 1);
621 * send setup info to LCR
622 * this function is called, when asterisk call is received and ref is received
624 static void send_setup_to_lcr(struct chan_call *call)
626 union parameter newparam;
627 struct ast_channel *ast = call->ast;
630 if (!call->ast || !call->ref)
633 #ifdef AST_1_8_OR_HIGHER
634 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
636 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
639 /* send setup message to LCR */
640 memset(&newparam, 0, sizeof(union parameter));
641 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
642 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
644 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
646 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
647 if (!!strcmp(call->interface, "pbx"))
648 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
649 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
650 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
651 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
652 call->display[0] = '\0';
654 #ifdef AST_1_8_OR_HIGHER
655 /* set stored call information */
656 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
657 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
659 if (call->cid_num[0])
660 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
661 if (call->cid_name[0])
662 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
663 if (call->cid_rdnis[0]) {
664 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
665 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
666 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
668 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
669 case AST_PRES_RESTRICTED:
670 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
672 case AST_PRES_UNAVAILABLE:
673 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
675 case AST_PRES_ALLOWED:
677 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
679 switch(ast->cid.cid_ton) {
681 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
684 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
687 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
690 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
693 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
694 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
696 // ast->transfercapability = atoi(tmp);
697 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
698 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
700 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
702 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
703 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
705 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
706 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
707 send_message(MESSAGE_SETUP, call->ref, &newparam);
709 /* change to outgoing setup state */
710 call->state = CHAN_LCR_STATE_OUT_SETUP;
714 * send dialing info to LCR
715 * this function is called, when setup acknowledge is received and dialing
718 static void send_dialque_to_lcr(struct chan_call *call)
720 union parameter newparam;
722 if (!call->ast || !call->ref || !call->dialque[0])
725 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
727 /* send setup message to LCR */
728 memset(&newparam, 0, sizeof(union parameter));
730 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
732 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
733 call->dialque[0] = '\0';
734 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
738 * in case of a bridge, the unsupported message can be forwarded directly
739 * to the remote call.
741 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
745 if (!call->bridge_call) return;
746 CDEBUG(call, NULL, "Sending message due bridging.\n");
747 send_message(message_type, call->bridge_call->ref, param);
751 * send release message to LCR and import bchannel if exported
753 static void send_release_and_import(struct chan_call *call, int cause, int location)
755 union parameter newparam;
757 /* importing channel */
758 if (call->bchannel) {
759 memset(&newparam, 0, sizeof(union parameter));
760 newparam.bchannel.type = BCHANNEL_RELEASE;
761 newparam.bchannel.handle = call->bchannel->handle;
762 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
764 /* sending release */
765 memset(&newparam, 0, sizeof(union parameter));
766 newparam.disconnectinfo.cause = cause;
767 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
768 send_message(MESSAGE_RELEASE, call->ref, &newparam);
772 * check if extension matches and start asterisk
773 * if it can match, proceed
776 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
779 union parameter newparam;
780 #if ASTERISK_VERSION_NUM < 110000
781 char *exten = ast->exten;
783 char *exten = ast_channel_exten(ast);
788 #if ASTERISK_VERSION_NUM < 110000
789 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
791 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
796 #if ASTERISK_VERSION_NUM < 110000
797 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
798 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
800 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
801 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
806 #if ASTERISK_VERSION_NUM < 110000
807 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
808 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);
810 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
811 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast_channel_context(ast));
816 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
817 /* send setup acknowledge to lcr */
818 memset(&newparam, 0, sizeof(union parameter));
819 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
822 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
827 #if ASTERISK_VERSION_NUM < 110000
828 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
830 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
832 /* send setup acknowledge to lcr */
833 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
834 memset(&newparam, 0, sizeof(union parameter));
835 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
839 call->state = CHAN_LCR_STATE_IN_DIALING;
841 /* if match, start pbx */
842 #if ASTERISK_VERSION_NUM < 110000
843 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
845 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
847 CDEBUG(call, ast, "Extensions matches.\n");
852 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
856 #if ASTERISK_VERSION_NUM < 110000
859 if (!*ast_channel_exten(ast)) {
862 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
870 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
871 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
873 /* release asterisk */
874 ast->hangupcause = call->cause;
875 /* change to release state */
876 call->state = CHAN_LCR_STATE_RELEASE;
877 ast_hangup(ast); // call will be destroyed here
881 /* send setup to asterisk */
882 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
884 #ifdef LCR_FOR_CALLWEAVER
886 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
889 ret = ast_pbx_start(ast);
891 cause = (ret==-2)?34:27;
894 call->pbx_started = 1;
895 ast_setstate(ast, AST_STATE_RING);
899 * incoming setup from LCR
901 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
903 struct ast_channel *ast;
905 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
907 /* create asterisk channel instrance */
909 #ifdef LCR_FOR_CALLWEAVER
910 ast = ast_channel_alloc(1);
913 #ifdef LCR_FOR_ASTERISK
914 #ifdef AST_1_8_OR_HIGHER
915 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
917 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
923 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
924 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
931 ast->tech_pvt = call;
932 ast->tech = &lcr_tech;
933 ast->fds[0] = call->pipe[0];
935 /* fill setup information */
936 if (param->setup.dialinginfo.id)
937 #if ASTERISK_VERSION_NUM < 110000
938 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
939 if (param->setup.context[0])
940 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
942 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
944 strncpy(ast_channel_exten(ast), param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
945 if (param->setup.context[0])
946 strncpy(ast_channel_context(ast), param->setup.context, AST_MAX_CONTEXT-1);
948 strncpy(ast_channel_context(ast), param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
952 #ifdef AST_1_8_OR_HIGHER
953 if (param->setup.callerinfo.id[0]) {
954 ast->caller.id.number.valid = 1;
955 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
956 if (!param->setup.callerinfo.id[0]) {
957 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
958 ast->caller.id.number.plan = (0 << 4) | 1;
960 switch (param->setup.callerinfo.present) {
961 case INFO_PRESENT_ALLOWED:
962 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
964 case INFO_PRESENT_RESTRICTED:
965 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
968 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
970 switch (param->setup.callerinfo.screen) {
971 case INFO_SCREEN_USER:
972 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
974 case INFO_SCREEN_USER_VERIFIED_PASSED:
975 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
977 case INFO_SCREEN_USER_VERIFIED_FAILED:
978 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
981 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
983 switch (param->setup.callerinfo.ntype) {
984 case INFO_NTYPE_SUBSCRIBER:
985 ast->caller.id.number.plan = (4 << 4) | 1;
987 case INFO_NTYPE_NATIONAL:
988 ast->caller.id.number.plan = (2 << 4) | 1;
990 case INFO_NTYPE_INTERNATIONAL:
991 ast->caller.id.number.plan = (1 << 4) | 1;
994 ast->caller.id.number.plan = (0 << 4) | 1;
997 if (param->setup.callerinfo.id2[0]) {
998 ast->caller.ani.number.valid = 1;
999 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
1000 switch (param->setup.callerinfo.present2) {
1001 case INFO_PRESENT_ALLOWED:
1002 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
1004 case INFO_PRESENT_RESTRICTED:
1005 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
1008 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
1010 switch (param->setup.callerinfo.screen2) {
1011 case INFO_SCREEN_USER:
1012 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1014 case INFO_SCREEN_USER_VERIFIED_PASSED:
1015 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1017 case INFO_SCREEN_USER_VERIFIED_FAILED:
1018 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1021 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1023 switch (param->setup.callerinfo.ntype2) {
1024 case INFO_NTYPE_SUBSCRIBER:
1025 ast->caller.ani.number.plan = (4 << 4) | 1;
1027 case INFO_NTYPE_NATIONAL:
1028 ast->caller.ani.number.plan = (2 << 4) | 1;
1030 case INFO_NTYPE_INTERNATIONAL:
1031 ast->caller.ani.number.plan = (1 << 4) | 1;
1034 ast->caller.ani.number.plan = (0 << 4) | 1;
1037 if (param->setup.callerinfo.name[0]) {
1038 ast->caller.id.name.valid = 1;
1039 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
1041 if (param->setup.redirinfo.id[0]) {
1042 ast->redirecting.from.number.valid = 1;
1043 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
1044 switch (param->setup.redirinfo.present) {
1045 case INFO_PRESENT_ALLOWED:
1046 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
1048 case INFO_PRESENT_RESTRICTED:
1049 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1052 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1054 switch (param->setup.redirinfo.screen) {
1055 case INFO_SCREEN_USER:
1056 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1058 case INFO_SCREEN_USER_VERIFIED_PASSED:
1059 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1061 case INFO_SCREEN_USER_VERIFIED_FAILED:
1062 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1065 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1067 switch (param->setup.redirinfo.ntype) {
1068 case INFO_NTYPE_SUBSCRIBER:
1069 ast->redirecting.from.number.plan = (4 << 4) | 1;
1071 case INFO_NTYPE_NATIONAL:
1072 ast->redirecting.from.number.plan = (2 << 4) | 1;
1074 case INFO_NTYPE_INTERNATIONAL:
1075 ast->redirecting.from.number.plan = (1 << 4) | 1;
1078 ast->redirecting.from.number.plan = (0 << 4) | 1;
1082 memset(&ast->cid, 0, sizeof(ast->cid));
1083 if (param->setup.callerinfo.id[0])
1084 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1085 if (param->setup.callerinfo.id2[0])
1086 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1087 if (param->setup.callerinfo.name[0])
1088 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1089 if (param->setup.redirinfo.id[0])
1090 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1091 switch (param->setup.callerinfo.present) {
1092 case INFO_PRESENT_ALLOWED:
1093 ast->cid.cid_pres = AST_PRES_ALLOWED;
1095 case INFO_PRESENT_RESTRICTED:
1096 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1099 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1101 switch (param->setup.callerinfo.ntype) {
1102 case INFO_NTYPE_SUBSCRIBER:
1103 ast->cid.cid_ton = 4;
1105 case INFO_NTYPE_NATIONAL:
1106 ast->cid.cid_ton = 2;
1108 case INFO_NTYPE_INTERNATIONAL:
1109 ast->cid.cid_ton = 1;
1112 ast->cid.cid_ton = 0;
1116 ast->transfercapability = param->setup.capainfo.bearer_capa;
1117 /* enable hdlc if transcap is data */
1118 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1120 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1122 /* configure channel */
1123 #if ASTERISK_VERSION_NUM < 100000
1124 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1125 ast->readformat = ast->rawreadformat = ast->nativeformats;
1126 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1128 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1129 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1130 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1131 ast_set_write_format(ast, &ast->rawwriteformat);
1132 ast_set_read_format(ast, &ast->rawreadformat);
1135 ast->hangupcause = 0;
1138 call->state = CHAN_LCR_STATE_IN_SETUP;
1140 if (!call->pbx_started)
1141 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1145 * incoming setup acknowledge from LCR
1147 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1149 if (!call->ast) return;
1151 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1153 /* send pending digits in dialque */
1154 if (call->dialque[0])
1155 send_dialque_to_lcr(call);
1156 /* change to overlap state */
1157 call->state = CHAN_LCR_STATE_OUT_DIALING;
1161 * incoming proceeding from LCR
1163 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1165 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1168 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1169 /* queue event for asterisk */
1170 if (call->ast && call->pbx_started) {
1174 write(wake_pipe[1], &byte, 1);
1176 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1182 * incoming alerting from LCR
1184 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1186 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1189 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1190 /* queue event to asterisk */
1191 if (call->ast && call->pbx_started) {
1195 write(wake_pipe[1], &byte, 1);
1197 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1202 * incoming connect from LCR
1204 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1206 union parameter newparam;
1208 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1211 call->state = CHAN_LCR_STATE_CONNECT;
1212 /* request bchannel */
1213 if (!call->bchannel) {
1214 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1215 memset(&newparam, 0, sizeof(union parameter));
1216 newparam.bchannel.type = BCHANNEL_REQUEST;
1217 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1219 /* copy connectinfo */
1220 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1221 /* queue event to asterisk */
1222 if (call->ast && call->pbx_started) {
1226 write(wake_pipe[1], &byte, 1);
1228 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1233 * incoming disconnect from LCR
1235 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1237 struct ast_channel *ast = call->ast;
1239 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1242 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1244 call->cause = param->disconnectinfo.cause;
1245 call->location = param->disconnectinfo.location;
1246 /* if bridge, forward disconnect and return */
1249 if (call->bridge_call) {
1250 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1251 bridge_message_if_bridged(call, message_type, param);
1255 /* release lcr with same cause */
1256 send_release_and_import(call, call->cause, call->location);
1258 /* change to release state */
1259 call->state = CHAN_LCR_STATE_RELEASE;
1260 /* queue release asterisk */
1262 ast->hangupcause = call->cause;
1263 if (call->pbx_started) {
1267 write(wake_pipe[1], &byte, 1);
1269 strcpy(call->queue_string, "H"); // overwrite other indications
1271 ast_hangup(ast); // call will be destroyed here
1277 * incoming release from LCR
1279 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1281 struct ast_channel *ast = call->ast;
1283 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1287 /* change to release state */
1288 call->state = CHAN_LCR_STATE_RELEASE;
1289 /* copy release info */
1291 call->cause = param->disconnectinfo.cause;
1292 call->location = param->disconnectinfo.location;
1294 /* if we have an asterisk instance, queue hangup, else we are done */
1296 ast->hangupcause = call->cause;
1297 if (call->pbx_started) {
1301 write(wake_pipe[1], &byte, 1);
1303 strcpy(call->queue_string, "H");
1305 ast_hangup(ast); // call will be destroyed here
1314 * incoming information from LCR
1316 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1318 struct ast_channel *ast = call->ast;
1320 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1324 /* pbx not started */
1325 if (!call->pbx_started) {
1326 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1327 #if ASTERISK_VERSION_NUM < 110000
1328 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1330 ast_channel_exten_set(ast, param->information.id);
1332 lcr_start_pbx(call, ast, param->information.sending_complete);
1336 /* change dailing state after setup */
1337 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1338 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1339 call->state = CHAN_LCR_STATE_IN_DIALING;
1340 // ast_setstate(ast, AST_STATE_DIALING);
1344 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1348 write(wake_pipe[1], &byte, 1);
1350 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1353 /* use bridge to forware message not supported by asterisk */
1354 if (call->state == CHAN_LCR_STATE_CONNECT) {
1355 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1356 bridge_message_if_bridged(call, message_type, param);
1361 * incoming information from LCR
1363 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1365 union parameter newparam;
1367 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1369 /* request bchannel, if call is resumed and we don't have it */
1370 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1371 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1372 memset(&newparam, 0, sizeof(union parameter));
1373 newparam.bchannel.type = BCHANNEL_REQUEST;
1374 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1377 if (!call->ast) return;
1379 /* use bridge to forware message not supported by asterisk */
1380 bridge_message_if_bridged(call, message_type, param);
1384 * incoming information from LCR
1386 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1388 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1390 if (!call->ast) return;
1392 /* use bridge to forware message not supported by asterisk */
1393 bridge_message_if_bridged(call, message_type, param);
1397 * incoming pattern from LCR
1399 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1401 union parameter newparam;
1403 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1405 if (!call->ast) return;
1407 /* pattern are indicated only once */
1408 if (call->has_pattern)
1410 call->has_pattern = 1;
1412 /* request bchannel */
1413 if (!call->bchannel) {
1414 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1415 memset(&newparam, 0, sizeof(union parameter));
1416 newparam.bchannel.type = BCHANNEL_REQUEST;
1417 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1419 /* queue PROGRESS, because tones are available */
1420 if (call->ast && call->pbx_started) {
1424 write(wake_pipe[1], &byte, 1);
1426 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1431 * got dtmf from bchannel (locked state)
1433 void lcr_in_dtmf(struct chan_call *call, int val)
1435 struct ast_channel *ast = call->ast;
1440 if (!call->pbx_started)
1443 if (!call->dsp_dtmf) {
1444 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1448 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1454 write(wake_pipe[1], &byte, 1);
1456 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1460 * message received from LCR
1462 int receive_message(int message_type, unsigned int ref, union parameter *param)
1464 struct bchannel *bchannel;
1465 struct chan_call *call;
1466 union parameter newparam;
1468 memset(&newparam, 0, sizeof(union parameter));
1470 /* handle bchannel message*/
1471 if (message_type == MESSAGE_BCHANNEL) {
1472 switch(param->bchannel.type) {
1473 case BCHANNEL_ASSIGN:
1474 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1475 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1476 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1479 /* create bchannel */
1480 bchannel = alloc_bchannel(param->bchannel.handle);
1482 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1486 /* configure channel */
1487 bchannel->b_tx_gain = param->bchannel.tx_gain;
1488 bchannel->b_rx_gain = param->bchannel.rx_gain;
1489 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1490 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1491 bchannel->b_bf_len = param->bchannel.crypt_len;
1492 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1494 bchannel->b_txdata = 0;
1495 bchannel->b_tx_dejitter = 1;
1497 /* in case, ref is not set, this bchannel instance must
1498 * be created until it is removed again by LCR */
1500 call = find_call_ref(ref);
1502 bchannel->call = call;
1503 call->bchannel = bchannel;
1505 bchannel_dtmf(bchannel, 1);
1507 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1508 if (call->pipeline[0])
1509 bchannel_pipeline(bchannel, call->pipeline);
1511 bchannel_gain(bchannel, call->rx_gain, 0);
1513 bchannel_gain(bchannel, call->tx_gain, 1);
1514 if (call->bridge_id) {
1515 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1516 bchannel_join(bchannel, call->bridge_id);
1518 /* ignore all dsp features, if it is a loopback interface */
1519 if (param->bchannel.isloopback)
1522 /* create only, if call exists, othewhise it bchannel is freed below... */
1523 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1524 bchannel_activate(bchannel, 1);
1527 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1528 newparam.bchannel.handle = param->bchannel.handle;
1529 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1530 /* if call has released before bchannel is assigned */
1532 newparam.bchannel.type = BCHANNEL_RELEASE;
1533 newparam.bchannel.handle = param->bchannel.handle;
1534 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1539 case BCHANNEL_REMOVE:
1540 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1541 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1542 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1545 /* unklink from call and destroy bchannel */
1546 free_bchannel(bchannel);
1549 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1550 newparam.bchannel.handle = param->bchannel.handle;
1551 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1556 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1561 /* handle new ref */
1562 if (message_type == MESSAGE_NEWREF) {
1563 if (param->newref.direction) {
1564 /* new ref from lcr */
1565 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1566 if (!ref || find_call_ref(ref)) {
1567 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1570 /* allocate new call instance */
1571 call = alloc_call();
1573 call->state = CHAN_LCR_STATE_IN_PREPARE;
1576 call->ref_was_assigned = 1;
1577 /* set dtmf (default, use option 'n' to disable */
1579 /* wait for setup (or release from asterisk) */
1581 /* new ref, as requested from this remote application */
1582 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1583 call = find_call_ref(0);
1585 /* send release, if ref does not exist */
1586 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1591 call->ref_was_assigned = 1;
1592 /* set dtmf (default, use option 'n' to disable */
1594 /* send pending setup info */
1595 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1596 send_setup_to_lcr(call);
1597 /* release if asterisk has signed off */
1598 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1601 send_release_and_import(call, call->cause, call->location);
1603 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1614 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1617 call = find_call_ref(ref);
1619 /* ignore ref that is not used (anymore) */
1620 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1624 /* handle messages */
1625 switch(message_type) {
1627 lcr_in_setup(call, message_type, param);
1630 case MESSAGE_OVERLAP:
1631 lcr_in_overlap(call, message_type, param);
1634 case MESSAGE_PROCEEDING:
1635 lcr_in_proceeding(call, message_type, param);
1638 case MESSAGE_ALERTING:
1639 lcr_in_alerting(call, message_type, param);
1642 case MESSAGE_CONNECT:
1643 lcr_in_connect(call, message_type, param);
1646 case MESSAGE_DISCONNECT:
1647 lcr_in_disconnect(call, message_type, param);
1650 case MESSAGE_RELEASE:
1651 lcr_in_release(call, message_type, param);
1654 case MESSAGE_INFORMATION:
1655 lcr_in_information(call, message_type, param);
1658 case MESSAGE_NOTIFY:
1659 lcr_in_notify(call, message_type, param);
1662 case MESSAGE_FACILITY:
1663 lcr_in_facility(call, message_type, param);
1666 case MESSAGE_PATTERN: // audio available from LCR
1667 if (!call->has_pattern)
1668 lcr_in_pattern(call, message_type, param);
1671 case MESSAGE_NOPATTERN: // audio not available from LCR
1674 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1675 call->audiopath = param->audiopath;
1679 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1686 * release all calls (due to broken socket)
1688 static void release_all_calls(void)
1690 struct chan_call *call;
1695 /* no ast, so we may directly free call */
1697 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1701 /* already in release process */
1702 if (call->state == CHAN_LCR_STATE_RELEASE) {
1706 /* release or queue release */
1708 call->state = CHAN_LCR_STATE_RELEASE;
1709 if (!call->pbx_started) {
1710 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1711 ast_hangup(call->ast); // call will be destroyed here
1714 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1718 write(wake_pipe[1], &byte, 1);
1720 strcpy(call->queue_string, "H");
1724 /* release all bchannels */
1725 while(bchannel_first)
1726 free_bchannel(bchannel_first);
1729 void close_socket(void);
1732 * warning! not thread safe
1733 * returns -1 for socket error, 0 for no work, 1 for work
1735 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1738 struct admin_list *admin;
1739 struct admin_message msg;
1741 if ((what & LCR_FD_READ)) {
1742 /* read from socket */
1743 len = read(lcr_sock, &msg, sizeof(msg));
1745 CERROR(NULL, NULL, "Socket closed.(read)\n");
1747 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1749 release_all_calls();
1750 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1754 if (len != sizeof(msg)) {
1755 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1758 if (msg.message != ADMIN_MESSAGE) {
1759 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1762 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1764 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1769 if ((what & LCR_FD_WRITE)) {
1770 /* write to socket */
1772 socket_fd.when &= ~LCR_FD_WRITE;
1775 admin = admin_first;
1776 len = write(lcr_sock, &admin->msg, sizeof(msg));
1778 CERROR(NULL, NULL, "Socket closed.(write)\n");
1782 if (len != sizeof(msg)) {
1783 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1787 admin_first = admin->next;
1791 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1800 * open and close socket and thread
1802 int open_socket(void)
1805 struct sockaddr_un sock_address;
1806 union parameter param;
1809 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1810 CERROR(NULL, NULL, "Failed to create socket.\n");
1814 /* set socket address and name */
1815 memset(&sock_address, 0, sizeof(sock_address));
1816 sock_address.sun_family = PF_UNIX;
1817 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1819 /* connect socket */
1820 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1823 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1827 /* register socket fd */
1828 memset(&socket_fd, 0, sizeof(socket_fd));
1829 socket_fd.fd = lcr_sock;
1830 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1832 /* enque hello message */
1833 memset(¶m, 0, sizeof(param));
1834 strcpy(param.hello.application, "asterisk");
1835 send_message(MESSAGE_HELLO, 0, ¶m);
1840 void close_socket(void)
1842 struct admin_list *admin, *temp;
1844 unregister_fd(&socket_fd);
1846 /* flush pending messages */
1847 admin = admin_first;
1850 admin = admin->next;
1863 /* sending queue to asterisk */
1864 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1868 read(wake_pipe[0], &byte, 1);
1875 static void handle_queue()
1877 struct chan_call *call;
1878 struct ast_channel *ast;
1879 struct ast_frame fr;
1885 p = call->queue_string;
1888 if (ast_channel_trylock(ast)) {
1889 ast_mutex_unlock(&chan_lock);
1891 ast_mutex_lock(&chan_lock);
1897 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1898 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1901 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1902 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1905 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1906 ast_queue_control(ast, AST_CONTROL_RINGING);
1907 ast_setstate(ast, AST_STATE_RINGING);
1910 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1911 ast_queue_control(ast, AST_CONTROL_ANSWER);
1914 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1915 ast_queue_hangup(ast);
1917 case '1': case '2': case '3': case 'A':
1918 case '4': case '5': case '6': case 'B':
1919 case '7': case '8': case '9': case 'C':
1920 case '*': case '0': case '#': case 'D':
1921 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1922 /* send digit to asterisk */
1923 memset(&fr, 0, sizeof(fr));
1925 #ifdef LCR_FOR_ASTERISK
1926 fr.frametype = AST_FRAME_DTMF_BEGIN;
1929 #ifdef LCR_FOR_CALLWEAVER
1930 fr.frametype = AST_FRAME_DTMF;
1933 #ifdef AST_1_8_OR_HIGHER
1934 fr.subclass.integer = *p;
1938 fr.delivery = ast_tv(0, 0);
1939 ast_queue_frame(ast, &fr);
1941 #ifdef LCR_FOR_ASTERISK
1942 fr.frametype = AST_FRAME_DTMF_END;
1943 ast_queue_frame(ast, &fr);
1948 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1952 call->queue_string[0] = '\0';
1953 ast_channel_unlock(ast);
1959 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1961 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1962 if (open_socket() < 0)
1963 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1968 void lock_chan(void)
1970 ast_mutex_lock(&chan_lock);
1973 void unlock_chan(void)
1975 ast_mutex_unlock(&chan_lock);
1978 /* chan_lcr thread */
1979 static void *chan_thread(void *arg)
1981 if (pipe(wake_pipe) < 0) {
1982 CERROR(NULL, NULL, "Failed to open pipe.\n");
1985 memset(&wake_fd, 0, sizeof(wake_fd));
1986 wake_fd.fd = wake_pipe[0];
1987 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1989 memset(&socket_retry, 0, sizeof(socket_retry));
1990 add_timer(&socket_retry, handle_retry, NULL, 0);
1992 bchannel_pid = getpid();
1994 /* open socket the first time */
1995 handle_retry(NULL, NULL, 0);
1997 ast_mutex_lock(&chan_lock);
2001 select_main(0, &global_change, lock_chan, unlock_chan);
2008 * new asterisk instance
2011 #ifdef AST_1_8_OR_HIGHER
2012 #if ASTERISK_VERSION_NUM < 100000
2013 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2015 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2018 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2021 char exten[256], *dial, *interface, *opt;
2022 struct ast_channel *ast;
2023 struct chan_call *call;
2025 ast_mutex_lock(&chan_lock);
2026 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2028 /* if socket is closed */
2030 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2031 ast_mutex_unlock(&chan_lock);
2035 /* create call instance */
2036 call = alloc_call();
2038 /* failed to create instance */
2039 ast_mutex_unlock(&chan_lock);
2043 /* create asterisk channel instrance */
2045 #ifdef LCR_FOR_ASTERISK
2046 #ifdef AST_1_8_OR_HIGHER
2047 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2049 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2053 #ifdef LCR_FOR_CALLWEAVER
2054 ast = ast_channel_alloc(1);
2058 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2060 /* failed to create instance */
2061 ast_mutex_unlock(&chan_lock);
2064 ast->tech = &lcr_tech;
2065 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2066 /* configure channel */
2067 #if ASTERISK_VERSION_NUM < 100000
2068 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2069 ast->readformat = ast->rawreadformat = ast->nativeformats;
2070 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2072 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2073 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2074 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2075 ast_set_write_format(ast, &ast->rawwriteformat);
2076 ast_set_read_format(ast, &ast->rawreadformat);
2079 ast->hangupcause = 0;
2083 ast->tech_pvt = call;
2084 ast->fds[0] = call->pipe[0];
2085 call->pbx_started = 0;
2087 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2090 * Extract interface, dialstring, options from data.
2093 * <interface>/<dialstring>
2094 * <interface>/<dialstring>/options
2096 strncpy(exten, (char *)data, sizeof(exten)-1);
2097 exten[sizeof(exten)-1] = '\0';
2098 if ((dial = strchr(exten, '/'))) {
2101 if ((opt = strchr(dial, '/')))
2110 strncpy(call->interface, interface, sizeof(call->interface)-1);
2111 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2112 apply_opt(call, (char *)opt);
2114 #ifdef AST_1_8_OR_HIGHER
2115 // clone_variables(requestor, ast);
2118 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2119 if (requestor->caller.ani.number.valid)
2120 if (requestor->caller.ani.number.str)
2121 if (requestor->caller.ani.number.str[0])
2122 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2123 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2124 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2126 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2127 if (requestor->caller.ani.name.valid)
2128 if (requestor->caller.ani.name.str)
2129 if (requestor->caller.ani.name.str[0])
2130 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2131 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2133 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2134 if (requestor->caller.ani.subaddress.valid)
2135 if (requestor->caller.ani.subaddress.str)
2136 if (requestor->caller.ani.subaddress.str[0])
2137 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2138 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2140 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2141 if (requestor->caller.id.number.valid)
2142 if (requestor->caller.id.number.str)
2143 if (requestor->caller.id.number.str[0])
2144 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2145 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2146 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2148 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2149 if (requestor->caller.id.name.valid)
2150 if (requestor->caller.id.name.str)
2151 if (requestor->caller.id.name.str[0])
2152 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2153 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2155 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2156 if (requestor->caller.id.subaddress.valid)
2157 if (requestor->caller.id.subaddress.str)
2158 if (requestor->caller.id.subaddress.str[0])
2159 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2160 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2162 if (requestor->dialed.number.str)
2163 if (requestor->dialed.number.str[0])
2164 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2165 ast->dialed.number.plan= requestor->dialed.number.plan;
2167 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2168 if (requestor->dialed.subaddress.valid)
2169 if (requestor->dialed.subaddress.str)
2170 if (requestor->dialed.subaddress.str[0])
2171 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2172 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2174 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2175 ast->redirecting.count= requestor->redirecting.count;
2176 ast->redirecting.reason= requestor->redirecting.reason;
2178 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2179 if (requestor->redirecting.from.number.valid)
2180 if (requestor->redirecting.from.number.str)
2181 if (requestor->redirecting.from.number.str[0])
2182 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2183 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2184 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2186 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2187 if (requestor->redirecting.to.number.valid)
2188 if (requestor->redirecting.to.number.str)
2189 if (requestor->redirecting.to.number.str[0])
2190 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2191 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2192 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2194 /* store call information for setup */
2197 if (requestor && requestor->caller.id.number.valid) {
2198 if (requestor->caller.id.number.str)
2199 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2200 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2201 case AST_PRES_RESTRICTED:
2202 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2204 case AST_PRES_UNAVAILABLE:
2205 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2207 case AST_PRES_ALLOWED:
2209 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2211 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2212 case AST_PRES_USER_NUMBER_UNSCREENED:
2213 call->callerinfo.screen = INFO_SCREEN_USER;
2215 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2216 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2218 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2219 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2222 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2224 switch((requestor->caller.id.number.plan >> 4) & 7) {
2226 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2229 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2232 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2235 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2238 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2241 if (requestor && requestor->caller.ani.number.valid) {
2242 if (requestor->caller.ani.number.str)
2243 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2244 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2245 case AST_PRES_RESTRICTED:
2246 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2248 case AST_PRES_UNAVAILABLE:
2249 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2251 case AST_PRES_ALLOWED:
2253 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2255 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2256 case AST_PRES_USER_NUMBER_UNSCREENED:
2257 call->callerinfo.screen2 = INFO_SCREEN_USER;
2259 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2260 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2262 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2263 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2266 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2268 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2270 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2273 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2276 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2279 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2282 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2285 if (requestor && requestor->caller.id.name.valid) {
2286 if (requestor->caller.id.name.str)
2287 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2291 if (requestor && requestor->redirecting.from.number.valid) {
2292 call->redirinfo.itype = INFO_ITYPE_CHAN;
2293 if (requestor->redirecting.from.number.str)
2294 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2295 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2296 case AST_PRES_RESTRICTED:
2297 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2299 case AST_PRES_UNAVAILABLE:
2300 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2302 case AST_PRES_ALLOWED:
2304 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2306 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2307 case AST_PRES_USER_NUMBER_UNSCREENED:
2308 call->redirinfo.screen = INFO_SCREEN_USER;
2310 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2311 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2313 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2314 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2317 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2319 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2321 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2324 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2327 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2330 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2335 ast_mutex_unlock(&chan_lock);
2340 * call from asterisk
2342 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2344 union parameter newparam;
2345 struct chan_call *call;
2347 ast_mutex_lock(&chan_lock);
2348 call = ast->tech_pvt;
2350 #ifdef LCR_FOR_CALLWEAVER
2352 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2356 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2357 ast_mutex_unlock(&chan_lock);
2361 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2363 /* pbx process is started */
2364 call->pbx_started = 1;
2365 /* send MESSAGE_NEWREF */
2366 memset(&newparam, 0, sizeof(union parameter));
2367 newparam.newref.direction = 0; /* request from app */
2368 if (!strcmp(call->interface, "pbx"))
2369 newparam.newref.mode = 1;
2370 send_message(MESSAGE_NEWREF, 0, &newparam);
2372 /* set hdlc if capability requires hdlc */
2373 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2374 || ast->transfercapability == INFO_BC_DATARESTRICTED
2375 || ast->transfercapability == INFO_BC_VIDEO)
2377 /* if hdlc is forced by option, we change transcap to data */
2379 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2380 && ast->transfercapability != INFO_BC_DATARESTRICTED
2381 && ast->transfercapability != INFO_BC_VIDEO)
2382 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2384 #ifndef AST_1_8_OR_HIGHER
2385 call->cid_num[0] = 0;
2386 call->cid_name[0] = 0;
2387 call->cid_rdnis[0] = 0;
2389 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2390 strncpy(call->cid_num, ast->cid.cid_num,
2391 sizeof(call->cid_num)-1);
2392 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2393 strncpy(call->cid_name, ast->cid.cid_name,
2394 sizeof(call->cid_name)-1);
2395 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2396 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2397 sizeof(call->cid_rdnis)-1);
2400 ast_mutex_unlock(&chan_lock);
2404 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2406 static const char* dtmf_tones[] = {
2407 "!941+1336/100,!0/100", /* 0 */
2408 "!697+1209/100,!0/100", /* 1 */
2409 "!697+1336/100,!0/100", /* 2 */
2410 "!697+1477/100,!0/100", /* 3 */
2411 "!770+1209/100,!0/100", /* 4 */
2412 "!770+1336/100,!0/100", /* 5 */
2413 "!770+1477/100,!0/100", /* 6 */
2414 "!852+1209/100,!0/100", /* 7 */
2415 "!852+1336/100,!0/100", /* 8 */
2416 "!852+1477/100,!0/100", /* 9 */
2417 "!697+1633/100,!0/100", /* A */
2418 "!770+1633/100,!0/100", /* B */
2419 "!852+1633/100,!0/100", /* C */
2420 "!941+1633/100,!0/100", /* D */
2421 "!941+1209/100,!0/100", /* * */
2422 "!941+1477/100,!0/100" }; /* # */
2424 if (digit >= '0' && digit <='9')
2425 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2426 else if (digit >= 'A' && digit <= 'D')
2427 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2428 else if (digit == '*')
2429 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2430 else if (digit == '#')
2431 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2433 #if ASTERISK_VERSION_NUM < 110000
2434 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2436 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2441 #ifdef LCR_FOR_ASTERISK
2442 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2444 #ifdef LCR_FOR_CALLWEAVER
2445 static int lcr_digit(struct ast_channel *ast, char digit)
2448 struct chan_call *call;
2449 union parameter newparam;
2452 #ifdef LCR_FOR_CALLWEAVER
2453 int inband_dtmf = 0;
2456 /* only pass IA5 number space */
2457 if (digit > 126 || digit < 32)
2460 ast_mutex_lock(&chan_lock);
2461 call = ast->tech_pvt;
2463 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2464 ast_mutex_unlock(&chan_lock);
2468 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2470 /* send information or queue them */
2471 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2472 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2473 memset(&newparam, 0, sizeof(union parameter));
2475 newparam.information.keypad[0] = digit;
2476 newparam.information.keypad[1] = '\0';
2478 newparam.information.id[0] = digit;
2479 newparam.information.id[1] = '\0';
2481 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2484 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2485 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2487 strncat(call->dialque, buf, strlen(call->dialque)-1);
2490 ast_mutex_unlock(&chan_lock);
2492 #ifdef LCR_FOR_ASTERISK
2496 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2498 int inband_dtmf = 0;
2499 struct chan_call *call;
2502 ast_mutex_lock(&chan_lock);
2504 call = ast->tech_pvt;
2508 "Received digit from Asterisk, "
2509 "but no call instance exists.\n");
2510 ast_mutex_unlock(&chan_lock);
2514 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2516 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2520 ast_mutex_unlock(&chan_lock);
2523 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2524 send_digit_to_chan(ast, digit);
2530 static int lcr_answer(struct ast_channel *ast)
2532 union parameter newparam;
2533 struct chan_call *call;
2535 ast_mutex_lock(&chan_lock);
2536 call = ast->tech_pvt;
2538 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2539 ast_mutex_unlock(&chan_lock);
2543 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2545 /* copy connectinfo, if bridged */
2546 if (call->bridge_call)
2547 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2548 /* send connect message to lcr */
2549 if (call->state != CHAN_LCR_STATE_CONNECT) {
2550 memset(&newparam, 0, sizeof(union parameter));
2551 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2552 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2553 call->state = CHAN_LCR_STATE_CONNECT;
2556 /* request bchannel */
2557 if (!call->bchannel) {
2558 CDEBUG(call, ast, "Requesting B-channel.\n");
2559 memset(&newparam, 0, sizeof(union parameter));
2560 newparam.bchannel.type = BCHANNEL_REQUEST;
2561 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2564 // memset(&newparam, 0, sizeof(union parameter));
2565 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2567 ast_mutex_unlock(&chan_lock);
2571 static int lcr_hangup(struct ast_channel *ast)
2573 struct chan_call *call;
2574 pthread_t tid = pthread_self();
2576 if (!pthread_equal(tid, chan_tid)) {
2577 ast_mutex_lock(&chan_lock);
2579 call = ast->tech_pvt;
2581 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2582 if (!pthread_equal(tid, chan_tid)) {
2583 ast_mutex_unlock(&chan_lock);
2588 if (!pthread_equal(tid, chan_tid))
2589 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2591 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2593 /* disconnect asterisk, maybe not required */
2594 ast->tech_pvt = NULL;
2598 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2599 if (ast->hangupcause > 0)
2600 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2602 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2605 if (!pthread_equal(tid, chan_tid)) {
2606 ast_mutex_unlock(&chan_lock);
2610 /* ref is not set, due to prepare setup or release */
2611 if (call->state == CHAN_LCR_STATE_RELEASE) {
2612 /* we get the response to our release */
2613 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2616 /* during prepare, we change to release state */
2617 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2618 call->state = CHAN_LCR_STATE_RELEASE;
2622 if (!pthread_equal(tid, chan_tid)) {
2623 ast_mutex_unlock(&chan_lock);
2628 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2630 struct chan_call *call;
2631 struct ast_frame * f = fr;
2633 #if ASTERISK_VERSION_NUM < 100000
2634 #ifdef AST_1_8_OR_HIGHER
2635 if (!f->subclass.codec)
2639 CDEBUG(NULL, ast, "No subclass\n");
2641 #ifdef AST_1_8_OR_HIGHER
2642 #if ASTERISK_VERSION_NUM < 100000
2643 if (!(f->subclass.codec & ast->nativeformats)) {
2645 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2648 if (!(f->subclass & ast->nativeformats)) {
2651 "Unexpected format. "
2652 "Activating emergency conversion...\n");
2654 #ifdef AST_1_8_OR_HIGHER
2655 #if ASTERISK_VERSION_NUM < 100000
2656 ast_set_write_format(ast, f->subclass.codec);
2658 ast_set_write_format(ast, &f->subclass.format);
2661 ast_set_write_format(ast, f->subclass);
2663 f = (ast->writetrans) ? ast_translate(
2664 ast->writetrans, fr, 0) : fr;
2667 ast_mutex_lock(&chan_lock);
2668 call = ast->tech_pvt;
2670 ast_mutex_unlock(&chan_lock);
2676 if (call->bchannel && f->samples)
2677 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2678 ast_mutex_unlock(&chan_lock);
2686 static struct ast_frame *lcr_read(struct ast_channel *ast)
2688 struct chan_call *call;
2691 ast_mutex_lock(&chan_lock);
2692 call = ast->tech_pvt;
2694 ast_mutex_unlock(&chan_lock);
2697 if (call->pipe[0] > -1) {
2698 if (call->rebuffer && !call->hdlc) {
2699 /* Make sure we have a complete 20ms (160byte) frame */
2700 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2702 call->framepos += len;
2705 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2707 if (len < 0 && errno == EAGAIN) {
2708 ast_mutex_unlock(&chan_lock);
2710 #ifdef LCR_FOR_ASTERISK
2711 return &ast_null_frame;
2714 #ifdef LCR_FOR_CALLWEAVER
2720 close(call->pipe[0]);
2723 ast_mutex_unlock(&chan_lock);
2725 } else if (call->rebuffer && call->framepos < 160) {
2726 /* Not a complete frame, so we send a null-frame */
2727 ast_mutex_unlock(&chan_lock);
2728 return &ast_null_frame;
2732 call->read_fr.frametype = AST_FRAME_VOICE;
2733 #ifdef AST_1_8_OR_HIGHER
2734 #if ASTERISK_VERSION_NUM < 100000
2735 call->read_fr.subclass.codec = ast->nativeformats;
2737 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2738 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2741 call->read_fr.subclass = ast->nativeformats;
2743 if (call->rebuffer) {
2744 call->read_fr.datalen = call->framepos;
2745 call->read_fr.samples = call->framepos;
2748 call->read_fr.datalen = len;
2749 call->read_fr.samples = len;
2751 call->read_fr.delivery = ast_tv(0,0);
2752 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2753 ast_mutex_unlock(&chan_lock);
2755 return &call->read_fr;
2758 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2760 union parameter newparam;
2762 struct chan_call *call;
2763 const struct tone_zone_sound *ts = NULL;
2765 ast_mutex_lock(&chan_lock);
2766 call = ast->tech_pvt;
2768 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2769 ast_mutex_unlock(&chan_lock);
2774 case AST_CONTROL_BUSY:
2775 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2776 ast_setstate(ast, AST_STATE_BUSY);
2777 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2778 /* send message to lcr */
2779 memset(&newparam, 0, sizeof(union parameter));
2780 newparam.disconnectinfo.cause = 17;
2781 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2782 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2784 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2786 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2787 ts = ast_get_indication_tone(ast->zone, "busy");
2790 case AST_CONTROL_CONGESTION:
2791 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2792 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2793 /* send message to lcr */
2794 memset(&newparam, 0, sizeof(union parameter));
2795 newparam.disconnectinfo.cause = ast->hangupcause;
2796 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2797 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2799 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2801 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2802 ts = ast_get_indication_tone(ast->zone, "congestion");
2805 case AST_CONTROL_PROCEEDING:
2806 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2807 if (call->state == CHAN_LCR_STATE_IN_SETUP
2808 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2809 /* send message to lcr */
2810 memset(&newparam, 0, sizeof(union parameter));
2811 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2813 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2816 case AST_CONTROL_RINGING:
2817 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2818 ast_setstate(ast, AST_STATE_RING);
2819 if (call->state == CHAN_LCR_STATE_IN_SETUP
2820 || call->state == CHAN_LCR_STATE_IN_DIALING
2821 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2822 /* send message to lcr */
2823 memset(&newparam, 0, sizeof(union parameter));
2824 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2826 call->state = CHAN_LCR_STATE_IN_ALERTING;
2828 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2829 ts = ast_get_indication_tone(ast->zone, "ring");
2832 case AST_CONTROL_PROGRESS:
2833 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2834 /* request bchannel */
2835 if (!call->bchannel) {
2836 CDEBUG(call, ast, "Requesting B-channel.\n");
2837 memset(&newparam, 0, sizeof(union parameter));
2838 newparam.bchannel.type = BCHANNEL_REQUEST;
2839 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2843 CDEBUG(call, ast, "Received indicate -1.\n");
2844 ast_playtones_stop(ast);
2848 case AST_CONTROL_VIDUPDATE:
2849 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2852 case AST_CONTROL_HOLD:
2853 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2854 /* send message to lcr */
2855 memset(&newparam, 0, sizeof(union parameter));
2856 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2857 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2859 /*start music onhold*/
2860 #ifdef LCR_FOR_ASTERISK
2861 #if ASTERISK_VERSION_NUM <110000
2862 ast_moh_start(ast,data,ast->musicclass);
2864 ast_moh_start(ast,data,ast_channel_musicclass(ast));
2868 #ifdef LCR_FOR_CALLWEAVER
2869 ast_moh_start(ast, NULL);
2874 case AST_CONTROL_UNHOLD:
2875 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2876 /* send message to lcr */
2877 memset(&newparam, 0, sizeof(union parameter));
2878 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2879 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2885 #ifdef AST_CONTROL_SRCUPDATE
2886 case AST_CONTROL_SRCUPDATE:
2890 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2893 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2898 if (ts && ts->data[0]) {
2899 ast_playtones_start(ast, 0, ts->data, 1);
2903 ast_mutex_unlock(&chan_lock);
2910 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2912 struct chan_call *call;
2918 ast_mutex_lock(&chan_lock);
2919 call = ast->tech_pvt;
2921 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2922 ast_mutex_unlock(&chan_lock);
2926 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2928 ast_mutex_unlock(&chan_lock);
2933 * send_text asterisk
2935 static int lcr_send_text(struct ast_channel *ast, const char *text)
2937 struct chan_call *call;
2938 union parameter newparam;
2940 ast_mutex_lock(&chan_lock);
2941 call = ast->tech_pvt;
2943 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2944 ast_mutex_unlock(&chan_lock);
2948 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2949 memset(&newparam, 0, sizeof(union parameter));
2950 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2951 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2952 ast_mutex_unlock(&chan_lock);
2959 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2960 struct ast_channel *ast2, int flags,
2961 struct ast_frame **fo,
2962 struct ast_channel **rc, int timeoutms)
2965 struct chan_call *call1, *call2;
2966 struct ast_channel *carr[2], *who;
2968 struct ast_frame *f;
2971 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2976 /* join via dsp (if the channels are currently open) */
2977 ast_mutex_lock(&chan_lock);
2978 call1 = ast1->tech_pvt;
2979 call2 = ast2->tech_pvt;
2980 if (!call1 || !call2) {
2981 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2982 ast_mutex_unlock(&chan_lock);
2983 return AST_BRIDGE_COMPLETE;
2986 /* join, if both call instances uses dsp
2987 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2989 if (!call1->nodsp && !call2->nodsp) {
2990 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2992 /* get bridge id and join */
2993 bridge_id = new_bridge_id();
2995 call1->bridge_id = bridge_id;
2996 if (call1->bchannel)
2997 bchannel_join(call1->bchannel, bridge_id);
2999 call2->bridge_id = bridge_id;
3000 if (call2->bchannel)
3001 bchannel_join(call2->bchannel, bridge_id);
3003 if (call1->nodsp && call2->nodsp)
3004 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3006 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3007 call1->bridge_call = call2;
3008 call2->bridge_call = call1;
3010 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3011 || call1->state == CHAN_LCR_STATE_IN_DIALING
3012 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3013 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3014 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3017 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3018 || call2->state == CHAN_LCR_STATE_IN_DIALING
3019 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3020 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3021 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3025 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3026 so let's do it for them. Hmpf.
3029 if (call1->on_hold) {
3030 union parameter newparam;
3032 memset(&newparam, 0, sizeof(union parameter));
3033 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3034 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3039 if (call2->on_hold) {
3040 union parameter newparam;
3042 memset(&newparam, 0, sizeof(union parameter));
3043 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3044 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3049 ast_mutex_unlock(&chan_lock);
3053 who = ast_waitfor_n(carr, 2, &to);
3056 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3061 if (!f || f->frametype == AST_FRAME_CONTROL) {
3063 CDEBUG(NULL, NULL, "Got hangup.\n");
3065 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3072 if ( f->frametype == AST_FRAME_DTMF ) {
3073 CDEBUG(NULL, NULL, "Got DTMF.\n");
3089 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3091 /* split channels */
3092 ast_mutex_lock(&chan_lock);
3093 call1 = ast1->tech_pvt;
3094 call2 = ast2->tech_pvt;
3095 if (call1 && call1->bridge_id) {
3096 call1->bridge_id = 0;
3097 if (call1->bchannel)
3098 bchannel_join(call1->bchannel, 0);
3099 if (call1->bridge_call)
3100 call1->bridge_call->bridge_call = NULL;
3102 if (call2 && call1->bridge_id) {
3103 call2->bridge_id = 0;
3104 if (call2->bchannel)
3105 bchannel_join(call2->bchannel, 0);
3106 if (call2->bridge_call)
3107 call2->bridge_call->bridge_call = NULL;
3109 call1->bridge_call = NULL;
3110 call2->bridge_call = NULL;
3112 ast_mutex_unlock(&chan_lock);
3113 return AST_BRIDGE_COMPLETE;
3115 static struct ast_channel_tech lcr_tech = {
3117 .description = "Channel driver for connecting to Linux-Call-Router",
3118 #if ASTERISK_VERSION_NUM < 100000
3119 .capabilities = AST_FORMAT_ALAW,
3121 .requester = lcr_request,
3123 #ifdef LCR_FOR_ASTERISK
3124 .send_digit_begin = lcr_digit_begin,
3125 .send_digit_end = lcr_digit_end,
3128 #ifdef LCR_FOR_CALLWEAVER
3129 .send_digit = lcr_digit,
3133 .bridge = lcr_bridge,
3134 .hangup = lcr_hangup,
3135 .answer = lcr_answer,
3138 .indicate = lcr_indicate,
3140 .send_text = lcr_send_text,
3149 static int lcr_show_lcr (int fd, int argc, char *argv[])
3154 static int lcr_show_calls (int fd, int argc, char *argv[])
3159 static int lcr_reload_routing (int fd, int argc, char *argv[])
3164 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3169 static int lcr_port_block (int fd, int argc, char *argv[])
3174 static int lcr_port_unblock (int fd, int argc, char *argv[])
3179 static int lcr_port_unload (int fd, int argc, char *argv[])
3184 static struct ast_cli_entry cli_show_lcr =
3185 { {"lcr", "show", "lcr", NULL},
3187 "Shows current states of LCR core",
3188 "Usage: lcr show lcr\n",
3191 static struct ast_cli_entry cli_show_calls =
3192 { {"lcr", "show", "calls", NULL},
3194 "Shows current calls made by LCR and Asterisk",
3195 "Usage: lcr show calls\n",
3198 static struct ast_cli_entry cli_reload_routing =
3199 { {"lcr", "reload", "routing", NULL},
3201 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3202 "Usage: lcr reload routing\n",
3205 static struct ast_cli_entry cli_reload_interfaces =
3206 { {"lcr", "reload", "interfaces", NULL},
3207 lcr_reload_interfaces,
3208 "Reloads interfaces conf of LCR",
3209 "Usage: lcr reload interfaces\n",
3212 static struct ast_cli_entry cli_port_block =
3213 { {"lcr", "port", "block", NULL},
3215 "Blocks LCR port for further calls",
3216 "Usage: lcr port block \"<port>\"\n",
3219 static struct ast_cli_entry cli_port_unblock =
3220 { {"lcr", "port", "unblock", NULL},
3222 "Unblocks or loads LCR port, port is opened my mISDN",
3223 "Usage: lcr port unblock \"<port>\"\n",
3226 static struct ast_cli_entry cli_port_unload =
3227 { {"lcr", "port", "unload", NULL},
3229 "Unloads LCR port, port is closes by mISDN",
3230 "Usage: lcr port unload \"<port>\"\n",
3235 #ifdef LCR_FOR_ASTERISK
3236 #ifdef AST_1_8_OR_HIGHER
3237 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3239 static int lcr_config_exec(struct ast_channel *ast, void *data)
3243 #ifdef LCR_FOR_CALLWEAVER
3244 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3247 struct chan_call *call;
3249 ast_mutex_lock(&chan_lock);
3251 #ifdef LCR_FOR_ASTERISK
3252 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3255 #ifdef LCR_FOR_CALLWEAVER
3256 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3262 if (call->ast == ast)
3268 #ifdef LCR_FOR_ASTERISK
3269 apply_opt(call, (char *)data);
3272 #ifdef LCR_FOR_CALLWEAVER
3273 apply_opt(call, (char *)argv[0]);
3277 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3279 ast_mutex_unlock(&chan_lock);
3284 * module loading and destruction
3286 int load_module(void)
3289 char options_error[256];
3291 for (i = 0; i < 256; i++) {
3292 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3293 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3296 if (read_options(options_error) == 0) {
3297 CERROR(NULL, NULL, "%s", options_error);
3299 #ifdef LCR_FOR_ASTERISK
3300 return AST_MODULE_LOAD_DECLINE;
3303 #ifdef LCR_FOR_CALLWEAVER
3309 ast_mutex_init(&chan_lock);
3310 ast_mutex_init(&log_lock);
3312 if (bchannel_initialize()) {
3313 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3316 #ifdef LCR_FOR_ASTERISK
3317 return AST_MODULE_LOAD_DECLINE;
3320 #ifdef LCR_FOR_CALLWEAVER
3326 #if ASTERISK_VERSION_NUM < 100000
3327 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3329 struct ast_format tmp;
3330 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3331 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3332 return AST_MODULE_LOAD_DECLINE;
3334 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3336 if (ast_channel_register(&lcr_tech)) {
3337 CERROR(NULL, NULL, "Unable to register channel class\n");
3338 bchannel_deinitialize();
3341 #ifdef LCR_FOR_ASTERISK
3342 return AST_MODULE_LOAD_DECLINE;
3345 #ifdef LCR_FOR_CALLWEAVER
3350 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3352 #ifdef LCR_FOR_ASTERISK
3353 "lcr_config(<opt><optarg>:<opt>:...)\n"
3356 #ifdef LCR_FOR_CALLWEAVER
3357 "lcr_config(<opt><optarg>:<opt>:...)\n",
3360 "Sets LCR opts. and optargs\n"
3362 "The available options are:\n"
3363 " d - Send display text on called phone, text is the optarg.\n"
3364 " n - Don't detect dtmf tones on called channel.\n"
3365 " h - Force data call (HDLC).\n"
3366 " t - Disable mISDN_dsp features (required for fax application).\n"
3367 " q - Add queue to make fax stream seamless (required for fax app).\n"
3368 " Use queue size in miliseconds for optarg. (try 250)\n"
3369 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3370 " Use time to detect for optarg.\n"
3371 " c - Make crypted outgoing call, optarg is keyindex.\n"
3372 " e - Perform echo cancelation on this channel.\n"
3373 " Takes mISDN pipeline option as optarg.\n"
3374 " s - Send Non Inband DTMF as inband.\n"
3375 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3376 " vr - rxgain control\n"
3377 " vt - txgain control\n"
3378 " Volume changes at factor 2 ^ optarg.\n"
3379 " k - use keypad to dial this call.\n"
3381 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3382 " -> use 16 for fax (3.1k audio)\n"
3384 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3385 "options: \"n:t:q250\" for seamless audio transmission.\n"
3390 ast_cli_register(&cli_show_lcr);
3391 ast_cli_register(&cli_show_calls);
3392 ast_cli_register(&cli_reload_routing);
3393 ast_cli_register(&cli_reload_interfaces);
3394 ast_cli_register(&cli_port_block);
3395 ast_cli_register(&cli_port_unblock);
3396 ast_cli_register(&cli_port_unload);
3399 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3400 /* failed to create thread */
3401 bchannel_deinitialize();
3403 ast_channel_unregister(&lcr_tech);
3405 #ifdef LCR_FOR_ASTERISK
3406 return AST_MODULE_LOAD_DECLINE;
3409 #ifdef LCR_FOR_CALLWEAVER
3417 int unload_module(void)
3419 /* First, take us out of the channel loop */
3420 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3422 pthread_cancel(chan_tid);
3426 del_timer(&socket_retry);
3428 unregister_fd(&wake_fd);
3429 close(wake_pipe[0]);
3430 close(wake_pipe[1]);
3432 // ast_mutex_unlock(&chan_lock);
3434 ast_channel_unregister(&lcr_tech);
3436 ast_unregister_application("lcr_config");
3438 if (mISDN_created) {
3439 bchannel_deinitialize();
3443 if (lcr_sock >= 0) {
3448 #if ASTERISK_VERSION_NUM >= 100000
3449 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3454 int reload_module(void)
3460 #ifdef LCR_FOR_ASTERISK
3461 #define AST_MODULE "chan_lcr"
3464 #ifdef LCR_FOR_CALLWEAVER
3469 ast_mutex_lock(&usecnt_lock);
3471 ast_mutex_unlock(&usecnt_lock);
3476 #ifdef LCR_FOR_ASTERISK
3477 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3478 .load = load_module,
3479 .unload = unload_module,
3480 .reload = reload_module,
3484 #ifdef LCR_FOR_CALLWEAVER
3485 char *description(void)