1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
139 #include <asterisk/module.h>
140 #include <asterisk/channel.h>
141 #include <asterisk/config.h>
142 #include <asterisk/logger.h>
143 #include <asterisk/pbx.h>
144 #include <asterisk/options.h>
145 #include <asterisk/io.h>
146 #include <asterisk/frame.h>
147 #include <asterisk/translate.h>
148 #include <asterisk/cli.h>
149 #include <asterisk/musiconhold.h>
150 #include <asterisk/dsp.h>
151 #include <asterisk/translate.h>
152 #include <asterisk/file.h>
153 #ifdef LCR_FOR_ASTERISK
154 #include <asterisk/callerid.h>
156 #ifdef LCR_FOR_CALLWEAVER
157 #include <asterisk/phone_no_utils.h>
160 #include <asterisk/indications.h>
161 #include <asterisk/app.h>
162 #include <asterisk/features.h>
163 #include <asterisk/sched.h>
164 #include <asterisk/version.h>
166 #include "extension.h"
168 #include "callerid.h"
169 #include "lcrsocket.h"
172 #include "bchannel.h"
174 #include "chan_lcr.h"
176 CHAN_LCR_STATE // state description structure
177 MESSAGES // message text
179 #ifdef LCR_FOR_CALLWEAVER
180 AST_MUTEX_DEFINE_STATIC(rand_lock);
183 unsigned char flip_bits[256];
185 #ifdef LCR_FOR_CALLWEAVER
186 static struct ast_frame nullframe = { AST_FRAME_NULL, };
192 char lcr_type[]="lcr";
194 #ifdef LCR_FOR_CALLWEAVER
195 static ast_mutex_t usecnt_lock;
197 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
201 ast_mutex_t chan_lock; /* global lock */
202 ast_mutex_t log_lock; /* logging log */
204 * used to indicate change in file descriptors, so select function's result may
207 int global_change = 0;
210 struct lcr_fd wake_fd;
212 int glob_channel = 0;
215 struct lcr_fd socket_fd;
216 struct lcr_timer socket_retry;
219 struct admin_list *next;
220 struct admin_message msg;
221 } *admin_first = NULL;
223 static struct ast_channel_tech lcr_tech;
228 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
231 char call_text[128] = "NULL";
232 char ast_text[128] = "NULL";
235 ast_mutex_lock(&log_lock);
238 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
239 buffer[sizeof(buffer)-1]=0;
243 sprintf(call_text, "%d", call->ref);
245 #if ASTERISK_VERSION_NUM < 110000
246 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
248 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
250 ast_text[sizeof(ast_text)-1] = '\0';
252 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
253 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
255 ast_mutex_unlock(&log_lock);
259 * channel and call instances
261 struct chan_call *call_first;
265 * special case: 0: find new ref, that has not been assigned a ref yet
268 struct chan_call *find_call_ref(unsigned int ref)
270 struct chan_call *call = call_first;
271 int assigned = (ref > 0);
274 if (call->ref == ref && call->ref_was_assigned == assigned)
281 void free_call(struct chan_call *call)
283 struct chan_call **temp = &call_first;
287 *temp = (*temp)->next;
288 if (call->pipe[0] > -1)
289 close(call->pipe[0]);
290 if (call->pipe[1] > -1)
291 close(call->pipe[1]);
292 if (call->bchannel) {
293 if (call->bchannel->call != call)
294 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
295 call->bchannel->call = NULL;
297 if (call->bridge_call) {
298 if (call->bridge_call->bridge_call != call)
299 CERROR(call, NULL, "Linked call structure has no link to us.\n");
300 call->bridge_call->bridge_call = NULL;
303 ast_translator_free_path(call->trans);
305 ast_dsp_free(call->dsp);
306 CDEBUG(call, NULL, "Call instance freed.\n");
311 temp = &((*temp)->next);
313 CERROR(call, NULL, "Call instance not found in list.\n");
316 struct chan_call *alloc_call(void)
318 struct chan_call **callp = &call_first;
321 callp = &((*callp)->next);
323 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
325 memset(*callp, 0, sizeof(struct chan_call));
326 if (pipe((*callp)->pipe) < 0) {
327 CERROR(*callp, NULL, "Failed to create pipe.\n");
331 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
332 CDEBUG(*callp, NULL, "Call instance allocated.\n");
336 unsigned short new_bridge_id(void)
338 struct chan_call *call;
339 unsigned short id = 1;
341 /* search for lowest bridge id that is not in use and not 0 */
345 if (call->bridge_id == id)
353 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
358 * enque message to LCR
360 int send_message(int message_type, unsigned int ref, union parameter *param)
362 struct admin_list *admin, **adminp;
365 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
368 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
370 adminp = &admin_first;
372 adminp = &((*adminp)->next);
373 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
375 CERROR(NULL, NULL, "No memory for message to LCR.\n");
380 admin->msg.message = ADMIN_MESSAGE;
381 admin->msg.u.msg.type = message_type;
382 admin->msg.u.msg.ref = ref;
383 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
384 socket_fd.when |= LCR_FD_WRITE;
388 write(wake_pipe[1], &byte, 1);
395 * apply options (in locked state)
397 void apply_opt(struct chan_call *call, char *data)
399 union parameter newparam;
400 char string[1024], *p = string, *opt, *key;
406 strncpy(string, data, sizeof(string)-1);
407 string[sizeof(string)-1] = '\0';
410 while((opt = strsep(&p, ":"))) {
413 if (opt[1] == '\0') {
414 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
417 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
418 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
419 strncpy(call->display, opt+1, sizeof(call->display)-1);
421 memset(&newparam, 0, sizeof(union parameter));
422 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
423 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
427 if (opt[1] != '\0') {
428 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
431 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
432 if (call->dsp_dtmf) {
435 bchannel_dtmf(call->bchannel, 0);
439 if (opt[1] == '\0') {
440 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
444 /* check for 0xXXXX... type of key */
445 if (!!strncmp((char *)key, "0x", 2)) {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
450 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
451 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
456 if (*key>='0' && *key<='9')
457 call->bf_key[i] = (*key-'0') << 8;
458 else if (*key>='a' && *key<='f')
459 call->bf_key[i] = (*key-'a'+10) << 8;
460 else if (*key>='A' && *key<='F')
461 call->bf_key[i] = (*key-'A'+10) << 8;
465 if (*key>='0' && *key<='9')
466 call->bf_key[i] += (*key - '0');
467 else if (*key>='a' && *key<='f')
468 call->bf_key[i] += (*key - 'a' + 10);
469 else if (*key>='A' && *key<='F')
470 call->bf_key[i] += (*key - 'A' + 10);
477 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
481 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
483 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
486 if (opt[1] != '\0') {
487 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
490 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
495 if (opt[1] != '\0') {
496 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
499 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
504 if (opt[1] == '\0') {
505 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
508 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
509 call->nodsp_queue = atoi(opt+1);
512 if (opt[1] == '\0') {
513 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
516 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
517 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
519 bchannel_pipeline(call->bchannel, call->pipeline);
522 if (opt[1] == '\0') {
523 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
526 call->faxdetect=atoi(opt+1);
528 call->dsp=ast_dsp_new();
530 #ifdef LCR_FOR_CALLWEAVER
531 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
533 #ifdef LCR_FOR_ASTERISK
534 #ifdef DSP_FEATURE_DTMF_DETECT
535 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
537 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
542 #ifdef LCR_FOR_CALLWEAVER
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
545 #ifdef LCR_FOR_ASTERISK
546 #if ASTERISK_VERSION_NUM < 100000
547 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
549 struct ast_format src;
550 struct ast_format dst;
551 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
552 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
553 call->trans=ast_translator_build_path(&dst, &src);
558 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
561 if (opt[1] != '\0') {
562 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
565 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
570 if (opt[1] != '\0') {
571 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
574 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
575 call->inband_dtmf = 1;
578 if (opt[1] != 'r' && opt[1] != 't') {
579 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
583 if (gain < -8 || gain >8) {
584 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
587 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
589 call->rx_gain = gain;
591 bchannel_gain(call->bchannel, call->rx_gain, 0);
593 call->tx_gain = gain;
595 bchannel_gain(call->bchannel, call->tx_gain, 1);
599 if (opt[1] != '\0') {
600 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
603 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
608 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
612 /* re-open, if bchannel is created */
613 if (call->bchannel && call->bchannel->b_sock > -1) {
614 bchannel_destroy(call->bchannel);
615 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
616 bchannel_activate(call->bchannel, 1);
621 * send setup info to LCR
622 * this function is called, when asterisk call is received and ref is received
624 static void send_setup_to_lcr(struct chan_call *call)
626 union parameter newparam;
627 struct ast_channel *ast = call->ast;
630 if (!call->ast || !call->ref)
633 #ifdef AST_1_8_OR_HIGHER
634 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
636 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
639 /* send setup message to LCR */
640 memset(&newparam, 0, sizeof(union parameter));
641 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
642 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
644 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
646 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
647 if (!!strcmp(call->interface, "pbx"))
648 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
649 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
650 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
651 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
652 call->display[0] = '\0';
654 #ifdef AST_1_8_OR_HIGHER
655 /* set stored call information */
656 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
657 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
659 if (call->cid_num[0])
660 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
661 if (call->cid_name[0])
662 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
663 if (call->cid_rdnis[0]) {
664 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
665 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
666 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
668 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
669 case AST_PRES_RESTRICTED:
670 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
672 case AST_PRES_UNAVAILABLE:
673 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
675 case AST_PRES_ALLOWED:
677 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
679 switch(ast->cid.cid_ton) {
681 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
684 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
687 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
690 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
693 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
694 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
696 #if ASTERISK_VERSION_NUM < 110000
697 // ast->transfercapability = atoi(tmp);
698 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
700 // ast_channel_transfercapability_set(ast, atoi(tmp));
701 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
703 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
705 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
707 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
708 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
710 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
711 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
712 send_message(MESSAGE_SETUP, call->ref, &newparam);
714 /* change to outgoing setup state */
715 call->state = CHAN_LCR_STATE_OUT_SETUP;
719 * send dialing info to LCR
720 * this function is called, when setup acknowledge is received and dialing
723 static void send_dialque_to_lcr(struct chan_call *call)
725 union parameter newparam;
727 if (!call->ast || !call->ref || !call->dialque[0])
730 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
732 /* send setup message to LCR */
733 memset(&newparam, 0, sizeof(union parameter));
735 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
737 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
738 call->dialque[0] = '\0';
739 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
743 * in case of a bridge, the unsupported message can be forwarded directly
744 * to the remote call.
746 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
750 if (!call->bridge_call) return;
751 CDEBUG(call, NULL, "Sending message due bridging.\n");
752 send_message(message_type, call->bridge_call->ref, param);
756 * send release message to LCR and import bchannel if exported
758 static void send_release_and_import(struct chan_call *call, int cause, int location)
760 union parameter newparam;
762 /* importing channel */
763 if (call->bchannel) {
764 memset(&newparam, 0, sizeof(union parameter));
765 newparam.bchannel.type = BCHANNEL_RELEASE;
766 newparam.bchannel.handle = call->bchannel->handle;
767 send_message(MESSAGE_BCHANNEL, call->ref, &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 *exten = ast_channel_exten(ast);
793 #if ASTERISK_VERSION_NUM < 110000
794 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
796 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
801 #if ASTERISK_VERSION_NUM < 110000
802 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
803 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
805 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
806 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
811 #if ASTERISK_VERSION_NUM < 110000
812 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
813 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);
815 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
816 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));
821 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
822 /* send setup acknowledge to lcr */
823 memset(&newparam, 0, sizeof(union parameter));
824 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
827 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
832 #if ASTERISK_VERSION_NUM < 110000
833 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
835 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
837 /* send setup acknowledge to lcr */
838 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
839 memset(&newparam, 0, sizeof(union parameter));
840 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
844 call->state = CHAN_LCR_STATE_IN_DIALING;
846 /* if match, start pbx */
847 #if ASTERISK_VERSION_NUM < 110000
848 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
850 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
852 CDEBUG(call, ast, "Extensions matches.\n");
857 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
861 #if ASTERISK_VERSION_NUM < 110000
864 if (!*ast_channel_exten(ast)) {
867 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
875 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
876 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
878 /* release asterisk */
879 #if ASTERISK_VERSION_NUM < 110000
880 ast->hangupcause = call->cause;
882 ast_channel_hangupcause_set(ast, call->cause);
884 /* change to release state */
885 call->state = CHAN_LCR_STATE_RELEASE;
886 ast_hangup(ast); // call will be destroyed here
890 /* send setup to asterisk */
891 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
893 #ifdef LCR_FOR_CALLWEAVER
895 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
898 ret = ast_pbx_start(ast);
900 cause = (ret==-2)?34:27;
903 call->pbx_started = 1;
904 ast_setstate(ast, AST_STATE_RING);
908 * incoming setup from LCR
910 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
912 struct ast_channel *ast;
914 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
916 /* create asterisk channel instrance */
918 #ifdef LCR_FOR_CALLWEAVER
919 ast = ast_channel_alloc(1);
922 #ifdef LCR_FOR_ASTERISK
923 #ifdef AST_1_8_OR_HIGHER
924 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
926 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
932 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
933 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
940 #if ASTERISK_VERSION_NUM < 110000
941 ast->tech_pvt = call;
942 ast->tech = &lcr_tech;
944 ast_channel_tech_pvt_set(ast, call);
945 ast_channel_tech_set(ast, &lcr_tech);
947 ast->fds[0] = call->pipe[0];
949 /* fill setup information */
950 if (param->setup.dialinginfo.id)
951 #if ASTERISK_VERSION_NUM < 110000
952 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
953 if (param->setup.context[0])
954 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
956 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
958 strncpy(ast_channel_exten(ast), param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
959 if (param->setup.context[0])
960 strncpy(ast_channel_context(ast), param->setup.context, AST_MAX_CONTEXT-1);
962 strncpy(ast_channel_context(ast), param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
966 #ifdef AST_1_8_OR_HIGHER
967 if (param->setup.callerinfo.id[0]) {
968 ast->caller.id.number.valid = 1;
969 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
970 if (!param->setup.callerinfo.id[0]) {
971 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
972 ast->caller.id.number.plan = (0 << 4) | 1;
974 switch (param->setup.callerinfo.present) {
975 case INFO_PRESENT_ALLOWED:
976 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
978 case INFO_PRESENT_RESTRICTED:
979 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
982 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
984 switch (param->setup.callerinfo.screen) {
985 case INFO_SCREEN_USER:
986 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
988 case INFO_SCREEN_USER_VERIFIED_PASSED:
989 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
991 case INFO_SCREEN_USER_VERIFIED_FAILED:
992 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
995 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
997 switch (param->setup.callerinfo.ntype) {
998 case INFO_NTYPE_SUBSCRIBER:
999 ast->caller.id.number.plan = (4 << 4) | 1;
1001 case INFO_NTYPE_NATIONAL:
1002 ast->caller.id.number.plan = (2 << 4) | 1;
1004 case INFO_NTYPE_INTERNATIONAL:
1005 ast->caller.id.number.plan = (1 << 4) | 1;
1008 ast->caller.id.number.plan = (0 << 4) | 1;
1011 if (param->setup.callerinfo.id2[0]) {
1012 ast->caller.ani.number.valid = 1;
1013 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
1014 switch (param->setup.callerinfo.present2) {
1015 case INFO_PRESENT_ALLOWED:
1016 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
1018 case INFO_PRESENT_RESTRICTED:
1019 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
1022 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
1024 switch (param->setup.callerinfo.screen2) {
1025 case INFO_SCREEN_USER:
1026 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1028 case INFO_SCREEN_USER_VERIFIED_PASSED:
1029 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1031 case INFO_SCREEN_USER_VERIFIED_FAILED:
1032 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1035 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1037 switch (param->setup.callerinfo.ntype2) {
1038 case INFO_NTYPE_SUBSCRIBER:
1039 ast->caller.ani.number.plan = (4 << 4) | 1;
1041 case INFO_NTYPE_NATIONAL:
1042 ast->caller.ani.number.plan = (2 << 4) | 1;
1044 case INFO_NTYPE_INTERNATIONAL:
1045 ast->caller.ani.number.plan = (1 << 4) | 1;
1048 ast->caller.ani.number.plan = (0 << 4) | 1;
1051 if (param->setup.callerinfo.name[0]) {
1052 ast->caller.id.name.valid = 1;
1053 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
1055 if (param->setup.redirinfo.id[0]) {
1056 ast->redirecting.from.number.valid = 1;
1057 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
1058 switch (param->setup.redirinfo.present) {
1059 case INFO_PRESENT_ALLOWED:
1060 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
1062 case INFO_PRESENT_RESTRICTED:
1063 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1066 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1068 switch (param->setup.redirinfo.screen) {
1069 case INFO_SCREEN_USER:
1070 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1072 case INFO_SCREEN_USER_VERIFIED_PASSED:
1073 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1075 case INFO_SCREEN_USER_VERIFIED_FAILED:
1076 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1079 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1081 switch (param->setup.redirinfo.ntype) {
1082 case INFO_NTYPE_SUBSCRIBER:
1083 ast->redirecting.from.number.plan = (4 << 4) | 1;
1085 case INFO_NTYPE_NATIONAL:
1086 ast->redirecting.from.number.plan = (2 << 4) | 1;
1088 case INFO_NTYPE_INTERNATIONAL:
1089 ast->redirecting.from.number.plan = (1 << 4) | 1;
1092 ast->redirecting.from.number.plan = (0 << 4) | 1;
1096 memset(&ast->cid, 0, sizeof(ast->cid));
1097 if (param->setup.callerinfo.id[0])
1098 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1099 if (param->setup.callerinfo.id2[0])
1100 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1101 if (param->setup.callerinfo.name[0])
1102 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1103 if (param->setup.redirinfo.id[0])
1104 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1105 switch (param->setup.callerinfo.present) {
1106 case INFO_PRESENT_ALLOWED:
1107 ast->cid.cid_pres = AST_PRES_ALLOWED;
1109 case INFO_PRESENT_RESTRICTED:
1110 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1113 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1115 switch (param->setup.callerinfo.ntype) {
1116 case INFO_NTYPE_SUBSCRIBER:
1117 ast->cid.cid_ton = 4;
1119 case INFO_NTYPE_NATIONAL:
1120 ast->cid.cid_ton = 2;
1122 case INFO_NTYPE_INTERNATIONAL:
1123 ast->cid.cid_ton = 1;
1126 ast->cid.cid_ton = 0;
1130 #if ASTERISK_VERSION_NUM < 110000
1131 ast->transfercapability = param->setup.capainfo.bearer_capa;
1133 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1135 /* enable hdlc if transcap is data */
1136 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1138 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1140 /* configure channel */
1141 #if ASTERISK_VERSION_NUM < 100000
1142 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1143 ast->readformat = ast->rawreadformat = ast->nativeformats;
1144 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1146 #if ASTERISK_VERSION_NUM < 110000
1147 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1148 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1149 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1150 ast_set_write_format(ast, &ast->rawwriteformat);
1151 ast_set_read_format(ast, &ast->rawreadformat);
1153 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1154 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1155 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1156 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1157 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1160 #if ASTERISK_VERSION_NUM < 110000
1162 ast->hangupcause = 0;
1164 ast_channel_priority_set(ast, 1);
1165 ast_channel_hangupcause_set(ast, 0);
1169 call->state = CHAN_LCR_STATE_IN_SETUP;
1171 if (!call->pbx_started)
1172 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1176 * incoming setup acknowledge from LCR
1178 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1180 if (!call->ast) return;
1182 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1184 /* send pending digits in dialque */
1185 if (call->dialque[0])
1186 send_dialque_to_lcr(call);
1187 /* change to overlap state */
1188 call->state = CHAN_LCR_STATE_OUT_DIALING;
1192 * incoming proceeding from LCR
1194 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1196 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1199 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1200 /* queue event for asterisk */
1201 if (call->ast && call->pbx_started) {
1205 write(wake_pipe[1], &byte, 1);
1207 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1213 * incoming alerting from LCR
1215 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1217 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1220 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1221 /* queue event to asterisk */
1222 if (call->ast && call->pbx_started) {
1226 write(wake_pipe[1], &byte, 1);
1228 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1233 * incoming connect from LCR
1235 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1237 union parameter newparam;
1239 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1242 call->state = CHAN_LCR_STATE_CONNECT;
1243 /* request bchannel */
1244 if (!call->bchannel) {
1245 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1246 memset(&newparam, 0, sizeof(union parameter));
1247 newparam.bchannel.type = BCHANNEL_REQUEST;
1248 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1250 /* copy connectinfo */
1251 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1252 /* queue event to asterisk */
1253 if (call->ast && call->pbx_started) {
1257 write(wake_pipe[1], &byte, 1);
1259 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1264 * incoming disconnect from LCR
1266 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1268 struct ast_channel *ast = call->ast;
1270 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1273 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1275 call->cause = param->disconnectinfo.cause;
1276 call->location = param->disconnectinfo.location;
1277 /* if bridge, forward disconnect and return */
1280 if (call->bridge_call) {
1281 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1282 bridge_message_if_bridged(call, message_type, param);
1286 /* release lcr with same cause */
1287 send_release_and_import(call, call->cause, call->location);
1289 /* change to release state */
1290 call->state = CHAN_LCR_STATE_RELEASE;
1291 /* queue release asterisk */
1293 #if ASTERISK_VERSION_NUM < 110000
1294 ast->hangupcause = call->cause;
1296 ast_channel_hangupcause_set(ast, call->cause);
1298 if (call->pbx_started) {
1302 write(wake_pipe[1], &byte, 1);
1304 strcpy(call->queue_string, "H"); // overwrite other indications
1306 ast_hangup(ast); // call will be destroyed here
1312 * incoming release from LCR
1314 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1316 struct ast_channel *ast = call->ast;
1318 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1322 /* change to release state */
1323 call->state = CHAN_LCR_STATE_RELEASE;
1324 /* copy release info */
1326 call->cause = param->disconnectinfo.cause;
1327 call->location = param->disconnectinfo.location;
1329 /* if we have an asterisk instance, queue hangup, else we are done */
1331 #if ASTERISK_VERSION_NUM < 110000
1332 ast->hangupcause = call->cause;
1334 ast_channel_hangupcause_set(ast, call->cause);
1336 if (call->pbx_started) {
1340 write(wake_pipe[1], &byte, 1);
1342 strcpy(call->queue_string, "H");
1344 ast_hangup(ast); // call will be destroyed here
1353 * incoming information from LCR
1355 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1357 struct ast_channel *ast = call->ast;
1359 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1363 /* pbx not started */
1364 if (!call->pbx_started) {
1365 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1366 #if ASTERISK_VERSION_NUM < 110000
1367 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1369 ast_channel_exten_set(ast, param->information.id);
1371 lcr_start_pbx(call, ast, param->information.sending_complete);
1375 /* change dailing state after setup */
1376 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1377 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1378 call->state = CHAN_LCR_STATE_IN_DIALING;
1379 // ast_setstate(ast, AST_STATE_DIALING);
1383 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1387 write(wake_pipe[1], &byte, 1);
1389 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1392 /* use bridge to forware message not supported by asterisk */
1393 if (call->state == CHAN_LCR_STATE_CONNECT) {
1394 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1395 bridge_message_if_bridged(call, message_type, param);
1400 * incoming information from LCR
1402 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1404 union parameter newparam;
1406 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1408 /* request bchannel, if call is resumed and we don't have it */
1409 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1410 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1411 memset(&newparam, 0, sizeof(union parameter));
1412 newparam.bchannel.type = BCHANNEL_REQUEST;
1413 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1416 if (!call->ast) return;
1418 /* use bridge to forware message not supported by asterisk */
1419 bridge_message_if_bridged(call, message_type, param);
1423 * incoming information from LCR
1425 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1427 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1429 if (!call->ast) return;
1431 /* use bridge to forware message not supported by asterisk */
1432 bridge_message_if_bridged(call, message_type, param);
1436 * incoming pattern from LCR
1438 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1440 union parameter newparam;
1442 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1444 if (!call->ast) return;
1446 /* pattern are indicated only once */
1447 if (call->has_pattern)
1449 call->has_pattern = 1;
1451 /* request bchannel */
1452 if (!call->bchannel) {
1453 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1454 memset(&newparam, 0, sizeof(union parameter));
1455 newparam.bchannel.type = BCHANNEL_REQUEST;
1456 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1458 /* queue PROGRESS, because tones are available */
1459 if (call->ast && call->pbx_started) {
1463 write(wake_pipe[1], &byte, 1);
1465 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1470 * got dtmf from bchannel (locked state)
1472 void lcr_in_dtmf(struct chan_call *call, int val)
1474 struct ast_channel *ast = call->ast;
1479 if (!call->pbx_started)
1482 if (!call->dsp_dtmf) {
1483 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1487 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1493 write(wake_pipe[1], &byte, 1);
1495 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1499 * message received from LCR
1501 int receive_message(int message_type, unsigned int ref, union parameter *param)
1503 struct bchannel *bchannel;
1504 struct chan_call *call;
1505 union parameter newparam;
1507 memset(&newparam, 0, sizeof(union parameter));
1509 /* handle bchannel message*/
1510 if (message_type == MESSAGE_BCHANNEL) {
1511 switch(param->bchannel.type) {
1512 case BCHANNEL_ASSIGN:
1513 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1514 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1515 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1518 /* create bchannel */
1519 bchannel = alloc_bchannel(param->bchannel.handle);
1521 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1525 /* configure channel */
1526 bchannel->b_tx_gain = param->bchannel.tx_gain;
1527 bchannel->b_rx_gain = param->bchannel.rx_gain;
1528 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1529 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1530 bchannel->b_bf_len = param->bchannel.crypt_len;
1531 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1533 bchannel->b_txdata = 0;
1534 bchannel->b_tx_dejitter = 1;
1536 /* in case, ref is not set, this bchannel instance must
1537 * be created until it is removed again by LCR */
1539 call = find_call_ref(ref);
1541 bchannel->call = call;
1542 call->bchannel = bchannel;
1544 bchannel_dtmf(bchannel, 1);
1546 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1547 if (call->pipeline[0])
1548 bchannel_pipeline(bchannel, call->pipeline);
1550 bchannel_gain(bchannel, call->rx_gain, 0);
1552 bchannel_gain(bchannel, call->tx_gain, 1);
1553 if (call->bridge_id) {
1554 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1555 bchannel_join(bchannel, call->bridge_id);
1557 /* ignore all dsp features, if it is a loopback interface */
1558 if (param->bchannel.isloopback)
1561 /* create only, if call exists, othewhise it bchannel is freed below... */
1562 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1563 bchannel_activate(bchannel, 1);
1566 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1567 newparam.bchannel.handle = param->bchannel.handle;
1568 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1569 /* if call has released before bchannel is assigned */
1571 newparam.bchannel.type = BCHANNEL_RELEASE;
1572 newparam.bchannel.handle = param->bchannel.handle;
1573 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1578 case BCHANNEL_REMOVE:
1579 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1580 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1581 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1584 /* unklink from call and destroy bchannel */
1585 free_bchannel(bchannel);
1588 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1589 newparam.bchannel.handle = param->bchannel.handle;
1590 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1595 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1600 /* handle new ref */
1601 if (message_type == MESSAGE_NEWREF) {
1602 if (param->newref.direction) {
1603 /* new ref from lcr */
1604 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1605 if (!ref || find_call_ref(ref)) {
1606 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1609 /* allocate new call instance */
1610 call = alloc_call();
1612 call->state = CHAN_LCR_STATE_IN_PREPARE;
1615 call->ref_was_assigned = 1;
1616 /* set dtmf (default, use option 'n' to disable */
1618 /* wait for setup (or release from asterisk) */
1620 /* new ref, as requested from this remote application */
1621 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1622 call = find_call_ref(0);
1624 /* send release, if ref does not exist */
1625 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1630 call->ref_was_assigned = 1;
1631 /* set dtmf (default, use option 'n' to disable */
1633 /* send pending setup info */
1634 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1635 send_setup_to_lcr(call);
1636 /* release if asterisk has signed off */
1637 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1640 send_release_and_import(call, call->cause, call->location);
1642 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1653 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1656 call = find_call_ref(ref);
1658 /* ignore ref that is not used (anymore) */
1659 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1663 /* handle messages */
1664 switch(message_type) {
1666 lcr_in_setup(call, message_type, param);
1669 case MESSAGE_OVERLAP:
1670 lcr_in_overlap(call, message_type, param);
1673 case MESSAGE_PROCEEDING:
1674 lcr_in_proceeding(call, message_type, param);
1677 case MESSAGE_ALERTING:
1678 lcr_in_alerting(call, message_type, param);
1681 case MESSAGE_CONNECT:
1682 lcr_in_connect(call, message_type, param);
1685 case MESSAGE_DISCONNECT:
1686 lcr_in_disconnect(call, message_type, param);
1689 case MESSAGE_RELEASE:
1690 lcr_in_release(call, message_type, param);
1693 case MESSAGE_INFORMATION:
1694 lcr_in_information(call, message_type, param);
1697 case MESSAGE_NOTIFY:
1698 lcr_in_notify(call, message_type, param);
1701 case MESSAGE_FACILITY:
1702 lcr_in_facility(call, message_type, param);
1705 case MESSAGE_PATTERN: // audio available from LCR
1706 if (!call->has_pattern)
1707 lcr_in_pattern(call, message_type, param);
1710 case MESSAGE_NOPATTERN: // audio not available from LCR
1713 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1714 call->audiopath = param->audiopath;
1718 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1725 * release all calls (due to broken socket)
1727 static void release_all_calls(void)
1729 struct chan_call *call;
1734 /* no ast, so we may directly free call */
1736 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1740 /* already in release process */
1741 if (call->state == CHAN_LCR_STATE_RELEASE) {
1745 /* release or queue release */
1747 call->state = CHAN_LCR_STATE_RELEASE;
1748 if (!call->pbx_started) {
1749 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1750 ast_hangup(call->ast); // call will be destroyed here
1753 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1757 write(wake_pipe[1], &byte, 1);
1759 strcpy(call->queue_string, "H");
1763 /* release all bchannels */
1764 while(bchannel_first)
1765 free_bchannel(bchannel_first);
1768 void close_socket(void);
1771 * warning! not thread safe
1772 * returns -1 for socket error, 0 for no work, 1 for work
1774 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1777 struct admin_list *admin;
1778 struct admin_message msg;
1780 if ((what & LCR_FD_READ)) {
1781 /* read from socket */
1782 len = read(lcr_sock, &msg, sizeof(msg));
1784 CERROR(NULL, NULL, "Socket closed.(read)\n");
1786 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1788 release_all_calls();
1789 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1793 if (len != sizeof(msg)) {
1794 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1797 if (msg.message != ADMIN_MESSAGE) {
1798 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1801 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1803 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1808 if ((what & LCR_FD_WRITE)) {
1809 /* write to socket */
1811 socket_fd.when &= ~LCR_FD_WRITE;
1814 admin = admin_first;
1815 len = write(lcr_sock, &admin->msg, sizeof(msg));
1817 CERROR(NULL, NULL, "Socket closed.(write)\n");
1821 if (len != sizeof(msg)) {
1822 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1826 admin_first = admin->next;
1830 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1839 * open and close socket and thread
1841 int open_socket(void)
1844 struct sockaddr_un sock_address;
1845 union parameter param;
1848 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1849 CERROR(NULL, NULL, "Failed to create socket.\n");
1853 /* set socket address and name */
1854 memset(&sock_address, 0, sizeof(sock_address));
1855 sock_address.sun_family = PF_UNIX;
1856 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1858 /* connect socket */
1859 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1862 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1866 /* register socket fd */
1867 memset(&socket_fd, 0, sizeof(socket_fd));
1868 socket_fd.fd = lcr_sock;
1869 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1871 /* enque hello message */
1872 memset(¶m, 0, sizeof(param));
1873 strcpy(param.hello.application, "asterisk");
1874 send_message(MESSAGE_HELLO, 0, ¶m);
1879 void close_socket(void)
1881 struct admin_list *admin, *temp;
1883 /* socket not created */
1887 unregister_fd(&socket_fd);
1889 /* flush pending messages */
1890 admin = admin_first;
1893 admin = admin->next;
1905 /* sending queue to asterisk */
1906 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1910 read(wake_pipe[0], &byte, 1);
1917 static void handle_queue()
1919 struct chan_call *call;
1920 struct ast_channel *ast;
1921 struct ast_frame fr;
1927 p = call->queue_string;
1930 if (ast_channel_trylock(ast)) {
1931 ast_mutex_unlock(&chan_lock);
1933 ast_mutex_lock(&chan_lock);
1939 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1940 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1943 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1944 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1947 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1948 ast_queue_control(ast, AST_CONTROL_RINGING);
1949 ast_setstate(ast, AST_STATE_RINGING);
1952 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1953 ast_queue_control(ast, AST_CONTROL_ANSWER);
1956 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1957 ast_queue_hangup(ast);
1959 case '1': case '2': case '3': case 'A':
1960 case '4': case '5': case '6': case 'B':
1961 case '7': case '8': case '9': case 'C':
1962 case '*': case '0': case '#': case 'D':
1963 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1964 /* send digit to asterisk */
1965 memset(&fr, 0, sizeof(fr));
1967 #ifdef LCR_FOR_ASTERISK
1968 fr.frametype = AST_FRAME_DTMF_BEGIN;
1971 #ifdef LCR_FOR_CALLWEAVER
1972 fr.frametype = AST_FRAME_DTMF;
1975 #ifdef AST_1_8_OR_HIGHER
1976 fr.subclass.integer = *p;
1980 fr.delivery = ast_tv(0, 0);
1981 ast_queue_frame(ast, &fr);
1983 #ifdef LCR_FOR_ASTERISK
1984 fr.frametype = AST_FRAME_DTMF_END;
1985 ast_queue_frame(ast, &fr);
1990 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1994 call->queue_string[0] = '\0';
1995 ast_channel_unlock(ast);
2001 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
2003 CDEBUG(NULL, NULL, "Retry to open socket.\n");
2004 if (open_socket() < 0)
2005 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
2010 void lock_chan(void)
2012 ast_mutex_lock(&chan_lock);
2015 void unlock_chan(void)
2017 ast_mutex_unlock(&chan_lock);
2020 /* chan_lcr thread */
2021 static void *chan_thread(void *arg)
2023 if (pipe(wake_pipe) < 0) {
2024 CERROR(NULL, NULL, "Failed to open pipe.\n");
2027 memset(&wake_fd, 0, sizeof(wake_fd));
2028 wake_fd.fd = wake_pipe[0];
2029 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2031 memset(&socket_retry, 0, sizeof(socket_retry));
2032 add_timer(&socket_retry, handle_retry, NULL, 0);
2034 bchannel_pid = getpid();
2036 /* open socket the first time */
2037 handle_retry(NULL, NULL, 0);
2039 ast_mutex_lock(&chan_lock);
2043 select_main(0, &global_change, lock_chan, unlock_chan);
2050 * new asterisk instance
2053 #ifdef AST_1_8_OR_HIGHER
2054 #if ASTERISK_VERSION_NUM < 100000
2055 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2057 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2060 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2063 char exten[256], *dial, *interface, *opt;
2064 struct ast_channel *ast;
2065 struct chan_call *call;
2067 ast_mutex_lock(&chan_lock);
2068 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2070 /* if socket is closed */
2072 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2073 ast_mutex_unlock(&chan_lock);
2077 /* create call instance */
2078 call = alloc_call();
2080 /* failed to create instance */
2081 ast_mutex_unlock(&chan_lock);
2085 /* create asterisk channel instrance */
2087 #ifdef LCR_FOR_ASTERISK
2088 #ifdef AST_1_8_OR_HIGHER
2089 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2091 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2095 #ifdef LCR_FOR_CALLWEAVER
2096 ast = ast_channel_alloc(1);
2100 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2102 /* failed to create instance */
2103 ast_mutex_unlock(&chan_lock);
2106 #if ASTERISK_VERSION_NUM < 110000
2107 ast->tech = &lcr_tech;
2108 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2110 ast_channel_tech_set(ast, &lcr_tech);
2111 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2113 /* configure channel */
2114 #if ASTERISK_VERSION_NUM < 100000
2115 #if ASTERISK_VERSION_NUM < 110000
2116 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2117 ast->readformat = ast->rawreadformat = ast->nativeformats;
2118 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2120 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2121 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2122 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2125 #if ASTERISK_VERSION_NUM < 110000
2126 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2127 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2128 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2129 ast_set_write_format(ast, &ast->rawwriteformat);
2130 ast_set_read_format(ast, &ast->rawreadformat);
2132 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2133 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2134 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2135 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2136 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2139 #if ASTERISK_VERSION_NUM < 110000
2141 ast->hangupcause = 0;
2143 ast_channel_priority_set(ast, 1);
2144 ast_channel_hangupcause_set(ast, 0);
2149 #if ASTERISK_VERSION_NUM < 110000
2150 ast->tech_pvt = call;
2152 ast_channel_tech_pvt_set(ast, call);
2154 ast->fds[0] = call->pipe[0];
2155 call->pbx_started = 0;
2157 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2160 * Extract interface, dialstring, options from data.
2163 * <interface>/<dialstring>
2164 * <interface>/<dialstring>/options
2166 strncpy(exten, (char *)data, sizeof(exten)-1);
2167 exten[sizeof(exten)-1] = '\0';
2168 if ((dial = strchr(exten, '/'))) {
2171 if ((opt = strchr(dial, '/')))
2180 strncpy(call->interface, interface, sizeof(call->interface)-1);
2181 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2182 apply_opt(call, (char *)opt);
2184 #ifdef AST_1_8_OR_HIGHER
2185 // clone_variables(requestor, ast);
2188 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2189 if (requestor->caller.ani.number.valid)
2190 if (requestor->caller.ani.number.str)
2191 if (requestor->caller.ani.number.str[0])
2192 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2193 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2194 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2196 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2197 if (requestor->caller.ani.name.valid)
2198 if (requestor->caller.ani.name.str)
2199 if (requestor->caller.ani.name.str[0])
2200 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2201 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2203 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2204 if (requestor->caller.ani.subaddress.valid)
2205 if (requestor->caller.ani.subaddress.str)
2206 if (requestor->caller.ani.subaddress.str[0])
2207 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2208 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2210 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2211 if (requestor->caller.id.number.valid)
2212 if (requestor->caller.id.number.str)
2213 if (requestor->caller.id.number.str[0])
2214 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2215 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2216 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2218 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2219 if (requestor->caller.id.name.valid)
2220 if (requestor->caller.id.name.str)
2221 if (requestor->caller.id.name.str[0])
2222 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2223 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2225 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2226 if (requestor->caller.id.subaddress.valid)
2227 if (requestor->caller.id.subaddress.str)
2228 if (requestor->caller.id.subaddress.str[0])
2229 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2230 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2232 if (requestor->dialed.number.str)
2233 if (requestor->dialed.number.str[0])
2234 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2235 ast->dialed.number.plan= requestor->dialed.number.plan;
2237 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2238 if (requestor->dialed.subaddress.valid)
2239 if (requestor->dialed.subaddress.str)
2240 if (requestor->dialed.subaddress.str[0])
2241 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2242 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2244 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2245 ast->redirecting.count= requestor->redirecting.count;
2246 ast->redirecting.reason= requestor->redirecting.reason;
2248 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2249 if (requestor->redirecting.from.number.valid)
2250 if (requestor->redirecting.from.number.str)
2251 if (requestor->redirecting.from.number.str[0])
2252 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2253 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2254 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2256 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2257 if (requestor->redirecting.to.number.valid)
2258 if (requestor->redirecting.to.number.str)
2259 if (requestor->redirecting.to.number.str[0])
2260 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2261 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2262 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2264 /* store call information for setup */
2267 if (requestor && requestor->caller.id.number.valid) {
2268 if (requestor->caller.id.number.str)
2269 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2270 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2271 case AST_PRES_RESTRICTED:
2272 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2274 case AST_PRES_UNAVAILABLE:
2275 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2277 case AST_PRES_ALLOWED:
2279 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2281 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2282 case AST_PRES_USER_NUMBER_UNSCREENED:
2283 call->callerinfo.screen = INFO_SCREEN_USER;
2285 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2286 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2288 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2289 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2292 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2294 switch((requestor->caller.id.number.plan >> 4) & 7) {
2296 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2299 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2302 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2305 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2308 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2311 if (requestor && requestor->caller.ani.number.valid) {
2312 if (requestor->caller.ani.number.str)
2313 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2314 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2315 case AST_PRES_RESTRICTED:
2316 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2318 case AST_PRES_UNAVAILABLE:
2319 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2321 case AST_PRES_ALLOWED:
2323 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2325 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2326 case AST_PRES_USER_NUMBER_UNSCREENED:
2327 call->callerinfo.screen2 = INFO_SCREEN_USER;
2329 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2330 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2332 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2333 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2336 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2338 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2340 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2343 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2346 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2349 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2352 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2355 if (requestor && requestor->caller.id.name.valid) {
2356 if (requestor->caller.id.name.str)
2357 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2361 if (requestor && requestor->redirecting.from.number.valid) {
2362 call->redirinfo.itype = INFO_ITYPE_CHAN;
2363 if (requestor->redirecting.from.number.str)
2364 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2365 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2366 case AST_PRES_RESTRICTED:
2367 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2369 case AST_PRES_UNAVAILABLE:
2370 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2372 case AST_PRES_ALLOWED:
2374 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2376 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2377 case AST_PRES_USER_NUMBER_UNSCREENED:
2378 call->redirinfo.screen = INFO_SCREEN_USER;
2380 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2381 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2383 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2384 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2387 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2389 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2391 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2394 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2397 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2400 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2405 ast_mutex_unlock(&chan_lock);
2410 * call from asterisk
2412 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2414 union parameter newparam;
2415 struct chan_call *call;
2416 #if ASTERISK_VERSION_NUM >= 110000
2417 int transfercapability;
2420 ast_mutex_lock(&chan_lock);
2421 #if ASTERISK_VERSION_NUM < 110000
2422 call = ast->tech_pvt;
2424 call = ast_channel_tech_pvt(ast);
2427 #ifdef LCR_FOR_CALLWEAVER
2429 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2433 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2434 ast_mutex_unlock(&chan_lock);
2438 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2440 /* pbx process is started */
2441 call->pbx_started = 1;
2442 /* send MESSAGE_NEWREF */
2443 memset(&newparam, 0, sizeof(union parameter));
2444 newparam.newref.direction = 0; /* request from app */
2445 if (!strcmp(call->interface, "pbx"))
2446 newparam.newref.mode = 1;
2447 send_message(MESSAGE_NEWREF, 0, &newparam);
2450 transfercapability=ast_channel_transfercapability(ast);
2452 /* set hdlc if capability requires hdlc */
2453 #if ASTERISK_VERSION_NUM < 110000
2454 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2455 || ast->transfercapability == INFO_BC_DATARESTRICTED
2456 || ast->transfercapability == INFO_BC_VIDEO)
2458 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2459 || transfercapability == INFO_BC_DATARESTRICTED
2460 || transfercapability == INFO_BC_VIDEO)
2463 /* if hdlc is forced by option, we change transcap to data */
2465 #if ASTERISK_VERSION_NUM < 110000
2466 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2467 && ast->transfercapability != INFO_BC_DATARESTRICTED
2468 && ast->transfercapability != INFO_BC_VIDEO)
2469 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2471 && transfercapability != INFO_BC_DATAUNRESTRICTED
2472 && transfercapability != INFO_BC_DATARESTRICTED
2473 && transfercapability != INFO_BC_VIDEO)
2474 transfercapability = INFO_BC_DATAUNRESTRICTED;
2477 #ifndef AST_1_8_OR_HIGHER
2478 call->cid_num[0] = 0;
2479 call->cid_name[0] = 0;
2480 call->cid_rdnis[0] = 0;
2482 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2483 strncpy(call->cid_num, ast->cid.cid_num,
2484 sizeof(call->cid_num)-1);
2485 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2486 strncpy(call->cid_name, ast->cid.cid_name,
2487 sizeof(call->cid_name)-1);
2488 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2489 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2490 sizeof(call->cid_rdnis)-1);
2493 ast_mutex_unlock(&chan_lock);
2497 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2499 static const char* dtmf_tones[] = {
2500 "!941+1336/100,!0/100", /* 0 */
2501 "!697+1209/100,!0/100", /* 1 */
2502 "!697+1336/100,!0/100", /* 2 */
2503 "!697+1477/100,!0/100", /* 3 */
2504 "!770+1209/100,!0/100", /* 4 */
2505 "!770+1336/100,!0/100", /* 5 */
2506 "!770+1477/100,!0/100", /* 6 */
2507 "!852+1209/100,!0/100", /* 7 */
2508 "!852+1336/100,!0/100", /* 8 */
2509 "!852+1477/100,!0/100", /* 9 */
2510 "!697+1633/100,!0/100", /* A */
2511 "!770+1633/100,!0/100", /* B */
2512 "!852+1633/100,!0/100", /* C */
2513 "!941+1633/100,!0/100", /* D */
2514 "!941+1209/100,!0/100", /* * */
2515 "!941+1477/100,!0/100" }; /* # */
2517 if (digit >= '0' && digit <='9')
2518 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2519 else if (digit >= 'A' && digit <= 'D')
2520 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2521 else if (digit == '*')
2522 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2523 else if (digit == '#')
2524 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2526 #if ASTERISK_VERSION_NUM < 110000
2527 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2529 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2534 #ifdef LCR_FOR_ASTERISK
2535 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2537 #ifdef LCR_FOR_CALLWEAVER
2538 static int lcr_digit(struct ast_channel *ast, char digit)
2541 struct chan_call *call;
2542 union parameter newparam;
2545 #ifdef LCR_FOR_CALLWEAVER
2546 int inband_dtmf = 0;
2549 /* only pass IA5 number space */
2550 if (digit > 126 || digit < 32)
2553 ast_mutex_lock(&chan_lock);
2554 #if ASTERISK_VERSION_NUM < 110000
2555 call = ast->tech_pvt;
2557 call = ast_channel_tech_pvt(ast);
2560 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2561 ast_mutex_unlock(&chan_lock);
2565 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2567 /* send information or queue them */
2568 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2569 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2570 memset(&newparam, 0, sizeof(union parameter));
2572 newparam.information.keypad[0] = digit;
2573 newparam.information.keypad[1] = '\0';
2575 newparam.information.id[0] = digit;
2576 newparam.information.id[1] = '\0';
2578 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2581 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2582 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2584 strncat(call->dialque, buf, strlen(call->dialque)-1);
2587 ast_mutex_unlock(&chan_lock);
2589 #ifdef LCR_FOR_ASTERISK
2593 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2595 int inband_dtmf = 0;
2596 struct chan_call *call;
2599 ast_mutex_lock(&chan_lock);
2601 #if ASTERISK_VERSION_NUM < 110000
2602 call = ast->tech_pvt;
2604 call = ast_channel_tech_pvt(ast);
2609 "Received digit from Asterisk, "
2610 "but no call instance exists.\n");
2611 ast_mutex_unlock(&chan_lock);
2615 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2617 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2621 ast_mutex_unlock(&chan_lock);
2624 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2625 send_digit_to_chan(ast, digit);
2631 static int lcr_answer(struct ast_channel *ast)
2633 union parameter newparam;
2634 struct chan_call *call;
2636 ast_mutex_lock(&chan_lock);
2637 #if ASTERISK_VERSION_NUM < 110000
2638 call = ast->tech_pvt;
2640 call = ast_channel_tech_pvt(ast);
2643 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2644 ast_mutex_unlock(&chan_lock);
2648 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2650 /* copy connectinfo, if bridged */
2651 if (call->bridge_call)
2652 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2653 /* send connect message to lcr */
2654 if (call->state != CHAN_LCR_STATE_CONNECT) {
2655 memset(&newparam, 0, sizeof(union parameter));
2656 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2657 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2658 call->state = CHAN_LCR_STATE_CONNECT;
2661 /* request bchannel */
2662 if (!call->bchannel) {
2663 CDEBUG(call, ast, "Requesting B-channel.\n");
2664 memset(&newparam, 0, sizeof(union parameter));
2665 newparam.bchannel.type = BCHANNEL_REQUEST;
2666 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2669 // memset(&newparam, 0, sizeof(union parameter));
2670 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2672 ast_mutex_unlock(&chan_lock);
2676 static int lcr_hangup(struct ast_channel *ast)
2678 struct chan_call *call;
2679 pthread_t tid = pthread_self();
2681 if (!pthread_equal(tid, chan_tid)) {
2682 ast_mutex_lock(&chan_lock);
2684 #if ASTERISK_VERSION_NUM < 110000
2685 call = ast->tech_pvt;
2687 call = ast_channel_tech_pvt(ast);
2690 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2691 if (!pthread_equal(tid, chan_tid)) {
2692 ast_mutex_unlock(&chan_lock);
2697 if (!pthread_equal(tid, chan_tid))
2698 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2700 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2702 /* disconnect asterisk, maybe not required */
2703 #if ASTERISK_VERSION_NUM < 110000
2704 ast->tech_pvt = NULL;
2706 ast_channel_tech_pvt_set(ast, NULL);
2711 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2712 #if ASTERISK_VERSION_NUM < 110000
2713 if (ast->hangupcause > 0)
2714 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2716 if (ast_channel_hangupcause(ast) > 0)
2717 send_release_and_import(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2720 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2723 if (!pthread_equal(tid, chan_tid)) {
2724 ast_mutex_unlock(&chan_lock);
2728 /* ref is not set, due to prepare setup or release */
2729 if (call->state == CHAN_LCR_STATE_RELEASE) {
2730 /* we get the response to our release */
2731 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2734 /* during prepare, we change to release state */
2735 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2736 call->state = CHAN_LCR_STATE_RELEASE;
2740 if (!pthread_equal(tid, chan_tid)) {
2741 ast_mutex_unlock(&chan_lock);
2746 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2748 struct chan_call *call;
2749 struct ast_frame * f = fr;
2751 #if ASTERISK_VERSION_NUM < 100000
2752 #ifdef AST_1_8_OR_HIGHER
2753 if (!f->subclass.codec)
2757 CDEBUG(NULL, ast, "No subclass\n");
2759 #ifdef AST_1_8_OR_HIGHER
2760 #if ASTERISK_VERSION_NUM < 100000
2761 #if ASTERISK_VERSION_NUM < 110000
2762 if (!(f->subclass.codec & ast->nativeformats)) {
2764 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2767 #if ASTERISK_VERSION_NUM < 110000
2768 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2770 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2774 #if ASTERISK_VERSION_NUM < 110000
2775 if (!(f->subclass & ast->nativeformats)) {
2777 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2781 "Unexpected format. "
2782 "Activating emergency conversion...\n");
2784 #ifdef AST_1_8_OR_HIGHER
2785 #if ASTERISK_VERSION_NUM < 100000
2786 ast_set_write_format(ast, f->subclass.codec);
2788 ast_set_write_format(ast, &f->subclass.format);
2791 ast_set_write_format(ast, f->subclass);
2793 #if ASTERISK_VERSION_NUM < 110000
2794 f = (ast->writetrans) ? ast_translate(
2795 ast->writetrans, fr, 0) : fr;
2797 f = (ast_channel_writetrans(ast)) ? ast_translate(
2798 ast_channel_writetrans(ast), fr, 0) : fr;
2802 ast_mutex_lock(&chan_lock);
2803 #if ASTERISK_VERSION_NUM < 110000
2804 call = ast->tech_pvt;
2806 call = ast_channel_tech_pvt(ast);
2809 ast_mutex_unlock(&chan_lock);
2815 if (call->bchannel && f->samples)
2816 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2817 ast_mutex_unlock(&chan_lock);
2825 static struct ast_frame *lcr_read(struct ast_channel *ast)
2827 struct chan_call *call;
2830 ast_mutex_lock(&chan_lock);
2831 #if ASTERISK_VERSION_NUM < 110000
2832 call = ast->tech_pvt;
2834 call = ast_channel_tech_pvt(ast);
2837 ast_mutex_unlock(&chan_lock);
2840 if (call->pipe[0] > -1) {
2841 if (call->rebuffer && !call->hdlc) {
2842 /* Make sure we have a complete 20ms (160byte) frame */
2843 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2845 call->framepos += len;
2848 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2850 if (len < 0 && errno == EAGAIN) {
2851 ast_mutex_unlock(&chan_lock);
2853 #ifdef LCR_FOR_ASTERISK
2854 return &ast_null_frame;
2857 #ifdef LCR_FOR_CALLWEAVER
2863 close(call->pipe[0]);
2866 ast_mutex_unlock(&chan_lock);
2868 } else if (call->rebuffer && call->framepos < 160) {
2869 /* Not a complete frame, so we send a null-frame */
2870 ast_mutex_unlock(&chan_lock);
2871 return &ast_null_frame;
2875 call->read_fr.frametype = AST_FRAME_VOICE;
2876 #ifdef AST_1_8_OR_HIGHER
2877 #if ASTERISK_VERSION_NUM < 100000
2878 #if ASTERISK_VERSION_NUM < 110000
2879 call->read_fr.subclass.codec = ast->nativeformats;
2881 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2884 #if ASTERISK_VERSION_NUM < 110000
2885 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2887 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2889 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2892 #if ASTERISK_VERSION_NUM < 110000
2893 call->read_fr.subclass = ast->nativeformats;
2895 call->read_fr.subclass = ast_channel_nativeformats(ast);
2898 if (call->rebuffer) {
2899 call->read_fr.datalen = call->framepos;
2900 call->read_fr.samples = call->framepos;
2903 call->read_fr.datalen = len;
2904 call->read_fr.samples = len;
2906 call->read_fr.delivery = ast_tv(0,0);
2907 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2908 ast_mutex_unlock(&chan_lock);
2910 return &call->read_fr;
2913 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2915 union parameter newparam;
2917 struct chan_call *call;
2918 const struct tone_zone_sound *ts = NULL;
2920 ast_mutex_lock(&chan_lock);
2921 #if ASTERISK_VERSION_NUM < 110000
2922 call = ast->tech_pvt;
2924 call = ast_channel_tech_pvt(ast);
2927 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2928 ast_mutex_unlock(&chan_lock);
2933 case AST_CONTROL_BUSY:
2934 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2935 ast_setstate(ast, AST_STATE_BUSY);
2936 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2937 /* send message to lcr */
2938 memset(&newparam, 0, sizeof(union parameter));
2939 newparam.disconnectinfo.cause = 17;
2940 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2941 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2943 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2945 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2946 #if ASTERISK_VERSION_NUM < 110000
2947 ts = ast_get_indication_tone(ast->zone, "busy");
2949 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2953 case AST_CONTROL_CONGESTION:
2954 #if ASTERISK_VERSION_NUM < 110000
2955 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2957 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2959 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2960 /* send message to lcr */
2961 memset(&newparam, 0, sizeof(union parameter));
2962 #if ASTERISK_VERSION_NUM < 110000
2963 newparam.disconnectinfo.cause = ast->hangupcause;
2965 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
2967 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2968 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2970 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2972 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2973 #if ASTERISK_VERSION_NUM < 110000
2974 ts = ast_get_indication_tone(ast->zone, "congestion");
2976 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
2980 case AST_CONTROL_PROCEEDING:
2981 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2982 if (call->state == CHAN_LCR_STATE_IN_SETUP
2983 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2984 /* send message to lcr */
2985 memset(&newparam, 0, sizeof(union parameter));
2986 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2988 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2991 case AST_CONTROL_RINGING:
2992 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2993 ast_setstate(ast, AST_STATE_RING);
2994 if (call->state == CHAN_LCR_STATE_IN_SETUP
2995 || call->state == CHAN_LCR_STATE_IN_DIALING
2996 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2997 /* send message to lcr */
2998 memset(&newparam, 0, sizeof(union parameter));
2999 send_message(MESSAGE_ALERTING, call->ref, &newparam);
3001 call->state = CHAN_LCR_STATE_IN_ALERTING;
3003 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
3004 #if ASTERISK_VERSION_NUM < 110000
3005 ts = ast_get_indication_tone(ast->zone, "ring");
3007 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
3011 case AST_CONTROL_PROGRESS:
3012 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
3013 /* request bchannel */
3014 if (!call->bchannel) {
3015 CDEBUG(call, ast, "Requesting B-channel.\n");
3016 memset(&newparam, 0, sizeof(union parameter));
3017 newparam.bchannel.type = BCHANNEL_REQUEST;
3018 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
3022 CDEBUG(call, ast, "Received indicate -1.\n");
3023 ast_playtones_stop(ast);
3027 case AST_CONTROL_VIDUPDATE:
3028 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
3031 case AST_CONTROL_HOLD:
3032 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
3033 /* send message to lcr */
3034 memset(&newparam, 0, sizeof(union parameter));
3035 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
3036 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3038 /*start music onhold*/
3039 #ifdef LCR_FOR_ASTERISK
3040 #if ASTERISK_VERSION_NUM <110000
3041 ast_moh_start(ast,data,ast->musicclass);
3043 ast_moh_start(ast,data,ast_channel_musicclass(ast));
3047 #ifdef LCR_FOR_CALLWEAVER
3048 ast_moh_start(ast, NULL);
3053 case AST_CONTROL_UNHOLD:
3054 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3055 /* send message to lcr */
3056 memset(&newparam, 0, sizeof(union parameter));
3057 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3058 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3064 #ifdef AST_CONTROL_SRCUPDATE
3065 case AST_CONTROL_SRCUPDATE:
3069 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3072 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3077 if (ts && ts->data[0]) {
3078 ast_playtones_start(ast, 0, ts->data, 1);
3082 ast_mutex_unlock(&chan_lock);
3089 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3091 struct chan_call *call;
3097 ast_mutex_lock(&chan_lock);
3098 #if ASTERISK_VERSION_NUM < 110000
3099 call = ast->tech_pvt;
3101 call = ast_channel_tech_pvt(ast);
3104 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3105 ast_mutex_unlock(&chan_lock);
3109 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3111 ast_mutex_unlock(&chan_lock);
3116 * send_text asterisk
3118 static int lcr_send_text(struct ast_channel *ast, const char *text)
3120 struct chan_call *call;
3121 union parameter newparam;
3123 ast_mutex_lock(&chan_lock);
3124 #if ASTERISK_VERSION_NUM < 110000
3125 call = ast->tech_pvt;
3127 call = ast_channel_tech_pvt(ast);
3130 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3131 ast_mutex_unlock(&chan_lock);
3135 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3136 memset(&newparam, 0, sizeof(union parameter));
3137 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3138 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3139 ast_mutex_unlock(&chan_lock);
3146 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3147 struct ast_channel *ast2, int flags,
3148 struct ast_frame **fo,
3149 struct ast_channel **rc, int timeoutms)
3152 struct chan_call *call1, *call2;
3153 struct ast_channel *carr[2], *who;
3155 struct ast_frame *f;
3158 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3163 /* join via dsp (if the channels are currently open) */
3164 ast_mutex_lock(&chan_lock);
3165 #if ASTERISK_VERSION_NUM < 110000
3166 call1 = ast1->tech_pvt;
3167 call2 = ast2->tech_pvt;
3169 call1 = ast_channel_tech_pvt(ast1);
3170 call2 = ast_channel_tech_pvt(ast2);
3172 if (!call1 || !call2) {
3173 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3174 ast_mutex_unlock(&chan_lock);
3175 return AST_BRIDGE_COMPLETE;
3178 /* join, if both call instances uses dsp
3179 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3181 if (!call1->nodsp && !call2->nodsp) {
3182 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3184 /* get bridge id and join */
3185 bridge_id = new_bridge_id();
3187 call1->bridge_id = bridge_id;
3188 if (call1->bchannel)
3189 bchannel_join(call1->bchannel, bridge_id);
3191 call2->bridge_id = bridge_id;
3192 if (call2->bchannel)
3193 bchannel_join(call2->bchannel, bridge_id);
3195 if (call1->nodsp && call2->nodsp)
3196 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3198 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3199 call1->bridge_call = call2;
3200 call2->bridge_call = call1;
3202 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3203 || call1->state == CHAN_LCR_STATE_IN_DIALING
3204 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3205 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3206 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3209 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3210 || call2->state == CHAN_LCR_STATE_IN_DIALING
3211 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3212 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3213 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3217 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3218 so let's do it for them. Hmpf.
3221 if (call1->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, call1->ref, &newparam);
3231 if (call2->on_hold) {
3232 union parameter newparam;
3234 memset(&newparam, 0, sizeof(union parameter));
3235 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3236 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3241 ast_mutex_unlock(&chan_lock);
3245 who = ast_waitfor_n(carr, 2, &to);
3248 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3253 if (!f || f->frametype == AST_FRAME_CONTROL) {
3255 CDEBUG(NULL, NULL, "Got hangup.\n");
3257 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3264 if ( f->frametype == AST_FRAME_DTMF ) {
3265 CDEBUG(NULL, NULL, "Got DTMF.\n");
3281 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3283 /* split channels */
3284 ast_mutex_lock(&chan_lock);
3285 #if ASTERISK_VERSION_NUM < 110000
3286 call1 = ast1->tech_pvt;
3287 call2 = ast2->tech_pvt;
3289 call1 = ast_channel_tech_pvt(ast1);
3290 call2 = ast_channel_tech_pvt(ast2);
3292 if (call1 && call1->bridge_id) {
3293 call1->bridge_id = 0;
3294 if (call1->bchannel)
3295 bchannel_join(call1->bchannel, 0);
3296 if (call1->bridge_call)
3297 call1->bridge_call->bridge_call = NULL;
3299 if (call2 && call1->bridge_id) {
3300 call2->bridge_id = 0;
3301 if (call2->bchannel)
3302 bchannel_join(call2->bchannel, 0);
3303 if (call2->bridge_call)
3304 call2->bridge_call->bridge_call = NULL;
3306 call1->bridge_call = NULL;
3307 call2->bridge_call = NULL;
3309 ast_mutex_unlock(&chan_lock);
3310 return AST_BRIDGE_COMPLETE;
3312 static struct ast_channel_tech lcr_tech = {
3314 .description = "Channel driver for connecting to Linux-Call-Router",
3315 #if ASTERISK_VERSION_NUM < 100000
3316 .capabilities = AST_FORMAT_ALAW,
3318 .requester = lcr_request,
3320 #ifdef LCR_FOR_ASTERISK
3321 .send_digit_begin = lcr_digit_begin,
3322 .send_digit_end = lcr_digit_end,
3325 #ifdef LCR_FOR_CALLWEAVER
3326 .send_digit = lcr_digit,
3330 .bridge = lcr_bridge,
3331 .hangup = lcr_hangup,
3332 .answer = lcr_answer,
3335 .indicate = lcr_indicate,
3337 .send_text = lcr_send_text,
3346 static int lcr_show_lcr (int fd, int argc, char *argv[])
3351 static int lcr_show_calls (int fd, int argc, char *argv[])
3356 static int lcr_reload_routing (int fd, int argc, char *argv[])
3361 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3366 static int lcr_port_block (int fd, int argc, char *argv[])
3371 static int lcr_port_unblock (int fd, int argc, char *argv[])
3376 static int lcr_port_unload (int fd, int argc, char *argv[])
3381 static struct ast_cli_entry cli_show_lcr =
3382 { {"lcr", "show", "lcr", NULL},
3384 "Shows current states of LCR core",
3385 "Usage: lcr show lcr\n",
3388 static struct ast_cli_entry cli_show_calls =
3389 { {"lcr", "show", "calls", NULL},
3391 "Shows current calls made by LCR and Asterisk",
3392 "Usage: lcr show calls\n",
3395 static struct ast_cli_entry cli_reload_routing =
3396 { {"lcr", "reload", "routing", NULL},
3398 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3399 "Usage: lcr reload routing\n",
3402 static struct ast_cli_entry cli_reload_interfaces =
3403 { {"lcr", "reload", "interfaces", NULL},
3404 lcr_reload_interfaces,
3405 "Reloads interfaces conf of LCR",
3406 "Usage: lcr reload interfaces\n",
3409 static struct ast_cli_entry cli_port_block =
3410 { {"lcr", "port", "block", NULL},
3412 "Blocks LCR port for further calls",
3413 "Usage: lcr port block \"<port>\"\n",
3416 static struct ast_cli_entry cli_port_unblock =
3417 { {"lcr", "port", "unblock", NULL},
3419 "Unblocks or loads LCR port, port is opened my mISDN",
3420 "Usage: lcr port unblock \"<port>\"\n",
3423 static struct ast_cli_entry cli_port_unload =
3424 { {"lcr", "port", "unload", NULL},
3426 "Unloads LCR port, port is closes by mISDN",
3427 "Usage: lcr port unload \"<port>\"\n",
3432 #ifdef LCR_FOR_ASTERISK
3433 #ifdef AST_1_8_OR_HIGHER
3434 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3436 static int lcr_config_exec(struct ast_channel *ast, void *data)
3440 #ifdef LCR_FOR_CALLWEAVER
3441 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3444 struct chan_call *call;
3446 ast_mutex_lock(&chan_lock);
3448 #ifdef LCR_FOR_ASTERISK
3449 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3452 #ifdef LCR_FOR_CALLWEAVER
3453 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3459 if (call->ast == ast)
3465 #ifdef LCR_FOR_ASTERISK
3466 apply_opt(call, (char *)data);
3469 #ifdef LCR_FOR_CALLWEAVER
3470 apply_opt(call, (char *)argv[0]);
3474 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3476 ast_mutex_unlock(&chan_lock);
3481 * module loading and destruction
3483 int load_module(void)
3486 char options_error[256];
3488 for (i = 0; i < 256; i++) {
3489 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3490 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3493 if (read_options(options_error) == 0) {
3494 CERROR(NULL, NULL, "%s", options_error);
3496 #ifdef LCR_FOR_ASTERISK
3497 return AST_MODULE_LOAD_DECLINE;
3500 #ifdef LCR_FOR_CALLWEAVER
3506 ast_mutex_init(&chan_lock);
3507 ast_mutex_init(&log_lock);
3509 if (bchannel_initialize()) {
3510 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3513 #ifdef LCR_FOR_ASTERISK
3514 return AST_MODULE_LOAD_DECLINE;
3517 #ifdef LCR_FOR_CALLWEAVER
3523 #if ASTERISK_VERSION_NUM < 100000
3524 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3526 struct ast_format tmp;
3527 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3528 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3529 return AST_MODULE_LOAD_DECLINE;
3531 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3533 if (ast_channel_register(&lcr_tech)) {
3534 CERROR(NULL, NULL, "Unable to register channel class\n");
3535 bchannel_deinitialize();
3538 #ifdef LCR_FOR_ASTERISK
3539 return AST_MODULE_LOAD_DECLINE;
3542 #ifdef LCR_FOR_CALLWEAVER
3547 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3549 #ifdef LCR_FOR_ASTERISK
3550 "lcr_config(<opt><optarg>:<opt>:...)\n"
3553 #ifdef LCR_FOR_CALLWEAVER
3554 "lcr_config(<opt><optarg>:<opt>:...)\n",
3557 "Sets LCR opts. and optargs\n"
3559 "The available options are:\n"
3560 " d - Send display text on called phone, text is the optarg.\n"
3561 " n - Don't detect dtmf tones on called channel.\n"
3562 " h - Force data call (HDLC).\n"
3563 " t - Disable mISDN_dsp features (required for fax application).\n"
3564 " q - Add queue to make fax stream seamless (required for fax app).\n"
3565 " Use queue size in miliseconds for optarg. (try 250)\n"
3566 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3567 " Use time to detect for optarg.\n"
3568 " c - Make crypted outgoing call, optarg is keyindex.\n"
3569 " e - Perform echo cancelation on this channel.\n"
3570 " Takes mISDN pipeline option as optarg.\n"
3571 " s - Send Non Inband DTMF as inband.\n"
3572 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3573 " vr - rxgain control\n"
3574 " vt - txgain control\n"
3575 " Volume changes at factor 2 ^ optarg.\n"
3576 " k - use keypad to dial this call.\n"
3578 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3579 " -> use 16 for fax (3.1k audio)\n"
3581 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3582 "options: \"n:t:q250\" for seamless audio transmission.\n"
3587 ast_cli_register(&cli_show_lcr);
3588 ast_cli_register(&cli_show_calls);
3589 ast_cli_register(&cli_reload_routing);
3590 ast_cli_register(&cli_reload_interfaces);
3591 ast_cli_register(&cli_port_block);
3592 ast_cli_register(&cli_port_unblock);
3593 ast_cli_register(&cli_port_unload);
3596 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3597 /* failed to create thread */
3598 bchannel_deinitialize();
3600 ast_channel_unregister(&lcr_tech);
3602 #ifdef LCR_FOR_ASTERISK
3603 return AST_MODULE_LOAD_DECLINE;
3606 #ifdef LCR_FOR_CALLWEAVER
3614 int unload_module(void)
3616 /* First, take us out of the channel loop */
3617 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3619 pthread_cancel(chan_tid);
3623 del_timer(&socket_retry);
3625 unregister_fd(&wake_fd);
3626 close(wake_pipe[0]);
3627 close(wake_pipe[1]);
3629 // ast_mutex_unlock(&chan_lock);
3631 ast_channel_unregister(&lcr_tech);
3633 ast_unregister_application("lcr_config");
3635 if (mISDN_created) {
3636 bchannel_deinitialize();
3640 if (lcr_sock >= 0) {
3645 #if ASTERISK_VERSION_NUM >= 100000
3646 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3651 int reload_module(void)
3657 #ifdef LCR_FOR_ASTERISK
3658 #define AST_MODULE "chan_lcr"
3661 #ifdef LCR_FOR_CALLWEAVER
3666 ast_mutex_lock(&usecnt_lock);
3668 ast_mutex_unlock(&usecnt_lock);
3673 #ifdef LCR_FOR_ASTERISK
3674 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3675 .load = load_module,
3676 .unload = unload_module,
3677 .reload = reload_module,
3681 #ifdef LCR_FOR_CALLWEAVER
3682 char *description(void)