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 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
246 ast_text[sizeof(ast_text)-1] = '\0';
248 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
249 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
251 ast_mutex_unlock(&log_lock);
255 * channel and call instances
257 struct chan_call *call_first;
261 * special case: 0: find new ref, that has not been assigned a ref yet
264 struct chan_call *find_call_ref(unsigned int ref)
266 struct chan_call *call = call_first;
267 int assigned = (ref > 0);
270 if (call->ref == ref && call->ref_was_assigned == assigned)
277 void free_call(struct chan_call *call)
279 struct chan_call **temp = &call_first;
283 *temp = (*temp)->next;
284 if (call->pipe[0] > -1)
285 close(call->pipe[0]);
286 if (call->pipe[1] > -1)
287 close(call->pipe[1]);
288 if (call->bchannel) {
289 if (call->bchannel->call != call)
290 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
291 call->bchannel->call = NULL;
293 if (call->bridge_call) {
294 if (call->bridge_call->bridge_call != call)
295 CERROR(call, NULL, "Linked call structure has no link to us.\n");
296 call->bridge_call->bridge_call = NULL;
299 ast_translator_free_path(call->trans);
301 ast_dsp_free(call->dsp);
302 CDEBUG(call, NULL, "Call instance freed.\n");
307 temp = &((*temp)->next);
309 CERROR(call, NULL, "Call instance not found in list.\n");
312 struct chan_call *alloc_call(void)
314 struct chan_call **callp = &call_first;
317 callp = &((*callp)->next);
319 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
321 memset(*callp, 0, sizeof(struct chan_call));
322 if (pipe((*callp)->pipe) < 0) {
323 CERROR(*callp, NULL, "Failed to create pipe.\n");
327 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
328 CDEBUG(*callp, NULL, "Call instance allocated.\n");
332 unsigned short new_bridge_id(void)
334 struct chan_call *call;
335 unsigned short id = 1;
337 /* search for lowest bridge id that is not in use and not 0 */
341 if (call->bridge_id == id)
349 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
354 * enque message to LCR
356 int send_message(int message_type, unsigned int ref, union parameter *param)
358 struct admin_list *admin, **adminp;
361 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
364 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
366 adminp = &admin_first;
368 adminp = &((*adminp)->next);
369 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
371 CERROR(NULL, NULL, "No memory for message to LCR.\n");
376 admin->msg.message = ADMIN_MESSAGE;
377 admin->msg.u.msg.type = message_type;
378 admin->msg.u.msg.ref = ref;
379 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
380 socket_fd.when |= LCR_FD_WRITE;
384 write(wake_pipe[1], &byte, 1);
391 * apply options (in locked state)
393 void apply_opt(struct chan_call *call, char *data)
395 union parameter newparam;
396 char string[1024], *p = string, *opt, *key;
402 strncpy(string, data, sizeof(string)-1);
403 string[sizeof(string)-1] = '\0';
406 while((opt = strsep(&p, ":"))) {
409 if (opt[1] == '\0') {
410 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
413 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
414 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
415 strncpy(call->display, opt+1, sizeof(call->display)-1);
417 memset(&newparam, 0, sizeof(union parameter));
418 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
419 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
423 if (opt[1] != '\0') {
424 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
427 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
428 if (call->dsp_dtmf) {
431 bchannel_dtmf(call->bchannel, 0);
435 if (opt[1] == '\0') {
436 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
440 /* check for 0xXXXX... type of key */
441 if (!!strncmp((char *)key, "0x", 2)) {
442 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
446 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
447 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
452 if (*key>='0' && *key<='9')
453 call->bf_key[i] = (*key-'0') << 8;
454 else if (*key>='a' && *key<='f')
455 call->bf_key[i] = (*key-'a'+10) << 8;
456 else if (*key>='A' && *key<='F')
457 call->bf_key[i] = (*key-'A'+10) << 8;
461 if (*key>='0' && *key<='9')
462 call->bf_key[i] += (*key - '0');
463 else if (*key>='a' && *key<='f')
464 call->bf_key[i] += (*key - 'a' + 10);
465 else if (*key>='A' && *key<='F')
466 call->bf_key[i] += (*key - 'A' + 10);
473 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
477 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
479 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
482 if (opt[1] != '\0') {
483 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
486 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
491 if (opt[1] != '\0') {
492 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
495 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
500 if (opt[1] == '\0') {
501 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
504 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
505 call->nodsp_queue = atoi(opt+1);
508 if (opt[1] == '\0') {
509 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
512 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
513 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
515 bchannel_pipeline(call->bchannel, call->pipeline);
518 if (opt[1] == '\0') {
519 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
522 call->faxdetect=atoi(opt+1);
524 call->dsp=ast_dsp_new();
526 #ifdef LCR_FOR_CALLWEAVER
527 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
529 #ifdef LCR_FOR_ASTERISK
530 #ifdef DSP_FEATURE_DTMF_DETECT
531 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
533 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
538 #ifdef LCR_FOR_CALLWEAVER
539 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
541 #ifdef LCR_FOR_ASTERISK
542 #if ASTERISK_VERSION_NUM < 100000
543 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
545 struct ast_format src;
546 struct ast_format dst;
547 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
548 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
549 call->trans=ast_translator_build_path(&dst, &src);
554 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
557 if (opt[1] != '\0') {
558 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
561 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
566 if (opt[1] != '\0') {
567 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
570 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
571 call->inband_dtmf = 1;
574 if (opt[1] != 'r' && opt[1] != 't') {
575 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
579 if (gain < -8 || gain >8) {
580 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
583 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
585 call->rx_gain = gain;
587 bchannel_gain(call->bchannel, call->rx_gain, 0);
589 call->tx_gain = gain;
591 bchannel_gain(call->bchannel, call->tx_gain, 1);
595 if (opt[1] != '\0') {
596 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
599 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
604 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
608 /* re-open, if bchannel is created */
609 if (call->bchannel && call->bchannel->b_sock > -1) {
610 bchannel_destroy(call->bchannel);
611 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
612 bchannel_activate(call->bchannel, 1);
617 * send setup info to LCR
618 * this function is called, when asterisk call is received and ref is received
620 static void send_setup_to_lcr(struct chan_call *call)
622 union parameter newparam;
623 struct ast_channel *ast = call->ast;
626 if (!call->ast || !call->ref)
629 #ifdef AST_1_8_OR_HIGHER
630 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
632 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
635 /* send setup message to LCR */
636 memset(&newparam, 0, sizeof(union parameter));
637 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
638 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
640 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
642 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
643 if (!!strcmp(call->interface, "pbx"))
644 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
645 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
646 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
647 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
648 call->display[0] = '\0';
650 #ifdef AST_1_8_OR_HIGHER
651 /* set stored call information */
652 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
653 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
655 if (call->cid_num[0])
656 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
657 if (call->cid_name[0])
658 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
659 if (call->cid_rdnis[0]) {
660 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
661 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
662 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
664 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
665 case AST_PRES_RESTRICTED:
666 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
668 case AST_PRES_UNAVAILABLE:
669 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
671 case AST_PRES_ALLOWED:
673 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
675 switch(ast->cid.cid_ton) {
677 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
680 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
683 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
686 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
689 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
690 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
692 // ast->transfercapability = atoi(tmp);
693 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
694 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
696 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
698 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
699 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
701 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
702 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
703 send_message(MESSAGE_SETUP, call->ref, &newparam);
705 /* change to outgoing setup state */
706 call->state = CHAN_LCR_STATE_OUT_SETUP;
710 * send dialing info to LCR
711 * this function is called, when setup acknowledge is received and dialing
714 static void send_dialque_to_lcr(struct chan_call *call)
716 union parameter newparam;
718 if (!call->ast || !call->ref || !call->dialque[0])
721 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
723 /* send setup message to LCR */
724 memset(&newparam, 0, sizeof(union parameter));
726 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
728 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
729 call->dialque[0] = '\0';
730 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
734 * in case of a bridge, the unsupported message can be forwarded directly
735 * to the remote call.
737 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
741 if (!call->bridge_call) return;
742 CDEBUG(call, NULL, "Sending message due bridging.\n");
743 send_message(message_type, call->bridge_call->ref, param);
747 * send release message to LCR and import bchannel if exported
749 static void send_release_and_import(struct chan_call *call, int cause, int location)
751 union parameter newparam;
753 /* importing channel */
754 if (call->bchannel) {
755 memset(&newparam, 0, sizeof(union parameter));
756 newparam.bchannel.type = BCHANNEL_RELEASE;
757 newparam.bchannel.handle = call->bchannel->handle;
758 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
760 /* sending release */
761 memset(&newparam, 0, sizeof(union parameter));
762 newparam.disconnectinfo.cause = cause;
763 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
764 send_message(MESSAGE_RELEASE, call->ref, &newparam);
768 * check if extension matches and start asterisk
769 * if it can match, proceed
772 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
775 union parameter newparam;
776 char *exten = ast->exten;
780 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
784 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
785 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
789 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
790 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);
794 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
795 /* send setup acknowledge to lcr */
796 memset(&newparam, 0, sizeof(union parameter));
797 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
800 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
805 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
806 /* send setup acknowledge to lcr */
807 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
808 memset(&newparam, 0, sizeof(union parameter));
809 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
813 call->state = CHAN_LCR_STATE_IN_DIALING;
815 /* if match, start pbx */
816 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
817 CDEBUG(call, ast, "Extensions matches.\n");
822 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
828 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
836 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
837 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
839 /* release asterisk */
840 ast->hangupcause = call->cause;
841 /* change to release state */
842 call->state = CHAN_LCR_STATE_RELEASE;
843 ast_hangup(ast); // call will be destroyed here
847 /* send setup to asterisk */
848 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
850 #ifdef LCR_FOR_CALLWEAVER
852 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
855 ret = ast_pbx_start(ast);
857 cause = (ret==-2)?34:27;
860 call->pbx_started = 1;
861 ast_setstate(ast, AST_STATE_RING);
865 * incoming setup from LCR
867 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
869 struct ast_channel *ast;
871 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
873 /* create asterisk channel instrance */
875 #ifdef LCR_FOR_CALLWEAVER
876 ast = ast_channel_alloc(1);
879 #ifdef LCR_FOR_ASTERISK
880 #ifdef AST_1_8_OR_HIGHER
881 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
883 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
889 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
890 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
897 ast->tech_pvt = call;
898 ast->tech = &lcr_tech;
899 ast->fds[0] = call->pipe[0];
901 /* fill setup information */
902 if (param->setup.dialinginfo.id)
903 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
904 if (param->setup.context[0])
905 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
907 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
911 #ifdef AST_1_8_OR_HIGHER
912 if (param->setup.callerinfo.id[0]) {
913 ast->caller.id.number.valid = 1;
914 ast->caller.id.number.str = strdup(param->setup.callerinfo.id);
915 if (!param->setup.callerinfo.id[0]) {
916 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
917 ast->caller.id.number.plan = (0 << 4) | 1;
919 switch (param->setup.callerinfo.present) {
920 case INFO_PRESENT_ALLOWED:
921 ast->caller.id.number.presentation = AST_PRES_ALLOWED;
923 case INFO_PRESENT_RESTRICTED:
924 ast->caller.id.number.presentation = AST_PRES_RESTRICTED;
927 ast->caller.id.number.presentation = AST_PRES_UNAVAILABLE;
929 switch (param->setup.callerinfo.screen) {
930 case INFO_SCREEN_USER:
931 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
933 case INFO_SCREEN_USER_VERIFIED_PASSED:
934 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
936 case INFO_SCREEN_USER_VERIFIED_FAILED:
937 ast->caller.id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
940 ast->caller.id.number.presentation |= AST_PRES_NETWORK_NUMBER;
942 switch (param->setup.callerinfo.ntype) {
943 case INFO_NTYPE_SUBSCRIBER:
944 ast->caller.id.number.plan = (4 << 4) | 1;
946 case INFO_NTYPE_NATIONAL:
947 ast->caller.id.number.plan = (2 << 4) | 1;
949 case INFO_NTYPE_INTERNATIONAL:
950 ast->caller.id.number.plan = (1 << 4) | 1;
953 ast->caller.id.number.plan = (0 << 4) | 1;
956 if (param->setup.callerinfo.id2[0]) {
957 ast->caller.ani.number.valid = 1;
958 ast->caller.ani.number.str = strdup(param->setup.callerinfo.id2);
959 switch (param->setup.callerinfo.present2) {
960 case INFO_PRESENT_ALLOWED:
961 ast->caller.ani.number.presentation = AST_PRES_ALLOWED;
963 case INFO_PRESENT_RESTRICTED:
964 ast->caller.ani.number.presentation = AST_PRES_RESTRICTED;
967 ast->caller.ani.number.presentation = AST_PRES_UNAVAILABLE;
969 switch (param->setup.callerinfo.screen2) {
970 case INFO_SCREEN_USER:
971 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
973 case INFO_SCREEN_USER_VERIFIED_PASSED:
974 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
976 case INFO_SCREEN_USER_VERIFIED_FAILED:
977 ast->caller.ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
980 ast->caller.ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
982 switch (param->setup.callerinfo.ntype2) {
983 case INFO_NTYPE_SUBSCRIBER:
984 ast->caller.ani.number.plan = (4 << 4) | 1;
986 case INFO_NTYPE_NATIONAL:
987 ast->caller.ani.number.plan = (2 << 4) | 1;
989 case INFO_NTYPE_INTERNATIONAL:
990 ast->caller.ani.number.plan = (1 << 4) | 1;
993 ast->caller.ani.number.plan = (0 << 4) | 1;
996 if (param->setup.callerinfo.name[0]) {
997 ast->caller.id.name.valid = 1;
998 ast->caller.id.name.str = strdup(param->setup.callerinfo.name);
1000 if (param->setup.redirinfo.id[0]) {
1001 ast->redirecting.from.number.valid = 1;
1002 ast->redirecting.from.number.str = strdup(param->setup.redirinfo.id);
1003 switch (param->setup.redirinfo.present) {
1004 case INFO_PRESENT_ALLOWED:
1005 ast->redirecting.from.number.presentation = AST_PRES_ALLOWED;
1007 case INFO_PRESENT_RESTRICTED:
1008 ast->redirecting.from.number.presentation = AST_PRES_RESTRICTED;
1011 ast->redirecting.from.number.presentation = AST_PRES_UNAVAILABLE;
1013 switch (param->setup.redirinfo.screen) {
1014 case INFO_SCREEN_USER:
1015 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1017 case INFO_SCREEN_USER_VERIFIED_PASSED:
1018 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1020 case INFO_SCREEN_USER_VERIFIED_FAILED:
1021 ast->redirecting.from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1024 ast->redirecting.from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1026 switch (param->setup.redirinfo.ntype) {
1027 case INFO_NTYPE_SUBSCRIBER:
1028 ast->redirecting.from.number.plan = (4 << 4) | 1;
1030 case INFO_NTYPE_NATIONAL:
1031 ast->redirecting.from.number.plan = (2 << 4) | 1;
1033 case INFO_NTYPE_INTERNATIONAL:
1034 ast->redirecting.from.number.plan = (1 << 4) | 1;
1037 ast->redirecting.from.number.plan = (0 << 4) | 1;
1041 memset(&ast->cid, 0, sizeof(ast->cid));
1042 if (param->setup.callerinfo.id[0])
1043 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1044 if (param->setup.callerinfo.id2[0])
1045 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1046 if (param->setup.callerinfo.name[0])
1047 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1048 if (param->setup.redirinfo.id[0])
1049 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1050 switch (param->setup.callerinfo.present) {
1051 case INFO_PRESENT_ALLOWED:
1052 ast->cid.cid_pres = AST_PRES_ALLOWED;
1054 case INFO_PRESENT_RESTRICTED:
1055 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1058 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1060 switch (param->setup.callerinfo.ntype) {
1061 case INFO_NTYPE_SUBSCRIBER:
1062 ast->cid.cid_ton = 4;
1064 case INFO_NTYPE_NATIONAL:
1065 ast->cid.cid_ton = 2;
1067 case INFO_NTYPE_INTERNATIONAL:
1068 ast->cid.cid_ton = 1;
1071 ast->cid.cid_ton = 0;
1075 ast->transfercapability = param->setup.capainfo.bearer_capa;
1076 /* enable hdlc if transcap is data */
1077 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1079 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1081 /* configure channel */
1082 #if ASTERISK_VERSION_NUM < 100000
1083 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1084 ast->readformat = ast->rawreadformat = ast->nativeformats;
1085 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1087 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1088 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1089 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1090 ast_set_write_format(ast, &ast->rawwriteformat);
1091 ast_set_read_format(ast, &ast->rawreadformat);
1094 ast->hangupcause = 0;
1097 call->state = CHAN_LCR_STATE_IN_SETUP;
1099 if (!call->pbx_started)
1100 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1104 * incoming setup acknowledge from LCR
1106 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1108 if (!call->ast) return;
1110 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1112 /* send pending digits in dialque */
1113 if (call->dialque[0])
1114 send_dialque_to_lcr(call);
1115 /* change to overlap state */
1116 call->state = CHAN_LCR_STATE_OUT_DIALING;
1120 * incoming proceeding from LCR
1122 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1124 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1127 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1128 /* queue event for asterisk */
1129 if (call->ast && call->pbx_started) {
1133 write(wake_pipe[1], &byte, 1);
1135 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1141 * incoming alerting from LCR
1143 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1145 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1148 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1149 /* queue event to asterisk */
1150 if (call->ast && call->pbx_started) {
1154 write(wake_pipe[1], &byte, 1);
1156 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1161 * incoming connect from LCR
1163 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1165 union parameter newparam;
1167 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1170 call->state = CHAN_LCR_STATE_CONNECT;
1171 /* request bchannel */
1172 if (!call->bchannel) {
1173 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1174 memset(&newparam, 0, sizeof(union parameter));
1175 newparam.bchannel.type = BCHANNEL_REQUEST;
1176 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1178 /* copy connectinfo */
1179 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1180 /* queue event to asterisk */
1181 if (call->ast && call->pbx_started) {
1185 write(wake_pipe[1], &byte, 1);
1187 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1192 * incoming disconnect from LCR
1194 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1196 struct ast_channel *ast = call->ast;
1198 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1201 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1203 call->cause = param->disconnectinfo.cause;
1204 call->location = param->disconnectinfo.location;
1205 /* if bridge, forward disconnect and return */
1208 if (call->bridge_call) {
1209 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1210 bridge_message_if_bridged(call, message_type, param);
1214 /* release lcr with same cause */
1215 send_release_and_import(call, call->cause, call->location);
1217 /* change to release state */
1218 call->state = CHAN_LCR_STATE_RELEASE;
1219 /* queue release asterisk */
1221 ast->hangupcause = call->cause;
1222 if (call->pbx_started) {
1226 write(wake_pipe[1], &byte, 1);
1228 strcpy(call->queue_string, "H"); // overwrite other indications
1230 ast_hangup(ast); // call will be destroyed here
1236 * incoming release from LCR
1238 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1240 struct ast_channel *ast = call->ast;
1242 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1246 /* change to release state */
1247 call->state = CHAN_LCR_STATE_RELEASE;
1248 /* copy release info */
1250 call->cause = param->disconnectinfo.cause;
1251 call->location = param->disconnectinfo.location;
1253 /* if we have an asterisk instance, queue hangup, else we are done */
1255 ast->hangupcause = call->cause;
1256 if (call->pbx_started) {
1260 write(wake_pipe[1], &byte, 1);
1262 strcpy(call->queue_string, "H");
1264 ast_hangup(ast); // call will be destroyed here
1273 * incoming information from LCR
1275 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1277 struct ast_channel *ast = call->ast;
1279 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1283 /* pbx not started */
1284 if (!call->pbx_started) {
1285 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1286 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1287 lcr_start_pbx(call, ast, param->information.sending_complete);
1291 /* change dailing state after setup */
1292 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1293 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1294 call->state = CHAN_LCR_STATE_IN_DIALING;
1295 // ast_setstate(ast, AST_STATE_DIALING);
1299 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1303 write(wake_pipe[1], &byte, 1);
1305 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1308 /* use bridge to forware message not supported by asterisk */
1309 if (call->state == CHAN_LCR_STATE_CONNECT) {
1310 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1311 bridge_message_if_bridged(call, message_type, param);
1316 * incoming information from LCR
1318 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1320 union parameter newparam;
1322 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1324 /* request bchannel, if call is resumed and we don't have it */
1325 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1326 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1327 memset(&newparam, 0, sizeof(union parameter));
1328 newparam.bchannel.type = BCHANNEL_REQUEST;
1329 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1332 if (!call->ast) return;
1334 /* use bridge to forware message not supported by asterisk */
1335 bridge_message_if_bridged(call, message_type, param);
1339 * incoming information from LCR
1341 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1343 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1345 if (!call->ast) return;
1347 /* use bridge to forware message not supported by asterisk */
1348 bridge_message_if_bridged(call, message_type, param);
1352 * incoming pattern from LCR
1354 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1356 union parameter newparam;
1358 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1360 if (!call->ast) return;
1362 /* pattern are indicated only once */
1363 if (call->has_pattern)
1365 call->has_pattern = 1;
1367 /* request bchannel */
1368 if (!call->bchannel) {
1369 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1370 memset(&newparam, 0, sizeof(union parameter));
1371 newparam.bchannel.type = BCHANNEL_REQUEST;
1372 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1374 /* queue PROGRESS, because tones are available */
1375 if (call->ast && call->pbx_started) {
1379 write(wake_pipe[1], &byte, 1);
1381 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1386 * got dtmf from bchannel (locked state)
1388 void lcr_in_dtmf(struct chan_call *call, int val)
1390 struct ast_channel *ast = call->ast;
1395 if (!call->pbx_started)
1398 if (!call->dsp_dtmf) {
1399 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1403 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1409 write(wake_pipe[1], &byte, 1);
1411 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1415 * message received from LCR
1417 int receive_message(int message_type, unsigned int ref, union parameter *param)
1419 struct bchannel *bchannel;
1420 struct chan_call *call;
1421 union parameter newparam;
1423 memset(&newparam, 0, sizeof(union parameter));
1425 /* handle bchannel message*/
1426 if (message_type == MESSAGE_BCHANNEL) {
1427 switch(param->bchannel.type) {
1428 case BCHANNEL_ASSIGN:
1429 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1430 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1431 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1434 /* create bchannel */
1435 bchannel = alloc_bchannel(param->bchannel.handle);
1437 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1441 /* configure channel */
1442 bchannel->b_tx_gain = param->bchannel.tx_gain;
1443 bchannel->b_rx_gain = param->bchannel.rx_gain;
1444 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1445 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1446 bchannel->b_bf_len = param->bchannel.crypt_len;
1447 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1449 bchannel->b_txdata = 0;
1450 bchannel->b_tx_dejitter = 1;
1452 /* in case, ref is not set, this bchannel instance must
1453 * be created until it is removed again by LCR */
1455 call = find_call_ref(ref);
1457 bchannel->call = call;
1458 call->bchannel = bchannel;
1460 bchannel_dtmf(bchannel, 1);
1462 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1463 if (call->pipeline[0])
1464 bchannel_pipeline(bchannel, call->pipeline);
1466 bchannel_gain(bchannel, call->rx_gain, 0);
1468 bchannel_gain(bchannel, call->tx_gain, 1);
1469 if (call->bridge_id) {
1470 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1471 bchannel_join(bchannel, call->bridge_id);
1473 /* ignore all dsp features, if it is a loopback interface */
1474 if (param->bchannel.isloopback)
1477 /* create only, if call exists, othewhise it bchannel is freed below... */
1478 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1479 bchannel_activate(bchannel, 1);
1482 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1483 newparam.bchannel.handle = param->bchannel.handle;
1484 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1485 /* if call has released before bchannel is assigned */
1487 newparam.bchannel.type = BCHANNEL_RELEASE;
1488 newparam.bchannel.handle = param->bchannel.handle;
1489 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1494 case BCHANNEL_REMOVE:
1495 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1496 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1497 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1500 /* unklink from call and destroy bchannel */
1501 free_bchannel(bchannel);
1504 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1505 newparam.bchannel.handle = param->bchannel.handle;
1506 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1511 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1516 /* handle new ref */
1517 if (message_type == MESSAGE_NEWREF) {
1518 if (param->newref.direction) {
1519 /* new ref from lcr */
1520 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1521 if (!ref || find_call_ref(ref)) {
1522 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1525 /* allocate new call instance */
1526 call = alloc_call();
1528 call->state = CHAN_LCR_STATE_IN_PREPARE;
1531 call->ref_was_assigned = 1;
1532 /* set dtmf (default, use option 'n' to disable */
1534 /* wait for setup (or release from asterisk) */
1536 /* new ref, as requested from this remote application */
1537 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1538 call = find_call_ref(0);
1540 /* send release, if ref does not exist */
1541 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1546 call->ref_was_assigned = 1;
1547 /* set dtmf (default, use option 'n' to disable */
1549 /* send pending setup info */
1550 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1551 send_setup_to_lcr(call);
1552 /* release if asterisk has signed off */
1553 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1556 send_release_and_import(call, call->cause, call->location);
1558 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1569 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1572 call = find_call_ref(ref);
1574 /* ignore ref that is not used (anymore) */
1575 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1579 /* handle messages */
1580 switch(message_type) {
1582 lcr_in_setup(call, message_type, param);
1585 case MESSAGE_OVERLAP:
1586 lcr_in_overlap(call, message_type, param);
1589 case MESSAGE_PROCEEDING:
1590 lcr_in_proceeding(call, message_type, param);
1593 case MESSAGE_ALERTING:
1594 lcr_in_alerting(call, message_type, param);
1597 case MESSAGE_CONNECT:
1598 lcr_in_connect(call, message_type, param);
1601 case MESSAGE_DISCONNECT:
1602 lcr_in_disconnect(call, message_type, param);
1605 case MESSAGE_RELEASE:
1606 lcr_in_release(call, message_type, param);
1609 case MESSAGE_INFORMATION:
1610 lcr_in_information(call, message_type, param);
1613 case MESSAGE_NOTIFY:
1614 lcr_in_notify(call, message_type, param);
1617 case MESSAGE_FACILITY:
1618 lcr_in_facility(call, message_type, param);
1621 case MESSAGE_PATTERN: // audio available from LCR
1622 if (!call->has_pattern)
1623 lcr_in_pattern(call, message_type, param);
1626 case MESSAGE_NOPATTERN: // audio not available from LCR
1629 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1630 call->audiopath = param->audiopath;
1634 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1641 * release all calls (due to broken socket)
1643 static void release_all_calls(void)
1645 struct chan_call *call;
1650 /* no ast, so we may directly free call */
1652 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1656 /* already in release process */
1657 if (call->state == CHAN_LCR_STATE_RELEASE) {
1661 /* release or queue release */
1663 call->state = CHAN_LCR_STATE_RELEASE;
1664 if (!call->pbx_started) {
1665 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1666 ast_hangup(call->ast); // call will be destroyed here
1669 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1673 write(wake_pipe[1], &byte, 1);
1675 strcpy(call->queue_string, "H");
1679 /* release all bchannels */
1680 while(bchannel_first)
1681 free_bchannel(bchannel_first);
1684 void close_socket(void);
1687 * warning! not thread safe
1688 * returns -1 for socket error, 0 for no work, 1 for work
1690 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1693 struct admin_list *admin;
1694 struct admin_message msg;
1696 if ((what & LCR_FD_READ)) {
1697 /* read from socket */
1698 len = read(lcr_sock, &msg, sizeof(msg));
1700 CERROR(NULL, NULL, "Socket closed.(read)\n");
1702 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1704 release_all_calls();
1705 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1709 if (len != sizeof(msg)) {
1710 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1713 if (msg.message != ADMIN_MESSAGE) {
1714 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1717 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1719 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1724 if ((what & LCR_FD_WRITE)) {
1725 /* write to socket */
1727 socket_fd.when &= ~LCR_FD_WRITE;
1730 admin = admin_first;
1731 len = write(lcr_sock, &admin->msg, sizeof(msg));
1733 CERROR(NULL, NULL, "Socket closed.(write)\n");
1737 if (len != sizeof(msg)) {
1738 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1742 admin_first = admin->next;
1746 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1755 * open and close socket and thread
1757 int open_socket(void)
1760 struct sockaddr_un sock_address;
1761 union parameter param;
1764 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1765 CERROR(NULL, NULL, "Failed to create socket.\n");
1769 /* set socket address and name */
1770 memset(&sock_address, 0, sizeof(sock_address));
1771 sock_address.sun_family = PF_UNIX;
1772 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1774 /* connect socket */
1775 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1778 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1782 /* register socket fd */
1783 memset(&socket_fd, 0, sizeof(socket_fd));
1784 socket_fd.fd = lcr_sock;
1785 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1787 /* enque hello message */
1788 memset(¶m, 0, sizeof(param));
1789 strcpy(param.hello.application, "asterisk");
1790 send_message(MESSAGE_HELLO, 0, ¶m);
1795 void close_socket(void)
1797 struct admin_list *admin, *temp;
1799 unregister_fd(&socket_fd);
1801 /* flush pending messages */
1802 admin = admin_first;
1805 admin = admin->next;
1818 /* sending queue to asterisk */
1819 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1823 read(wake_pipe[0], &byte, 1);
1830 static void handle_queue()
1832 struct chan_call *call;
1833 struct ast_channel *ast;
1834 struct ast_frame fr;
1840 p = call->queue_string;
1843 if (ast_channel_trylock(ast)) {
1844 ast_mutex_unlock(&chan_lock);
1846 ast_mutex_lock(&chan_lock);
1852 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1853 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1856 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1857 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1860 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1861 ast_queue_control(ast, AST_CONTROL_RINGING);
1862 ast_setstate(ast, AST_STATE_RINGING);
1865 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1866 ast_queue_control(ast, AST_CONTROL_ANSWER);
1869 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1870 ast_queue_hangup(ast);
1872 case '1': case '2': case '3': case 'A':
1873 case '4': case '5': case '6': case 'B':
1874 case '7': case '8': case '9': case 'C':
1875 case '*': case '0': case '#': case 'D':
1876 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1877 /* send digit to asterisk */
1878 memset(&fr, 0, sizeof(fr));
1880 #ifdef LCR_FOR_ASTERISK
1881 fr.frametype = AST_FRAME_DTMF_BEGIN;
1884 #ifdef LCR_FOR_CALLWEAVER
1885 fr.frametype = AST_FRAME_DTMF;
1888 #ifdef AST_1_8_OR_HIGHER
1889 fr.subclass.integer = *p;
1893 fr.delivery = ast_tv(0, 0);
1894 ast_queue_frame(ast, &fr);
1896 #ifdef LCR_FOR_ASTERISK
1897 fr.frametype = AST_FRAME_DTMF_END;
1898 ast_queue_frame(ast, &fr);
1903 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1907 call->queue_string[0] = '\0';
1908 ast_channel_unlock(ast);
1914 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1916 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1917 if (open_socket() < 0)
1918 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1923 void lock_chan(void)
1925 ast_mutex_lock(&chan_lock);
1928 void unlock_chan(void)
1930 ast_mutex_unlock(&chan_lock);
1933 /* chan_lcr thread */
1934 static void *chan_thread(void *arg)
1936 if (pipe(wake_pipe) < 0) {
1937 CERROR(NULL, NULL, "Failed to open pipe.\n");
1940 memset(&wake_fd, 0, sizeof(wake_fd));
1941 wake_fd.fd = wake_pipe[0];
1942 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1944 memset(&socket_retry, 0, sizeof(socket_retry));
1945 add_timer(&socket_retry, handle_retry, NULL, 0);
1947 bchannel_pid = getpid();
1949 /* open socket the first time */
1950 handle_retry(NULL, NULL, 0);
1952 ast_mutex_lock(&chan_lock);
1956 select_main(0, &global_change, lock_chan, unlock_chan);
1963 * new asterisk instance
1966 #ifdef AST_1_8_OR_HIGHER
1967 #if ASTERISK_VERSION_NUM < 100000
1968 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
1970 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
1973 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1976 char exten[256], *dial, *interface, *opt;
1977 struct ast_channel *ast;
1978 struct chan_call *call;
1980 ast_mutex_lock(&chan_lock);
1981 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1983 /* if socket is closed */
1985 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1986 ast_mutex_unlock(&chan_lock);
1990 /* create call instance */
1991 call = alloc_call();
1993 /* failed to create instance */
1994 ast_mutex_unlock(&chan_lock);
1998 /* create asterisk channel instrance */
2000 #ifdef LCR_FOR_ASTERISK
2001 #ifdef AST_1_8_OR_HIGHER
2002 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2004 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2008 #ifdef LCR_FOR_CALLWEAVER
2009 ast = ast_channel_alloc(1);
2013 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2015 /* failed to create instance */
2016 ast_mutex_unlock(&chan_lock);
2019 ast->tech = &lcr_tech;
2020 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2021 /* configure channel */
2022 #if ASTERISK_VERSION_NUM < 100000
2023 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2024 ast->readformat = ast->rawreadformat = ast->nativeformats;
2025 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2027 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2028 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2029 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2030 ast_set_write_format(ast, &ast->rawwriteformat);
2031 ast_set_read_format(ast, &ast->rawreadformat);
2034 ast->hangupcause = 0;
2038 ast->tech_pvt = call;
2039 ast->fds[0] = call->pipe[0];
2040 call->pbx_started = 0;
2042 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2045 * Extract interface, dialstring, options from data.
2048 * <interface>/<dialstring>
2049 * <interface>/<dialstring>/options
2051 strncpy(exten, (char *)data, sizeof(exten)-1);
2052 exten[sizeof(exten)-1] = '\0';
2053 if ((dial = strchr(exten, '/'))) {
2056 if ((opt = strchr(dial, '/')))
2065 strncpy(call->interface, interface, sizeof(call->interface)-1);
2066 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2067 apply_opt(call, (char *)opt);
2069 #ifdef AST_1_8_OR_HIGHER
2070 // clone_variables(requestor, ast);
2073 ast->caller.ani.number.valid= requestor->caller.ani.number.valid;
2074 if (requestor->caller.ani.number.valid)
2075 if (requestor->caller.ani.number.str)
2076 if (requestor->caller.ani.number.str[0])
2077 ast->caller.ani.number.str= strdup(requestor->caller.ani.number.str);
2078 ast->caller.ani.number.plan= requestor->caller.ani.number.plan;
2079 ast->caller.ani.number.presentation= requestor->caller.ani.number.presentation;
2081 ast->caller.ani.name.valid= requestor->caller.ani.name.valid;
2082 if (requestor->caller.ani.name.valid)
2083 if (requestor->caller.ani.name.str)
2084 if (requestor->caller.ani.name.str[0])
2085 ast->caller.ani.name.str= strdup(requestor->caller.ani.name.str);
2086 ast->caller.ani.name.presentation= requestor->caller.ani.name.presentation;
2088 ast->caller.ani.subaddress.valid= requestor->caller.ani.subaddress.valid;
2089 if (requestor->caller.ani.subaddress.valid)
2090 if (requestor->caller.ani.subaddress.str)
2091 if (requestor->caller.ani.subaddress.str[0])
2092 ast->caller.ani.subaddress.str= strdup(requestor->caller.ani.subaddress.str);
2093 ast->caller.ani.subaddress.type= requestor->caller.ani.subaddress.type;
2095 ast->caller.id.number.valid= requestor->caller.id.number.valid;
2096 if (requestor->caller.id.number.valid)
2097 if (requestor->caller.id.number.str)
2098 if (requestor->caller.id.number.str[0])
2099 ast->caller.id.number.str= strdup(requestor->caller.id.number.str);
2100 ast->caller.id.number.plan= requestor->caller.id.number.plan;
2101 ast->caller.id.number.presentation= requestor->caller.id.number.presentation;
2103 ast->caller.id.name.valid= requestor->caller.id.name.valid;
2104 if (requestor->caller.id.name.valid)
2105 if (requestor->caller.id.name.str)
2106 if (requestor->caller.id.name.str[0])
2107 ast->caller.id.name.str= strdup(requestor->caller.id.name.str);
2108 ast->caller.id.name.presentation= requestor->caller.id.name.presentation;
2110 ast->caller.id.subaddress.valid= requestor->caller.id.subaddress.valid;
2111 if (requestor->caller.id.subaddress.valid)
2112 if (requestor->caller.id.subaddress.str)
2113 if (requestor->caller.id.subaddress.str[0])
2114 ast->caller.id.subaddress.str= strdup(requestor->caller.id.subaddress.str);
2115 ast->caller.id.subaddress.type= requestor->caller.id.subaddress.type;
2117 if (requestor->dialed.number.str)
2118 if (requestor->dialed.number.str[0])
2119 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2120 ast->dialed.number.plan= requestor->dialed.number.plan;
2122 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2123 if (requestor->dialed.subaddress.valid)
2124 if (requestor->dialed.subaddress.str)
2125 if (requestor->dialed.subaddress.str[0])
2126 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2127 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2129 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2130 ast->redirecting.count= requestor->redirecting.count;
2131 ast->redirecting.reason= requestor->redirecting.reason;
2133 ast->redirecting.from.number.valid= requestor->redirecting.from.number.valid;
2134 if (requestor->redirecting.from.number.valid)
2135 if (requestor->redirecting.from.number.str)
2136 if (requestor->redirecting.from.number.str[0])
2137 ast->redirecting.from.number.str= strdup(requestor->redirecting.from.number.str);
2138 ast->redirecting.from.number.plan= requestor->redirecting.from.number.plan;
2139 ast->redirecting.from.number.presentation= requestor->redirecting.from.number.presentation;
2141 ast->redirecting.to.number.valid= requestor->redirecting.to.number.valid;
2142 if (requestor->redirecting.to.number.valid)
2143 if (requestor->redirecting.to.number.str)
2144 if (requestor->redirecting.to.number.str[0])
2145 ast->redirecting.to.number.str= strdup(requestor->redirecting.to.number.str);
2146 ast->redirecting.to.number.plan= requestor->redirecting.to.number.plan;
2147 ast->redirecting.to.number.presentation= requestor->redirecting.to.number.presentation;
2149 /* store call information for setup */
2152 if (requestor && requestor->caller.id.number.valid) {
2153 if (requestor->caller.id.number.str)
2154 strncpy(call->callerinfo.id, requestor->caller.id.number.str, sizeof(call->callerinfo.id)-1);
2155 switch(requestor->caller.id.number.presentation & AST_PRES_RESTRICTION) {
2156 case AST_PRES_RESTRICTED:
2157 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2159 case AST_PRES_UNAVAILABLE:
2160 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2162 case AST_PRES_ALLOWED:
2164 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2166 switch(requestor->caller.id.number.presentation & AST_PRES_NUMBER_TYPE) {
2167 case AST_PRES_USER_NUMBER_UNSCREENED:
2168 call->callerinfo.screen = INFO_SCREEN_USER;
2170 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2171 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2173 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2174 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2177 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2179 switch((requestor->caller.id.number.plan >> 4) & 7) {
2181 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2184 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2187 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2190 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2193 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2196 if (requestor && requestor->caller.ani.number.valid) {
2197 if (requestor->caller.ani.number.str)
2198 strncpy(call->callerinfo.id2, requestor->caller.ani.number.str, sizeof(call->callerinfo.id2)-1);
2199 switch(requestor->caller.ani.number.presentation & AST_PRES_RESTRICTION) {
2200 case AST_PRES_RESTRICTED:
2201 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2203 case AST_PRES_UNAVAILABLE:
2204 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2206 case AST_PRES_ALLOWED:
2208 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2210 switch(requestor->caller.ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2211 case AST_PRES_USER_NUMBER_UNSCREENED:
2212 call->callerinfo.screen2 = INFO_SCREEN_USER;
2214 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2215 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2217 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2218 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2221 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2223 switch((requestor->caller.ani.number.plan >> 4) & 7) {
2225 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2228 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2231 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2234 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2237 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2240 if (requestor && requestor->caller.id.name.valid) {
2241 if (requestor->caller.id.name.str)
2242 strncpy(call->callerinfo.name, requestor->caller.id.name.str, sizeof(call->callerinfo.name)-1);
2246 if (requestor && requestor->redirecting.from.number.valid) {
2247 call->redirinfo.itype = INFO_ITYPE_CHAN;
2248 if (requestor->redirecting.from.number.str)
2249 strncpy(call->redirinfo.id, requestor->redirecting.from.number.str, sizeof(call->redirinfo.id)-1);
2250 switch(requestor->redirecting.from.number.presentation & AST_PRES_RESTRICTION) {
2251 case AST_PRES_RESTRICTED:
2252 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2254 case AST_PRES_UNAVAILABLE:
2255 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2257 case AST_PRES_ALLOWED:
2259 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2261 switch(requestor->redirecting.from.number.presentation & AST_PRES_NUMBER_TYPE) {
2262 case AST_PRES_USER_NUMBER_UNSCREENED:
2263 call->redirinfo.screen = INFO_SCREEN_USER;
2265 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2266 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2268 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2269 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2272 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2274 switch((requestor->redirecting.from.number.plan >> 4) & 7) {
2276 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2279 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2282 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2285 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2290 ast_mutex_unlock(&chan_lock);
2295 * call from asterisk
2297 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2299 union parameter newparam;
2300 struct chan_call *call;
2302 ast_mutex_lock(&chan_lock);
2303 call = ast->tech_pvt;
2305 #ifdef LCR_FOR_CALLWEAVER
2307 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2311 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2312 ast_mutex_unlock(&chan_lock);
2316 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2318 /* pbx process is started */
2319 call->pbx_started = 1;
2320 /* send MESSAGE_NEWREF */
2321 memset(&newparam, 0, sizeof(union parameter));
2322 newparam.newref.direction = 0; /* request from app */
2323 if (!strcmp(call->interface, "pbx"))
2324 newparam.newref.mode = 1;
2325 send_message(MESSAGE_NEWREF, 0, &newparam);
2327 /* set hdlc if capability requires hdlc */
2328 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2329 || ast->transfercapability == INFO_BC_DATARESTRICTED
2330 || ast->transfercapability == INFO_BC_VIDEO)
2332 /* if hdlc is forced by option, we change transcap to data */
2334 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2335 && ast->transfercapability != INFO_BC_DATARESTRICTED
2336 && ast->transfercapability != INFO_BC_VIDEO)
2337 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2339 #ifndef AST_1_8_OR_HIGHER
2340 call->cid_num[0] = 0;
2341 call->cid_name[0] = 0;
2342 call->cid_rdnis[0] = 0;
2344 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2345 strncpy(call->cid_num, ast->cid.cid_num,
2346 sizeof(call->cid_num)-1);
2347 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2348 strncpy(call->cid_name, ast->cid.cid_name,
2349 sizeof(call->cid_name)-1);
2350 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2351 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2352 sizeof(call->cid_rdnis)-1);
2355 ast_mutex_unlock(&chan_lock);
2359 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2361 static const char* dtmf_tones[] = {
2362 "!941+1336/100,!0/100", /* 0 */
2363 "!697+1209/100,!0/100", /* 1 */
2364 "!697+1336/100,!0/100", /* 2 */
2365 "!697+1477/100,!0/100", /* 3 */
2366 "!770+1209/100,!0/100", /* 4 */
2367 "!770+1336/100,!0/100", /* 5 */
2368 "!770+1477/100,!0/100", /* 6 */
2369 "!852+1209/100,!0/100", /* 7 */
2370 "!852+1336/100,!0/100", /* 8 */
2371 "!852+1477/100,!0/100", /* 9 */
2372 "!697+1633/100,!0/100", /* A */
2373 "!770+1633/100,!0/100", /* B */
2374 "!852+1633/100,!0/100", /* C */
2375 "!941+1633/100,!0/100", /* D */
2376 "!941+1209/100,!0/100", /* * */
2377 "!941+1477/100,!0/100" }; /* # */
2379 if (digit >= '0' && digit <='9')
2380 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2381 else if (digit >= 'A' && digit <= 'D')
2382 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2383 else if (digit == '*')
2384 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2385 else if (digit == '#')
2386 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2389 CDEBUG(NULL, ast, "Unable to handle DTMF tone "
2390 "'%c' for '%s'\n", digit, ast->name);
2394 #ifdef LCR_FOR_ASTERISK
2395 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2397 #ifdef LCR_FOR_CALLWEAVER
2398 static int lcr_digit(struct ast_channel *ast, char digit)
2401 struct chan_call *call;
2402 union parameter newparam;
2405 #ifdef LCR_FOR_CALLWEAVER
2406 int inband_dtmf = 0;
2409 /* only pass IA5 number space */
2410 if (digit > 126 || digit < 32)
2413 ast_mutex_lock(&chan_lock);
2414 call = ast->tech_pvt;
2416 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2417 ast_mutex_unlock(&chan_lock);
2421 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2423 /* send information or queue them */
2424 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2425 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2426 memset(&newparam, 0, sizeof(union parameter));
2428 newparam.information.keypad[0] = digit;
2429 newparam.information.keypad[1] = '\0';
2431 newparam.information.id[0] = digit;
2432 newparam.information.id[1] = '\0';
2434 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2437 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2438 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2440 strncat(call->dialque, buf, strlen(call->dialque)-1);
2443 ast_mutex_unlock(&chan_lock);
2445 #ifdef LCR_FOR_ASTERISK
2449 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2451 int inband_dtmf = 0;
2452 struct chan_call *call;
2455 ast_mutex_lock(&chan_lock);
2457 call = ast->tech_pvt;
2461 "Received digit from Asterisk, "
2462 "but no call instance exists.\n");
2463 ast_mutex_unlock(&chan_lock);
2467 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2469 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2473 ast_mutex_unlock(&chan_lock);
2476 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2477 send_digit_to_chan(ast, digit);
2483 static int lcr_answer(struct ast_channel *ast)
2485 union parameter newparam;
2486 struct chan_call *call;
2488 ast_mutex_lock(&chan_lock);
2489 call = ast->tech_pvt;
2491 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2492 ast_mutex_unlock(&chan_lock);
2496 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2498 /* copy connectinfo, if bridged */
2499 if (call->bridge_call)
2500 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2501 /* send connect message to lcr */
2502 if (call->state != CHAN_LCR_STATE_CONNECT) {
2503 memset(&newparam, 0, sizeof(union parameter));
2504 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2505 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2506 call->state = CHAN_LCR_STATE_CONNECT;
2509 /* request bchannel */
2510 if (!call->bchannel) {
2511 CDEBUG(call, ast, "Requesting B-channel.\n");
2512 memset(&newparam, 0, sizeof(union parameter));
2513 newparam.bchannel.type = BCHANNEL_REQUEST;
2514 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2517 // memset(&newparam, 0, sizeof(union parameter));
2518 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2520 ast_mutex_unlock(&chan_lock);
2524 static int lcr_hangup(struct ast_channel *ast)
2526 struct chan_call *call;
2527 pthread_t tid = pthread_self();
2529 if (!pthread_equal(tid, chan_tid)) {
2530 ast_mutex_lock(&chan_lock);
2532 call = ast->tech_pvt;
2534 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2535 if (!pthread_equal(tid, chan_tid)) {
2536 ast_mutex_unlock(&chan_lock);
2541 if (!pthread_equal(tid, chan_tid))
2542 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2544 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2546 /* disconnect asterisk, maybe not required */
2547 ast->tech_pvt = NULL;
2551 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2552 if (ast->hangupcause > 0)
2553 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2555 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2558 if (!pthread_equal(tid, chan_tid)) {
2559 ast_mutex_unlock(&chan_lock);
2563 /* ref is not set, due to prepare setup or release */
2564 if (call->state == CHAN_LCR_STATE_RELEASE) {
2565 /* we get the response to our release */
2566 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2569 /* during prepare, we change to release state */
2570 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2571 call->state = CHAN_LCR_STATE_RELEASE;
2575 if (!pthread_equal(tid, chan_tid)) {
2576 ast_mutex_unlock(&chan_lock);
2581 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2583 struct chan_call *call;
2584 struct ast_frame * f = fr;
2586 #ifdef AST_1_8_OR_HIGHER
2587 if (!f->subclass.integer)
2591 CDEBUG(NULL, ast, "No subclass\n");
2592 #ifdef AST_1_8_OR_HIGHER
2593 #if ASTERISK_VERSION_NUM < 100000
2594 if (!(f->subclass.integer & ast->nativeformats)) {
2596 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2599 if (!(f->subclass & ast->nativeformats)) {
2602 "Unexpected format. "
2603 "Activating emergency conversion...\n");
2605 #ifdef AST_1_8_OR_HIGHER
2606 #if ASTERISK_VERSION_NUM < 100000
2607 ast_set_write_format(ast, f->subclass.integer);
2609 ast_set_write_format(ast, &f->subclass.format);
2612 ast_set_write_format(ast, f->subclass);
2614 f = (ast->writetrans) ? ast_translate(
2615 ast->writetrans, fr, 0) : fr;
2618 ast_mutex_lock(&chan_lock);
2619 call = ast->tech_pvt;
2621 ast_mutex_unlock(&chan_lock);
2627 if (call->bchannel && f->samples)
2628 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2629 ast_mutex_unlock(&chan_lock);
2637 static struct ast_frame *lcr_read(struct ast_channel *ast)
2639 struct chan_call *call;
2642 ast_mutex_lock(&chan_lock);
2643 call = ast->tech_pvt;
2645 ast_mutex_unlock(&chan_lock);
2648 if (call->pipe[0] > -1) {
2649 if (call->rebuffer && !call->hdlc) {
2650 /* Make sure we have a complete 20ms (160byte) frame */
2651 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2653 call->framepos += len;
2656 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2658 if (len < 0 && errno == EAGAIN) {
2659 ast_mutex_unlock(&chan_lock);
2661 #ifdef LCR_FOR_ASTERISK
2662 return &ast_null_frame;
2665 #ifdef LCR_FOR_CALLWEAVER
2671 close(call->pipe[0]);
2674 ast_mutex_unlock(&chan_lock);
2676 } else if (call->rebuffer && call->framepos < 160) {
2677 /* Not a complete frame, so we send a null-frame */
2678 ast_mutex_unlock(&chan_lock);
2679 return &ast_null_frame;
2683 call->read_fr.frametype = AST_FRAME_VOICE;
2684 #ifdef AST_1_8_OR_HIGHER
2685 #if ASTERISK_VERSION_NUM < 100000
2686 call->read_fr.subclass.integer = ast->nativeformats;
2688 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2689 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2692 call->read_fr.subclass = ast->nativeformats;
2694 if (call->rebuffer) {
2695 call->read_fr.datalen = call->framepos;
2696 call->read_fr.samples = call->framepos;
2699 call->read_fr.datalen = len;
2700 call->read_fr.samples = len;
2702 call->read_fr.delivery = ast_tv(0,0);
2703 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2704 ast_mutex_unlock(&chan_lock);
2706 return &call->read_fr;
2709 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2711 union parameter newparam;
2713 struct chan_call *call;
2714 const struct tone_zone_sound *ts = NULL;
2716 ast_mutex_lock(&chan_lock);
2717 call = ast->tech_pvt;
2719 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2720 ast_mutex_unlock(&chan_lock);
2725 case AST_CONTROL_BUSY:
2726 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2727 ast_setstate(ast, AST_STATE_BUSY);
2728 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2729 /* send message to lcr */
2730 memset(&newparam, 0, sizeof(union parameter));
2731 newparam.disconnectinfo.cause = 17;
2732 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2733 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2735 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2737 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2738 ts = ast_get_indication_tone(ast->zone, "busy");
2741 case AST_CONTROL_CONGESTION:
2742 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2743 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2744 /* send message to lcr */
2745 memset(&newparam, 0, sizeof(union parameter));
2746 newparam.disconnectinfo.cause = ast->hangupcause;
2747 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2748 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2750 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2752 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2753 ts = ast_get_indication_tone(ast->zone, "congestion");
2756 case AST_CONTROL_PROCEEDING:
2757 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2758 if (call->state == CHAN_LCR_STATE_IN_SETUP
2759 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2760 /* send message to lcr */
2761 memset(&newparam, 0, sizeof(union parameter));
2762 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2764 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2767 case AST_CONTROL_RINGING:
2768 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2769 ast_setstate(ast, AST_STATE_RING);
2770 if (call->state == CHAN_LCR_STATE_IN_SETUP
2771 || call->state == CHAN_LCR_STATE_IN_DIALING
2772 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2773 /* send message to lcr */
2774 memset(&newparam, 0, sizeof(union parameter));
2775 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2777 call->state = CHAN_LCR_STATE_IN_ALERTING;
2779 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2780 ts = ast_get_indication_tone(ast->zone, "ring");
2783 case AST_CONTROL_PROGRESS:
2784 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2785 /* request bchannel */
2786 if (!call->bchannel) {
2787 CDEBUG(call, ast, "Requesting B-channel.\n");
2788 memset(&newparam, 0, sizeof(union parameter));
2789 newparam.bchannel.type = BCHANNEL_REQUEST;
2790 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2794 CDEBUG(call, ast, "Received indicate -1.\n");
2795 ast_playtones_stop(ast);
2799 case AST_CONTROL_VIDUPDATE:
2800 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2803 case AST_CONTROL_HOLD:
2804 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2805 /* send message to lcr */
2806 memset(&newparam, 0, sizeof(union parameter));
2807 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2808 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2810 /*start music onhold*/
2811 #ifdef LCR_FOR_ASTERISK
2812 ast_moh_start(ast,data,ast->musicclass);
2815 #ifdef LCR_FOR_CALLWEAVER
2816 ast_moh_start(ast, NULL);
2821 case AST_CONTROL_UNHOLD:
2822 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2823 /* send message to lcr */
2824 memset(&newparam, 0, sizeof(union parameter));
2825 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2826 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2832 #ifdef AST_CONTROL_SRCUPDATE
2833 case AST_CONTROL_SRCUPDATE:
2837 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2840 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2845 if (ts && ts->data[0]) {
2846 ast_playtones_start(ast, 0, ts->data, 1);
2850 ast_mutex_unlock(&chan_lock);
2857 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2859 struct chan_call *call;
2865 ast_mutex_lock(&chan_lock);
2866 call = ast->tech_pvt;
2868 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2869 ast_mutex_unlock(&chan_lock);
2873 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2875 ast_mutex_unlock(&chan_lock);
2880 * send_text asterisk
2882 static int lcr_send_text(struct ast_channel *ast, const char *text)
2884 struct chan_call *call;
2885 union parameter newparam;
2887 ast_mutex_lock(&chan_lock);
2888 call = ast->tech_pvt;
2890 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2891 ast_mutex_unlock(&chan_lock);
2895 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2896 memset(&newparam, 0, sizeof(union parameter));
2897 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2898 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2899 ast_mutex_unlock(&chan_lock);
2906 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2907 struct ast_channel *ast2, int flags,
2908 struct ast_frame **fo,
2909 struct ast_channel **rc, int timeoutms)
2912 struct chan_call *call1, *call2;
2913 struct ast_channel *carr[2], *who;
2915 struct ast_frame *f;
2918 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2923 /* join via dsp (if the channels are currently open) */
2924 ast_mutex_lock(&chan_lock);
2925 call1 = ast1->tech_pvt;
2926 call2 = ast2->tech_pvt;
2927 if (!call1 || !call2) {
2928 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2929 ast_mutex_unlock(&chan_lock);
2930 return AST_BRIDGE_COMPLETE;
2933 /* join, if both call instances uses dsp
2934 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
2936 if (!call1->nodsp && !call2->nodsp) {
2937 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2939 /* get bridge id and join */
2940 bridge_id = new_bridge_id();
2942 call1->bridge_id = bridge_id;
2943 if (call1->bchannel)
2944 bchannel_join(call1->bchannel, bridge_id);
2946 call2->bridge_id = bridge_id;
2947 if (call2->bchannel)
2948 bchannel_join(call2->bchannel, bridge_id);
2950 if (call1->nodsp && call2->nodsp)
2951 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2953 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2954 call1->bridge_call = call2;
2955 call2->bridge_call = call1;
2957 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2958 || call1->state == CHAN_LCR_STATE_IN_DIALING
2959 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2960 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2961 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2964 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2965 || call2->state == CHAN_LCR_STATE_IN_DIALING
2966 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2967 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2968 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2972 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2973 so let's do it for them. Hmpf.
2976 if (call1->on_hold) {
2977 union parameter newparam;
2979 memset(&newparam, 0, sizeof(union parameter));
2980 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2981 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2986 if (call2->on_hold) {
2987 union parameter newparam;
2989 memset(&newparam, 0, sizeof(union parameter));
2990 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2991 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2996 ast_mutex_unlock(&chan_lock);
3000 who = ast_waitfor_n(carr, 2, &to);
3003 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3008 if (!f || f->frametype == AST_FRAME_CONTROL) {
3010 CDEBUG(NULL, NULL, "Got hangup.\n");
3012 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3019 if ( f->frametype == AST_FRAME_DTMF ) {
3020 CDEBUG(NULL, NULL, "Got DTMF.\n");
3036 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3038 /* split channels */
3039 ast_mutex_lock(&chan_lock);
3040 call1 = ast1->tech_pvt;
3041 call2 = ast2->tech_pvt;
3042 if (call1 && call1->bridge_id) {
3043 call1->bridge_id = 0;
3044 if (call1->bchannel)
3045 bchannel_join(call1->bchannel, 0);
3046 if (call1->bridge_call)
3047 call1->bridge_call->bridge_call = NULL;
3049 if (call2 && call1->bridge_id) {
3050 call2->bridge_id = 0;
3051 if (call2->bchannel)
3052 bchannel_join(call2->bchannel, 0);
3053 if (call2->bridge_call)
3054 call2->bridge_call->bridge_call = NULL;
3056 call1->bridge_call = NULL;
3057 call2->bridge_call = NULL;
3059 ast_mutex_unlock(&chan_lock);
3060 return AST_BRIDGE_COMPLETE;
3062 static struct ast_channel_tech lcr_tech = {
3064 .description = "Channel driver for connecting to Linux-Call-Router",
3065 #if ASTERISK_VERSION_NUM < 100000
3066 .capabilities = AST_FORMAT_ALAW,
3068 .requester = lcr_request,
3070 #ifdef LCR_FOR_ASTERISK
3071 .send_digit_begin = lcr_digit_begin,
3072 .send_digit_end = lcr_digit_end,
3075 #ifdef LCR_FOR_CALLWEAVER
3076 .send_digit = lcr_digit,
3080 .bridge = lcr_bridge,
3081 .hangup = lcr_hangup,
3082 .answer = lcr_answer,
3085 .indicate = lcr_indicate,
3087 .send_text = lcr_send_text,
3096 static int lcr_show_lcr (int fd, int argc, char *argv[])
3101 static int lcr_show_calls (int fd, int argc, char *argv[])
3106 static int lcr_reload_routing (int fd, int argc, char *argv[])
3111 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3116 static int lcr_port_block (int fd, int argc, char *argv[])
3121 static int lcr_port_unblock (int fd, int argc, char *argv[])
3126 static int lcr_port_unload (int fd, int argc, char *argv[])
3131 static struct ast_cli_entry cli_show_lcr =
3132 { {"lcr", "show", "lcr", NULL},
3134 "Shows current states of LCR core",
3135 "Usage: lcr show lcr\n",
3138 static struct ast_cli_entry cli_show_calls =
3139 { {"lcr", "show", "calls", NULL},
3141 "Shows current calls made by LCR and Asterisk",
3142 "Usage: lcr show calls\n",
3145 static struct ast_cli_entry cli_reload_routing =
3146 { {"lcr", "reload", "routing", NULL},
3148 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3149 "Usage: lcr reload routing\n",
3152 static struct ast_cli_entry cli_reload_interfaces =
3153 { {"lcr", "reload", "interfaces", NULL},
3154 lcr_reload_interfaces,
3155 "Reloads interfaces conf of LCR",
3156 "Usage: lcr reload interfaces\n",
3159 static struct ast_cli_entry cli_port_block =
3160 { {"lcr", "port", "block", NULL},
3162 "Blocks LCR port for further calls",
3163 "Usage: lcr port block \"<port>\"\n",
3166 static struct ast_cli_entry cli_port_unblock =
3167 { {"lcr", "port", "unblock", NULL},
3169 "Unblocks or loads LCR port, port is opened my mISDN",
3170 "Usage: lcr port unblock \"<port>\"\n",
3173 static struct ast_cli_entry cli_port_unload =
3174 { {"lcr", "port", "unload", NULL},
3176 "Unloads LCR port, port is closes by mISDN",
3177 "Usage: lcr port unload \"<port>\"\n",
3182 #ifdef LCR_FOR_ASTERISK
3183 #ifdef AST_1_8_OR_HIGHER
3184 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3186 static int lcr_config_exec(struct ast_channel *ast, void *data)
3190 #ifdef LCR_FOR_CALLWEAVER
3191 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3194 struct chan_call *call;
3196 ast_mutex_lock(&chan_lock);
3198 #ifdef LCR_FOR_ASTERISK
3199 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3202 #ifdef LCR_FOR_CALLWEAVER
3203 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3209 if (call->ast == ast)
3215 #ifdef LCR_FOR_ASTERISK
3216 apply_opt(call, (char *)data);
3219 #ifdef LCR_FOR_CALLWEAVER
3220 apply_opt(call, (char *)argv[0]);
3224 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3226 ast_mutex_unlock(&chan_lock);
3231 * module loading and destruction
3233 int load_module(void)
3236 char options_error[256];
3238 for (i = 0; i < 256; i++) {
3239 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3240 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3243 if (read_options(options_error) == 0) {
3244 CERROR(NULL, NULL, "%s", options_error);
3246 #ifdef LCR_FOR_ASTERISK
3247 return AST_MODULE_LOAD_DECLINE;
3250 #ifdef LCR_FOR_CALLWEAVER
3256 ast_mutex_init(&chan_lock);
3257 ast_mutex_init(&log_lock);
3259 if (bchannel_initialize()) {
3260 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3263 #ifdef LCR_FOR_ASTERISK
3264 return AST_MODULE_LOAD_DECLINE;
3267 #ifdef LCR_FOR_CALLWEAVER
3273 #if ASTERISK_VERSION_NUM < 100000
3274 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3276 struct ast_format tmp;
3277 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3278 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3279 return AST_MODULE_LOAD_DECLINE;
3281 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3283 if (ast_channel_register(&lcr_tech)) {
3284 CERROR(NULL, NULL, "Unable to register channel class\n");
3285 bchannel_deinitialize();
3288 #ifdef LCR_FOR_ASTERISK
3289 return AST_MODULE_LOAD_DECLINE;
3292 #ifdef LCR_FOR_CALLWEAVER
3297 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3299 #ifdef LCR_FOR_ASTERISK
3300 "lcr_config(<opt><optarg>:<opt>:...)\n"
3303 #ifdef LCR_FOR_CALLWEAVER
3304 "lcr_config(<opt><optarg>:<opt>:...)\n",
3307 "Sets LCR opts. and optargs\n"
3309 "The available options are:\n"
3310 " d - Send display text on called phone, text is the optarg.\n"
3311 " n - Don't detect dtmf tones on called channel.\n"
3312 " h - Force data call (HDLC).\n"
3313 " t - Disable mISDN_dsp features (required for fax application).\n"
3314 " q - Add queue to make fax stream seamless (required for fax app).\n"
3315 " Use queue size in miliseconds for optarg. (try 250)\n"
3316 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3317 " Use time to detect for optarg.\n"
3318 " c - Make crypted outgoing call, optarg is keyindex.\n"
3319 " e - Perform echo cancelation on this channel.\n"
3320 " Takes mISDN pipeline option as optarg.\n"
3321 " s - Send Non Inband DTMF as inband.\n"
3322 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3323 " vr - rxgain control\n"
3324 " vt - txgain control\n"
3325 " Volume changes at factor 2 ^ optarg.\n"
3326 " k - use keypad to dial this call.\n"
3328 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3329 " -> use 16 for fax (3.1k audio)\n"
3331 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3332 "options: \"n:t:q250\" for seamless audio transmission.\n"
3337 ast_cli_register(&cli_show_lcr);
3338 ast_cli_register(&cli_show_calls);
3339 ast_cli_register(&cli_reload_routing);
3340 ast_cli_register(&cli_reload_interfaces);
3341 ast_cli_register(&cli_port_block);
3342 ast_cli_register(&cli_port_unblock);
3343 ast_cli_register(&cli_port_unload);
3346 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3347 /* failed to create thread */
3348 bchannel_deinitialize();
3350 ast_channel_unregister(&lcr_tech);
3352 #ifdef LCR_FOR_ASTERISK
3353 return AST_MODULE_LOAD_DECLINE;
3356 #ifdef LCR_FOR_CALLWEAVER
3364 int unload_module(void)
3366 /* First, take us out of the channel loop */
3367 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3369 pthread_cancel(chan_tid);
3373 del_timer(&socket_retry);
3375 unregister_fd(&wake_fd);
3376 close(wake_pipe[0]);
3377 close(wake_pipe[1]);
3379 // ast_mutex_unlock(&chan_lock);
3381 ast_channel_unregister(&lcr_tech);
3383 ast_unregister_application("lcr_config");
3385 if (mISDN_created) {
3386 bchannel_deinitialize();
3390 if (lcr_sock >= 0) {
3395 #if ASTERISK_VERSION_NUM >= 100000
3396 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3401 int reload_module(void)
3407 #ifdef LCR_FOR_ASTERISK
3408 #define AST_MODULE "chan_lcr"
3411 #ifdef LCR_FOR_CALLWEAVER
3416 ast_mutex_lock(&usecnt_lock);
3418 ast_mutex_unlock(&usecnt_lock);
3423 #ifdef LCR_FOR_ASTERISK
3424 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3425 .load = load_module,
3426 .unload = unload_module,
3427 .reload = reload_module,
3431 #ifdef LCR_FOR_CALLWEAVER
3432 char *description(void)