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>
141 * Fwd declare struct ast_channel to get rid of gcc warning about
142 * incompatible pointer type passed to ast_register_application2.
146 #include <asterisk/module.h>
147 #include <asterisk/channel.h>
148 #include <asterisk/config.h>
149 #include <asterisk/logger.h>
150 #include <asterisk/pbx.h>
151 #include <asterisk/options.h>
152 #include <asterisk/io.h>
153 #include <asterisk/frame.h>
154 #include <asterisk/translate.h>
155 #include <asterisk/cli.h>
156 #include <asterisk/musiconhold.h>
157 #include <asterisk/dsp.h>
158 #include <asterisk/translate.h>
159 #include <asterisk/file.h>
160 #ifdef LCR_FOR_ASTERISK
161 #include <asterisk/callerid.h>
163 #ifdef LCR_FOR_CALLWEAVER
164 #include <asterisk/phone_no_utils.h>
167 #include <asterisk/indications.h>
168 #include <asterisk/app.h>
169 #include <asterisk/features.h>
170 #include <asterisk/sched.h>
171 #if ASTERISK_VERSION_NUM < 110000
172 #include <asterisk/version.h>
174 #include "extension.h"
176 #include "callerid.h"
177 #include "lcrsocket.h"
181 #include "chan_lcr.h"
183 CHAN_LCR_STATE // state description structure
184 MESSAGES // message text
186 #ifdef LCR_FOR_CALLWEAVER
187 AST_MUTEX_DEFINE_STATIC(rand_lock);
190 unsigned char flip_bits[256];
192 #ifdef LCR_FOR_CALLWEAVER
193 static struct ast_frame nullframe = { AST_FRAME_NULL, };
198 char lcr_type[]="lcr";
200 #ifdef LCR_FOR_CALLWEAVER
201 static ast_mutex_t usecnt_lock;
203 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
207 ast_mutex_t chan_lock; /* global lock */
208 ast_mutex_t log_lock; /* logging log */
210 * used to indicate change in file descriptors, so select function's result may
213 int global_change = 0;
216 struct lcr_fd wake_fd;
218 int glob_channel = 0;
221 struct lcr_fd socket_fd;
222 struct lcr_timer socket_retry;
225 struct admin_list *next;
226 struct admin_message msg;
227 } *admin_first = NULL;
229 static struct ast_channel_tech lcr_tech;
234 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, ...)
237 char call_text[128] = "NULL";
238 char ast_text[128] = "NULL";
241 ast_mutex_lock(&log_lock);
244 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
245 buffer[sizeof(buffer)-1]=0;
249 sprintf(call_text, "%d", call->ref);
251 #if ASTERISK_VERSION_NUM < 110000
252 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
254 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
256 ast_text[sizeof(ast_text)-1] = '\0';
258 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
259 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
261 ast_mutex_unlock(&log_lock);
265 * channel and call instances
267 struct chan_call *call_first;
271 * special case: 0: find new ref, that has not been assigned a ref yet
274 struct chan_call *find_call_ref(unsigned int ref)
276 struct chan_call *call = call_first;
277 int assigned = (ref > 0);
280 if (call->ref == ref && call->ref_was_assigned == assigned)
287 void free_call(struct chan_call *call)
289 struct chan_call **temp = &call_first;
293 *temp = (*temp)->next;
294 if (call->pipe[0] > -1)
295 close(call->pipe[0]);
296 if (call->pipe[1] > -1)
297 close(call->pipe[1]);
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 if (message_type != MESSAGE_TRAFFIC)
370 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
372 adminp = &admin_first;
374 adminp = &((*adminp)->next);
375 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
377 CERROR(NULL, NULL, "No memory for message to LCR.\n");
382 admin->msg.message = ADMIN_MESSAGE;
383 admin->msg.u.msg.type = message_type;
384 admin->msg.u.msg.ref = ref;
385 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
386 socket_fd.when |= LCR_FD_WRITE;
391 rc = write(wake_pipe[1], &byte, 1);
398 * apply options (in locked state)
400 void apply_opt(struct chan_call *call, char *data)
402 union parameter newparam;
403 char string[1024], *p = string, *opt;//, *key;
409 strncpy(string, data, sizeof(string)-1);
410 string[sizeof(string)-1] = '\0';
413 while((opt = strsep(&p, ":"))) {
416 if (opt[1] == '\0') {
417 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
420 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
421 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
422 strncpy(call->display, opt+1, sizeof(call->display)-1);
424 memset(&newparam, 0, sizeof(union parameter));
425 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
426 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
430 if (opt[1] != '\0') {
431 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
434 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
439 if (opt[1] == '\0') {
440 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
444 /* check for 0xXXXX... type of key */
445 if (!!strncmp((char *)key, "0x", 2)) {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
450 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
451 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
456 if (*key>='0' && *key<='9')
457 call->bf_key[i] = (*key-'0') << 8;
458 else if (*key>='a' && *key<='f')
459 call->bf_key[i] = (*key-'a'+10) << 8;
460 else if (*key>='A' && *key<='F')
461 call->bf_key[i] = (*key-'A'+10) << 8;
465 if (*key>='0' && *key<='9')
466 call->bf_key[i] += (*key - '0');
467 else if (*key>='a' && *key<='f')
468 call->bf_key[i] += (*key - 'a' + 10);
469 else if (*key>='A' && *key<='F')
470 call->bf_key[i] += (*key - 'A' + 10);
477 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
481 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
483 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
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 'q' (queue) expects parameter.\n", opt);
500 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
501 call->tx_queue = atoi(opt+1);
505 if (opt[1] == '\0') {
506 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
509 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
510 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
512 bchannel_pipeline(call->bchannel, call->pipeline);
516 if (opt[1] == '\0') {
517 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
520 call->faxdetect=atoi(opt+1);
522 call->dsp=ast_dsp_new();
524 #ifdef LCR_FOR_CALLWEAVER
525 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
527 #ifdef LCR_FOR_ASTERISK
528 #ifdef DSP_FEATURE_DTMF_DETECT
529 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
531 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
536 #ifdef LCR_FOR_CALLWEAVER
537 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
539 #ifdef LCR_FOR_ASTERISK
540 #if ASTERISK_VERSION_NUM < 100000
541 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
543 struct ast_format src;
544 struct ast_format dst;
545 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
546 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
547 call->trans=ast_translator_build_path(&dst, &src);
552 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
555 if (opt[1] != '\0') {
556 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
559 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
564 if (opt[1] != '\0') {
565 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
568 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
569 call->inband_dtmf = 1;
573 if (opt[1] != 'r' && opt[1] != 't') {
574 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
578 if (gain < -8 || gain >8) {
579 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
582 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
584 call->rx_gain = gain;
586 bchannel_gain(call->bchannel, call->rx_gain, 0);
588 call->tx_gain = gain;
590 bchannel_gain(call->bchannel, call->tx_gain, 1);
595 if (opt[1] != '\0') {
596 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
599 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
604 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
610 * send setup info to LCR
611 * this function is called, when asterisk call is received and ref is received
613 static void send_setup_to_lcr(struct chan_call *call)
615 union parameter newparam;
616 struct ast_channel *ast = call->ast;
619 if (!call->ast || !call->ref)
622 #ifdef AST_1_8_OR_HIGHER
623 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
625 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
628 /* send setup message to LCR */
629 memset(&newparam, 0, sizeof(union parameter));
630 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
631 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
633 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
635 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
636 if (!!strcmp(call->interface, "pbx"))
637 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
638 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
639 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
640 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
641 call->display[0] = '\0';
643 #ifdef AST_1_8_OR_HIGHER
644 /* set stored call information */
645 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
646 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
648 if (call->cid_num[0])
649 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
650 if (call->cid_name[0])
651 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
652 if (call->cid_rdnis[0]) {
653 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
654 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
655 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
657 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
658 case AST_PRES_RESTRICTED:
659 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
661 case AST_PRES_UNAVAILABLE:
662 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
664 case AST_PRES_ALLOWED:
666 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
668 switch(ast->cid.cid_ton) {
670 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
673 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
676 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
679 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
682 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
683 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
685 #if ASTERISK_VERSION_NUM < 110000
686 // ast->transfercapability = atoi(tmp);
687 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
689 // ast_channel_transfercapability_set(ast, atoi(tmp));
690 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
692 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
694 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
696 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
697 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
699 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
700 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
701 send_message(MESSAGE_SETUP, call->ref, &newparam);
702 if (call->tx_queue) {
703 memset(&newparam, 0, sizeof(union parameter));
704 newparam.queue = call->tx_queue * 8;
705 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
708 /* change to outgoing setup state */
709 call->state = CHAN_LCR_STATE_OUT_SETUP;
713 * send dialing info to LCR
714 * this function is called, when setup acknowledge is received and dialing
717 static void send_dialque_to_lcr(struct chan_call *call)
719 union parameter newparam;
721 if (!call->ast || !call->ref || !call->dialque[0])
724 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
726 /* send setup message to LCR */
727 memset(&newparam, 0, sizeof(union parameter));
729 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
731 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
732 call->dialque[0] = '\0';
733 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
737 * in case of a bridge, the unsupported message can be forwarded directly
738 * to the remote call.
740 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
744 if (!call->bridge_call) return;
745 CDEBUG(call, NULL, "Sending message due bridging.\n");
746 send_message(message_type, call->bridge_call->ref, param);
750 * send release message to LCR
752 static void send_release(struct chan_call *call, int cause, int location)
754 union parameter newparam;
756 /* sending release */
757 memset(&newparam, 0, sizeof(union parameter));
758 newparam.disconnectinfo.cause = cause;
759 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
760 send_message(MESSAGE_RELEASE, call->ref, &newparam);
764 * check if extension matches and start asterisk
765 * if it can match, proceed
768 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
771 union parameter newparam;
772 #if ASTERISK_VERSION_NUM < 110000
773 char *exten = ast->exten;
775 char s_exten[AST_MAX_EXTENSION];
778 strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
784 #if ASTERISK_VERSION_NUM < 110000
785 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
787 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
792 #if ASTERISK_VERSION_NUM < 110000
793 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
794 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
796 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
797 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
802 #if ASTERISK_VERSION_NUM < 110000
803 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
804 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);
806 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
807 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));
812 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
813 /* send setup acknowledge to lcr */
814 memset(&newparam, 0, sizeof(union parameter));
815 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
818 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
823 #if ASTERISK_VERSION_NUM < 110000
824 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
826 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
828 /* send setup acknowledge to lcr */
829 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
830 memset(&newparam, 0, sizeof(union parameter));
831 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
835 call->state = CHAN_LCR_STATE_IN_DIALING;
837 /* if match, start pbx */
838 #if ASTERISK_VERSION_NUM < 110000
839 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
841 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
843 CDEBUG(call, ast, "Extensions matches.\n");
847 /* send setup acknowledge to lcr */
848 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
849 memset(&newparam, 0, sizeof(union parameter));
850 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
854 call->state = CHAN_LCR_STATE_IN_DIALING;
857 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
861 #if ASTERISK_VERSION_NUM < 110000
864 if (!*ast_channel_exten(ast)) {
866 /* send setup acknowledge to lcr */
867 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
868 memset(&newparam, 0, sizeof(union parameter));
869 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
873 call->state = CHAN_LCR_STATE_IN_DIALING;
876 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
884 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
885 send_release(call, cause, LOCATION_PRIVATE_LOCAL);
887 /* release asterisk */
888 #if ASTERISK_VERSION_NUM < 110000
889 ast->hangupcause = call->cause;
891 ast_channel_hangupcause_set(ast, call->cause);
893 /* change to release state */
894 call->state = CHAN_LCR_STATE_RELEASE;
895 ast_hangup(ast); // call will be destroyed here
899 /* send setup to asterisk */
900 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
902 #ifdef LCR_FOR_CALLWEAVER
904 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
907 ret = ast_pbx_start(ast);
909 cause = (ret==-2)?34:27;
912 call->pbx_started = 1;
913 ast_setstate(ast, AST_STATE_RING);
917 * incoming setup from LCR
919 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
921 struct ast_channel *ast;
922 #ifdef AST_1_8_OR_HIGHER
923 struct ast_party_redirecting *ast_redir;
924 struct ast_party_caller *ast_caller;
926 struct ast_callerid *ast_caller;
928 #if ASTERISK_VERSION_NUM >= 110000
929 struct ast_party_redirecting s_ast_redir;
930 struct ast_party_caller s_ast_caller;
931 ast_party_redirecting_init(&s_ast_redir);
932 ast_party_caller_init(&s_ast_caller);
934 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
936 /* create asterisk channel instrance */
938 #ifdef LCR_FOR_CALLWEAVER
939 ast = ast_channel_alloc(1);
942 #ifdef LCR_FOR_ASTERISK
943 #ifdef AST_1_8_OR_HIGHER
944 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
946 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
950 #if ASTERISK_VERSION_NUM < 110000
951 #ifdef AST_1_8_OR_HIGHER
952 ast_redir = &ast->redirecting;
953 ast_caller = &ast->caller;
955 ast_caller = &ast->cid;
958 ast_redir = &s_ast_redir;
959 ast_caller = &s_ast_caller;
964 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
965 send_release(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
972 #if ASTERISK_VERSION_NUM < 110000
973 ast->tech_pvt = call;
974 ast->tech = &lcr_tech;
975 ast->fds[0] = call->pipe[0];
977 ast_channel_tech_pvt_set(ast, call);
978 ast_channel_tech_set(ast, &lcr_tech);
979 ast_channel_set_fd(ast, 0, call->pipe[0]);
982 /* fill setup information */
983 if (param->setup.dialinginfo.id)
984 #if ASTERISK_VERSION_NUM < 110000
985 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
986 if (param->setup.dialinginfo.context[0])
987 strncpy(ast->context, param->setup.dialinginfo.context, AST_MAX_CONTEXT-1);
989 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
991 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
992 if (param->setup.context[0])
993 ast_channel_context_set(ast, param->setup.context);
995 ast_channel_context_set(ast, param->setup.callerinfo.interface);
999 #ifdef AST_1_8_OR_HIGHER
1000 if (param->setup.callerinfo.id[0]) {
1001 ast_caller->id.number.valid = 1;
1002 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
1003 if (!param->setup.callerinfo.id[0]) {
1004 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1005 ast_caller->id.number.plan = (0 << 4) | 1;
1007 switch (param->setup.callerinfo.present) {
1008 case INFO_PRESENT_ALLOWED:
1009 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
1011 case INFO_PRESENT_RESTRICTED:
1012 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1015 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1017 switch (param->setup.callerinfo.screen) {
1018 case INFO_SCREEN_USER:
1019 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1021 case INFO_SCREEN_USER_VERIFIED_PASSED:
1022 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1024 case INFO_SCREEN_USER_VERIFIED_FAILED:
1025 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1028 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1030 switch (param->setup.callerinfo.ntype) {
1031 case INFO_NTYPE_SUBSCRIBER:
1032 ast_caller->id.number.plan = (4 << 4) | 1;
1034 case INFO_NTYPE_NATIONAL:
1035 ast_caller->id.number.plan = (2 << 4) | 1;
1037 case INFO_NTYPE_INTERNATIONAL:
1038 ast_caller->id.number.plan = (1 << 4) | 1;
1041 ast_caller->id.number.plan = (0 << 4) | 1;
1044 if (param->setup.callerinfo.id2[0]) {
1045 ast_caller->ani.number.valid = 1;
1046 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1047 switch (param->setup.callerinfo.present2) {
1048 case INFO_PRESENT_ALLOWED:
1049 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1051 case INFO_PRESENT_RESTRICTED:
1052 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1055 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1057 switch (param->setup.callerinfo.screen2) {
1058 case INFO_SCREEN_USER:
1059 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1061 case INFO_SCREEN_USER_VERIFIED_PASSED:
1062 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1064 case INFO_SCREEN_USER_VERIFIED_FAILED:
1065 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1068 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1070 switch (param->setup.callerinfo.ntype2) {
1071 case INFO_NTYPE_SUBSCRIBER:
1072 ast_caller->ani.number.plan = (4 << 4) | 1;
1074 case INFO_NTYPE_NATIONAL:
1075 ast_caller->ani.number.plan = (2 << 4) | 1;
1077 case INFO_NTYPE_INTERNATIONAL:
1078 ast_caller->ani.number.plan = (1 << 4) | 1;
1081 ast_caller->ani.number.plan = (0 << 4) | 1;
1084 if (param->setup.callerinfo.name[0]) {
1085 ast_caller->id.name.valid = 1;
1086 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1088 #if ASTERISK_VERSION_NUM >= 110000
1089 ast_channel_caller_set(ast, ast_caller);
1091 if (param->setup.redirinfo.id[0]) {
1092 ast_redir->from.number.valid = 1;
1093 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1094 switch (param->setup.redirinfo.present) {
1095 case INFO_PRESENT_ALLOWED:
1096 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1098 case INFO_PRESENT_RESTRICTED:
1099 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1102 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1104 switch (param->setup.redirinfo.screen) {
1105 case INFO_SCREEN_USER:
1106 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1108 case INFO_SCREEN_USER_VERIFIED_PASSED:
1109 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1111 case INFO_SCREEN_USER_VERIFIED_FAILED:
1112 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1115 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1117 switch (param->setup.redirinfo.ntype) {
1118 case INFO_NTYPE_SUBSCRIBER:
1119 ast_redir->from.number.plan = (4 << 4) | 1;
1121 case INFO_NTYPE_NATIONAL:
1122 ast_redir->from.number.plan = (2 << 4) | 1;
1124 case INFO_NTYPE_INTERNATIONAL:
1125 ast_redir->from.number.plan = (1 << 4) | 1;
1128 ast_redir->from.number.plan = (0 << 4) | 1;
1130 #if ASTERISK_VERSION_NUM >= 110000
1131 ast_channel_redirecting_set(ast, ast_redir);
1135 memset(&ast->cid, 0, sizeof(ast->cid));
1136 if (param->setup.callerinfo.id[0])
1137 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1138 if (param->setup.callerinfo.id2[0])
1139 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1140 if (param->setup.callerinfo.name[0])
1141 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1142 if (param->setup.redirinfo.id[0])
1143 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1144 switch (param->setup.callerinfo.present) {
1145 case INFO_PRESENT_ALLOWED:
1146 ast->cid.cid_pres = AST_PRES_ALLOWED;
1148 case INFO_PRESENT_RESTRICTED:
1149 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1152 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1154 switch (param->setup.callerinfo.ntype) {
1155 case INFO_NTYPE_SUBSCRIBER:
1156 ast->cid.cid_ton = 4;
1158 case INFO_NTYPE_NATIONAL:
1159 ast->cid.cid_ton = 2;
1161 case INFO_NTYPE_INTERNATIONAL:
1162 ast->cid.cid_ton = 1;
1165 ast->cid.cid_ton = 0;
1169 #if ASTERISK_VERSION_NUM < 110000
1170 ast->transfercapability = param->setup.capainfo.bearer_capa;
1172 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1174 /* enable hdlc if transcap is data */
1175 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1177 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1179 /* configure channel */
1180 #if ASTERISK_VERSION_NUM < 100000
1181 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1182 ast->readformat = ast->rawreadformat = ast->nativeformats;
1183 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1185 #if ASTERISK_VERSION_NUM < 110000
1186 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1187 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1188 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1189 ast_set_write_format(ast, &ast->rawwriteformat);
1190 ast_set_read_format(ast, &ast->rawreadformat);
1192 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1193 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1194 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1195 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1196 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1199 #if ASTERISK_VERSION_NUM < 110000
1201 ast->hangupcause = 0;
1203 ast_channel_priority_set(ast, 1);
1204 ast_channel_hangupcause_set(ast, 0);
1208 call->state = CHAN_LCR_STATE_IN_SETUP;
1210 if (!call->pbx_started)
1211 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1215 * incoming setup acknowledge from LCR
1217 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1219 if (!call->ast) return;
1221 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1223 /* send pending digits in dialque */
1224 if (call->dialque[0])
1225 send_dialque_to_lcr(call);
1226 /* change to overlap state */
1227 call->state = CHAN_LCR_STATE_OUT_DIALING;
1231 * incoming proceeding from LCR
1233 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1235 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1238 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1239 /* queue event for asterisk */
1240 if (call->ast && call->pbx_started) {
1245 rc = write(wake_pipe[1], &byte, 1);
1247 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1253 * incoming alerting from LCR
1255 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1257 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1260 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1261 /* queue event to asterisk */
1262 if (call->ast && call->pbx_started) {
1267 rc = write(wake_pipe[1], &byte, 1);
1269 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1274 * incoming connect from LCR
1276 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1278 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1281 call->state = CHAN_LCR_STATE_CONNECT;
1282 /* copy connectinfo */
1283 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1284 /* queue event to asterisk */
1285 if (call->ast && call->pbx_started) {
1290 rc = write(wake_pipe[1], &byte, 1);
1292 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1297 * incoming disconnect from LCR
1299 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1301 struct ast_channel *ast = call->ast;
1303 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1306 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1308 call->cause = param->disconnectinfo.cause;
1309 call->location = param->disconnectinfo.location;
1310 /* if bridge, forward disconnect and return */
1313 if (call->bridge_call) {
1314 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1315 bridge_message_if_bridged(call, message_type, param);
1319 /* release lcr with same cause */
1320 send_release(call, call->cause, call->location);
1322 /* change to release state */
1323 call->state = CHAN_LCR_STATE_RELEASE;
1324 /* queue release asterisk */
1326 #if ASTERISK_VERSION_NUM < 110000
1327 ast->hangupcause = call->cause;
1329 ast_channel_hangupcause_set(ast, call->cause);
1331 if (call->pbx_started) {
1336 rc = write(wake_pipe[1], &byte, 1);
1338 strcpy(call->queue_string, "H"); // overwrite other indications
1340 ast_hangup(ast); // call will be destroyed here
1346 * incoming release from LCR
1348 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1350 struct ast_channel *ast = call->ast;
1352 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1356 /* change to release state */
1357 call->state = CHAN_LCR_STATE_RELEASE;
1358 /* copy release info */
1360 call->cause = param->disconnectinfo.cause;
1361 call->location = param->disconnectinfo.location;
1363 /* if we have an asterisk instance, queue hangup, else we are done */
1365 #if ASTERISK_VERSION_NUM < 110000
1366 ast->hangupcause = call->cause;
1368 ast_channel_hangupcause_set(ast, call->cause);
1370 if (call->pbx_started) {
1375 rc = write(wake_pipe[1], &byte, 1);
1377 strcpy(call->queue_string, "H");
1379 ast_hangup(ast); // call will be destroyed here
1388 * incoming information from LCR
1390 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1392 struct ast_channel *ast = call->ast;
1394 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1398 /* pbx not started */
1399 if (!call->pbx_started) {
1400 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1401 #if ASTERISK_VERSION_NUM < 110000
1402 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1404 ast_channel_exten_set(ast, param->information.id);
1406 lcr_start_pbx(call, ast, param->information.sending_complete);
1410 /* change dailing state after setup */
1411 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1412 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1413 call->state = CHAN_LCR_STATE_IN_DIALING;
1414 // ast_setstate(ast, AST_STATE_DIALING);
1418 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1423 rc = write(wake_pipe[1], &byte, 1);
1425 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1428 /* use bridge to forware message not supported by asterisk */
1429 if (call->state == CHAN_LCR_STATE_CONNECT) {
1430 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1431 bridge_message_if_bridged(call, message_type, param);
1436 * incoming information from LCR
1438 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1440 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1442 if (!call->ast) return;
1444 /* use bridge to forware message not supported by asterisk */
1445 bridge_message_if_bridged(call, message_type, param);
1449 * incoming information from LCR
1451 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1453 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1455 if (!call->ast) return;
1457 /* use bridge to forware message not supported by asterisk */
1458 bridge_message_if_bridged(call, message_type, param);
1462 * incoming pattern from LCR
1464 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1466 union parameter newparam;
1468 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1470 if (!call->ast) return;
1472 /* pattern are indicated only once */
1473 if (call->has_pattern)
1475 call->has_pattern = 1;
1477 /* request bchannel */
1478 CDEBUG(call, call->ast, "Requesting audio path (ref=%d)\n", call->ref);
1479 memset(&newparam, 0, sizeof(union parameter));
1480 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
1482 /* queue PROGRESS, because tones are available */
1483 if (call->ast && call->pbx_started) {
1488 rc = write(wake_pipe[1], &byte, 1);
1490 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1495 * got dtmf from bchannel (locked state)
1497 void lcr_in_dtmf(struct chan_call *call, int val)
1499 struct ast_channel *ast = call->ast;
1504 if (!call->pbx_started)
1507 if (!call->dsp_dtmf) {
1508 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1512 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1519 rc = write(wake_pipe[1], &byte, 1);
1521 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1525 * message received from LCR
1527 int receive_message(int message_type, unsigned int ref, union parameter *param)
1529 struct chan_call *call;
1530 union parameter newparam;
1533 memset(&newparam, 0, sizeof(union parameter));
1535 /* handle new ref */
1536 if (message_type == MESSAGE_NEWREF) {
1537 if (param->newref.direction) {
1538 /* new ref from lcr */
1539 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1540 if (!ref || find_call_ref(ref)) {
1541 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1544 /* allocate new call instance */
1545 call = alloc_call();
1547 call->state = CHAN_LCR_STATE_IN_PREPARE;
1550 call->ref_was_assigned = 1;
1551 /* set dtmf (default, use option 'n' to disable */
1553 /* wait for setup (or release from asterisk) */
1555 /* new ref, as requested from this remote application */
1556 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1557 call = find_call_ref(0);
1559 /* send release, if ref does not exist */
1560 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1565 call->ref_was_assigned = 1;
1566 /* set dtmf (default, use option 'n' to disable */
1568 /* send pending setup info */
1569 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1570 send_setup_to_lcr(call);
1571 /* release if asterisk has signed off */
1572 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1575 send_release(call, call->cause, call->location);
1577 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1588 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1591 call = find_call_ref(ref);
1593 /* ignore ref that is not used (anymore) */
1594 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1598 /* handle messages */
1599 switch(message_type) {
1601 lcr_in_setup(call, message_type, param);
1604 case MESSAGE_OVERLAP:
1605 lcr_in_overlap(call, message_type, param);
1608 case MESSAGE_PROCEEDING:
1609 lcr_in_proceeding(call, message_type, param);
1612 case MESSAGE_ALERTING:
1613 lcr_in_alerting(call, message_type, param);
1616 case MESSAGE_CONNECT:
1617 lcr_in_connect(call, message_type, param);
1620 case MESSAGE_DISCONNECT:
1621 lcr_in_disconnect(call, message_type, param);
1624 case MESSAGE_RELEASE:
1625 lcr_in_release(call, message_type, param);
1628 case MESSAGE_INFORMATION:
1629 lcr_in_information(call, message_type, param);
1632 case MESSAGE_NOTIFY:
1633 lcr_in_notify(call, message_type, param);
1636 case MESSAGE_FACILITY:
1637 lcr_in_facility(call, message_type, param);
1640 case MESSAGE_PATTERN: // audio available from LCR
1641 if (!call->has_pattern)
1642 lcr_in_pattern(call, message_type, param);
1645 case MESSAGE_NOPATTERN: // audio not available from LCR
1648 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1649 call->audiopath = param->audiopath;
1652 case MESSAGE_TRAFFIC: // if remote audio connected or hold
1654 unsigned char *p = param->traffic.data;
1655 int i, len = param->traffic.len;
1656 for (i = 0; i < len; i++, p++)
1659 rc = write(call->pipe[1], param->traffic.data, param->traffic.len);
1663 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1670 * release all calls (due to broken socket)
1672 static void release_all_calls(void)
1674 struct chan_call *call;
1679 /* no ast, so we may directly free call */
1681 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1685 /* already in release process */
1686 if (call->state == CHAN_LCR_STATE_RELEASE) {
1690 /* release or queue release */
1692 call->state = CHAN_LCR_STATE_RELEASE;
1693 if (!call->pbx_started) {
1694 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1695 ast_hangup(call->ast); // call will be destroyed here
1698 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1703 rc = write(wake_pipe[1], &byte, 1);
1705 strcpy(call->queue_string, "H");
1710 void close_socket(void);
1713 * warning! not thread safe
1714 * returns -1 for socket error, 0 for no work, 1 for work
1716 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1719 struct admin_list *admin;
1720 struct admin_message msg;
1722 if ((what & LCR_FD_READ)) {
1723 /* read from socket */
1724 len = read(lcr_sock, &msg, sizeof(msg));
1726 CERROR(NULL, NULL, "Socket closed.(read)\n");
1728 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1730 release_all_calls();
1731 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1735 if (len != sizeof(msg)) {
1736 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1739 if (msg.message != ADMIN_MESSAGE) {
1740 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1743 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1745 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1750 if ((what & LCR_FD_WRITE)) {
1751 /* write to socket */
1753 socket_fd.when &= ~LCR_FD_WRITE;
1756 admin = admin_first;
1757 len = write(lcr_sock, &admin->msg, sizeof(msg));
1759 CERROR(NULL, NULL, "Socket closed.(write)\n");
1763 if (len != sizeof(msg)) {
1764 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1768 admin_first = admin->next;
1772 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1781 * open and close socket and thread
1783 int open_socket(void)
1786 struct sockaddr_un sock_address;
1787 union parameter param;
1790 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1791 CERROR(NULL, NULL, "Failed to create socket.\n");
1795 /* set socket address and name */
1796 memset(&sock_address, 0, sizeof(sock_address));
1797 sock_address.sun_family = PF_UNIX;
1798 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1800 /* connect socket */
1801 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1804 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1808 /* register socket fd */
1809 memset(&socket_fd, 0, sizeof(socket_fd));
1810 socket_fd.fd = lcr_sock;
1811 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1813 /* enque hello message */
1814 memset(¶m, 0, sizeof(param));
1815 strcpy(param.hello.application, "asterisk");
1816 send_message(MESSAGE_HELLO, 0, ¶m);
1821 void close_socket(void)
1823 struct admin_list *admin, *temp;
1825 /* socket not created */
1829 unregister_fd(&socket_fd);
1831 /* flush pending messages */
1832 admin = admin_first;
1835 admin = admin->next;
1847 /* sending queue to asterisk */
1848 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1853 rc = read(wake_pipe[0], &byte, 1);
1860 static void handle_queue()
1862 struct chan_call *call;
1863 struct ast_channel *ast;
1864 struct ast_frame fr;
1870 p = call->queue_string;
1873 if (ast_channel_trylock(ast)) {
1874 ast_mutex_unlock(&chan_lock);
1876 ast_mutex_lock(&chan_lock);
1882 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1883 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1886 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1887 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1890 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1891 ast_queue_control(ast, AST_CONTROL_RINGING);
1892 ast_setstate(ast, AST_STATE_RINGING);
1895 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1896 ast_queue_control(ast, AST_CONTROL_ANSWER);
1899 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1900 ast_queue_hangup(ast);
1902 case '1': case '2': case '3': case 'A':
1903 case '4': case '5': case '6': case 'B':
1904 case '7': case '8': case '9': case 'C':
1905 case '*': case '0': case '#': case 'D':
1906 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1907 /* send digit to asterisk */
1908 memset(&fr, 0, sizeof(fr));
1910 #ifdef LCR_FOR_ASTERISK
1911 fr.frametype = AST_FRAME_DTMF_BEGIN;
1914 #ifdef LCR_FOR_CALLWEAVER
1915 fr.frametype = AST_FRAME_DTMF;
1918 #ifdef AST_1_8_OR_HIGHER
1919 fr.subclass.integer = *p;
1923 fr.delivery = ast_tv(0, 0);
1924 ast_queue_frame(ast, &fr);
1926 #ifdef LCR_FOR_ASTERISK
1927 fr.frametype = AST_FRAME_DTMF_END;
1928 ast_queue_frame(ast, &fr);
1933 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1937 call->queue_string[0] = '\0';
1938 ast_channel_unlock(ast);
1944 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1946 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1947 if (open_socket() < 0)
1948 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1953 void lock_chan(void)
1955 ast_mutex_lock(&chan_lock);
1958 void unlock_chan(void)
1960 ast_mutex_unlock(&chan_lock);
1963 /* chan_lcr thread */
1964 static void *chan_thread(void *arg)
1966 if (pipe(wake_pipe) < 0) {
1967 CERROR(NULL, NULL, "Failed to open pipe.\n");
1970 memset(&wake_fd, 0, sizeof(wake_fd));
1971 wake_fd.fd = wake_pipe[0];
1972 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1974 memset(&socket_retry, 0, sizeof(socket_retry));
1975 add_timer(&socket_retry, handle_retry, NULL, 0);
1977 /* open socket the first time */
1978 handle_retry(NULL, NULL, 0);
1980 ast_mutex_lock(&chan_lock);
1984 select_main(0, &global_change, lock_chan, unlock_chan);
1991 * new asterisk instance
1994 #ifdef AST_1_8_OR_HIGHER
1995 #if ASTERISK_VERSION_NUM < 100000
1996 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
1998 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2001 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2004 char exten[256], *dial, *interface, *opt;
2005 struct ast_channel *ast;
2006 struct chan_call *call;
2007 #ifdef AST_1_8_OR_HIGHER
2008 const struct ast_party_redirecting *req_redir;
2009 const struct ast_party_caller *req_caller;
2012 ast_mutex_lock(&chan_lock);
2013 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2015 /* if socket is closed */
2017 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2018 ast_mutex_unlock(&chan_lock);
2022 /* create call instance */
2023 call = alloc_call();
2025 /* failed to create instance */
2026 ast_mutex_unlock(&chan_lock);
2030 /* create asterisk channel instrance */
2032 #ifdef LCR_FOR_ASTERISK
2033 #ifdef AST_1_8_OR_HIGHER
2034 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2036 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2040 #ifdef LCR_FOR_CALLWEAVER
2041 ast = ast_channel_alloc(1);
2045 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2047 /* failed to create instance */
2048 ast_mutex_unlock(&chan_lock);
2051 #if ASTERISK_VERSION_NUM < 110000
2052 ast->tech = &lcr_tech;
2053 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2054 #ifdef AST_1_8_OR_HIGHER
2055 req_redir = &requestor->redirecting;
2056 req_caller = &requestor->caller;
2059 ast_channel_tech_set(ast, &lcr_tech);
2060 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2061 req_redir = ast_channel_redirecting(requestor);
2062 req_caller = ast_channel_caller(requestor);
2064 /* configure channel */
2065 #if ASTERISK_VERSION_NUM < 100000
2066 #if ASTERISK_VERSION_NUM < 110000
2067 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2068 ast->readformat = ast->rawreadformat = ast->nativeformats;
2069 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2071 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2072 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2073 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2076 #if ASTERISK_VERSION_NUM < 110000
2077 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2078 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2079 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2080 ast_set_write_format(ast, &ast->rawwriteformat);
2081 ast_set_read_format(ast, &ast->rawreadformat);
2083 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2084 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2085 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2086 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2087 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2090 #if ASTERISK_VERSION_NUM < 110000
2092 ast->hangupcause = 0;
2094 ast_channel_priority_set(ast, 1);
2095 ast_channel_hangupcause_set(ast, 0);
2100 #if ASTERISK_VERSION_NUM < 110000
2101 ast->tech_pvt = call;
2102 ast->fds[0] = call->pipe[0];
2104 ast_channel_tech_pvt_set(ast, call);
2105 ast_channel_set_fd(ast, 0, call->pipe[0]);
2107 call->pbx_started = 0;
2109 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2112 * Extract interface, dialstring, options from data.
2115 * <interface>/<dialstring>
2116 * <interface>/<dialstring>/options
2118 strncpy(exten, (char *)data, sizeof(exten)-1);
2119 exten[sizeof(exten)-1] = '\0';
2120 if ((dial = strchr(exten, '/'))) {
2123 if ((opt = strchr(dial, '/')))
2132 strncpy(call->interface, interface, sizeof(call->interface)-1);
2133 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2134 apply_opt(call, (char *)opt);
2136 #ifdef AST_1_8_OR_HIGHER
2137 // clone_variables(requestor, ast);
2140 ast->caller.ani.number.valid= req_caller->ani.number.valid;
2141 if (req_caller->ani.number.valid)
2142 if (req_caller->ani.number.str)
2143 if (req_caller->ani.number.str[0])
2144 ast->caller.ani.number.str= strdup(req_caller->ani.number.str);
2145 ast->caller.ani.number.plan= req_caller->ani.number.plan;
2146 ast->caller.ani.number.presentation= req_caller->ani.number.presentation;
2148 ast->caller.ani.name.valid= req_caller->ani.name.valid;
2149 if (req_caller->ani.name.valid)
2150 if (req_caller->ani.name.str)
2151 if (req_caller->ani.name.str[0])
2152 ast->caller.ani.name.str= strdup(req_caller->ani.name.str);
2153 ast->caller.ani.name.presentation= req_caller->ani.name.presentation;
2155 ast->caller.ani.subaddress.valid= req_caller->ani.subaddress.valid;
2156 if (req_caller->ani.subaddress.valid)
2157 if (req_caller->ani.subaddress.str)
2158 if (req_caller->ani.subaddress.str[0])
2159 ast->caller.ani.subaddress.str= strdup(req_caller->ani.subaddress.str);
2160 ast->caller.ani.subaddress.type= req_caller->ani.subaddress.type;
2162 ast->caller.id.number.valid= req_caller->id.number.valid;
2163 if (req_caller->id.number.valid)
2164 if (req_caller->id.number.str)
2165 if (req_caller->id.number.str[0])
2166 ast->caller.id.number.str= strdup(req_caller->id.number.str);
2167 ast->caller.id.number.plan= req_caller->id.number.plan;
2168 ast->caller.id.number.presentation= req_caller->id.number.presentation;
2170 ast->caller.id.name.valid= req_caller->id.name.valid;
2171 if (req_caller->id.name.valid)
2172 if (req_caller->id.name.str)
2173 if (req_caller->id.name.str[0])
2174 ast->caller.id.name.str= strdup(req_caller->id.name.str);
2175 ast->caller.id.name.presentation= req_caller->id.name.presentation;
2177 ast->caller.id.subaddress.valid= req_caller->id.subaddress.valid;
2178 if (req_caller->id.subaddress.valid)
2179 if (req_caller->id.subaddress.str)
2180 if (req_caller->id.subaddress.str[0])
2181 ast->caller.id.subaddress.str= strdup(req_caller->id.subaddress.str);
2182 ast->caller.id.subaddress.type= req_caller->id.subaddress.type;
2184 if (requestor->dialed.number.str)
2185 if (requestor->dialed.number.str[0])
2186 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2187 ast->dialed.number.plan= requestor->dialed.number.plan;
2189 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2190 if (requestor->dialed.subaddress.valid)
2191 if (requestor->dialed.subaddress.str)
2192 if (requestor->dialed.subaddress.str[0])
2193 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2194 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2196 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2197 ast->redirecting.count= req_redir->count;
2198 ast->redirecting.reason= req_redir->reason;
2200 ast->redirecting.from.number.valid= req_redir->from.number.valid;
2201 if (req_redir->from.number.valid)
2202 if (req_redir->from.number.str)
2203 if (req_redir->from.number.str[0])
2204 ast->redirecting.from.number.str= strdup(req_redir->from.number.str);
2205 ast->redirecting.from.number.plan= req_redir->from.number.plan;
2206 ast->redirecting.from.number.presentation= req_redir->from.number.presentation;
2208 ast->redirecting.to.number.valid= req_redir->to.number.valid;
2209 if (req_redir->to.number.valid)
2210 if (req_redir->to.number.str)
2211 if (req_redir->to.number.str[0])
2212 ast->redirecting.to.number.str= strdup(req_redir->to.number.str);
2213 ast->redirecting.to.number.plan= req_redir->to.number.plan;
2214 ast->redirecting.to.number.presentation= req_redir->to.number.presentation;
2216 /* store call information for setup */
2219 if (requestor && req_caller->id.number.valid) {
2220 if (req_caller->id.number.str)
2221 strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2222 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2223 case AST_PRES_RESTRICTED:
2224 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2226 case AST_PRES_UNAVAILABLE:
2227 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2229 case AST_PRES_ALLOWED:
2231 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2233 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2234 case AST_PRES_USER_NUMBER_UNSCREENED:
2235 call->callerinfo.screen = INFO_SCREEN_USER;
2237 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2238 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2240 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2241 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2244 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2246 switch((req_caller->id.number.plan >> 4) & 7) {
2248 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2251 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2254 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2257 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2260 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2263 if (requestor && req_caller->ani.number.valid) {
2264 if (req_caller->ani.number.str)
2265 strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2266 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2267 case AST_PRES_RESTRICTED:
2268 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2270 case AST_PRES_UNAVAILABLE:
2271 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2273 case AST_PRES_ALLOWED:
2275 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2277 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2278 case AST_PRES_USER_NUMBER_UNSCREENED:
2279 call->callerinfo.screen2 = INFO_SCREEN_USER;
2281 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2282 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2284 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2285 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2288 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2290 switch((req_caller->ani.number.plan >> 4) & 7) {
2292 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2295 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2298 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2301 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2304 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2307 if (requestor && req_caller->id.name.valid) {
2308 if (req_caller->id.name.str)
2309 strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2313 if (requestor && req_redir->from.number.valid) {
2314 call->redirinfo.itype = INFO_ITYPE_CHAN;
2315 if (req_redir->from.number.str)
2316 strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2317 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2318 case AST_PRES_RESTRICTED:
2319 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2321 case AST_PRES_UNAVAILABLE:
2322 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2324 case AST_PRES_ALLOWED:
2326 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2328 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2329 case AST_PRES_USER_NUMBER_UNSCREENED:
2330 call->redirinfo.screen = INFO_SCREEN_USER;
2332 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2333 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2335 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2336 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2339 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2341 switch((req_redir->from.number.plan >> 4) & 7) {
2343 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2346 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2349 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2352 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2357 ast_mutex_unlock(&chan_lock);
2362 * call from asterisk
2364 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2366 union parameter newparam;
2367 struct chan_call *call;
2368 #if ASTERISK_VERSION_NUM >= 110000
2369 int transfercapability;
2372 ast_mutex_lock(&chan_lock);
2373 #if ASTERISK_VERSION_NUM < 110000
2374 call = ast->tech_pvt;
2376 call = ast_channel_tech_pvt(ast);
2379 #ifdef LCR_FOR_CALLWEAVER
2381 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2385 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2386 ast_mutex_unlock(&chan_lock);
2390 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2392 /* pbx process is started */
2393 call->pbx_started = 1;
2394 /* send MESSAGE_NEWREF */
2395 memset(&newparam, 0, sizeof(union parameter));
2396 newparam.newref.direction = 0; /* request from app */
2397 send_message(MESSAGE_NEWREF, 0, &newparam);
2399 /* set hdlc if capability requires hdlc */
2400 #if ASTERISK_VERSION_NUM < 110000
2401 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2402 || ast->transfercapability == INFO_BC_DATARESTRICTED
2403 || ast->transfercapability == INFO_BC_VIDEO)
2405 transfercapability=ast_channel_transfercapability(ast);
2406 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2407 || transfercapability == INFO_BC_DATARESTRICTED
2408 || transfercapability == INFO_BC_VIDEO)
2411 /* if hdlc is forced by option, we change transcap to data */
2413 #if ASTERISK_VERSION_NUM < 110000
2414 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2415 && ast->transfercapability != INFO_BC_DATARESTRICTED
2416 && ast->transfercapability != INFO_BC_VIDEO)
2417 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2419 && transfercapability != INFO_BC_DATAUNRESTRICTED
2420 && transfercapability != INFO_BC_DATARESTRICTED
2421 && transfercapability != INFO_BC_VIDEO)
2422 transfercapability = INFO_BC_DATAUNRESTRICTED;
2425 #ifndef AST_1_8_OR_HIGHER
2426 call->cid_num[0] = 0;
2427 call->cid_name[0] = 0;
2428 call->cid_rdnis[0] = 0;
2430 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2431 strncpy(call->cid_num, ast->cid.cid_num,
2432 sizeof(call->cid_num)-1);
2433 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2434 strncpy(call->cid_name, ast->cid.cid_name,
2435 sizeof(call->cid_name)-1);
2436 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2437 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2438 sizeof(call->cid_rdnis)-1);
2441 ast_mutex_unlock(&chan_lock);
2445 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2447 static const char* dtmf_tones[] = {
2448 "!941+1336/100,!0/100", /* 0 */
2449 "!697+1209/100,!0/100", /* 1 */
2450 "!697+1336/100,!0/100", /* 2 */
2451 "!697+1477/100,!0/100", /* 3 */
2452 "!770+1209/100,!0/100", /* 4 */
2453 "!770+1336/100,!0/100", /* 5 */
2454 "!770+1477/100,!0/100", /* 6 */
2455 "!852+1209/100,!0/100", /* 7 */
2456 "!852+1336/100,!0/100", /* 8 */
2457 "!852+1477/100,!0/100", /* 9 */
2458 "!697+1633/100,!0/100", /* A */
2459 "!770+1633/100,!0/100", /* B */
2460 "!852+1633/100,!0/100", /* C */
2461 "!941+1633/100,!0/100", /* D */
2462 "!941+1209/100,!0/100", /* * */
2463 "!941+1477/100,!0/100" }; /* # */
2465 if (digit >= '0' && digit <='9')
2466 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2467 else if (digit >= 'A' && digit <= 'D')
2468 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2469 else if (digit == '*')
2470 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2471 else if (digit == '#')
2472 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2474 #if ASTERISK_VERSION_NUM < 110000
2475 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2477 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2482 #ifdef LCR_FOR_ASTERISK
2483 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2485 #ifdef LCR_FOR_CALLWEAVER
2486 static int lcr_digit(struct ast_channel *ast, char digit)
2489 struct chan_call *call;
2490 union parameter newparam;
2493 #ifdef LCR_FOR_CALLWEAVER
2494 int inband_dtmf = 0;
2497 /* only pass IA5 number space */
2498 if (digit > 126 || digit < 32)
2501 ast_mutex_lock(&chan_lock);
2502 #if ASTERISK_VERSION_NUM < 110000
2503 call = ast->tech_pvt;
2505 call = ast_channel_tech_pvt(ast);
2508 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2509 ast_mutex_unlock(&chan_lock);
2513 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2515 /* send information or queue them */
2516 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2517 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2518 memset(&newparam, 0, sizeof(union parameter));
2520 newparam.information.keypad[0] = digit;
2521 newparam.information.keypad[1] = '\0';
2523 newparam.information.id[0] = digit;
2524 newparam.information.id[1] = '\0';
2526 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2529 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2530 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2532 strncat(call->dialque, buf, strlen(call->dialque)-1);
2535 ast_mutex_unlock(&chan_lock);
2537 #ifdef LCR_FOR_ASTERISK
2541 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2543 int inband_dtmf = 0;
2544 struct chan_call *call;
2547 ast_mutex_lock(&chan_lock);
2549 #if ASTERISK_VERSION_NUM < 110000
2550 call = ast->tech_pvt;
2552 call = ast_channel_tech_pvt(ast);
2557 "Received digit from Asterisk, "
2558 "but no call instance exists.\n");
2559 ast_mutex_unlock(&chan_lock);
2563 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2565 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2569 ast_mutex_unlock(&chan_lock);
2572 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2573 send_digit_to_chan(ast, digit);
2579 static int lcr_answer(struct ast_channel *ast)
2581 union parameter newparam;
2582 struct chan_call *call;
2584 ast_mutex_lock(&chan_lock);
2585 #if ASTERISK_VERSION_NUM < 110000
2586 call = ast->tech_pvt;
2588 call = ast_channel_tech_pvt(ast);
2591 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2592 ast_mutex_unlock(&chan_lock);
2596 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2598 /* copy connectinfo, if bridged */
2599 if (call->bridge_call)
2600 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2601 /* send connect message to lcr */
2602 if (call->state != CHAN_LCR_STATE_CONNECT) {
2603 memset(&newparam, 0, sizeof(union parameter));
2604 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2605 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2606 call->state = CHAN_LCR_STATE_CONNECT;
2610 // memset(&newparam, 0, sizeof(union parameter));
2611 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2613 ast_mutex_unlock(&chan_lock);
2617 static int lcr_hangup(struct ast_channel *ast)
2619 struct chan_call *call;
2620 pthread_t tid = pthread_self();
2622 if (!pthread_equal(tid, chan_tid)) {
2623 ast_mutex_lock(&chan_lock);
2625 #if ASTERISK_VERSION_NUM < 110000
2626 call = ast->tech_pvt;
2628 call = ast_channel_tech_pvt(ast);
2631 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2632 if (!pthread_equal(tid, chan_tid)) {
2633 ast_mutex_unlock(&chan_lock);
2638 if (!pthread_equal(tid, chan_tid))
2639 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2641 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2643 /* disconnect asterisk, maybe not required */
2644 #if ASTERISK_VERSION_NUM < 110000
2645 ast->tech_pvt = NULL;
2648 ast_channel_tech_pvt_set(ast, NULL);
2649 ast_channel_set_fd(ast, 0, -1);
2653 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2654 #if ASTERISK_VERSION_NUM < 110000
2655 if (ast->hangupcause > 0)
2656 send_release(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2658 if (ast_channel_hangupcause(ast) > 0)
2659 send_release(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2662 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2665 if (!pthread_equal(tid, chan_tid)) {
2666 ast_mutex_unlock(&chan_lock);
2670 /* ref is not set, due to prepare setup or release */
2671 if (call->state == CHAN_LCR_STATE_RELEASE) {
2672 /* we get the response to our release */
2673 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2676 /* during prepare, we change to release state */
2677 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2678 call->state = CHAN_LCR_STATE_RELEASE;
2682 if (!pthread_equal(tid, chan_tid)) {
2683 ast_mutex_unlock(&chan_lock);
2688 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2690 union parameter newparam;
2691 struct chan_call *call;
2692 struct ast_frame * f = fr;
2693 unsigned char *p, *q;
2696 #if ASTERISK_VERSION_NUM < 100000
2697 #ifdef AST_1_8_OR_HIGHER
2698 if (!f->subclass.codec)
2702 CDEBUG(NULL, ast, "No subclass\n");
2704 #ifdef AST_1_8_OR_HIGHER
2705 #if ASTERISK_VERSION_NUM < 100000
2706 #if ASTERISK_VERSION_NUM < 110000
2707 if (!(f->subclass.codec & ast->nativeformats)) {
2709 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2712 #if ASTERISK_VERSION_NUM < 110000
2713 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2715 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2719 #if ASTERISK_VERSION_NUM < 110000
2720 if (!(f->subclass & ast->nativeformats)) {
2722 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2726 "Unexpected format. "
2727 "Activating emergency conversion...\n");
2729 #ifdef AST_1_8_OR_HIGHER
2730 #if ASTERISK_VERSION_NUM < 100000
2731 ast_set_write_format(ast, f->subclass.codec);
2733 ast_set_write_format(ast, &f->subclass.format);
2736 ast_set_write_format(ast, f->subclass);
2738 #if ASTERISK_VERSION_NUM < 110000
2739 f = (ast->writetrans) ? ast_translate(
2740 ast->writetrans, fr, 0) : fr;
2742 f = (ast_channel_writetrans(ast)) ? ast_translate(
2743 ast_channel_writetrans(ast), fr, 0) : fr;
2747 ast_mutex_lock(&chan_lock);
2748 #if ASTERISK_VERSION_NUM < 110000
2749 call = ast->tech_pvt;
2751 call = ast_channel_tech_pvt(ast);
2754 ast_mutex_unlock(&chan_lock);
2761 p = *((unsigned char **)&(f->data));
2762 q = newparam.traffic.data;
2763 memset(&newparam, 0, sizeof(union parameter));
2765 l = (len > sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
2766 newparam.traffic.len = l;
2769 *q++ = flip_bits[*p++];
2770 send_message(MESSAGE_TRAFFIC, call->ref, &newparam);
2772 ast_mutex_unlock(&chan_lock);
2780 static struct ast_frame *lcr_read(struct ast_channel *ast)
2782 struct chan_call *call;
2785 ast_mutex_lock(&chan_lock);
2786 #if ASTERISK_VERSION_NUM < 110000
2787 call = ast->tech_pvt;
2789 call = ast_channel_tech_pvt(ast);
2792 ast_mutex_unlock(&chan_lock);
2795 if (call->pipe[0] > -1) {
2796 if (call->rebuffer && !call->hdlc) {
2797 /* Make sure we have a complete 20ms (160byte) frame */
2798 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2800 call->framepos += len;
2803 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2805 if (len < 0 && errno == EAGAIN) {
2806 ast_mutex_unlock(&chan_lock);
2808 #ifdef LCR_FOR_ASTERISK
2809 return &ast_null_frame;
2812 #ifdef LCR_FOR_CALLWEAVER
2818 close(call->pipe[0]);
2821 ast_mutex_unlock(&chan_lock);
2823 } else if (call->rebuffer && call->framepos < 160) {
2824 /* Not a complete frame, so we send a null-frame */
2825 ast_mutex_unlock(&chan_lock);
2826 return &ast_null_frame;
2830 call->read_fr.frametype = AST_FRAME_VOICE;
2831 #ifdef AST_1_8_OR_HIGHER
2832 #if ASTERISK_VERSION_NUM < 100000
2833 #if ASTERISK_VERSION_NUM < 110000
2834 call->read_fr.subclass.codec = ast->nativeformats;
2836 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2839 #if ASTERISK_VERSION_NUM < 110000
2840 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2842 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2844 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2847 #if ASTERISK_VERSION_NUM < 110000
2848 call->read_fr.subclass = ast->nativeformats;
2850 call->read_fr.subclass = ast_channel_nativeformats(ast);
2853 if (call->rebuffer) {
2854 call->read_fr.datalen = call->framepos;
2855 call->read_fr.samples = call->framepos;
2858 call->read_fr.datalen = len;
2859 call->read_fr.samples = len;
2861 call->read_fr.delivery = ast_tv(0,0);
2862 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2863 ast_mutex_unlock(&chan_lock);
2865 return &call->read_fr;
2868 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2870 union parameter newparam;
2872 struct chan_call *call;
2873 const struct ast_tone_zone_sound *ts = NULL;
2875 ast_mutex_lock(&chan_lock);
2876 #if ASTERISK_VERSION_NUM < 110000
2877 call = ast->tech_pvt;
2879 call = ast_channel_tech_pvt(ast);
2882 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2883 ast_mutex_unlock(&chan_lock);
2888 case AST_CONTROL_BUSY:
2889 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2890 ast_setstate(ast, AST_STATE_BUSY);
2891 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2892 /* send message to lcr */
2893 memset(&newparam, 0, sizeof(union parameter));
2894 newparam.disconnectinfo.cause = 17;
2895 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2896 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2898 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2900 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2901 #if ASTERISK_VERSION_NUM < 110000
2902 ts = ast_get_indication_tone(ast->zone, "busy");
2904 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2908 case AST_CONTROL_CONGESTION:
2909 #if ASTERISK_VERSION_NUM < 110000
2910 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2912 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2914 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2915 /* send message to lcr */
2916 memset(&newparam, 0, sizeof(union parameter));
2917 #if ASTERISK_VERSION_NUM < 110000
2918 newparam.disconnectinfo.cause = ast->hangupcause;
2920 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
2922 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2923 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2925 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2927 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2928 #if ASTERISK_VERSION_NUM < 110000
2929 ts = ast_get_indication_tone(ast->zone, "congestion");
2931 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
2935 case AST_CONTROL_PROCEEDING:
2936 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2937 if (call->state == CHAN_LCR_STATE_IN_SETUP
2938 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2939 /* send message to lcr */
2940 memset(&newparam, 0, sizeof(union parameter));
2941 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2943 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2946 case AST_CONTROL_RINGING:
2947 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2948 ast_setstate(ast, AST_STATE_RING);
2949 if (call->state == CHAN_LCR_STATE_IN_SETUP
2950 || call->state == CHAN_LCR_STATE_IN_DIALING
2951 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2952 /* send message to lcr */
2953 memset(&newparam, 0, sizeof(union parameter));
2954 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2956 call->state = CHAN_LCR_STATE_IN_ALERTING;
2958 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2959 #if ASTERISK_VERSION_NUM < 110000
2960 ts = ast_get_indication_tone(ast->zone, "ring");
2962 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
2966 case AST_CONTROL_PROGRESS:
2967 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2968 /* request bchannel */
2969 CDEBUG(call, ast, "Requesting audio path.\n");
2970 memset(&newparam, 0, sizeof(union parameter));
2971 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
2974 CDEBUG(call, ast, "Received indicate -1.\n");
2975 ast_playtones_stop(ast);
2979 case AST_CONTROL_VIDUPDATE:
2980 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2983 case AST_CONTROL_HOLD:
2984 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2985 /* send message to lcr */
2986 memset(&newparam, 0, sizeof(union parameter));
2987 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2988 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2990 /*start music onhold*/
2991 #ifdef LCR_FOR_ASTERISK
2992 #if ASTERISK_VERSION_NUM <110000
2993 ast_moh_start(ast,data,ast->musicclass);
2995 ast_moh_start(ast,data,ast_channel_musicclass(ast));
2999 #ifdef LCR_FOR_CALLWEAVER
3000 ast_moh_start(ast, NULL);
3005 case AST_CONTROL_UNHOLD:
3006 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3007 /* send message to lcr */
3008 memset(&newparam, 0, sizeof(union parameter));
3009 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3010 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3016 #ifdef AST_CONTROL_SRCUPDATE
3017 case AST_CONTROL_SRCUPDATE:
3021 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3024 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3029 if (ts && ts->data[0]) {
3030 ast_playtones_start(ast, 0, ts->data, 1);
3034 ast_mutex_unlock(&chan_lock);
3041 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3043 struct chan_call *call;
3049 ast_mutex_lock(&chan_lock);
3050 #if ASTERISK_VERSION_NUM < 110000
3051 call = ast->tech_pvt;
3053 call = ast_channel_tech_pvt(ast);
3056 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3057 ast_mutex_unlock(&chan_lock);
3061 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3063 ast_mutex_unlock(&chan_lock);
3068 * send_text asterisk
3070 static int lcr_send_text(struct ast_channel *ast, const char *text)
3072 struct chan_call *call;
3073 union parameter newparam;
3075 ast_mutex_lock(&chan_lock);
3076 #if ASTERISK_VERSION_NUM < 110000
3077 call = ast->tech_pvt;
3079 call = ast_channel_tech_pvt(ast);
3082 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3083 ast_mutex_unlock(&chan_lock);
3087 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3088 memset(&newparam, 0, sizeof(union parameter));
3089 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3090 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3091 ast_mutex_unlock(&chan_lock);
3098 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3099 struct ast_channel *ast2, int flags,
3100 struct ast_frame **fo,
3101 struct ast_channel **rc, int timeoutms)
3104 struct chan_call *call1, *call2;
3105 struct ast_channel *carr[2], *who;
3107 struct ast_frame *f;
3110 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3115 /* join via dsp (if the channels are currently open) */
3116 ast_mutex_lock(&chan_lock);
3117 #if ASTERISK_VERSION_NUM < 110000
3118 call1 = ast1->tech_pvt;
3119 call2 = ast2->tech_pvt;
3121 call1 = ast_channel_tech_pvt(ast1);
3122 call2 = ast_channel_tech_pvt(ast2);
3124 if (!call1 || !call2) {
3125 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3126 ast_mutex_unlock(&chan_lock);
3127 return AST_BRIDGE_COMPLETE;
3130 /* join, if both call instances uses dsp
3131 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3133 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3135 /* get bridge id and join */
3136 bridge_id = new_bridge_id();
3139 call1->bridge_id = bridge_id;
3140 if (call1->bchannel)
3141 bchannel_join(call1->bchannel, bridge_id);
3143 call2->bridge_id = bridge_id;
3144 if (call2->bchannel)
3145 bchannel_join(call2->bchannel, bridge_id);
3151 call1->bridge_call = call2;
3152 call2->bridge_call = call1;
3154 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3155 || call1->state == CHAN_LCR_STATE_IN_DIALING
3156 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3157 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3158 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3161 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3162 || call2->state == CHAN_LCR_STATE_IN_DIALING
3163 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3164 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3165 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3169 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3170 so let's do it for them. Hmpf.
3173 if (call1->on_hold) {
3174 union parameter newparam;
3176 memset(&newparam, 0, sizeof(union parameter));
3177 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3178 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3183 if (call2->on_hold) {
3184 union parameter newparam;
3186 memset(&newparam, 0, sizeof(union parameter));
3187 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3188 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3193 ast_mutex_unlock(&chan_lock);
3197 who = ast_waitfor_n(carr, 2, &to);
3200 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3205 if (!f || f->frametype == AST_FRAME_CONTROL) {
3207 CDEBUG(NULL, NULL, "Got hangup.\n");
3209 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3216 if ( f->frametype == AST_FRAME_DTMF ) {
3217 CDEBUG(NULL, NULL, "Got DTMF.\n");
3233 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3235 /* split channels */
3236 ast_mutex_lock(&chan_lock);
3237 #if ASTERISK_VERSION_NUM < 110000
3238 call1 = ast1->tech_pvt;
3239 call2 = ast2->tech_pvt;
3241 call1 = ast_channel_tech_pvt(ast1);
3242 call2 = ast_channel_tech_pvt(ast2);
3244 if (call1 && call1->bridge_id) {
3245 call1->bridge_id = 0;
3246 if (call1->bridge_call)
3247 call1->bridge_call->bridge_call = NULL;
3249 if (call2 && call1->bridge_id) {
3250 call2->bridge_id = 0;
3251 if (call2->bridge_call)
3252 call2->bridge_call->bridge_call = NULL;
3254 call1->bridge_call = NULL;
3255 call2->bridge_call = NULL;
3257 ast_mutex_unlock(&chan_lock);
3258 return AST_BRIDGE_COMPLETE;
3260 static struct ast_channel_tech lcr_tech = {
3262 .description = "Channel driver for connecting to Linux-Call-Router",
3263 #if ASTERISK_VERSION_NUM < 100000
3264 .capabilities = AST_FORMAT_ALAW,
3266 .requester = lcr_request,
3268 #ifdef LCR_FOR_ASTERISK
3269 .send_digit_begin = lcr_digit_begin,
3270 .send_digit_end = lcr_digit_end,
3273 #ifdef LCR_FOR_CALLWEAVER
3274 .send_digit = lcr_digit,
3278 .bridge = lcr_bridge,
3279 .hangup = lcr_hangup,
3280 .answer = lcr_answer,
3283 .indicate = lcr_indicate,
3285 .send_text = lcr_send_text,
3294 static int lcr_show_lcr (int fd, int argc, char *argv[])
3299 static int lcr_show_calls (int fd, int argc, char *argv[])
3304 static int lcr_reload_routing (int fd, int argc, char *argv[])
3309 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3314 static int lcr_port_block (int fd, int argc, char *argv[])
3319 static int lcr_port_unblock (int fd, int argc, char *argv[])
3324 static int lcr_port_unload (int fd, int argc, char *argv[])
3329 static struct ast_cli_entry cli_show_lcr =
3330 { {"lcr", "show", "lcr", NULL},
3332 "Shows current states of LCR core",
3333 "Usage: lcr show lcr\n",
3336 static struct ast_cli_entry cli_show_calls =
3337 { {"lcr", "show", "calls", NULL},
3339 "Shows current calls made by LCR and Asterisk",
3340 "Usage: lcr show calls\n",
3343 static struct ast_cli_entry cli_reload_routing =
3344 { {"lcr", "reload", "routing", NULL},
3346 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3347 "Usage: lcr reload routing\n",
3350 static struct ast_cli_entry cli_reload_interfaces =
3351 { {"lcr", "reload", "interfaces", NULL},
3352 lcr_reload_interfaces,
3353 "Reloads interfaces conf of LCR",
3354 "Usage: lcr reload interfaces\n",
3357 static struct ast_cli_entry cli_port_block =
3358 { {"lcr", "port", "block", NULL},
3360 "Blocks LCR port for further calls",
3361 "Usage: lcr port block \"<port>\"\n",
3364 static struct ast_cli_entry cli_port_unblock =
3365 { {"lcr", "port", "unblock", NULL},
3367 "Unblocks or loads LCR port, port is opened my mISDN",
3368 "Usage: lcr port unblock \"<port>\"\n",
3371 static struct ast_cli_entry cli_port_unload =
3372 { {"lcr", "port", "unload", NULL},
3374 "Unloads LCR port, port is closes by mISDN",
3375 "Usage: lcr port unload \"<port>\"\n",
3380 #ifdef LCR_FOR_ASTERISK
3381 #ifdef AST_1_8_OR_HIGHER
3382 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3384 static int lcr_config_exec(struct ast_channel *ast, void *data)
3388 #ifdef LCR_FOR_CALLWEAVER
3389 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3392 struct chan_call *call;
3394 ast_mutex_lock(&chan_lock);
3396 #ifdef LCR_FOR_ASTERISK
3397 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3400 #ifdef LCR_FOR_CALLWEAVER
3401 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3407 if (call->ast == ast)
3413 #ifdef LCR_FOR_ASTERISK
3414 apply_opt(call, (char *)data);
3417 #ifdef LCR_FOR_CALLWEAVER
3418 apply_opt(call, (char *)argv[0]);
3422 if (call->tx_queue) {
3423 union parameter newparam;
3425 memset(&newparam, 0, sizeof(union parameter));
3426 newparam.queue = call->tx_queue * 8;
3427 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
3430 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3432 ast_mutex_unlock(&chan_lock);
3437 * module loading and destruction
3439 int load_module(void)
3442 char options_error[256];
3444 for (i = 0; i < 256; i++) {
3445 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3446 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3449 if (read_options(options_error) == 0) {
3450 CERROR(NULL, NULL, "%s", options_error);
3452 #ifdef LCR_FOR_ASTERISK
3453 return AST_MODULE_LOAD_DECLINE;
3456 #ifdef LCR_FOR_CALLWEAVER
3462 ast_mutex_init(&chan_lock);
3463 ast_mutex_init(&log_lock);
3465 #if ASTERISK_VERSION_NUM < 100000
3466 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3468 struct ast_format tmp;
3469 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3470 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3471 return AST_MODULE_LOAD_DECLINE;
3473 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3475 if (ast_channel_register(&lcr_tech)) {
3476 CERROR(NULL, NULL, "Unable to register channel class\n");
3479 #ifdef LCR_FOR_ASTERISK
3480 return AST_MODULE_LOAD_DECLINE;
3483 #ifdef LCR_FOR_CALLWEAVER
3488 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3490 #ifdef LCR_FOR_ASTERISK
3491 "lcr_config(<opt><optarg>:<opt>:...)\n"
3494 #ifdef LCR_FOR_CALLWEAVER
3495 "lcr_config(<opt><optarg>:<opt>:...)\n",
3498 "Sets LCR opts. and optargs\n"
3500 "The available options are:\n"
3501 " d - Send display text on called phone, text is the optarg.\n"
3502 " n - Don't detect dtmf tones on called channel.\n"
3503 " h - Force data call (HDLC).\n"
3504 " q - Add queue to make fax stream seamless (required for fax app).\n"
3505 " Use queue size in miliseconds for optarg. (try 250)\n"
3506 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3507 " Use time to detect for optarg.\n"
3509 " c - Make crypted outgoing call, optarg is keyindex.\n"
3510 " e - Perform echo cancelation on this channel.\n"
3512 " Takes mISDN pipeline option as optarg.\n"
3513 " s - Send Non Inband DTMF as inband.\n"
3514 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3516 " vr - rxgain control\n"
3517 " vt - txgain control\n"
3519 " Volume changes at factor 2 ^ optarg.\n"
3520 " k - use keypad to dial this call.\n"
3522 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3523 " -> use 16 for fax (3.1k audio)\n"
3525 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3526 "options: \"n:t:q250\" for seamless audio transmission.\n"
3531 ast_cli_register(&cli_show_lcr);
3532 ast_cli_register(&cli_show_calls);
3533 ast_cli_register(&cli_reload_routing);
3534 ast_cli_register(&cli_reload_interfaces);
3535 ast_cli_register(&cli_port_block);
3536 ast_cli_register(&cli_port_unblock);
3537 ast_cli_register(&cli_port_unload);
3540 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3541 /* failed to create thread */
3543 ast_channel_unregister(&lcr_tech);
3545 #ifdef LCR_FOR_ASTERISK
3546 return AST_MODULE_LOAD_DECLINE;
3549 #ifdef LCR_FOR_CALLWEAVER
3557 int unload_module(void)
3559 /* First, take us out of the channel loop */
3560 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3562 pthread_cancel(chan_tid);
3566 del_timer(&socket_retry);
3568 unregister_fd(&wake_fd);
3569 close(wake_pipe[0]);
3570 close(wake_pipe[1]);
3572 // ast_mutex_unlock(&chan_lock);
3574 ast_channel_unregister(&lcr_tech);
3576 ast_unregister_application("lcr_config");
3578 if (lcr_sock >= 0) {
3583 #if ASTERISK_VERSION_NUM >= 100000
3584 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3589 int reload_module(void)
3595 #ifdef LCR_FOR_ASTERISK
3596 #define AST_MODULE "chan_lcr"
3599 #ifdef LCR_FOR_CALLWEAVER
3604 ast_mutex_lock(&usecnt_lock);
3606 ast_mutex_unlock(&usecnt_lock);
3611 #ifdef LCR_FOR_ASTERISK
3612 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3613 .load = load_module,
3614 .unload = unload_module,
3615 .reload = reload_module,
3619 #ifdef LCR_FOR_CALLWEAVER
3620 char *description(void)