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 no parameter.\n", opt);
522 CDEBUG(call, call->ast, "Option 'f' (faxdetect).\n");
525 if (opt[1] != '\0') {
526 CERROR(call, call->ast, "Option 'a' (asterisk DTMF) expects no parameter.\n", opt);
531 CDEBUG(call, call->ast, "Option 'a' (Asterisk DTMF detection).\n");
534 if (opt[1] != '\0') {
535 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
538 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
543 if (opt[1] != '\0') {
544 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
547 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
548 call->inband_dtmf = 1;
552 if (opt[1] != 'r' && opt[1] != 't') {
553 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
557 if (gain < -8 || gain >8) {
558 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
561 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
563 call->rx_gain = gain;
565 bchannel_gain(call->bchannel, call->rx_gain, 0);
567 call->tx_gain = gain;
569 bchannel_gain(call->bchannel, call->tx_gain, 1);
574 if (opt[1] != '\0') {
575 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
578 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
583 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
587 if (call->faxdetect || call->ast_dsp) {
589 call->dsp=ast_dsp_new();
591 #ifdef LCR_FOR_CALLWEAVER
592 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_CNG_DETECT : 0));
594 #ifdef LCR_FOR_ASTERISK
595 #ifdef DSP_FEATURE_DTMF_DETECT
596 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
598 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
603 #ifdef LCR_FOR_CALLWEAVER
604 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
606 #ifdef LCR_FOR_ASTERISK
607 #if ASTERISK_VERSION_NUM < 100000
608 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
610 // struct ast_format src;
611 // struct ast_format dst;
612 // ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
613 // ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
614 // call->trans=ast_translator_build_path(&dst, &src);
623 * send setup info to LCR
624 * this function is called, when asterisk call is received and ref is received
626 static void send_setup_to_lcr(struct chan_call *call)
628 union parameter newparam;
629 struct ast_channel *ast = call->ast;
632 if (!call->ast || !call->ref)
635 #ifdef AST_1_8_OR_HIGHER
636 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
638 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
641 /* send setup message to LCR */
642 memset(&newparam, 0, sizeof(union parameter));
643 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
644 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
646 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
648 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
649 if (!!strcmp(call->interface, "pbx"))
650 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
651 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
652 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
653 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
654 call->display[0] = '\0';
656 #ifdef AST_1_8_OR_HIGHER
657 /* set stored call information */
658 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
659 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
661 if (call->cid_num[0])
662 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
663 if (call->cid_name[0])
664 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
665 if (call->cid_rdnis[0]) {
666 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
667 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
668 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
670 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
671 case AST_PRES_RESTRICTED:
672 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
674 case AST_PRES_UNAVAILABLE:
675 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
677 case AST_PRES_ALLOWED:
679 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
681 switch(ast->cid.cid_ton) {
683 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
686 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
689 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
692 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
695 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
696 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
698 #if ASTERISK_VERSION_NUM < 110000
699 // ast->transfercapability = atoi(tmp);
700 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
702 // ast_channel_transfercapability_set(ast, atoi(tmp));
703 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
705 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
707 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
709 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
710 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
712 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
713 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
714 send_message(MESSAGE_SETUP, call->ref, &newparam);
715 if (call->tx_queue) {
716 memset(&newparam, 0, sizeof(union parameter));
717 newparam.queue = call->tx_queue * 8;
718 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
721 /* change to outgoing setup state */
722 call->state = CHAN_LCR_STATE_OUT_SETUP;
726 * send dialing info to LCR
727 * this function is called, when setup acknowledge is received and dialing
730 static void send_dialque_to_lcr(struct chan_call *call)
732 union parameter newparam;
734 if (!call->ast || !call->ref || !call->dialque[0])
737 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
739 /* send setup message to LCR */
740 memset(&newparam, 0, sizeof(union parameter));
742 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
744 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
745 call->dialque[0] = '\0';
746 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
750 * in case of a bridge, the unsupported message can be forwarded directly
751 * to the remote call.
753 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
757 if (!call->bridge_call) return;
758 CDEBUG(call, NULL, "Sending message due bridging.\n");
759 send_message(message_type, call->bridge_call->ref, param);
763 * send release message to LCR
765 static void send_release(struct chan_call *call, int cause, int location)
767 union parameter newparam;
769 /* sending release */
770 memset(&newparam, 0, sizeof(union parameter));
771 newparam.disconnectinfo.cause = cause;
772 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
773 send_message(MESSAGE_RELEASE, call->ref, &newparam);
777 * check if extension matches and start asterisk
778 * if it can match, proceed
781 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
784 union parameter newparam;
785 #if ASTERISK_VERSION_NUM < 110000
786 char *exten = ast->exten;
788 char s_exten[AST_MAX_EXTENSION];
791 strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
797 #if ASTERISK_VERSION_NUM < 110000
798 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
800 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
805 #if ASTERISK_VERSION_NUM < 110000
806 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
807 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
809 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
810 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
815 #if ASTERISK_VERSION_NUM < 110000
816 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
817 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);
819 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
820 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));
825 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
826 /* send setup acknowledge to lcr */
827 memset(&newparam, 0, sizeof(union parameter));
828 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
831 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
836 #if ASTERISK_VERSION_NUM < 110000
837 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
839 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
841 /* send setup acknowledge to lcr */
842 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
843 memset(&newparam, 0, sizeof(union parameter));
844 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
848 call->state = CHAN_LCR_STATE_IN_DIALING;
850 /* if match, start pbx */
851 #if ASTERISK_VERSION_NUM < 110000
852 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
854 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
856 CDEBUG(call, ast, "Extensions matches.\n");
860 /* send setup acknowledge to lcr */
861 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
862 memset(&newparam, 0, sizeof(union parameter));
863 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
867 call->state = CHAN_LCR_STATE_IN_DIALING;
870 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
874 #if ASTERISK_VERSION_NUM < 110000
877 if (!*ast_channel_exten(ast)) {
879 /* send setup acknowledge to lcr */
880 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
881 memset(&newparam, 0, sizeof(union parameter));
882 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
886 call->state = CHAN_LCR_STATE_IN_DIALING;
889 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
897 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
898 send_release(call, cause, LOCATION_PRIVATE_LOCAL);
900 /* release asterisk */
901 #if ASTERISK_VERSION_NUM < 110000
902 ast->hangupcause = call->cause;
904 ast_channel_hangupcause_set(ast, call->cause);
906 /* change to release state */
907 call->state = CHAN_LCR_STATE_RELEASE;
908 ast_hangup(ast); // call will be destroyed here
912 /* send setup to asterisk */
913 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
915 #ifdef LCR_FOR_CALLWEAVER
917 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
920 ret = ast_pbx_start(ast);
922 cause = (ret==-2)?34:27;
925 call->pbx_started = 1;
926 ast_setstate(ast, AST_STATE_RING);
930 * incoming setup from LCR
932 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
934 struct ast_channel *ast;
935 #ifdef AST_1_8_OR_HIGHER
936 struct ast_party_redirecting *ast_redir;
937 struct ast_party_caller *ast_caller;
939 struct ast_callerid *ast_caller;
941 #if ASTERISK_VERSION_NUM >= 110000
942 struct ast_party_redirecting s_ast_redir;
943 struct ast_party_caller s_ast_caller;
944 ast_party_redirecting_init(&s_ast_redir);
945 ast_party_caller_init(&s_ast_caller);
947 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
949 /* create asterisk channel instrance */
951 #ifdef LCR_FOR_CALLWEAVER
952 ast = ast_channel_alloc(1);
955 #ifdef LCR_FOR_ASTERISK
956 #ifdef AST_1_8_OR_HIGHER
957 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
959 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
963 #if ASTERISK_VERSION_NUM < 110000
964 #ifdef AST_1_8_OR_HIGHER
965 ast_redir = &ast->redirecting;
966 ast_caller = &ast->caller;
968 ast_caller = &ast->cid;
971 ast_redir = &s_ast_redir;
972 ast_caller = &s_ast_caller;
977 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
978 send_release(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
985 #if ASTERISK_VERSION_NUM < 110000
986 ast->tech_pvt = call;
987 ast->tech = &lcr_tech;
988 ast->fds[0] = call->pipe[0];
990 ast_channel_tech_pvt_set(ast, call);
991 ast_channel_tech_set(ast, &lcr_tech);
992 ast_channel_set_fd(ast, 0, call->pipe[0]);
995 /* fill setup information */
996 if (param->setup.dialinginfo.id)
997 #if ASTERISK_VERSION_NUM < 110000
998 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
999 if (param->setup.dialinginfo.context[0])
1000 strncpy(ast->context, param->setup.dialinginfo.context, AST_MAX_CONTEXT-1);
1002 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
1004 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
1005 if (param->setup.context[0])
1006 ast_channel_context_set(ast, param->setup.context);
1008 ast_channel_context_set(ast, param->setup.callerinfo.interface);
1012 #ifdef AST_1_8_OR_HIGHER
1013 if (param->setup.callerinfo.id[0]) {
1014 ast_caller->id.number.valid = 1;
1015 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
1016 if (!param->setup.callerinfo.id[0]) {
1017 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1018 ast_caller->id.number.plan = (0 << 4) | 1;
1020 switch (param->setup.callerinfo.present) {
1021 case INFO_PRESENT_ALLOWED:
1022 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
1024 case INFO_PRESENT_RESTRICTED:
1025 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1028 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1030 switch (param->setup.callerinfo.screen) {
1031 case INFO_SCREEN_USER:
1032 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1034 case INFO_SCREEN_USER_VERIFIED_PASSED:
1035 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1037 case INFO_SCREEN_USER_VERIFIED_FAILED:
1038 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1041 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1043 switch (param->setup.callerinfo.ntype) {
1044 case INFO_NTYPE_SUBSCRIBER:
1045 ast_caller->id.number.plan = (4 << 4) | 1;
1047 case INFO_NTYPE_NATIONAL:
1048 ast_caller->id.number.plan = (2 << 4) | 1;
1050 case INFO_NTYPE_INTERNATIONAL:
1051 ast_caller->id.number.plan = (1 << 4) | 1;
1054 ast_caller->id.number.plan = (0 << 4) | 1;
1057 if (param->setup.callerinfo.id2[0]) {
1058 ast_caller->ani.number.valid = 1;
1059 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1060 switch (param->setup.callerinfo.present2) {
1061 case INFO_PRESENT_ALLOWED:
1062 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1064 case INFO_PRESENT_RESTRICTED:
1065 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1068 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1070 switch (param->setup.callerinfo.screen2) {
1071 case INFO_SCREEN_USER:
1072 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1074 case INFO_SCREEN_USER_VERIFIED_PASSED:
1075 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1077 case INFO_SCREEN_USER_VERIFIED_FAILED:
1078 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1081 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1083 switch (param->setup.callerinfo.ntype2) {
1084 case INFO_NTYPE_SUBSCRIBER:
1085 ast_caller->ani.number.plan = (4 << 4) | 1;
1087 case INFO_NTYPE_NATIONAL:
1088 ast_caller->ani.number.plan = (2 << 4) | 1;
1090 case INFO_NTYPE_INTERNATIONAL:
1091 ast_caller->ani.number.plan = (1 << 4) | 1;
1094 ast_caller->ani.number.plan = (0 << 4) | 1;
1097 if (param->setup.callerinfo.name[0]) {
1098 ast_caller->id.name.valid = 1;
1099 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1101 #if ASTERISK_VERSION_NUM >= 110000
1102 ast_channel_caller_set(ast, ast_caller);
1104 if (param->setup.redirinfo.id[0]) {
1105 ast_redir->from.number.valid = 1;
1106 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1107 switch (param->setup.redirinfo.present) {
1108 case INFO_PRESENT_ALLOWED:
1109 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1111 case INFO_PRESENT_RESTRICTED:
1112 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1115 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1117 switch (param->setup.redirinfo.screen) {
1118 case INFO_SCREEN_USER:
1119 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1121 case INFO_SCREEN_USER_VERIFIED_PASSED:
1122 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1124 case INFO_SCREEN_USER_VERIFIED_FAILED:
1125 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1128 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1130 switch (param->setup.redirinfo.ntype) {
1131 case INFO_NTYPE_SUBSCRIBER:
1132 ast_redir->from.number.plan = (4 << 4) | 1;
1134 case INFO_NTYPE_NATIONAL:
1135 ast_redir->from.number.plan = (2 << 4) | 1;
1137 case INFO_NTYPE_INTERNATIONAL:
1138 ast_redir->from.number.plan = (1 << 4) | 1;
1141 ast_redir->from.number.plan = (0 << 4) | 1;
1143 #if ASTERISK_VERSION_NUM >= 110000
1144 ast_channel_redirecting_set(ast, ast_redir);
1148 memset(&ast->cid, 0, sizeof(ast->cid));
1149 if (param->setup.callerinfo.id[0])
1150 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1151 if (param->setup.callerinfo.id2[0])
1152 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1153 if (param->setup.callerinfo.name[0])
1154 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1155 if (param->setup.redirinfo.id[0])
1156 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1157 switch (param->setup.callerinfo.present) {
1158 case INFO_PRESENT_ALLOWED:
1159 ast->cid.cid_pres = AST_PRES_ALLOWED;
1161 case INFO_PRESENT_RESTRICTED:
1162 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1165 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1167 switch (param->setup.callerinfo.ntype) {
1168 case INFO_NTYPE_SUBSCRIBER:
1169 ast->cid.cid_ton = 4;
1171 case INFO_NTYPE_NATIONAL:
1172 ast->cid.cid_ton = 2;
1174 case INFO_NTYPE_INTERNATIONAL:
1175 ast->cid.cid_ton = 1;
1178 ast->cid.cid_ton = 0;
1182 #if ASTERISK_VERSION_NUM < 110000
1183 ast->transfercapability = param->setup.capainfo.bearer_capa;
1185 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1187 /* enable hdlc if transcap is data */
1188 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1190 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1192 /* configure channel */
1193 #if ASTERISK_VERSION_NUM < 100000
1194 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1195 ast->readformat = ast->rawreadformat = ast->nativeformats;
1196 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1198 #if ASTERISK_VERSION_NUM < 110000
1199 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1200 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1201 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1202 ast_set_write_format(ast, &ast->rawwriteformat);
1203 ast_set_read_format(ast, &ast->rawreadformat);
1205 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1206 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1207 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1208 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1209 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1212 #if ASTERISK_VERSION_NUM < 110000
1214 ast->hangupcause = 0;
1216 ast_channel_priority_set(ast, 1);
1217 ast_channel_hangupcause_set(ast, 0);
1221 call->state = CHAN_LCR_STATE_IN_SETUP;
1223 if (!call->pbx_started)
1224 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1228 * incoming setup acknowledge from LCR
1230 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1232 if (!call->ast) return;
1234 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1236 /* send pending digits in dialque */
1237 if (call->dialque[0])
1238 send_dialque_to_lcr(call);
1239 /* change to overlap state */
1240 call->state = CHAN_LCR_STATE_OUT_DIALING;
1244 * incoming proceeding from LCR
1246 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1248 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1251 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1252 /* queue event for asterisk */
1253 if (call->ast && call->pbx_started) {
1258 rc = write(wake_pipe[1], &byte, 1);
1260 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1266 * incoming alerting from LCR
1268 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1270 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1273 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1274 /* queue event to asterisk */
1275 if (call->ast && call->pbx_started) {
1280 rc = write(wake_pipe[1], &byte, 1);
1282 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1287 * incoming connect from LCR
1289 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1291 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1294 call->state = CHAN_LCR_STATE_CONNECT;
1295 /* copy connectinfo */
1296 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1297 /* queue event to asterisk */
1298 if (call->ast && call->pbx_started) {
1303 rc = write(wake_pipe[1], &byte, 1);
1305 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1310 * incoming disconnect from LCR
1312 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1314 struct ast_channel *ast = call->ast;
1316 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1319 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1321 call->cause = param->disconnectinfo.cause;
1322 call->location = param->disconnectinfo.location;
1323 /* if bridge, forward disconnect and return */
1326 if (call->bridge_call) {
1327 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1328 bridge_message_if_bridged(call, message_type, param);
1332 /* release lcr with same cause */
1333 send_release(call, call->cause, call->location);
1335 /* change to release state */
1336 call->state = CHAN_LCR_STATE_RELEASE;
1337 /* queue release asterisk */
1339 #if ASTERISK_VERSION_NUM < 110000
1340 ast->hangupcause = call->cause;
1342 ast_channel_hangupcause_set(ast, call->cause);
1344 if (call->pbx_started) {
1349 rc = write(wake_pipe[1], &byte, 1);
1351 strcpy(call->queue_string, "H"); // overwrite other indications
1353 ast_hangup(ast); // call will be destroyed here
1359 * incoming release from LCR
1361 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1363 struct ast_channel *ast = call->ast;
1365 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1369 /* change to release state */
1370 call->state = CHAN_LCR_STATE_RELEASE;
1371 /* copy release info */
1373 call->cause = param->disconnectinfo.cause;
1374 call->location = param->disconnectinfo.location;
1376 /* if we have an asterisk instance, queue hangup, else we are done */
1378 #if ASTERISK_VERSION_NUM < 110000
1379 ast->hangupcause = call->cause;
1381 ast_channel_hangupcause_set(ast, call->cause);
1383 if (call->pbx_started) {
1388 rc = write(wake_pipe[1], &byte, 1);
1390 strcpy(call->queue_string, "H");
1392 ast_hangup(ast); // call will be destroyed here
1401 * incoming information from LCR
1403 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1405 struct ast_channel *ast = call->ast;
1407 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1411 /* pbx not started */
1412 if (!call->pbx_started) {
1413 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1414 #if ASTERISK_VERSION_NUM < 110000
1415 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1417 ast_channel_exten_set(ast, param->information.id);
1419 lcr_start_pbx(call, ast, param->information.sending_complete);
1423 /* change dailing state after setup */
1424 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1425 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1426 call->state = CHAN_LCR_STATE_IN_DIALING;
1427 // ast_setstate(ast, AST_STATE_DIALING);
1431 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1436 rc = write(wake_pipe[1], &byte, 1);
1438 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1441 /* use bridge to forware message not supported by asterisk */
1442 if (call->state == CHAN_LCR_STATE_CONNECT) {
1443 if (call->bridge_call) {
1444 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1445 bridge_message_if_bridged(call, message_type, param);
1447 if (call->dsp_dtmf) {
1452 rc = write(wake_pipe[1], &byte, 1);
1454 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1456 CDEBUG(call, call->ast, "LCR's DTMF detection is disabled.\n");
1462 * incoming information from LCR
1464 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1466 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1468 if (!call->ast) return;
1470 /* use bridge to forware message not supported by asterisk */
1471 bridge_message_if_bridged(call, message_type, param);
1475 * incoming information from LCR
1477 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1479 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1481 if (!call->ast) return;
1483 /* use bridge to forware message not supported by asterisk */
1484 bridge_message_if_bridged(call, message_type, param);
1488 * incoming pattern from LCR
1490 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1492 union parameter newparam;
1494 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1496 if (!call->ast) return;
1498 /* pattern are indicated only once */
1499 if (call->has_pattern)
1501 call->has_pattern = 1;
1503 /* request bchannel */
1504 CDEBUG(call, call->ast, "Requesting audio path (ref=%d)\n", call->ref);
1505 memset(&newparam, 0, sizeof(union parameter));
1506 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
1508 /* queue PROGRESS, because tones are available */
1509 if (call->ast && call->pbx_started) {
1514 rc = write(wake_pipe[1], &byte, 1);
1516 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1521 * got dtmf from bchannel (locked state)
1523 void lcr_in_dtmf(struct chan_call *call, int val)
1525 struct ast_channel *ast = call->ast;
1530 if (!call->pbx_started)
1533 if (!call->dsp_dtmf) {
1534 CDEBUG(call, call->ast, "Recognised DTMF digit '%c' by LCR, but ignoring. (disabled by option)\n", val);
1538 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1545 rc = write(wake_pipe[1], &byte, 1);
1547 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1551 * message received from LCR
1553 int receive_message(int message_type, unsigned int ref, union parameter *param)
1555 struct chan_call *call;
1556 union parameter newparam;
1559 memset(&newparam, 0, sizeof(union parameter));
1561 /* handle new ref */
1562 if (message_type == MESSAGE_NEWREF) {
1563 if (param->newref.direction) {
1564 /* new ref from lcr */
1565 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1566 if (!ref || find_call_ref(ref)) {
1567 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1570 /* allocate new call instance */
1571 call = alloc_call();
1573 call->state = CHAN_LCR_STATE_IN_PREPARE;
1576 call->ref_was_assigned = 1;
1577 /* set dtmf (default, use option 'n' to disable */
1579 /* wait for setup (or release from asterisk) */
1581 /* new ref, as requested from this remote application */
1582 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1583 call = find_call_ref(0);
1585 /* send release, if ref does not exist */
1586 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1591 call->ref_was_assigned = 1;
1592 /* set dtmf (default, use option 'n' to disable */
1594 /* send pending setup info */
1595 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1596 send_setup_to_lcr(call);
1597 /* release if asterisk has signed off */
1598 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1601 send_release(call, call->cause, call->location);
1603 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1609 send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1615 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1618 call = find_call_ref(ref);
1620 /* ignore ref that is not used (anymore) */
1621 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1625 /* handle messages */
1626 switch(message_type) {
1628 lcr_in_setup(call, message_type, param);
1631 case MESSAGE_OVERLAP:
1632 lcr_in_overlap(call, message_type, param);
1635 case MESSAGE_PROCEEDING:
1636 lcr_in_proceeding(call, message_type, param);
1639 case MESSAGE_ALERTING:
1640 lcr_in_alerting(call, message_type, param);
1643 case MESSAGE_CONNECT:
1644 lcr_in_connect(call, message_type, param);
1647 case MESSAGE_DISCONNECT:
1648 lcr_in_disconnect(call, message_type, param);
1651 case MESSAGE_RELEASE:
1652 lcr_in_release(call, message_type, param);
1655 case MESSAGE_INFORMATION:
1656 lcr_in_information(call, message_type, param);
1659 case MESSAGE_NOTIFY:
1660 lcr_in_notify(call, message_type, param);
1663 case MESSAGE_FACILITY:
1664 lcr_in_facility(call, message_type, param);
1667 case MESSAGE_PATTERN: // audio available from LCR
1668 if (!call->has_pattern)
1669 lcr_in_pattern(call, message_type, param);
1672 case MESSAGE_NOPATTERN: // audio not available from LCR
1675 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1676 call->audiopath = param->audiopath;
1679 case MESSAGE_TRAFFIC: // if remote audio connected or hold
1681 unsigned char *p = param->traffic.data;
1682 int i, len = param->traffic.len;
1683 for (i = 0; i < len; i++, p++)
1686 rc = write(call->pipe[1], param->traffic.data, param->traffic.len);
1690 lcr_in_dtmf(call, param->dtmf);
1694 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1701 * release all calls (due to broken socket)
1703 static void release_all_calls(void)
1705 struct chan_call *call;
1710 /* no ast, so we may directly free call */
1712 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1716 /* already in release process */
1717 if (call->state == CHAN_LCR_STATE_RELEASE) {
1721 /* release or queue release */
1723 call->state = CHAN_LCR_STATE_RELEASE;
1724 if (!call->pbx_started) {
1725 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1726 ast_hangup(call->ast); // call will be destroyed here
1729 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1734 rc = write(wake_pipe[1], &byte, 1);
1736 strcpy(call->queue_string, "H");
1741 void close_socket(void);
1744 * warning! not thread safe
1745 * returns -1 for socket error, 0 for no work, 1 for work
1747 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1750 struct admin_list *admin;
1751 struct admin_message msg;
1753 if ((what & LCR_FD_READ)) {
1754 /* read from socket */
1755 len = read(lcr_sock, &msg, sizeof(msg));
1757 CERROR(NULL, NULL, "Socket closed.(read)\n");
1759 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1761 release_all_calls();
1762 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1766 if (len != sizeof(msg)) {
1767 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1770 if (msg.message != ADMIN_MESSAGE) {
1771 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1774 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1776 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1781 if ((what & LCR_FD_WRITE)) {
1782 /* write to socket */
1784 socket_fd.when &= ~LCR_FD_WRITE;
1787 admin = admin_first;
1788 len = write(lcr_sock, &admin->msg, sizeof(msg));
1790 CERROR(NULL, NULL, "Socket closed.(write)\n");
1794 if (len != sizeof(msg)) {
1795 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1799 admin_first = admin->next;
1803 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1812 * open and close socket and thread
1814 int open_socket(void)
1817 struct sockaddr_un sock_address;
1818 union parameter param;
1821 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1822 CERROR(NULL, NULL, "Failed to create socket.\n");
1826 /* set socket address and name */
1827 memset(&sock_address, 0, sizeof(sock_address));
1828 sock_address.sun_family = PF_UNIX;
1829 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1831 /* connect socket */
1832 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1835 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1839 /* register socket fd */
1840 memset(&socket_fd, 0, sizeof(socket_fd));
1841 socket_fd.fd = lcr_sock;
1842 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1844 /* enque hello message */
1845 memset(¶m, 0, sizeof(param));
1846 strcpy(param.hello.application, "asterisk");
1847 send_message(MESSAGE_HELLO, 0, ¶m);
1852 void close_socket(void)
1854 struct admin_list *admin, *temp;
1856 /* socket not created */
1860 unregister_fd(&socket_fd);
1862 /* flush pending messages */
1863 admin = admin_first;
1866 admin = admin->next;
1878 /* sending queue to asterisk */
1879 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1884 rc = read(wake_pipe[0], &byte, 1);
1891 static void handle_queue()
1893 struct chan_call *call;
1894 struct ast_channel *ast;
1895 struct ast_frame fr;
1901 p = call->queue_string;
1904 if (ast_channel_trylock(ast)) {
1905 ast_mutex_unlock(&chan_lock);
1907 ast_mutex_lock(&chan_lock);
1913 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1914 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1917 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1918 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1921 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1922 ast_queue_control(ast, AST_CONTROL_RINGING);
1923 ast_setstate(ast, AST_STATE_RINGING);
1926 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1927 ast_queue_control(ast, AST_CONTROL_ANSWER);
1930 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1931 ast_queue_hangup(ast);
1933 case '1': case '2': case '3': case 'A':
1934 case '4': case '5': case '6': case 'B':
1935 case '7': case '8': case '9': case 'C':
1936 case '*': case '0': case '#': case 'D':
1937 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1938 /* send digit to asterisk */
1939 memset(&fr, 0, sizeof(fr));
1941 #ifdef LCR_FOR_ASTERISK
1942 fr.frametype = AST_FRAME_DTMF_BEGIN;
1945 #ifdef LCR_FOR_CALLWEAVER
1946 fr.frametype = AST_FRAME_DTMF;
1949 #ifdef AST_1_8_OR_HIGHER
1950 fr.subclass.integer = *p;
1954 fr.delivery = ast_tv(0, 0);
1955 ast_queue_frame(ast, &fr);
1957 #ifdef LCR_FOR_ASTERISK
1958 fr.frametype = AST_FRAME_DTMF_END;
1959 ast_queue_frame(ast, &fr);
1964 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1968 call->queue_string[0] = '\0';
1969 ast_channel_unlock(ast);
1975 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1977 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1978 if (open_socket() < 0)
1979 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1984 void lock_chan(void)
1986 ast_mutex_lock(&chan_lock);
1989 void unlock_chan(void)
1991 ast_mutex_unlock(&chan_lock);
1994 /* chan_lcr thread */
1995 static void *chan_thread(void *arg)
1997 if (pipe(wake_pipe) < 0) {
1998 CERROR(NULL, NULL, "Failed to open pipe.\n");
2001 memset(&wake_fd, 0, sizeof(wake_fd));
2002 wake_fd.fd = wake_pipe[0];
2003 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2005 memset(&socket_retry, 0, sizeof(socket_retry));
2006 add_timer(&socket_retry, handle_retry, NULL, 0);
2008 /* open socket the first time */
2009 handle_retry(NULL, NULL, 0);
2011 ast_mutex_lock(&chan_lock);
2015 select_main(0, &global_change, lock_chan, unlock_chan);
2022 * new asterisk instance
2025 #ifdef AST_1_8_OR_HIGHER
2026 #if ASTERISK_VERSION_NUM < 100000
2027 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2029 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2032 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2035 char exten[256], *dial, *interface, *opt;
2036 struct ast_channel *ast;
2037 struct chan_call *call;
2038 #ifdef AST_1_8_OR_HIGHER
2039 const struct ast_party_redirecting *req_redir;
2040 const struct ast_party_caller *req_caller;
2043 ast_mutex_lock(&chan_lock);
2044 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2046 /* if socket is closed */
2048 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2049 ast_mutex_unlock(&chan_lock);
2053 /* create call instance */
2054 call = alloc_call();
2056 /* failed to create instance */
2057 ast_mutex_unlock(&chan_lock);
2061 /* create asterisk channel instrance */
2063 #ifdef LCR_FOR_ASTERISK
2064 #ifdef AST_1_8_OR_HIGHER
2065 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2067 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2071 #ifdef LCR_FOR_CALLWEAVER
2072 ast = ast_channel_alloc(1);
2076 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2078 /* failed to create instance */
2079 ast_mutex_unlock(&chan_lock);
2082 #if ASTERISK_VERSION_NUM < 110000
2083 ast->tech = &lcr_tech;
2084 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2085 #ifdef AST_1_8_OR_HIGHER
2086 req_redir = &requestor->redirecting;
2087 req_caller = &requestor->caller;
2090 ast_channel_tech_set(ast, &lcr_tech);
2091 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2092 req_redir = ast_channel_redirecting(requestor);
2093 req_caller = ast_channel_caller(requestor);
2095 /* configure channel */
2096 #if ASTERISK_VERSION_NUM < 100000
2097 #if ASTERISK_VERSION_NUM < 110000
2098 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2099 ast->readformat = ast->rawreadformat = ast->nativeformats;
2100 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2102 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2103 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2104 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2107 #if ASTERISK_VERSION_NUM < 110000
2108 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2109 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2110 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2111 ast_set_write_format(ast, &ast->rawwriteformat);
2112 ast_set_read_format(ast, &ast->rawreadformat);
2114 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2115 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2116 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2117 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2118 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2121 #if ASTERISK_VERSION_NUM < 110000
2123 ast->hangupcause = 0;
2125 ast_channel_priority_set(ast, 1);
2126 ast_channel_hangupcause_set(ast, 0);
2131 #if ASTERISK_VERSION_NUM < 110000
2132 ast->tech_pvt = call;
2133 ast->fds[0] = call->pipe[0];
2135 ast_channel_tech_pvt_set(ast, call);
2136 ast_channel_set_fd(ast, 0, call->pipe[0]);
2138 call->pbx_started = 0;
2140 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2143 * Extract interface, dialstring, options from data.
2146 * <interface>/<dialstring>
2147 * <interface>/<dialstring>/options
2149 strncpy(exten, (char *)data, sizeof(exten)-1);
2150 exten[sizeof(exten)-1] = '\0';
2151 if ((dial = strchr(exten, '/'))) {
2154 if ((opt = strchr(dial, '/')))
2163 strncpy(call->interface, interface, sizeof(call->interface)-1);
2164 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2165 apply_opt(call, (char *)opt);
2167 #ifdef AST_1_8_OR_HIGHER
2168 // clone_variables(requestor, ast);
2171 ast->caller.ani.number.valid= req_caller->ani.number.valid;
2172 if (req_caller->ani.number.valid)
2173 if (req_caller->ani.number.str)
2174 if (req_caller->ani.number.str[0])
2175 ast->caller.ani.number.str= strdup(req_caller->ani.number.str);
2176 ast->caller.ani.number.plan= req_caller->ani.number.plan;
2177 ast->caller.ani.number.presentation= req_caller->ani.number.presentation;
2179 ast->caller.ani.name.valid= req_caller->ani.name.valid;
2180 if (req_caller->ani.name.valid)
2181 if (req_caller->ani.name.str)
2182 if (req_caller->ani.name.str[0])
2183 ast->caller.ani.name.str= strdup(req_caller->ani.name.str);
2184 ast->caller.ani.name.presentation= req_caller->ani.name.presentation;
2186 ast->caller.ani.subaddress.valid= req_caller->ani.subaddress.valid;
2187 if (req_caller->ani.subaddress.valid)
2188 if (req_caller->ani.subaddress.str)
2189 if (req_caller->ani.subaddress.str[0])
2190 ast->caller.ani.subaddress.str= strdup(req_caller->ani.subaddress.str);
2191 ast->caller.ani.subaddress.type= req_caller->ani.subaddress.type;
2193 ast->caller.id.number.valid= req_caller->id.number.valid;
2194 if (req_caller->id.number.valid)
2195 if (req_caller->id.number.str)
2196 if (req_caller->id.number.str[0])
2197 ast->caller.id.number.str= strdup(req_caller->id.number.str);
2198 ast->caller.id.number.plan= req_caller->id.number.plan;
2199 ast->caller.id.number.presentation= req_caller->id.number.presentation;
2201 ast->caller.id.name.valid= req_caller->id.name.valid;
2202 if (req_caller->id.name.valid)
2203 if (req_caller->id.name.str)
2204 if (req_caller->id.name.str[0])
2205 ast->caller.id.name.str= strdup(req_caller->id.name.str);
2206 ast->caller.id.name.presentation= req_caller->id.name.presentation;
2208 ast->caller.id.subaddress.valid= req_caller->id.subaddress.valid;
2209 if (req_caller->id.subaddress.valid)
2210 if (req_caller->id.subaddress.str)
2211 if (req_caller->id.subaddress.str[0])
2212 ast->caller.id.subaddress.str= strdup(req_caller->id.subaddress.str);
2213 ast->caller.id.subaddress.type= req_caller->id.subaddress.type;
2215 if (requestor->dialed.number.str)
2216 if (requestor->dialed.number.str[0])
2217 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2218 ast->dialed.number.plan= requestor->dialed.number.plan;
2220 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2221 if (requestor->dialed.subaddress.valid)
2222 if (requestor->dialed.subaddress.str)
2223 if (requestor->dialed.subaddress.str[0])
2224 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2225 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2227 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2228 ast->redirecting.count= req_redir->count;
2229 ast->redirecting.reason= req_redir->reason;
2231 ast->redirecting.from.number.valid= req_redir->from.number.valid;
2232 if (req_redir->from.number.valid)
2233 if (req_redir->from.number.str)
2234 if (req_redir->from.number.str[0])
2235 ast->redirecting.from.number.str= strdup(req_redir->from.number.str);
2236 ast->redirecting.from.number.plan= req_redir->from.number.plan;
2237 ast->redirecting.from.number.presentation= req_redir->from.number.presentation;
2239 ast->redirecting.to.number.valid= req_redir->to.number.valid;
2240 if (req_redir->to.number.valid)
2241 if (req_redir->to.number.str)
2242 if (req_redir->to.number.str[0])
2243 ast->redirecting.to.number.str= strdup(req_redir->to.number.str);
2244 ast->redirecting.to.number.plan= req_redir->to.number.plan;
2245 ast->redirecting.to.number.presentation= req_redir->to.number.presentation;
2247 /* store call information for setup */
2250 if (requestor && req_caller->id.number.valid) {
2251 if (req_caller->id.number.str)
2252 strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2253 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2254 case AST_PRES_RESTRICTED:
2255 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2257 case AST_PRES_UNAVAILABLE:
2258 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2260 case AST_PRES_ALLOWED:
2262 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2264 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2265 case AST_PRES_USER_NUMBER_UNSCREENED:
2266 call->callerinfo.screen = INFO_SCREEN_USER;
2268 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2269 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2271 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2272 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2275 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2277 switch((req_caller->id.number.plan >> 4) & 7) {
2279 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2282 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2285 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2288 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2291 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2294 if (requestor && req_caller->ani.number.valid) {
2295 if (req_caller->ani.number.str)
2296 strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2297 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2298 case AST_PRES_RESTRICTED:
2299 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2301 case AST_PRES_UNAVAILABLE:
2302 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2304 case AST_PRES_ALLOWED:
2306 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2308 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2309 case AST_PRES_USER_NUMBER_UNSCREENED:
2310 call->callerinfo.screen2 = INFO_SCREEN_USER;
2312 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2313 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2315 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2316 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2319 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2321 switch((req_caller->ani.number.plan >> 4) & 7) {
2323 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2326 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2329 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2332 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2335 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2338 if (requestor && req_caller->id.name.valid) {
2339 if (req_caller->id.name.str)
2340 strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2344 if (requestor && req_redir->from.number.valid) {
2345 call->redirinfo.itype = INFO_ITYPE_CHAN;
2346 if (req_redir->from.number.str)
2347 strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2348 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2349 case AST_PRES_RESTRICTED:
2350 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2352 case AST_PRES_UNAVAILABLE:
2353 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2355 case AST_PRES_ALLOWED:
2357 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2359 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2360 case AST_PRES_USER_NUMBER_UNSCREENED:
2361 call->redirinfo.screen = INFO_SCREEN_USER;
2363 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2364 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2366 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2367 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2370 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2372 switch((req_redir->from.number.plan >> 4) & 7) {
2374 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2377 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2380 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2383 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2388 ast_mutex_unlock(&chan_lock);
2393 * call from asterisk
2395 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2397 union parameter newparam;
2398 struct chan_call *call;
2399 #if ASTERISK_VERSION_NUM >= 110000
2400 int transfercapability;
2403 ast_mutex_lock(&chan_lock);
2404 #if ASTERISK_VERSION_NUM < 110000
2405 call = ast->tech_pvt;
2407 call = ast_channel_tech_pvt(ast);
2410 #ifdef LCR_FOR_CALLWEAVER
2412 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2416 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2417 ast_mutex_unlock(&chan_lock);
2421 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2423 /* pbx process is started */
2424 call->pbx_started = 1;
2425 /* send MESSAGE_NEWREF */
2426 memset(&newparam, 0, sizeof(union parameter));
2427 newparam.newref.direction = 0; /* request from app */
2428 send_message(MESSAGE_NEWREF, 0, &newparam);
2430 /* set hdlc if capability requires hdlc */
2431 #if ASTERISK_VERSION_NUM < 110000
2432 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2433 || ast->transfercapability == INFO_BC_DATARESTRICTED
2434 || ast->transfercapability == INFO_BC_VIDEO)
2436 transfercapability=ast_channel_transfercapability(ast);
2437 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2438 || transfercapability == INFO_BC_DATARESTRICTED
2439 || transfercapability == INFO_BC_VIDEO)
2442 /* if hdlc is forced by option, we change transcap to data */
2444 #if ASTERISK_VERSION_NUM < 110000
2445 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2446 && ast->transfercapability != INFO_BC_DATARESTRICTED
2447 && ast->transfercapability != INFO_BC_VIDEO)
2448 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2450 && transfercapability != INFO_BC_DATAUNRESTRICTED
2451 && transfercapability != INFO_BC_DATARESTRICTED
2452 && transfercapability != INFO_BC_VIDEO)
2453 transfercapability = INFO_BC_DATAUNRESTRICTED;
2456 #ifndef AST_1_8_OR_HIGHER
2457 call->cid_num[0] = 0;
2458 call->cid_name[0] = 0;
2459 call->cid_rdnis[0] = 0;
2461 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2462 strncpy(call->cid_num, ast->cid.cid_num,
2463 sizeof(call->cid_num)-1);
2464 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2465 strncpy(call->cid_name, ast->cid.cid_name,
2466 sizeof(call->cid_name)-1);
2467 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2468 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2469 sizeof(call->cid_rdnis)-1);
2472 ast_mutex_unlock(&chan_lock);
2476 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2478 static const char* dtmf_tones[] = {
2479 "!941+1336/100,!0/100", /* 0 */
2480 "!697+1209/100,!0/100", /* 1 */
2481 "!697+1336/100,!0/100", /* 2 */
2482 "!697+1477/100,!0/100", /* 3 */
2483 "!770+1209/100,!0/100", /* 4 */
2484 "!770+1336/100,!0/100", /* 5 */
2485 "!770+1477/100,!0/100", /* 6 */
2486 "!852+1209/100,!0/100", /* 7 */
2487 "!852+1336/100,!0/100", /* 8 */
2488 "!852+1477/100,!0/100", /* 9 */
2489 "!697+1633/100,!0/100", /* A */
2490 "!770+1633/100,!0/100", /* B */
2491 "!852+1633/100,!0/100", /* C */
2492 "!941+1633/100,!0/100", /* D */
2493 "!941+1209/100,!0/100", /* * */
2494 "!941+1477/100,!0/100" }; /* # */
2496 if (digit >= '0' && digit <='9')
2497 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2498 else if (digit >= 'A' && digit <= 'D')
2499 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2500 else if (digit == '*')
2501 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2502 else if (digit == '#')
2503 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2505 #if ASTERISK_VERSION_NUM < 110000
2506 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2508 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2513 #ifdef LCR_FOR_ASTERISK
2514 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2516 #ifdef LCR_FOR_CALLWEAVER
2517 static int lcr_digit(struct ast_channel *ast, char digit)
2520 struct chan_call *call;
2521 union parameter newparam;
2524 #ifdef LCR_FOR_CALLWEAVER
2525 int inband_dtmf = 0;
2528 /* only pass IA5 number space */
2529 if (digit > 126 || digit < 32)
2532 ast_mutex_lock(&chan_lock);
2533 #if ASTERISK_VERSION_NUM < 110000
2534 call = ast->tech_pvt;
2536 call = ast_channel_tech_pvt(ast);
2539 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2540 ast_mutex_unlock(&chan_lock);
2544 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2546 /* send information or queue them */
2547 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2548 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2549 memset(&newparam, 0, sizeof(union parameter));
2551 newparam.information.keypad[0] = digit;
2552 newparam.information.keypad[1] = '\0';
2554 newparam.information.id[0] = digit;
2555 newparam.information.id[1] = '\0';
2557 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2560 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2561 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2563 strncat(call->dialque, buf, strlen(call->dialque)-1);
2566 ast_mutex_unlock(&chan_lock);
2568 #ifdef LCR_FOR_ASTERISK
2572 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2574 int inband_dtmf = 0;
2575 struct chan_call *call;
2578 ast_mutex_lock(&chan_lock);
2580 #if ASTERISK_VERSION_NUM < 110000
2581 call = ast->tech_pvt;
2583 call = ast_channel_tech_pvt(ast);
2588 "Received digit from Asterisk, "
2589 "but no call instance exists.\n");
2590 ast_mutex_unlock(&chan_lock);
2594 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2596 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2600 ast_mutex_unlock(&chan_lock);
2603 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2604 send_digit_to_chan(ast, digit);
2610 static int lcr_answer(struct ast_channel *ast)
2612 union parameter newparam;
2613 struct chan_call *call;
2615 ast_mutex_lock(&chan_lock);
2616 #if ASTERISK_VERSION_NUM < 110000
2617 call = ast->tech_pvt;
2619 call = ast_channel_tech_pvt(ast);
2622 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2623 ast_mutex_unlock(&chan_lock);
2627 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2629 /* copy connectinfo, if bridged */
2630 if (call->bridge_call)
2631 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2632 /* send connect message to lcr */
2633 if (call->state != CHAN_LCR_STATE_CONNECT) {
2634 memset(&newparam, 0, sizeof(union parameter));
2635 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2636 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2637 call->state = CHAN_LCR_STATE_CONNECT;
2641 // memset(&newparam, 0, sizeof(union parameter));
2642 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2644 ast_mutex_unlock(&chan_lock);
2648 static int lcr_hangup(struct ast_channel *ast)
2650 struct chan_call *call;
2651 pthread_t tid = pthread_self();
2653 if (!pthread_equal(tid, chan_tid)) {
2654 ast_mutex_lock(&chan_lock);
2656 #if ASTERISK_VERSION_NUM < 110000
2657 call = ast->tech_pvt;
2659 call = ast_channel_tech_pvt(ast);
2662 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2663 if (!pthread_equal(tid, chan_tid)) {
2664 ast_mutex_unlock(&chan_lock);
2669 if (!pthread_equal(tid, chan_tid))
2670 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2672 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2674 /* disconnect asterisk, maybe not required */
2675 #if ASTERISK_VERSION_NUM < 110000
2676 ast->tech_pvt = NULL;
2679 ast_channel_tech_pvt_set(ast, NULL);
2680 ast_channel_set_fd(ast, 0, -1);
2684 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2685 #if ASTERISK_VERSION_NUM < 110000
2686 if (ast->hangupcause > 0)
2687 send_release(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2689 if (ast_channel_hangupcause(ast) > 0)
2690 send_release(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2693 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2696 if (!pthread_equal(tid, chan_tid)) {
2697 ast_mutex_unlock(&chan_lock);
2701 /* ref is not set, due to prepare setup or release */
2702 if (call->state == CHAN_LCR_STATE_RELEASE) {
2703 /* we get the response to our release */
2704 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2707 /* during prepare, we change to release state */
2708 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2709 call->state = CHAN_LCR_STATE_RELEASE;
2713 if (!pthread_equal(tid, chan_tid)) {
2714 ast_mutex_unlock(&chan_lock);
2719 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2721 union parameter newparam;
2722 struct chan_call *call;
2723 struct ast_frame * f = fr;
2724 unsigned char *p, *q;
2727 #if ASTERISK_VERSION_NUM < 100000
2728 #ifdef AST_1_8_OR_HIGHER
2729 if (!f->subclass.codec)
2733 CDEBUG(NULL, ast, "No subclass\n");
2735 #ifdef AST_1_8_OR_HIGHER
2736 #if ASTERISK_VERSION_NUM < 100000
2737 #if ASTERISK_VERSION_NUM < 110000
2738 if (!(f->subclass.codec & ast->nativeformats)) {
2740 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2743 #if ASTERISK_VERSION_NUM < 110000
2744 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2746 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2750 #if ASTERISK_VERSION_NUM < 110000
2751 if (!(f->subclass & ast->nativeformats)) {
2753 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2757 "Unexpected format. "
2758 "Activating emergency conversion...\n");
2760 #ifdef AST_1_8_OR_HIGHER
2761 #if ASTERISK_VERSION_NUM < 100000
2762 ast_set_write_format(ast, f->subclass.codec);
2764 ast_set_write_format(ast, &f->subclass.format);
2767 ast_set_write_format(ast, f->subclass);
2769 #if ASTERISK_VERSION_NUM < 110000
2770 f = (ast->writetrans) ? ast_translate(
2771 ast->writetrans, fr, 0) : fr;
2773 f = (ast_channel_writetrans(ast)) ? ast_translate(
2774 ast_channel_writetrans(ast), fr, 0) : fr;
2778 ast_mutex_lock(&chan_lock);
2779 #if ASTERISK_VERSION_NUM < 110000
2780 call = ast->tech_pvt;
2782 call = ast_channel_tech_pvt(ast);
2784 if (!call || !call->ref) {
2785 /* drop the frame, if no ref exists, but return successfull delivery, or asterisk will abort connection */
2786 ast_mutex_unlock(&chan_lock);
2793 p = *((unsigned char **)&(f->data));
2794 q = newparam.traffic.data;
2795 memset(&newparam, 0, sizeof(union parameter));
2797 l = (len > sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
2798 newparam.traffic.len = l;
2801 *q++ = flip_bits[*p++];
2802 send_message(MESSAGE_TRAFFIC, call->ref, &newparam);
2804 ast_mutex_unlock(&chan_lock);
2812 static struct ast_frame *lcr_read(struct ast_channel *ast)
2814 struct chan_call *call;
2816 struct ast_frame *f = NULL;
2818 ast_mutex_lock(&chan_lock);
2819 #if ASTERISK_VERSION_NUM < 110000
2820 call = ast->tech_pvt;
2822 call = ast_channel_tech_pvt(ast);
2825 ast_mutex_unlock(&chan_lock);
2828 if (call->pipe[0] > -1) {
2829 if (call->rebuffer && !call->hdlc) {
2830 /* Make sure we have a complete 20ms (160byte) frame */
2831 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2833 call->framepos += len;
2836 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2838 if (len < 0 && errno == EAGAIN) {
2839 ast_mutex_unlock(&chan_lock);
2841 #ifdef LCR_FOR_ASTERISK
2842 return &ast_null_frame;
2845 #ifdef LCR_FOR_CALLWEAVER
2851 close(call->pipe[0]);
2854 ast_mutex_unlock(&chan_lock);
2856 } else if (call->rebuffer && call->framepos < 160) {
2857 /* Not a complete frame, so we send a null-frame */
2858 ast_mutex_unlock(&chan_lock);
2859 return &ast_null_frame;
2863 call->read_fr.frametype = AST_FRAME_VOICE;
2864 #ifdef AST_1_8_OR_HIGHER
2865 #if ASTERISK_VERSION_NUM < 100000
2866 #if ASTERISK_VERSION_NUM < 110000
2867 call->read_fr.subclass.codec = ast->nativeformats;
2869 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2872 #if ASTERISK_VERSION_NUM < 110000
2873 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2875 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2877 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2880 #if ASTERISK_VERSION_NUM < 110000
2881 call->read_fr.subclass = ast->nativeformats;
2883 call->read_fr.subclass = ast_channel_nativeformats(ast);
2886 if (call->rebuffer) {
2887 call->read_fr.datalen = call->framepos;
2888 call->read_fr.samples = call->framepos;
2891 call->read_fr.datalen = len;
2892 call->read_fr.samples = len;
2894 call->read_fr.delivery = ast_tv(0,0);
2895 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2898 f = ast_dsp_process(ast, call->dsp, &call->read_fr);
2899 if (f && f->frametype == AST_FRAME_DTMF)
2900 CDEBUG(call, ast, "Asterisk detected inband DTMF: %c.\n", f->subclass.integer);
2902 ast_mutex_unlock(&chan_lock);
2904 if (f && f->frametype == AST_FRAME_DTMF)
2907 return &call->read_fr;
2910 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2912 union parameter newparam;
2914 struct chan_call *call;
2915 const struct ast_tone_zone_sound *ts = NULL;
2917 ast_mutex_lock(&chan_lock);
2918 #if ASTERISK_VERSION_NUM < 110000
2919 call = ast->tech_pvt;
2921 call = ast_channel_tech_pvt(ast);
2924 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2925 ast_mutex_unlock(&chan_lock);
2930 case AST_CONTROL_BUSY:
2931 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2932 ast_setstate(ast, AST_STATE_BUSY);
2933 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2934 /* send message to lcr */
2935 memset(&newparam, 0, sizeof(union parameter));
2936 newparam.disconnectinfo.cause = 17;
2937 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2938 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2940 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2942 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2943 #if ASTERISK_VERSION_NUM < 110000
2944 ts = ast_get_indication_tone(ast->zone, "busy");
2946 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2950 case AST_CONTROL_CONGESTION:
2951 #if ASTERISK_VERSION_NUM < 110000
2952 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2954 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2956 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2957 /* send message to lcr */
2958 memset(&newparam, 0, sizeof(union parameter));
2959 #if ASTERISK_VERSION_NUM < 110000
2960 newparam.disconnectinfo.cause = ast->hangupcause;
2962 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
2964 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2965 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2967 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2969 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2970 #if ASTERISK_VERSION_NUM < 110000
2971 ts = ast_get_indication_tone(ast->zone, "congestion");
2973 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
2977 case AST_CONTROL_PROCEEDING:
2978 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2979 if (call->state == CHAN_LCR_STATE_IN_SETUP
2980 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2981 /* send message to lcr */
2982 memset(&newparam, 0, sizeof(union parameter));
2983 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2985 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2988 case AST_CONTROL_RINGING:
2989 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2990 ast_setstate(ast, AST_STATE_RING);
2991 if (call->state == CHAN_LCR_STATE_IN_SETUP
2992 || call->state == CHAN_LCR_STATE_IN_DIALING
2993 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2994 /* send message to lcr */
2995 memset(&newparam, 0, sizeof(union parameter));
2996 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2998 call->state = CHAN_LCR_STATE_IN_ALERTING;
3000 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
3001 #if ASTERISK_VERSION_NUM < 110000
3002 ts = ast_get_indication_tone(ast->zone, "ring");
3004 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
3008 case AST_CONTROL_PROGRESS:
3009 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
3010 /* request bchannel */
3011 CDEBUG(call, ast, "Requesting audio path.\n");
3012 memset(&newparam, 0, sizeof(union parameter));
3013 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
3016 CDEBUG(call, ast, "Received indicate -1.\n");
3017 ast_playtones_stop(ast);
3021 case AST_CONTROL_VIDUPDATE:
3022 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
3025 case AST_CONTROL_HOLD:
3026 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
3027 /* send message to lcr */
3028 memset(&newparam, 0, sizeof(union parameter));
3029 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
3030 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3032 /*start music onhold*/
3033 #ifdef LCR_FOR_ASTERISK
3034 #if ASTERISK_VERSION_NUM <110000
3035 ast_moh_start(ast,data,ast->musicclass);
3037 ast_moh_start(ast,data,ast_channel_musicclass(ast));
3041 #ifdef LCR_FOR_CALLWEAVER
3042 ast_moh_start(ast, NULL);
3047 case AST_CONTROL_UNHOLD:
3048 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3049 /* send message to lcr */
3050 memset(&newparam, 0, sizeof(union parameter));
3051 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3052 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3058 #ifdef AST_CONTROL_SRCUPDATE
3059 case AST_CONTROL_SRCUPDATE:
3063 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3066 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3071 if (ts && ts->data[0]) {
3072 ast_playtones_start(ast, 0, ts->data, 1);
3076 ast_mutex_unlock(&chan_lock);
3083 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3085 struct chan_call *call;
3091 ast_mutex_lock(&chan_lock);
3092 #if ASTERISK_VERSION_NUM < 110000
3093 call = ast->tech_pvt;
3095 call = ast_channel_tech_pvt(ast);
3098 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3099 ast_mutex_unlock(&chan_lock);
3103 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3105 ast_mutex_unlock(&chan_lock);
3110 * send_text asterisk
3112 static int lcr_send_text(struct ast_channel *ast, const char *text)
3114 struct chan_call *call;
3115 union parameter newparam;
3117 ast_mutex_lock(&chan_lock);
3118 #if ASTERISK_VERSION_NUM < 110000
3119 call = ast->tech_pvt;
3121 call = ast_channel_tech_pvt(ast);
3124 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3125 ast_mutex_unlock(&chan_lock);
3129 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3130 memset(&newparam, 0, sizeof(union parameter));
3131 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3132 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3133 ast_mutex_unlock(&chan_lock);
3140 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3141 struct ast_channel *ast2, int flags,
3142 struct ast_frame **fo,
3143 struct ast_channel **rc, int timeoutms)
3146 struct chan_call *call1, *call2;
3147 struct ast_channel *carr[2], *who;
3149 struct ast_frame *f;
3152 /* bridge is disabled, because there is currerntly no concept to bridge mISDN channels */
3153 return AST_BRIDGE_FAILED;
3155 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3160 /* join via dsp (if the channels are currently open) */
3161 ast_mutex_lock(&chan_lock);
3162 #if ASTERISK_VERSION_NUM < 110000
3163 call1 = ast1->tech_pvt;
3164 call2 = ast2->tech_pvt;
3166 call1 = ast_channel_tech_pvt(ast1);
3167 call2 = ast_channel_tech_pvt(ast2);
3169 if (!call1 || !call2) {
3170 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3171 ast_mutex_unlock(&chan_lock);
3172 return AST_BRIDGE_COMPLETE;
3175 /* join, if both call instances uses dsp
3176 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3178 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3180 /* get bridge id and join */
3181 bridge_id = new_bridge_id();
3183 call1->bridge_id = bridge_id;
3184 call2->bridge_id = bridge_id;
3185 // FIXME: do bridiging
3186 // bchannel_join(call1->bchannel, bridge_id);
3187 // bchannel_join(call2->bchannel, bridge_id);
3189 call1->bridge_call = call2;
3190 call2->bridge_call = call1;
3192 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3193 || call1->state == CHAN_LCR_STATE_IN_DIALING
3194 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3195 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3196 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3199 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3200 || call2->state == CHAN_LCR_STATE_IN_DIALING
3201 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3202 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3203 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3207 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3208 so let's do it for them. Hmpf.
3211 if (call1->on_hold) {
3212 union parameter newparam;
3214 memset(&newparam, 0, sizeof(union parameter));
3215 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3216 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3221 if (call2->on_hold) {
3222 union parameter newparam;
3224 memset(&newparam, 0, sizeof(union parameter));
3225 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3226 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3231 ast_mutex_unlock(&chan_lock);
3235 who = ast_waitfor_n(carr, 2, &to);
3238 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3243 if (!f || f->frametype == AST_FRAME_CONTROL) {
3245 CDEBUG(NULL, NULL, "Got hangup.\n");
3247 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3254 if ( f->frametype == AST_FRAME_DTMF ) {
3255 CDEBUG(NULL, NULL, "Got DTMF.\n");
3271 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3273 /* split channels */
3274 ast_mutex_lock(&chan_lock);
3275 #if ASTERISK_VERSION_NUM < 110000
3276 call1 = ast1->tech_pvt;
3277 call2 = ast2->tech_pvt;
3279 call1 = ast_channel_tech_pvt(ast1);
3280 call2 = ast_channel_tech_pvt(ast2);
3282 if (call1 && call1->bridge_id) {
3283 call1->bridge_id = 0;
3284 if (call1->bridge_call)
3285 call1->bridge_call->bridge_call = NULL;
3287 if (call2 && call1->bridge_id) {
3288 call2->bridge_id = 0;
3289 if (call2->bridge_call)
3290 call2->bridge_call->bridge_call = NULL;
3292 call1->bridge_call = NULL;
3293 call2->bridge_call = NULL;
3295 ast_mutex_unlock(&chan_lock);
3296 return AST_BRIDGE_COMPLETE;
3298 static struct ast_channel_tech lcr_tech = {
3300 .description = "Channel driver for connecting to Linux-Call-Router",
3301 #if ASTERISK_VERSION_NUM < 100000
3302 .capabilities = AST_FORMAT_ALAW,
3304 .requester = lcr_request,
3306 #ifdef LCR_FOR_ASTERISK
3307 .send_digit_begin = lcr_digit_begin,
3308 .send_digit_end = lcr_digit_end,
3311 #ifdef LCR_FOR_CALLWEAVER
3312 .send_digit = lcr_digit,
3316 .bridge = lcr_bridge,
3317 .hangup = lcr_hangup,
3318 .answer = lcr_answer,
3321 .indicate = lcr_indicate,
3323 .send_text = lcr_send_text,
3332 static int lcr_show_lcr (int fd, int argc, char *argv[])
3337 static int lcr_show_calls (int fd, int argc, char *argv[])
3342 static int lcr_reload_routing (int fd, int argc, char *argv[])
3347 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3352 static int lcr_port_block (int fd, int argc, char *argv[])
3357 static int lcr_port_unblock (int fd, int argc, char *argv[])
3362 static int lcr_port_unload (int fd, int argc, char *argv[])
3367 static struct ast_cli_entry cli_show_lcr =
3368 { {"lcr", "show", "lcr", NULL},
3370 "Shows current states of LCR core",
3371 "Usage: lcr show lcr\n",
3374 static struct ast_cli_entry cli_show_calls =
3375 { {"lcr", "show", "calls", NULL},
3377 "Shows current calls made by LCR and Asterisk",
3378 "Usage: lcr show calls\n",
3381 static struct ast_cli_entry cli_reload_routing =
3382 { {"lcr", "reload", "routing", NULL},
3384 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3385 "Usage: lcr reload routing\n",
3388 static struct ast_cli_entry cli_reload_interfaces =
3389 { {"lcr", "reload", "interfaces", NULL},
3390 lcr_reload_interfaces,
3391 "Reloads interfaces conf of LCR",
3392 "Usage: lcr reload interfaces\n",
3395 static struct ast_cli_entry cli_port_block =
3396 { {"lcr", "port", "block", NULL},
3398 "Blocks LCR port for further calls",
3399 "Usage: lcr port block \"<port>\"\n",
3402 static struct ast_cli_entry cli_port_unblock =
3403 { {"lcr", "port", "unblock", NULL},
3405 "Unblocks or loads LCR port, port is opened my mISDN",
3406 "Usage: lcr port unblock \"<port>\"\n",
3409 static struct ast_cli_entry cli_port_unload =
3410 { {"lcr", "port", "unload", NULL},
3412 "Unloads LCR port, port is closes by mISDN",
3413 "Usage: lcr port unload \"<port>\"\n",
3418 #ifdef LCR_FOR_ASTERISK
3419 #ifdef AST_1_8_OR_HIGHER
3420 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3422 static int lcr_config_exec(struct ast_channel *ast, void *data)
3426 #ifdef LCR_FOR_CALLWEAVER
3427 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3430 struct chan_call *call;
3432 ast_mutex_lock(&chan_lock);
3434 #ifdef LCR_FOR_ASTERISK
3435 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3438 #ifdef LCR_FOR_CALLWEAVER
3439 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3445 if (call->ast == ast)
3451 #ifdef LCR_FOR_ASTERISK
3452 apply_opt(call, (char *)data);
3455 #ifdef LCR_FOR_CALLWEAVER
3456 apply_opt(call, (char *)argv[0]);
3460 if (call->tx_queue) {
3461 union parameter newparam;
3463 memset(&newparam, 0, sizeof(union parameter));
3464 newparam.queue = call->tx_queue * 8;
3465 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
3468 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3470 ast_mutex_unlock(&chan_lock);
3475 * module loading and destruction
3477 int load_module(void)
3480 char options_error[256];
3482 for (i = 0; i < 256; i++) {
3483 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3484 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3487 if (read_options(options_error) == 0) {
3488 CERROR(NULL, NULL, "%s", options_error);
3490 #ifdef LCR_FOR_ASTERISK
3491 return AST_MODULE_LOAD_DECLINE;
3494 #ifdef LCR_FOR_CALLWEAVER
3500 ast_mutex_init(&chan_lock);
3501 ast_mutex_init(&log_lock);
3503 #if ASTERISK_VERSION_NUM < 100000
3504 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3506 struct ast_format tmp;
3507 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3508 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3509 return AST_MODULE_LOAD_DECLINE;
3511 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3513 if (ast_channel_register(&lcr_tech)) {
3514 CERROR(NULL, NULL, "Unable to register channel class\n");
3517 #ifdef LCR_FOR_ASTERISK
3518 return AST_MODULE_LOAD_DECLINE;
3521 #ifdef LCR_FOR_CALLWEAVER
3526 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3528 #ifdef LCR_FOR_ASTERISK
3529 "lcr_config(<opt><optarg>:<opt>:...)\n"
3532 #ifdef LCR_FOR_CALLWEAVER
3533 "lcr_config(<opt><optarg>:<opt>:...)\n",
3536 "Sets LCR opts. and optargs\n"
3538 "The available options are:\n"
3539 " d - Send display text on called phone, text is the optarg.\n"
3540 " n - Don't detect dtmf tones from LCR.\n"
3541 " h - Force data call (HDLC).\n"
3542 " q - Add queue to make fax stream seamless (required for fax app).\n"
3543 " Use queue size in miliseconds for optarg. (try 250)\n"
3544 " a - Adding DTMF detection.\n"
3545 " f - Adding fax detection.\n"
3547 " c - Make crypted outgoing call, optarg is keyindex.\n"
3548 " e - Perform echo cancelation on this channel.\n"
3550 " Takes mISDN pipeline option as optarg.\n"
3551 " s - Send Non Inband DTMF as inband. (disables LCR's DTMF)\n"
3552 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3554 " vr - rxgain control\n"
3555 " vt - txgain control\n"
3557 " Volume changes at factor 2 ^ optarg.\n"
3558 " k - use keypad to dial this call.\n"
3560 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3561 " -> use 16 for fax (3.1k audio)\n"
3563 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3564 "options: \"n:t:q250\" for seamless audio transmission.\n"
3569 ast_cli_register(&cli_show_lcr);
3570 ast_cli_register(&cli_show_calls);
3571 ast_cli_register(&cli_reload_routing);
3572 ast_cli_register(&cli_reload_interfaces);
3573 ast_cli_register(&cli_port_block);
3574 ast_cli_register(&cli_port_unblock);
3575 ast_cli_register(&cli_port_unload);
3578 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3579 /* failed to create thread */
3581 ast_channel_unregister(&lcr_tech);
3583 #ifdef LCR_FOR_ASTERISK
3584 return AST_MODULE_LOAD_DECLINE;
3587 #ifdef LCR_FOR_CALLWEAVER
3595 int unload_module(void)
3597 /* First, take us out of the channel loop */
3598 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3600 pthread_cancel(chan_tid);
3604 del_timer(&socket_retry);
3606 unregister_fd(&wake_fd);
3607 close(wake_pipe[0]);
3608 close(wake_pipe[1]);
3610 // ast_mutex_unlock(&chan_lock);
3612 ast_channel_unregister(&lcr_tech);
3614 ast_unregister_application("lcr_config");
3616 if (lcr_sock >= 0) {
3621 #if ASTERISK_VERSION_NUM >= 100000
3622 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3627 int reload_module(void)
3633 #ifdef LCR_FOR_ASTERISK
3634 #define AST_MODULE "chan_lcr"
3637 #ifdef LCR_FOR_CALLWEAVER
3642 ast_mutex_lock(&usecnt_lock);
3644 ast_mutex_unlock(&usecnt_lock);
3649 #ifdef LCR_FOR_ASTERISK
3650 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3651 .load = load_module,
3652 .unload = unload_module,
3653 .reload = reload_module,
3657 #ifdef LCR_FOR_CALLWEAVER
3658 char *description(void)