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_ALLOWED:
547 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
549 case AST_PRES_RESTRICTED:
550 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
552 case AST_PRES_UNAVAILABLE:
553 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
556 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
558 switch(ast->cid.cid_ton)
561 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
564 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
567 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
570 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
572 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
573 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
575 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
577 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
578 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
580 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
581 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
582 send_message(MESSAGE_SETUP, call->ref, &newparam);
584 /* change to outgoing setup state */
585 call->state = CHAN_LCR_STATE_OUT_SETUP;
589 * send dialing info to LCR
590 * this function is called, when setup acknowledge is received and dialing
593 static void send_dialque_to_lcr(struct chan_call *call)
595 union parameter newparam;
597 if (!call->ast || !call->ref || !call->dialque[0])
600 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
602 /* send setup message to LCR */
603 memset(&newparam, 0, sizeof(union parameter));
604 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
605 call->dialque[0] = '\0';
606 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
610 * in case of a bridge, the unsupported message can be forwarded directly
611 * to the remote call.
613 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
617 if (!call->bridge_call) return;
618 CDEBUG(call, NULL, "Sending message due bridging.\n");
619 send_message(message_type, call->bridge_call->ref, param);
623 * send release message to LCR and import bchannel if exported
625 static void send_release_and_import(struct chan_call *call, int cause, int location)
627 union parameter newparam;
629 /* importing channel */
630 if (call->bchannel) {
631 memset(&newparam, 0, sizeof(union parameter));
632 newparam.bchannel.type = BCHANNEL_RELEASE;
633 newparam.bchannel.handle = call->bchannel->handle;
634 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
636 /* sending release */
637 memset(&newparam, 0, sizeof(union parameter));
638 newparam.disconnectinfo.cause = cause;
639 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
640 send_message(MESSAGE_RELEASE, call->ref, &newparam);
644 * check if extension matches and start asterisk
645 * if it can match, proceed
648 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
651 union parameter newparam;
652 char *exten = ast->exten;
656 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
661 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad))
663 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
667 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad))
669 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);
673 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
674 /* send setup acknowledge to lcr */
675 memset(&newparam, 0, sizeof(union parameter));
676 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
679 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
684 if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad))
686 /* send setup acknowledge to lcr */
687 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
688 memset(&newparam, 0, sizeof(union parameter));
689 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
693 call->state = CHAN_LCR_STATE_IN_DIALING;
695 /* if match, start pbx */
696 if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
697 CDEBUG(call, ast, "Extensions matches.\n");
702 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
710 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
711 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
713 /* release asterisk */
714 ast->hangupcause = call->cause;
715 /* change to release state */
716 call->state = CHAN_LCR_STATE_RELEASE;
717 ast_hangup(ast); // call will be destroyed here
721 /* send setup to asterisk */
722 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
723 ret = ast_pbx_start(ast);
726 cause = (ret==-2)?34:27;
729 call->pbx_started = 1;
730 ast_setstate(ast, AST_STATE_RING);
734 * incoming setup from LCR
736 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
738 struct ast_channel *ast;
740 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
742 /* create asterisk channel instrance */
743 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
747 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
748 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
755 ast->tech_pvt = call;
756 ast->tech = &lcr_tech;
757 ast->fds[0] = call->pipe[0];
759 /* fill setup information */
760 if (param->setup.dialinginfo.id)
761 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
762 if (param->setup.context[0])
763 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
765 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
766 if (param->setup.callerinfo.id[0])
767 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
768 if (param->setup.callerinfo.name[0])
769 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
770 if (param->setup.redirinfo.id[0])
771 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
772 switch (param->setup.callerinfo.present)
774 case INFO_PRESENT_ALLOWED:
775 ast->cid.cid_pres = AST_PRES_ALLOWED;
777 case INFO_PRESENT_RESTRICTED:
778 ast->cid.cid_pres = AST_PRES_RESTRICTED;
781 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
783 switch (param->setup.callerinfo.ntype)
785 case INFO_NTYPE_SUBSCRIBER:
786 ast->cid.cid_ton = 4;
788 case INFO_NTYPE_NATIONAL:
789 ast->cid.cid_ton = 2;
791 case INFO_NTYPE_INTERNATIONAL:
792 ast->cid.cid_ton = 1;
795 ast->cid.cid_ton = 0;
797 ast->transfercapability = param->setup.capainfo.bearer_capa;
798 /* enable hdlc if transcap is data */
799 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
801 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
803 /* configure channel */
804 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
805 ast->readformat = ast->rawreadformat = ast->nativeformats;
806 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
808 ast->hangupcause = 0;
811 call->state = CHAN_LCR_STATE_IN_SETUP;
813 if (!call->pbx_started)
814 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
818 * incoming setup acknowledge from LCR
820 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
822 if (!call->ast) return;
824 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
826 /* send pending digits in dialque */
827 if (call->dialque[0])
828 send_dialque_to_lcr(call);
829 /* change to overlap state */
830 call->state = CHAN_LCR_STATE_OUT_DIALING;
834 * incoming proceeding from LCR
836 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
838 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
841 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
842 /* queue event for asterisk */
843 if (call->ast && call->pbx_started)
844 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
848 * incoming alerting from LCR
850 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
852 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
855 call->state = CHAN_LCR_STATE_OUT_ALERTING;
856 /* queue event to asterisk */
857 if (call->ast && call->pbx_started)
858 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
862 * incoming connect from LCR
864 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
866 union parameter newparam;
868 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
871 call->state = CHAN_LCR_STATE_CONNECT;
872 /* request bchannel */
873 if (!call->bchannel) {
874 CDEBUG(call, call->ast, "Requesting B-channel.\n");
875 memset(&newparam, 0, sizeof(union parameter));
876 newparam.bchannel.type = BCHANNEL_REQUEST;
877 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
879 /* copy connectinfo */
880 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
881 /* queue event to asterisk */
882 if (call->ast && call->pbx_started)
883 strncat(call->queue_string, "A", sizeof(call->queue_string)-1);
887 * incoming disconnect from LCR
889 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
891 struct ast_channel *ast = call->ast;
893 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
896 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
898 call->cause = param->disconnectinfo.cause;
899 call->location = param->disconnectinfo.location;
900 /* if bridge, forward disconnect and return */
903 if (call->bridge_call)
905 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
906 bridge_message_if_bridged(call, message_type, param);
910 /* release lcr with same cause */
911 send_release_and_import(call, call->cause, call->location);
913 /* change to release state */
914 call->state = CHAN_LCR_STATE_RELEASE;
915 /* queue release asterisk */
918 ast->hangupcause = call->cause;
919 if (call->pbx_started)
920 strcpy(call->queue_string, "H"); // overwrite other indications
922 ast_hangup(ast); // call will be destroyed here
928 * incoming release from LCR
930 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
932 struct ast_channel *ast = call->ast;
934 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
938 /* change to release state */
939 call->state = CHAN_LCR_STATE_RELEASE;
940 /* copy release info */
943 call->cause = param->disconnectinfo.cause;
944 call->location = param->disconnectinfo.location;
946 /* if we have an asterisk instance, queue hangup, else we are done */
949 ast->hangupcause = call->cause;
950 if (call->pbx_started)
951 strcpy(call->queue_string, "H");
953 ast_hangup(ast); // call will be destroyed here
963 * incoming information from LCR
965 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
967 struct ast_channel *ast = call->ast;
969 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
973 /* pbx not started */
974 if (!call->pbx_started)
976 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
977 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
978 lcr_start_pbx(call, ast, param->information.sending_complete);
982 /* change dailing state after setup */
983 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
984 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
985 call->state = CHAN_LCR_STATE_IN_DIALING;
986 // ast_setstate(ast, AST_STATE_DIALING);
990 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
991 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
993 /* use bridge to forware message not supported by asterisk */
994 if (call->state == CHAN_LCR_STATE_CONNECT) {
995 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
996 bridge_message_if_bridged(call, message_type, param);
1001 * incoming information from LCR
1003 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1005 union parameter newparam;
1007 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1009 /* request bchannel, if call is resumed and we don't have it */
1010 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1011 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1012 memset(&newparam, 0, sizeof(union parameter));
1013 newparam.bchannel.type = BCHANNEL_REQUEST;
1014 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1017 if (!call->ast) return;
1019 /* use bridge to forware message not supported by asterisk */
1020 bridge_message_if_bridged(call, message_type, param);
1024 * incoming information from LCR
1026 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1028 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1030 if (!call->ast) return;
1032 /* use bridge to forware message not supported by asterisk */
1033 bridge_message_if_bridged(call, message_type, param);
1037 * got dtmf from bchannel (locked state)
1039 void lcr_in_dtmf(struct chan_call *call, int val)
1041 struct ast_channel *ast = call->ast;
1046 if (!call->pbx_started)
1049 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1052 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1056 * message received from LCR
1058 int receive_message(int message_type, unsigned int ref, union parameter *param)
1060 struct bchannel *bchannel;
1061 struct chan_call *call;
1062 union parameter newparam;
1064 memset(&newparam, 0, sizeof(union parameter));
1066 /* handle bchannel message*/
1067 if (message_type == MESSAGE_BCHANNEL)
1069 switch(param->bchannel.type)
1071 case BCHANNEL_ASSIGN:
1072 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1073 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1075 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1078 /* create bchannel */
1079 bchannel = alloc_bchannel(param->bchannel.handle);
1082 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1086 /* configure channel */
1087 bchannel->b_tx_gain = param->bchannel.tx_gain;
1088 bchannel->b_rx_gain = param->bchannel.rx_gain;
1089 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1090 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1092 bchannel->b_bf_len = param->bchannel.crypt_len;
1093 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1095 bchannel->b_txdata = 0;
1096 bchannel->b_dtmf = 1;
1097 bchannel->b_tx_dejitter = 1;
1099 /* in case, ref is not set, this bchannel instance must
1100 * be created until it is removed again by LCR */
1102 call = find_call_ref(ref);
1105 bchannel->call = call;
1106 call->bchannel = bchannel;
1108 bchannel_dtmf(bchannel, 1);
1110 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1111 if (call->pipeline[0])
1112 bchannel_pipeline(bchannel, call->pipeline);
1114 bchannel_gain(bchannel, call->rx_gain, 0);
1116 bchannel_gain(bchannel, call->tx_gain, 1);
1117 if (call->bridge_id) {
1118 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1119 bchannel_join(bchannel, call->bridge_id);
1121 /* create only, if call exists, othewhise it bchannel is freed below... */
1122 if (bchannel_create(bchannel, ((call->nodsp)?1:0) + ((call->hdlc)?2:0)))
1123 bchannel_activate(bchannel, 1);
1126 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1127 newparam.bchannel.handle = param->bchannel.handle;
1128 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1129 /* if call has released before bchannel is assigned */
1131 newparam.bchannel.type = BCHANNEL_RELEASE;
1132 newparam.bchannel.handle = param->bchannel.handle;
1133 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1138 case BCHANNEL_REMOVE:
1139 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1140 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1142 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1145 /* unklink from call and destroy bchannel */
1146 free_bchannel(bchannel);
1149 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1150 newparam.bchannel.handle = param->bchannel.handle;
1151 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1156 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1161 /* handle new ref */
1162 if (message_type == MESSAGE_NEWREF)
1164 if (param->direction)
1166 /* new ref from lcr */
1167 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1168 if (!ref || find_call_ref(ref))
1170 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1173 /* allocate new call instance */
1174 call = alloc_call();
1176 call->state = CHAN_LCR_STATE_IN_PREPARE;
1179 call->ref_was_assigned = 1;
1180 /* wait for setup (or release from asterisk) */
1183 /* new ref, as requested from this remote application */
1184 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1185 call = find_call_ref(0);
1188 /* send release, if ref does not exist */
1189 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1190 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1195 call->ref_was_assigned = 1;
1196 /* send pending setup info */
1197 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1198 send_setup_to_lcr(call);
1199 /* release if asterisk has signed off */
1200 else if (call->state == CHAN_LCR_STATE_RELEASE)
1204 send_release_and_import(call, call->cause, call->location);
1206 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1218 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1221 call = find_call_ref(ref);
1224 /* ignore ref that is not used (anymore) */
1225 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1229 /* handle messages */
1230 switch(message_type)
1233 lcr_in_setup(call, message_type, param);
1236 case MESSAGE_OVERLAP:
1237 lcr_in_overlap(call, message_type, param);
1240 case MESSAGE_PROCEEDING:
1241 lcr_in_proceeding(call, message_type, param);
1244 case MESSAGE_ALERTING:
1245 lcr_in_alerting(call, message_type, param);
1248 case MESSAGE_CONNECT:
1249 lcr_in_connect(call, message_type, param);
1252 case MESSAGE_DISCONNECT:
1253 lcr_in_disconnect(call, message_type, param);
1256 case MESSAGE_RELEASE:
1257 lcr_in_release(call, message_type, param);
1260 case MESSAGE_INFORMATION:
1261 lcr_in_information(call, message_type, param);
1264 case MESSAGE_NOTIFY:
1265 lcr_in_notify(call, message_type, param);
1268 case MESSAGE_FACILITY:
1269 lcr_in_facility(call, message_type, param);
1272 case MESSAGE_PATTERN: // audio available from LCR
1275 case MESSAGE_NOPATTERN: // audio not available from LCR
1278 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1279 call->audiopath = param->audiopath;
1283 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1290 * release all calls (due to broken socket)
1292 static void release_all_calls(void)
1294 struct chan_call *call;
1299 /* no ast, so we may directly free call */
1301 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1305 /* already in release process */
1306 if (call->state == CHAN_LCR_STATE_RELEASE) {
1310 /* release or queue release */
1312 call->state = CHAN_LCR_STATE_RELEASE;
1313 if (!call->pbx_started) {
1314 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1315 ast_hangup(call->ast); // call will be destroyed here
1318 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1319 strcpy(call->queue_string, "H");
1323 /* release all bchannels */
1324 while(bchannel_first)
1325 free_bchannel(bchannel_first);
1330 * warning! not thread safe
1331 * returns -1 for socket error, 0 for no work, 1 for work
1333 int handle_socket(void)
1337 struct admin_list *admin;
1338 struct admin_message msg;
1340 /* read from socket */
1341 len = read(lcr_sock, &msg, sizeof(msg));
1344 CERROR(NULL, NULL, "Socket closed.\n");
1345 return(-1); // socket closed
1349 if (len != sizeof(msg))
1351 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1352 return(-1); // socket error
1354 if (msg.message != ADMIN_MESSAGE)
1356 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1359 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1363 if (errno != EWOULDBLOCK)
1365 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1370 /* write to socket */
1373 admin = admin_first;
1374 len = write(lcr_sock, &admin->msg, sizeof(msg));
1377 CERROR(NULL, NULL, "Socket closed.\n");
1378 return(-1); // socket closed
1382 if (len != sizeof(msg))
1384 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1385 return(-1); // socket error
1388 admin_first = admin->next;
1394 if (errno != EWOULDBLOCK)
1396 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1405 * open and close socket and thread
1407 int open_socket(void)
1411 struct sockaddr_un sock_address;
1412 unsigned int on = 1;
1413 union parameter param;
1416 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1418 CERROR(NULL, NULL, "Failed to create socket.\n");
1422 /* set socket address and name */
1423 memset(&sock_address, 0, sizeof(sock_address));
1424 sock_address.sun_family = PF_UNIX;
1425 sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1427 /* connect socket */
1428 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1432 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1436 /* set non-blocking io */
1437 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1441 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1445 /* enque hello message */
1446 memset(¶m, 0, sizeof(param));
1447 strcpy(param.hello.application, "asterisk");
1448 send_message(MESSAGE_HELLO, 0, ¶m);
1453 void close_socket(void)
1455 struct admin_list *admin, *temp;
1457 /* flush pending messages */
1458 admin = admin_first;
1461 admin = admin->next;
1473 /* sending queue to asterisk */
1474 static int queue_send(void)
1477 struct chan_call *call;
1478 struct ast_channel *ast;
1479 struct ast_frame fr;
1484 p = call->queue_string;
1487 /* there is something to queue */
1488 if (!ast_channel_trylock(ast)) { /* succeed */
1492 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1493 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1496 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1497 ast_queue_control(ast, AST_CONTROL_RINGING);
1500 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1501 ast_queue_control(ast, AST_CONTROL_ANSWER);
1504 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1505 ast_queue_hangup(ast);
1507 case '1': case '2': case '3': case 'a':
1508 case '4': case '5': case '6': case 'b':
1509 case '7': case '8': case '9': case 'c':
1510 case '*': case '0': case '#': case 'd':
1511 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1512 /* send digit to asterisk */
1513 memset(&fr, 0, sizeof(fr));
1514 fr.frametype = AST_FRAME_DTMF_BEGIN;
1516 fr.delivery = ast_tv(0, 0);
1517 ast_queue_frame(ast, &fr);
1518 fr.frametype = AST_FRAME_DTMF_END;
1519 ast_queue_frame(ast, &fr);
1522 CDEBUG(call, ast, "Ignoring queued digit 0x%02d.\n", *p);
1526 call->queue_string[0] = '\0';
1527 ast_channel_unlock(ast);
1537 /* signal handler */
1538 void sighandler(int sigset)
1542 /* chan_lcr thread */
1543 static void *chan_thread(void *arg)
1547 union parameter param;
1548 time_t retry = 0, now;
1550 bchannel_pid = getpid();
1552 // signal(SIGPIPE, sighandler);
1554 memset(¶m, 0, sizeof(union parameter));
1558 ast_mutex_lock(&chan_lock);
1565 ret = handle_socket();
1567 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1569 release_all_calls();
1576 if (retry && now-retry > 5) {
1577 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1579 if (open_socket() < 0) {
1588 ret = bchannel_handle();
1592 /* handle messages to asterisk */
1597 /* delay if no work done */
1599 ast_mutex_unlock(&chan_lock);
1601 ast_mutex_lock(&chan_lock);
1607 CERROR(NULL, NULL, "Thread exit.\n");
1609 ast_mutex_unlock(&chan_lock);
1611 // signal(SIGPIPE, SIG_DFL);
1617 * new asterisk instance
1620 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1622 char exten[256], *dial, *interface, *opt;
1623 struct ast_channel *ast;
1624 struct chan_call *call;
1626 ast_mutex_lock(&chan_lock);
1627 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1629 /* if socket is closed */
1632 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1633 ast_mutex_unlock(&chan_lock);
1637 /* create call instance */
1638 call = alloc_call();
1641 /* failed to create instance */
1642 ast_mutex_unlock(&chan_lock);
1646 /* create asterisk channel instrance */
1647 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1650 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1652 /* failed to create instance */
1653 ast_mutex_unlock(&chan_lock);
1656 ast->tech = &lcr_tech;
1657 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1658 /* configure channel */
1659 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1660 ast->readformat = ast->rawreadformat = ast->nativeformats;
1661 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1663 ast->hangupcause = 0;
1667 ast->tech_pvt = call;
1668 ast->fds[0] = call->pipe[0];
1669 call->pbx_started = 0;
1671 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1674 * Extract interface, dialstring, options from data.
1677 * <interface>/<dialstring>
1678 * <interface>/<dialstring>/options
1680 strncpy(exten, (char *)data, sizeof(exten)-1);
1681 exten[sizeof(exten)-1] = '\0';
1682 if ((dial = strchr(exten, '/'))) {
1685 if ((opt = strchr(dial, '/')))
1694 strncpy(call->interface, interface, sizeof(call->interface)-1);
1695 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1696 apply_opt(call, (char *)opt);
1698 ast_mutex_unlock(&chan_lock);
1703 * call from asterisk
1705 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1707 union parameter newparam;
1708 struct chan_call *call;
1710 ast_mutex_lock(&chan_lock);
1711 call = ast->tech_pvt;
1713 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1714 ast_mutex_unlock(&chan_lock);
1718 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1720 /* pbx process is started */
1721 call->pbx_started = 1;
1722 /* send MESSAGE_NEWREF */
1723 memset(&newparam, 0, sizeof(union parameter));
1724 newparam.direction = 0; /* request from app */
1725 send_message(MESSAGE_NEWREF, 0, &newparam);
1727 /* set hdlc if capability requires hdlc */
1728 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1729 || ast->transfercapability == INFO_BC_DATARESTRICTED
1730 || ast->transfercapability == INFO_BC_VIDEO)
1732 /* if hdlc is forced by option, we change transcap to data */
1734 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1735 && ast->transfercapability != INFO_BC_DATARESTRICTED
1736 && ast->transfercapability != INFO_BC_VIDEO)
1737 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1739 call->cid_num[0] = 0;
1740 call->cid_name[0] = 0;
1741 call->cid_rdnis[0] = 0;
1743 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1744 strncpy(call->cid_num, ast->cid.cid_num,
1745 sizeof(call->cid_num)-1);
1747 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1748 strncpy(call->cid_name, ast->cid.cid_name,
1749 sizeof(call->cid_name)-1);
1750 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1751 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1752 sizeof(call->cid_rdnis)-1);
1754 ast_mutex_unlock(&chan_lock);
1758 static void send_digit_to_chan(struct ast_channel * ast, char digit )
1760 static const char* dtmf_tones[] = {
1761 "!941+1336/100,!0/100", /* 0 */
1762 "!697+1209/100,!0/100", /* 1 */
1763 "!697+1336/100,!0/100", /* 2 */
1764 "!697+1477/100,!0/100", /* 3 */
1765 "!770+1209/100,!0/100", /* 4 */
1766 "!770+1336/100,!0/100", /* 5 */
1767 "!770+1477/100,!0/100", /* 6 */
1768 "!852+1209/100,!0/100", /* 7 */
1769 "!852+1336/100,!0/100", /* 8 */
1770 "!852+1477/100,!0/100", /* 9 */
1771 "!697+1633/100,!0/100", /* A */
1772 "!770+1633/100,!0/100", /* B */
1773 "!852+1633/100,!0/100", /* C */
1774 "!941+1633/100,!0/100", /* D */
1775 "!941+1209/100,!0/100", /* * */
1776 "!941+1477/100,!0/100" }; /* # */
1778 if (digit >= '0' && digit <='9')
1779 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
1780 else if (digit >= 'A' && digit <= 'D')
1781 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
1782 else if (digit == '*')
1783 ast_playtones_start(ast,0,dtmf_tones[14], 0);
1784 else if (digit == '#')
1785 ast_playtones_start(ast,0,dtmf_tones[15], 0);
1788 ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
1789 "'%c' for '%s'\n", digit, ast->name);
1794 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1796 struct chan_call *call;
1797 union parameter newparam;
1800 /* only pass IA5 number space */
1801 if (digit > 126 || digit < 32)
1804 ast_mutex_lock(&chan_lock);
1805 call = ast->tech_pvt;
1807 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1808 ast_mutex_unlock(&chan_lock);
1812 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1814 /* send information or queue them */
1815 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1817 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1818 memset(&newparam, 0, sizeof(union parameter));
1819 newparam.information.id[0] = digit;
1820 newparam.information.id[1] = '\0';
1821 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1824 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1826 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1828 strncat(call->dialque, buf, strlen(call->dialque)-1);
1831 ast_mutex_unlock(&chan_lock);
1835 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1837 int inband_dtmf = 0;
1838 struct chan_call *call;
1840 ast_mutex_lock(&chan_lock);
1842 call = ast->tech_pvt;
1846 "Received digit from Asterisk, "
1847 "but no call instance exists.\n");
1848 ast_mutex_unlock(&chan_lock);
1852 CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
1854 if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
1858 ast_mutex_unlock(&chan_lock);
1861 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
1862 send_digit_to_chan(ast, digit);
1868 static int lcr_answer(struct ast_channel *ast)
1870 union parameter newparam;
1871 struct chan_call *call;
1873 ast_mutex_lock(&chan_lock);
1874 call = ast->tech_pvt;
1876 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1877 ast_mutex_unlock(&chan_lock);
1881 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
1883 /* copy connectinfo, if bridged */
1884 if (call->bridge_call)
1885 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1886 /* send connect message to lcr */
1887 if (call->state != CHAN_LCR_STATE_CONNECT) {
1888 memset(&newparam, 0, sizeof(union parameter));
1889 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1890 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1891 call->state = CHAN_LCR_STATE_CONNECT;
1894 /* request bchannel */
1895 if (!call->bchannel) {
1896 CDEBUG(call, ast, "Requesting B-channel.\n");
1897 memset(&newparam, 0, sizeof(union parameter));
1898 newparam.bchannel.type = BCHANNEL_REQUEST;
1899 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1902 // memset(&newparam, 0, sizeof(union parameter));
1903 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1906 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1910 ast_mutex_unlock(&chan_lock);
1914 static int lcr_hangup(struct ast_channel *ast)
1916 struct chan_call *call;
1917 pthread_t tid = pthread_self();
1919 if (!pthread_equal(tid, chan_tid))
1920 ast_mutex_lock(&chan_lock);
1921 call = ast->tech_pvt;
1923 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1924 if (!pthread_equal(tid, chan_tid))
1925 ast_mutex_unlock(&chan_lock);
1929 if (!pthread_equal(tid, chan_tid))
1930 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1932 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1934 /* disconnect asterisk, maybe not required */
1935 ast->tech_pvt = NULL;
1940 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1941 if (ast->hangupcause > 0)
1942 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1944 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1947 if (!pthread_equal(tid, chan_tid))
1948 ast_mutex_unlock(&chan_lock);
1952 /* ref is not set, due to prepare setup or release */
1953 if (call->state == CHAN_LCR_STATE_RELEASE)
1955 /* we get the response to our release */
1956 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1960 /* during prepare, we change to release state */
1961 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1962 call->state = CHAN_LCR_STATE_RELEASE;
1966 if (!pthread_equal(tid, chan_tid))
1967 ast_mutex_unlock(&chan_lock);
1971 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1973 struct chan_call *call;
1976 CDEBUG(NULL, ast, "No subclass\n");
1977 if (!(f->subclass & ast->nativeformats))
1978 CDEBUG(NULL, ast, "Unexpected format.\n");
1980 ast_mutex_lock(&chan_lock);
1981 call = ast->tech_pvt;
1983 ast_mutex_unlock(&chan_lock);
1986 if (call->bchannel && f->samples)
1987 bchannel_transmit(call->bchannel, f->data, f->samples);
1988 ast_mutex_unlock(&chan_lock);
1993 static struct ast_frame *lcr_read(struct ast_channel *ast)
1995 struct chan_call *call;
1998 ast_mutex_lock(&chan_lock);
1999 call = ast->tech_pvt;
2001 ast_mutex_unlock(&chan_lock);
2004 if (call->pipe[0] > -1) {
2005 if (call->rebuffer && !call->hdlc) {
2006 len = read(call->pipe[0], call->read_buff, 160);
2008 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2010 if (len < 0 && errno == EAGAIN) {
2011 ast_mutex_unlock(&chan_lock);
2012 return &ast_null_frame;
2015 close(call->pipe[0]);
2017 ast_mutex_unlock(&chan_lock);
2022 call->read_fr.frametype = AST_FRAME_VOICE;
2023 call->read_fr.subclass = ast->nativeformats;
2024 call->read_fr.datalen = len;
2025 call->read_fr.samples = len;
2026 call->read_fr.delivery = ast_tv(0,0);
2027 call->read_fr.data = call->read_buff;
2028 ast_mutex_unlock(&chan_lock);
2030 return &call->read_fr;
2033 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2035 union parameter newparam;
2037 struct chan_call *call;
2039 ast_mutex_lock(&chan_lock);
2040 call = ast->tech_pvt;
2042 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2043 ast_mutex_unlock(&chan_lock);
2048 case AST_CONTROL_BUSY:
2049 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2050 ast_setstate(ast, AST_STATE_BUSY);
2051 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2052 /* send message to lcr */
2053 memset(&newparam, 0, sizeof(union parameter));
2054 newparam.disconnectinfo.cause = 17;
2055 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2056 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2058 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2061 case AST_CONTROL_CONGESTION:
2062 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2063 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2064 /* send message to lcr */
2065 memset(&newparam, 0, sizeof(union parameter));
2066 newparam.disconnectinfo.cause = ast->hangupcause;
2067 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2068 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2070 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2073 case AST_CONTROL_PROCEEDING:
2074 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2075 if (call->state == CHAN_LCR_STATE_IN_SETUP
2076 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2077 /* send message to lcr */
2078 memset(&newparam, 0, sizeof(union parameter));
2079 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2081 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2084 case AST_CONTROL_RINGING:
2085 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2086 ast_setstate(ast, AST_STATE_RINGING);
2087 if (call->state == CHAN_LCR_STATE_IN_SETUP
2088 || call->state == CHAN_LCR_STATE_IN_DIALING
2089 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2090 /* send message to lcr */
2091 memset(&newparam, 0, sizeof(union parameter));
2092 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2094 call->state = CHAN_LCR_STATE_IN_ALERTING;
2098 CDEBUG(call, ast, "Received indicate -1.\n");
2102 case AST_CONTROL_VIDUPDATE:
2103 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2106 case AST_CONTROL_HOLD:
2107 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2108 /* send message to lcr */
2109 memset(&newparam, 0, sizeof(union parameter));
2110 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2111 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2113 /*start music onhold*/
2114 ast_moh_start(ast,data,ast->musicclass);
2117 case AST_CONTROL_UNHOLD:
2118 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2119 /* send message to lcr */
2120 memset(&newparam, 0, sizeof(union parameter));
2121 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2122 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2128 #if ASTERISK_VERSION_NUM >= 10600
2129 case AST_CONTROL_SRCUPDATE:
2130 CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
2134 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2140 ast_mutex_unlock(&chan_lock);
2147 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2149 struct chan_call *call;
2155 ast_mutex_lock(&chan_lock);
2156 call = ast->tech_pvt;
2158 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2159 ast_mutex_unlock(&chan_lock);
2163 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2165 ast_mutex_unlock(&chan_lock);
2170 * send_text asterisk
2172 static int lcr_send_text(struct ast_channel *ast, const char *text)
2174 struct chan_call *call;
2175 union parameter newparam;
2177 ast_mutex_lock(&chan_lock);
2178 call = ast->tech_pvt;
2180 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2181 ast_mutex_unlock(&chan_lock);
2185 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2186 memset(&newparam, 0, sizeof(union parameter));
2187 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2188 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2189 ast_mutex_lock(&chan_lock);
2196 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2197 struct ast_channel *ast2, int flags,
2198 struct ast_frame **fo,
2199 struct ast_channel **rc, int timeoutms)
2202 struct chan_call *call1, *call2;
2203 struct ast_channel *carr[2], *who;
2205 struct ast_frame *f;
2208 CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
2213 /* join via dsp (if the channels are currently open) */
2214 ast_mutex_lock(&chan_lock);
2215 call1 = ast1->tech_pvt;
2216 call2 = ast2->tech_pvt;
2217 if (!call1 || !call2) {
2218 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2219 ast_mutex_unlock(&chan_lock);
2220 return AST_BRIDGE_COMPLETE;
2223 /* join, if both call instances uses dsp */
2224 if (!call1->nodsp && !call2->nodsp) {
2225 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
2227 /* get bridge id and join */
2228 bridge_id = new_bridge_id();
2230 call1->bridge_id = bridge_id;
2231 if (call1->bchannel)
2232 bchannel_join(call1->bchannel, bridge_id);
2234 call2->bridge_id = bridge_id;
2235 if (call2->bchannel)
2236 bchannel_join(call2->bchannel, bridge_id);
2238 if (call1->nodsp && call2->nodsp)
2239 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
2241 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
2242 call1->bridge_call = call2;
2243 call2->bridge_call = call1;
2245 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2246 || call1->state == CHAN_LCR_STATE_IN_DIALING
2247 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2248 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2249 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2252 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2253 || call2->state == CHAN_LCR_STATE_IN_DIALING
2254 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2255 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2256 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2260 /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
2261 so let's do it for them. Hmpf.
2264 if (call1->on_hold) {
2265 union parameter newparam;
2267 memset(&newparam, 0, sizeof(union parameter));
2268 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2269 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
2274 if (call2->on_hold) {
2275 union parameter newparam;
2277 memset(&newparam, 0, sizeof(union parameter));
2278 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2279 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
2284 ast_mutex_unlock(&chan_lock);
2288 who = ast_waitfor_n(carr, 2, &to);
2291 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2296 if (!f || f->frametype == AST_FRAME_CONTROL) {
2298 CDEBUG(NULL, NULL, "Got hangup.\n");
2300 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2307 if ( f->frametype == AST_FRAME_DTMF ) {
2308 CDEBUG(NULL, NULL, "Got DTMF.\n");
2324 CDEBUG(NULL, NULL, "Releasing bridge.\n");
2326 /* split channels */
2327 ast_mutex_lock(&chan_lock);
2328 call1 = ast1->tech_pvt;
2329 call2 = ast2->tech_pvt;
2330 if (call1 && call1->bridge_id)
2332 call1->bridge_id = 0;
2333 if (call1->bchannel)
2334 bchannel_join(call1->bchannel, 0);
2335 if (call1->bridge_call)
2336 call1->bridge_call->bridge_call = NULL;
2338 if (call2 && call1->bridge_id)
2340 call2->bridge_id = 0;
2341 if (call2->bchannel)
2342 bchannel_join(call2->bchannel, 0);
2343 if (call2->bridge_call)
2344 call2->bridge_call->bridge_call = NULL;
2346 call1->bridge_call = NULL;
2347 call2->bridge_call = NULL;
2349 ast_mutex_unlock(&chan_lock);
2350 return AST_BRIDGE_COMPLETE;
2352 static struct ast_channel_tech lcr_tech = {
2354 .description="Channel driver for connecting to Linux-Call-Router",
2355 .requester=lcr_request,
2356 .send_digit_begin=lcr_digit_begin,
2357 .send_digit_end=lcr_digit_end,
2364 .indicate=lcr_indicate,
2366 .send_text=lcr_send_text,
2375 static int lcr_show_lcr (int fd, int argc, char *argv[])
2380 static int lcr_show_calls (int fd, int argc, char *argv[])
2385 static int lcr_reload_routing (int fd, int argc, char *argv[])
2390 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2395 static int lcr_port_block (int fd, int argc, char *argv[])
2400 static int lcr_port_unblock (int fd, int argc, char *argv[])
2405 static int lcr_port_unload (int fd, int argc, char *argv[])
2410 static struct ast_cli_entry cli_show_lcr =
2411 { {"lcr", "show", "lcr", NULL},
2413 "Shows current states of LCR core",
2414 "Usage: lcr show lcr\n",
2417 static struct ast_cli_entry cli_show_calls =
2418 { {"lcr", "show", "calls", NULL},
2420 "Shows current calls made by LCR and Asterisk",
2421 "Usage: lcr show calls\n",
2424 static struct ast_cli_entry cli_reload_routing =
2425 { {"lcr", "reload", "routing", NULL},
2427 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2428 "Usage: lcr reload routing\n",
2431 static struct ast_cli_entry cli_reload_interfaces =
2432 { {"lcr", "reload", "interfaces", NULL},
2433 lcr_reload_interfaces,
2434 "Reloads interfaces conf of LCR",
2435 "Usage: lcr reload interfaces\n",
2438 static struct ast_cli_entry cli_port_block =
2439 { {"lcr", "port", "block", NULL},
2441 "Blocks LCR port for further calls",
2442 "Usage: lcr port block \"<port>\"\n",
2445 static struct ast_cli_entry cli_port_unblock =
2446 { {"lcr", "port", "unblock", NULL},
2448 "Unblocks or loads LCR port, port is opened my mISDN",
2449 "Usage: lcr port unblock \"<port>\"\n",
2452 static struct ast_cli_entry cli_port_unload =
2453 { {"lcr", "port", "unload", NULL},
2455 "Unloads LCR port, port is closes by mISDN",
2456 "Usage: lcr port unload \"<port>\"\n",
2462 static int lcr_config_exec(struct ast_channel *ast, void *data)
2464 struct chan_call *call;
2466 ast_mutex_lock(&chan_lock);
2467 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2471 if (call->ast == ast)
2476 apply_opt(call, (char *)data);
2478 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2480 ast_mutex_unlock(&chan_lock);
2485 * module loading and destruction
2487 int load_module(void)
2491 for (i = 0; i < 256; i++) {
2492 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2493 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2496 if (read_options() == 0) {
2497 CERROR(NULL, NULL, "%s", options_error);
2498 return AST_MODULE_LOAD_DECLINE;
2501 ast_mutex_init(&chan_lock);
2502 ast_mutex_init(&log_lock);
2504 if (open_socket() < 0) {
2505 /* continue with closed socket */
2508 if (bchannel_initialize()) {
2509 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2511 return AST_MODULE_LOAD_DECLINE;
2515 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2516 if (ast_channel_register(&lcr_tech)) {
2517 CERROR(NULL, NULL, "Unable to register channel class\n");
2518 bchannel_deinitialize();
2520 return AST_MODULE_LOAD_DECLINE;
2523 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2524 "lcr_config(<opt><optarg>:<opt>:...)\n"
2525 "Sets LCR opts. and optargs\n"
2527 "The available options are:\n"
2528 " d - Send display text on called phone, text is the optarg.\n"
2529 " n - Don't detect dtmf tones on called channel.\n"
2530 " h - Force data call (HDLC).\n"
2531 " t - Disable mISDN_dsp features (required for fax application).\n"
2532 " c - Make crypted outgoing call, optarg is keyindex.\n"
2533 " e - Perform echo cancelation on this channel.\n"
2534 " Takes mISDN pipeline option as optarg.\n"
2535 " s - Send Non Inband DTMF as inband.\n"
2536 " vr - rxgain control\n"
2537 " vt - txgain control\n"
2538 " Volume changes at factor 2 ^ optarg.\n"
2543 ast_cli_register(&cli_show_lcr);
2544 ast_cli_register(&cli_show_calls);
2545 ast_cli_register(&cli_reload_routing);
2546 ast_cli_register(&cli_reload_interfaces);
2547 ast_cli_register(&cli_port_block);
2548 ast_cli_register(&cli_port_unblock);
2549 ast_cli_register(&cli_port_unload);
2553 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2555 /* failed to create thread */
2556 bchannel_deinitialize();
2558 ast_channel_unregister(&lcr_tech);
2559 return AST_MODULE_LOAD_DECLINE;
2564 int unload_module(void)
2566 /* First, take us out of the channel loop */
2567 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2570 pthread_join(chan_tid, NULL);
2572 ast_channel_unregister(&lcr_tech);
2574 ast_unregister_application("lcr_config");
2577 if (mISDN_created) {
2578 bchannel_deinitialize();
2582 if (lcr_sock >= 0) {
2590 int reload_module(void)
2597 #define AST_MODULE "chan_lcr"
2599 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2600 .load = load_module,
2601 .unload = unload_module,
2602 .reload = reload_module,