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 #if ASTERISK_VERSION_NUM < 110000
165 #include <asterisk/version.h>
167 #include "extension.h"
169 #include "callerid.h"
170 #include "lcrsocket.h"
173 #include "bchannel.h"
175 #include "chan_lcr.h"
177 CHAN_LCR_STATE // state description structure
178 MESSAGES // message text
180 #ifdef LCR_FOR_CALLWEAVER
181 AST_MUTEX_DEFINE_STATIC(rand_lock);
184 unsigned char flip_bits[256];
186 #ifdef LCR_FOR_CALLWEAVER
187 static struct ast_frame nullframe = { AST_FRAME_NULL, };
193 char lcr_type[]="lcr";
195 #ifdef LCR_FOR_CALLWEAVER
196 static ast_mutex_t usecnt_lock;
198 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
202 ast_mutex_t chan_lock; /* global lock */
203 ast_mutex_t log_lock; /* logging log */
205 * used to indicate change in file descriptors, so select function's result may
208 int global_change = 0;
211 struct lcr_fd wake_fd;
213 int glob_channel = 0;
216 struct lcr_fd socket_fd;
217 struct lcr_timer socket_retry;
220 struct admin_list *next;
221 struct admin_message msg;
222 } *admin_first = NULL;
224 static struct ast_channel_tech lcr_tech;
229 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
232 char call_text[128] = "NULL";
233 char ast_text[128] = "NULL";
236 ast_mutex_lock(&log_lock);
239 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
240 buffer[sizeof(buffer)-1]=0;
244 sprintf(call_text, "%d", call->ref);
246 #if ASTERISK_VERSION_NUM < 110000
247 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
249 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
251 ast_text[sizeof(ast_text)-1] = '\0';
253 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
254 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
256 ast_mutex_unlock(&log_lock);
260 * channel and call instances
262 struct chan_call *call_first;
266 * special case: 0: find new ref, that has not been assigned a ref yet
269 struct chan_call *find_call_ref(unsigned int ref)
271 struct chan_call *call = call_first;
272 int assigned = (ref > 0);
275 if (call->ref == ref && call->ref_was_assigned == assigned)
282 void free_call(struct chan_call *call)
284 struct chan_call **temp = &call_first;
288 *temp = (*temp)->next;
289 if (call->pipe[0] > -1)
290 close(call->pipe[0]);
291 if (call->pipe[1] > -1)
292 close(call->pipe[1]);
293 if (call->bchannel) {
294 if (call->bchannel->call != call)
295 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
296 call->bchannel->call = NULL;
298 if (call->bridge_call) {
299 if (call->bridge_call->bridge_call != call)
300 CERROR(call, NULL, "Linked call structure has no link to us.\n");
301 call->bridge_call->bridge_call = NULL;
304 ast_translator_free_path(call->trans);
306 ast_dsp_free(call->dsp);
307 CDEBUG(call, NULL, "Call instance freed.\n");
312 temp = &((*temp)->next);
314 CERROR(call, NULL, "Call instance not found in list.\n");
317 struct chan_call *alloc_call(void)
319 struct chan_call **callp = &call_first;
322 callp = &((*callp)->next);
324 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
326 memset(*callp, 0, sizeof(struct chan_call));
327 if (pipe((*callp)->pipe) < 0) {
328 CERROR(*callp, NULL, "Failed to create pipe.\n");
332 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
333 CDEBUG(*callp, NULL, "Call instance allocated.\n");
337 unsigned short new_bridge_id(void)
339 struct chan_call *call;
340 unsigned short id = 1;
342 /* search for lowest bridge id that is not in use and not 0 */
346 if (call->bridge_id == id)
354 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
359 * enque message to LCR
361 int send_message(int message_type, unsigned int ref, union parameter *param)
363 struct admin_list *admin, **adminp;
366 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
369 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
371 adminp = &admin_first;
373 adminp = &((*adminp)->next);
374 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
376 CERROR(NULL, NULL, "No memory for message to LCR.\n");
381 admin->msg.message = ADMIN_MESSAGE;
382 admin->msg.u.msg.type = message_type;
383 admin->msg.u.msg.ref = ref;
384 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
385 socket_fd.when |= LCR_FD_WRITE;
389 write(wake_pipe[1], &byte, 1);
396 * apply options (in locked state)
398 void apply_opt(struct chan_call *call, char *data)
400 union parameter newparam;
401 char string[1024], *p = string, *opt, *key;
407 strncpy(string, data, sizeof(string)-1);
408 string[sizeof(string)-1] = '\0';
411 while((opt = strsep(&p, ":"))) {
414 if (opt[1] == '\0') {
415 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
418 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
419 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
420 strncpy(call->display, opt+1, sizeof(call->display)-1);
422 memset(&newparam, 0, sizeof(union parameter));
423 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
424 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
428 if (opt[1] != '\0') {
429 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
432 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
433 if (call->dsp_dtmf) {
436 bchannel_dtmf(call->bchannel, 0);
440 if (opt[1] == '\0') {
441 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
445 /* check for 0xXXXX... type of key */
446 if (!!strncmp((char *)key, "0x", 2)) {
447 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
451 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
452 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
457 if (*key>='0' && *key<='9')
458 call->bf_key[i] = (*key-'0') << 8;
459 else if (*key>='a' && *key<='f')
460 call->bf_key[i] = (*key-'a'+10) << 8;
461 else if (*key>='A' && *key<='F')
462 call->bf_key[i] = (*key-'A'+10) << 8;
466 if (*key>='0' && *key<='9')
467 call->bf_key[i] += (*key - '0');
468 else if (*key>='a' && *key<='f')
469 call->bf_key[i] += (*key - 'a' + 10);
470 else if (*key>='A' && *key<='F')
471 call->bf_key[i] += (*key - 'A' + 10);
478 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
482 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
484 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
487 if (opt[1] != '\0') {
488 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
491 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
496 if (opt[1] != '\0') {
497 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
500 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
505 if (opt[1] == '\0') {
506 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
509 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
510 call->nodsp_queue = atoi(opt+1);
513 if (opt[1] == '\0') {
514 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
517 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
518 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
520 bchannel_pipeline(call->bchannel, call->pipeline);
523 if (opt[1] == '\0') {
524 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
527 call->faxdetect=atoi(opt+1);
529 call->dsp=ast_dsp_new();
531 #ifdef LCR_FOR_CALLWEAVER
532 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
534 #ifdef LCR_FOR_ASTERISK
535 #ifdef DSP_FEATURE_DTMF_DETECT
536 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
538 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
543 #ifdef LCR_FOR_CALLWEAVER
544 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
546 #ifdef LCR_FOR_ASTERISK
547 #if ASTERISK_VERSION_NUM < 100000
548 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
550 struct ast_format src;
551 struct ast_format dst;
552 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
553 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
554 call->trans=ast_translator_build_path(&dst, &src);
559 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
562 if (opt[1] != '\0') {
563 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
566 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
571 if (opt[1] != '\0') {
572 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
575 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
576 call->inband_dtmf = 1;
579 if (opt[1] != 'r' && opt[1] != 't') {
580 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
584 if (gain < -8 || gain >8) {
585 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
588 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
590 call->rx_gain = gain;
592 bchannel_gain(call->bchannel, call->rx_gain, 0);
594 call->tx_gain = gain;
596 bchannel_gain(call->bchannel, call->tx_gain, 1);
600 if (opt[1] != '\0') {
601 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
604 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
609 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
613 /* re-open, if bchannel is created */
614 if (call->bchannel && call->bchannel->b_sock > -1) {
615 bchannel_destroy(call->bchannel);
616 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
617 bchannel_activate(call->bchannel, 1);
622 * send setup info to LCR
623 * this function is called, when asterisk call is received and ref is received
625 static void send_setup_to_lcr(struct chan_call *call)
627 union parameter newparam;
628 struct ast_channel *ast = call->ast;
631 if (!call->ast || !call->ref)
634 #ifdef AST_1_8_OR_HIGHER
635 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
637 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
640 /* send setup message to LCR */
641 memset(&newparam, 0, sizeof(union parameter));
642 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
643 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
645 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
647 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
648 if (!!strcmp(call->interface, "pbx"))
649 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
650 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
651 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
652 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
653 call->display[0] = '\0';
655 #ifdef AST_1_8_OR_HIGHER
656 /* set stored call information */
657 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
658 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
660 if (call->cid_num[0])
661 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
662 if (call->cid_name[0])
663 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
664 if (call->cid_rdnis[0]) {
665 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
666 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
667 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
669 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
670 case AST_PRES_RESTRICTED:
671 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
673 case AST_PRES_UNAVAILABLE:
674 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
676 case AST_PRES_ALLOWED:
678 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
680 switch(ast->cid.cid_ton) {
682 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
685 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
688 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
691 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
694 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
695 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
697 #if ASTERISK_VERSION_NUM < 110000
698 // ast->transfercapability = atoi(tmp);
699 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
701 // ast_channel_transfercapability_set(ast, atoi(tmp));
702 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
704 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
706 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
708 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
709 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
711 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
712 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
713 send_message(MESSAGE_SETUP, call->ref, &newparam);
715 /* change to outgoing setup state */
716 call->state = CHAN_LCR_STATE_OUT_SETUP;
720 * send dialing info to LCR
721 * this function is called, when setup acknowledge is received and dialing
724 static void send_dialque_to_lcr(struct chan_call *call)
726 union parameter newparam;
728 if (!call->ast || !call->ref || !call->dialque[0])
731 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
733 /* send setup message to LCR */
734 memset(&newparam, 0, sizeof(union parameter));
736 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
738 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
739 call->dialque[0] = '\0';
740 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
744 * in case of a bridge, the unsupported message can be forwarded directly
745 * to the remote call.
747 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
751 if (!call->bridge_call) return;
752 CDEBUG(call, NULL, "Sending message due bridging.\n");
753 send_message(message_type, call->bridge_call->ref, param);
757 * send release message to LCR and import bchannel if exported
759 static void send_release_and_import(struct chan_call *call, int cause, int location)
761 union parameter newparam;
763 /* importing channel */
764 if (call->bchannel) {
765 memset(&newparam, 0, sizeof(union parameter));
766 newparam.bchannel.type = BCHANNEL_RELEASE;
767 newparam.bchannel.handle = call->bchannel->handle;
768 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
770 /* sending release */
771 memset(&newparam, 0, sizeof(union parameter));
772 newparam.disconnectinfo.cause = cause;
773 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
774 send_message(MESSAGE_RELEASE, call->ref, &newparam);
778 * check if extension matches and start asterisk
779 * if it can match, proceed
782 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
785 union parameter newparam;
786 #if ASTERISK_VERSION_NUM < 110000
787 char *exten = ast->exten;
789 char s_exten[AST_MAX_EXTENSION];
792 strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
798 #if ASTERISK_VERSION_NUM < 110000
799 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
801 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
806 #if ASTERISK_VERSION_NUM < 110000
807 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
808 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
810 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
811 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
816 #if ASTERISK_VERSION_NUM < 110000
817 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
818 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);
820 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
821 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));
826 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
827 /* send setup acknowledge to lcr */
828 memset(&newparam, 0, sizeof(union parameter));
829 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
832 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
837 #if ASTERISK_VERSION_NUM < 110000
838 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
840 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
842 /* send setup acknowledge to lcr */
843 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
844 memset(&newparam, 0, sizeof(union parameter));
845 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
849 call->state = CHAN_LCR_STATE_IN_DIALING;
851 /* if match, start pbx */
852 #if ASTERISK_VERSION_NUM < 110000
853 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
855 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
857 CDEBUG(call, ast, "Extensions matches.\n");
862 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
866 #if ASTERISK_VERSION_NUM < 110000
869 if (!*ast_channel_exten(ast)) {
872 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
880 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
881 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
883 /* release asterisk */
884 #if ASTERISK_VERSION_NUM < 110000
885 ast->hangupcause = call->cause;
887 ast_channel_hangupcause_set(ast, call->cause);
889 /* change to release state */
890 call->state = CHAN_LCR_STATE_RELEASE;
891 ast_hangup(ast); // call will be destroyed here
895 /* send setup to asterisk */
896 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
898 #ifdef LCR_FOR_CALLWEAVER
900 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
903 ret = ast_pbx_start(ast);
905 cause = (ret==-2)?34:27;
908 call->pbx_started = 1;
909 ast_setstate(ast, AST_STATE_RING);
913 * incoming setup from LCR
915 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
917 struct ast_channel *ast;
918 struct ast_party_redirecting *ast_redir;
919 struct ast_party_caller *ast_caller;
920 #if ASTERISK_VERSION_NUM >= 110000
921 struct ast_party_redirecting s_ast_redir;
922 struct ast_party_caller s_ast_caller;
923 ast_party_redirecting_init(&s_ast_redir);
924 ast_party_caller_init(&s_ast_caller);
926 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
928 /* create asterisk channel instrance */
930 #ifdef LCR_FOR_CALLWEAVER
931 ast = ast_channel_alloc(1);
934 #ifdef LCR_FOR_ASTERISK
935 #ifdef AST_1_8_OR_HIGHER
936 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
938 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
942 #if ASTERISK_VERSION_NUM < 110000
943 ast_redir = &ast->redirecting;
944 ast_caller = &ast->caller;
946 ast_redir = &s_ast_redir;
947 ast_caller = &s_ast_caller;
952 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
953 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
960 #if ASTERISK_VERSION_NUM < 110000
961 ast->tech_pvt = call;
962 ast->tech = &lcr_tech;
963 ast->fds[0] = call->pipe[0];
965 ast_channel_tech_pvt_set(ast, call);
966 ast_channel_tech_set(ast, &lcr_tech);
967 ast_channel_set_fd(ast, 0, call->pipe[0]);
970 /* fill setup information */
971 if (param->setup.dialinginfo.id)
972 #if ASTERISK_VERSION_NUM < 110000
973 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
974 if (param->setup.context[0])
975 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
977 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
979 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
980 if (param->setup.context[0])
981 ast_channel_context_set(ast, param->setup.context);
983 ast_channel_context_set(ast, param->setup.callerinfo.interface);
987 #ifdef AST_1_8_OR_HIGHER
988 if (param->setup.callerinfo.id[0]) {
989 ast_caller->id.number.valid = 1;
990 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
991 if (!param->setup.callerinfo.id[0]) {
992 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
993 ast_caller->id.number.plan = (0 << 4) | 1;
995 switch (param->setup.callerinfo.present) {
996 case INFO_PRESENT_ALLOWED:
997 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
999 case INFO_PRESENT_RESTRICTED:
1000 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1003 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1005 switch (param->setup.callerinfo.screen) {
1006 case INFO_SCREEN_USER:
1007 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1009 case INFO_SCREEN_USER_VERIFIED_PASSED:
1010 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1012 case INFO_SCREEN_USER_VERIFIED_FAILED:
1013 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1016 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1018 switch (param->setup.callerinfo.ntype) {
1019 case INFO_NTYPE_SUBSCRIBER:
1020 ast_caller->id.number.plan = (4 << 4) | 1;
1022 case INFO_NTYPE_NATIONAL:
1023 ast_caller->id.number.plan = (2 << 4) | 1;
1025 case INFO_NTYPE_INTERNATIONAL:
1026 ast_caller->id.number.plan = (1 << 4) | 1;
1029 ast_caller->id.number.plan = (0 << 4) | 1;
1032 if (param->setup.callerinfo.id2[0]) {
1033 ast_caller->ani.number.valid = 1;
1034 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1035 switch (param->setup.callerinfo.present2) {
1036 case INFO_PRESENT_ALLOWED:
1037 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1039 case INFO_PRESENT_RESTRICTED:
1040 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1043 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1045 switch (param->setup.callerinfo.screen2) {
1046 case INFO_SCREEN_USER:
1047 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1049 case INFO_SCREEN_USER_VERIFIED_PASSED:
1050 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1052 case INFO_SCREEN_USER_VERIFIED_FAILED:
1053 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1056 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1058 switch (param->setup.callerinfo.ntype2) {
1059 case INFO_NTYPE_SUBSCRIBER:
1060 ast_caller->ani.number.plan = (4 << 4) | 1;
1062 case INFO_NTYPE_NATIONAL:
1063 ast_caller->ani.number.plan = (2 << 4) | 1;
1065 case INFO_NTYPE_INTERNATIONAL:
1066 ast_caller->ani.number.plan = (1 << 4) | 1;
1069 ast_caller->ani.number.plan = (0 << 4) | 1;
1072 if (param->setup.callerinfo.name[0]) {
1073 ast_caller->id.name.valid = 1;
1074 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1076 #if ASTERISK_VERSION_NUM >= 110000
1077 ast_channel_caller_set(ast, ast_caller);
1079 if (param->setup.redirinfo.id[0]) {
1080 ast_redir->from.number.valid = 1;
1081 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1082 switch (param->setup.redirinfo.present) {
1083 case INFO_PRESENT_ALLOWED:
1084 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1086 case INFO_PRESENT_RESTRICTED:
1087 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1090 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1092 switch (param->setup.redirinfo.screen) {
1093 case INFO_SCREEN_USER:
1094 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1096 case INFO_SCREEN_USER_VERIFIED_PASSED:
1097 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1099 case INFO_SCREEN_USER_VERIFIED_FAILED:
1100 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1103 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1105 switch (param->setup.redirinfo.ntype) {
1106 case INFO_NTYPE_SUBSCRIBER:
1107 ast_redir->from.number.plan = (4 << 4) | 1;
1109 case INFO_NTYPE_NATIONAL:
1110 ast_redir->from.number.plan = (2 << 4) | 1;
1112 case INFO_NTYPE_INTERNATIONAL:
1113 ast_redir->from.number.plan = (1 << 4) | 1;
1116 ast_redir->from.number.plan = (0 << 4) | 1;
1118 #if ASTERISK_VERSION_NUM >= 110000
1119 ast_channel_redirecting_set(ast, ast_redir);
1123 memset(&ast->cid, 0, sizeof(ast->cid));
1124 if (param->setup.callerinfo.id[0])
1125 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1126 if (param->setup.callerinfo.id2[0])
1127 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1128 if (param->setup.callerinfo.name[0])
1129 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1130 if (param->setup.redirinfo.id[0])
1131 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1132 switch (param->setup.callerinfo.present) {
1133 case INFO_PRESENT_ALLOWED:
1134 ast->cid.cid_pres = AST_PRES_ALLOWED;
1136 case INFO_PRESENT_RESTRICTED:
1137 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1140 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1142 switch (param->setup.callerinfo.ntype) {
1143 case INFO_NTYPE_SUBSCRIBER:
1144 ast->cid.cid_ton = 4;
1146 case INFO_NTYPE_NATIONAL:
1147 ast->cid.cid_ton = 2;
1149 case INFO_NTYPE_INTERNATIONAL:
1150 ast->cid.cid_ton = 1;
1153 ast->cid.cid_ton = 0;
1157 #if ASTERISK_VERSION_NUM < 110000
1158 ast->transfercapability = param->setup.capainfo.bearer_capa;
1160 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1162 /* enable hdlc if transcap is data */
1163 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1165 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1167 /* configure channel */
1168 #if ASTERISK_VERSION_NUM < 100000
1169 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1170 ast->readformat = ast->rawreadformat = ast->nativeformats;
1171 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1173 #if ASTERISK_VERSION_NUM < 110000
1174 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1175 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1176 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1177 ast_set_write_format(ast, &ast->rawwriteformat);
1178 ast_set_read_format(ast, &ast->rawreadformat);
1180 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1181 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1182 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1183 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1184 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1187 #if ASTERISK_VERSION_NUM < 110000
1189 ast->hangupcause = 0;
1191 ast_channel_priority_set(ast, 1);
1192 ast_channel_hangupcause_set(ast, 0);
1196 call->state = CHAN_LCR_STATE_IN_SETUP;
1198 if (!call->pbx_started)
1199 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1203 * incoming setup acknowledge from LCR
1205 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1207 if (!call->ast) return;
1209 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1211 /* send pending digits in dialque */
1212 if (call->dialque[0])
1213 send_dialque_to_lcr(call);
1214 /* change to overlap state */
1215 call->state = CHAN_LCR_STATE_OUT_DIALING;
1219 * incoming proceeding from LCR
1221 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1223 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1226 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1227 /* queue event for asterisk */
1228 if (call->ast && call->pbx_started) {
1232 write(wake_pipe[1], &byte, 1);
1234 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1240 * incoming alerting from LCR
1242 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1244 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1247 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1248 /* queue event to asterisk */
1249 if (call->ast && call->pbx_started) {
1253 write(wake_pipe[1], &byte, 1);
1255 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1260 * incoming connect from LCR
1262 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1264 union parameter newparam;
1266 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1269 call->state = CHAN_LCR_STATE_CONNECT;
1270 /* request bchannel */
1271 if (!call->bchannel) {
1272 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1273 memset(&newparam, 0, sizeof(union parameter));
1274 newparam.bchannel.type = BCHANNEL_REQUEST;
1275 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1277 /* copy connectinfo */
1278 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1279 /* queue event to asterisk */
1280 if (call->ast && call->pbx_started) {
1284 write(wake_pipe[1], &byte, 1);
1286 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1291 * incoming disconnect from LCR
1293 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1295 struct ast_channel *ast = call->ast;
1297 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1300 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1302 call->cause = param->disconnectinfo.cause;
1303 call->location = param->disconnectinfo.location;
1304 /* if bridge, forward disconnect and return */
1307 if (call->bridge_call) {
1308 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1309 bridge_message_if_bridged(call, message_type, param);
1313 /* release lcr with same cause */
1314 send_release_and_import(call, call->cause, call->location);
1316 /* change to release state */
1317 call->state = CHAN_LCR_STATE_RELEASE;
1318 /* queue release asterisk */
1320 #if ASTERISK_VERSION_NUM < 110000
1321 ast->hangupcause = call->cause;
1323 ast_channel_hangupcause_set(ast, call->cause);
1325 if (call->pbx_started) {
1329 write(wake_pipe[1], &byte, 1);
1331 strcpy(call->queue_string, "H"); // overwrite other indications
1333 ast_hangup(ast); // call will be destroyed here
1339 * incoming release from LCR
1341 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1343 struct ast_channel *ast = call->ast;
1345 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1349 /* change to release state */
1350 call->state = CHAN_LCR_STATE_RELEASE;
1351 /* copy release info */
1353 call->cause = param->disconnectinfo.cause;
1354 call->location = param->disconnectinfo.location;
1356 /* if we have an asterisk instance, queue hangup, else we are done */
1358 #if ASTERISK_VERSION_NUM < 110000
1359 ast->hangupcause = call->cause;
1361 ast_channel_hangupcause_set(ast, call->cause);
1363 if (call->pbx_started) {
1367 write(wake_pipe[1], &byte, 1);
1369 strcpy(call->queue_string, "H");
1371 ast_hangup(ast); // call will be destroyed here
1380 * incoming information from LCR
1382 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1384 struct ast_channel *ast = call->ast;
1386 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1390 /* pbx not started */
1391 if (!call->pbx_started) {
1392 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1393 #if ASTERISK_VERSION_NUM < 110000
1394 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1396 ast_channel_exten_set(ast, param->information.id);
1398 lcr_start_pbx(call, ast, param->information.sending_complete);
1402 /* change dailing state after setup */
1403 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1404 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1405 call->state = CHAN_LCR_STATE_IN_DIALING;
1406 // ast_setstate(ast, AST_STATE_DIALING);
1410 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1414 write(wake_pipe[1], &byte, 1);
1416 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1419 /* use bridge to forware message not supported by asterisk */
1420 if (call->state == CHAN_LCR_STATE_CONNECT) {
1421 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1422 bridge_message_if_bridged(call, message_type, param);
1427 * incoming information from LCR
1429 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1431 union parameter newparam;
1433 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1435 /* request bchannel, if call is resumed and we don't have it */
1436 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1437 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1438 memset(&newparam, 0, sizeof(union parameter));
1439 newparam.bchannel.type = BCHANNEL_REQUEST;
1440 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1443 if (!call->ast) return;
1445 /* use bridge to forware message not supported by asterisk */
1446 bridge_message_if_bridged(call, message_type, param);
1450 * incoming information from LCR
1452 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1454 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1456 if (!call->ast) return;
1458 /* use bridge to forware message not supported by asterisk */
1459 bridge_message_if_bridged(call, message_type, param);
1463 * incoming pattern from LCR
1465 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1467 union parameter newparam;
1469 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1471 if (!call->ast) return;
1473 /* pattern are indicated only once */
1474 if (call->has_pattern)
1476 call->has_pattern = 1;
1478 /* request bchannel */
1479 if (!call->bchannel) {
1480 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1481 memset(&newparam, 0, sizeof(union parameter));
1482 newparam.bchannel.type = BCHANNEL_REQUEST;
1483 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1485 /* queue PROGRESS, because tones are available */
1486 if (call->ast && call->pbx_started) {
1490 write(wake_pipe[1], &byte, 1);
1492 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1497 * got dtmf from bchannel (locked state)
1499 void lcr_in_dtmf(struct chan_call *call, int val)
1501 struct ast_channel *ast = call->ast;
1506 if (!call->pbx_started)
1509 if (!call->dsp_dtmf) {
1510 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1514 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1520 write(wake_pipe[1], &byte, 1);
1522 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1526 * message received from LCR
1528 int receive_message(int message_type, unsigned int ref, union parameter *param)
1530 struct bchannel *bchannel;
1531 struct chan_call *call;
1532 union parameter newparam;
1534 memset(&newparam, 0, sizeof(union parameter));
1536 /* handle bchannel message*/
1537 if (message_type == MESSAGE_BCHANNEL) {
1538 switch(param->bchannel.type) {
1539 case BCHANNEL_ASSIGN:
1540 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1541 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1542 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1545 /* create bchannel */
1546 bchannel = alloc_bchannel(param->bchannel.handle);
1548 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1552 /* configure channel */
1553 bchannel->b_tx_gain = param->bchannel.tx_gain;
1554 bchannel->b_rx_gain = param->bchannel.rx_gain;
1555 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1556 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1557 bchannel->b_bf_len = param->bchannel.crypt_len;
1558 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1560 bchannel->b_txdata = 0;
1561 bchannel->b_tx_dejitter = 1;
1563 /* in case, ref is not set, this bchannel instance must
1564 * be created until it is removed again by LCR */
1566 call = find_call_ref(ref);
1568 bchannel->call = call;
1569 call->bchannel = bchannel;
1571 bchannel_dtmf(bchannel, 1);
1573 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1574 if (call->pipeline[0])
1575 bchannel_pipeline(bchannel, call->pipeline);
1577 bchannel_gain(bchannel, call->rx_gain, 0);
1579 bchannel_gain(bchannel, call->tx_gain, 1);
1580 if (call->bridge_id) {
1581 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1582 bchannel_join(bchannel, call->bridge_id);
1584 /* ignore all dsp features, if it is a loopback interface */
1585 if (param->bchannel.isloopback)
1588 /* create only, if call exists, othewhise it bchannel is freed below... */
1589 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1590 bchannel_activate(bchannel, 1);
1593 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1594 newparam.bchannel.handle = param->bchannel.handle;
1595 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1596 /* if call has released before bchannel is assigned */
1598 newparam.bchannel.type = BCHANNEL_RELEASE;
1599 newparam.bchannel.handle = param->bchannel.handle;
1600 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1605 case BCHANNEL_REMOVE:
1606 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1607 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1608 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1611 /* unklink from call and destroy bchannel */
1612 free_bchannel(bchannel);
1615 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1616 newparam.bchannel.handle = param->bchannel.handle;
1617 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1622 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1627 /* handle new ref */
1628 if (message_type == MESSAGE_NEWREF) {
1629 if (param->newref.direction) {
1630 /* new ref from lcr */
1631 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1632 if (!ref || find_call_ref(ref)) {
1633 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1636 /* allocate new call instance */
1637 call = alloc_call();
1639 call->state = CHAN_LCR_STATE_IN_PREPARE;
1642 call->ref_was_assigned = 1;
1643 /* set dtmf (default, use option 'n' to disable */
1645 /* wait for setup (or release from asterisk) */
1647 /* new ref, as requested from this remote application */
1648 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1649 call = find_call_ref(0);
1651 /* send release, if ref does not exist */
1652 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1657 call->ref_was_assigned = 1;
1658 /* set dtmf (default, use option 'n' to disable */
1660 /* send pending setup info */
1661 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1662 send_setup_to_lcr(call);
1663 /* release if asterisk has signed off */
1664 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1667 send_release_and_import(call, call->cause, call->location);
1669 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1680 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1683 call = find_call_ref(ref);
1685 /* ignore ref that is not used (anymore) */
1686 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1690 /* handle messages */
1691 switch(message_type) {
1693 lcr_in_setup(call, message_type, param);
1696 case MESSAGE_OVERLAP:
1697 lcr_in_overlap(call, message_type, param);
1700 case MESSAGE_PROCEEDING:
1701 lcr_in_proceeding(call, message_type, param);
1704 case MESSAGE_ALERTING:
1705 lcr_in_alerting(call, message_type, param);
1708 case MESSAGE_CONNECT:
1709 lcr_in_connect(call, message_type, param);
1712 case MESSAGE_DISCONNECT:
1713 lcr_in_disconnect(call, message_type, param);
1716 case MESSAGE_RELEASE:
1717 lcr_in_release(call, message_type, param);
1720 case MESSAGE_INFORMATION:
1721 lcr_in_information(call, message_type, param);
1724 case MESSAGE_NOTIFY:
1725 lcr_in_notify(call, message_type, param);
1728 case MESSAGE_FACILITY:
1729 lcr_in_facility(call, message_type, param);
1732 case MESSAGE_PATTERN: // audio available from LCR
1733 if (!call->has_pattern)
1734 lcr_in_pattern(call, message_type, param);
1737 case MESSAGE_NOPATTERN: // audio not available from LCR
1740 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1741 call->audiopath = param->audiopath;
1745 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1752 * release all calls (due to broken socket)
1754 static void release_all_calls(void)
1756 struct chan_call *call;
1761 /* no ast, so we may directly free call */
1763 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1767 /* already in release process */
1768 if (call->state == CHAN_LCR_STATE_RELEASE) {
1772 /* release or queue release */
1774 call->state = CHAN_LCR_STATE_RELEASE;
1775 if (!call->pbx_started) {
1776 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1777 ast_hangup(call->ast); // call will be destroyed here
1780 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1784 write(wake_pipe[1], &byte, 1);
1786 strcpy(call->queue_string, "H");
1790 /* release all bchannels */
1791 while(bchannel_first)
1792 free_bchannel(bchannel_first);
1795 void close_socket(void);
1798 * warning! not thread safe
1799 * returns -1 for socket error, 0 for no work, 1 for work
1801 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1804 struct admin_list *admin;
1805 struct admin_message msg;
1807 if ((what & LCR_FD_READ)) {
1808 /* read from socket */
1809 len = read(lcr_sock, &msg, sizeof(msg));
1811 CERROR(NULL, NULL, "Socket closed.(read)\n");
1813 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1815 release_all_calls();
1816 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1820 if (len != sizeof(msg)) {
1821 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1824 if (msg.message != ADMIN_MESSAGE) {
1825 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1828 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1830 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1835 if ((what & LCR_FD_WRITE)) {
1836 /* write to socket */
1838 socket_fd.when &= ~LCR_FD_WRITE;
1841 admin = admin_first;
1842 len = write(lcr_sock, &admin->msg, sizeof(msg));
1844 CERROR(NULL, NULL, "Socket closed.(write)\n");
1848 if (len != sizeof(msg)) {
1849 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1853 admin_first = admin->next;
1857 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1866 * open and close socket and thread
1868 int open_socket(void)
1871 struct sockaddr_un sock_address;
1872 union parameter param;
1875 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1876 CERROR(NULL, NULL, "Failed to create socket.\n");
1880 /* set socket address and name */
1881 memset(&sock_address, 0, sizeof(sock_address));
1882 sock_address.sun_family = PF_UNIX;
1883 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1885 /* connect socket */
1886 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1889 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1893 /* register socket fd */
1894 memset(&socket_fd, 0, sizeof(socket_fd));
1895 socket_fd.fd = lcr_sock;
1896 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1898 /* enque hello message */
1899 memset(¶m, 0, sizeof(param));
1900 strcpy(param.hello.application, "asterisk");
1901 send_message(MESSAGE_HELLO, 0, ¶m);
1906 void close_socket(void)
1908 struct admin_list *admin, *temp;
1910 /* socket not created */
1914 unregister_fd(&socket_fd);
1916 /* flush pending messages */
1917 admin = admin_first;
1920 admin = admin->next;
1932 /* sending queue to asterisk */
1933 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1937 read(wake_pipe[0], &byte, 1);
1944 static void handle_queue()
1946 struct chan_call *call;
1947 struct ast_channel *ast;
1948 struct ast_frame fr;
1954 p = call->queue_string;
1957 if (ast_channel_trylock(ast)) {
1958 ast_mutex_unlock(&chan_lock);
1960 ast_mutex_lock(&chan_lock);
1966 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1967 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1970 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1971 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1974 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1975 ast_queue_control(ast, AST_CONTROL_RINGING);
1976 ast_setstate(ast, AST_STATE_RINGING);
1979 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1980 ast_queue_control(ast, AST_CONTROL_ANSWER);
1983 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1984 ast_queue_hangup(ast);
1986 case '1': case '2': case '3': case 'A':
1987 case '4': case '5': case '6': case 'B':
1988 case '7': case '8': case '9': case 'C':
1989 case '*': case '0': case '#': case 'D':
1990 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1991 /* send digit to asterisk */
1992 memset(&fr, 0, sizeof(fr));
1994 #ifdef LCR_FOR_ASTERISK
1995 fr.frametype = AST_FRAME_DTMF_BEGIN;
1998 #ifdef LCR_FOR_CALLWEAVER
1999 fr.frametype = AST_FRAME_DTMF;
2002 #ifdef AST_1_8_OR_HIGHER
2003 fr.subclass.integer = *p;
2007 fr.delivery = ast_tv(0, 0);
2008 ast_queue_frame(ast, &fr);
2010 #ifdef LCR_FOR_ASTERISK
2011 fr.frametype = AST_FRAME_DTMF_END;
2012 ast_queue_frame(ast, &fr);
2017 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
2021 call->queue_string[0] = '\0';
2022 ast_channel_unlock(ast);
2028 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
2030 CDEBUG(NULL, NULL, "Retry to open socket.\n");
2031 if (open_socket() < 0)
2032 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
2037 void lock_chan(void)
2039 ast_mutex_lock(&chan_lock);
2042 void unlock_chan(void)
2044 ast_mutex_unlock(&chan_lock);
2047 /* chan_lcr thread */
2048 static void *chan_thread(void *arg)
2050 if (pipe(wake_pipe) < 0) {
2051 CERROR(NULL, NULL, "Failed to open pipe.\n");
2054 memset(&wake_fd, 0, sizeof(wake_fd));
2055 wake_fd.fd = wake_pipe[0];
2056 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2058 memset(&socket_retry, 0, sizeof(socket_retry));
2059 add_timer(&socket_retry, handle_retry, NULL, 0);
2061 bchannel_pid = getpid();
2063 /* open socket the first time */
2064 handle_retry(NULL, NULL, 0);
2066 ast_mutex_lock(&chan_lock);
2070 select_main(0, &global_change, lock_chan, unlock_chan);
2077 * new asterisk instance
2080 #ifdef AST_1_8_OR_HIGHER
2081 #if ASTERISK_VERSION_NUM < 100000
2082 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2084 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, struct ast_channel *requestor, void *data, int *cause)
2087 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2090 char exten[256], *dial, *interface, *opt;
2091 struct ast_channel *ast;
2092 struct chan_call *call;
2093 struct ast_party_redirecting *req_redir;
2094 struct ast_party_caller *req_caller;
2096 ast_mutex_lock(&chan_lock);
2097 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2099 /* if socket is closed */
2101 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2102 ast_mutex_unlock(&chan_lock);
2106 /* create call instance */
2107 call = alloc_call();
2109 /* failed to create instance */
2110 ast_mutex_unlock(&chan_lock);
2114 /* create asterisk channel instrance */
2116 #ifdef LCR_FOR_ASTERISK
2117 #ifdef AST_1_8_OR_HIGHER
2118 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2120 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2124 #ifdef LCR_FOR_CALLWEAVER
2125 ast = ast_channel_alloc(1);
2129 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2131 /* failed to create instance */
2132 ast_mutex_unlock(&chan_lock);
2135 #if ASTERISK_VERSION_NUM < 110000
2136 ast->tech = &lcr_tech;
2137 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2138 req_redir = &requestor->redirecting;
2139 req_caller = &requestor->caller;
2141 ast_channel_tech_set(ast, &lcr_tech);
2142 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2143 req_redir = ast_channel_redirecting(requestor);
2144 req_caller = ast_channel_caller(requestor);
2146 /* configure channel */
2147 #if ASTERISK_VERSION_NUM < 100000
2148 #if ASTERISK_VERSION_NUM < 110000
2149 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2150 ast->readformat = ast->rawreadformat = ast->nativeformats;
2151 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2153 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2154 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2155 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2158 #if ASTERISK_VERSION_NUM < 110000
2159 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2160 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2161 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2162 ast_set_write_format(ast, &ast->rawwriteformat);
2163 ast_set_read_format(ast, &ast->rawreadformat);
2165 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2166 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2167 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2168 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2169 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2172 #if ASTERISK_VERSION_NUM < 110000
2174 ast->hangupcause = 0;
2176 ast_channel_priority_set(ast, 1);
2177 ast_channel_hangupcause_set(ast, 0);
2182 #if ASTERISK_VERSION_NUM < 110000
2183 ast->tech_pvt = call;
2184 ast->fds[0] = call->pipe[0];
2186 ast_channel_tech_pvt_set(ast, call);
2187 ast_channel_set_fd(ast, 0, call->pipe[0]);
2189 call->pbx_started = 0;
2191 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2194 * Extract interface, dialstring, options from data.
2197 * <interface>/<dialstring>
2198 * <interface>/<dialstring>/options
2200 strncpy(exten, (char *)data, sizeof(exten)-1);
2201 exten[sizeof(exten)-1] = '\0';
2202 if ((dial = strchr(exten, '/'))) {
2205 if ((opt = strchr(dial, '/')))
2214 strncpy(call->interface, interface, sizeof(call->interface)-1);
2215 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2216 apply_opt(call, (char *)opt);
2218 #ifdef AST_1_8_OR_HIGHER
2219 // clone_variables(requestor, ast);
2222 ast->caller.ani.number.valid= req_caller->ani.number.valid;
2223 if (req_caller->ani.number.valid)
2224 if (req_caller->ani.number.str)
2225 if (req_caller->ani.number.str[0])
2226 ast->caller.ani.number.str= strdup(req_caller->ani.number.str);
2227 ast->caller.ani.number.plan= req_caller->ani.number.plan;
2228 ast->caller.ani.number.presentation= req_caller->ani.number.presentation;
2230 ast->caller.ani.name.valid= req_caller->ani.name.valid;
2231 if (req_caller->ani.name.valid)
2232 if (req_caller->ani.name.str)
2233 if (req_caller->ani.name.str[0])
2234 ast->caller.ani.name.str= strdup(req_caller->ani.name.str);
2235 ast->caller.ani.name.presentation= req_caller->ani.name.presentation;
2237 ast->caller.ani.subaddress.valid= req_caller->ani.subaddress.valid;
2238 if (req_caller->ani.subaddress.valid)
2239 if (req_caller->ani.subaddress.str)
2240 if (req_caller->ani.subaddress.str[0])
2241 ast->caller.ani.subaddress.str= strdup(req_caller->ani.subaddress.str);
2242 ast->caller.ani.subaddress.type= req_caller->ani.subaddress.type;
2244 ast->caller.id.number.valid= req_caller->id.number.valid;
2245 if (req_caller->id.number.valid)
2246 if (req_caller->id.number.str)
2247 if (req_caller->id.number.str[0])
2248 ast->caller.id.number.str= strdup(req_caller->id.number.str);
2249 ast->caller.id.number.plan= req_caller->id.number.plan;
2250 ast->caller.id.number.presentation= req_caller->id.number.presentation;
2252 ast->caller.id.name.valid= req_caller->id.name.valid;
2253 if (req_caller->id.name.valid)
2254 if (req_caller->id.name.str)
2255 if (req_caller->id.name.str[0])
2256 ast->caller.id.name.str= strdup(req_caller->id.name.str);
2257 ast->caller.id.name.presentation= req_caller->id.name.presentation;
2259 ast->caller.id.subaddress.valid= req_caller->id.subaddress.valid;
2260 if (req_caller->id.subaddress.valid)
2261 if (req_caller->id.subaddress.str)
2262 if (req_caller->id.subaddress.str[0])
2263 ast->caller.id.subaddress.str= strdup(req_caller->id.subaddress.str);
2264 ast->caller.id.subaddress.type= req_caller->id.subaddress.type;
2266 if (requestor->dialed.number.str)
2267 if (requestor->dialed.number.str[0])
2268 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2269 ast->dialed.number.plan= requestor->dialed.number.plan;
2271 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2272 if (requestor->dialed.subaddress.valid)
2273 if (requestor->dialed.subaddress.str)
2274 if (requestor->dialed.subaddress.str[0])
2275 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2276 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2278 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2279 ast->redirecting.count= req_redir->count;
2280 ast->redirecting.reason= req_redir->reason;
2282 ast->redirecting.from.number.valid= req_redir->from.number.valid;
2283 if (req_redir->from.number.valid)
2284 if (req_redir->from.number.str)
2285 if (req_redir->from.number.str[0])
2286 ast->redirecting.from.number.str= strdup(req_redir->from.number.str);
2287 ast->redirecting.from.number.plan= req_redir->from.number.plan;
2288 ast->redirecting.from.number.presentation= req_redir->from.number.presentation;
2290 ast->redirecting.to.number.valid= req_redir->to.number.valid;
2291 if (req_redir->to.number.valid)
2292 if (req_redir->to.number.str)
2293 if (req_redir->to.number.str[0])
2294 ast->redirecting.to.number.str= strdup(req_redir->to.number.str);
2295 ast->redirecting.to.number.plan= req_redir->to.number.plan;
2296 ast->redirecting.to.number.presentation= req_redir->to.number.presentation;
2298 /* store call information for setup */
2301 if (requestor && req_caller->id.number.valid) {
2302 if (req_caller->id.number.str)
2303 strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2304 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2305 case AST_PRES_RESTRICTED:
2306 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2308 case AST_PRES_UNAVAILABLE:
2309 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2311 case AST_PRES_ALLOWED:
2313 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2315 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2316 case AST_PRES_USER_NUMBER_UNSCREENED:
2317 call->callerinfo.screen = INFO_SCREEN_USER;
2319 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2320 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2322 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2323 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2326 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2328 switch((req_caller->id.number.plan >> 4) & 7) {
2330 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2333 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2336 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2339 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2342 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2345 if (requestor && req_caller->ani.number.valid) {
2346 if (req_caller->ani.number.str)
2347 strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2348 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2349 case AST_PRES_RESTRICTED:
2350 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2352 case AST_PRES_UNAVAILABLE:
2353 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2355 case AST_PRES_ALLOWED:
2357 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2359 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2360 case AST_PRES_USER_NUMBER_UNSCREENED:
2361 call->callerinfo.screen2 = INFO_SCREEN_USER;
2363 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2364 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2366 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2367 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2370 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2372 switch((req_caller->ani.number.plan >> 4) & 7) {
2374 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2377 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2380 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2383 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2386 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2389 if (requestor && req_caller->id.name.valid) {
2390 if (req_caller->id.name.str)
2391 strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2395 if (requestor && req_redir->from.number.valid) {
2396 call->redirinfo.itype = INFO_ITYPE_CHAN;
2397 if (req_redir->from.number.str)
2398 strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2399 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2400 case AST_PRES_RESTRICTED:
2401 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2403 case AST_PRES_UNAVAILABLE:
2404 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2406 case AST_PRES_ALLOWED:
2408 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2410 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2411 case AST_PRES_USER_NUMBER_UNSCREENED:
2412 call->redirinfo.screen = INFO_SCREEN_USER;
2414 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2415 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2417 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2418 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2421 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2423 switch((req_redir->from.number.plan >> 4) & 7) {
2425 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2428 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2431 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2434 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2439 ast_mutex_unlock(&chan_lock);
2444 * call from asterisk
2446 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2448 union parameter newparam;
2449 struct chan_call *call;
2450 #if ASTERISK_VERSION_NUM >= 110000
2451 int transfercapability;
2454 ast_mutex_lock(&chan_lock);
2455 #if ASTERISK_VERSION_NUM < 110000
2456 call = ast->tech_pvt;
2458 call = ast_channel_tech_pvt(ast);
2461 #ifdef LCR_FOR_CALLWEAVER
2463 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2467 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2468 ast_mutex_unlock(&chan_lock);
2472 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2474 /* pbx process is started */
2475 call->pbx_started = 1;
2476 /* send MESSAGE_NEWREF */
2477 memset(&newparam, 0, sizeof(union parameter));
2478 newparam.newref.direction = 0; /* request from app */
2479 if (!strcmp(call->interface, "pbx"))
2480 newparam.newref.mode = 1;
2481 send_message(MESSAGE_NEWREF, 0, &newparam);
2483 /* set hdlc if capability requires hdlc */
2484 #if ASTERISK_VERSION_NUM < 110000
2485 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2486 || ast->transfercapability == INFO_BC_DATARESTRICTED
2487 || ast->transfercapability == INFO_BC_VIDEO)
2489 transfercapability=ast_channel_transfercapability(ast);
2490 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2491 || transfercapability == INFO_BC_DATARESTRICTED
2492 || transfercapability == INFO_BC_VIDEO)
2495 /* if hdlc is forced by option, we change transcap to data */
2497 #if ASTERISK_VERSION_NUM < 110000
2498 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2499 && ast->transfercapability != INFO_BC_DATARESTRICTED
2500 && ast->transfercapability != INFO_BC_VIDEO)
2501 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2503 && transfercapability != INFO_BC_DATAUNRESTRICTED
2504 && transfercapability != INFO_BC_DATARESTRICTED
2505 && transfercapability != INFO_BC_VIDEO)
2506 transfercapability = INFO_BC_DATAUNRESTRICTED;
2509 #ifndef AST_1_8_OR_HIGHER
2510 call->cid_num[0] = 0;
2511 call->cid_name[0] = 0;
2512 call->cid_rdnis[0] = 0;
2514 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2515 strncpy(call->cid_num, ast->cid.cid_num,
2516 sizeof(call->cid_num)-1);
2517 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2518 strncpy(call->cid_name, ast->cid.cid_name,
2519 sizeof(call->cid_name)-1);
2520 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2521 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2522 sizeof(call->cid_rdnis)-1);
2525 ast_mutex_unlock(&chan_lock);
2529 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2531 static const char* dtmf_tones[] = {
2532 "!941+1336/100,!0/100", /* 0 */
2533 "!697+1209/100,!0/100", /* 1 */
2534 "!697+1336/100,!0/100", /* 2 */
2535 "!697+1477/100,!0/100", /* 3 */
2536 "!770+1209/100,!0/100", /* 4 */
2537 "!770+1336/100,!0/100", /* 5 */
2538 "!770+1477/100,!0/100", /* 6 */
2539 "!852+1209/100,!0/100", /* 7 */
2540 "!852+1336/100,!0/100", /* 8 */
2541 "!852+1477/100,!0/100", /* 9 */
2542 "!697+1633/100,!0/100", /* A */
2543 "!770+1633/100,!0/100", /* B */
2544 "!852+1633/100,!0/100", /* C */
2545 "!941+1633/100,!0/100", /* D */
2546 "!941+1209/100,!0/100", /* * */
2547 "!941+1477/100,!0/100" }; /* # */
2549 if (digit >= '0' && digit <='9')
2550 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2551 else if (digit >= 'A' && digit <= 'D')
2552 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2553 else if (digit == '*')
2554 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2555 else if (digit == '#')
2556 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2558 #if ASTERISK_VERSION_NUM < 110000
2559 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2561 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2566 #ifdef LCR_FOR_ASTERISK
2567 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2569 #ifdef LCR_FOR_CALLWEAVER
2570 static int lcr_digit(struct ast_channel *ast, char digit)
2573 struct chan_call *call;
2574 union parameter newparam;
2577 #ifdef LCR_FOR_CALLWEAVER
2578 int inband_dtmf = 0;
2581 /* only pass IA5 number space */
2582 if (digit > 126 || digit < 32)
2585 ast_mutex_lock(&chan_lock);
2586 #if ASTERISK_VERSION_NUM < 110000
2587 call = ast->tech_pvt;
2589 call = ast_channel_tech_pvt(ast);
2592 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2593 ast_mutex_unlock(&chan_lock);
2597 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2599 /* send information or queue them */
2600 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2601 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2602 memset(&newparam, 0, sizeof(union parameter));
2604 newparam.information.keypad[0] = digit;
2605 newparam.information.keypad[1] = '\0';
2607 newparam.information.id[0] = digit;
2608 newparam.information.id[1] = '\0';
2610 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2613 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2614 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2616 strncat(call->dialque, buf, strlen(call->dialque)-1);
2619 ast_mutex_unlock(&chan_lock);
2621 #ifdef LCR_FOR_ASTERISK
2625 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2627 int inband_dtmf = 0;
2628 struct chan_call *call;
2631 ast_mutex_lock(&chan_lock);
2633 #if ASTERISK_VERSION_NUM < 110000
2634 call = ast->tech_pvt;
2636 call = ast_channel_tech_pvt(ast);
2641 "Received digit from Asterisk, "
2642 "but no call instance exists.\n");
2643 ast_mutex_unlock(&chan_lock);
2647 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2649 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2653 ast_mutex_unlock(&chan_lock);
2656 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2657 send_digit_to_chan(ast, digit);
2663 static int lcr_answer(struct ast_channel *ast)
2665 union parameter newparam;
2666 struct chan_call *call;
2668 ast_mutex_lock(&chan_lock);
2669 #if ASTERISK_VERSION_NUM < 110000
2670 call = ast->tech_pvt;
2672 call = ast_channel_tech_pvt(ast);
2675 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2676 ast_mutex_unlock(&chan_lock);
2680 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2682 /* copy connectinfo, if bridged */
2683 if (call->bridge_call)
2684 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2685 /* send connect message to lcr */
2686 if (call->state != CHAN_LCR_STATE_CONNECT) {
2687 memset(&newparam, 0, sizeof(union parameter));
2688 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2689 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2690 call->state = CHAN_LCR_STATE_CONNECT;
2693 /* request bchannel */
2694 if (!call->bchannel) {
2695 CDEBUG(call, ast, "Requesting B-channel.\n");
2696 memset(&newparam, 0, sizeof(union parameter));
2697 newparam.bchannel.type = BCHANNEL_REQUEST;
2698 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2701 // memset(&newparam, 0, sizeof(union parameter));
2702 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2704 ast_mutex_unlock(&chan_lock);
2708 static int lcr_hangup(struct ast_channel *ast)
2710 struct chan_call *call;
2711 pthread_t tid = pthread_self();
2713 if (!pthread_equal(tid, chan_tid)) {
2714 ast_mutex_lock(&chan_lock);
2716 #if ASTERISK_VERSION_NUM < 110000
2717 call = ast->tech_pvt;
2719 call = ast_channel_tech_pvt(ast);
2722 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2723 if (!pthread_equal(tid, chan_tid)) {
2724 ast_mutex_unlock(&chan_lock);
2729 if (!pthread_equal(tid, chan_tid))
2730 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2732 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2734 /* disconnect asterisk, maybe not required */
2735 #if ASTERISK_VERSION_NUM < 110000
2736 ast->tech_pvt = NULL;
2739 ast_channel_tech_pvt_set(ast, NULL);
2740 ast_channel_set_fd(ast, 0, -1);
2744 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2745 #if ASTERISK_VERSION_NUM < 110000
2746 if (ast->hangupcause > 0)
2747 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2749 if (ast_channel_hangupcause(ast) > 0)
2750 send_release_and_import(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2753 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2756 if (!pthread_equal(tid, chan_tid)) {
2757 ast_mutex_unlock(&chan_lock);
2761 /* ref is not set, due to prepare setup or release */
2762 if (call->state == CHAN_LCR_STATE_RELEASE) {
2763 /* we get the response to our release */
2764 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2767 /* during prepare, we change to release state */
2768 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2769 call->state = CHAN_LCR_STATE_RELEASE;
2773 if (!pthread_equal(tid, chan_tid)) {
2774 ast_mutex_unlock(&chan_lock);
2779 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2781 struct chan_call *call;
2782 struct ast_frame * f = fr;
2784 #if ASTERISK_VERSION_NUM < 100000
2785 #ifdef AST_1_8_OR_HIGHER
2786 if (!f->subclass.codec)
2790 CDEBUG(NULL, ast, "No subclass\n");
2792 #ifdef AST_1_8_OR_HIGHER
2793 #if ASTERISK_VERSION_NUM < 100000
2794 #if ASTERISK_VERSION_NUM < 110000
2795 if (!(f->subclass.codec & ast->nativeformats)) {
2797 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2800 #if ASTERISK_VERSION_NUM < 110000
2801 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2803 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2807 #if ASTERISK_VERSION_NUM < 110000
2808 if (!(f->subclass & ast->nativeformats)) {
2810 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2814 "Unexpected format. "
2815 "Activating emergency conversion...\n");
2817 #ifdef AST_1_8_OR_HIGHER
2818 #if ASTERISK_VERSION_NUM < 100000
2819 ast_set_write_format(ast, f->subclass.codec);
2821 ast_set_write_format(ast, &f->subclass.format);
2824 ast_set_write_format(ast, f->subclass);
2826 #if ASTERISK_VERSION_NUM < 110000
2827 f = (ast->writetrans) ? ast_translate(
2828 ast->writetrans, fr, 0) : fr;
2830 f = (ast_channel_writetrans(ast)) ? ast_translate(
2831 ast_channel_writetrans(ast), fr, 0) : fr;
2835 ast_mutex_lock(&chan_lock);
2836 #if ASTERISK_VERSION_NUM < 110000
2837 call = ast->tech_pvt;
2839 call = ast_channel_tech_pvt(ast);
2842 ast_mutex_unlock(&chan_lock);
2848 if (call->bchannel && f->samples)
2849 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2850 ast_mutex_unlock(&chan_lock);
2858 static struct ast_frame *lcr_read(struct ast_channel *ast)
2860 struct chan_call *call;
2863 ast_mutex_lock(&chan_lock);
2864 #if ASTERISK_VERSION_NUM < 110000
2865 call = ast->tech_pvt;
2867 call = ast_channel_tech_pvt(ast);
2870 ast_mutex_unlock(&chan_lock);
2873 if (call->pipe[0] > -1) {
2874 if (call->rebuffer && !call->hdlc) {
2875 /* Make sure we have a complete 20ms (160byte) frame */
2876 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2878 call->framepos += len;
2881 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2883 if (len < 0 && errno == EAGAIN) {
2884 ast_mutex_unlock(&chan_lock);
2886 #ifdef LCR_FOR_ASTERISK
2887 return &ast_null_frame;
2890 #ifdef LCR_FOR_CALLWEAVER
2896 close(call->pipe[0]);
2899 ast_mutex_unlock(&chan_lock);
2901 } else if (call->rebuffer && call->framepos < 160) {
2902 /* Not a complete frame, so we send a null-frame */
2903 ast_mutex_unlock(&chan_lock);
2904 return &ast_null_frame;
2908 call->read_fr.frametype = AST_FRAME_VOICE;
2909 #ifdef AST_1_8_OR_HIGHER
2910 #if ASTERISK_VERSION_NUM < 100000
2911 #if ASTERISK_VERSION_NUM < 110000
2912 call->read_fr.subclass.codec = ast->nativeformats;
2914 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2917 #if ASTERISK_VERSION_NUM < 110000
2918 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2920 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2922 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2925 #if ASTERISK_VERSION_NUM < 110000
2926 call->read_fr.subclass = ast->nativeformats;
2928 call->read_fr.subclass = ast_channel_nativeformats(ast);
2931 if (call->rebuffer) {
2932 call->read_fr.datalen = call->framepos;
2933 call->read_fr.samples = call->framepos;
2936 call->read_fr.datalen = len;
2937 call->read_fr.samples = len;
2939 call->read_fr.delivery = ast_tv(0,0);
2940 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2941 ast_mutex_unlock(&chan_lock);
2943 return &call->read_fr;
2946 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2948 union parameter newparam;
2950 struct chan_call *call;
2951 const struct ast_tone_zone_sound *ts = NULL;
2953 ast_mutex_lock(&chan_lock);
2954 #if ASTERISK_VERSION_NUM < 110000
2955 call = ast->tech_pvt;
2957 call = ast_channel_tech_pvt(ast);
2960 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2961 ast_mutex_unlock(&chan_lock);
2966 case AST_CONTROL_BUSY:
2967 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2968 ast_setstate(ast, AST_STATE_BUSY);
2969 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2970 /* send message to lcr */
2971 memset(&newparam, 0, sizeof(union parameter));
2972 newparam.disconnectinfo.cause = 17;
2973 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2974 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2976 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2978 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2979 #if ASTERISK_VERSION_NUM < 110000
2980 ts = ast_get_indication_tone(ast->zone, "busy");
2982 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2986 case AST_CONTROL_CONGESTION:
2987 #if ASTERISK_VERSION_NUM < 110000
2988 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2990 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2992 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2993 /* send message to lcr */
2994 memset(&newparam, 0, sizeof(union parameter));
2995 #if ASTERISK_VERSION_NUM < 110000
2996 newparam.disconnectinfo.cause = ast->hangupcause;
2998 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
3000 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3001 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
3003 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
3005 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
3006 #if ASTERISK_VERSION_NUM < 110000
3007 ts = ast_get_indication_tone(ast->zone, "congestion");
3009 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
3013 case AST_CONTROL_PROCEEDING:
3014 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
3015 if (call->state == CHAN_LCR_STATE_IN_SETUP
3016 || call->state == CHAN_LCR_STATE_IN_DIALING) {
3017 /* send message to lcr */
3018 memset(&newparam, 0, sizeof(union parameter));
3019 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
3021 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
3024 case AST_CONTROL_RINGING:
3025 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
3026 ast_setstate(ast, AST_STATE_RING);
3027 if (call->state == CHAN_LCR_STATE_IN_SETUP
3028 || call->state == CHAN_LCR_STATE_IN_DIALING
3029 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
3030 /* send message to lcr */
3031 memset(&newparam, 0, sizeof(union parameter));
3032 send_message(MESSAGE_ALERTING, call->ref, &newparam);
3034 call->state = CHAN_LCR_STATE_IN_ALERTING;
3036 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
3037 #if ASTERISK_VERSION_NUM < 110000
3038 ts = ast_get_indication_tone(ast->zone, "ring");
3040 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
3044 case AST_CONTROL_PROGRESS:
3045 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
3046 /* request bchannel */
3047 if (!call->bchannel) {
3048 CDEBUG(call, ast, "Requesting B-channel.\n");
3049 memset(&newparam, 0, sizeof(union parameter));
3050 newparam.bchannel.type = BCHANNEL_REQUEST;
3051 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
3055 CDEBUG(call, ast, "Received indicate -1.\n");
3056 ast_playtones_stop(ast);
3060 case AST_CONTROL_VIDUPDATE:
3061 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
3064 case AST_CONTROL_HOLD:
3065 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
3066 /* send message to lcr */
3067 memset(&newparam, 0, sizeof(union parameter));
3068 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
3069 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3071 /*start music onhold*/
3072 #ifdef LCR_FOR_ASTERISK
3073 #if ASTERISK_VERSION_NUM <110000
3074 ast_moh_start(ast,data,ast->musicclass);
3076 ast_moh_start(ast,data,ast_channel_musicclass(ast));
3080 #ifdef LCR_FOR_CALLWEAVER
3081 ast_moh_start(ast, NULL);
3086 case AST_CONTROL_UNHOLD:
3087 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3088 /* send message to lcr */
3089 memset(&newparam, 0, sizeof(union parameter));
3090 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3091 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3097 #ifdef AST_CONTROL_SRCUPDATE
3098 case AST_CONTROL_SRCUPDATE:
3102 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3105 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3110 if (ts && ts->data[0]) {
3111 ast_playtones_start(ast, 0, ts->data, 1);
3115 ast_mutex_unlock(&chan_lock);
3122 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3124 struct chan_call *call;
3130 ast_mutex_lock(&chan_lock);
3131 #if ASTERISK_VERSION_NUM < 110000
3132 call = ast->tech_pvt;
3134 call = ast_channel_tech_pvt(ast);
3137 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3138 ast_mutex_unlock(&chan_lock);
3142 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3144 ast_mutex_unlock(&chan_lock);
3149 * send_text asterisk
3151 static int lcr_send_text(struct ast_channel *ast, const char *text)
3153 struct chan_call *call;
3154 union parameter newparam;
3156 ast_mutex_lock(&chan_lock);
3157 #if ASTERISK_VERSION_NUM < 110000
3158 call = ast->tech_pvt;
3160 call = ast_channel_tech_pvt(ast);
3163 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3164 ast_mutex_unlock(&chan_lock);
3168 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3169 memset(&newparam, 0, sizeof(union parameter));
3170 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3171 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3172 ast_mutex_unlock(&chan_lock);
3179 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3180 struct ast_channel *ast2, int flags,
3181 struct ast_frame **fo,
3182 struct ast_channel **rc, int timeoutms)
3185 struct chan_call *call1, *call2;
3186 struct ast_channel *carr[2], *who;
3188 struct ast_frame *f;
3191 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3196 /* join via dsp (if the channels are currently open) */
3197 ast_mutex_lock(&chan_lock);
3198 #if ASTERISK_VERSION_NUM < 110000
3199 call1 = ast1->tech_pvt;
3200 call2 = ast2->tech_pvt;
3202 call1 = ast_channel_tech_pvt(ast1);
3203 call2 = ast_channel_tech_pvt(ast2);
3205 if (!call1 || !call2) {
3206 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3207 ast_mutex_unlock(&chan_lock);
3208 return AST_BRIDGE_COMPLETE;
3211 /* join, if both call instances uses dsp
3212 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3214 if (!call1->nodsp && !call2->nodsp) {
3215 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3217 /* get bridge id and join */
3218 bridge_id = new_bridge_id();
3220 call1->bridge_id = bridge_id;
3221 if (call1->bchannel)
3222 bchannel_join(call1->bchannel, bridge_id);
3224 call2->bridge_id = bridge_id;
3225 if (call2->bchannel)
3226 bchannel_join(call2->bchannel, bridge_id);
3228 if (call1->nodsp && call2->nodsp)
3229 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3231 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3232 call1->bridge_call = call2;
3233 call2->bridge_call = call1;
3235 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3236 || call1->state == CHAN_LCR_STATE_IN_DIALING
3237 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3238 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3239 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3242 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3243 || call2->state == CHAN_LCR_STATE_IN_DIALING
3244 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3245 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3246 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3250 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3251 so let's do it for them. Hmpf.
3254 if (call1->on_hold) {
3255 union parameter newparam;
3257 memset(&newparam, 0, sizeof(union parameter));
3258 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3259 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3264 if (call2->on_hold) {
3265 union parameter newparam;
3267 memset(&newparam, 0, sizeof(union parameter));
3268 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3269 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3274 ast_mutex_unlock(&chan_lock);
3278 who = ast_waitfor_n(carr, 2, &to);
3281 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3286 if (!f || f->frametype == AST_FRAME_CONTROL) {
3288 CDEBUG(NULL, NULL, "Got hangup.\n");
3290 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3297 if ( f->frametype == AST_FRAME_DTMF ) {
3298 CDEBUG(NULL, NULL, "Got DTMF.\n");
3314 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3316 /* split channels */
3317 ast_mutex_lock(&chan_lock);
3318 #if ASTERISK_VERSION_NUM < 110000
3319 call1 = ast1->tech_pvt;
3320 call2 = ast2->tech_pvt;
3322 call1 = ast_channel_tech_pvt(ast1);
3323 call2 = ast_channel_tech_pvt(ast2);
3325 if (call1 && call1->bridge_id) {
3326 call1->bridge_id = 0;
3327 if (call1->bchannel)
3328 bchannel_join(call1->bchannel, 0);
3329 if (call1->bridge_call)
3330 call1->bridge_call->bridge_call = NULL;
3332 if (call2 && call1->bridge_id) {
3333 call2->bridge_id = 0;
3334 if (call2->bchannel)
3335 bchannel_join(call2->bchannel, 0);
3336 if (call2->bridge_call)
3337 call2->bridge_call->bridge_call = NULL;
3339 call1->bridge_call = NULL;
3340 call2->bridge_call = NULL;
3342 ast_mutex_unlock(&chan_lock);
3343 return AST_BRIDGE_COMPLETE;
3345 static struct ast_channel_tech lcr_tech = {
3347 .description = "Channel driver for connecting to Linux-Call-Router",
3348 #if ASTERISK_VERSION_NUM < 100000
3349 .capabilities = AST_FORMAT_ALAW,
3351 .requester = lcr_request,
3353 #ifdef LCR_FOR_ASTERISK
3354 .send_digit_begin = lcr_digit_begin,
3355 .send_digit_end = lcr_digit_end,
3358 #ifdef LCR_FOR_CALLWEAVER
3359 .send_digit = lcr_digit,
3363 .bridge = lcr_bridge,
3364 .hangup = lcr_hangup,
3365 .answer = lcr_answer,
3368 .indicate = lcr_indicate,
3370 .send_text = lcr_send_text,
3379 static int lcr_show_lcr (int fd, int argc, char *argv[])
3384 static int lcr_show_calls (int fd, int argc, char *argv[])
3389 static int lcr_reload_routing (int fd, int argc, char *argv[])
3394 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3399 static int lcr_port_block (int fd, int argc, char *argv[])
3404 static int lcr_port_unblock (int fd, int argc, char *argv[])
3409 static int lcr_port_unload (int fd, int argc, char *argv[])
3414 static struct ast_cli_entry cli_show_lcr =
3415 { {"lcr", "show", "lcr", NULL},
3417 "Shows current states of LCR core",
3418 "Usage: lcr show lcr\n",
3421 static struct ast_cli_entry cli_show_calls =
3422 { {"lcr", "show", "calls", NULL},
3424 "Shows current calls made by LCR and Asterisk",
3425 "Usage: lcr show calls\n",
3428 static struct ast_cli_entry cli_reload_routing =
3429 { {"lcr", "reload", "routing", NULL},
3431 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3432 "Usage: lcr reload routing\n",
3435 static struct ast_cli_entry cli_reload_interfaces =
3436 { {"lcr", "reload", "interfaces", NULL},
3437 lcr_reload_interfaces,
3438 "Reloads interfaces conf of LCR",
3439 "Usage: lcr reload interfaces\n",
3442 static struct ast_cli_entry cli_port_block =
3443 { {"lcr", "port", "block", NULL},
3445 "Blocks LCR port for further calls",
3446 "Usage: lcr port block \"<port>\"\n",
3449 static struct ast_cli_entry cli_port_unblock =
3450 { {"lcr", "port", "unblock", NULL},
3452 "Unblocks or loads LCR port, port is opened my mISDN",
3453 "Usage: lcr port unblock \"<port>\"\n",
3456 static struct ast_cli_entry cli_port_unload =
3457 { {"lcr", "port", "unload", NULL},
3459 "Unloads LCR port, port is closes by mISDN",
3460 "Usage: lcr port unload \"<port>\"\n",
3465 #ifdef LCR_FOR_ASTERISK
3466 #ifdef AST_1_8_OR_HIGHER
3467 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3469 static int lcr_config_exec(struct ast_channel *ast, void *data)
3473 #ifdef LCR_FOR_CALLWEAVER
3474 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3477 struct chan_call *call;
3479 ast_mutex_lock(&chan_lock);
3481 #ifdef LCR_FOR_ASTERISK
3482 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3485 #ifdef LCR_FOR_CALLWEAVER
3486 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3492 if (call->ast == ast)
3498 #ifdef LCR_FOR_ASTERISK
3499 apply_opt(call, (char *)data);
3502 #ifdef LCR_FOR_CALLWEAVER
3503 apply_opt(call, (char *)argv[0]);
3507 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3509 ast_mutex_unlock(&chan_lock);
3514 * module loading and destruction
3516 int load_module(void)
3519 char options_error[256];
3521 for (i = 0; i < 256; i++) {
3522 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3523 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3526 if (read_options(options_error) == 0) {
3527 CERROR(NULL, NULL, "%s", options_error);
3529 #ifdef LCR_FOR_ASTERISK
3530 return AST_MODULE_LOAD_DECLINE;
3533 #ifdef LCR_FOR_CALLWEAVER
3539 ast_mutex_init(&chan_lock);
3540 ast_mutex_init(&log_lock);
3542 if (bchannel_initialize()) {
3543 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3546 #ifdef LCR_FOR_ASTERISK
3547 return AST_MODULE_LOAD_DECLINE;
3550 #ifdef LCR_FOR_CALLWEAVER
3556 #if ASTERISK_VERSION_NUM < 100000
3557 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3559 struct ast_format tmp;
3560 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3561 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3562 return AST_MODULE_LOAD_DECLINE;
3564 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3566 if (ast_channel_register(&lcr_tech)) {
3567 CERROR(NULL, NULL, "Unable to register channel class\n");
3568 bchannel_deinitialize();
3571 #ifdef LCR_FOR_ASTERISK
3572 return AST_MODULE_LOAD_DECLINE;
3575 #ifdef LCR_FOR_CALLWEAVER
3580 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3582 #ifdef LCR_FOR_ASTERISK
3583 "lcr_config(<opt><optarg>:<opt>:...)\n"
3586 #ifdef LCR_FOR_CALLWEAVER
3587 "lcr_config(<opt><optarg>:<opt>:...)\n",
3590 "Sets LCR opts. and optargs\n"
3592 "The available options are:\n"
3593 " d - Send display text on called phone, text is the optarg.\n"
3594 " n - Don't detect dtmf tones on called channel.\n"
3595 " h - Force data call (HDLC).\n"
3596 " t - Disable mISDN_dsp features (required for fax application).\n"
3597 " q - Add queue to make fax stream seamless (required for fax app).\n"
3598 " Use queue size in miliseconds for optarg. (try 250)\n"
3599 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3600 " Use time to detect for optarg.\n"
3601 " c - Make crypted outgoing call, optarg is keyindex.\n"
3602 " e - Perform echo cancelation on this channel.\n"
3603 " Takes mISDN pipeline option as optarg.\n"
3604 " s - Send Non Inband DTMF as inband.\n"
3605 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3606 " vr - rxgain control\n"
3607 " vt - txgain control\n"
3608 " Volume changes at factor 2 ^ optarg.\n"
3609 " k - use keypad to dial this call.\n"
3611 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3612 " -> use 16 for fax (3.1k audio)\n"
3614 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3615 "options: \"n:t:q250\" for seamless audio transmission.\n"
3620 ast_cli_register(&cli_show_lcr);
3621 ast_cli_register(&cli_show_calls);
3622 ast_cli_register(&cli_reload_routing);
3623 ast_cli_register(&cli_reload_interfaces);
3624 ast_cli_register(&cli_port_block);
3625 ast_cli_register(&cli_port_unblock);
3626 ast_cli_register(&cli_port_unload);
3629 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3630 /* failed to create thread */
3631 bchannel_deinitialize();
3633 ast_channel_unregister(&lcr_tech);
3635 #ifdef LCR_FOR_ASTERISK
3636 return AST_MODULE_LOAD_DECLINE;
3639 #ifdef LCR_FOR_CALLWEAVER
3647 int unload_module(void)
3649 /* First, take us out of the channel loop */
3650 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3652 pthread_cancel(chan_tid);
3656 del_timer(&socket_retry);
3658 unregister_fd(&wake_fd);
3659 close(wake_pipe[0]);
3660 close(wake_pipe[1]);
3662 // ast_mutex_unlock(&chan_lock);
3664 ast_channel_unregister(&lcr_tech);
3666 ast_unregister_application("lcr_config");
3668 if (mISDN_created) {
3669 bchannel_deinitialize();
3673 if (lcr_sock >= 0) {
3678 #if ASTERISK_VERSION_NUM >= 100000
3679 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3684 int reload_module(void)
3690 #ifdef LCR_FOR_ASTERISK
3691 #define AST_MODULE "chan_lcr"
3694 #ifdef LCR_FOR_CALLWEAVER
3699 ast_mutex_lock(&usecnt_lock);
3701 ast_mutex_unlock(&usecnt_lock);
3706 #ifdef LCR_FOR_ASTERISK
3707 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3708 .load = load_module,
3709 .unload = unload_module,
3710 .reload = reload_module,
3714 #ifdef LCR_FOR_CALLWEAVER
3715 char *description(void)