1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
141 * Fwd declare struct ast_channel to get rid of gcc warning about
142 * incompatible pointer type passed to ast_register_application2.
146 #include <asterisk/module.h>
147 #include <asterisk/channel.h>
148 #include <asterisk/config.h>
149 #include <asterisk/logger.h>
150 #include <asterisk/pbx.h>
151 #include <asterisk/options.h>
152 #include <asterisk/io.h>
153 #include <asterisk/frame.h>
154 #include <asterisk/translate.h>
155 #include <asterisk/cli.h>
156 #include <asterisk/musiconhold.h>
157 #include <asterisk/dsp.h>
158 #include <asterisk/translate.h>
159 #include <asterisk/file.h>
160 #ifdef LCR_FOR_ASTERISK
161 #include <asterisk/callerid.h>
163 #ifdef LCR_FOR_CALLWEAVER
164 #include <asterisk/phone_no_utils.h>
167 #include <asterisk/indications.h>
168 #include <asterisk/app.h>
169 #include <asterisk/features.h>
170 #include <asterisk/sched.h>
171 #if ASTERISK_VERSION_NUM < 110000
172 #include <asterisk/version.h>
174 #include "extension.h"
176 #include "callerid.h"
177 #include "lcrsocket.h"
180 #include "bchannel.h"
182 #include "chan_lcr.h"
184 CHAN_LCR_STATE // state description structure
185 MESSAGES // message text
187 #ifdef LCR_FOR_CALLWEAVER
188 AST_MUTEX_DEFINE_STATIC(rand_lock);
191 unsigned char flip_bits[256];
193 #ifdef LCR_FOR_CALLWEAVER
194 static struct ast_frame nullframe = { AST_FRAME_NULL, };
200 char lcr_type[]="lcr";
202 #ifdef LCR_FOR_CALLWEAVER
203 static ast_mutex_t usecnt_lock;
205 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
209 ast_mutex_t chan_lock; /* global lock */
210 ast_mutex_t log_lock; /* logging log */
212 * used to indicate change in file descriptors, so select function's result may
215 int global_change = 0;
218 struct lcr_fd wake_fd;
220 int glob_channel = 0;
223 struct lcr_fd socket_fd;
224 struct lcr_timer socket_retry;
227 struct admin_list *next;
228 struct admin_message msg;
229 } *admin_first = NULL;
231 static struct ast_channel_tech lcr_tech;
236 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, ...)
239 char call_text[128] = "NULL";
240 char ast_text[128] = "NULL";
243 ast_mutex_lock(&log_lock);
246 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
247 buffer[sizeof(buffer)-1]=0;
251 sprintf(call_text, "%d", call->ref);
253 #if ASTERISK_VERSION_NUM < 110000
254 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
256 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
258 ast_text[sizeof(ast_text)-1] = '\0';
260 // ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
261 printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
263 ast_mutex_unlock(&log_lock);
267 * channel and call instances
269 struct chan_call *call_first;
273 * special case: 0: find new ref, that has not been assigned a ref yet
276 struct chan_call *find_call_ref(unsigned int ref)
278 struct chan_call *call = call_first;
279 int assigned = (ref > 0);
282 if (call->ref == ref && call->ref_was_assigned == assigned)
289 void free_call(struct chan_call *call)
291 struct chan_call **temp = &call_first;
295 *temp = (*temp)->next;
296 if (call->pipe[0] > -1)
297 close(call->pipe[0]);
298 if (call->pipe[1] > -1)
299 close(call->pipe[1]);
300 if (call->bchannel) {
301 if (call->bchannel->call != call)
302 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
303 call->bchannel->call = NULL;
305 if (call->bridge_call) {
306 if (call->bridge_call->bridge_call != call)
307 CERROR(call, NULL, "Linked call structure has no link to us.\n");
308 call->bridge_call->bridge_call = NULL;
311 ast_translator_free_path(call->trans);
313 ast_dsp_free(call->dsp);
314 CDEBUG(call, NULL, "Call instance freed.\n");
319 temp = &((*temp)->next);
321 CERROR(call, NULL, "Call instance not found in list.\n");
324 struct chan_call *alloc_call(void)
326 struct chan_call **callp = &call_first;
329 callp = &((*callp)->next);
331 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
333 memset(*callp, 0, sizeof(struct chan_call));
334 if (pipe((*callp)->pipe) < 0) {
335 CERROR(*callp, NULL, "Failed to create pipe.\n");
339 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
340 CDEBUG(*callp, NULL, "Call instance allocated.\n");
344 unsigned short new_bridge_id(void)
346 struct chan_call *call;
347 unsigned short id = 1;
349 /* search for lowest bridge id that is not in use and not 0 */
353 if (call->bridge_id == id)
361 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
366 * enque message to LCR
368 int send_message(int message_type, unsigned int ref, union parameter *param)
370 struct admin_list *admin, **adminp;
373 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
376 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
378 adminp = &admin_first;
380 adminp = &((*adminp)->next);
381 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
383 CERROR(NULL, NULL, "No memory for message to LCR.\n");
388 admin->msg.message = ADMIN_MESSAGE;
389 admin->msg.u.msg.type = message_type;
390 admin->msg.u.msg.ref = ref;
391 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
392 socket_fd.when |= LCR_FD_WRITE;
396 write(wake_pipe[1], &byte, 1);
403 * apply options (in locked state)
405 void apply_opt(struct chan_call *call, char *data)
407 union parameter newparam;
408 char string[1024], *p = string, *opt, *key;
414 strncpy(string, data, sizeof(string)-1);
415 string[sizeof(string)-1] = '\0';
418 while((opt = strsep(&p, ":"))) {
421 if (opt[1] == '\0') {
422 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
425 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
426 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
427 strncpy(call->display, opt+1, sizeof(call->display)-1);
429 memset(&newparam, 0, sizeof(union parameter));
430 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
431 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
435 if (opt[1] != '\0') {
436 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
439 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
440 if (call->dsp_dtmf) {
443 bchannel_dtmf(call->bchannel, 0);
447 if (opt[1] == '\0') {
448 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
452 /* check for 0xXXXX... type of key */
453 if (!!strncmp((char *)key, "0x", 2)) {
454 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
458 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
459 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
464 if (*key>='0' && *key<='9')
465 call->bf_key[i] = (*key-'0') << 8;
466 else if (*key>='a' && *key<='f')
467 call->bf_key[i] = (*key-'a'+10) << 8;
468 else if (*key>='A' && *key<='F')
469 call->bf_key[i] = (*key-'A'+10) << 8;
473 if (*key>='0' && *key<='9')
474 call->bf_key[i] += (*key - '0');
475 else if (*key>='a' && *key<='f')
476 call->bf_key[i] += (*key - 'a' + 10);
477 else if (*key>='A' && *key<='F')
478 call->bf_key[i] += (*key - 'A' + 10);
485 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
489 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
491 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
494 if (opt[1] != '\0') {
495 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
498 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
503 if (opt[1] != '\0') {
504 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
507 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
512 if (opt[1] == '\0') {
513 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
516 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
517 call->nodsp_queue = atoi(opt+1);
520 if (opt[1] == '\0') {
521 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
524 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
525 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
527 bchannel_pipeline(call->bchannel, call->pipeline);
530 if (opt[1] == '\0') {
531 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
534 call->faxdetect=atoi(opt+1);
536 call->dsp=ast_dsp_new();
538 #ifdef LCR_FOR_CALLWEAVER
539 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
541 #ifdef LCR_FOR_ASTERISK
542 #ifdef DSP_FEATURE_DTMF_DETECT
543 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
545 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
550 #ifdef LCR_FOR_CALLWEAVER
551 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
553 #ifdef LCR_FOR_ASTERISK
554 #if ASTERISK_VERSION_NUM < 100000
555 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
557 struct ast_format src;
558 struct ast_format dst;
559 ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
560 ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
561 call->trans=ast_translator_build_path(&dst, &src);
566 CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
569 if (opt[1] != '\0') {
570 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
573 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
578 if (opt[1] != '\0') {
579 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
582 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
583 call->inband_dtmf = 1;
586 if (opt[1] != 'r' && opt[1] != 't') {
587 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
591 if (gain < -8 || gain >8) {
592 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
595 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
597 call->rx_gain = gain;
599 bchannel_gain(call->bchannel, call->rx_gain, 0);
601 call->tx_gain = gain;
603 bchannel_gain(call->bchannel, call->tx_gain, 1);
607 if (opt[1] != '\0') {
608 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
611 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
616 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
620 /* re-open, if bchannel is created */
621 if (call->bchannel && call->bchannel->b_sock > -1) {
622 bchannel_destroy(call->bchannel);
623 if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
624 bchannel_activate(call->bchannel, 1);
629 * send setup info to LCR
630 * this function is called, when asterisk call is received and ref is received
632 static void send_setup_to_lcr(struct chan_call *call)
634 union parameter newparam;
635 struct ast_channel *ast = call->ast;
638 if (!call->ast || !call->ref)
641 #ifdef AST_1_8_OR_HIGHER
642 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
644 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
647 /* send setup message to LCR */
648 memset(&newparam, 0, sizeof(union parameter));
649 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
650 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
652 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
654 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
655 if (!!strcmp(call->interface, "pbx"))
656 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
657 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
658 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
659 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
660 call->display[0] = '\0';
662 #ifdef AST_1_8_OR_HIGHER
663 /* set stored call information */
664 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
665 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
667 if (call->cid_num[0])
668 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
669 if (call->cid_name[0])
670 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
671 if (call->cid_rdnis[0]) {
672 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
673 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
674 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
676 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
677 case AST_PRES_RESTRICTED:
678 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
680 case AST_PRES_UNAVAILABLE:
681 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
683 case AST_PRES_ALLOWED:
685 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
687 switch(ast->cid.cid_ton) {
689 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
692 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
695 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
698 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
701 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
702 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
704 #if ASTERISK_VERSION_NUM < 110000
705 // ast->transfercapability = atoi(tmp);
706 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
708 // ast_channel_transfercapability_set(ast, atoi(tmp));
709 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
711 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
713 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
715 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
716 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
718 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
719 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
720 send_message(MESSAGE_SETUP, call->ref, &newparam);
722 /* change to outgoing setup state */
723 call->state = CHAN_LCR_STATE_OUT_SETUP;
727 * send dialing info to LCR
728 * this function is called, when setup acknowledge is received and dialing
731 static void send_dialque_to_lcr(struct chan_call *call)
733 union parameter newparam;
735 if (!call->ast || !call->ref || !call->dialque[0])
738 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
740 /* send setup message to LCR */
741 memset(&newparam, 0, sizeof(union parameter));
743 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
745 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
746 call->dialque[0] = '\0';
747 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
751 * in case of a bridge, the unsupported message can be forwarded directly
752 * to the remote call.
754 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
758 if (!call->bridge_call) return;
759 CDEBUG(call, NULL, "Sending message due bridging.\n");
760 send_message(message_type, call->bridge_call->ref, param);
764 * send release message to LCR and import bchannel if exported
766 static void send_release_and_import(struct chan_call *call, int cause, int location)
768 union parameter newparam;
770 /* importing channel */
771 if (call->bchannel) {
772 memset(&newparam, 0, sizeof(union parameter));
773 newparam.bchannel.type = BCHANNEL_RELEASE;
774 newparam.bchannel.handle = call->bchannel->handle;
775 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
777 /* sending release */
778 memset(&newparam, 0, sizeof(union parameter));
779 newparam.disconnectinfo.cause = cause;
780 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
781 send_message(MESSAGE_RELEASE, call->ref, &newparam);
785 * check if extension matches and start asterisk
786 * if it can match, proceed
789 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
792 union parameter newparam;
793 #if ASTERISK_VERSION_NUM < 110000
794 char *exten = ast->exten;
796 char s_exten[AST_MAX_EXTENSION];
799 strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
805 #if ASTERISK_VERSION_NUM < 110000
806 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
808 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
813 #if ASTERISK_VERSION_NUM < 110000
814 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
815 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
817 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
818 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
823 #if ASTERISK_VERSION_NUM < 110000
824 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
825 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);
827 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
828 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));
833 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
834 /* send setup acknowledge to lcr */
835 memset(&newparam, 0, sizeof(union parameter));
836 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
839 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
844 #if ASTERISK_VERSION_NUM < 110000
845 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
847 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
849 /* send setup acknowledge to lcr */
850 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
851 memset(&newparam, 0, sizeof(union parameter));
852 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
856 call->state = CHAN_LCR_STATE_IN_DIALING;
858 /* if match, start pbx */
859 #if ASTERISK_VERSION_NUM < 110000
860 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
862 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
864 CDEBUG(call, ast, "Extensions matches.\n");
869 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
873 #if ASTERISK_VERSION_NUM < 110000
876 if (!*ast_channel_exten(ast)) {
879 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
887 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
888 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
890 /* release asterisk */
891 #if ASTERISK_VERSION_NUM < 110000
892 ast->hangupcause = call->cause;
894 ast_channel_hangupcause_set(ast, call->cause);
896 /* change to release state */
897 call->state = CHAN_LCR_STATE_RELEASE;
898 ast_hangup(ast); // call will be destroyed here
902 /* send setup to asterisk */
903 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
905 #ifdef LCR_FOR_CALLWEAVER
907 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
910 ret = ast_pbx_start(ast);
912 cause = (ret==-2)?34:27;
915 call->pbx_started = 1;
916 ast_setstate(ast, AST_STATE_RING);
920 * incoming setup from LCR
922 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
924 struct ast_channel *ast;
925 struct ast_party_redirecting *ast_redir;
926 struct ast_party_caller *ast_caller;
927 #if ASTERISK_VERSION_NUM >= 110000
928 struct ast_party_redirecting s_ast_redir;
929 struct ast_party_caller s_ast_caller;
930 ast_party_redirecting_init(&s_ast_redir);
931 ast_party_caller_init(&s_ast_caller);
933 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
935 /* create asterisk channel instrance */
937 #ifdef LCR_FOR_CALLWEAVER
938 ast = ast_channel_alloc(1);
941 #ifdef LCR_FOR_ASTERISK
942 #ifdef AST_1_8_OR_HIGHER
943 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
945 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
949 #if ASTERISK_VERSION_NUM < 110000
950 ast_redir = &ast->redirecting;
951 ast_caller = &ast->caller;
953 ast_redir = &s_ast_redir;
954 ast_caller = &s_ast_caller;
959 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
960 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
967 #if ASTERISK_VERSION_NUM < 110000
968 ast->tech_pvt = call;
969 ast->tech = &lcr_tech;
970 ast->fds[0] = call->pipe[0];
972 ast_channel_tech_pvt_set(ast, call);
973 ast_channel_tech_set(ast, &lcr_tech);
974 ast_channel_set_fd(ast, 0, call->pipe[0]);
977 /* fill setup information */
978 if (param->setup.dialinginfo.id)
979 #if ASTERISK_VERSION_NUM < 110000
980 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
981 if (param->setup.context[0])
982 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
984 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
986 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
987 if (param->setup.context[0])
988 ast_channel_context_set(ast, param->setup.context);
990 ast_channel_context_set(ast, param->setup.callerinfo.interface);
994 #ifdef AST_1_8_OR_HIGHER
995 if (param->setup.callerinfo.id[0]) {
996 ast_caller->id.number.valid = 1;
997 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
998 if (!param->setup.callerinfo.id[0]) {
999 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1000 ast_caller->id.number.plan = (0 << 4) | 1;
1002 switch (param->setup.callerinfo.present) {
1003 case INFO_PRESENT_ALLOWED:
1004 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
1006 case INFO_PRESENT_RESTRICTED:
1007 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1010 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1012 switch (param->setup.callerinfo.screen) {
1013 case INFO_SCREEN_USER:
1014 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1016 case INFO_SCREEN_USER_VERIFIED_PASSED:
1017 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1019 case INFO_SCREEN_USER_VERIFIED_FAILED:
1020 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1023 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1025 switch (param->setup.callerinfo.ntype) {
1026 case INFO_NTYPE_SUBSCRIBER:
1027 ast_caller->id.number.plan = (4 << 4) | 1;
1029 case INFO_NTYPE_NATIONAL:
1030 ast_caller->id.number.plan = (2 << 4) | 1;
1032 case INFO_NTYPE_INTERNATIONAL:
1033 ast_caller->id.number.plan = (1 << 4) | 1;
1036 ast_caller->id.number.plan = (0 << 4) | 1;
1039 if (param->setup.callerinfo.id2[0]) {
1040 ast_caller->ani.number.valid = 1;
1041 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1042 switch (param->setup.callerinfo.present2) {
1043 case INFO_PRESENT_ALLOWED:
1044 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1046 case INFO_PRESENT_RESTRICTED:
1047 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1050 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1052 switch (param->setup.callerinfo.screen2) {
1053 case INFO_SCREEN_USER:
1054 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1056 case INFO_SCREEN_USER_VERIFIED_PASSED:
1057 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1059 case INFO_SCREEN_USER_VERIFIED_FAILED:
1060 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1063 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1065 switch (param->setup.callerinfo.ntype2) {
1066 case INFO_NTYPE_SUBSCRIBER:
1067 ast_caller->ani.number.plan = (4 << 4) | 1;
1069 case INFO_NTYPE_NATIONAL:
1070 ast_caller->ani.number.plan = (2 << 4) | 1;
1072 case INFO_NTYPE_INTERNATIONAL:
1073 ast_caller->ani.number.plan = (1 << 4) | 1;
1076 ast_caller->ani.number.plan = (0 << 4) | 1;
1079 if (param->setup.callerinfo.name[0]) {
1080 ast_caller->id.name.valid = 1;
1081 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1083 #if ASTERISK_VERSION_NUM >= 110000
1084 ast_channel_caller_set(ast, ast_caller);
1086 if (param->setup.redirinfo.id[0]) {
1087 ast_redir->from.number.valid = 1;
1088 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1089 switch (param->setup.redirinfo.present) {
1090 case INFO_PRESENT_ALLOWED:
1091 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1093 case INFO_PRESENT_RESTRICTED:
1094 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1097 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1099 switch (param->setup.redirinfo.screen) {
1100 case INFO_SCREEN_USER:
1101 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1103 case INFO_SCREEN_USER_VERIFIED_PASSED:
1104 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1106 case INFO_SCREEN_USER_VERIFIED_FAILED:
1107 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1110 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1112 switch (param->setup.redirinfo.ntype) {
1113 case INFO_NTYPE_SUBSCRIBER:
1114 ast_redir->from.number.plan = (4 << 4) | 1;
1116 case INFO_NTYPE_NATIONAL:
1117 ast_redir->from.number.plan = (2 << 4) | 1;
1119 case INFO_NTYPE_INTERNATIONAL:
1120 ast_redir->from.number.plan = (1 << 4) | 1;
1123 ast_redir->from.number.plan = (0 << 4) | 1;
1125 #if ASTERISK_VERSION_NUM >= 110000
1126 ast_channel_redirecting_set(ast, ast_redir);
1130 memset(&ast->cid, 0, sizeof(ast->cid));
1131 if (param->setup.callerinfo.id[0])
1132 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1133 if (param->setup.callerinfo.id2[0])
1134 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1135 if (param->setup.callerinfo.name[0])
1136 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1137 if (param->setup.redirinfo.id[0])
1138 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1139 switch (param->setup.callerinfo.present) {
1140 case INFO_PRESENT_ALLOWED:
1141 ast->cid.cid_pres = AST_PRES_ALLOWED;
1143 case INFO_PRESENT_RESTRICTED:
1144 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1147 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1149 switch (param->setup.callerinfo.ntype) {
1150 case INFO_NTYPE_SUBSCRIBER:
1151 ast->cid.cid_ton = 4;
1153 case INFO_NTYPE_NATIONAL:
1154 ast->cid.cid_ton = 2;
1156 case INFO_NTYPE_INTERNATIONAL:
1157 ast->cid.cid_ton = 1;
1160 ast->cid.cid_ton = 0;
1164 #if ASTERISK_VERSION_NUM < 110000
1165 ast->transfercapability = param->setup.capainfo.bearer_capa;
1167 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1169 /* enable hdlc if transcap is data */
1170 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1172 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1174 /* configure channel */
1175 #if ASTERISK_VERSION_NUM < 100000
1176 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1177 ast->readformat = ast->rawreadformat = ast->nativeformats;
1178 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1180 #if ASTERISK_VERSION_NUM < 110000
1181 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1182 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1183 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1184 ast_set_write_format(ast, &ast->rawwriteformat);
1185 ast_set_read_format(ast, &ast->rawreadformat);
1187 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1188 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1189 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1190 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1191 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1194 #if ASTERISK_VERSION_NUM < 110000
1196 ast->hangupcause = 0;
1198 ast_channel_priority_set(ast, 1);
1199 ast_channel_hangupcause_set(ast, 0);
1203 call->state = CHAN_LCR_STATE_IN_SETUP;
1205 if (!call->pbx_started)
1206 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1210 * incoming setup acknowledge from LCR
1212 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1214 if (!call->ast) return;
1216 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1218 /* send pending digits in dialque */
1219 if (call->dialque[0])
1220 send_dialque_to_lcr(call);
1221 /* change to overlap state */
1222 call->state = CHAN_LCR_STATE_OUT_DIALING;
1226 * incoming proceeding from LCR
1228 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1230 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1233 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1234 /* queue event for asterisk */
1235 if (call->ast && call->pbx_started) {
1239 write(wake_pipe[1], &byte, 1);
1241 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1247 * incoming alerting from LCR
1249 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1251 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1254 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1255 /* queue event to asterisk */
1256 if (call->ast && call->pbx_started) {
1260 write(wake_pipe[1], &byte, 1);
1262 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1267 * incoming connect from LCR
1269 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1271 union parameter newparam;
1273 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1276 call->state = CHAN_LCR_STATE_CONNECT;
1277 /* request bchannel */
1278 if (!call->bchannel) {
1279 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1280 memset(&newparam, 0, sizeof(union parameter));
1281 newparam.bchannel.type = BCHANNEL_REQUEST;
1282 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1284 /* copy connectinfo */
1285 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1286 /* queue event to asterisk */
1287 if (call->ast && call->pbx_started) {
1291 write(wake_pipe[1], &byte, 1);
1293 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1298 * incoming disconnect from LCR
1300 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1302 struct ast_channel *ast = call->ast;
1304 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1307 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1309 call->cause = param->disconnectinfo.cause;
1310 call->location = param->disconnectinfo.location;
1311 /* if bridge, forward disconnect and return */
1314 if (call->bridge_call) {
1315 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1316 bridge_message_if_bridged(call, message_type, param);
1320 /* release lcr with same cause */
1321 send_release_and_import(call, call->cause, call->location);
1323 /* change to release state */
1324 call->state = CHAN_LCR_STATE_RELEASE;
1325 /* queue release asterisk */
1327 #if ASTERISK_VERSION_NUM < 110000
1328 ast->hangupcause = call->cause;
1330 ast_channel_hangupcause_set(ast, call->cause);
1332 if (call->pbx_started) {
1336 write(wake_pipe[1], &byte, 1);
1338 strcpy(call->queue_string, "H"); // overwrite other indications
1340 ast_hangup(ast); // call will be destroyed here
1346 * incoming release from LCR
1348 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1350 struct ast_channel *ast = call->ast;
1352 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1356 /* change to release state */
1357 call->state = CHAN_LCR_STATE_RELEASE;
1358 /* copy release info */
1360 call->cause = param->disconnectinfo.cause;
1361 call->location = param->disconnectinfo.location;
1363 /* if we have an asterisk instance, queue hangup, else we are done */
1365 #if ASTERISK_VERSION_NUM < 110000
1366 ast->hangupcause = call->cause;
1368 ast_channel_hangupcause_set(ast, call->cause);
1370 if (call->pbx_started) {
1374 write(wake_pipe[1], &byte, 1);
1376 strcpy(call->queue_string, "H");
1378 ast_hangup(ast); // call will be destroyed here
1387 * incoming information from LCR
1389 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1391 struct ast_channel *ast = call->ast;
1393 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1397 /* pbx not started */
1398 if (!call->pbx_started) {
1399 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1400 #if ASTERISK_VERSION_NUM < 110000
1401 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1403 ast_channel_exten_set(ast, param->information.id);
1405 lcr_start_pbx(call, ast, param->information.sending_complete);
1409 /* change dailing state after setup */
1410 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1411 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1412 call->state = CHAN_LCR_STATE_IN_DIALING;
1413 // ast_setstate(ast, AST_STATE_DIALING);
1417 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1421 write(wake_pipe[1], &byte, 1);
1423 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1426 /* use bridge to forware message not supported by asterisk */
1427 if (call->state == CHAN_LCR_STATE_CONNECT) {
1428 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1429 bridge_message_if_bridged(call, message_type, param);
1434 * incoming information from LCR
1436 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1438 union parameter newparam;
1440 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1442 /* request bchannel, if call is resumed and we don't have it */
1443 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1444 CDEBUG(call, call->ast, "Reqesting bchannel at resume. (ref=%d)\n", call->ref);
1445 memset(&newparam, 0, sizeof(union parameter));
1446 newparam.bchannel.type = BCHANNEL_REQUEST;
1447 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1450 if (!call->ast) return;
1452 /* use bridge to forware message not supported by asterisk */
1453 bridge_message_if_bridged(call, message_type, param);
1457 * incoming information from LCR
1459 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1461 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1463 if (!call->ast) return;
1465 /* use bridge to forware message not supported by asterisk */
1466 bridge_message_if_bridged(call, message_type, param);
1470 * incoming pattern from LCR
1472 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1474 union parameter newparam;
1476 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1478 if (!call->ast) return;
1480 /* pattern are indicated only once */
1481 if (call->has_pattern)
1483 call->has_pattern = 1;
1485 /* request bchannel */
1486 if (!call->bchannel) {
1487 CDEBUG(call, call->ast, "Requesting B-channel. (ref=%d)\n", call->ref);
1488 memset(&newparam, 0, sizeof(union parameter));
1489 newparam.bchannel.type = BCHANNEL_REQUEST;
1490 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1492 /* queue PROGRESS, because tones are available */
1493 if (call->ast && call->pbx_started) {
1497 write(wake_pipe[1], &byte, 1);
1499 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1504 * got dtmf from bchannel (locked state)
1506 void lcr_in_dtmf(struct chan_call *call, int val)
1508 struct ast_channel *ast = call->ast;
1513 if (!call->pbx_started)
1516 if (!call->dsp_dtmf) {
1517 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1521 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1527 write(wake_pipe[1], &byte, 1);
1529 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1533 * message received from LCR
1535 int receive_message(int message_type, unsigned int ref, union parameter *param)
1537 struct bchannel *bchannel;
1538 struct chan_call *call;
1539 union parameter newparam;
1541 memset(&newparam, 0, sizeof(union parameter));
1543 /* handle bchannel message*/
1544 if (message_type == MESSAGE_BCHANNEL) {
1545 switch(param->bchannel.type) {
1546 case BCHANNEL_ASSIGN:
1547 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1548 if ((bchannel = find_bchannel_handle(param->bchannel.handle))) {
1549 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1552 /* create bchannel */
1553 bchannel = alloc_bchannel(param->bchannel.handle);
1555 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1559 /* configure channel */
1560 bchannel->b_tx_gain = param->bchannel.tx_gain;
1561 bchannel->b_rx_gain = param->bchannel.rx_gain;
1562 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1563 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key)) {
1564 bchannel->b_bf_len = param->bchannel.crypt_len;
1565 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1567 bchannel->b_txdata = 0;
1568 bchannel->b_tx_dejitter = 1;
1570 /* in case, ref is not set, this bchannel instance must
1571 * be created until it is removed again by LCR */
1573 call = find_call_ref(ref);
1575 bchannel->call = call;
1576 call->bchannel = bchannel;
1578 bchannel_dtmf(bchannel, 1);
1580 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1581 if (call->pipeline[0])
1582 bchannel_pipeline(bchannel, call->pipeline);
1584 bchannel_gain(bchannel, call->rx_gain, 0);
1586 bchannel_gain(bchannel, call->tx_gain, 1);
1587 if (call->bridge_id) {
1588 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1589 bchannel_join(bchannel, call->bridge_id);
1591 /* ignore all dsp features, if it is a loopback interface */
1592 if (param->bchannel.isloopback)
1595 /* create only, if call exists, othewhise it bchannel is freed below... */
1596 if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
1597 bchannel_activate(bchannel, 1);
1600 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1601 newparam.bchannel.handle = param->bchannel.handle;
1602 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1603 /* if call has released before bchannel is assigned */
1605 newparam.bchannel.type = BCHANNEL_RELEASE;
1606 newparam.bchannel.handle = param->bchannel.handle;
1607 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1612 case BCHANNEL_REMOVE:
1613 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1614 if (!(bchannel = find_bchannel_handle(param->bchannel.handle))) {
1615 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1618 /* unklink from call and destroy bchannel */
1619 free_bchannel(bchannel);
1622 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1623 newparam.bchannel.handle = param->bchannel.handle;
1624 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1629 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1634 /* handle new ref */
1635 if (message_type == MESSAGE_NEWREF) {
1636 if (param->newref.direction) {
1637 /* new ref from lcr */
1638 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1639 if (!ref || find_call_ref(ref)) {
1640 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1643 /* allocate new call instance */
1644 call = alloc_call();
1646 call->state = CHAN_LCR_STATE_IN_PREPARE;
1649 call->ref_was_assigned = 1;
1650 /* set dtmf (default, use option 'n' to disable */
1652 /* wait for setup (or release from asterisk) */
1654 /* new ref, as requested from this remote application */
1655 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1656 call = find_call_ref(0);
1658 /* send release, if ref does not exist */
1659 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1664 call->ref_was_assigned = 1;
1665 /* set dtmf (default, use option 'n' to disable */
1667 /* send pending setup info */
1668 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1669 send_setup_to_lcr(call);
1670 /* release if asterisk has signed off */
1671 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1674 send_release_and_import(call, call->cause, call->location);
1676 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1687 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1690 call = find_call_ref(ref);
1692 /* ignore ref that is not used (anymore) */
1693 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1697 /* handle messages */
1698 switch(message_type) {
1700 lcr_in_setup(call, message_type, param);
1703 case MESSAGE_OVERLAP:
1704 lcr_in_overlap(call, message_type, param);
1707 case MESSAGE_PROCEEDING:
1708 lcr_in_proceeding(call, message_type, param);
1711 case MESSAGE_ALERTING:
1712 lcr_in_alerting(call, message_type, param);
1715 case MESSAGE_CONNECT:
1716 lcr_in_connect(call, message_type, param);
1719 case MESSAGE_DISCONNECT:
1720 lcr_in_disconnect(call, message_type, param);
1723 case MESSAGE_RELEASE:
1724 lcr_in_release(call, message_type, param);
1727 case MESSAGE_INFORMATION:
1728 lcr_in_information(call, message_type, param);
1731 case MESSAGE_NOTIFY:
1732 lcr_in_notify(call, message_type, param);
1735 case MESSAGE_FACILITY:
1736 lcr_in_facility(call, message_type, param);
1739 case MESSAGE_PATTERN: // audio available from LCR
1740 if (!call->has_pattern)
1741 lcr_in_pattern(call, message_type, param);
1744 case MESSAGE_NOPATTERN: // audio not available from LCR
1747 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1748 call->audiopath = param->audiopath;
1752 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1759 * release all calls (due to broken socket)
1761 static void release_all_calls(void)
1763 struct chan_call *call;
1768 /* no ast, so we may directly free call */
1770 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1774 /* already in release process */
1775 if (call->state == CHAN_LCR_STATE_RELEASE) {
1779 /* release or queue release */
1781 call->state = CHAN_LCR_STATE_RELEASE;
1782 if (!call->pbx_started) {
1783 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1784 ast_hangup(call->ast); // call will be destroyed here
1787 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1791 write(wake_pipe[1], &byte, 1);
1793 strcpy(call->queue_string, "H");
1797 /* release all bchannels */
1798 while(bchannel_first)
1799 free_bchannel(bchannel_first);
1802 void close_socket(void);
1805 * warning! not thread safe
1806 * returns -1 for socket error, 0 for no work, 1 for work
1808 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1811 struct admin_list *admin;
1812 struct admin_message msg;
1814 if ((what & LCR_FD_READ)) {
1815 /* read from socket */
1816 len = read(lcr_sock, &msg, sizeof(msg));
1818 CERROR(NULL, NULL, "Socket closed.(read)\n");
1820 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1822 release_all_calls();
1823 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1827 if (len != sizeof(msg)) {
1828 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1831 if (msg.message != ADMIN_MESSAGE) {
1832 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1835 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1837 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1842 if ((what & LCR_FD_WRITE)) {
1843 /* write to socket */
1845 socket_fd.when &= ~LCR_FD_WRITE;
1848 admin = admin_first;
1849 len = write(lcr_sock, &admin->msg, sizeof(msg));
1851 CERROR(NULL, NULL, "Socket closed.(write)\n");
1855 if (len != sizeof(msg)) {
1856 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1860 admin_first = admin->next;
1864 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1873 * open and close socket and thread
1875 int open_socket(void)
1878 struct sockaddr_un sock_address;
1879 union parameter param;
1882 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1883 CERROR(NULL, NULL, "Failed to create socket.\n");
1887 /* set socket address and name */
1888 memset(&sock_address, 0, sizeof(sock_address));
1889 sock_address.sun_family = PF_UNIX;
1890 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1892 /* connect socket */
1893 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1896 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1900 /* register socket fd */
1901 memset(&socket_fd, 0, sizeof(socket_fd));
1902 socket_fd.fd = lcr_sock;
1903 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1905 /* enque hello message */
1906 memset(¶m, 0, sizeof(param));
1907 strcpy(param.hello.application, "asterisk");
1908 send_message(MESSAGE_HELLO, 0, ¶m);
1913 void close_socket(void)
1915 struct admin_list *admin, *temp;
1917 /* socket not created */
1921 unregister_fd(&socket_fd);
1923 /* flush pending messages */
1924 admin = admin_first;
1927 admin = admin->next;
1939 /* sending queue to asterisk */
1940 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1944 read(wake_pipe[0], &byte, 1);
1951 static void handle_queue()
1953 struct chan_call *call;
1954 struct ast_channel *ast;
1955 struct ast_frame fr;
1961 p = call->queue_string;
1964 if (ast_channel_trylock(ast)) {
1965 ast_mutex_unlock(&chan_lock);
1967 ast_mutex_lock(&chan_lock);
1973 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1974 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1977 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1978 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1981 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1982 ast_queue_control(ast, AST_CONTROL_RINGING);
1983 ast_setstate(ast, AST_STATE_RINGING);
1986 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1987 ast_queue_control(ast, AST_CONTROL_ANSWER);
1990 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1991 ast_queue_hangup(ast);
1993 case '1': case '2': case '3': case 'A':
1994 case '4': case '5': case '6': case 'B':
1995 case '7': case '8': case '9': case 'C':
1996 case '*': case '0': case '#': case 'D':
1997 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1998 /* send digit to asterisk */
1999 memset(&fr, 0, sizeof(fr));
2001 #ifdef LCR_FOR_ASTERISK
2002 fr.frametype = AST_FRAME_DTMF_BEGIN;
2005 #ifdef LCR_FOR_CALLWEAVER
2006 fr.frametype = AST_FRAME_DTMF;
2009 #ifdef AST_1_8_OR_HIGHER
2010 fr.subclass.integer = *p;
2014 fr.delivery = ast_tv(0, 0);
2015 ast_queue_frame(ast, &fr);
2017 #ifdef LCR_FOR_ASTERISK
2018 fr.frametype = AST_FRAME_DTMF_END;
2019 ast_queue_frame(ast, &fr);
2024 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
2028 call->queue_string[0] = '\0';
2029 ast_channel_unlock(ast);
2035 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
2037 CDEBUG(NULL, NULL, "Retry to open socket.\n");
2038 if (open_socket() < 0)
2039 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
2044 void lock_chan(void)
2046 ast_mutex_lock(&chan_lock);
2049 void unlock_chan(void)
2051 ast_mutex_unlock(&chan_lock);
2054 /* chan_lcr thread */
2055 static void *chan_thread(void *arg)
2057 if (pipe(wake_pipe) < 0) {
2058 CERROR(NULL, NULL, "Failed to open pipe.\n");
2061 memset(&wake_fd, 0, sizeof(wake_fd));
2062 wake_fd.fd = wake_pipe[0];
2063 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2065 memset(&socket_retry, 0, sizeof(socket_retry));
2066 add_timer(&socket_retry, handle_retry, NULL, 0);
2068 bchannel_pid = getpid();
2070 /* open socket the first time */
2071 handle_retry(NULL, NULL, 0);
2073 ast_mutex_lock(&chan_lock);
2077 select_main(0, &global_change, lock_chan, unlock_chan);
2084 * new asterisk instance
2087 #ifdef AST_1_8_OR_HIGHER
2088 #if ASTERISK_VERSION_NUM < 100000
2089 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2091 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, struct ast_channel *requestor, void *data, int *cause)
2094 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2097 char exten[256], *dial, *interface, *opt;
2098 struct ast_channel *ast;
2099 struct chan_call *call;
2100 struct ast_party_redirecting *req_redir;
2101 struct ast_party_caller *req_caller;
2103 ast_mutex_lock(&chan_lock);
2104 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2106 /* if socket is closed */
2108 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2109 ast_mutex_unlock(&chan_lock);
2113 /* create call instance */
2114 call = alloc_call();
2116 /* failed to create instance */
2117 ast_mutex_unlock(&chan_lock);
2121 /* create asterisk channel instrance */
2123 #ifdef LCR_FOR_ASTERISK
2124 #ifdef AST_1_8_OR_HIGHER
2125 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2127 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2131 #ifdef LCR_FOR_CALLWEAVER
2132 ast = ast_channel_alloc(1);
2136 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2138 /* failed to create instance */
2139 ast_mutex_unlock(&chan_lock);
2142 #if ASTERISK_VERSION_NUM < 110000
2143 ast->tech = &lcr_tech;
2144 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2145 req_redir = &requestor->redirecting;
2146 req_caller = &requestor->caller;
2148 ast_channel_tech_set(ast, &lcr_tech);
2149 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2150 req_redir = ast_channel_redirecting(requestor);
2151 req_caller = ast_channel_caller(requestor);
2153 /* configure channel */
2154 #if ASTERISK_VERSION_NUM < 100000
2155 #if ASTERISK_VERSION_NUM < 110000
2156 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2157 ast->readformat = ast->rawreadformat = ast->nativeformats;
2158 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2160 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2161 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2162 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2165 #if ASTERISK_VERSION_NUM < 110000
2166 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2167 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2168 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2169 ast_set_write_format(ast, &ast->rawwriteformat);
2170 ast_set_read_format(ast, &ast->rawreadformat);
2172 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2173 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2174 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2175 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2176 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2179 #if ASTERISK_VERSION_NUM < 110000
2181 ast->hangupcause = 0;
2183 ast_channel_priority_set(ast, 1);
2184 ast_channel_hangupcause_set(ast, 0);
2189 #if ASTERISK_VERSION_NUM < 110000
2190 ast->tech_pvt = call;
2191 ast->fds[0] = call->pipe[0];
2193 ast_channel_tech_pvt_set(ast, call);
2194 ast_channel_set_fd(ast, 0, call->pipe[0]);
2196 call->pbx_started = 0;
2198 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2201 * Extract interface, dialstring, options from data.
2204 * <interface>/<dialstring>
2205 * <interface>/<dialstring>/options
2207 strncpy(exten, (char *)data, sizeof(exten)-1);
2208 exten[sizeof(exten)-1] = '\0';
2209 if ((dial = strchr(exten, '/'))) {
2212 if ((opt = strchr(dial, '/')))
2221 strncpy(call->interface, interface, sizeof(call->interface)-1);
2222 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2223 apply_opt(call, (char *)opt);
2225 #ifdef AST_1_8_OR_HIGHER
2226 // clone_variables(requestor, ast);
2229 ast->caller.ani.number.valid= req_caller->ani.number.valid;
2230 if (req_caller->ani.number.valid)
2231 if (req_caller->ani.number.str)
2232 if (req_caller->ani.number.str[0])
2233 ast->caller.ani.number.str= strdup(req_caller->ani.number.str);
2234 ast->caller.ani.number.plan= req_caller->ani.number.plan;
2235 ast->caller.ani.number.presentation= req_caller->ani.number.presentation;
2237 ast->caller.ani.name.valid= req_caller->ani.name.valid;
2238 if (req_caller->ani.name.valid)
2239 if (req_caller->ani.name.str)
2240 if (req_caller->ani.name.str[0])
2241 ast->caller.ani.name.str= strdup(req_caller->ani.name.str);
2242 ast->caller.ani.name.presentation= req_caller->ani.name.presentation;
2244 ast->caller.ani.subaddress.valid= req_caller->ani.subaddress.valid;
2245 if (req_caller->ani.subaddress.valid)
2246 if (req_caller->ani.subaddress.str)
2247 if (req_caller->ani.subaddress.str[0])
2248 ast->caller.ani.subaddress.str= strdup(req_caller->ani.subaddress.str);
2249 ast->caller.ani.subaddress.type= req_caller->ani.subaddress.type;
2251 ast->caller.id.number.valid= req_caller->id.number.valid;
2252 if (req_caller->id.number.valid)
2253 if (req_caller->id.number.str)
2254 if (req_caller->id.number.str[0])
2255 ast->caller.id.number.str= strdup(req_caller->id.number.str);
2256 ast->caller.id.number.plan= req_caller->id.number.plan;
2257 ast->caller.id.number.presentation= req_caller->id.number.presentation;
2259 ast->caller.id.name.valid= req_caller->id.name.valid;
2260 if (req_caller->id.name.valid)
2261 if (req_caller->id.name.str)
2262 if (req_caller->id.name.str[0])
2263 ast->caller.id.name.str= strdup(req_caller->id.name.str);
2264 ast->caller.id.name.presentation= req_caller->id.name.presentation;
2266 ast->caller.id.subaddress.valid= req_caller->id.subaddress.valid;
2267 if (req_caller->id.subaddress.valid)
2268 if (req_caller->id.subaddress.str)
2269 if (req_caller->id.subaddress.str[0])
2270 ast->caller.id.subaddress.str= strdup(req_caller->id.subaddress.str);
2271 ast->caller.id.subaddress.type= req_caller->id.subaddress.type;
2273 if (requestor->dialed.number.str)
2274 if (requestor->dialed.number.str[0])
2275 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2276 ast->dialed.number.plan= requestor->dialed.number.plan;
2278 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2279 if (requestor->dialed.subaddress.valid)
2280 if (requestor->dialed.subaddress.str)
2281 if (requestor->dialed.subaddress.str[0])
2282 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2283 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2285 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2286 ast->redirecting.count= req_redir->count;
2287 ast->redirecting.reason= req_redir->reason;
2289 ast->redirecting.from.number.valid= req_redir->from.number.valid;
2290 if (req_redir->from.number.valid)
2291 if (req_redir->from.number.str)
2292 if (req_redir->from.number.str[0])
2293 ast->redirecting.from.number.str= strdup(req_redir->from.number.str);
2294 ast->redirecting.from.number.plan= req_redir->from.number.plan;
2295 ast->redirecting.from.number.presentation= req_redir->from.number.presentation;
2297 ast->redirecting.to.number.valid= req_redir->to.number.valid;
2298 if (req_redir->to.number.valid)
2299 if (req_redir->to.number.str)
2300 if (req_redir->to.number.str[0])
2301 ast->redirecting.to.number.str= strdup(req_redir->to.number.str);
2302 ast->redirecting.to.number.plan= req_redir->to.number.plan;
2303 ast->redirecting.to.number.presentation= req_redir->to.number.presentation;
2305 /* store call information for setup */
2308 if (requestor && req_caller->id.number.valid) {
2309 if (req_caller->id.number.str)
2310 strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2311 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2312 case AST_PRES_RESTRICTED:
2313 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2315 case AST_PRES_UNAVAILABLE:
2316 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2318 case AST_PRES_ALLOWED:
2320 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2322 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2323 case AST_PRES_USER_NUMBER_UNSCREENED:
2324 call->callerinfo.screen = INFO_SCREEN_USER;
2326 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2327 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2329 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2330 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2333 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2335 switch((req_caller->id.number.plan >> 4) & 7) {
2337 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2340 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2343 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2346 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2349 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2352 if (requestor && req_caller->ani.number.valid) {
2353 if (req_caller->ani.number.str)
2354 strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2355 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2356 case AST_PRES_RESTRICTED:
2357 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2359 case AST_PRES_UNAVAILABLE:
2360 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2362 case AST_PRES_ALLOWED:
2364 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2366 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2367 case AST_PRES_USER_NUMBER_UNSCREENED:
2368 call->callerinfo.screen2 = INFO_SCREEN_USER;
2370 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2371 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2373 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2374 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2377 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2379 switch((req_caller->ani.number.plan >> 4) & 7) {
2381 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2384 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2387 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2390 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2393 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2396 if (requestor && req_caller->id.name.valid) {
2397 if (req_caller->id.name.str)
2398 strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2402 if (requestor && req_redir->from.number.valid) {
2403 call->redirinfo.itype = INFO_ITYPE_CHAN;
2404 if (req_redir->from.number.str)
2405 strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2406 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2407 case AST_PRES_RESTRICTED:
2408 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2410 case AST_PRES_UNAVAILABLE:
2411 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2413 case AST_PRES_ALLOWED:
2415 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2417 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2418 case AST_PRES_USER_NUMBER_UNSCREENED:
2419 call->redirinfo.screen = INFO_SCREEN_USER;
2421 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2422 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2424 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2425 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2428 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2430 switch((req_redir->from.number.plan >> 4) & 7) {
2432 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2435 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2438 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2441 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2446 ast_mutex_unlock(&chan_lock);
2451 * call from asterisk
2453 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2455 union parameter newparam;
2456 struct chan_call *call;
2457 #if ASTERISK_VERSION_NUM >= 110000
2458 int transfercapability;
2461 ast_mutex_lock(&chan_lock);
2462 #if ASTERISK_VERSION_NUM < 110000
2463 call = ast->tech_pvt;
2465 call = ast_channel_tech_pvt(ast);
2468 #ifdef LCR_FOR_CALLWEAVER
2470 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2474 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2475 ast_mutex_unlock(&chan_lock);
2479 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2481 /* pbx process is started */
2482 call->pbx_started = 1;
2483 /* send MESSAGE_NEWREF */
2484 memset(&newparam, 0, sizeof(union parameter));
2485 newparam.newref.direction = 0; /* request from app */
2486 if (!strcmp(call->interface, "pbx"))
2487 newparam.newref.mode = 1;
2488 send_message(MESSAGE_NEWREF, 0, &newparam);
2490 /* set hdlc if capability requires hdlc */
2491 #if ASTERISK_VERSION_NUM < 110000
2492 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2493 || ast->transfercapability == INFO_BC_DATARESTRICTED
2494 || ast->transfercapability == INFO_BC_VIDEO)
2496 transfercapability=ast_channel_transfercapability(ast);
2497 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2498 || transfercapability == INFO_BC_DATARESTRICTED
2499 || transfercapability == INFO_BC_VIDEO)
2502 /* if hdlc is forced by option, we change transcap to data */
2504 #if ASTERISK_VERSION_NUM < 110000
2505 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2506 && ast->transfercapability != INFO_BC_DATARESTRICTED
2507 && ast->transfercapability != INFO_BC_VIDEO)
2508 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2510 && transfercapability != INFO_BC_DATAUNRESTRICTED
2511 && transfercapability != INFO_BC_DATARESTRICTED
2512 && transfercapability != INFO_BC_VIDEO)
2513 transfercapability = INFO_BC_DATAUNRESTRICTED;
2516 #ifndef AST_1_8_OR_HIGHER
2517 call->cid_num[0] = 0;
2518 call->cid_name[0] = 0;
2519 call->cid_rdnis[0] = 0;
2521 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2522 strncpy(call->cid_num, ast->cid.cid_num,
2523 sizeof(call->cid_num)-1);
2524 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2525 strncpy(call->cid_name, ast->cid.cid_name,
2526 sizeof(call->cid_name)-1);
2527 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2528 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2529 sizeof(call->cid_rdnis)-1);
2532 ast_mutex_unlock(&chan_lock);
2536 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2538 static const char* dtmf_tones[] = {
2539 "!941+1336/100,!0/100", /* 0 */
2540 "!697+1209/100,!0/100", /* 1 */
2541 "!697+1336/100,!0/100", /* 2 */
2542 "!697+1477/100,!0/100", /* 3 */
2543 "!770+1209/100,!0/100", /* 4 */
2544 "!770+1336/100,!0/100", /* 5 */
2545 "!770+1477/100,!0/100", /* 6 */
2546 "!852+1209/100,!0/100", /* 7 */
2547 "!852+1336/100,!0/100", /* 8 */
2548 "!852+1477/100,!0/100", /* 9 */
2549 "!697+1633/100,!0/100", /* A */
2550 "!770+1633/100,!0/100", /* B */
2551 "!852+1633/100,!0/100", /* C */
2552 "!941+1633/100,!0/100", /* D */
2553 "!941+1209/100,!0/100", /* * */
2554 "!941+1477/100,!0/100" }; /* # */
2556 if (digit >= '0' && digit <='9')
2557 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2558 else if (digit >= 'A' && digit <= 'D')
2559 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2560 else if (digit == '*')
2561 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2562 else if (digit == '#')
2563 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2565 #if ASTERISK_VERSION_NUM < 110000
2566 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2568 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2573 #ifdef LCR_FOR_ASTERISK
2574 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2576 #ifdef LCR_FOR_CALLWEAVER
2577 static int lcr_digit(struct ast_channel *ast, char digit)
2580 struct chan_call *call;
2581 union parameter newparam;
2584 #ifdef LCR_FOR_CALLWEAVER
2585 int inband_dtmf = 0;
2588 /* only pass IA5 number space */
2589 if (digit > 126 || digit < 32)
2592 ast_mutex_lock(&chan_lock);
2593 #if ASTERISK_VERSION_NUM < 110000
2594 call = ast->tech_pvt;
2596 call = ast_channel_tech_pvt(ast);
2599 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2600 ast_mutex_unlock(&chan_lock);
2604 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2606 /* send information or queue them */
2607 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2608 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2609 memset(&newparam, 0, sizeof(union parameter));
2611 newparam.information.keypad[0] = digit;
2612 newparam.information.keypad[1] = '\0';
2614 newparam.information.id[0] = digit;
2615 newparam.information.id[1] = '\0';
2617 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2620 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2621 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2623 strncat(call->dialque, buf, strlen(call->dialque)-1);
2626 ast_mutex_unlock(&chan_lock);
2628 #ifdef LCR_FOR_ASTERISK
2632 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2634 int inband_dtmf = 0;
2635 struct chan_call *call;
2638 ast_mutex_lock(&chan_lock);
2640 #if ASTERISK_VERSION_NUM < 110000
2641 call = ast->tech_pvt;
2643 call = ast_channel_tech_pvt(ast);
2648 "Received digit from Asterisk, "
2649 "but no call instance exists.\n");
2650 ast_mutex_unlock(&chan_lock);
2654 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2656 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2660 ast_mutex_unlock(&chan_lock);
2663 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2664 send_digit_to_chan(ast, digit);
2670 static int lcr_answer(struct ast_channel *ast)
2672 union parameter newparam;
2673 struct chan_call *call;
2675 ast_mutex_lock(&chan_lock);
2676 #if ASTERISK_VERSION_NUM < 110000
2677 call = ast->tech_pvt;
2679 call = ast_channel_tech_pvt(ast);
2682 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2683 ast_mutex_unlock(&chan_lock);
2687 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2689 /* copy connectinfo, if bridged */
2690 if (call->bridge_call)
2691 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2692 /* send connect message to lcr */
2693 if (call->state != CHAN_LCR_STATE_CONNECT) {
2694 memset(&newparam, 0, sizeof(union parameter));
2695 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2696 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2697 call->state = CHAN_LCR_STATE_CONNECT;
2700 /* request bchannel */
2701 if (!call->bchannel) {
2702 CDEBUG(call, ast, "Requesting B-channel.\n");
2703 memset(&newparam, 0, sizeof(union parameter));
2704 newparam.bchannel.type = BCHANNEL_REQUEST;
2705 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
2708 // memset(&newparam, 0, sizeof(union parameter));
2709 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2711 ast_mutex_unlock(&chan_lock);
2715 static int lcr_hangup(struct ast_channel *ast)
2717 struct chan_call *call;
2718 pthread_t tid = pthread_self();
2720 if (!pthread_equal(tid, chan_tid)) {
2721 ast_mutex_lock(&chan_lock);
2723 #if ASTERISK_VERSION_NUM < 110000
2724 call = ast->tech_pvt;
2726 call = ast_channel_tech_pvt(ast);
2729 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2730 if (!pthread_equal(tid, chan_tid)) {
2731 ast_mutex_unlock(&chan_lock);
2736 if (!pthread_equal(tid, chan_tid))
2737 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2739 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2741 /* disconnect asterisk, maybe not required */
2742 #if ASTERISK_VERSION_NUM < 110000
2743 ast->tech_pvt = NULL;
2746 ast_channel_tech_pvt_set(ast, NULL);
2747 ast_channel_set_fd(ast, 0, -1);
2751 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2752 #if ASTERISK_VERSION_NUM < 110000
2753 if (ast->hangupcause > 0)
2754 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2756 if (ast_channel_hangupcause(ast) > 0)
2757 send_release_and_import(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2760 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2763 if (!pthread_equal(tid, chan_tid)) {
2764 ast_mutex_unlock(&chan_lock);
2768 /* ref is not set, due to prepare setup or release */
2769 if (call->state == CHAN_LCR_STATE_RELEASE) {
2770 /* we get the response to our release */
2771 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2774 /* during prepare, we change to release state */
2775 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2776 call->state = CHAN_LCR_STATE_RELEASE;
2780 if (!pthread_equal(tid, chan_tid)) {
2781 ast_mutex_unlock(&chan_lock);
2786 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2788 struct chan_call *call;
2789 struct ast_frame * f = fr;
2791 #if ASTERISK_VERSION_NUM < 100000
2792 #ifdef AST_1_8_OR_HIGHER
2793 if (!f->subclass.codec)
2797 CDEBUG(NULL, ast, "No subclass\n");
2799 #ifdef AST_1_8_OR_HIGHER
2800 #if ASTERISK_VERSION_NUM < 100000
2801 #if ASTERISK_VERSION_NUM < 110000
2802 if (!(f->subclass.codec & ast->nativeformats)) {
2804 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2807 #if ASTERISK_VERSION_NUM < 110000
2808 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2810 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2814 #if ASTERISK_VERSION_NUM < 110000
2815 if (!(f->subclass & ast->nativeformats)) {
2817 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2821 "Unexpected format. "
2822 "Activating emergency conversion...\n");
2824 #ifdef AST_1_8_OR_HIGHER
2825 #if ASTERISK_VERSION_NUM < 100000
2826 ast_set_write_format(ast, f->subclass.codec);
2828 ast_set_write_format(ast, &f->subclass.format);
2831 ast_set_write_format(ast, f->subclass);
2833 #if ASTERISK_VERSION_NUM < 110000
2834 f = (ast->writetrans) ? ast_translate(
2835 ast->writetrans, fr, 0) : fr;
2837 f = (ast_channel_writetrans(ast)) ? ast_translate(
2838 ast_channel_writetrans(ast), fr, 0) : fr;
2842 ast_mutex_lock(&chan_lock);
2843 #if ASTERISK_VERSION_NUM < 110000
2844 call = ast->tech_pvt;
2846 call = ast_channel_tech_pvt(ast);
2849 ast_mutex_unlock(&chan_lock);
2855 if (call->bchannel && f->samples)
2856 bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
2857 ast_mutex_unlock(&chan_lock);
2865 static struct ast_frame *lcr_read(struct ast_channel *ast)
2867 struct chan_call *call;
2870 ast_mutex_lock(&chan_lock);
2871 #if ASTERISK_VERSION_NUM < 110000
2872 call = ast->tech_pvt;
2874 call = ast_channel_tech_pvt(ast);
2877 ast_mutex_unlock(&chan_lock);
2880 if (call->pipe[0] > -1) {
2881 if (call->rebuffer && !call->hdlc) {
2882 /* Make sure we have a complete 20ms (160byte) frame */
2883 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2885 call->framepos += len;
2888 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2890 if (len < 0 && errno == EAGAIN) {
2891 ast_mutex_unlock(&chan_lock);
2893 #ifdef LCR_FOR_ASTERISK
2894 return &ast_null_frame;
2897 #ifdef LCR_FOR_CALLWEAVER
2903 close(call->pipe[0]);
2906 ast_mutex_unlock(&chan_lock);
2908 } else if (call->rebuffer && call->framepos < 160) {
2909 /* Not a complete frame, so we send a null-frame */
2910 ast_mutex_unlock(&chan_lock);
2911 return &ast_null_frame;
2915 call->read_fr.frametype = AST_FRAME_VOICE;
2916 #ifdef AST_1_8_OR_HIGHER
2917 #if ASTERISK_VERSION_NUM < 100000
2918 #if ASTERISK_VERSION_NUM < 110000
2919 call->read_fr.subclass.codec = ast->nativeformats;
2921 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2924 #if ASTERISK_VERSION_NUM < 110000
2925 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2927 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2929 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2932 #if ASTERISK_VERSION_NUM < 110000
2933 call->read_fr.subclass = ast->nativeformats;
2935 call->read_fr.subclass = ast_channel_nativeformats(ast);
2938 if (call->rebuffer) {
2939 call->read_fr.datalen = call->framepos;
2940 call->read_fr.samples = call->framepos;
2943 call->read_fr.datalen = len;
2944 call->read_fr.samples = len;
2946 call->read_fr.delivery = ast_tv(0,0);
2947 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2948 ast_mutex_unlock(&chan_lock);
2950 return &call->read_fr;
2953 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2955 union parameter newparam;
2957 struct chan_call *call;
2958 const struct ast_tone_zone_sound *ts = NULL;
2960 ast_mutex_lock(&chan_lock);
2961 #if ASTERISK_VERSION_NUM < 110000
2962 call = ast->tech_pvt;
2964 call = ast_channel_tech_pvt(ast);
2967 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2968 ast_mutex_unlock(&chan_lock);
2973 case AST_CONTROL_BUSY:
2974 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2975 ast_setstate(ast, AST_STATE_BUSY);
2976 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2977 /* send message to lcr */
2978 memset(&newparam, 0, sizeof(union parameter));
2979 newparam.disconnectinfo.cause = 17;
2980 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2981 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2983 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2985 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2986 #if ASTERISK_VERSION_NUM < 110000
2987 ts = ast_get_indication_tone(ast->zone, "busy");
2989 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2993 case AST_CONTROL_CONGESTION:
2994 #if ASTERISK_VERSION_NUM < 110000
2995 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2997 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2999 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
3000 /* send message to lcr */
3001 memset(&newparam, 0, sizeof(union parameter));
3002 #if ASTERISK_VERSION_NUM < 110000
3003 newparam.disconnectinfo.cause = ast->hangupcause;
3005 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
3007 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
3008 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
3010 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
3012 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
3013 #if ASTERISK_VERSION_NUM < 110000
3014 ts = ast_get_indication_tone(ast->zone, "congestion");
3016 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
3020 case AST_CONTROL_PROCEEDING:
3021 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
3022 if (call->state == CHAN_LCR_STATE_IN_SETUP
3023 || call->state == CHAN_LCR_STATE_IN_DIALING) {
3024 /* send message to lcr */
3025 memset(&newparam, 0, sizeof(union parameter));
3026 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
3028 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
3031 case AST_CONTROL_RINGING:
3032 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
3033 ast_setstate(ast, AST_STATE_RING);
3034 if (call->state == CHAN_LCR_STATE_IN_SETUP
3035 || call->state == CHAN_LCR_STATE_IN_DIALING
3036 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
3037 /* send message to lcr */
3038 memset(&newparam, 0, sizeof(union parameter));
3039 send_message(MESSAGE_ALERTING, call->ref, &newparam);
3041 call->state = CHAN_LCR_STATE_IN_ALERTING;
3043 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
3044 #if ASTERISK_VERSION_NUM < 110000
3045 ts = ast_get_indication_tone(ast->zone, "ring");
3047 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
3051 case AST_CONTROL_PROGRESS:
3052 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
3053 /* request bchannel */
3054 if (!call->bchannel) {
3055 CDEBUG(call, ast, "Requesting B-channel.\n");
3056 memset(&newparam, 0, sizeof(union parameter));
3057 newparam.bchannel.type = BCHANNEL_REQUEST;
3058 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
3062 CDEBUG(call, ast, "Received indicate -1.\n");
3063 ast_playtones_stop(ast);
3067 case AST_CONTROL_VIDUPDATE:
3068 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
3071 case AST_CONTROL_HOLD:
3072 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
3073 /* send message to lcr */
3074 memset(&newparam, 0, sizeof(union parameter));
3075 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
3076 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3078 /*start music onhold*/
3079 #ifdef LCR_FOR_ASTERISK
3080 #if ASTERISK_VERSION_NUM <110000
3081 ast_moh_start(ast,data,ast->musicclass);
3083 ast_moh_start(ast,data,ast_channel_musicclass(ast));
3087 #ifdef LCR_FOR_CALLWEAVER
3088 ast_moh_start(ast, NULL);
3093 case AST_CONTROL_UNHOLD:
3094 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3095 /* send message to lcr */
3096 memset(&newparam, 0, sizeof(union parameter));
3097 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3098 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3104 #ifdef AST_CONTROL_SRCUPDATE
3105 case AST_CONTROL_SRCUPDATE:
3109 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3112 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3117 if (ts && ts->data[0]) {
3118 ast_playtones_start(ast, 0, ts->data, 1);
3122 ast_mutex_unlock(&chan_lock);
3129 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3131 struct chan_call *call;
3137 ast_mutex_lock(&chan_lock);
3138 #if ASTERISK_VERSION_NUM < 110000
3139 call = ast->tech_pvt;
3141 call = ast_channel_tech_pvt(ast);
3144 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3145 ast_mutex_unlock(&chan_lock);
3149 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3151 ast_mutex_unlock(&chan_lock);
3156 * send_text asterisk
3158 static int lcr_send_text(struct ast_channel *ast, const char *text)
3160 struct chan_call *call;
3161 union parameter newparam;
3163 ast_mutex_lock(&chan_lock);
3164 #if ASTERISK_VERSION_NUM < 110000
3165 call = ast->tech_pvt;
3167 call = ast_channel_tech_pvt(ast);
3170 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3171 ast_mutex_unlock(&chan_lock);
3175 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3176 memset(&newparam, 0, sizeof(union parameter));
3177 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3178 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3179 ast_mutex_unlock(&chan_lock);
3186 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3187 struct ast_channel *ast2, int flags,
3188 struct ast_frame **fo,
3189 struct ast_channel **rc, int timeoutms)
3192 struct chan_call *call1, *call2;
3193 struct ast_channel *carr[2], *who;
3195 struct ast_frame *f;
3198 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3203 /* join via dsp (if the channels are currently open) */
3204 ast_mutex_lock(&chan_lock);
3205 #if ASTERISK_VERSION_NUM < 110000
3206 call1 = ast1->tech_pvt;
3207 call2 = ast2->tech_pvt;
3209 call1 = ast_channel_tech_pvt(ast1);
3210 call2 = ast_channel_tech_pvt(ast2);
3212 if (!call1 || !call2) {
3213 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3214 ast_mutex_unlock(&chan_lock);
3215 return AST_BRIDGE_COMPLETE;
3218 /* join, if both call instances uses dsp
3219 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3221 if (!call1->nodsp && !call2->nodsp) {
3222 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3224 /* get bridge id and join */
3225 bridge_id = new_bridge_id();
3227 call1->bridge_id = bridge_id;
3228 if (call1->bchannel)
3229 bchannel_join(call1->bchannel, bridge_id);
3231 call2->bridge_id = bridge_id;
3232 if (call2->bchannel)
3233 bchannel_join(call2->bchannel, bridge_id);
3235 if (call1->nodsp && call2->nodsp)
3236 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3238 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3239 call1->bridge_call = call2;
3240 call2->bridge_call = call1;
3242 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3243 || call1->state == CHAN_LCR_STATE_IN_DIALING
3244 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3245 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3246 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3249 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3250 || call2->state == CHAN_LCR_STATE_IN_DIALING
3251 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3252 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3253 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3257 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3258 so let's do it for them. Hmpf.
3261 if (call1->on_hold) {
3262 union parameter newparam;
3264 memset(&newparam, 0, sizeof(union parameter));
3265 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3266 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3271 if (call2->on_hold) {
3272 union parameter newparam;
3274 memset(&newparam, 0, sizeof(union parameter));
3275 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3276 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3281 ast_mutex_unlock(&chan_lock);
3285 who = ast_waitfor_n(carr, 2, &to);
3288 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3293 if (!f || f->frametype == AST_FRAME_CONTROL) {
3295 CDEBUG(NULL, NULL, "Got hangup.\n");
3297 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3304 if ( f->frametype == AST_FRAME_DTMF ) {
3305 CDEBUG(NULL, NULL, "Got DTMF.\n");
3321 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3323 /* split channels */
3324 ast_mutex_lock(&chan_lock);
3325 #if ASTERISK_VERSION_NUM < 110000
3326 call1 = ast1->tech_pvt;
3327 call2 = ast2->tech_pvt;
3329 call1 = ast_channel_tech_pvt(ast1);
3330 call2 = ast_channel_tech_pvt(ast2);
3332 if (call1 && call1->bridge_id) {
3333 call1->bridge_id = 0;
3334 if (call1->bchannel)
3335 bchannel_join(call1->bchannel, 0);
3336 if (call1->bridge_call)
3337 call1->bridge_call->bridge_call = NULL;
3339 if (call2 && call1->bridge_id) {
3340 call2->bridge_id = 0;
3341 if (call2->bchannel)
3342 bchannel_join(call2->bchannel, 0);
3343 if (call2->bridge_call)
3344 call2->bridge_call->bridge_call = NULL;
3346 call1->bridge_call = NULL;
3347 call2->bridge_call = NULL;
3349 ast_mutex_unlock(&chan_lock);
3350 return AST_BRIDGE_COMPLETE;
3352 static struct ast_channel_tech lcr_tech = {
3354 .description = "Channel driver for connecting to Linux-Call-Router",
3355 #if ASTERISK_VERSION_NUM < 100000
3356 .capabilities = AST_FORMAT_ALAW,
3358 .requester = lcr_request,
3360 #ifdef LCR_FOR_ASTERISK
3361 .send_digit_begin = lcr_digit_begin,
3362 .send_digit_end = lcr_digit_end,
3365 #ifdef LCR_FOR_CALLWEAVER
3366 .send_digit = lcr_digit,
3370 .bridge = lcr_bridge,
3371 .hangup = lcr_hangup,
3372 .answer = lcr_answer,
3375 .indicate = lcr_indicate,
3377 .send_text = lcr_send_text,
3386 static int lcr_show_lcr (int fd, int argc, char *argv[])
3391 static int lcr_show_calls (int fd, int argc, char *argv[])
3396 static int lcr_reload_routing (int fd, int argc, char *argv[])
3401 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3406 static int lcr_port_block (int fd, int argc, char *argv[])
3411 static int lcr_port_unblock (int fd, int argc, char *argv[])
3416 static int lcr_port_unload (int fd, int argc, char *argv[])
3421 static struct ast_cli_entry cli_show_lcr =
3422 { {"lcr", "show", "lcr", NULL},
3424 "Shows current states of LCR core",
3425 "Usage: lcr show lcr\n",
3428 static struct ast_cli_entry cli_show_calls =
3429 { {"lcr", "show", "calls", NULL},
3431 "Shows current calls made by LCR and Asterisk",
3432 "Usage: lcr show calls\n",
3435 static struct ast_cli_entry cli_reload_routing =
3436 { {"lcr", "reload", "routing", NULL},
3438 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3439 "Usage: lcr reload routing\n",
3442 static struct ast_cli_entry cli_reload_interfaces =
3443 { {"lcr", "reload", "interfaces", NULL},
3444 lcr_reload_interfaces,
3445 "Reloads interfaces conf of LCR",
3446 "Usage: lcr reload interfaces\n",
3449 static struct ast_cli_entry cli_port_block =
3450 { {"lcr", "port", "block", NULL},
3452 "Blocks LCR port for further calls",
3453 "Usage: lcr port block \"<port>\"\n",
3456 static struct ast_cli_entry cli_port_unblock =
3457 { {"lcr", "port", "unblock", NULL},
3459 "Unblocks or loads LCR port, port is opened my mISDN",
3460 "Usage: lcr port unblock \"<port>\"\n",
3463 static struct ast_cli_entry cli_port_unload =
3464 { {"lcr", "port", "unload", NULL},
3466 "Unloads LCR port, port is closes by mISDN",
3467 "Usage: lcr port unload \"<port>\"\n",
3472 #ifdef LCR_FOR_ASTERISK
3473 #ifdef AST_1_8_OR_HIGHER
3474 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3476 static int lcr_config_exec(struct ast_channel *ast, void *data)
3480 #ifdef LCR_FOR_CALLWEAVER
3481 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3484 struct chan_call *call;
3486 ast_mutex_lock(&chan_lock);
3488 #ifdef LCR_FOR_ASTERISK
3489 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3492 #ifdef LCR_FOR_CALLWEAVER
3493 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3499 if (call->ast == ast)
3505 #ifdef LCR_FOR_ASTERISK
3506 apply_opt(call, (char *)data);
3509 #ifdef LCR_FOR_CALLWEAVER
3510 apply_opt(call, (char *)argv[0]);
3514 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3516 ast_mutex_unlock(&chan_lock);
3521 * module loading and destruction
3523 int load_module(void)
3526 char options_error[256];
3528 for (i = 0; i < 256; i++) {
3529 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3530 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3533 if (read_options(options_error) == 0) {
3534 CERROR(NULL, NULL, "%s", options_error);
3536 #ifdef LCR_FOR_ASTERISK
3537 return AST_MODULE_LOAD_DECLINE;
3540 #ifdef LCR_FOR_CALLWEAVER
3546 ast_mutex_init(&chan_lock);
3547 ast_mutex_init(&log_lock);
3549 if (bchannel_initialize()) {
3550 CERROR(NULL, NULL, "Unable to open mISDN device\n");
3553 #ifdef LCR_FOR_ASTERISK
3554 return AST_MODULE_LOAD_DECLINE;
3557 #ifdef LCR_FOR_CALLWEAVER
3563 #if ASTERISK_VERSION_NUM < 100000
3564 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3566 struct ast_format tmp;
3567 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3568 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3569 return AST_MODULE_LOAD_DECLINE;
3571 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3573 if (ast_channel_register(&lcr_tech)) {
3574 CERROR(NULL, NULL, "Unable to register channel class\n");
3575 bchannel_deinitialize();
3578 #ifdef LCR_FOR_ASTERISK
3579 return AST_MODULE_LOAD_DECLINE;
3582 #ifdef LCR_FOR_CALLWEAVER
3587 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3589 #ifdef LCR_FOR_ASTERISK
3590 "lcr_config(<opt><optarg>:<opt>:...)\n"
3593 #ifdef LCR_FOR_CALLWEAVER
3594 "lcr_config(<opt><optarg>:<opt>:...)\n",
3597 "Sets LCR opts. and optargs\n"
3599 "The available options are:\n"
3600 " d - Send display text on called phone, text is the optarg.\n"
3601 " n - Don't detect dtmf tones on called channel.\n"
3602 " h - Force data call (HDLC).\n"
3603 " t - Disable mISDN_dsp features (required for fax application).\n"
3604 " q - Add queue to make fax stream seamless (required for fax app).\n"
3605 " Use queue size in miliseconds for optarg. (try 250)\n"
3606 " f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3607 " Use time to detect for optarg.\n"
3608 " c - Make crypted outgoing call, optarg is keyindex.\n"
3609 " e - Perform echo cancelation on this channel.\n"
3610 " Takes mISDN pipeline option as optarg.\n"
3611 " s - Send Non Inband DTMF as inband.\n"
3612 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3613 " vr - rxgain control\n"
3614 " vt - txgain control\n"
3615 " Volume changes at factor 2 ^ optarg.\n"
3616 " k - use keypad to dial this call.\n"
3618 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3619 " -> use 16 for fax (3.1k audio)\n"
3621 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3622 "options: \"n:t:q250\" for seamless audio transmission.\n"
3627 ast_cli_register(&cli_show_lcr);
3628 ast_cli_register(&cli_show_calls);
3629 ast_cli_register(&cli_reload_routing);
3630 ast_cli_register(&cli_reload_interfaces);
3631 ast_cli_register(&cli_port_block);
3632 ast_cli_register(&cli_port_unblock);
3633 ast_cli_register(&cli_port_unload);
3636 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3637 /* failed to create thread */
3638 bchannel_deinitialize();
3640 ast_channel_unregister(&lcr_tech);
3642 #ifdef LCR_FOR_ASTERISK
3643 return AST_MODULE_LOAD_DECLINE;
3646 #ifdef LCR_FOR_CALLWEAVER
3654 int unload_module(void)
3656 /* First, take us out of the channel loop */
3657 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3659 pthread_cancel(chan_tid);
3663 del_timer(&socket_retry);
3665 unregister_fd(&wake_fd);
3666 close(wake_pipe[0]);
3667 close(wake_pipe[1]);
3669 // ast_mutex_unlock(&chan_lock);
3671 ast_channel_unregister(&lcr_tech);
3673 ast_unregister_application("lcr_config");
3675 if (mISDN_created) {
3676 bchannel_deinitialize();
3680 if (lcr_sock >= 0) {
3685 #if ASTERISK_VERSION_NUM >= 100000
3686 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3691 int reload_module(void)
3697 #ifdef LCR_FOR_ASTERISK
3698 #define AST_MODULE "chan_lcr"
3701 #ifdef LCR_FOR_CALLWEAVER
3706 ast_mutex_lock(&usecnt_lock);
3708 ast_mutex_unlock(&usecnt_lock);
3713 #ifdef LCR_FOR_ASTERISK
3714 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3715 .load = load_module,
3716 .unload = unload_module,
3717 .reload = reload_module,
3721 #ifdef LCR_FOR_CALLWEAVER
3722 char *description(void)