1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** Asterisk socket client **
10 \*****************************************************************************/
16 To connect, open an LCR socket and send a MESSAGE_HELLO to socket with
17 the application name. This name is unique an can be used for routing calls.
18 Now the channel driver is linked to LCR and can receive and make calls.
21 Call is initiated by LCR:
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1). The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
33 Call is initiated by Asterisk:
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
58 Call is released by LCR:
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
73 Call is released by Asterisk:
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
88 The deadlocking problem:
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
118 #include <sys/types.h>
120 //#include <signal.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
127 #include <semaphore.h>
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
141 * Fwd declare struct ast_channel to get rid of gcc warning about
142 * incompatible pointer type passed to ast_register_application2.
146 #include <asterisk/module.h>
147 #include <asterisk/channel.h>
148 #include <asterisk/config.h>
149 #include <asterisk/logger.h>
150 #include <asterisk/pbx.h>
151 #include <asterisk/options.h>
152 #include <asterisk/io.h>
153 #include <asterisk/frame.h>
154 #include <asterisk/translate.h>
155 #include <asterisk/cli.h>
156 #include <asterisk/musiconhold.h>
157 #include <asterisk/dsp.h>
158 #include <asterisk/translate.h>
159 #include <asterisk/file.h>
160 #ifdef LCR_FOR_ASTERISK
161 #include <asterisk/callerid.h>
163 #ifdef LCR_FOR_CALLWEAVER
164 #include <asterisk/phone_no_utils.h>
167 #include <asterisk/indications.h>
168 #include <asterisk/app.h>
169 #include <asterisk/features.h>
170 #include <asterisk/sched.h>
171 #if ASTERISK_VERSION_NUM < 110000
172 #include <asterisk/version.h>
174 #include "extension.h"
176 #include "callerid.h"
177 #include "lcrsocket.h"
181 #include "chan_lcr.h"
183 CHAN_LCR_STATE // state description structure
184 MESSAGES // message text
186 #ifdef LCR_FOR_CALLWEAVER
187 AST_MUTEX_DEFINE_STATIC(rand_lock);
190 unsigned char flip_bits[256];
192 #ifdef LCR_FOR_CALLWEAVER
193 static struct ast_frame nullframe = { AST_FRAME_NULL, };
198 char lcr_type[]="lcr";
200 #ifdef LCR_FOR_CALLWEAVER
201 static ast_mutex_t usecnt_lock;
203 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
207 ast_mutex_t chan_lock; /* global lock */
208 ast_mutex_t log_lock; /* logging log */
210 * used to indicate change in file descriptors, so select function's result may
213 int global_change = 0;
216 struct lcr_fd wake_fd;
218 int glob_channel = 0;
221 struct lcr_fd socket_fd;
222 struct lcr_timer socket_retry;
225 struct admin_list *next;
226 struct admin_message msg;
227 } *admin_first = NULL;
229 static struct ast_channel_tech lcr_tech;
234 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
237 char call_text[128] = "NULL";
238 char ast_text[128] = "NULL";
241 if (!option_debug) return;
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->bridge_call) {
301 if (call->bridge_call->bridge_call != call)
302 CERROR(call, NULL, "Linked call structure has no link to us.\n");
303 call->bridge_call->bridge_call = NULL;
306 ast_translator_free_path(call->trans);
308 ast_dsp_free(call->dsp);
309 CDEBUG(call, NULL, "Call instance freed.\n");
314 temp = &((*temp)->next);
316 CERROR(call, NULL, "Call instance not found in list.\n");
319 struct chan_call *alloc_call(void)
321 struct chan_call **callp = &call_first;
324 callp = &((*callp)->next);
326 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
328 memset(*callp, 0, sizeof(struct chan_call));
329 if (pipe((*callp)->pipe) < 0) {
330 CERROR(*callp, NULL, "Failed to create pipe.\n");
334 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
335 CDEBUG(*callp, NULL, "Call instance allocated.\n");
337 /* unset dtmf (default, use option 'd' to enable) */
338 (*callp)->dsp_dtmf = 0;
343 unsigned short new_bridge_id(void)
345 struct chan_call *call;
346 unsigned short id = 1;
348 /* search for lowest bridge id that is not in use and not 0 */
352 if (call->bridge_id == id)
360 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
365 * enque message to LCR
367 int send_message(int message_type, unsigned int ref, union parameter *param)
369 struct admin_list *admin, **adminp;
372 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
375 if (message_type != MESSAGE_TRAFFIC)
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;
397 rc = write(wake_pipe[1], &byte, 1);
404 * apply options (in locked state)
406 void apply_opt(struct chan_call *call, char *data)
408 union parameter newparam;
409 char string[1024], *p = string, *opt;//, *key;
415 strncpy(string, data, sizeof(string)-1);
416 string[sizeof(string)-1] = '\0';
419 while((opt = strsep(&p, ":"))) {
422 if (opt[1] == '\0') {
423 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
426 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
427 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
428 strncpy(call->display, opt+1, sizeof(call->display)-1);
430 memset(&newparam, 0, sizeof(union parameter));
431 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
432 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
436 if (opt[1] != '\0') {
437 CERROR(call, call->ast, "Option 'D' (DTMF) expects no parameter.\n", opt);
440 CDEBUG(call, call->ast, "Option 'D' (DTMF).\n");
445 if (opt[1] == '\0') {
446 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
450 /* check for 0xXXXX... type of key */
451 if (!!strncmp((char *)key, "0x", 2)) {
452 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
456 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
457 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
462 if (*key>='0' && *key<='9')
463 call->bf_key[i] = (*key-'0') << 8;
464 else if (*key>='a' && *key<='f')
465 call->bf_key[i] = (*key-'a'+10) << 8;
466 else if (*key>='A' && *key<='F')
467 call->bf_key[i] = (*key-'A'+10) << 8;
471 if (*key>='0' && *key<='9')
472 call->bf_key[i] += (*key - '0');
473 else if (*key>='a' && *key<='f')
474 call->bf_key[i] += (*key - 'a' + 10);
475 else if (*key>='A' && *key<='F')
476 call->bf_key[i] += (*key - 'A' + 10);
483 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
487 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
489 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
493 if (opt[1] != '\0') {
494 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
497 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
502 if (opt[1] == '\0') {
503 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
506 CDEBUG(call, call->ast, "Option 'q' (queue).\n");
507 call->tx_queue = atoi(opt+1);
511 if (opt[1] == '\0') {
512 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
515 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
516 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
518 bchannel_pipeline(call->bchannel, call->pipeline);
522 if (opt[1] != '\0') {
523 CERROR(call, call->ast, "Option 'f' (faxdetect) expects no parameter.\n", opt);
528 CDEBUG(call, call->ast, "Option 'f' (faxdetect).\n");
531 if (opt[1] != '\0') {
532 CERROR(call, call->ast, "Option 'a' (asterisk DTMF) expects no parameter.\n", opt);
537 CDEBUG(call, call->ast, "Option 'a' (Asterisk DTMF detection).\n");
540 if (opt[1] != '\0') {
541 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
544 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
549 if (opt[1] != '\0') {
550 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
553 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
554 call->inband_dtmf = 1;
558 if (opt[1] != 'r' && opt[1] != 't') {
559 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
563 if (gain < -8 || gain >8) {
564 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
567 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
569 call->rx_gain = gain;
571 bchannel_gain(call->bchannel, call->rx_gain, 0);
573 call->tx_gain = gain;
575 bchannel_gain(call->bchannel, call->tx_gain, 1);
580 if (opt[1] != '\0') {
581 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
584 CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
589 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
593 if (call->faxdetect || call->ast_dsp) {
595 call->dsp=ast_dsp_new();
597 #ifdef LCR_FOR_CALLWEAVER
598 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_CNG_DETECT : 0));
600 #ifdef LCR_FOR_ASTERISK
601 #ifdef DSP_FEATURE_DTMF_DETECT
602 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
604 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT | ((call->faxdetect) ? DSP_FEATURE_FAX_DETECT : 0));
609 #ifdef LCR_FOR_CALLWEAVER
610 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
612 #ifdef LCR_FOR_ASTERISK
613 #if ASTERISK_VERSION_NUM < 100000
614 call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
616 // struct ast_format src;
617 // struct ast_format dst;
618 // ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
619 // ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
620 // call->trans=ast_translator_build_path(&dst, &src);
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 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
656 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
657 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
658 call->display[0] = '\0';
660 #ifdef AST_1_8_OR_HIGHER
661 /* set stored call information */
662 memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
663 memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
665 if (call->cid_num[0])
666 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
667 if (call->cid_name[0])
668 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
669 if (call->cid_rdnis[0]) {
670 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
671 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
672 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
674 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
675 case AST_PRES_RESTRICTED:
676 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
678 case AST_PRES_UNAVAILABLE:
679 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
681 case AST_PRES_ALLOWED:
683 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
685 switch(ast->cid.cid_ton) {
687 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
690 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
693 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
696 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
699 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
700 // tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
702 #if ASTERISK_VERSION_NUM < 110000
703 // ast->transfercapability = atoi(tmp);
704 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
706 // ast_channel_transfercapability_set(ast, atoi(tmp));
707 newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
709 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
711 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
713 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
714 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
716 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
717 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
718 send_message(MESSAGE_SETUP, call->ref, &newparam);
719 if (call->tx_queue) {
720 memset(&newparam, 0, sizeof(union parameter));
721 newparam.queue = call->tx_queue * 8;
722 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
725 /* change to outgoing setup state */
726 call->state = CHAN_LCR_STATE_OUT_SETUP;
730 * send dialing info to LCR
731 * this function is called, when setup acknowledge is received and dialing
734 static void send_dialque_to_lcr(struct chan_call *call)
736 union parameter newparam;
738 if (!call->ast || !call->ref || !call->dialque[0])
741 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
743 /* send setup message to LCR */
744 memset(&newparam, 0, sizeof(union parameter));
746 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
748 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
749 call->dialque[0] = '\0';
750 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
754 * in case of a bridge, the unsupported message can be forwarded directly
755 * to the remote call.
757 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
761 if (!call->bridge_call) return;
762 CDEBUG(call, NULL, "Sending message due bridging.\n");
763 send_message(message_type, call->bridge_call->ref, param);
767 * send release message to LCR
769 static void send_release(struct chan_call *call, int cause, int location)
771 union parameter newparam;
773 /* sending release */
774 memset(&newparam, 0, sizeof(union parameter));
775 newparam.disconnectinfo.cause = cause;
776 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
777 send_message(MESSAGE_RELEASE, call->ref, &newparam);
781 * check if extension matches and start asterisk
782 * if it can match, proceed
785 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
788 union parameter newparam;
789 #if ASTERISK_VERSION_NUM < 110000
790 char *exten = ast->exten;
792 char s_exten[AST_MAX_EXTENSION];
795 strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
801 #if ASTERISK_VERSION_NUM < 110000
802 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
804 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
809 #if ASTERISK_VERSION_NUM < 110000
810 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
811 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
813 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
814 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
819 #if ASTERISK_VERSION_NUM < 110000
820 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
821 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);
823 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
824 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));
829 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
830 /* send setup acknowledge to lcr */
831 memset(&newparam, 0, sizeof(union parameter));
832 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
835 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
840 #if ASTERISK_VERSION_NUM < 110000
841 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
843 if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
845 /* send setup acknowledge to lcr */
846 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
847 memset(&newparam, 0, sizeof(union parameter));
848 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
852 call->state = CHAN_LCR_STATE_IN_DIALING;
854 /* if match, start pbx */
855 #if ASTERISK_VERSION_NUM < 110000
856 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
858 if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
860 CDEBUG(call, ast, "Extensions matches.\n");
864 /* send setup acknowledge to lcr */
865 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
866 memset(&newparam, 0, sizeof(union parameter));
867 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
871 call->state = CHAN_LCR_STATE_IN_DIALING;
874 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
878 #if ASTERISK_VERSION_NUM < 110000
881 if (!*ast_channel_exten(ast)) {
883 /* send setup acknowledge to lcr */
884 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
885 memset(&newparam, 0, sizeof(union parameter));
886 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
890 call->state = CHAN_LCR_STATE_IN_DIALING;
893 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
901 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
902 send_release(call, cause, LOCATION_PRIVATE_LOCAL);
904 /* release asterisk */
905 #if ASTERISK_VERSION_NUM < 110000
906 ast->hangupcause = call->cause;
908 ast_channel_hangupcause_set(ast, call->cause);
910 /* change to release state */
911 call->state = CHAN_LCR_STATE_RELEASE;
912 ast_hangup(ast); // call will be destroyed here
916 /* send setup to asterisk */
917 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
919 #ifdef LCR_FOR_CALLWEAVER
921 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
924 ret = ast_pbx_start(ast);
926 cause = (ret==-2)?34:27;
929 call->pbx_started = 1;
930 ast_setstate(ast, AST_STATE_RING);
934 * incoming setup from LCR
936 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
938 struct ast_channel *ast;
939 #ifdef AST_1_8_OR_HIGHER
940 struct ast_party_redirecting *ast_redir;
941 struct ast_party_caller *ast_caller;
943 struct ast_callerid *ast_caller;
945 #if ASTERISK_VERSION_NUM >= 110000
946 struct ast_party_redirecting s_ast_redir;
947 struct ast_party_caller s_ast_caller;
948 ast_party_redirecting_init(&s_ast_redir);
949 ast_party_caller_init(&s_ast_caller);
951 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
953 /* create asterisk channel instrance */
955 #ifdef LCR_FOR_CALLWEAVER
956 ast = ast_channel_alloc(1);
959 #ifdef LCR_FOR_ASTERISK
960 #ifdef AST_1_8_OR_HIGHER
961 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
963 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
967 #if ASTERISK_VERSION_NUM < 110000
968 #ifdef AST_1_8_OR_HIGHER
969 ast_redir = &ast->redirecting;
970 ast_caller = &ast->caller;
972 ast_caller = &ast->cid;
975 ast_redir = &s_ast_redir;
976 ast_caller = &s_ast_caller;
981 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
982 send_release(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
989 #if ASTERISK_VERSION_NUM < 110000
990 ast->tech_pvt = call;
991 ast->tech = &lcr_tech;
992 ast->fds[0] = call->pipe[0];
994 ast_channel_tech_pvt_set(ast, call);
995 ast_channel_tech_set(ast, &lcr_tech);
996 ast_channel_set_fd(ast, 0, call->pipe[0]);
999 /* fill setup information */
1000 if (param->setup.dialinginfo.id)
1001 #if ASTERISK_VERSION_NUM < 110000
1002 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
1003 if (param->setup.dialinginfo.context[0])
1004 strncpy(ast->context, param->setup.dialinginfo.context, AST_MAX_CONTEXT-1);
1006 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
1008 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
1009 if (param->setup.dialinginfo.context[0])
1010 ast_channel_context_set(ast, param->setup.dialinginfo.context);
1012 ast_channel_context_set(ast, param->setup.callerinfo.interface);
1016 #ifdef AST_1_8_OR_HIGHER
1017 if (param->setup.callerinfo.id[0]) {
1018 ast_caller->id.number.valid = 1;
1019 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
1020 if (!param->setup.callerinfo.id[0]) {
1021 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1022 ast_caller->id.number.plan = (0 << 4) | 1;
1024 switch (param->setup.callerinfo.present) {
1025 case INFO_PRESENT_ALLOWED:
1026 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
1028 case INFO_PRESENT_RESTRICTED:
1029 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1032 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1034 switch (param->setup.callerinfo.screen) {
1035 case INFO_SCREEN_USER:
1036 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1038 case INFO_SCREEN_USER_VERIFIED_PASSED:
1039 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1041 case INFO_SCREEN_USER_VERIFIED_FAILED:
1042 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1045 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1047 switch (param->setup.callerinfo.ntype) {
1048 case INFO_NTYPE_SUBSCRIBER:
1049 ast_caller->id.number.plan = (4 << 4) | 1;
1051 case INFO_NTYPE_NATIONAL:
1052 ast_caller->id.number.plan = (2 << 4) | 1;
1054 case INFO_NTYPE_INTERNATIONAL:
1055 ast_caller->id.number.plan = (1 << 4) | 1;
1058 ast_caller->id.number.plan = (0 << 4) | 1;
1061 if (param->setup.callerinfo.id2[0]) {
1062 ast_caller->ani.number.valid = 1;
1063 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1064 switch (param->setup.callerinfo.present2) {
1065 case INFO_PRESENT_ALLOWED:
1066 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1068 case INFO_PRESENT_RESTRICTED:
1069 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1072 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1074 switch (param->setup.callerinfo.screen2) {
1075 case INFO_SCREEN_USER:
1076 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1078 case INFO_SCREEN_USER_VERIFIED_PASSED:
1079 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1081 case INFO_SCREEN_USER_VERIFIED_FAILED:
1082 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1085 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1087 switch (param->setup.callerinfo.ntype2) {
1088 case INFO_NTYPE_SUBSCRIBER:
1089 ast_caller->ani.number.plan = (4 << 4) | 1;
1091 case INFO_NTYPE_NATIONAL:
1092 ast_caller->ani.number.plan = (2 << 4) | 1;
1094 case INFO_NTYPE_INTERNATIONAL:
1095 ast_caller->ani.number.plan = (1 << 4) | 1;
1098 ast_caller->ani.number.plan = (0 << 4) | 1;
1101 if (param->setup.callerinfo.name[0]) {
1102 ast_caller->id.name.valid = 1;
1103 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1105 #if ASTERISK_VERSION_NUM >= 110000
1106 ast_channel_caller_set(ast, ast_caller);
1108 if (param->setup.redirinfo.id[0]) {
1109 ast_redir->from.number.valid = 1;
1110 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1111 switch (param->setup.redirinfo.present) {
1112 case INFO_PRESENT_ALLOWED:
1113 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1115 case INFO_PRESENT_RESTRICTED:
1116 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1119 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1121 switch (param->setup.redirinfo.screen) {
1122 case INFO_SCREEN_USER:
1123 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1125 case INFO_SCREEN_USER_VERIFIED_PASSED:
1126 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1128 case INFO_SCREEN_USER_VERIFIED_FAILED:
1129 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1132 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1134 switch (param->setup.redirinfo.ntype) {
1135 case INFO_NTYPE_SUBSCRIBER:
1136 ast_redir->from.number.plan = (4 << 4) | 1;
1138 case INFO_NTYPE_NATIONAL:
1139 ast_redir->from.number.plan = (2 << 4) | 1;
1141 case INFO_NTYPE_INTERNATIONAL:
1142 ast_redir->from.number.plan = (1 << 4) | 1;
1145 ast_redir->from.number.plan = (0 << 4) | 1;
1147 #if ASTERISK_VERSION_NUM >= 110000
1148 ast_channel_redirecting_set(ast, ast_redir);
1152 memset(&ast->cid, 0, sizeof(ast->cid));
1153 if (param->setup.callerinfo.id[0])
1154 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1155 if (param->setup.callerinfo.id2[0])
1156 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1157 if (param->setup.callerinfo.name[0])
1158 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1159 if (param->setup.redirinfo.id[0])
1160 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1161 switch (param->setup.callerinfo.present) {
1162 case INFO_PRESENT_ALLOWED:
1163 ast->cid.cid_pres = AST_PRES_ALLOWED;
1165 case INFO_PRESENT_RESTRICTED:
1166 ast->cid.cid_pres = AST_PRES_RESTRICTED;
1169 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1171 switch (param->setup.callerinfo.ntype) {
1172 case INFO_NTYPE_SUBSCRIBER:
1173 ast->cid.cid_ton = 4;
1175 case INFO_NTYPE_NATIONAL:
1176 ast->cid.cid_ton = 2;
1178 case INFO_NTYPE_INTERNATIONAL:
1179 ast->cid.cid_ton = 1;
1182 ast->cid.cid_ton = 0;
1186 #if ASTERISK_VERSION_NUM < 110000
1187 ast->transfercapability = param->setup.capainfo.bearer_capa;
1189 ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1191 /* enable hdlc if transcap is data */
1192 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1194 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1196 /* configure channel */
1197 #if ASTERISK_VERSION_NUM < 100000
1198 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1199 ast->readformat = ast->rawreadformat = ast->nativeformats;
1200 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1202 #if ASTERISK_VERSION_NUM < 110000
1203 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1204 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1205 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1206 ast_set_write_format(ast, &ast->rawwriteformat);
1207 ast_set_read_format(ast, &ast->rawreadformat);
1209 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1210 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1211 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1212 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1213 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1216 #if ASTERISK_VERSION_NUM < 110000
1218 ast->hangupcause = 0;
1220 ast_channel_priority_set(ast, 1);
1221 ast_channel_hangupcause_set(ast, 0);
1225 call->state = CHAN_LCR_STATE_IN_SETUP;
1227 if (!call->pbx_started)
1228 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1232 * incoming setup acknowledge from LCR
1234 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1236 if (!call->ast) return;
1238 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1240 /* send pending digits in dialque */
1241 if (call->dialque[0])
1242 send_dialque_to_lcr(call);
1243 /* change to overlap state */
1244 call->state = CHAN_LCR_STATE_OUT_DIALING;
1248 * incoming proceeding from LCR
1250 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1252 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1255 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1256 /* queue event for asterisk */
1257 if (call->ast && call->pbx_started) {
1262 rc = write(wake_pipe[1], &byte, 1);
1264 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1270 * incoming alerting from LCR
1272 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1274 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1277 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1278 /* queue event to asterisk */
1279 if (call->ast && call->pbx_started) {
1284 rc = write(wake_pipe[1], &byte, 1);
1286 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1291 * incoming connect from LCR
1293 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1295 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1298 call->state = CHAN_LCR_STATE_CONNECT;
1299 /* copy connectinfo */
1300 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
1301 /* queue event to asterisk */
1302 if (call->ast && call->pbx_started) {
1307 rc = write(wake_pipe[1], &byte, 1);
1309 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1314 * incoming disconnect from LCR
1316 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1318 struct ast_channel *ast = call->ast;
1320 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1323 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1325 call->cause = param->disconnectinfo.cause;
1326 call->location = param->disconnectinfo.location;
1327 /* if bridge, forward disconnect and return */
1330 if (call->bridge_call) {
1331 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1332 bridge_message_if_bridged(call, message_type, param);
1336 /* release lcr with same cause */
1337 send_release(call, call->cause, call->location);
1339 /* change to release state */
1340 call->state = CHAN_LCR_STATE_RELEASE;
1341 /* queue release asterisk */
1343 #if ASTERISK_VERSION_NUM < 110000
1344 ast->hangupcause = call->cause;
1346 ast_channel_hangupcause_set(ast, call->cause);
1348 if (call->pbx_started) {
1353 rc = write(wake_pipe[1], &byte, 1);
1355 strcpy(call->queue_string, "H"); // overwrite other indications
1357 ast_hangup(ast); // call will be destroyed here
1363 * incoming release from LCR
1365 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1367 struct ast_channel *ast = call->ast;
1369 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1373 /* change to release state */
1374 call->state = CHAN_LCR_STATE_RELEASE;
1375 /* copy release info */
1377 call->cause = param->disconnectinfo.cause;
1378 call->location = param->disconnectinfo.location;
1380 /* if we have an asterisk instance, queue hangup, else we are done */
1382 #if ASTERISK_VERSION_NUM < 110000
1383 ast->hangupcause = call->cause;
1385 ast_channel_hangupcause_set(ast, call->cause);
1387 if (call->pbx_started) {
1392 rc = write(wake_pipe[1], &byte, 1);
1394 strcpy(call->queue_string, "H");
1396 ast_hangup(ast); // call will be destroyed here
1405 * incoming information from LCR
1407 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1409 struct ast_channel *ast = call->ast;
1411 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1415 /* pbx not started */
1416 if (!call->pbx_started) {
1417 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1418 #if ASTERISK_VERSION_NUM < 110000
1419 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1421 char *tmp[AST_MAX_EXTENSION];
1422 snprintf(tmp,AST_MAX_EXTENSION,"%s%s",ast_channel_exten(ast),param->information.id);
1423 ast_channel_exten_set(ast, tmp);
1425 lcr_start_pbx(call, ast, param->information.sending_complete);
1429 /* change dailing state after setup */
1430 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1431 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1432 call->state = CHAN_LCR_STATE_IN_DIALING;
1433 // ast_setstate(ast, AST_STATE_DIALING);
1437 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1442 rc = write(wake_pipe[1], &byte, 1);
1444 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1447 /* use bridge to forware message not supported by asterisk */
1448 if (call->state == CHAN_LCR_STATE_CONNECT) {
1449 if (call->bridge_call) {
1450 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1451 bridge_message_if_bridged(call, message_type, param);
1453 if (call->dsp_dtmf) {
1458 rc = write(wake_pipe[1], &byte, 1);
1460 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1462 CDEBUG(call, call->ast, "LCR's DTMF detection is disabled.\n");
1468 * incoming information from LCR
1470 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1472 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1474 if (!call->ast) return;
1476 /* use bridge to forware message not supported by asterisk */
1477 bridge_message_if_bridged(call, message_type, param);
1481 * incoming information from LCR
1483 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1485 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1487 if (!call->ast) return;
1489 /* use bridge to forware message not supported by asterisk */
1490 bridge_message_if_bridged(call, message_type, param);
1494 * incoming pattern from LCR
1496 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1498 union parameter newparam;
1500 CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1502 if (!call->ast) return;
1504 /* pattern are indicated only once */
1505 if (call->has_pattern)
1507 call->has_pattern = 1;
1509 /* request bchannel */
1510 CDEBUG(call, call->ast, "Requesting audio path (ref=%d)\n", call->ref);
1511 memset(&newparam, 0, sizeof(union parameter));
1512 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
1514 /* queue PROGRESS, because tones are available */
1515 if (call->ast && call->pbx_started) {
1520 rc = write(wake_pipe[1], &byte, 1);
1522 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1527 * got dtmf from bchannel (locked state)
1529 void lcr_in_dtmf(struct chan_call *call, int val)
1531 struct ast_channel *ast = call->ast;
1536 if (!call->pbx_started)
1539 if (!call->dsp_dtmf) {
1540 CDEBUG(call, call->ast, "Recognised DTMF digit '%c' by LCR, but ignoring. (disabled by option)\n", val);
1544 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1551 rc = write(wake_pipe[1], &byte, 1);
1553 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1557 * message received from LCR
1559 int receive_message(int message_type, unsigned int ref, union parameter *param)
1561 struct chan_call *call;
1562 union parameter newparam;
1565 memset(&newparam, 0, sizeof(union parameter));
1567 /* handle new ref */
1568 if (message_type == MESSAGE_NEWREF) {
1569 if (param->newref.direction) {
1570 /* new ref from lcr */
1571 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1572 if (!ref || find_call_ref(ref)) {
1573 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1576 /* allocate new call instance */
1577 call = alloc_call();
1579 call->state = CHAN_LCR_STATE_IN_PREPARE;
1582 call->ref_was_assigned = 1;
1583 /* wait for setup (or release from asterisk) */
1585 /* new ref, as requested from this remote application */
1586 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1587 call = find_call_ref(0);
1589 /* send release, if ref does not exist */
1590 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1595 call->ref_was_assigned = 1;
1596 /* send pending setup info */
1597 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1598 send_setup_to_lcr(call);
1599 /* release if asterisk has signed off */
1600 else if (call->state == CHAN_LCR_STATE_RELEASE) {
1603 send_release(call, call->cause, call->location);
1605 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1611 send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1617 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1620 call = find_call_ref(ref);
1622 /* ignore ref that is not used (anymore) */
1623 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1627 /* handle messages */
1628 switch(message_type) {
1630 lcr_in_setup(call, message_type, param);
1633 case MESSAGE_OVERLAP:
1634 lcr_in_overlap(call, message_type, param);
1637 case MESSAGE_PROCEEDING:
1638 lcr_in_proceeding(call, message_type, param);
1641 case MESSAGE_ALERTING:
1642 lcr_in_alerting(call, message_type, param);
1645 case MESSAGE_CONNECT:
1646 lcr_in_connect(call, message_type, param);
1649 case MESSAGE_DISCONNECT:
1650 lcr_in_disconnect(call, message_type, param);
1653 case MESSAGE_RELEASE:
1654 lcr_in_release(call, message_type, param);
1657 case MESSAGE_INFORMATION:
1658 lcr_in_information(call, message_type, param);
1661 case MESSAGE_NOTIFY:
1662 lcr_in_notify(call, message_type, param);
1665 case MESSAGE_FACILITY:
1666 lcr_in_facility(call, message_type, param);
1669 case MESSAGE_PATTERN: // audio available from LCR
1670 if (!call->has_pattern)
1671 lcr_in_pattern(call, message_type, param);
1674 case MESSAGE_NOPATTERN: // audio not available from LCR
1677 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1678 call->audiopath = param->audiopath;
1681 case MESSAGE_TRAFFIC: // if remote audio connected or hold
1683 unsigned char *p = param->traffic.data;
1684 int i, len = param->traffic.len;
1685 for (i = 0; i < len; i++, p++)
1688 rc = write(call->pipe[1], param->traffic.data, param->traffic.len);
1692 lcr_in_dtmf(call, param->dtmf);
1696 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1703 * release all calls (due to broken socket)
1705 static void release_all_calls(void)
1707 struct chan_call *call;
1712 /* no ast, so we may directly free call */
1714 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1718 /* already in release process */
1719 if (call->state == CHAN_LCR_STATE_RELEASE) {
1723 /* release or queue release */
1725 call->state = CHAN_LCR_STATE_RELEASE;
1726 if (!call->pbx_started) {
1727 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1728 ast_hangup(call->ast); // call will be destroyed here
1731 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1736 rc = write(wake_pipe[1], &byte, 1);
1738 strcpy(call->queue_string, "H");
1743 void close_socket(void);
1746 * warning! not thread safe
1747 * returns -1 for socket error, 0 for no work, 1 for work
1749 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1752 struct admin_list *admin;
1753 struct admin_message msg;
1755 if ((what & LCR_FD_READ)) {
1756 /* read from socket */
1757 len = read(lcr_sock, &msg, sizeof(msg));
1759 CERROR(NULL, NULL, "Socket closed.(read)\n");
1761 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1763 release_all_calls();
1764 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1768 if (len != sizeof(msg)) {
1769 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1772 if (msg.message != ADMIN_MESSAGE) {
1773 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1776 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1778 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1783 if ((what & LCR_FD_WRITE)) {
1784 /* write to socket */
1786 socket_fd.when &= ~LCR_FD_WRITE;
1789 admin = admin_first;
1790 len = write(lcr_sock, &admin->msg, sizeof(msg));
1792 CERROR(NULL, NULL, "Socket closed.(write)\n");
1796 if (len != sizeof(msg)) {
1797 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1801 admin_first = admin->next;
1805 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1814 * open and close socket and thread
1816 int open_socket(void)
1819 struct sockaddr_un sock_address;
1820 union parameter param;
1823 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1824 CERROR(NULL, NULL, "Failed to create socket.\n");
1828 /* set socket address and name */
1829 memset(&sock_address, 0, sizeof(sock_address));
1830 sock_address.sun_family = PF_UNIX;
1831 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1833 /* connect socket */
1834 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1837 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1841 /* register socket fd */
1842 memset(&socket_fd, 0, sizeof(socket_fd));
1843 socket_fd.fd = lcr_sock;
1844 register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1846 /* enque hello message */
1847 memset(¶m, 0, sizeof(param));
1848 strcpy(param.hello.application, "asterisk");
1849 send_message(MESSAGE_HELLO, 0, ¶m);
1854 void close_socket(void)
1856 struct admin_list *admin, *temp;
1858 /* socket not created */
1862 unregister_fd(&socket_fd);
1864 /* flush pending messages */
1865 admin = admin_first;
1868 admin = admin->next;
1880 /* sending queue to asterisk */
1881 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1886 rc = read(wake_pipe[0], &byte, 1);
1893 static void handle_queue()
1895 struct chan_call *call;
1896 struct ast_channel *ast;
1897 struct ast_frame fr;
1903 p = call->queue_string;
1906 if (ast_channel_trylock(ast)) {
1907 ast_mutex_unlock(&chan_lock);
1909 ast_mutex_lock(&chan_lock);
1915 CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1916 ast_queue_control(ast, AST_CONTROL_PROGRESS);
1919 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1920 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1923 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1924 ast_queue_control(ast, AST_CONTROL_RINGING);
1925 ast_setstate(ast, AST_STATE_RINGING);
1928 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1929 ast_queue_control(ast, AST_CONTROL_ANSWER);
1932 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1933 ast_queue_hangup(ast);
1935 case '1': case '2': case '3': case 'A':
1936 case '4': case '5': case '6': case 'B':
1937 case '7': case '8': case '9': case 'C':
1938 case '*': case '0': case '#': case 'D':
1939 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1940 /* send digit to asterisk */
1941 memset(&fr, 0, sizeof(fr));
1943 #ifdef LCR_FOR_ASTERISK
1944 fr.frametype = AST_FRAME_DTMF_BEGIN;
1947 #ifdef LCR_FOR_CALLWEAVER
1948 fr.frametype = AST_FRAME_DTMF;
1951 #ifdef AST_1_8_OR_HIGHER
1952 fr.subclass.integer = *p;
1956 fr.delivery = ast_tv(0, 0);
1957 ast_queue_frame(ast, &fr);
1959 #ifdef LCR_FOR_ASTERISK
1960 fr.frametype = AST_FRAME_DTMF_END;
1961 ast_queue_frame(ast, &fr);
1966 CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1970 call->queue_string[0] = '\0';
1971 ast_channel_unlock(ast);
1977 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1979 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1980 if (open_socket() < 0)
1981 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1986 void lock_chan(void)
1988 ast_mutex_lock(&chan_lock);
1991 void unlock_chan(void)
1993 ast_mutex_unlock(&chan_lock);
1996 /* chan_lcr thread */
1997 static void *chan_thread(void *arg)
1999 if (pipe(wake_pipe) < 0) {
2000 CERROR(NULL, NULL, "Failed to open pipe.\n");
2003 memset(&wake_fd, 0, sizeof(wake_fd));
2004 wake_fd.fd = wake_pipe[0];
2005 register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
2007 memset(&socket_retry, 0, sizeof(socket_retry));
2008 add_timer(&socket_retry, handle_retry, NULL, 0);
2010 /* open socket the first time */
2011 handle_retry(NULL, NULL, 0);
2013 ast_mutex_lock(&chan_lock);
2017 select_main(0, &global_change, lock_chan, unlock_chan);
2024 * new asterisk instance
2027 #ifdef AST_1_8_OR_HIGHER
2028 #if ASTERISK_VERSION_NUM < 100000
2029 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
2031 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
2034 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
2037 char exten[256], *dial, *interface, *opt;
2038 struct ast_channel *ast;
2039 struct chan_call *call;
2040 #ifdef AST_1_8_OR_HIGHER
2041 const struct ast_party_redirecting *req_redir;
2042 const struct ast_party_caller *req_caller;
2045 ast_mutex_lock(&chan_lock);
2046 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2048 /* if socket is closed */
2050 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2051 ast_mutex_unlock(&chan_lock);
2055 /* create call instance */
2056 call = alloc_call();
2058 /* failed to create instance */
2059 ast_mutex_unlock(&chan_lock);
2063 /* create asterisk channel instrance */
2065 #ifdef LCR_FOR_ASTERISK
2066 #ifdef AST_1_8_OR_HIGHER
2067 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2069 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2073 #ifdef LCR_FOR_CALLWEAVER
2074 ast = ast_channel_alloc(1);
2078 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2080 /* failed to create instance */
2081 ast_mutex_unlock(&chan_lock);
2084 #if ASTERISK_VERSION_NUM < 110000
2085 ast->tech = &lcr_tech;
2086 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2087 #ifdef AST_1_8_OR_HIGHER
2088 req_redir = &requestor->redirecting;
2089 req_caller = &requestor->caller;
2092 ast_channel_tech_set(ast, &lcr_tech);
2093 ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2094 req_redir = ast_channel_redirecting(requestor);
2095 req_caller = ast_channel_caller(requestor);
2097 /* configure channel */
2098 #if ASTERISK_VERSION_NUM < 100000
2099 #if ASTERISK_VERSION_NUM < 110000
2100 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2101 ast->readformat = ast->rawreadformat = ast->nativeformats;
2102 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
2104 ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2105 ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2106 ast->writeformat = ast->rawwriteformat = ast_channel_nativeformats(ast);
2109 #if ASTERISK_VERSION_NUM < 110000
2110 ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2111 ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2112 ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2113 ast_set_write_format(ast, &ast->rawwriteformat);
2114 ast_set_read_format(ast, &ast->rawreadformat);
2116 ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2117 ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2118 ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2119 ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2120 ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2123 #if ASTERISK_VERSION_NUM < 110000
2125 ast->hangupcause = 0;
2127 ast_channel_priority_set(ast, 1);
2128 ast_channel_hangupcause_set(ast, 0);
2133 #if ASTERISK_VERSION_NUM < 110000
2134 ast->tech_pvt = call;
2135 ast->fds[0] = call->pipe[0];
2137 ast_channel_tech_pvt_set(ast, call);
2138 ast_channel_set_fd(ast, 0, call->pipe[0]);
2140 call->pbx_started = 0;
2142 call->state = CHAN_LCR_STATE_OUT_PREPARE;
2145 * Extract interface, dialstring, options from data.
2148 * <interface>/<dialstring>
2149 * <interface>/<dialstring>/options
2151 strncpy(exten, (char *)data, sizeof(exten)-1);
2152 exten[sizeof(exten)-1] = '\0';
2153 if ((dial = strchr(exten, '/'))) {
2156 if ((opt = strchr(dial, '/')))
2165 strncpy(call->interface, interface, sizeof(call->interface)-1);
2166 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2167 apply_opt(call, (char *)opt);
2169 #ifdef AST_1_8_OR_HIGHER
2170 // clone_variables(requestor, ast);
2173 ast->caller.ani.number.valid= req_caller->ani.number.valid;
2174 if (req_caller->ani.number.valid)
2175 if (req_caller->ani.number.str)
2176 if (req_caller->ani.number.str[0])
2177 ast->caller.ani.number.str= strdup(req_caller->ani.number.str);
2178 ast->caller.ani.number.plan= req_caller->ani.number.plan;
2179 ast->caller.ani.number.presentation= req_caller->ani.number.presentation;
2181 ast->caller.ani.name.valid= req_caller->ani.name.valid;
2182 if (req_caller->ani.name.valid)
2183 if (req_caller->ani.name.str)
2184 if (req_caller->ani.name.str[0])
2185 ast->caller.ani.name.str= strdup(req_caller->ani.name.str);
2186 ast->caller.ani.name.presentation= req_caller->ani.name.presentation;
2188 ast->caller.ani.subaddress.valid= req_caller->ani.subaddress.valid;
2189 if (req_caller->ani.subaddress.valid)
2190 if (req_caller->ani.subaddress.str)
2191 if (req_caller->ani.subaddress.str[0])
2192 ast->caller.ani.subaddress.str= strdup(req_caller->ani.subaddress.str);
2193 ast->caller.ani.subaddress.type= req_caller->ani.subaddress.type;
2195 ast->caller.id.number.valid= req_caller->id.number.valid;
2196 if (req_caller->id.number.valid)
2197 if (req_caller->id.number.str)
2198 if (req_caller->id.number.str[0])
2199 ast->caller.id.number.str= strdup(req_caller->id.number.str);
2200 ast->caller.id.number.plan= req_caller->id.number.plan;
2201 ast->caller.id.number.presentation= req_caller->id.number.presentation;
2203 ast->caller.id.name.valid= req_caller->id.name.valid;
2204 if (req_caller->id.name.valid)
2205 if (req_caller->id.name.str)
2206 if (req_caller->id.name.str[0])
2207 ast->caller.id.name.str= strdup(req_caller->id.name.str);
2208 ast->caller.id.name.presentation= req_caller->id.name.presentation;
2210 ast->caller.id.subaddress.valid= req_caller->id.subaddress.valid;
2211 if (req_caller->id.subaddress.valid)
2212 if (req_caller->id.subaddress.str)
2213 if (req_caller->id.subaddress.str[0])
2214 ast->caller.id.subaddress.str= strdup(req_caller->id.subaddress.str);
2215 ast->caller.id.subaddress.type= req_caller->id.subaddress.type;
2217 if (requestor->dialed.number.str)
2218 if (requestor->dialed.number.str[0])
2219 ast->dialed.number.str= strdup(requestor->dialed.number.str);
2220 ast->dialed.number.plan= requestor->dialed.number.plan;
2222 ast->dialed.subaddress.valid= requestor->dialed.subaddress.valid;
2223 if (requestor->dialed.subaddress.valid)
2224 if (requestor->dialed.subaddress.str)
2225 if (requestor->dialed.subaddress.str[0])
2226 ast->dialed.subaddress.str= strdup(requestor->dialed.subaddress.str);
2227 ast->dialed.subaddress.type= requestor->dialed.subaddress.type;
2229 ast->dialed.transit_network_select= requestor->dialed.transit_network_select;
2230 ast->redirecting.count= req_redir->count;
2231 ast->redirecting.reason= req_redir->reason;
2233 ast->redirecting.from.number.valid= req_redir->from.number.valid;
2234 if (req_redir->from.number.valid)
2235 if (req_redir->from.number.str)
2236 if (req_redir->from.number.str[0])
2237 ast->redirecting.from.number.str= strdup(req_redir->from.number.str);
2238 ast->redirecting.from.number.plan= req_redir->from.number.plan;
2239 ast->redirecting.from.number.presentation= req_redir->from.number.presentation;
2241 ast->redirecting.to.number.valid= req_redir->to.number.valid;
2242 if (req_redir->to.number.valid)
2243 if (req_redir->to.number.str)
2244 if (req_redir->to.number.str[0])
2245 ast->redirecting.to.number.str= strdup(req_redir->to.number.str);
2246 ast->redirecting.to.number.plan= req_redir->to.number.plan;
2247 ast->redirecting.to.number.presentation= req_redir->to.number.presentation;
2249 /* store call information for setup */
2252 if (requestor && req_caller->id.number.valid) {
2253 if (req_caller->id.number.str)
2254 strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2255 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2256 case AST_PRES_RESTRICTED:
2257 call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2259 case AST_PRES_UNAVAILABLE:
2260 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2262 case AST_PRES_ALLOWED:
2264 call->callerinfo.present = INFO_PRESENT_ALLOWED;
2266 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2267 case AST_PRES_USER_NUMBER_UNSCREENED:
2268 call->callerinfo.screen = INFO_SCREEN_USER;
2270 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2271 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2273 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2274 call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2277 call->callerinfo.screen = INFO_SCREEN_NETWORK;
2279 switch((req_caller->id.number.plan >> 4) & 7) {
2281 call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2284 call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2287 call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2290 call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2293 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2296 if (requestor && req_caller->ani.number.valid) {
2297 if (req_caller->ani.number.str)
2298 strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2299 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2300 case AST_PRES_RESTRICTED:
2301 call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2303 case AST_PRES_UNAVAILABLE:
2304 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2306 case AST_PRES_ALLOWED:
2308 call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2310 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2311 case AST_PRES_USER_NUMBER_UNSCREENED:
2312 call->callerinfo.screen2 = INFO_SCREEN_USER;
2314 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2315 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2317 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2318 call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2321 call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2323 switch((req_caller->ani.number.plan >> 4) & 7) {
2325 call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2328 call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2331 call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2334 call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2337 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2340 if (requestor && req_caller->id.name.valid) {
2341 if (req_caller->id.name.str)
2342 strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2346 if (requestor && req_redir->from.number.valid) {
2347 call->redirinfo.itype = INFO_ITYPE_CHAN;
2348 if (req_redir->from.number.str)
2349 strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2350 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2351 case AST_PRES_RESTRICTED:
2352 call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2354 case AST_PRES_UNAVAILABLE:
2355 call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2357 case AST_PRES_ALLOWED:
2359 call->redirinfo.present = INFO_PRESENT_ALLOWED;
2361 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2362 case AST_PRES_USER_NUMBER_UNSCREENED:
2363 call->redirinfo.screen = INFO_SCREEN_USER;
2365 case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2366 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2368 case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2369 call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2372 call->redirinfo.screen = INFO_SCREEN_NETWORK;
2374 switch((req_redir->from.number.plan >> 4) & 7) {
2376 call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2379 call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2382 call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2385 call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2390 ast_mutex_unlock(&chan_lock);
2395 * call from asterisk
2397 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2399 union parameter newparam;
2400 struct chan_call *call;
2401 #if ASTERISK_VERSION_NUM >= 110000
2402 int transfercapability;
2405 ast_mutex_lock(&chan_lock);
2406 #if ASTERISK_VERSION_NUM < 110000
2407 call = ast->tech_pvt;
2409 call = ast_channel_tech_pvt(ast);
2412 #ifdef LCR_FOR_CALLWEAVER
2414 snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2418 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2419 ast_mutex_unlock(&chan_lock);
2423 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2425 /* pbx process is started */
2426 call->pbx_started = 1;
2427 /* send MESSAGE_NEWREF */
2428 memset(&newparam, 0, sizeof(union parameter));
2429 newparam.newref.direction = 0; /* request from app */
2430 strncpy(newparam.newref.interface, call->interface, sizeof(newparam.newref.interface) - 1);
2431 send_message(MESSAGE_NEWREF, 0, &newparam);
2433 /* set hdlc if capability requires hdlc */
2434 #if ASTERISK_VERSION_NUM < 110000
2435 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2436 || ast->transfercapability == INFO_BC_DATARESTRICTED
2437 || ast->transfercapability == INFO_BC_VIDEO)
2439 transfercapability=ast_channel_transfercapability(ast);
2440 if (transfercapability == INFO_BC_DATAUNRESTRICTED
2441 || transfercapability == INFO_BC_DATARESTRICTED
2442 || transfercapability == INFO_BC_VIDEO)
2445 /* if hdlc is forced by option, we change transcap to data */
2447 #if ASTERISK_VERSION_NUM < 110000
2448 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2449 && ast->transfercapability != INFO_BC_DATARESTRICTED
2450 && ast->transfercapability != INFO_BC_VIDEO)
2451 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2453 && transfercapability != INFO_BC_DATAUNRESTRICTED
2454 && transfercapability != INFO_BC_DATARESTRICTED
2455 && transfercapability != INFO_BC_VIDEO)
2456 transfercapability = INFO_BC_DATAUNRESTRICTED;
2459 #ifndef AST_1_8_OR_HIGHER
2460 call->cid_num[0] = 0;
2461 call->cid_name[0] = 0;
2462 call->cid_rdnis[0] = 0;
2464 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2465 strncpy(call->cid_num, ast->cid.cid_num,
2466 sizeof(call->cid_num)-1);
2467 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2468 strncpy(call->cid_name, ast->cid.cid_name,
2469 sizeof(call->cid_name)-1);
2470 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2471 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2472 sizeof(call->cid_rdnis)-1);
2475 ast_mutex_unlock(&chan_lock);
2479 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2481 static const char* dtmf_tones[] = {
2482 "!941+1336/100,!0/100", /* 0 */
2483 "!697+1209/100,!0/100", /* 1 */
2484 "!697+1336/100,!0/100", /* 2 */
2485 "!697+1477/100,!0/100", /* 3 */
2486 "!770+1209/100,!0/100", /* 4 */
2487 "!770+1336/100,!0/100", /* 5 */
2488 "!770+1477/100,!0/100", /* 6 */
2489 "!852+1209/100,!0/100", /* 7 */
2490 "!852+1336/100,!0/100", /* 8 */
2491 "!852+1477/100,!0/100", /* 9 */
2492 "!697+1633/100,!0/100", /* A */
2493 "!770+1633/100,!0/100", /* B */
2494 "!852+1633/100,!0/100", /* C */
2495 "!941+1633/100,!0/100", /* D */
2496 "!941+1209/100,!0/100", /* * */
2497 "!941+1477/100,!0/100" }; /* # */
2499 if (digit >= '0' && digit <='9')
2500 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2501 else if (digit >= 'A' && digit <= 'D')
2502 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2503 else if (digit == '*')
2504 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2505 else if (digit == '#')
2506 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2508 #if ASTERISK_VERSION_NUM < 110000
2509 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2511 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2516 #ifdef LCR_FOR_ASTERISK
2517 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2519 #ifdef LCR_FOR_CALLWEAVER
2520 static int lcr_digit(struct ast_channel *ast, char digit)
2523 struct chan_call *call;
2524 union parameter newparam;
2527 #ifdef LCR_FOR_CALLWEAVER
2528 int inband_dtmf = 0;
2531 /* only pass IA5 number space */
2532 if (digit > 126 || digit < 32)
2535 ast_mutex_lock(&chan_lock);
2536 #if ASTERISK_VERSION_NUM < 110000
2537 call = ast->tech_pvt;
2539 call = ast_channel_tech_pvt(ast);
2542 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2543 ast_mutex_unlock(&chan_lock);
2547 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2549 /* send information or queue them */
2550 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2551 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2552 memset(&newparam, 0, sizeof(union parameter));
2554 newparam.information.keypad[0] = digit;
2555 newparam.information.keypad[1] = '\0';
2557 newparam.information.id[0] = digit;
2558 newparam.information.id[1] = '\0';
2560 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2563 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2564 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2566 strncat(call->dialque, buf, strlen(call->dialque)-1);
2569 ast_mutex_unlock(&chan_lock);
2571 #ifdef LCR_FOR_ASTERISK
2575 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2577 int inband_dtmf = 0;
2578 struct chan_call *call;
2581 ast_mutex_lock(&chan_lock);
2583 #if ASTERISK_VERSION_NUM < 110000
2584 call = ast->tech_pvt;
2586 call = ast_channel_tech_pvt(ast);
2591 "Received digit from Asterisk, "
2592 "but no call instance exists.\n");
2593 ast_mutex_unlock(&chan_lock);
2597 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2599 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2603 ast_mutex_unlock(&chan_lock);
2606 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2607 send_digit_to_chan(ast, digit);
2613 static int lcr_answer(struct ast_channel *ast)
2615 union parameter newparam;
2616 struct chan_call *call;
2618 ast_mutex_lock(&chan_lock);
2619 #if ASTERISK_VERSION_NUM < 110000
2620 call = ast->tech_pvt;
2622 call = ast_channel_tech_pvt(ast);
2625 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2626 ast_mutex_unlock(&chan_lock);
2630 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2632 /* copy connectinfo, if bridged */
2633 if (call->bridge_call)
2634 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2635 /* send connect message to lcr */
2636 if (call->state != CHAN_LCR_STATE_CONNECT) {
2637 memset(&newparam, 0, sizeof(union parameter));
2638 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2639 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2640 call->state = CHAN_LCR_STATE_CONNECT;
2644 // memset(&newparam, 0, sizeof(union parameter));
2645 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2647 ast_mutex_unlock(&chan_lock);
2651 static int lcr_hangup(struct ast_channel *ast)
2653 struct chan_call *call;
2654 pthread_t tid = pthread_self();
2656 if (!pthread_equal(tid, chan_tid)) {
2657 ast_mutex_lock(&chan_lock);
2659 #if ASTERISK_VERSION_NUM < 110000
2660 call = ast->tech_pvt;
2662 call = ast_channel_tech_pvt(ast);
2665 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2666 if (!pthread_equal(tid, chan_tid)) {
2667 ast_mutex_unlock(&chan_lock);
2672 if (!pthread_equal(tid, chan_tid))
2673 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2675 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2677 /* disconnect asterisk, maybe not required */
2678 #if ASTERISK_VERSION_NUM < 110000
2679 ast->tech_pvt = NULL;
2682 ast_channel_tech_pvt_set(ast, NULL);
2683 ast_channel_set_fd(ast, 0, -1);
2687 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2688 #if ASTERISK_VERSION_NUM < 110000
2689 if (ast->hangupcause > 0)
2690 send_release(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2692 if (ast_channel_hangupcause(ast) > 0)
2693 send_release(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2696 send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2699 if (!pthread_equal(tid, chan_tid)) {
2700 ast_mutex_unlock(&chan_lock);
2704 /* ref is not set, due to prepare setup or release */
2705 if (call->state == CHAN_LCR_STATE_RELEASE) {
2706 /* we get the response to our release */
2707 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2710 /* during prepare, we change to release state */
2711 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2712 call->state = CHAN_LCR_STATE_RELEASE;
2716 if (!pthread_equal(tid, chan_tid)) {
2717 ast_mutex_unlock(&chan_lock);
2722 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2724 union parameter newparam;
2725 struct chan_call *call;
2726 struct ast_frame * f = fr;
2727 unsigned char *p, *q;
2730 #if ASTERISK_VERSION_NUM < 100000
2731 #ifdef AST_1_8_OR_HIGHER
2732 if (!f->subclass.codec)
2736 CDEBUG(NULL, ast, "No subclass\n");
2738 #ifdef AST_1_8_OR_HIGHER
2739 #if ASTERISK_VERSION_NUM < 100000
2740 #if ASTERISK_VERSION_NUM < 110000
2741 if (!(f->subclass.codec & ast->nativeformats)) {
2743 if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2746 #if ASTERISK_VERSION_NUM < 110000
2747 if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2749 if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2753 #if ASTERISK_VERSION_NUM < 110000
2754 if (!(f->subclass & ast->nativeformats)) {
2756 if (!(f->subclass & ast_channel_nativeformats(ast))) {
2760 "Unexpected format. "
2761 "Activating emergency conversion...\n");
2763 #ifdef AST_1_8_OR_HIGHER
2764 #if ASTERISK_VERSION_NUM < 100000
2765 ast_set_write_format(ast, f->subclass.codec);
2767 ast_set_write_format(ast, &f->subclass.format);
2770 ast_set_write_format(ast, f->subclass);
2772 #if ASTERISK_VERSION_NUM < 110000
2773 f = (ast->writetrans) ? ast_translate(
2774 ast->writetrans, fr, 0) : fr;
2776 f = (ast_channel_writetrans(ast)) ? ast_translate(
2777 ast_channel_writetrans(ast), fr, 0) : fr;
2781 ast_mutex_lock(&chan_lock);
2782 #if ASTERISK_VERSION_NUM < 110000
2783 call = ast->tech_pvt;
2785 call = ast_channel_tech_pvt(ast);
2787 if (!call || !call->ref) {
2788 /* drop the frame, if no ref exists, but return successfull delivery, or asterisk will abort connection */
2789 ast_mutex_unlock(&chan_lock);
2796 p = *((unsigned char **)&(f->data));
2797 q = newparam.traffic.data;
2798 memset(&newparam, 0, sizeof(union parameter));
2800 l = (len > sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
2801 newparam.traffic.len = l;
2804 *q++ = flip_bits[*p++];
2805 send_message(MESSAGE_TRAFFIC, call->ref, &newparam);
2807 ast_mutex_unlock(&chan_lock);
2815 static struct ast_frame *lcr_read(struct ast_channel *ast)
2817 struct chan_call *call;
2819 struct ast_frame *f = NULL;
2821 ast_mutex_lock(&chan_lock);
2822 #if ASTERISK_VERSION_NUM < 110000
2823 call = ast->tech_pvt;
2825 call = ast_channel_tech_pvt(ast);
2828 ast_mutex_unlock(&chan_lock);
2831 if (call->pipe[0] > -1) {
2832 if (call->rebuffer && !call->hdlc) {
2833 /* Make sure we have a complete 20ms (160byte) frame */
2834 len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2836 call->framepos += len;
2839 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2841 if (len < 0 && errno == EAGAIN) {
2842 ast_mutex_unlock(&chan_lock);
2844 #ifdef LCR_FOR_ASTERISK
2845 return &ast_null_frame;
2848 #ifdef LCR_FOR_CALLWEAVER
2854 close(call->pipe[0]);
2857 ast_mutex_unlock(&chan_lock);
2859 } else if (call->rebuffer && call->framepos < 160) {
2860 /* Not a complete frame, so we send a null-frame */
2861 ast_mutex_unlock(&chan_lock);
2862 return &ast_null_frame;
2866 call->read_fr.frametype = AST_FRAME_VOICE;
2867 #ifdef AST_1_8_OR_HIGHER
2868 #if ASTERISK_VERSION_NUM < 100000
2869 #if ASTERISK_VERSION_NUM < 110000
2870 call->read_fr.subclass.codec = ast->nativeformats;
2872 call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2875 #if ASTERISK_VERSION_NUM < 110000
2876 ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2878 ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2880 call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2883 #if ASTERISK_VERSION_NUM < 110000
2884 call->read_fr.subclass = ast->nativeformats;
2886 call->read_fr.subclass = ast_channel_nativeformats(ast);
2889 if (call->rebuffer) {
2890 call->read_fr.datalen = call->framepos;
2891 call->read_fr.samples = call->framepos;
2894 call->read_fr.datalen = len;
2895 call->read_fr.samples = len;
2897 call->read_fr.delivery = ast_tv(0,0);
2898 *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2901 f = ast_dsp_process(ast, call->dsp, &call->read_fr);
2902 if (f && f->frametype == AST_FRAME_DTMF)
2903 #ifdef AST_1_8_OR_HIGHER
2904 CDEBUG(call, ast, "Asterisk detected inband DTMF: %c.\n", f->subclass.integer);
2906 CDEBUG(call, ast, "Asterisk detected inband DTMF: %c.\n", f->subclass);
2909 ast_mutex_unlock(&chan_lock);
2911 if (f && f->frametype == AST_FRAME_DTMF)
2914 return &call->read_fr;
2917 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2919 union parameter newparam;
2921 struct chan_call *call;
2922 const struct ast_tone_zone_sound *ts = NULL;
2924 ast_mutex_lock(&chan_lock);
2925 #if ASTERISK_VERSION_NUM < 110000
2926 call = ast->tech_pvt;
2928 call = ast_channel_tech_pvt(ast);
2931 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2932 ast_mutex_unlock(&chan_lock);
2937 case AST_CONTROL_BUSY:
2938 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2939 ast_setstate(ast, AST_STATE_BUSY);
2940 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2941 /* send message to lcr */
2942 memset(&newparam, 0, sizeof(union parameter));
2943 newparam.disconnectinfo.cause = 17;
2944 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2945 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2947 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2949 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2950 #if ASTERISK_VERSION_NUM < 110000
2951 ts = ast_get_indication_tone(ast->zone, "busy");
2953 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2957 case AST_CONTROL_CONGESTION:
2958 #if ASTERISK_VERSION_NUM < 110000
2959 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2961 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2963 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2964 /* send message to lcr */
2965 memset(&newparam, 0, sizeof(union parameter));
2966 #if ASTERISK_VERSION_NUM < 110000
2967 newparam.disconnectinfo.cause = ast->hangupcause;
2969 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
2971 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2972 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2974 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2976 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2977 #if ASTERISK_VERSION_NUM < 110000
2978 ts = ast_get_indication_tone(ast->zone, "congestion");
2980 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
2984 case AST_CONTROL_PROCEEDING:
2985 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2986 if (call->state == CHAN_LCR_STATE_IN_SETUP
2987 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2988 /* send message to lcr */
2989 memset(&newparam, 0, sizeof(union parameter));
2990 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2992 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2995 case AST_CONTROL_RINGING:
2996 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2997 ast_setstate(ast, AST_STATE_RING);
2998 if (call->state == CHAN_LCR_STATE_IN_SETUP
2999 || call->state == CHAN_LCR_STATE_IN_DIALING
3000 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
3001 /* send message to lcr */
3002 memset(&newparam, 0, sizeof(union parameter));
3003 send_message(MESSAGE_ALERTING, call->ref, &newparam);
3005 call->state = CHAN_LCR_STATE_IN_ALERTING;
3007 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
3008 #if ASTERISK_VERSION_NUM < 110000
3009 ts = ast_get_indication_tone(ast->zone, "ring");
3011 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
3015 case AST_CONTROL_PROGRESS:
3016 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
3017 if (call->state == CHAN_LCR_STATE_IN_SETUP
3018 || call->state == CHAN_LCR_STATE_IN_DIALING) {
3019 CDEBUG(call, ast, "Changing to proceeding state, because no more dialing possible.\n");
3020 /* send message to lcr */
3021 memset(&newparam, 0, sizeof(union parameter));
3022 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
3024 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
3026 /* request bchannel */
3027 CDEBUG(call, ast, "Requesting audio path.\n");
3028 memset(&newparam, 0, sizeof(union parameter));
3029 send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
3032 CDEBUG(call, ast, "Received indicate -1.\n");
3033 ast_playtones_stop(ast);
3037 case AST_CONTROL_VIDUPDATE:
3038 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
3041 case AST_CONTROL_HOLD:
3042 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
3043 /* send message to lcr */
3044 memset(&newparam, 0, sizeof(union parameter));
3045 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
3046 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3048 /*start music onhold*/
3049 #ifdef LCR_FOR_ASTERISK
3050 #if ASTERISK_VERSION_NUM <110000
3051 ast_moh_start(ast,data,ast->musicclass);
3053 ast_moh_start(ast,data,ast_channel_musicclass(ast));
3057 #ifdef LCR_FOR_CALLWEAVER
3058 ast_moh_start(ast, NULL);
3063 case AST_CONTROL_UNHOLD:
3064 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
3065 /* send message to lcr */
3066 memset(&newparam, 0, sizeof(union parameter));
3067 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3068 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3074 #ifdef AST_CONTROL_SRCUPDATE
3075 case AST_CONTROL_SRCUPDATE:
3079 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3082 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3087 if (ts && ts->data[0]) {
3088 ast_playtones_start(ast, 0, ts->data, 1);
3092 ast_mutex_unlock(&chan_lock);
3099 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3101 struct chan_call *call;
3107 ast_mutex_lock(&chan_lock);
3108 #if ASTERISK_VERSION_NUM < 110000
3109 call = ast->tech_pvt;
3111 call = ast_channel_tech_pvt(ast);
3114 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3115 ast_mutex_unlock(&chan_lock);
3119 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3121 ast_mutex_unlock(&chan_lock);
3126 * send_text asterisk
3128 static int lcr_send_text(struct ast_channel *ast, const char *text)
3130 struct chan_call *call;
3131 union parameter newparam;
3133 ast_mutex_lock(&chan_lock);
3134 #if ASTERISK_VERSION_NUM < 110000
3135 call = ast->tech_pvt;
3137 call = ast_channel_tech_pvt(ast);
3140 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3141 ast_mutex_unlock(&chan_lock);
3145 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3146 memset(&newparam, 0, sizeof(union parameter));
3147 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3148 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3149 ast_mutex_unlock(&chan_lock);
3156 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3157 struct ast_channel *ast2, int flags,
3158 struct ast_frame **fo,
3159 struct ast_channel **rc, int timeoutms)
3162 struct chan_call *call1, *call2;
3163 struct ast_channel *carr[2], *who;
3165 struct ast_frame *f;
3168 /* bridge is disabled, because there is currerntly no concept to bridge mISDN channels */
3169 return AST_BRIDGE_FAILED;
3171 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3176 /* join via dsp (if the channels are currently open) */
3177 ast_mutex_lock(&chan_lock);
3178 #if ASTERISK_VERSION_NUM < 110000
3179 call1 = ast1->tech_pvt;
3180 call2 = ast2->tech_pvt;
3182 call1 = ast_channel_tech_pvt(ast1);
3183 call2 = ast_channel_tech_pvt(ast2);
3185 if (!call1 || !call2) {
3186 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3187 ast_mutex_unlock(&chan_lock);
3188 return AST_BRIDGE_COMPLETE;
3191 /* join, if both call instances uses dsp
3192 ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3194 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3196 /* get bridge id and join */
3197 bridge_id = new_bridge_id();
3199 call1->bridge_id = bridge_id;
3200 call2->bridge_id = bridge_id;
3201 // FIXME: do bridiging
3202 // bchannel_join(call1->bchannel, bridge_id);
3203 // bchannel_join(call2->bchannel, bridge_id);
3205 call1->bridge_call = call2;
3206 call2->bridge_call = call1;
3208 if (call1->state == CHAN_LCR_STATE_IN_SETUP
3209 || call1->state == CHAN_LCR_STATE_IN_DIALING
3210 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3211 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3212 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3215 if (call2->state == CHAN_LCR_STATE_IN_SETUP
3216 || call2->state == CHAN_LCR_STATE_IN_DIALING
3217 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3218 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3219 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3223 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3224 so let's do it for them. Hmpf.
3227 if (call1->on_hold) {
3228 union parameter newparam;
3230 memset(&newparam, 0, sizeof(union parameter));
3231 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3232 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3237 if (call2->on_hold) {
3238 union parameter newparam;
3240 memset(&newparam, 0, sizeof(union parameter));
3241 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3242 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3247 ast_mutex_unlock(&chan_lock);
3251 who = ast_waitfor_n(carr, 2, &to);
3254 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3259 if (!f || f->frametype == AST_FRAME_CONTROL) {
3261 CDEBUG(NULL, NULL, "Got hangup.\n");
3263 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3270 if ( f->frametype == AST_FRAME_DTMF ) {
3271 CDEBUG(NULL, NULL, "Got DTMF.\n");
3287 CDEBUG(NULL, NULL, "Releasing bridge.\n");
3289 /* split channels */
3290 ast_mutex_lock(&chan_lock);
3291 #if ASTERISK_VERSION_NUM < 110000
3292 call1 = ast1->tech_pvt;
3293 call2 = ast2->tech_pvt;
3295 call1 = ast_channel_tech_pvt(ast1);
3296 call2 = ast_channel_tech_pvt(ast2);
3298 if (call1 && call1->bridge_id) {
3299 call1->bridge_id = 0;
3300 if (call1->bridge_call)
3301 call1->bridge_call->bridge_call = NULL;
3303 if (call2 && call1->bridge_id) {
3304 call2->bridge_id = 0;
3305 if (call2->bridge_call)
3306 call2->bridge_call->bridge_call = NULL;
3308 call1->bridge_call = NULL;
3309 call2->bridge_call = NULL;
3311 ast_mutex_unlock(&chan_lock);
3312 return AST_BRIDGE_COMPLETE;
3314 static struct ast_channel_tech lcr_tech = {
3316 .description = "Channel driver for connecting to Linux-Call-Router",
3317 #if ASTERISK_VERSION_NUM < 100000
3318 .capabilities = AST_FORMAT_ALAW,
3320 .requester = lcr_request,
3322 #ifdef LCR_FOR_ASTERISK
3323 .send_digit_begin = lcr_digit_begin,
3324 .send_digit_end = lcr_digit_end,
3327 #ifdef LCR_FOR_CALLWEAVER
3328 .send_digit = lcr_digit,
3332 .bridge = lcr_bridge,
3333 .hangup = lcr_hangup,
3334 .answer = lcr_answer,
3337 .indicate = lcr_indicate,
3339 .send_text = lcr_send_text,
3348 static int lcr_show_lcr (int fd, int argc, char *argv[])
3353 static int lcr_show_calls (int fd, int argc, char *argv[])
3358 static int lcr_reload_routing (int fd, int argc, char *argv[])
3363 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3368 static int lcr_port_block (int fd, int argc, char *argv[])
3373 static int lcr_port_unblock (int fd, int argc, char *argv[])
3378 static int lcr_port_unload (int fd, int argc, char *argv[])
3383 static struct ast_cli_entry cli_show_lcr =
3384 { {"lcr", "show", "lcr", NULL},
3386 "Shows current states of LCR core",
3387 "Usage: lcr show lcr\n",
3390 static struct ast_cli_entry cli_show_calls =
3391 { {"lcr", "show", "calls", NULL},
3393 "Shows current calls made by LCR and Asterisk",
3394 "Usage: lcr show calls\n",
3397 static struct ast_cli_entry cli_reload_routing =
3398 { {"lcr", "reload", "routing", NULL},
3400 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3401 "Usage: lcr reload routing\n",
3404 static struct ast_cli_entry cli_reload_interfaces =
3405 { {"lcr", "reload", "interfaces", NULL},
3406 lcr_reload_interfaces,
3407 "Reloads interfaces conf of LCR",
3408 "Usage: lcr reload interfaces\n",
3411 static struct ast_cli_entry cli_port_block =
3412 { {"lcr", "port", "block", NULL},
3414 "Blocks LCR port for further calls",
3415 "Usage: lcr port block \"<port>\"\n",
3418 static struct ast_cli_entry cli_port_unblock =
3419 { {"lcr", "port", "unblock", NULL},
3421 "Unblocks or loads LCR port, port is opened my mISDN",
3422 "Usage: lcr port unblock \"<port>\"\n",
3425 static struct ast_cli_entry cli_port_unload =
3426 { {"lcr", "port", "unload", NULL},
3428 "Unloads LCR port, port is closes by mISDN",
3429 "Usage: lcr port unload \"<port>\"\n",
3434 #ifdef LCR_FOR_ASTERISK
3435 #ifdef AST_1_8_OR_HIGHER
3436 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3438 static int lcr_config_exec(struct ast_channel *ast, void *data)
3442 #ifdef LCR_FOR_CALLWEAVER
3443 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3446 struct chan_call *call;
3448 ast_mutex_lock(&chan_lock);
3450 #ifdef LCR_FOR_ASTERISK
3451 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3454 #ifdef LCR_FOR_CALLWEAVER
3455 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3461 if (call->ast == ast)
3467 #ifdef LCR_FOR_ASTERISK
3468 apply_opt(call, (char *)data);
3471 #ifdef LCR_FOR_CALLWEAVER
3472 apply_opt(call, (char *)argv[0]);
3476 if (call->tx_queue) {
3477 union parameter newparam;
3479 memset(&newparam, 0, sizeof(union parameter));
3480 newparam.queue = call->tx_queue * 8;
3481 send_message(MESSAGE_DISABLE_DEJITTER, call->ref, &newparam);
3484 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3486 ast_mutex_unlock(&chan_lock);
3491 * module loading and destruction
3493 int load_module(void)
3496 char options_error[256];
3498 for (i = 0; i < 256; i++) {
3499 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3500 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3503 if (read_options(options_error) == 0) {
3504 CERROR(NULL, NULL, "%s", options_error);
3506 #ifdef LCR_FOR_ASTERISK
3507 return AST_MODULE_LOAD_DECLINE;
3510 #ifdef LCR_FOR_CALLWEAVER
3516 ast_mutex_init(&chan_lock);
3517 ast_mutex_init(&log_lock);
3519 #if ASTERISK_VERSION_NUM < 100000
3520 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3522 struct ast_format tmp;
3523 ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3524 if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3525 return AST_MODULE_LOAD_DECLINE;
3527 ast_format_cap_add(lcr_tech.capabilities, &tmp);
3529 if (ast_channel_register(&lcr_tech)) {
3530 CERROR(NULL, NULL, "Unable to register channel class\n");
3533 #ifdef LCR_FOR_ASTERISK
3534 return AST_MODULE_LOAD_DECLINE;
3537 #ifdef LCR_FOR_CALLWEAVER
3542 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3544 #ifdef LCR_FOR_ASTERISK
3545 "lcr_config(<opt><optarg>:<opt>:...)\n"
3548 #ifdef LCR_FOR_CALLWEAVER
3549 "lcr_config(<opt><optarg>:<opt>:...)\n",
3552 "Sets LCR opts. and optargs\n"
3554 "The available options are:\n"
3555 " d - Send display text on called phone, text is the optarg.\n"
3556 " D - Forward detected dtmf tones from LCR.\n"
3557 " h - Force data call (HDLC).\n"
3558 " q - Add queue to make fax stream seamless (required for fax app).\n"
3559 " Use queue size in miliseconds for optarg. (try 250)\n"
3560 " a - Adding DTMF detection.\n"
3561 " f - Adding fax detection.\n"
3563 " c - Make crypted outgoing call, optarg is keyindex.\n"
3564 " e - Perform echo cancelation on this channel.\n"
3566 " Takes mISDN pipeline option as optarg.\n"
3567 " s - Send Non Inband DTMF as inband. (disables LCR's DTMF)\n"
3568 " r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3570 " vr - rxgain control\n"
3571 " vt - txgain control\n"
3573 " Volume changes at factor 2 ^ optarg.\n"
3574 " k - use keypad to dial this call.\n"
3576 "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3577 " -> use 16 for fax (3.1k audio)\n"
3579 "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3580 "options: \"n:t:q250\" for seamless audio transmission.\n"
3585 ast_cli_register(&cli_show_lcr);
3586 ast_cli_register(&cli_show_calls);
3587 ast_cli_register(&cli_reload_routing);
3588 ast_cli_register(&cli_reload_interfaces);
3589 ast_cli_register(&cli_port_block);
3590 ast_cli_register(&cli_port_unblock);
3591 ast_cli_register(&cli_port_unload);
3594 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3595 /* failed to create thread */
3597 ast_channel_unregister(&lcr_tech);
3599 #ifdef LCR_FOR_ASTERISK
3600 return AST_MODULE_LOAD_DECLINE;
3603 #ifdef LCR_FOR_CALLWEAVER
3611 int unload_module(void)
3613 /* First, take us out of the channel loop */
3614 CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3616 pthread_cancel(chan_tid);
3620 del_timer(&socket_retry);
3622 unregister_fd(&wake_fd);
3623 close(wake_pipe[0]);
3624 close(wake_pipe[1]);
3626 // ast_mutex_unlock(&chan_lock);
3628 ast_channel_unregister(&lcr_tech);
3630 ast_unregister_application("lcr_config");
3632 if (lcr_sock >= 0) {
3637 #if ASTERISK_VERSION_NUM >= 100000
3638 lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3643 int reload_module(void)
3649 #ifdef LCR_FOR_ASTERISK
3650 #define AST_MODULE "chan_lcr"
3653 #ifdef LCR_FOR_CALLWEAVER
3658 ast_mutex_lock(&usecnt_lock);
3660 ast_mutex_unlock(&usecnt_lock);
3665 #ifdef LCR_FOR_ASTERISK
3666 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3667 .load = load_module,
3668 .unload = unload_module,
3669 .reload = reload_module,
3673 #ifdef LCR_FOR_CALLWEAVER
3674 char *description(void)