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.
113 #include <sys/types.h>
115 //#include <signal.h>
118 #include <sys/ioctl.h>
119 #include <sys/socket.h>
122 #include <semaphore.h>
124 #include <asterisk/module.h>
125 #include <asterisk/channel.h>
126 #include <asterisk/config.h>
127 #include <asterisk/logger.h>
128 #include <asterisk/pbx.h>
129 #include <asterisk/options.h>
130 #include <asterisk/io.h>
131 #include <asterisk/frame.h>
132 #include <asterisk/translate.h>
133 #include <asterisk/cli.h>
134 #include <asterisk/musiconhold.h>
135 #include <asterisk/dsp.h>
136 #include <asterisk/translate.h>
137 #include <asterisk/file.h>
138 #include <asterisk/callerid.h>
139 #include <asterisk/indications.h>
140 #include <asterisk/app.h>
141 #include <asterisk/features.h>
142 #include <asterisk/sched.h>
144 #include "extension.h"
146 #include "callerid.h"
147 #include "lcrsocket.h"
149 #include "bchannel.h"
151 #include "chan_lcr.h"
153 CHAN_LCR_STATE // state description structure
154 MESSAGES // message text
156 unsigned char flip_bits[256];
161 char lcr_type[]="lcr";
164 ast_mutex_t chan_lock; /* global lock */
165 ast_mutex_t log_lock; /* logging log */
168 int glob_channel = 0;
173 struct admin_list *next;
174 struct admin_message msg;
175 } *admin_first = NULL;
177 static struct ast_channel_tech lcr_tech;
182 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, ...)
185 char call_text[128] = "NULL";
186 char ast_text[128] = "NULL";
189 ast_mutex_lock(&log_lock);
192 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
193 buffer[sizeof(buffer)-1]=0;
197 sprintf(call_text, "%d", call->ref);
199 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
200 ast_text[sizeof(ast_text)-1] = '\0';
202 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
204 ast_mutex_unlock(&log_lock);
208 * channel and call instances
210 struct chan_call *call_first;
214 * special case: 0: find new ref, that has not been assigned a ref yet
217 struct chan_call *find_call_ref(unsigned int ref)
219 struct chan_call *call = call_first;
220 int assigned = (ref > 0);
224 if (call->ref == ref && call->ref_was_assigned == assigned)
231 void free_call(struct chan_call *call)
233 struct chan_call **temp = &call_first;
239 *temp = (*temp)->next;
240 if (call->pipe[0] > -1)
241 close(call->pipe[0]);
242 if (call->pipe[1] > -1)
243 close(call->pipe[1]);
246 if (call->bchannel->call != call)
247 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
248 call->bchannel->call = NULL;
250 if (call->bridge_call)
252 if (call->bridge_call->bridge_call != call)
253 CERROR(call, NULL, "Linked call structure has no link to us.\n");
254 call->bridge_call->bridge_call = NULL;
256 CDEBUG(call, NULL, "Call instance freed.\n");
260 temp = &((*temp)->next);
262 CERROR(call, NULL, "Call instance not found in list.\n");
265 struct chan_call *alloc_call(void)
267 struct chan_call **callp = &call_first;
270 callp = &((*callp)->next);
272 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
274 memset(*callp, 0, sizeof(struct chan_call));
275 if (pipe((*callp)->pipe) < 0) {
276 CERROR(*callp, NULL, "Failed to create pipe.\n");
280 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
281 CDEBUG(*callp, NULL, "Call instance allocated.\n");
285 unsigned short new_bridge_id(void)
287 struct chan_call *call;
288 unsigned short id = 1;
290 /* search for lowest bridge id that is not in use and not 0 */
296 if (call->bridge_id == id)
304 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
309 * enque message to LCR
311 int send_message(int message_type, unsigned int ref, union parameter *param)
313 struct admin_list *admin, **adminp;
316 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
319 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
321 adminp = &admin_first;
323 adminp = &((*adminp)->next);
324 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
326 CERROR(NULL, NULL, "No memory for message to LCR.\n");
331 admin->msg.message = ADMIN_MESSAGE;
332 admin->msg.u.msg.type = message_type;
333 admin->msg.u.msg.ref = ref;
334 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
340 * apply options (in locked state)
342 void apply_opt(struct chan_call *call, char *data)
344 union parameter newparam;
345 char string[1024], *p = string, *opt, *key;
346 int gain, i, newmode = 0;
351 strncpy(string, data, sizeof(string)-1);
352 string[sizeof(string)-1] = '\0';
355 while((opt = strsep(&p, ":")))
359 if (opt[1] == '\0') {
360 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
363 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
364 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
365 strncpy(call->display, opt+1, sizeof(call->display)-1);
367 memset(&newparam, 0, sizeof(union parameter));
368 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
369 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
373 if (opt[1] != '\0') {
374 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
377 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
381 if (opt[1] == '\0') {
382 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
386 /* check for 0xXXXX... type of key */
387 if (!!strncmp((char *)key, "0x", 2)) {
388 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
392 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
393 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
399 if (*key>='0' && *key<='9')
400 call->bf_key[i] = (*key-'0') << 8;
401 else if (*key>='a' && *key<='f')
402 call->bf_key[i] = (*key-'a'+10) << 8;
403 else if (*key>='A' && *key<='F')
404 call->bf_key[i] = (*key-'A'+10) << 8;
408 if (*key>='0' && *key<='9')
409 call->bf_key[i] += (*key - '0');
410 else if (*key>='a' && *key<='f')
411 call->bf_key[i] += (*key - 'a' + 10);
412 else if (*key>='A' && *key<='F')
413 call->bf_key[i] += (*key - 'A' + 10);
420 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
424 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
426 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
429 if (opt[1] != '\0') {
430 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
433 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
440 if (opt[1] != '\0') {
441 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
444 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
451 if (opt[1] == '\0') {
452 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
455 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
456 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
458 bchannel_pipeline(call->bchannel, call->pipeline);
461 if (opt[1] != '\0') {
462 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
465 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
469 if (opt[1] != '\0') {
470 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
473 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
474 call->inband_dtmf = 1;
477 if (opt[1] != 'r' && opt[1] != 't') {
478 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
482 if (gain < -8 || gain >8) {
483 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
486 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
488 call->rx_gain = gain;
490 bchannel_gain(call->bchannel, call->rx_gain, 0);
492 call->tx_gain = gain;
494 bchannel_gain(call->bchannel, call->tx_gain, 1);
498 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
502 /* re-open, if bchannel is created */
503 if (call->bchannel && call->bchannel->b_sock > -1) {
504 bchannel_destroy(call->bchannel);
505 if (bchannel_create(call->bchannel, ((call->nodsp)?1:0) + ((call->hdlc)?2:0)))
506 bchannel_activate(call->bchannel, 1);
511 * send setup info to LCR
512 * this function is called, when asterisk call is received and ref is received
514 static void send_setup_to_lcr(struct chan_call *call)
516 union parameter newparam;
517 struct ast_channel *ast = call->ast;
519 if (!call->ast || !call->ref)
522 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
524 /* send setup message to LCR */
525 memset(&newparam, 0, sizeof(union parameter));
526 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
527 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
528 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
529 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
530 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
531 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
532 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
533 call->display[0] = '\0';
534 if (call->cid_num[0])
535 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
536 if (call->cid_name[0])
537 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
538 if (call->cid_rdnis[0])
540 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
541 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
542 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
544 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
546 case AST_PRES_RESTRICTED:
547 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
549 case AST_PRES_UNAVAILABLE:
550 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
552 case AST_PRES_ALLOWED:
554 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
556 switch(ast->cid.cid_ton)
559 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
562 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
565 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
568 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
570 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
571 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
573 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
575 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
576 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
578 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
579 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
580 send_message(MESSAGE_SETUP, call->ref, &newparam);
582 /* change to outgoing setup state */
583 call->state = CHAN_LCR_STATE_OUT_SETUP;
587 * send dialing info to LCR
588 * this function is called, when setup acknowledge is received and dialing
591 static void send_dialque_to_lcr(struct chan_call *call)
593 union parameter newparam;
595 if (!call->ast || !call->ref || !call->dialque[0])
598 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
600 /* send setup message to LCR */
601 memset(&newparam, 0, sizeof(union parameter));
602 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
603 call->dialque[0] = '\0';
604 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
608 * in case of a bridge, the unsupported message can be forwarded directly
609 * to the remote call.
611 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
615 if (!call->bridge_call) return;
616 CDEBUG(call, NULL, "Sending message due bridging.\n");
617 send_message(message_type, call->bridge_call->ref, param);
621 * send release message to LCR and import bchannel if exported
623 static void send_release_and_import(struct chan_call *call, int cause, int location)
625 union parameter newparam;
627 /* importing channel */
628 if (call->bchannel) {
629 memset(&newparam, 0, sizeof(union parameter));
630 newparam.bchannel.type = BCHANNEL_RELEASE;
631 newparam.bchannel.handle = call->bchannel->handle;
632 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
634 /* sending release */
635 memset(&newparam, 0, sizeof(union parameter));
636 newparam.disconnectinfo.cause = cause;
637 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
638 send_message(MESSAGE_RELEASE, call->ref, &newparam);
642 * check if extension matches and start asterisk
643 * if it can match, proceed
646 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
649 union parameter newparam;
650 char *exten = ast->exten;
654 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
659 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad))
661 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
665 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad))
667 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);
671 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
672 /* send setup acknowledge to lcr */
673 memset(&newparam, 0, sizeof(union parameter));
674 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
677 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
682 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad))
684 /* send setup acknowledge to lcr */
685 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
686 memset(&newparam, 0, sizeof(union parameter));
687 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
691 call->state = CHAN_LCR_STATE_IN_DIALING;
693 /* if match, start pbx */
694 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
695 CDEBUG(call, ast, "Extensions matches.\n");
700 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
706 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
714 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
715 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
717 /* release asterisk */
718 ast->hangupcause = call->cause;
719 /* change to release state */
720 call->state = CHAN_LCR_STATE_RELEASE;
721 ast_hangup(ast); // call will be destroyed here
725 /* send setup to asterisk */
726 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
727 ret = ast_pbx_start(ast);
730 cause = (ret==-2)?34:27;
733 call->pbx_started = 1;
734 ast_setstate(ast, AST_STATE_RING);
738 * incoming setup from LCR
740 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
742 struct ast_channel *ast;
744 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
746 /* create asterisk channel instrance */
747 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
751 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
752 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
759 ast->tech_pvt = call;
760 ast->tech = &lcr_tech;
761 ast->fds[0] = call->pipe[0];
763 /* fill setup information */
764 if (param->setup.dialinginfo.id)
765 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
766 if (param->setup.context[0])
767 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
769 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
770 if (param->setup.callerinfo.id[0])
771 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
772 if (param->setup.callerinfo.name[0])
773 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
774 if (param->setup.redirinfo.id[0])
775 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
776 switch (param->setup.callerinfo.present)
778 case INFO_PRESENT_ALLOWED:
779 ast->cid.cid_pres = AST_PRES_ALLOWED;
781 case INFO_PRESENT_RESTRICTED:
782 ast->cid.cid_pres = AST_PRES_RESTRICTED;
785 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
787 switch (param->setup.callerinfo.ntype)
789 case INFO_NTYPE_SUBSCRIBER:
790 ast->cid.cid_ton = 4;
792 case INFO_NTYPE_NATIONAL:
793 ast->cid.cid_ton = 2;
795 case INFO_NTYPE_INTERNATIONAL:
796 ast->cid.cid_ton = 1;
799 ast->cid.cid_ton = 0;
801 ast->transfercapability = param->setup.capainfo.bearer_capa;
802 /* enable hdlc if transcap is data */
803 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
805 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
807 /* configure channel */
808 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
809 ast->readformat = ast->rawreadformat = ast->nativeformats;
810 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
812 ast->hangupcause = 0;
815 call->state = CHAN_LCR_STATE_IN_SETUP;
817 if (!call->pbx_started)
818 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
822 * incoming setup acknowledge from LCR
824 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
826 if (!call->ast) return;
828 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
830 /* send pending digits in dialque */
831 if (call->dialque[0])
832 send_dialque_to_lcr(call);
833 /* change to overlap state */
834 call->state = CHAN_LCR_STATE_OUT_DIALING;
838 * incoming proceeding from LCR
840 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
842 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
845 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
846 /* queue event for asterisk */
847 if (call->ast && call->pbx_started)
848 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
852 * incoming alerting from LCR
854 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
856 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
859 call->state = CHAN_LCR_STATE_OUT_ALERTING;
860 /* queue event to asterisk */
861 if (call->ast && call->pbx_started)
862 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
866 * incoming connect from LCR
868 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
870 union parameter newparam;
872 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
875 call->state = CHAN_LCR_STATE_CONNECT;
876 /* request bchannel */
877 if (!call->bchannel) {
878 CDEBUG(call, call->ast, "Requesting B-channel.\n");
879 memset(&newparam, 0, sizeof(union parameter));
880 newparam.bchannel.type = BCHANNEL_REQUEST;
881 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
883 /* copy connectinfo */
884 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
885 /* queue event to asterisk */
886 if (call->ast && call->pbx_started)
887 strncat(call->queue_string, "A", sizeof(call->queue_string)-1);
891 * incoming disconnect from LCR
893 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
895 struct ast_channel *ast = call->ast;
897 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
900 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
902 call->cause = param->disconnectinfo.cause;
903 call->location = param->disconnectinfo.location;
904 /* if bridge, forward disconnect and return */
907 if (call->bridge_call)
909 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
910 bridge_message_if_bridged(call, message_type, param);
914 /* release lcr with same cause */
915 send_release_and_import(call, call->cause, call->location);
917 /* change to release state */
918 call->state = CHAN_LCR_STATE_RELEASE;
919 /* queue release asterisk */
922 ast->hangupcause = call->cause;
923 if (call->pbx_started)
924 strcpy(call->queue_string, "H"); // overwrite other indications
926 ast_hangup(ast); // call will be destroyed here
932 * incoming release from LCR
934 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
936 struct ast_channel *ast = call->ast;
938 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
942 /* change to release state */
943 call->state = CHAN_LCR_STATE_RELEASE;
944 /* copy release info */
947 call->cause = param->disconnectinfo.cause;
948 call->location = param->disconnectinfo.location;
950 /* if we have an asterisk instance, queue hangup, else we are done */
953 ast->hangupcause = call->cause;
954 if (call->pbx_started)
955 strcpy(call->queue_string, "H");
957 ast_hangup(ast); // call will be destroyed here
967 * incoming information from LCR
969 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
971 struct ast_channel *ast = call->ast;
973 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
977 /* pbx not started */
978 if (!call->pbx_started)
980 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
981 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
982 lcr_start_pbx(call, ast, param->information.sending_complete);
986 /* change dailing state after setup */
987 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
988 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
989 call->state = CHAN_LCR_STATE_IN_DIALING;
990 // ast_setstate(ast, AST_STATE_DIALING);
994 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
995 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
997 /* use bridge to forware message not supported by asterisk */
998 if (call->state == CHAN_LCR_STATE_CONNECT) {
999 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1000 bridge_message_if_bridged(call, message_type, param);
1005 * incoming information from LCR
1007 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1009 union parameter newparam;
1011 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1013 /* request bchannel, if call is resumed and we don't have it */
1014 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1015 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1016 memset(&newparam, 0, sizeof(union parameter));
1017 newparam.bchannel.type = BCHANNEL_REQUEST;
1018 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1021 if (!call->ast) return;
1023 /* use bridge to forware message not supported by asterisk */
1024 bridge_message_if_bridged(call, message_type, param);
1028 * incoming information from LCR
1030 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1032 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1034 if (!call->ast) return;
1036 /* use bridge to forware message not supported by asterisk */
1037 bridge_message_if_bridged(call, message_type, param);
1041 * got dtmf from bchannel (locked state)
1043 void lcr_in_dtmf(struct chan_call *call, int val)
1045 struct ast_channel *ast = call->ast;
1050 if (!call->pbx_started)
1053 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1056 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1060 * message received from LCR
1062 int receive_message(int message_type, unsigned int ref, union parameter *param)
1064 struct bchannel *bchannel;
1065 struct chan_call *call;
1066 union parameter newparam;
1068 memset(&newparam, 0, sizeof(union parameter));
1070 /* handle bchannel message*/
1071 if (message_type == MESSAGE_BCHANNEL)
1073 switch(param->bchannel.type)
1075 case BCHANNEL_ASSIGN:
1076 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1077 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1079 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1082 /* create bchannel */
1083 bchannel = alloc_bchannel(param->bchannel.handle);
1086 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1090 /* configure channel */
1091 bchannel->b_tx_gain = param->bchannel.tx_gain;
1092 bchannel->b_rx_gain = param->bchannel.rx_gain;
1093 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1094 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1096 bchannel->b_bf_len = param->bchannel.crypt_len;
1097 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1099 bchannel->b_txdata = 0;
1100 bchannel->b_dtmf = 1;
1101 bchannel->b_tx_dejitter = 1;
1103 /* in case, ref is not set, this bchannel instance must
1104 * be created until it is removed again by LCR */
1106 call = find_call_ref(ref);
1109 bchannel->call = call;
1110 call->bchannel = bchannel;
1112 bchannel_dtmf(bchannel, 1);
1114 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1115 if (call->pipeline[0])
1116 bchannel_pipeline(bchannel, call->pipeline);
1118 bchannel_gain(bchannel, call->rx_gain, 0);
1120 bchannel_gain(bchannel, call->tx_gain, 1);
1121 if (call->bridge_id) {
1122 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1123 bchannel_join(bchannel, call->bridge_id);
1125 /* create only, if call exists, othewhise it bchannel is freed below... */
1126 if (bchannel_create(bchannel, ((call->nodsp)?1:0) + ((call->hdlc)?2:0)))
1127 bchannel_activate(bchannel, 1);
1130 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1131 newparam.bchannel.handle = param->bchannel.handle;
1132 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1133 /* if call has released before bchannel is assigned */
1135 newparam.bchannel.type = BCHANNEL_RELEASE;
1136 newparam.bchannel.handle = param->bchannel.handle;
1137 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1142 case BCHANNEL_REMOVE:
1143 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1144 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1146 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1149 /* unklink from call and destroy bchannel */
1150 free_bchannel(bchannel);
1153 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1154 newparam.bchannel.handle = param->bchannel.handle;
1155 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1160 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1165 /* handle new ref */
1166 if (message_type == MESSAGE_NEWREF)
1168 if (param->direction)
1170 /* new ref from lcr */
1171 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1172 if (!ref || find_call_ref(ref))
1174 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1177 /* allocate new call instance */
1178 call = alloc_call();
1180 call->state = CHAN_LCR_STATE_IN_PREPARE;
1183 call->ref_was_assigned = 1;
1184 /* wait for setup (or release from asterisk) */
1187 /* new ref, as requested from this remote application */
1188 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1189 call = find_call_ref(0);
1192 /* send release, if ref does not exist */
1193 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1194 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1199 call->ref_was_assigned = 1;
1200 /* send pending setup info */
1201 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1202 send_setup_to_lcr(call);
1203 /* release if asterisk has signed off */
1204 else if (call->state == CHAN_LCR_STATE_RELEASE)
1208 send_release_and_import(call, call->cause, call->location);
1210 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1222 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1225 call = find_call_ref(ref);
1228 /* ignore ref that is not used (anymore) */
1229 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1233 /* handle messages */
1234 switch(message_type)
1237 lcr_in_setup(call, message_type, param);
1240 case MESSAGE_OVERLAP:
1241 lcr_in_overlap(call, message_type, param);
1244 case MESSAGE_PROCEEDING:
1245 lcr_in_proceeding(call, message_type, param);
1248 case MESSAGE_ALERTING:
1249 lcr_in_alerting(call, message_type, param);
1252 case MESSAGE_CONNECT:
1253 lcr_in_connect(call, message_type, param);
1256 case MESSAGE_DISCONNECT:
1257 lcr_in_disconnect(call, message_type, param);
1260 case MESSAGE_RELEASE:
1261 lcr_in_release(call, message_type, param);
1264 case MESSAGE_INFORMATION:
1265 lcr_in_information(call, message_type, param);
1268 case MESSAGE_NOTIFY:
1269 lcr_in_notify(call, message_type, param);
1272 case MESSAGE_FACILITY:
1273 lcr_in_facility(call, message_type, param);
1276 case MESSAGE_PATTERN: // audio available from LCR
1279 case MESSAGE_NOPATTERN: // audio not available from LCR
1282 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1283 call->audiopath = param->audiopath;
1287 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1294 * release all calls (due to broken socket)
1296 static void release_all_calls(void)
1298 struct chan_call *call;
1303 /* no ast, so we may directly free call */
1305 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1309 /* already in release process */
1310 if (call->state == CHAN_LCR_STATE_RELEASE) {
1314 /* release or queue release */
1316 call->state = CHAN_LCR_STATE_RELEASE;
1317 if (!call->pbx_started) {
1318 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1319 ast_hangup(call->ast); // call will be destroyed here
1322 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1323 strcpy(call->queue_string, "H");
1327 /* release all bchannels */
1328 while(bchannel_first)
1329 free_bchannel(bchannel_first);
1334 * warning! not thread safe
1335 * returns -1 for socket error, 0 for no work, 1 for work
1337 int handle_socket(void)
1341 struct admin_list *admin;
1342 struct admin_message msg;
1344 /* read from socket */
1345 len = read(lcr_sock, &msg, sizeof(msg));
1348 CERROR(NULL, NULL, "Socket closed.\n");
1349 return(-1); // socket closed
1353 if (len != sizeof(msg))
1355 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1356 return(-1); // socket error
1358 if (msg.message != ADMIN_MESSAGE)
1360 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1363 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1367 if (errno != EWOULDBLOCK)
1369 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1374 /* write to socket */
1377 admin = admin_first;
1378 len = write(lcr_sock, &admin->msg, sizeof(msg));
1381 CERROR(NULL, NULL, "Socket closed.\n");
1382 return(-1); // socket closed
1386 if (len != sizeof(msg))
1388 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1389 return(-1); // socket error
1392 admin_first = admin->next;
1398 if (errno != EWOULDBLOCK)
1400 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1409 * open and close socket and thread
1411 int open_socket(void)
1415 struct sockaddr_un sock_address;
1416 unsigned int on = 1;
1417 union parameter param;
1420 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1422 CERROR(NULL, NULL, "Failed to create socket.\n");
1426 /* set socket address and name */
1427 memset(&sock_address, 0, sizeof(sock_address));
1428 sock_address.sun_family = PF_UNIX;
1429 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1431 /* connect socket */
1432 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1436 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1440 /* set non-blocking io */
1441 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1445 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1449 /* enque hello message */
1450 memset(¶m, 0, sizeof(param));
1451 strcpy(param.hello.application, "asterisk");
1452 send_message(MESSAGE_HELLO, 0, ¶m);
1457 void close_socket(void)
1459 struct admin_list *admin, *temp;
1461 /* flush pending messages */
1462 admin = admin_first;
1465 admin = admin->next;
1477 /* sending queue to asterisk */
1478 static int queue_send(void)
1481 struct chan_call *call;
1482 struct ast_channel *ast;
1483 struct ast_frame fr;
1488 p = call->queue_string;
1491 /* there is something to queue */
1492 if (!ast_channel_trylock(ast)) { /* succeed */
1496 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1497 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1500 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1501 ast_queue_control(ast, AST_CONTROL_RINGING);
1502 ast_setstate(ast, AST_STATE_RINGING);
1505 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1506 ast_queue_control(ast, AST_CONTROL_ANSWER);
1509 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1510 ast_queue_hangup(ast);
1512 case '1': case '2': case '3': case 'a':
1513 case '4': case '5': case '6': case 'b':
1514 case '7': case '8': case '9': case 'c':
1515 case '*': case '0': case '#': case 'd':
1516 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1517 /* send digit to asterisk */
1518 memset(&fr, 0, sizeof(fr));
1519 fr.frametype = AST_FRAME_DTMF_BEGIN;
1521 fr.delivery = ast_tv(0, 0);
1522 ast_queue_frame(ast, &fr);
1523 fr.frametype = AST_FRAME_DTMF_END;
1524 ast_queue_frame(ast, &fr);
1527 CDEBUG(call, ast, "Ignoring queued digit 0x%02d.\n", *p);
1531 call->queue_string[0] = '\0';
1532 ast_channel_unlock(ast);
1542 /* signal handler */
1543 void sighandler(int sigset)
1547 /* chan_lcr thread */
1548 static void *chan_thread(void *arg)
1552 union parameter param;
1553 time_t retry = 0, now;
1555 bchannel_pid = getpid();
1557 // signal(SIGPIPE, sighandler);
1559 memset(¶m, 0, sizeof(union parameter));
1563 ast_mutex_lock(&chan_lock);
1570 ret = handle_socket();
1572 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1574 release_all_calls();
1581 if (retry && now-retry > 5) {
1582 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1584 if (open_socket() < 0) {
1593 ret = bchannel_handle();
1597 /* handle messages to asterisk */
1602 /* delay if no work done */
1604 ast_mutex_unlock(&chan_lock);
1606 ast_mutex_lock(&chan_lock);
1612 CERROR(NULL, NULL, "Thread exit.\n");
1614 ast_mutex_unlock(&chan_lock);
1616 // signal(SIGPIPE, SIG_DFL);
1622 * new asterisk instance
1625 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1627 char exten[256], *dial, *interface, *opt;
1628 struct ast_channel *ast;
1629 struct chan_call *call;
1631 ast_mutex_lock(&chan_lock);
1632 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1634 /* if socket is closed */
1637 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1638 ast_mutex_unlock(&chan_lock);
1642 /* create call instance */
1643 call = alloc_call();
1646 /* failed to create instance */
1647 ast_mutex_unlock(&chan_lock);
1651 /* create asterisk channel instrance */
1652 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1655 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1657 /* failed to create instance */
1658 ast_mutex_unlock(&chan_lock);
1661 ast->tech = &lcr_tech;
1662 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1663 /* configure channel */
1664 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1665 ast->readformat = ast->rawreadformat = ast->nativeformats;
1666 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1668 ast->hangupcause = 0;
1672 ast->tech_pvt = call;
1673 ast->fds[0] = call->pipe[0];
1674 call->pbx_started = 0;
1676 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1679 * Extract interface, dialstring, options from data.
1682 * <interface>/<dialstring>
1683 * <interface>/<dialstring>/options
1685 strncpy(exten, (char *)data, sizeof(exten)-1);
1686 exten[sizeof(exten)-1] = '\0';
1687 if ((dial = strchr(exten, '/'))) {
1690 if ((opt = strchr(dial, '/')))
1699 strncpy(call->interface, interface, sizeof(call->interface)-1);
1700 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1701 apply_opt(call, (char *)opt);
1703 ast_mutex_unlock(&chan_lock);
1708 * call from asterisk
1710 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1712 union parameter newparam;
1713 struct chan_call *call;
1715 ast_mutex_lock(&chan_lock);
1716 call = ast->tech_pvt;
1718 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1719 ast_mutex_unlock(&chan_lock);
1723 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1725 /* pbx process is started */
1726 call->pbx_started = 1;
1727 /* send MESSAGE_NEWREF */
1728 memset(&newparam, 0, sizeof(union parameter));
1729 newparam.direction = 0; /* request from app */
1730 send_message(MESSAGE_NEWREF, 0, &newparam);
1732 /* set hdlc if capability requires hdlc */
1733 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1734 || ast->transfercapability == INFO_BC_DATARESTRICTED
1735 || ast->transfercapability == INFO_BC_VIDEO)
1737 /* if hdlc is forced by option, we change transcap to data */
1739 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1740 && ast->transfercapability != INFO_BC_DATARESTRICTED
1741 && ast->transfercapability != INFO_BC_VIDEO)
1742 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1744 call->cid_num[0] = 0;
1745 call->cid_name[0] = 0;
1746 call->cid_rdnis[0] = 0;
1748 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1749 strncpy(call->cid_num, ast->cid.cid_num,
1750 sizeof(call->cid_num)-1);
1752 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1753 strncpy(call->cid_name, ast->cid.cid_name,
1754 sizeof(call->cid_name)-1);
1755 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1756 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1757 sizeof(call->cid_rdnis)-1);
1759 ast_mutex_unlock(&chan_lock);
1763 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1765 static const char* dtmf_tones[] = {
1766 "!941+1336/100,!0/100", /* 0 */
1767 "!697+1209/100,!0/100", /* 1 */
1768 "!697+1336/100,!0/100", /* 2 */
1769 "!697+1477/100,!0/100", /* 3 */
1770 "!770+1209/100,!0/100", /* 4 */
1771 "!770+1336/100,!0/100", /* 5 */
1772 "!770+1477/100,!0/100", /* 6 */
1773 "!852+1209/100,!0/100", /* 7 */
1774 "!852+1336/100,!0/100", /* 8 */
1775 "!852+1477/100,!0/100", /* 9 */
1776 "!697+1633/100,!0/100", /* A */
1777 "!770+1633/100,!0/100", /* B */
1778 "!852+1633/100,!0/100", /* C */
1779 "!941+1633/100,!0/100", /* D */
1780 "!941+1209/100,!0/100", /* * */
1781 "!941+1477/100,!0/100" }; /* # */
1783 if (digit >= '0' && digit <='9')
1784 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1785 else if (digit >= 'A' && digit <= 'D')
1786 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1787 else if (digit == '*')
1788 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1789 else if (digit == '#')
1790 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1793 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1794 "'%c' for '%s'\n", digit, ast->name);
1799 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1801 struct chan_call *call;
1802 union parameter newparam;
1805 /* only pass IA5 number space */
1806 if (digit > 126 || digit < 32)
1809 ast_mutex_lock(&chan_lock);
1810 call = ast->tech_pvt;
1812 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1813 ast_mutex_unlock(&chan_lock);
1817 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1819 /* send information or queue them */
1820 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1822 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1823 memset(&newparam, 0, sizeof(union parameter));
1824 newparam.information.id[0] = digit;
1825 newparam.information.id[1] = '\0';
1826 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1829 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1831 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1833 strncat(call->dialque, buf, strlen(call->dialque)-1);
1836 ast_mutex_unlock(&chan_lock);
1840 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1842 int inband_dtmf = 0;
1843 struct chan_call *call;
1845 ast_mutex_lock(&chan_lock);
1847 call = ast->tech_pvt;
1851 "Received digit from Asterisk, "
1852 "but no call instance exists.\n");
1853 ast_mutex_unlock(&chan_lock);
1857 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
1859 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
1863 ast_mutex_unlock(&chan_lock);
1866 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
1867 send_digit_to_chan(ast, digit);
1873 static int lcr_answer(struct ast_channel *ast)
1875 union parameter newparam;
1876 struct chan_call *call;
1878 ast_mutex_lock(&chan_lock);
1879 call = ast->tech_pvt;
1881 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1882 ast_mutex_unlock(&chan_lock);
1886 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
1888 /* copy connectinfo, if bridged */
1889 if (call->bridge_call)
1890 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1891 /* send connect message to lcr */
1892 if (call->state != CHAN_LCR_STATE_CONNECT) {
1893 memset(&newparam, 0, sizeof(union parameter));
1894 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1895 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1896 call->state = CHAN_LCR_STATE_CONNECT;
1899 /* request bchannel */
1900 if (!call->bchannel) {
1901 CDEBUG(call, ast, "Requesting B-channel.\n");
1902 memset(&newparam, 0, sizeof(union parameter));
1903 newparam.bchannel.type = BCHANNEL_REQUEST;
1904 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1907 // memset(&newparam, 0, sizeof(union parameter));
1908 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1911 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1915 ast_mutex_unlock(&chan_lock);
1919 static int lcr_hangup(struct ast_channel *ast)
1921 struct chan_call *call;
1922 pthread_t tid = pthread_self();
1924 if (!pthread_equal(tid, chan_tid))
1925 ast_mutex_lock(&chan_lock);
1926 call = ast->tech_pvt;
1928 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1929 if (!pthread_equal(tid, chan_tid))
1930 ast_mutex_unlock(&chan_lock);
1934 if (!pthread_equal(tid, chan_tid))
1935 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1937 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1939 /* disconnect asterisk, maybe not required */
1940 ast->tech_pvt = NULL;
1945 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1946 if (ast->hangupcause > 0)
1947 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1949 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1952 if (!pthread_equal(tid, chan_tid))
1953 ast_mutex_unlock(&chan_lock);
1957 /* ref is not set, due to prepare setup or release */
1958 if (call->state == CHAN_LCR_STATE_RELEASE)
1960 /* we get the response to our release */
1961 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1965 /* during prepare, we change to release state */
1966 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1967 call->state = CHAN_LCR_STATE_RELEASE;
1971 if (!pthread_equal(tid, chan_tid))
1972 ast_mutex_unlock(&chan_lock);
1976 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1978 struct chan_call *call;
1981 CDEBUG(NULL, ast, "No subclass\n");
1982 if (!(f->subclass & ast->nativeformats))
1983 CDEBUG(NULL, ast, "Unexpected format.\n");
1985 ast_mutex_lock(&chan_lock);
1986 call = ast->tech_pvt;
1988 ast_mutex_unlock(&chan_lock);
1991 if (call->bchannel && f->samples)
1992 bchannel_transmit(call->bchannel, f->data, f->samples);
1993 ast_mutex_unlock(&chan_lock);
1998 static struct ast_frame *lcr_read(struct ast_channel *ast)
2000 struct chan_call *call;
2003 ast_mutex_lock(&chan_lock);
2004 call = ast->tech_pvt;
2006 ast_mutex_unlock(&chan_lock);
2009 if (call->pipe[0] > -1) {
2010 if (call->rebuffer && !call->hdlc) {
2011 len = read(call->pipe[0], call->read_buff, 160);
2013 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2015 if (len < 0 && errno == EAGAIN) {
2016 ast_mutex_unlock(&chan_lock);
2017 return &ast_null_frame;
2020 close(call->pipe[0]);
2022 ast_mutex_unlock(&chan_lock);
2027 call->read_fr.frametype = AST_FRAME_VOICE;
2028 call->read_fr.subclass = ast->nativeformats;
2029 call->read_fr.datalen = len;
2030 call->read_fr.samples = len;
2031 call->read_fr.delivery = ast_tv(0,0);
2032 call->read_fr.data = call->read_buff;
2033 ast_mutex_unlock(&chan_lock);
2035 return &call->read_fr;
2038 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2040 union parameter newparam;
2042 struct chan_call *call;
2044 ast_mutex_lock(&chan_lock);
2045 call = ast->tech_pvt;
2047 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2048 ast_mutex_unlock(&chan_lock);
2053 case AST_CONTROL_BUSY:
2054 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2055 ast_setstate(ast, AST_STATE_BUSY);
2056 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2057 /* send message to lcr */
2058 memset(&newparam, 0, sizeof(union parameter));
2059 newparam.disconnectinfo.cause = 17;
2060 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2061 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2063 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2066 case AST_CONTROL_CONGESTION:
2067 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2068 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2069 /* send message to lcr */
2070 memset(&newparam, 0, sizeof(union parameter));
2071 newparam.disconnectinfo.cause = ast->hangupcause;
2072 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2073 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2075 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2078 case AST_CONTROL_PROCEEDING:
2079 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2080 if (call->state == CHAN_LCR_STATE_IN_SETUP
2081 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2082 /* send message to lcr */
2083 memset(&newparam, 0, sizeof(union parameter));
2084 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2086 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2089 case AST_CONTROL_RINGING:
2090 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2091 ast_setstate(ast, AST_STATE_RINGING);
2092 if (call->state == CHAN_LCR_STATE_IN_SETUP
2093 || call->state == CHAN_LCR_STATE_IN_DIALING
2094 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2095 /* send message to lcr */
2096 memset(&newparam, 0, sizeof(union parameter));
2097 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2099 call->state = CHAN_LCR_STATE_IN_ALERTING;
2103 CDEBUG(call, ast, "Received indicate -1.\n");
2107 case AST_CONTROL_VIDUPDATE:
2108 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2111 case AST_CONTROL_HOLD:
2112 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2113 /* send message to lcr */
2114 memset(&newparam, 0, sizeof(union parameter));
2115 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2116 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2118 /*start music onhold*/
2119 ast_moh_start(ast,data,ast->musicclass);
2122 case AST_CONTROL_UNHOLD:
2123 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2124 /* send message to lcr */
2125 memset(&newparam, 0, sizeof(union parameter));
2126 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2127 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2133 #ifdef AST_CONTROL_SRCUPDATE
2134 case AST_CONTROL_SRCUPDATE:
2135 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2139 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2145 ast_mutex_unlock(&chan_lock);
2152 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2154 struct chan_call *call;
2160 ast_mutex_lock(&chan_lock);
2161 call = ast->tech_pvt;
2163 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2164 ast_mutex_unlock(&chan_lock);
2168 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2170 ast_mutex_unlock(&chan_lock);
2175 * send_text asterisk
2177 static int lcr_send_text(struct ast_channel *ast, const char *text)
2179 struct chan_call *call;
2180 union parameter newparam;
2182 ast_mutex_lock(&chan_lock);
2183 call = ast->tech_pvt;
2185 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2186 ast_mutex_unlock(&chan_lock);
2190 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2191 memset(&newparam, 0, sizeof(union parameter));
2192 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2193 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2194 ast_mutex_lock(&chan_lock);
2201 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2202 struct ast_channel *ast2, int flags,
2203 struct ast_frame **fo,
2204 struct ast_channel **rc, int timeoutms)
2207 struct chan_call *call1, *call2;
2208 struct ast_channel *carr[2], *who;
2210 struct ast_frame *f;
2213 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2218 /* join via dsp (if the channels are currently open) */
2219 ast_mutex_lock(&chan_lock);
2220 call1 = ast1->tech_pvt;
2221 call2 = ast2->tech_pvt;
2222 if (!call1 || !call2) {
2223 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2224 ast_mutex_unlock(&chan_lock);
2225 return AST_BRIDGE_COMPLETE;
2228 /* join, if both call instances uses dsp */
2229 if (!call1->nodsp && !call2->nodsp) {
2230 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2232 /* get bridge id and join */
2233 bridge_id = new_bridge_id();
2235 call1->bridge_id = bridge_id;
2236 if (call1->bchannel)
2237 bchannel_join(call1->bchannel, bridge_id);
2239 call2->bridge_id = bridge_id;
2240 if (call2->bchannel)
2241 bchannel_join(call2->bchannel, bridge_id);
2243 if (call1->nodsp && call2->nodsp)
2244 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2246 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2247 call1->bridge_call = call2;
2248 call2->bridge_call = call1;
2250 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2251 || call1->state == CHAN_LCR_STATE_IN_DIALING
2252 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2253 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2254 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2257 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2258 || call2->state == CHAN_LCR_STATE_IN_DIALING
2259 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2260 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2261 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2265 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2266 so let's do it for them. Hmpf.
2269 if (call1->on_hold) {
2270 union parameter newparam;
2272 memset(&newparam, 0, sizeof(union parameter));
2273 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2274 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2279 if (call2->on_hold) {
2280 union parameter newparam;
2282 memset(&newparam, 0, sizeof(union parameter));
2283 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2284 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2289 ast_mutex_unlock(&chan_lock);
2293 who = ast_waitfor_n(carr, 2, &to);
2296 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2301 if (!f || f->frametype == AST_FRAME_CONTROL) {
2303 CDEBUG(NULL, NULL, "Got hangup.\n");
2305 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2312 if ( f->frametype == AST_FRAME_DTMF ) {
2313 CDEBUG(NULL, NULL, "Got DTMF.\n");
2329 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2331 /* split channels */
2332 ast_mutex_lock(&chan_lock);
2333 call1 = ast1->tech_pvt;
2334 call2 = ast2->tech_pvt;
2335 if (call1 && call1->bridge_id)
2337 call1->bridge_id = 0;
2338 if (call1->bchannel)
2339 bchannel_join(call1->bchannel, 0);
2340 if (call1->bridge_call)
2341 call1->bridge_call->bridge_call = NULL;
2343 if (call2 && call1->bridge_id)
2345 call2->bridge_id = 0;
2346 if (call2->bchannel)
2347 bchannel_join(call2->bchannel, 0);
2348 if (call2->bridge_call)
2349 call2->bridge_call->bridge_call = NULL;
2351 call1->bridge_call = NULL;
2352 call2->bridge_call = NULL;
2354 ast_mutex_unlock(&chan_lock);
2355 return AST_BRIDGE_COMPLETE;
2357 static struct ast_channel_tech lcr_tech = {
2359 .description="Channel driver for connecting to Linux-Call-Router",
2360 .requester=lcr_request,
2361 .send_digit_begin=lcr_digit_begin,
2362 .send_digit_end=lcr_digit_end,
2369 .indicate=lcr_indicate,
2371 .send_text=lcr_send_text,
2380 static int lcr_show_lcr (int fd, int argc, char *argv[])
2385 static int lcr_show_calls (int fd, int argc, char *argv[])
2390 static int lcr_reload_routing (int fd, int argc, char *argv[])
2395 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2400 static int lcr_port_block (int fd, int argc, char *argv[])
2405 static int lcr_port_unblock (int fd, int argc, char *argv[])
2410 static int lcr_port_unload (int fd, int argc, char *argv[])
2415 static struct ast_cli_entry cli_show_lcr =
2416 { {"lcr", "show", "lcr", NULL},
2418 "Shows current states of LCR core",
2419 "Usage: lcr show lcr\n",
2422 static struct ast_cli_entry cli_show_calls =
2423 { {"lcr", "show", "calls", NULL},
2425 "Shows current calls made by LCR and Asterisk",
2426 "Usage: lcr show calls\n",
2429 static struct ast_cli_entry cli_reload_routing =
2430 { {"lcr", "reload", "routing", NULL},
2432 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2433 "Usage: lcr reload routing\n",
2436 static struct ast_cli_entry cli_reload_interfaces =
2437 { {"lcr", "reload", "interfaces", NULL},
2438 lcr_reload_interfaces,
2439 "Reloads interfaces conf of LCR",
2440 "Usage: lcr reload interfaces\n",
2443 static struct ast_cli_entry cli_port_block =
2444 { {"lcr", "port", "block", NULL},
2446 "Blocks LCR port for further calls",
2447 "Usage: lcr port block \"<port>\"\n",
2450 static struct ast_cli_entry cli_port_unblock =
2451 { {"lcr", "port", "unblock", NULL},
2453 "Unblocks or loads LCR port, port is opened my mISDN",
2454 "Usage: lcr port unblock \"<port>\"\n",
2457 static struct ast_cli_entry cli_port_unload =
2458 { {"lcr", "port", "unload", NULL},
2460 "Unloads LCR port, port is closes by mISDN",
2461 "Usage: lcr port unload \"<port>\"\n",
2467 static int lcr_config_exec(struct ast_channel *ast, void *data)
2469 struct chan_call *call;
2471 ast_mutex_lock(&chan_lock);
2472 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2476 if (call->ast == ast)
2481 apply_opt(call, (char *)data);
2483 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2485 ast_mutex_unlock(&chan_lock);
2490 * module loading and destruction
2492 int load_module(void)
2496 for (i = 0; i < 256; i++) {
2497 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2498 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2501 if (read_options() == 0) {
2502 CERROR(NULL, NULL, "%s", options_error);
2503 return AST_MODULE_LOAD_DECLINE;
2506 ast_mutex_init(&chan_lock);
2507 ast_mutex_init(&log_lock);
2509 if (open_socket() < 0) {
2510 /* continue with closed socket */
2513 if (bchannel_initialize()) {
2514 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2516 return AST_MODULE_LOAD_DECLINE;
2520 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2521 if (ast_channel_register(&lcr_tech)) {
2522 CERROR(NULL, NULL, "Unable to register channel class\n");
2523 bchannel_deinitialize();
2525 return AST_MODULE_LOAD_DECLINE;
2528 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2529 "lcr_config(<opt><optarg>:<opt>:...)\n"
2530 "Sets LCR opts. and optargs\n"
2532 "The available options are:\n"
2533 " d - Send display text on called phone, text is the optarg.\n"
2534 " n - Don't detect dtmf tones on called channel.\n"
2535 " h - Force data call (HDLC).\n"
2536 " t - Disable mISDN_dsp features (required for fax application).\n"
2537 " c - Make crypted outgoing call, optarg is keyindex.\n"
2538 " e - Perform echo cancelation on this channel.\n"
2539 " Takes mISDN pipeline option as optarg.\n"
2540 " s - Send Non Inband DTMF as inband.\n"
2541 " vr - rxgain control\n"
2542 " vt - txgain control\n"
2543 " Volume changes at factor 2 ^ optarg.\n"
2548 ast_cli_register(&cli_show_lcr);
2549 ast_cli_register(&cli_show_calls);
2550 ast_cli_register(&cli_reload_routing);
2551 ast_cli_register(&cli_reload_interfaces);
2552 ast_cli_register(&cli_port_block);
2553 ast_cli_register(&cli_port_unblock);
2554 ast_cli_register(&cli_port_unload);
2558 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2560 /* failed to create thread */
2561 bchannel_deinitialize();
2563 ast_channel_unregister(&lcr_tech);
2564 return AST_MODULE_LOAD_DECLINE;
2569 int unload_module(void)
2571 /* First, take us out of the channel loop */
2572 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2575 pthread_join(chan_tid, NULL);
2577 ast_channel_unregister(&lcr_tech);
2579 ast_unregister_application("lcr_config");
2582 if (mISDN_created) {
2583 bchannel_deinitialize();
2587 if (lcr_sock >= 0) {
2595 int reload_module(void)
2602 #define AST_MODULE "chan_lcr"
2604 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2605 .load = load_module,
2606 .unload = unload_module,
2607 .reload = reload_module,