1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
139 #include <asterisk/module.h>
140 #include <asterisk/channel.h>
141 #include <asterisk/config.h>
142 #include <asterisk/logger.h>
143 #include <asterisk/pbx.h>
144 #include <asterisk/options.h>
145 #include <asterisk/io.h>
146 #include <asterisk/frame.h>
147 #include <asterisk/translate.h>
148 #include <asterisk/cli.h>
149 #include <asterisk/musiconhold.h>
150 #include <asterisk/dsp.h>
151 #include <asterisk/translate.h>
152 #include <asterisk/file.h>
153 #ifdef LCR_FOR_ASTERISK
154 #include <asterisk/callerid.h>
156 #ifdef LCR_FOR_CALLWEAVER
157 #include <asterisk/phone_no_utils.h>
160 #include <asterisk/indications.h>
161 #include <asterisk/app.h>
162 #include <asterisk/features.h>
163 #include <asterisk/sched.h>
164 #include <asterisk/version.h>
166 #include "extension.h"
168 #include "callerid.h"
169 #include "lcrsocket.h"
172 #include "bchannel.h"
174 #include "chan_lcr.h"
176 CHAN_LCR_STATE // state description structure
177 MESSAGES // message text
179 #ifdef LCR_FOR_CALLWEAVER
180 AST_MUTEX_DEFINE_STATIC(rand_lock);
183 unsigned char flip_bits[256];
185 #ifdef LCR_FOR_CALLWEAVER
186 static struct ast_frame nullframe = { AST_FRAME_NULL, };
192 char lcr_type[]="lcr";
194 #ifdef LCR_FOR_CALLWEAVER
195 static ast_mutex_t usecnt_lock;
197 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
201 ast_mutex_t chan_lock; /* global lock */
202 ast_mutex_t log_lock; /* logging log */
204 * used to indicate change in file descriptors, so select function's result may
207 int global_change = 0;
210 struct lcr_fd wake_fd;
212 int glob_channel = 0;
215 struct lcr_fd socket_fd;
216 struct lcr_timer socket_retry;
219 struct admin_list *next;
220 struct admin_message msg;
221 } *admin_first = NULL;
223 static struct ast_channel_tech lcr_tech;
228 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
231 char call_text[128] = "NULL";
232 char ast_text[128] = "NULL";
235 ast_mutex_lock(&log_lock);
238 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
239 buffer[sizeof(buffer)-1]=0;
243 sprintf(call_text, "%d", call->ref);
245 #if ASTERISK_VERSION_NUM < 110000
246 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
248 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
250 ast_text[sizeof(ast_text)-1] = '\0';
252 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
253 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
255 ast_mutex_unlock(&log_lock);
259 * channel and call instances
261 struct chan_call *call_first;
265 * special case: 0: find new ref, that has not been assigned a ref yet
268 struct chan_call *find_call_ref(unsigned int ref)
270 struct chan_call *call = call_first;
271 int assigned = (ref > 0);
274 if (call->ref == ref && call->ref_was_assigned == assigned)
281 void free_call(struct chan_call *call)
283 struct chan_call **temp = &call_first;
287 *temp = (*temp)->next;
288 if (call->pipe[0] > -1)
289 close(call->pipe[0]);
290 if (call->pipe[1] > -1)
291 close(call->pipe[1]);
292 if (call->bchannel) {
293 if (call->bchannel->call != call)
294 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
295 call->bchannel->call = NULL;
297 if (call->bridge_call) {
298 if (call->bridge_call->bridge_call != call)
299 CERROR(call, NULL, "Linked call structure has no link to us.\n");
300 call->bridge_call->bridge_call = NULL;
303 ast_translator_free_path(call->trans);
305 ast_dsp_free(call->dsp);
306 CDEBUG(call, NULL, "Call instance freed.\n");
311 temp = &((*temp)->next);
313 CERROR(call, NULL, "Call instance not found in list.\n");
316 struct chan_call *alloc_call(void)
318 struct chan_call **callp = &call_first;
321 callp = &((*callp)->next);
323 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
325 memset(*callp, 0, sizeof(struct chan_call));
326 if (pipe((*callp)->pipe) < 0) {
327 CERROR(*callp, NULL, "Failed to create pipe.\n");
331 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
332 CDEBUG(*callp, NULL, "Call instance allocated.\n");
336 unsigned short new_bridge_id(void)
338 struct chan_call *call;
339 unsigned short id = 1;
341 /* search for lowest bridge id that is not in use and not 0 */
345 if (call->bridge_id == id)
353 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
358 * enque message to LCR
360 int send_message(int message_type, unsigned int ref, union parameter *param)
362 struct admin_list *admin, **adminp;
365 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
368 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
370 adminp = &admin_first;
372 adminp = &((*adminp)->next);
373 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
375 CERROR(NULL, NULL, "No memory for message to LCR.\n");
380 admin->msg.message = ADMIN_MESSAGE;
381 admin->msg.u.msg.type = message_type;
382 admin->msg.u.msg.ref = ref;
383 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
384 socket_fd.when |= LCR_FD_WRITE;
388 write(wake_pipe[1], &byte, 1);
395 * apply options (in locked state)
397 void apply_opt(struct chan_call *call, char *data)
399 union parameter newparam;
400 char string[1024], *p = string, *opt, *key;
406 strncpy(string, data, sizeof(string)-1);
407 string[sizeof(string)-1] = '\0';
410 while((opt = strsep(&p, ":"))) {
413 if (opt[1] == '\0') {
414 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
417 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
418 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
419 strncpy(call->display, opt+1, sizeof(call->display)-1);
421 memset(&newparam, 0, sizeof(union parameter));
422 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
423 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
427 if (opt[1] != '\0') {
428 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
431 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
432 if (call->dsp_dtmf) {
435 bchannel_dtmf(call->bchannel, 0);
439 if (opt[1] == '\0') {
440 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
444 /* check for 0xXXXX... type of key */
445 if (!!strncmp((char *)key, "0x", 2)) {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
450 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
451 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
456 if (*key>='0' && *key<='9')
457 call->bf_key[i] = (*key-'0') << 8;
458 else if (*key>='a' && *key<='f')
459 call->bf_key[i] = (*key-'a'+10) << 8;
460 else if (*key>='A' && *key<='F')
461 call->bf_key[i] = (*key-'A'+10) << 8;
465 if (*key>='0' && *key<='9')
466 call->bf_key[i] += (*key - '0');
467 else if (*key>='a' && *key<='f')
468 call->bf_key[i] += (*key - 'a' + 10);
469 else if (*key>='A' && *key<='F')
470 call->bf_key[i] += (*key - 'A' + 10);
477 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
481 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
483 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
486 if (opt[1] != '\0') {
487 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
490 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
495 if (opt[1] != '\0') {
496 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
499 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
504 if (opt[1] == '\0') {
505 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
508 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
509 call->nodsp_queue = atoi(opt+1);
512 if (opt[1] == '\0') {
513 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
516 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
517 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
519 bchannel_pipeline(call->bchannel, call->pipeline);
522 if (opt[1] == '\0') {
523 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
526 call->faxdetect=atoi(opt+1);
528 call->dsp=ast_dsp_new();
530 #ifdef LCR_FOR_CALLWEAVER
531 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
533 #ifdef LCR_FOR_ASTERISK
534 #ifdef DSP_FEATURE_DTMF_DETECT
535 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
537 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
542 #ifdef LCR_FOR_CALLWEAVER
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
545 #ifdef LCR_FOR_ASTERISK
546 #if ASTERISK_VERSION_NUM < 100000
547 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
549 struct ast_format src;
550 struct ast_format dst;
551 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
552 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
553 call->trans=ast_translator_build_path(&dst, &src);
558 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
561 if (opt[1] != '\0') {
562 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
565 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
570 if (opt[1] != '\0') {
571 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
574 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
575 call->inband_dtmf = 1;
578 if (opt[1] != 'r' && opt[1] != 't') {
579 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
583 if (gain < -8 || gain >8) {
584 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
587 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
589 call->rx_gain = gain;
591 bchannel_gain(call->bchannel, call->rx_gain, 0);
593 call->tx_gain = gain;
595 bchannel_gain(call->bchannel, call->tx_gain, 1);
599 if (opt[1] != '\0') {
600 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
603 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
608 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
612 /* re-open, if bchannel is created */
613 if (call->bchannel && call->bchannel->b_sock > -1) {
614 bchannel_destroy(call->bchannel);
615 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
616 bchannel_activate(call->bchannel, 1);
621 * send setup info to LCR
622 * this function is called, when asterisk call is received and ref is received
624 static void send_setup_to_lcr(struct chan_call *call)
626 union parameter newparam;
627 struct ast_channel *ast = call->ast;
630 if (!call->ast || !call->ref)
633 #ifdef AST_1_8_OR_HIGHER
634 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
636 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
639 /* send setup message to LCR */
640 memset(&newparam, 0, sizeof(union parameter));
641 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
642 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
644 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
646 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
647 if (!!strcmp(call->interface, "pbx"))
648 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
649 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
650 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
651 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
652 call->display[0] = '\0';
654 #ifdef AST_1_8_OR_HIGHER
655 /* set stored call information */
656 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
657 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
659 if (call->cid_num[0])
660 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
661 if (call->cid_name[0])
662 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
663 if (call->cid_rdnis[0]) {
664 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
665 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
666 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
668 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
669 case AST_PRES_RESTRICTED:
670 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
672 case AST_PRES_UNAVAILABLE:
673 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
675 case AST_PRES_ALLOWED:
677 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
679 switch(ast->cid.cid_ton) {
681 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
684 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
687 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
690 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
693 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
694 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
696 // ast->transfercapability = atoi(tmp);
697 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
698 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
700 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
702 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
703 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
705 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
706 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
707 send_message(MESSAGE_SETUP, call->ref, &newparam);
709 /* change to outgoing setup state */
710 call->state = CHAN_LCR_STATE_OUT_SETUP;
714 * send dialing info to LCR
715 * this function is called, when setup acknowledge is received and dialing
718 static void send_dialque_to_lcr(struct chan_call *call)
720 union parameter newparam;
722 if (!call->ast || !call->ref || !call->dialque[0])
725 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
727 /* send setup message to LCR */
728 memset(&newparam, 0, sizeof(union parameter));
730 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
732 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
733 call->dialque[0] = '\0';
734 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
738 * in case of a bridge, the unsupported message can be forwarded directly
739 * to the remote call.
741 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
745 if (!call->bridge_call) return;
746 CDEBUG(call, NULL, "Sending message due bridging.\n");
747 send_message(message_type, call->bridge_call->ref, param);
751 * send release message to LCR and import bchannel if exported
753 static void send_release_and_import(struct chan_call *call, int cause, int location)
755 union parameter newparam;
757 /* importing channel */
758 if (call->bchannel) {
759 memset(&newparam, 0, sizeof(union parameter));
760 newparam.bchannel.type = BCHANNEL_RELEASE;
761 newparam.bchannel.handle = call->bchannel->handle;
762 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
764 /* sending release */
765 memset(&newparam, 0, sizeof(union parameter));
766 newparam.disconnectinfo.cause = cause;
767 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
768 send_message(MESSAGE_RELEASE, call->ref, &newparam);
772 * check if extension matches and start asterisk
773 * if it can match, proceed
776 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
779 union parameter newparam;
780 #if ASTERISK_VERSION_NUM < 110000
781 char *exten = ast->exten;
783 char *exten = ast_channel_exten(ast);
788 #if ASTERISK_VERSION_NUM < 110000
789 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
791 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
796 #if ASTERISK_VERSION_NUM < 110000
797 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
798 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
800 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
801 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
806 #if ASTERISK_VERSION_NUM < 110000
807 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
808 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast->context);
810 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
811 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast_channel_context(ast));
816 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
817 /* send setup acknowledge to lcr */
818 memset(&newparam, 0, sizeof(union parameter));
819 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
822 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
827 #if ASTERISK_VERSION_NUM < 110000
828 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
830 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
832 /* send setup acknowledge to lcr */
833 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
834 memset(&newparam, 0, sizeof(union parameter));
835 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
839 call->state = CHAN_LCR_STATE_IN_DIALING;
841 /* if match, start pbx */
842 #if ASTERISK_VERSION_NUM < 110000
843 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
845 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
847 CDEBUG(call, ast, "Extensions matches.\n");
852 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
856 #if ASTERISK_VERSION_NUM < 110000
859 if (!*ast_channel_exten(ast)) {
862 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
870 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
871 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
873 /* release asterisk */
874 ast->hangupcause = call->cause;
875 /* change to release state */
876 call->state = CHAN_LCR_STATE_RELEASE;
877 ast_hangup(ast); // call will be destroyed here
881 /* send setup to asterisk */
882 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
884 #ifdef LCR_FOR_CALLWEAVER
886 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
889 ret = ast_pbx_start(ast);
891 cause = (ret==-2)?34:27;
894 call->pbx_started = 1;
895 ast_setstate(ast, AST_STATE_RING);
899 * incoming setup from LCR
901 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
903 struct ast_channel *ast;
905 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
907 /* create asterisk channel instrance */
909 #ifdef LCR_FOR_CALLWEAVER
910 ast = ast_channel_alloc(1);
913 #ifdef LCR_FOR_ASTERISK
914 #ifdef AST_1_8_OR_HIGHER
915 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
917 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
923 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
924 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
931 ast->tech_pvt = call;
932 ast->tech = &lcr_tech;
933 ast->fds[0] = call->pipe[0];
935 /* fill setup information */
936 if (param->setup.dialinginfo.id)
937 #if ASTERISK_VERSION_NUM < 110000
938 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
939 if (param->setup.context[0])
940 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
942 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
944 strncpy(ast_channel_exten(ast), param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
945 if (param->setup.context[0])
946 strncpy(ast_channel_context(ast), param->setup.context, AST_MAX_CONTEXT-1);
948 strncpy(ast_channel_context(ast), param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
952 #ifdef AST_1_8_OR_HIGHER
953 if (param->setup.callerinfo.id[0]) {
954 ast->caller.id.number.valid = 1;
955 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
956 if (!param->setup.callerinfo.id[0]) {
957 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
958 ast->caller.id.number.plan = (0 << 4) | 1;
960 switch (param->setup.callerinfo.present) {
961 case INFO_PRESENT_ALLOWED:
962 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
964 case INFO_PRESENT_RESTRICTED:
965 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
968 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
970 switch (param->setup.callerinfo.screen) {
971 case INFO_SCREEN_USER:
972 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
974 case INFO_SCREEN_USER_VERIFIED_PASSED:
975 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
977 case INFO_SCREEN_USER_VERIFIED_FAILED:
978 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
981 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
983 switch (param->setup.callerinfo.ntype) {
984 case INFO_NTYPE_SUBSCRIBER:
985 ast->caller.id.number.plan = (4 << 4) | 1;
987 case INFO_NTYPE_NATIONAL:
988 ast->caller.id.number.plan = (2 << 4) | 1;
990 case INFO_NTYPE_INTERNATIONAL:
991 ast->caller.id.number.plan = (1 << 4) | 1;
994 ast->caller.id.number.plan = (0 << 4) | 1;
997 if (param->setup.callerinfo.id2[0]) {
998 ast->caller.ani.number.valid = 1;
999 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
1000 switch (param->setup.callerinfo.present2) {
1001 case INFO_PRESENT_ALLOWED:
1002 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
1004 case INFO_PRESENT_RESTRICTED:
1005 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
1008 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
1010 switch (param->setup.callerinfo.screen2) {
1011 case INFO_SCREEN_USER:
1012 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1014 case INFO_SCREEN_USER_VERIFIED_PASSED:
1015 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1017 case INFO_SCREEN_USER_VERIFIED_FAILED:
1018 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1021 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1023 switch (param->setup.callerinfo.ntype2) {
1024 case INFO_NTYPE_SUBSCRIBER:
1025 ast->caller.ani.number.plan = (4 << 4) | 1;
1027 case INFO_NTYPE_NATIONAL:
1028 ast->caller.ani.number.plan = (2 << 4) | 1;
1030 case INFO_NTYPE_INTERNATIONAL:
1031 ast->caller.ani.number.plan = (1 << 4) | 1;
1034 ast->caller.ani.number.plan = (0 << 4) | 1;
1037 if (param->setup.callerinfo.name[0]) {
1038 ast->caller.id.name.valid = 1;
1039 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
1041 if (param->setup.redirinfo.id[0]) {
1042 ast->redirecting.from.number.valid = 1;
1043 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
1044 switch (param->setup.redirinfo.present) {
1045 case INFO_PRESENT_ALLOWED:
1046 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
1048 case INFO_PRESENT_RESTRICTED:
1049 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1052 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1054 switch (param->setup.redirinfo.screen) {
1055 case INFO_SCREEN_USER:
1056 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1058 case INFO_SCREEN_USER_VERIFIED_PASSED:
1059 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1061 case INFO_SCREEN_USER_VERIFIED_FAILED:
1062 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1065 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1067 switch (param->setup.redirinfo.ntype) {
1068 case INFO_NTYPE_SUBSCRIBER:
1069 ast->redirecting.from.number.plan = (4 << 4) | 1;
1071 case INFO_NTYPE_NATIONAL:
1072 ast->redirecting.from.number.plan = (2 << 4) | 1;
1074 case INFO_NTYPE_INTERNATIONAL:
1075 ast->redirecting.from.number.plan = (1 << 4) | 1;
1078 ast->redirecting.from.number.plan = (0 << 4) | 1;
1082 memset(&ast->cid, 0, sizeof(ast->cid));
1083 if (param->setup.callerinfo.id[0])
1084 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1085 if (param->setup.callerinfo.id2[0])
1086 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1087 if (param->setup.callerinfo.name[0])
1088 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1089 if (param->setup.redirinfo.id[0])
1090 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1091 switch (param->setup.callerinfo.present) {
1092 case INFO_PRESENT_ALLOWED:
1093 ast->cid.cid_pres = AST_PRES_ALLOWED;
1095 case INFO_PRESENT_RESTRICTED:
1096 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1099 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1101 switch (param->setup.callerinfo.ntype) {
1102 case INFO_NTYPE_SUBSCRIBER:
1103 ast->cid.cid_ton = 4;
1105 case INFO_NTYPE_NATIONAL:
1106 ast->cid.cid_ton = 2;
1108 case INFO_NTYPE_INTERNATIONAL:
1109 ast->cid.cid_ton = 1;
1112 ast->cid.cid_ton = 0;
1116 ast->transfercapability = param->setup.capainfo.bearer_capa;
1117 /* enable hdlc if transcap is data */
1118 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1120 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1122 /* configure channel */
1123 #if ASTERISK_VERSION_NUM < 100000
1124 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1125 ast->readformat = ast->rawreadformat = ast->nativeformats;
1126 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1128 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1129 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1130 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1131 ast_set_write_format(ast, &ast->rawwriteformat);
1132 ast_set_read_format(ast, &ast->rawreadformat);
1135 ast->hangupcause = 0;
1138 call->state = CHAN_LCR_STATE_IN_SETUP;
1140 if (!call->pbx_started)
1141 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1145 * incoming setup acknowledge from LCR
1147 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1149 if (!call->ast) return;
1151 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1153 /* send pending digits in dialque */
1154 if (call->dialque[0])
1155 send_dialque_to_lcr(call);
1156 /* change to overlap state */
1157 call->state = CHAN_LCR_STATE_OUT_DIALING;
1161 * incoming proceeding from LCR
1163 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1165 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1168 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1169 /* queue event for asterisk */
1170 if (call->ast && call->pbx_started) {
1174 write(wake_pipe[1], &byte, 1);
1176 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1182 * incoming alerting from LCR
1184 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1186 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1189 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1190 /* queue event to asterisk */
1191 if (call->ast && call->pbx_started) {
1195 write(wake_pipe[1], &byte, 1);
1197 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1202 * incoming connect from LCR
1204 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1206 union parameter newparam;
1208 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1211 call->state = CHAN_LCR_STATE_CONNECT;
1212 /* request bchannel */
1213 if (!call->bchannel) {
1214 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1215 memset(&newparam, 0, sizeof(union parameter));
1216 newparam.bchannel.type = BCHANNEL_REQUEST;
1217 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1219 /* copy connectinfo */
1220 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1221 /* queue event to asterisk */
1222 if (call->ast && call->pbx_started) {
1226 write(wake_pipe[1], &byte, 1);
1228 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1233 * incoming disconnect from LCR
1235 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1237 struct ast_channel *ast = call->ast;
1239 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1242 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1244 call->cause = param->disconnectinfo.cause;
1245 call->location = param->disconnectinfo.location;
1246 /* if bridge, forward disconnect and return */
1249 if (call->bridge_call) {
1250 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1251 bridge_message_if_bridged(call, message_type, param);
1255 /* release lcr with same cause */
1256 send_release_and_import(call, call->cause, call->location);
1258 /* change to release state */
1259 call->state = CHAN_LCR_STATE_RELEASE;
1260 /* queue release asterisk */
1262 ast->hangupcause = call->cause;
1263 if (call->pbx_started) {
1267 write(wake_pipe[1], &byte, 1);
1269 strcpy(call->queue_string, "H"); // overwrite other indications
1271 ast_hangup(ast); // call will be destroyed here
1277 * incoming release from LCR
1279 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1281 struct ast_channel *ast = call->ast;
1283 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1287 /* change to release state */
1288 call->state = CHAN_LCR_STATE_RELEASE;
1289 /* copy release info */
1291 call->cause = param->disconnectinfo.cause;
1292 call->location = param->disconnectinfo.location;
1294 /* if we have an asterisk instance, queue hangup, else we are done */
1296 ast->hangupcause = call->cause;
1297 if (call->pbx_started) {
1301 write(wake_pipe[1], &byte, 1);
1303 strcpy(call->queue_string, "H");
1305 ast_hangup(ast); // call will be destroyed here
1314 * incoming information from LCR
1316 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1318 struct ast_channel *ast = call->ast;
1320 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1324 /* pbx not started */
1325 if (!call->pbx_started) {
1326 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1327 #if ASTERISK_VERSION_NUM < 110000
1328 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1330 ast_channel_exten_set(ast, param->information.id);
1332 lcr_start_pbx(call, ast, param->information.sending_complete);
1336 /* change dailing state after setup */
1337 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1338 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1339 call->state = CHAN_LCR_STATE_IN_DIALING;
1340 // ast_setstate(ast, AST_STATE_DIALING);
1344 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1348 write(wake_pipe[1], &byte, 1);
1350 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1353 /* use bridge to forware message not supported by asterisk */
1354 if (call->state == CHAN_LCR_STATE_CONNECT) {
1355 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1356 bridge_message_if_bridged(call, message_type, param);
1361 * incoming information from LCR
1363 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1365 union parameter newparam;
1367 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1369 /* request bchannel, if call is resumed and we don't have it */
1370 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1371 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1372 memset(&newparam, 0, sizeof(union parameter));
1373 newparam.bchannel.type = BCHANNEL_REQUEST;
1374 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1377 if (!call->ast) return;
1379 /* use bridge to forware message not supported by asterisk */
1380 bridge_message_if_bridged(call, message_type, param);
1384 * incoming information from LCR
1386 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1388 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1390 if (!call->ast) return;
1392 /* use bridge to forware message not supported by asterisk */
1393 bridge_message_if_bridged(call, message_type, param);
1397 * incoming pattern from LCR
1399 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1401 union parameter newparam;
1403 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1405 if (!call->ast) return;
1407 /* pattern are indicated only once */
1408 if (call->has_pattern)
1410 call->has_pattern = 1;
1412 /* request bchannel */
1413 if (!call->bchannel) {
1414 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1415 memset(&newparam, 0, sizeof(union parameter));
1416 newparam.bchannel.type = BCHANNEL_REQUEST;
1417 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1419 /* queue PROGRESS, because tones are available */
1420 if (call->ast && call->pbx_started) {
1424 write(wake_pipe[1], &byte, 1);
1426 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1431 * got dtmf from bchannel (locked state)
1433 void lcr_in_dtmf(struct chan_call *call, int val)
1435 struct ast_channel *ast = call->ast;
1440 if (!call->pbx_started)
1443 if (!call->dsp_dtmf) {
1444 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1448 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1454 write(wake_pipe[1], &byte, 1);
1456 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1460 * message received from LCR
1462 int receive_message(int message_type, unsigned int ref, union parameter *param)
1464 struct bchannel *bchannel;
1465 struct chan_call *call;
1466 union parameter newparam;
1468 memset(&newparam, 0, sizeof(union parameter));
1470 /* handle bchannel message*/
1471 if (message_type == MESSAGE_BCHANNEL) {
1472 switch(param->bchannel.type) {
1473 case BCHANNEL_ASSIGN:
1474 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1475 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1476 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1479 /* create bchannel */
1480 bchannel = alloc_bchannel(param->bchannel.handle);
1482 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1486 /* configure channel */
1487 bchannel->b_tx_gain = param->bchannel.tx_gain;
1488 bchannel->b_rx_gain = param->bchannel.rx_gain;
1489 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1490 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1491 bchannel->b_bf_len = param->bchannel.crypt_len;
1492 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1494 bchannel->b_txdata = 0;
1495 bchannel->b_tx_dejitter = 1;
1497 /* in case, ref is not set, this bchannel instance must
1498 * be created until it is removed again by LCR */
1500 call = find_call_ref(ref);
1502 bchannel->call = call;
1503 call->bchannel = bchannel;
1505 bchannel_dtmf(bchannel, 1);
1507 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1508 if (call->pipeline[0])
1509 bchannel_pipeline(bchannel, call->pipeline);
1511 bchannel_gain(bchannel, call->rx_gain, 0);
1513 bchannel_gain(bchannel, call->tx_gain, 1);
1514 if (call->bridge_id) {
1515 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1516 bchannel_join(bchannel, call->bridge_id);
1518 /* ignore all dsp features, if it is a loopback interface */
1519 if (param->bchannel.isloopback)
1522 /* create only, if call exists, othewhise it bchannel is freed below... */
1523 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1524 bchannel_activate(bchannel, 1);
1527 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1528 newparam.bchannel.handle = param->bchannel.handle;
1529 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1530 /* if call has released before bchannel is assigned */
1532 newparam.bchannel.type = BCHANNEL_RELEASE;
1533 newparam.bchannel.handle = param->bchannel.handle;
1534 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1539 case BCHANNEL_REMOVE:
1540 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1541 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1542 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1545 /* unklink from call and destroy bchannel */
1546 free_bchannel(bchannel);
1549 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1550 newparam.bchannel.handle = param->bchannel.handle;
1551 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1556 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1561 /* handle new ref */
1562 if (message_type == MESSAGE_NEWREF) {
1563 if (param->newref.direction) {
1564 /* new ref from lcr */
1565 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1566 if (!ref || find_call_ref(ref)) {
1567 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1570 /* allocate new call instance */
1571 call = alloc_call();
1573 call->state = CHAN_LCR_STATE_IN_PREPARE;
1576 call->ref_was_assigned = 1;
1577 /* set dtmf (default, use option 'n' to disable */
1579 /* wait for setup (or release from asterisk) */
1581 /* new ref, as requested from this remote application */
1582 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1583 call = find_call_ref(0);
1585 /* send release, if ref does not exist */
1586 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1591 call->ref_was_assigned = 1;
1592 /* set dtmf (default, use option 'n' to disable */
1594 /* send pending setup info */
1595 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1596 send_setup_to_lcr(call);
1597 /* release if asterisk has signed off */
1598 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1601 send_release_and_import(call, call->cause, call->location);
1603 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1614 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1617 call = find_call_ref(ref);
1619 /* ignore ref that is not used (anymore) */
1620 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1624 /* handle messages */
1625 switch(message_type) {
1627 lcr_in_setup(call, message_type, param);
1630 case MESSAGE_OVERLAP:
1631 lcr_in_overlap(call, message_type, param);
1634 case MESSAGE_PROCEEDING:
1635 lcr_in_proceeding(call, message_type, param);
1638 case MESSAGE_ALERTING:
1639 lcr_in_alerting(call, message_type, param);
1642 case MESSAGE_CONNECT:
1643 lcr_in_connect(call, message_type, param);
1646 case MESSAGE_DISCONNECT:
1647 lcr_in_disconnect(call, message_type, param);
1650 case MESSAGE_RELEASE:
1651 lcr_in_release(call, message_type, param);
1654 case MESSAGE_INFORMATION:
1655 lcr_in_information(call, message_type, param);
1658 case MESSAGE_NOTIFY:
1659 lcr_in_notify(call, message_type, param);
1662 case MESSAGE_FACILITY:
1663 lcr_in_facility(call, message_type, param);
1666 case MESSAGE_PATTERN: // audio available from LCR
1667 if (!call->has_pattern)
1668 lcr_in_pattern(call, message_type, param);
1671 case MESSAGE_NOPATTERN: // audio not available from LCR
1674 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1675 call->audiopath = param->audiopath;
1679 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1686 * release all calls (due to broken socket)
1688 static void release_all_calls(void)
1690 struct chan_call *call;
1695 /* no ast, so we may directly free call */
1697 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1701 /* already in release process */
1702 if (call->state == CHAN_LCR_STATE_RELEASE) {
1706 /* release or queue release */
1708 call->state = CHAN_LCR_STATE_RELEASE;
1709 if (!call->pbx_started) {
1710 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1711 ast_hangup(call->ast); // call will be destroyed here
1714 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1718 write(wake_pipe[1], &byte, 1);
1720 strcpy(call->queue_string, "H");
1724 /* release all bchannels */
1725 while(bchannel_first)
1726 free_bchannel(bchannel_first);
1729 void close_socket(void);
1732 * warning! not thread safe
1733 * returns -1 for socket error, 0 for no work, 1 for work
1735 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1738 struct admin_list *admin;
1739 struct admin_message msg;
1741 if ((what & LCR_FD_READ)) {
1742 /* read from socket */
1743 len = read(lcr_sock, &msg, sizeof(msg));
1745 CERROR(NULL, NULL, "Socket closed.(read)\n");
1747 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1749 release_all_calls();
1750 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1754 if (len != sizeof(msg)) {
1755 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1758 if (msg.message != ADMIN_MESSAGE) {
1759 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1762 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1764 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1769 if ((what & LCR_FD_WRITE)) {
1770 /* write to socket */
1772 socket_fd.when &= ~LCR_FD_WRITE;
1775 admin = admin_first;
1776 len = write(lcr_sock, &admin->msg, sizeof(msg));
1778 CERROR(NULL, NULL, "Socket closed.(write)\n");
1782 if (len != sizeof(msg)) {
1783 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1787 admin_first = admin->next;
1791 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1800 * open and close socket and thread
1802 int open_socket(void)
1805 struct sockaddr_un sock_address;
1806 union parameter param;
1809 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1810 CERROR(NULL, NULL, "Failed to create socket.\n");
1814 /* set socket address and name */
1815 memset(&sock_address, 0, sizeof(sock_address));
1816 sock_address.sun_family = PF_UNIX;
1817 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1819 /* connect socket */
1820 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1823 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1827 /* register socket fd */
1828 memset(&socket_fd, 0, sizeof(socket_fd));
1829 socket_fd.fd = lcr_sock;
1830 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1832 /* enque hello message */
1833 memset(¶m, 0, sizeof(param));
1834 strcpy(param.hello.application, "asterisk");
1835 send_message(MESSAGE_HELLO, 0, ¶m);
1840 void close_socket(void)
1842 struct admin_list *admin, *temp;
1844 /* socket not created */
1848 unregister_fd(&socket_fd);
1850 /* flush pending messages */
1851 admin = admin_first;
1854 admin = admin->next;
1866 /* sending queue to asterisk */
1867 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1871 read(wake_pipe[0], &byte, 1);
1878 static void handle_queue()
1880 struct chan_call *call;
1881 struct ast_channel *ast;
1882 struct ast_frame fr;
1888 p = call->queue_string;
1891 if (ast_channel_trylock(ast)) {
1892 ast_mutex_unlock(&chan_lock);
1894 ast_mutex_lock(&chan_lock);
1900 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1901 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1904 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1905 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1908 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1909 ast_queue_control(ast, AST_CONTROL_RINGING);
1910 ast_setstate(ast, AST_STATE_RINGING);
1913 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1914 ast_queue_control(ast, AST_CONTROL_ANSWER);
1917 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1918 ast_queue_hangup(ast);
1920 case '1': case '2': case '3': case 'A':
1921 case '4': case '5': case '6': case 'B':
1922 case '7': case '8': case '9': case 'C':
1923 case '*': case '0': case '#': case 'D':
1924 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1925 /* send digit to asterisk */
1926 memset(&fr, 0, sizeof(fr));
1928 #ifdef LCR_FOR_ASTERISK
1929 fr.frametype = AST_FRAME_DTMF_BEGIN;
1932 #ifdef LCR_FOR_CALLWEAVER
1933 fr.frametype = AST_FRAME_DTMF;
1936 #ifdef AST_1_8_OR_HIGHER
1937 fr.subclass.integer = *p;
1941 fr.delivery = ast_tv(0, 0);
1942 ast_queue_frame(ast, &fr);
1944 #ifdef LCR_FOR_ASTERISK
1945 fr.frametype = AST_FRAME_DTMF_END;
1946 ast_queue_frame(ast, &fr);
1951 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1955 call->queue_string[0] = '\0';
1956 ast_channel_unlock(ast);
1962 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1964 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1965 if (open_socket() < 0)
1966 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1971 void lock_chan(void)
1973 ast_mutex_lock(&chan_lock);
1976 void unlock_chan(void)
1978 ast_mutex_unlock(&chan_lock);
1981 /* chan_lcr thread */
1982 static void *chan_thread(void *arg)
1984 if (pipe(wake_pipe) < 0) {
1985 CERROR(NULL, NULL, "Failed to open pipe.\n");
1988 memset(&wake_fd, 0, sizeof(wake_fd));
1989 wake_fd.fd = wake_pipe[0];
1990 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1992 memset(&socket_retry, 0, sizeof(socket_retry));
1993 add_timer(&socket_retry, handle_retry, NULL, 0);
1995 bchannel_pid = getpid();
1997 /* open socket the first time */
1998 handle_retry(NULL, NULL, 0);
2000 ast_mutex_lock(&chan_lock);
2004 select_main(0, &global_change, lock_chan, unlock_chan);
2011 * new asterisk instance
2014 #ifdef AST_1_8_OR_HIGHER
2015 #if ASTERISK_VERSION_NUM < 100000
2016 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2018 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2021 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2024 char exten[256], *dial, *interface, *opt;
2025 struct ast_channel *ast;
2026 struct chan_call *call;
2028 ast_mutex_lock(&chan_lock);
2029 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2031 /* if socket is closed */
2033 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2034 ast_mutex_unlock(&chan_lock);
2038 /* create call instance */
2039 call = alloc_call();
2041 /* failed to create instance */
2042 ast_mutex_unlock(&chan_lock);
2046 /* create asterisk channel instrance */
2048 #ifdef LCR_FOR_ASTERISK
2049 #ifdef AST_1_8_OR_HIGHER
2050 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2052 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2056 #ifdef LCR_FOR_CALLWEAVER
2057 ast = ast_channel_alloc(1);
2061 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2063 /* failed to create instance */
2064 ast_mutex_unlock(&chan_lock);
2067 ast->tech = &lcr_tech;
2068 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2069 /* configure channel */
2070 #if ASTERISK_VERSION_NUM < 100000
2071 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2072 ast->readformat = ast->rawreadformat = ast->nativeformats;
2073 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2075 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2076 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2077 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2078 ast_set_write_format(ast, &ast->rawwriteformat);
2079 ast_set_read_format(ast, &ast->rawreadformat);
2082 ast->hangupcause = 0;
2086 ast->tech_pvt = call;
2087 ast->fds[0] = call->pipe[0];
2088 call->pbx_started = 0;
2090 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2093 * Extract interface, dialstring, options from data.
2096 * <interface>/<dialstring>
2097 * <interface>/<dialstring>/options
2099 strncpy(exten, (char *)data, sizeof(exten)-1);
2100 exten[sizeof(exten)-1] = '\0';
2101 if ((dial = strchr(exten, '/'))) {
2104 if ((opt = strchr(dial, '/')))
2113 strncpy(call->interface, interface, sizeof(call->interface)-1);
2114 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2115 apply_opt(call, (char *)opt);
2117 #ifdef AST_1_8_OR_HIGHER
2118 // clone_variables(requestor, ast);
2121 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2122 if (requestor->caller.ani.number.valid)
2123 if (requestor->caller.ani.number.str)
2124 if (requestor->caller.ani.number.str[0])
2125 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2126 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2127 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2129 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2130 if (requestor->caller.ani.name.valid)
2131 if (requestor->caller.ani.name.str)
2132 if (requestor->caller.ani.name.str[0])
2133 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2134 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2136 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2137 if (requestor->caller.ani.subaddress.valid)
2138 if (requestor->caller.ani.subaddress.str)
2139 if (requestor->caller.ani.subaddress.str[0])
2140 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2141 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2143 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2144 if (requestor->caller.id.number.valid)
2145 if (requestor->caller.id.number.str)
2146 if (requestor->caller.id.number.str[0])
2147 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2148 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2149 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2151 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2152 if (requestor->caller.id.name.valid)
2153 if (requestor->caller.id.name.str)
2154 if (requestor->caller.id.name.str[0])
2155 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2156 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2158 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2159 if (requestor->caller.id.subaddress.valid)
2160 if (requestor->caller.id.subaddress.str)
2161 if (requestor->caller.id.subaddress.str[0])
2162 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2163 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2165 if (requestor->dialed.number.str)
2166 if (requestor->dialed.number.str[0])
2167 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2168 ast->dialed.number.plan= requestor->dialed.number.plan;
2170 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2171 if (requestor->dialed.subaddress.valid)
2172 if (requestor->dialed.subaddress.str)
2173 if (requestor->dialed.subaddress.str[0])
2174 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2175 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2177 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2178 ast->redirecting.count= requestor->redirecting.count;
2179 ast->redirecting.reason= requestor->redirecting.reason;
2181 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2182 if (requestor->redirecting.from.number.valid)
2183 if (requestor->redirecting.from.number.str)
2184 if (requestor->redirecting.from.number.str[0])
2185 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2186 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2187 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2189 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2190 if (requestor->redirecting.to.number.valid)
2191 if (requestor->redirecting.to.number.str)
2192 if (requestor->redirecting.to.number.str[0])
2193 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2194 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2195 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2197 /* store call information for setup */
2200 if (requestor && requestor->caller.id.number.valid) {
2201 if (requestor->caller.id.number.str)
2202 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2203 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2204 case AST_PRES_RESTRICTED:
2205 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2207 case AST_PRES_UNAVAILABLE:
2208 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2210 case AST_PRES_ALLOWED:
2212 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2214 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2215 case AST_PRES_USER_NUMBER_UNSCREENED:
2216 call->callerinfo.screen = INFO_SCREEN_USER;
2218 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2219 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2221 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2222 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2225 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2227 switch((requestor->caller.id.number.plan >> 4) & 7) {
2229 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2232 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2235 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2238 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2241 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2244 if (requestor && requestor->caller.ani.number.valid) {
2245 if (requestor->caller.ani.number.str)
2246 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2247 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2248 case AST_PRES_RESTRICTED:
2249 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2251 case AST_PRES_UNAVAILABLE:
2252 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2254 case AST_PRES_ALLOWED:
2256 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2258 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2259 case AST_PRES_USER_NUMBER_UNSCREENED:
2260 call->callerinfo.screen2 = INFO_SCREEN_USER;
2262 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2263 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2265 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2266 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2269 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2271 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2273 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2276 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2279 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2282 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2285 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2288 if (requestor && requestor->caller.id.name.valid) {
2289 if (requestor->caller.id.name.str)
2290 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2294 if (requestor && requestor->redirecting.from.number.valid) {
2295 call->redirinfo.itype = INFO_ITYPE_CHAN;
2296 if (requestor->redirecting.from.number.str)
2297 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2298 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2299 case AST_PRES_RESTRICTED:
2300 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2302 case AST_PRES_UNAVAILABLE:
2303 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2305 case AST_PRES_ALLOWED:
2307 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2309 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2310 case AST_PRES_USER_NUMBER_UNSCREENED:
2311 call->redirinfo.screen = INFO_SCREEN_USER;
2313 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2314 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2316 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2317 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2320 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2322 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2324 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2327 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2330 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2333 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2338 ast_mutex_unlock(&chan_lock);
2343 * call from asterisk
2345 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2347 union parameter newparam;
2348 struct chan_call *call;
2350 ast_mutex_lock(&chan_lock);
2351 call = ast->tech_pvt;
2353 #ifdef LCR_FOR_CALLWEAVER
2355 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2359 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2360 ast_mutex_unlock(&chan_lock);
2364 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2366 /* pbx process is started */
2367 call->pbx_started = 1;
2368 /* send MESSAGE_NEWREF */
2369 memset(&newparam, 0, sizeof(union parameter));
2370 newparam.newref.direction = 0; /* request from app */
2371 if (!strcmp(call->interface, "pbx"))
2372 newparam.newref.mode = 1;
2373 send_message(MESSAGE_NEWREF, 0, &newparam);
2375 /* set hdlc if capability requires hdlc */
2376 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2377 || ast->transfercapability == INFO_BC_DATARESTRICTED
2378 || ast->transfercapability == INFO_BC_VIDEO)
2380 /* if hdlc is forced by option, we change transcap to data */
2382 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2383 && ast->transfercapability != INFO_BC_DATARESTRICTED
2384 && ast->transfercapability != INFO_BC_VIDEO)
2385 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2387 #ifndef AST_1_8_OR_HIGHER
2388 call->cid_num[0] = 0;
2389 call->cid_name[0] = 0;
2390 call->cid_rdnis[0] = 0;
2392 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2393 strncpy(call->cid_num, ast->cid.cid_num,
2394 sizeof(call->cid_num)-1);
2395 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2396 strncpy(call->cid_name, ast->cid.cid_name,
2397 sizeof(call->cid_name)-1);
2398 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2399 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2400 sizeof(call->cid_rdnis)-1);
2403 ast_mutex_unlock(&chan_lock);
2407 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2409 static const char* dtmf_tones[] = {
2410 "!941+1336/100,!0/100", /* 0 */
2411 "!697+1209/100,!0/100", /* 1 */
2412 "!697+1336/100,!0/100", /* 2 */
2413 "!697+1477/100,!0/100", /* 3 */
2414 "!770+1209/100,!0/100", /* 4 */
2415 "!770+1336/100,!0/100", /* 5 */
2416 "!770+1477/100,!0/100", /* 6 */
2417 "!852+1209/100,!0/100", /* 7 */
2418 "!852+1336/100,!0/100", /* 8 */
2419 "!852+1477/100,!0/100", /* 9 */
2420 "!697+1633/100,!0/100", /* A */
2421 "!770+1633/100,!0/100", /* B */
2422 "!852+1633/100,!0/100", /* C */
2423 "!941+1633/100,!0/100", /* D */
2424 "!941+1209/100,!0/100", /* * */
2425 "!941+1477/100,!0/100" }; /* # */
2427 if (digit >= '0' && digit <='9')
2428 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2429 else if (digit >= 'A' && digit <= 'D')
2430 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2431 else if (digit == '*')
2432 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2433 else if (digit == '#')
2434 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2436 #if ASTERISK_VERSION_NUM < 110000
2437 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2439 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2444 #ifdef LCR_FOR_ASTERISK
2445 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2447 #ifdef LCR_FOR_CALLWEAVER
2448 static int lcr_digit(struct ast_channel *ast, char digit)
2451 struct chan_call *call;
2452 union parameter newparam;
2455 #ifdef LCR_FOR_CALLWEAVER
2456 int inband_dtmf = 0;
2459 /* only pass IA5 number space */
2460 if (digit > 126 || digit < 32)
2463 ast_mutex_lock(&chan_lock);
2464 call = ast->tech_pvt;
2466 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2467 ast_mutex_unlock(&chan_lock);
2471 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2473 /* send information or queue them */
2474 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2475 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2476 memset(&newparam, 0, sizeof(union parameter));
2478 newparam.information.keypad[0] = digit;
2479 newparam.information.keypad[1] = '\0';
2481 newparam.information.id[0] = digit;
2482 newparam.information.id[1] = '\0';
2484 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2487 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2488 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2490 strncat(call->dialque, buf, strlen(call->dialque)-1);
2493 ast_mutex_unlock(&chan_lock);
2495 #ifdef LCR_FOR_ASTERISK
2499 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2501 int inband_dtmf = 0;
2502 struct chan_call *call;
2505 ast_mutex_lock(&chan_lock);
2507 call = ast->tech_pvt;
2511 "Received digit from Asterisk, "
2512 "but no call instance exists.\n");
2513 ast_mutex_unlock(&chan_lock);
2517 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2519 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2523 ast_mutex_unlock(&chan_lock);
2526 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2527 send_digit_to_chan(ast, digit);
2533 static int lcr_answer(struct ast_channel *ast)
2535 union parameter newparam;
2536 struct chan_call *call;
2538 ast_mutex_lock(&chan_lock);
2539 call = ast->tech_pvt;
2541 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2542 ast_mutex_unlock(&chan_lock);
2546 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2548 /* copy connectinfo, if bridged */
2549 if (call->bridge_call)
2550 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2551 /* send connect message to lcr */
2552 if (call->state != CHAN_LCR_STATE_CONNECT) {
2553 memset(&newparam, 0, sizeof(union parameter));
2554 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2555 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2556 call->state = CHAN_LCR_STATE_CONNECT;
2559 /* request bchannel */
2560 if (!call->bchannel) {
2561 CDEBUG(call, ast, "Requesting B-channel.\n");
2562 memset(&newparam, 0, sizeof(union parameter));
2563 newparam.bchannel.type = BCHANNEL_REQUEST;
2564 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2567 // memset(&newparam, 0, sizeof(union parameter));
2568 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2570 ast_mutex_unlock(&chan_lock);
2574 static int lcr_hangup(struct ast_channel *ast)
2576 struct chan_call *call;
2577 pthread_t tid = pthread_self();
2579 if (!pthread_equal(tid, chan_tid)) {
2580 ast_mutex_lock(&chan_lock);
2582 call = ast->tech_pvt;
2584 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2585 if (!pthread_equal(tid, chan_tid)) {
2586 ast_mutex_unlock(&chan_lock);
2591 if (!pthread_equal(tid, chan_tid))
2592 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2594 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2596 /* disconnect asterisk, maybe not required */
2597 ast->tech_pvt = NULL;
2601 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2602 if (ast->hangupcause > 0)
2603 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2605 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2608 if (!pthread_equal(tid, chan_tid)) {
2609 ast_mutex_unlock(&chan_lock);
2613 /* ref is not set, due to prepare setup or release */
2614 if (call->state == CHAN_LCR_STATE_RELEASE) {
2615 /* we get the response to our release */
2616 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2619 /* during prepare, we change to release state */
2620 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2621 call->state = CHAN_LCR_STATE_RELEASE;
2625 if (!pthread_equal(tid, chan_tid)) {
2626 ast_mutex_unlock(&chan_lock);
2631 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2633 struct chan_call *call;
2634 struct ast_frame * f = fr;
2636 #if ASTERISK_VERSION_NUM < 100000
2637 #ifdef AST_1_8_OR_HIGHER
2638 if (!f->subclass.codec)
2642 CDEBUG(NULL, ast, "No subclass\n");
2644 #ifdef AST_1_8_OR_HIGHER
2645 #if ASTERISK_VERSION_NUM < 100000
2646 if (!(f->subclass.codec & ast->nativeformats)) {
2648 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2651 if (!(f->subclass & ast->nativeformats)) {
2654 "Unexpected format. "
2655 "Activating emergency conversion...\n");
2657 #ifdef AST_1_8_OR_HIGHER
2658 #if ASTERISK_VERSION_NUM < 100000
2659 ast_set_write_format(ast, f->subclass.codec);
2661 ast_set_write_format(ast, &f->subclass.format);
2664 ast_set_write_format(ast, f->subclass);
2666 f = (ast->writetrans) ? ast_translate(
2667 ast->writetrans, fr, 0) : fr;
2670 ast_mutex_lock(&chan_lock);
2671 call = ast->tech_pvt;
2673 ast_mutex_unlock(&chan_lock);
2679 if (call->bchannel && f->samples)
2680 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2681 ast_mutex_unlock(&chan_lock);
2689 static struct ast_frame *lcr_read(struct ast_channel *ast)
2691 struct chan_call *call;
2694 ast_mutex_lock(&chan_lock);
2695 call = ast->tech_pvt;
2697 ast_mutex_unlock(&chan_lock);
2700 if (call->pipe[0] > -1) {
2701 if (call->rebuffer && !call->hdlc) {
2702 /* Make sure we have a complete 20ms (160byte) frame */
2703 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2705 call->framepos += len;
2708 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2710 if (len < 0 && errno == EAGAIN) {
2711 ast_mutex_unlock(&chan_lock);
2713 #ifdef LCR_FOR_ASTERISK
2714 return &ast_null_frame;
2717 #ifdef LCR_FOR_CALLWEAVER
2723 close(call->pipe[0]);
2726 ast_mutex_unlock(&chan_lock);
2728 } else if (call->rebuffer && call->framepos < 160) {
2729 /* Not a complete frame, so we send a null-frame */
2730 ast_mutex_unlock(&chan_lock);
2731 return &ast_null_frame;
2735 call->read_fr.frametype = AST_FRAME_VOICE;
2736 #ifdef AST_1_8_OR_HIGHER
2737 #if ASTERISK_VERSION_NUM < 100000
2738 call->read_fr.subclass.codec = ast->nativeformats;
2740 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2741 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2744 call->read_fr.subclass = ast->nativeformats;
2746 if (call->rebuffer) {
2747 call->read_fr.datalen = call->framepos;
2748 call->read_fr.samples = call->framepos;
2751 call->read_fr.datalen = len;
2752 call->read_fr.samples = len;
2754 call->read_fr.delivery = ast_tv(0,0);
2755 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2756 ast_mutex_unlock(&chan_lock);
2758 return &call->read_fr;
2761 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2763 union parameter newparam;
2765 struct chan_call *call;
2766 const struct tone_zone_sound *ts = NULL;
2768 ast_mutex_lock(&chan_lock);
2769 call = ast->tech_pvt;
2771 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2772 ast_mutex_unlock(&chan_lock);
2777 case AST_CONTROL_BUSY:
2778 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2779 ast_setstate(ast, AST_STATE_BUSY);
2780 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2781 /* send message to lcr */
2782 memset(&newparam, 0, sizeof(union parameter));
2783 newparam.disconnectinfo.cause = 17;
2784 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2785 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2787 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2789 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2790 ts = ast_get_indication_tone(ast->zone, "busy");
2793 case AST_CONTROL_CONGESTION:
2794 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2795 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2796 /* send message to lcr */
2797 memset(&newparam, 0, sizeof(union parameter));
2798 newparam.disconnectinfo.cause = ast->hangupcause;
2799 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2800 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2802 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2804 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2805 ts = ast_get_indication_tone(ast->zone, "congestion");
2808 case AST_CONTROL_PROCEEDING:
2809 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2810 if (call->state == CHAN_LCR_STATE_IN_SETUP
2811 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2812 /* send message to lcr */
2813 memset(&newparam, 0, sizeof(union parameter));
2814 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2816 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2819 case AST_CONTROL_RINGING:
2820 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2821 ast_setstate(ast, AST_STATE_RING);
2822 if (call->state == CHAN_LCR_STATE_IN_SETUP
2823 || call->state == CHAN_LCR_STATE_IN_DIALING
2824 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2825 /* send message to lcr */
2826 memset(&newparam, 0, sizeof(union parameter));
2827 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2829 call->state = CHAN_LCR_STATE_IN_ALERTING;
2831 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2832 ts = ast_get_indication_tone(ast->zone, "ring");
2835 case AST_CONTROL_PROGRESS:
2836 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2837 /* request bchannel */
2838 if (!call->bchannel) {
2839 CDEBUG(call, ast, "Requesting B-channel.\n");
2840 memset(&newparam, 0, sizeof(union parameter));
2841 newparam.bchannel.type = BCHANNEL_REQUEST;
2842 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2846 CDEBUG(call, ast, "Received indicate -1.\n");
2847 ast_playtones_stop(ast);
2851 case AST_CONTROL_VIDUPDATE:
2852 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2855 case AST_CONTROL_HOLD:
2856 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2857 /* send message to lcr */
2858 memset(&newparam, 0, sizeof(union parameter));
2859 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2860 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2862 /*start music onhold*/
2863 #ifdef LCR_FOR_ASTERISK
2864 #if ASTERISK_VERSION_NUM <110000
2865 ast_moh_start(ast,data,ast->musicclass);
2867 ast_moh_start(ast,data,ast_channel_musicclass(ast));
2871 #ifdef LCR_FOR_CALLWEAVER
2872 ast_moh_start(ast, NULL);
2877 case AST_CONTROL_UNHOLD:
2878 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2879 /* send message to lcr */
2880 memset(&newparam, 0, sizeof(union parameter));
2881 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2882 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2888 #ifdef AST_CONTROL_SRCUPDATE
2889 case AST_CONTROL_SRCUPDATE:
2893 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2896 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2901 if (ts && ts->data[0]) {
2902 ast_playtones_start(ast, 0, ts->data, 1);
2906 ast_mutex_unlock(&chan_lock);
2913 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2915 struct chan_call *call;
2921 ast_mutex_lock(&chan_lock);
2922 call = ast->tech_pvt;
2924 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2925 ast_mutex_unlock(&chan_lock);
2929 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2931 ast_mutex_unlock(&chan_lock);
2936 * send_text asterisk
2938 static int lcr_send_text(struct ast_channel *ast, const char *text)
2940 struct chan_call *call;
2941 union parameter newparam;
2943 ast_mutex_lock(&chan_lock);
2944 call = ast->tech_pvt;
2946 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2947 ast_mutex_unlock(&chan_lock);
2951 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2952 memset(&newparam, 0, sizeof(union parameter));
2953 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2954 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2955 ast_mutex_unlock(&chan_lock);
2962 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2963 struct ast_channel *ast2, int flags,
2964 struct ast_frame **fo,
2965 struct ast_channel **rc, int timeoutms)
2968 struct chan_call *call1, *call2;
2969 struct ast_channel *carr[2], *who;
2971 struct ast_frame *f;
2974 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2979 /* join via dsp (if the channels are currently open) */
2980 ast_mutex_lock(&chan_lock);
2981 call1 = ast1->tech_pvt;
2982 call2 = ast2->tech_pvt;
2983 if (!call1 || !call2) {
2984 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2985 ast_mutex_unlock(&chan_lock);
2986 return AST_BRIDGE_COMPLETE;
2989 /* join, if both call instances uses dsp
2990 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2992 if (!call1->nodsp && !call2->nodsp) {
2993 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2995 /* get bridge id and join */
2996 bridge_id = new_bridge_id();
2998 call1->bridge_id = bridge_id;
2999 if (call1->bchannel)
3000 bchannel_join(call1->bchannel, bridge_id);
3002 call2->bridge_id = bridge_id;
3003 if (call2->bchannel)
3004 bchannel_join(call2->bchannel, bridge_id);
3006 if (call1->nodsp && call2->nodsp)
3007 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3009 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3010 call1->bridge_call = call2;
3011 call2->bridge_call = call1;
3013 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3014 || call1->state == CHAN_LCR_STATE_IN_DIALING
3015 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3016 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3017 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3020 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3021 || call2->state == CHAN_LCR_STATE_IN_DIALING
3022 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3023 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3024 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3028 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3029 so let's do it for them. Hmpf.
3032 if (call1->on_hold) {
3033 union parameter newparam;
3035 memset(&newparam, 0, sizeof(union parameter));
3036 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3037 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3042 if (call2->on_hold) {
3043 union parameter newparam;
3045 memset(&newparam, 0, sizeof(union parameter));
3046 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3047 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3052 ast_mutex_unlock(&chan_lock);
3056 who = ast_waitfor_n(carr, 2, &to);
3059 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3064 if (!f || f->frametype == AST_FRAME_CONTROL) {
3066 CDEBUG(NULL, NULL, "Got hangup.\n");
3068 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3075 if ( f->frametype == AST_FRAME_DTMF ) {
3076 CDEBUG(NULL, NULL, "Got DTMF.\n");
3092 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3094 /* split channels */
3095 ast_mutex_lock(&chan_lock);
3096 call1 = ast1->tech_pvt;
3097 call2 = ast2->tech_pvt;
3098 if (call1 && call1->bridge_id) {
3099 call1->bridge_id = 0;
3100 if (call1->bchannel)
3101 bchannel_join(call1->bchannel, 0);
3102 if (call1->bridge_call)
3103 call1->bridge_call->bridge_call = NULL;
3105 if (call2 && call1->bridge_id) {
3106 call2->bridge_id = 0;
3107 if (call2->bchannel)
3108 bchannel_join(call2->bchannel, 0);
3109 if (call2->bridge_call)
3110 call2->bridge_call->bridge_call = NULL;
3112 call1->bridge_call = NULL;
3113 call2->bridge_call = NULL;
3115 ast_mutex_unlock(&chan_lock);
3116 return AST_BRIDGE_COMPLETE;
3118 static struct ast_channel_tech lcr_tech = {
3120 .description = "Channel driver for connecting to Linux-Call-Router",
3121 #if ASTERISK_VERSION_NUM < 100000
3122 .capabilities = AST_FORMAT_ALAW,
3124 .requester = lcr_request,
3126 #ifdef LCR_FOR_ASTERISK
3127 .send_digit_begin = lcr_digit_begin,
3128 .send_digit_end = lcr_digit_end,
3131 #ifdef LCR_FOR_CALLWEAVER
3132 .send_digit = lcr_digit,
3136 .bridge = lcr_bridge,
3137 .hangup = lcr_hangup,
3138 .answer = lcr_answer,
3141 .indicate = lcr_indicate,
3143 .send_text = lcr_send_text,
3152 static int lcr_show_lcr (int fd, int argc, char *argv[])
3157 static int lcr_show_calls (int fd, int argc, char *argv[])
3162 static int lcr_reload_routing (int fd, int argc, char *argv[])
3167 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3172 static int lcr_port_block (int fd, int argc, char *argv[])
3177 static int lcr_port_unblock (int fd, int argc, char *argv[])
3182 static int lcr_port_unload (int fd, int argc, char *argv[])
3187 static struct ast_cli_entry cli_show_lcr =
3188 { {"lcr", "show", "lcr", NULL},
3190 "Shows current states of LCR core",
3191 "Usage: lcr show lcr\n",
3194 static struct ast_cli_entry cli_show_calls =
3195 { {"lcr", "show", "calls", NULL},
3197 "Shows current calls made by LCR and Asterisk",
3198 "Usage: lcr show calls\n",
3201 static struct ast_cli_entry cli_reload_routing =
3202 { {"lcr", "reload", "routing", NULL},
3204 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3205 "Usage: lcr reload routing\n",
3208 static struct ast_cli_entry cli_reload_interfaces =
3209 { {"lcr", "reload", "interfaces", NULL},
3210 lcr_reload_interfaces,
3211 "Reloads interfaces conf of LCR",
3212 "Usage: lcr reload interfaces\n",
3215 static struct ast_cli_entry cli_port_block =
3216 { {"lcr", "port", "block", NULL},
3218 "Blocks LCR port for further calls",
3219 "Usage: lcr port block \"<port>\"\n",
3222 static struct ast_cli_entry cli_port_unblock =
3223 { {"lcr", "port", "unblock", NULL},
3225 "Unblocks or loads LCR port, port is opened my mISDN",
3226 "Usage: lcr port unblock \"<port>\"\n",
3229 static struct ast_cli_entry cli_port_unload =
3230 { {"lcr", "port", "unload", NULL},
3232 "Unloads LCR port, port is closes by mISDN",
3233 "Usage: lcr port unload \"<port>\"\n",
3238 #ifdef LCR_FOR_ASTERISK
3239 #ifdef AST_1_8_OR_HIGHER
3240 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3242 static int lcr_config_exec(struct ast_channel *ast, void *data)
3246 #ifdef LCR_FOR_CALLWEAVER
3247 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3250 struct chan_call *call;
3252 ast_mutex_lock(&chan_lock);
3254 #ifdef LCR_FOR_ASTERISK
3255 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3258 #ifdef LCR_FOR_CALLWEAVER
3259 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3265 if (call->ast == ast)
3271 #ifdef LCR_FOR_ASTERISK
3272 apply_opt(call, (char *)data);
3275 #ifdef LCR_FOR_CALLWEAVER
3276 apply_opt(call, (char *)argv[0]);
3280 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3282 ast_mutex_unlock(&chan_lock);
3287 * module loading and destruction
3289 int load_module(void)
3292 char options_error[256];
3294 for (i = 0; i < 256; i++) {
3295 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3296 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3299 if (read_options(options_error) == 0) {
3300 CERROR(NULL, NULL, "%s", options_error);
3302 #ifdef LCR_FOR_ASTERISK
3303 return AST_MODULE_LOAD_DECLINE;
3306 #ifdef LCR_FOR_CALLWEAVER
3312 ast_mutex_init(&chan_lock);
3313 ast_mutex_init(&log_lock);
3315 if (bchannel_initialize()) {
3316 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3319 #ifdef LCR_FOR_ASTERISK
3320 return AST_MODULE_LOAD_DECLINE;
3323 #ifdef LCR_FOR_CALLWEAVER
3329 #if ASTERISK_VERSION_NUM < 100000
3330 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3332 struct ast_format tmp;
3333 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3334 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3335 return AST_MODULE_LOAD_DECLINE;
3337 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3339 if (ast_channel_register(&lcr_tech)) {
3340 CERROR(NULL, NULL, "Unable to register channel class\n");
3341 bchannel_deinitialize();
3344 #ifdef LCR_FOR_ASTERISK
3345 return AST_MODULE_LOAD_DECLINE;
3348 #ifdef LCR_FOR_CALLWEAVER
3353 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3355 #ifdef LCR_FOR_ASTERISK
3356 "lcr_config(<opt><optarg>:<opt>:...)\n"
3359 #ifdef LCR_FOR_CALLWEAVER
3360 "lcr_config(<opt><optarg>:<opt>:...)\n",
3363 "Sets LCR opts. and optargs\n"
3365 "The available options are:\n"
3366 " d - Send display text on called phone, text is the optarg.\n"
3367 " n - Don't detect dtmf tones on called channel.\n"
3368 " h - Force data call (HDLC).\n"
3369 " t - Disable mISDN_dsp features (required for fax application).\n"
3370 " q - Add queue to make fax stream seamless (required for fax app).\n"
3371 " Use queue size in miliseconds for optarg. (try 250)\n"
3372 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3373 " Use time to detect for optarg.\n"
3374 " c - Make crypted outgoing call, optarg is keyindex.\n"
3375 " e - Perform echo cancelation on this channel.\n"
3376 " Takes mISDN pipeline option as optarg.\n"
3377 " s - Send Non Inband DTMF as inband.\n"
3378 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3379 " vr - rxgain control\n"
3380 " vt - txgain control\n"
3381 " Volume changes at factor 2 ^ optarg.\n"
3382 " k - use keypad to dial this call.\n"
3384 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3385 " -> use 16 for fax (3.1k audio)\n"
3387 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3388 "options: \"n:t:q250\" for seamless audio transmission.\n"
3393 ast_cli_register(&cli_show_lcr);
3394 ast_cli_register(&cli_show_calls);
3395 ast_cli_register(&cli_reload_routing);
3396 ast_cli_register(&cli_reload_interfaces);
3397 ast_cli_register(&cli_port_block);
3398 ast_cli_register(&cli_port_unblock);
3399 ast_cli_register(&cli_port_unload);
3402 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3403 /* failed to create thread */
3404 bchannel_deinitialize();
3406 ast_channel_unregister(&lcr_tech);
3408 #ifdef LCR_FOR_ASTERISK
3409 return AST_MODULE_LOAD_DECLINE;
3412 #ifdef LCR_FOR_CALLWEAVER
3420 int unload_module(void)
3422 /* First, take us out of the channel loop */
3423 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3425 pthread_cancel(chan_tid);
3429 del_timer(&socket_retry);
3431 unregister_fd(&wake_fd);
3432 close(wake_pipe[0]);
3433 close(wake_pipe[1]);
3435 // ast_mutex_unlock(&chan_lock);
3437 ast_channel_unregister(&lcr_tech);
3439 ast_unregister_application("lcr_config");
3441 if (mISDN_created) {
3442 bchannel_deinitialize();
3446 if (lcr_sock >= 0) {
3451 #if ASTERISK_VERSION_NUM >= 100000
3452 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3457 int reload_module(void)
3463 #ifdef LCR_FOR_ASTERISK
3464 #define AST_MODULE "chan_lcr"
3467 #ifdef LCR_FOR_CALLWEAVER
3472 ast_mutex_lock(&usecnt_lock);
3474 ast_mutex_unlock(&usecnt_lock);
3479 #ifdef LCR_FOR_ASTERISK
3480 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3481 .load = load_module,
3482 .unload = unload_module,
3483 .reload = reload_module,
3487 #ifdef LCR_FOR_CALLWEAVER
3488 char *description(void)