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 A new chan_call instance is created. The call reference (ref) is given by
25 MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
26 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
27 using ast_channel_alloc(1). The setup information is given to asterisk.
28 The new Asterisk instance pointer (ast) is stored to chan_call structure.
29 The state changes to CHAN_LCR_STATE_IN_SETUP.
32 Call is initiated by Asterisk:
34 If a call is reveiced from Asterisk, a new chan_call instance is created.
35 The new Asterisk instance pointer (ast) is stored to chan_call structure.
36 A MESSASGE_NEWREF is sent to LCR requesting a new call reference (ref).
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 Further dialing information is queued.
39 After the new callref is received by special MESSAGE_NEWREF reply, new ref
40 is stored in the chan_call structure.
41 The setup information is sent to LCR using MESSAGE_SETUP.
42 The state changes to CHAN_LCR_STATE_OUT_SETUP.
47 During call process, messages are received and sent.
48 The state changes accordingly.
49 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
50 If a MESSAGE_OVERLAP is received, further dialing is required.
51 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
52 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
55 Call is released by LCR:
57 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
58 The current ref is set to 0, to indicate released reference.
59 The state changes to CHAN_LCR_STATE_RELEASE.
60 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
61 the chan_call instance is destroyed.
62 After lcr_hangup() is called-back by Asterisk, the chan_call instance
63 is destroyed, because the current ref is set to 0 and the state equals
64 CHAN_LCR_STATE_RELEASE.
65 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
66 "Call is released by Asterisk".
69 Call is released by Asterisk:
71 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
72 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
73 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
74 set to CHAN_LCR_STATE_RELEASE.
75 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
76 LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
78 "Call is released by LCR".
87 #include <sys/types.h>
91 #include <sys/ioctl.h>
92 #include <sys/socket.h>
95 #include <semaphore.h>
97 #include <asterisk/module.h>
98 #include <asterisk/channel.h>
99 #include <asterisk/config.h>
100 #include <asterisk/logger.h>
101 #include <asterisk/pbx.h>
102 #include <asterisk/options.h>
103 #include <asterisk/io.h>
104 #include <asterisk/frame.h>
105 #include <asterisk/translate.h>
106 #include <asterisk/cli.h>
107 #include <asterisk/musiconhold.h>
108 #include <asterisk/dsp.h>
109 #include <asterisk/translate.h>
110 #include <asterisk/file.h>
111 #include <asterisk/callerid.h>
112 #include <asterisk/indications.h>
113 #include <asterisk/app.h>
114 #include <asterisk/features.h>
115 #include <asterisk/sched.h>
117 #include "extension.h"
119 #include "lcrsocket.h"
121 #include "bchannel.h"
122 #include "chan_lcr.h"
123 #include "callerid.h"
125 CHAN_LCR_STATE // state description structure
127 u_char flip_bits[256];
132 char lcr_type[]="lcr";
135 ast_mutex_t chan_lock; /* global lock */
136 ast_mutex_t log_lock; /* logging log */
139 int glob_channel = 0;
144 struct admin_list *next;
145 struct admin_msg msg;
146 } *admin_first = NULL;
148 static struct ast_channel_tech lcr_tech;
153 void chan_lcr_log(int type, const char *file, int line, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
156 char call_text[128] = "NULL";
157 char ast_text[128] = "NULL";
160 ast_mutex_lock(&log_lock);
163 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
164 buffer[sizeof(buffer)-1]=0;
168 sprintf(call_text, "%ld", call->ref);
170 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
171 ast_text[sizeof(ast_text)-1] = '\0';
173 ast_log(type, file, line, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
175 ast_mutex_unlock(&log_lock);
179 * channel and call instances
181 struct chan_call *call_first;
183 struct chan_call *find_call_ref(unsigned long ref)
185 struct chan_call *call = call_first;
189 if (call->ref == ref)
197 struct chan_call *find_call_ast(struct ast_channel *ast)
199 struct chan_call *call = call_first;
203 if (call->ast == ast)
210 struct chan_call *find_call_handle(unsigned long handle)
212 struct chan_call *call = call_first;
216 if (call->bchannel_handle == handle)
224 void free_call(struct chan_call *call)
226 struct chan_call **temp = &call_first;
232 *temp = (*temp)->next;
234 close(call->pipe[0]);
236 close(call->pipe[1]);
239 if (call->bchannel->call != call)
240 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
241 call->bchannel->call = NULL;
243 if (call->bridge_call)
245 if (call->bridge_call->bridge_call != call)
246 CERROR(call, NULL, "Linked call structure has no link to us.\n");
247 call->bridge_call->bridge_call = NULL;
249 CDEBUG(call, NULL, "Call instance freed.\n");
253 temp = &((*temp)->next);
255 CERROR(call, NULL, "Call instance not found in list.\n");
258 struct chan_call *alloc_call(void)
260 struct chan_call **callp = &call_first;
263 callp = &((*callp)->next);
265 *callp = (struct chan_call *)malloc(sizeof(struct chan_call));
267 memset(*callp, 0, sizeof(struct chan_call));
268 if (pipe((*callp)->pipe) < 0) {
269 CERROR(*callp, NULL, "Failed to create pipe.\n");
273 CDEBUG(*callp, NULL, "Call instance allocated.\n");
278 unsigned short new_bridge_id(void)
280 struct chan_call *call;
281 unsigned short id = 1;
283 /* search for lowest bridge id that is not in use and not 0 */
289 if (call->bridge_id == id)
297 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
303 * enque message to LCR
305 int send_message(int message_type, unsigned long ref, union parameter *param)
307 struct admin_list *admin, **adminp;
310 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
313 CDEBUG(NULL, NULL, "Sending message %d to socket.\n", message_type);
315 adminp = &admin_first;
317 adminp = &((*adminp)->next);
318 admin = (struct admin_list *)malloc(sizeof(struct admin_list));
321 admin->msg.type = message_type;
322 admin->msg.ref = ref;
323 memcpy(&admin->msg.param, param, sizeof(union parameter));
329 * send setup info to LCR
330 * this function is called, when asterisk call is received and ref is received
332 static void send_setup_to_lcr(struct chan_call *call)
334 union parameter newparam;
335 struct ast_channel *ast = call->ast;
337 if (!call->ast || !call->ref)
340 CDEBUG(call, call->ast, "Sending setup to LCR.\n");
342 /* send setup message to LCR */
343 memset(&newparam, 0, sizeof(union parameter));
344 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
345 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
346 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
347 strncpy(newparam.setup.callerinfo.id, ast->cid.cid_num, sizeof(newparam.setup.callerinfo.id)-1);
348 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
349 strncpy(newparam.setup.callerinfo.name, ast->cid.cid_name, sizeof(newparam.setup.callerinfo.name)-1);
350 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
352 strncpy(newparam.setup.redirinfo.id, ast->cid.cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
353 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
354 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
356 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
358 case AST_PRES_ALLOWED:
359 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
361 case AST_PRES_RESTRICTED:
362 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
364 case AST_PRES_UNAVAILABLE:
365 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
368 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
370 switch(ast->cid.cid_ton)
373 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
376 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
379 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
382 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
384 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
386 newparam.setup.capainfo.bearer_info1 = alaw 3, ulaw 2;
388 newparam.setup.capainfo.bearer_info1 = 3;
389 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
390 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
391 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
392 send_message(MESSAGE_SETUP, call->ref, &newparam);
394 /* change to outgoing setup state */
395 call->state = CHAN_LCR_STATE_OUT_SETUP;
399 * send dialing info to LCR
400 * this function is called, when setup acknowledge is received and dialing
403 static void send_dialque_to_lcr(struct chan_call *call)
405 union parameter newparam;
407 if (!call->ast || !call->ref || !call->dialque)
410 CDEBUG(call, call->ast, "Sending dial queue to LCR.\n");
412 /* send setup message to LCR */
413 memset(&newparam, 0, sizeof(union parameter));
414 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
415 call->dialque[0] = '\0';
416 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
420 * in case of a bridge, the unsupported message can be forwarded directly
421 * to the remote call.
423 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
427 if (!call->bridge_call) return;
428 CDEBUG(call, NULL, "Sending message due briding.\n");
429 send_message(message_type, call->bridge_call->ref, param);
433 * check if extension matches and start asterisk
434 * if it can match, proceed
437 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
440 union parameter newparam;
445 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
447 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
450 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
452 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", ast->exten, ast->context);
456 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
457 /* send setup acknowledge to lcr */
458 memset(&newparam, 0, sizeof(union parameter));
459 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
462 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
467 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
469 /* send setup acknowledge to lcr */
470 memset(&newparam, 0, sizeof(union parameter));
471 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
474 call->state = CHAN_LCR_STATE_IN_DIALING;
476 /* if match, start pbx */
477 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
478 CDEBUG(call, ast, "Extensions matches.\n");
483 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
491 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
492 memset(&newparam, 0, sizeof(union parameter));
493 newparam.disconnectinfo.cause = cause;
494 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
495 send_message(MESSAGE_RELEASE, call->ref, &newparam);
497 /* release asterisk */
498 ast->hangupcause = call->cause;
499 /* change to release state */
500 call->state = CHAN_LCR_STATE_RELEASE;
501 ast_hangup(ast); // call will be destroyed here
505 /* send setup to asterisk */
506 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
507 ret = ast_pbx_start(ast);
510 cause = (ret==-2)?34:27;
513 call->pbx_started = 1;
518 * incoming setup from LCR
520 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
522 struct ast_channel *ast;
523 union parameter newparam;
525 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %d, dialing %d)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
527 /* create asterisk channel instrance */
528 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
532 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
533 memset(&newparam, 0, sizeof(union parameter));
534 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
535 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
536 send_message(MESSAGE_RELEASE, call->ref, &newparam);
541 /* set ast pointer */
543 ast->tech_pvt = call;
544 ast->tech = &lcr_tech;
545 ast->fds[0] = call->pipe[0];
547 /* fill setup information */
548 if (param->setup.dialinginfo.id)
549 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
550 if (param->setup.context[0])
551 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
552 if (param->setup.callerinfo.id[0])
553 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
554 if (param->setup.callerinfo.name[0])
555 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
557 if (param->setup.redirinfo.id[0])
558 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
560 switch (param->setup.callerinfo.present)
562 case INFO_PRESENT_ALLOWED:
563 ast->cid.cid_pres = AST_PRES_ALLOWED;
565 case INFO_PRESENT_RESTRICTED:
566 ast->cid.cid_pres = AST_PRES_RESTRICTED;
569 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
571 switch (param->setup.callerinfo.ntype)
573 case INFO_NTYPE_SUBSCRIBER:
574 ast->cid.cid_ton = 4;
576 case INFO_NTYPE_NATIONAL:
577 ast->cid.cid_ton = 2;
579 case INFO_NTYPE_INTERNATIONAL:
580 ast->cid.cid_ton = 1;
583 ast->cid.cid_ton = 0;
585 ast->transfercapability = param->setup.capainfo.bearer_capa;
587 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1);
589 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, "0", "00"), sizeof(call->oad)-1);
592 /* configure channel */
594 ast->nativeformats = configfile->lawformat;
595 ast->readformat = ast->rawreadformat = configfile->lawformat;
596 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
598 ast->nativeformats = AST_FORMAT_ALAW;
599 ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
600 ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
602 ast->hangupcause = 0;
605 call->state = CHAN_LCR_STATE_IN_SETUP;
607 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
611 * incoming setup acknowledge from LCR
613 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
615 if (!call->ast) return;
617 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
619 /* send pending digits in dialque */
621 send_dialque_to_lcr(call);
622 /* change to overlap state */
623 call->state = CHAN_LCR_STATE_OUT_DIALING;
627 * incoming proceeding from LCR
629 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
631 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
634 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
635 /* send event to asterisk */
636 if (call->ast && call->pbx_started)
637 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
641 * incoming alerting from LCR
643 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
645 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
648 call->state = CHAN_LCR_STATE_OUT_ALERTING;
649 /* send event to asterisk */
650 if (call->ast && call->pbx_started)
651 ast_queue_control(call->ast, AST_CONTROL_RINGING);
655 * incoming connect from LCR
657 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
659 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
662 call->state = CHAN_LCR_STATE_CONNECT;
663 /* copy connectinfo */
664 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
665 /* send event to asterisk */
666 if (call->ast && call->pbx_started)
667 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
671 * incoming disconnect from LCR
673 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
675 struct ast_channel *ast = call->ast;
677 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
680 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
682 call->cause = param->disconnectinfo.cause;
683 call->location = param->disconnectinfo.location;
684 /* if bridge, forward disconnect and return */
687 if (call->bridge_call)
689 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
690 bridge_message_if_bridged(call, message_type, param);
694 /* release lcr with same cause */
695 send_message(MESSAGE_RELEASE, call->ref, param);
697 /* change to release state */
698 call->state = CHAN_LCR_STATE_RELEASE;
699 /* release asterisk */
702 ast->hangupcause = call->cause;
703 if (call->pbx_started)
704 ast_queue_hangup(ast);
706 ast_hangup(ast); // call will be destroyed here
712 * incoming setup acknowledge from LCR
714 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
716 struct ast_channel *ast = call->ast;
718 CDEBUG(call, call->ast, "Incomming release from LCR. (cause=%d)\n", param->disconnectinfo.cause);
722 /* change to release state */
723 call->state = CHAN_LCR_STATE_RELEASE;
724 /* copy release info */
727 call->cause = param->disconnectinfo.cause;
728 call->location = param->disconnectinfo.location;
730 /* if we have an asterisk instance, send hangup, else we are done */
733 ast->hangupcause = call->cause;
734 if (call->pbx_started)
735 ast_queue_hangup(ast);
737 ast_hangup(ast); // call will be destroyed here
747 * incoming information from LCR
749 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
754 CDEBUG(call, call->ast, "Incomming information from LCR. (dialing=%d)\n", param->information.id);
756 if (!call->ast) return;
758 /* pbx not started */
759 if (!call->pbx_started)
761 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
762 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
763 lcr_start_pbx(call, ast, param->information.sending_complete);
768 p = param->information.id;
769 if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
771 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
774 /* send digit to asterisk */
775 memset(&fr, 0, sizeof(fr));
776 fr.frametype = AST_FRAME_DTMF;
778 fr.delivery = ast_tv(0, 0);
779 ast_queue_frame(call->ast, &fr);
783 /* use bridge to forware message not supported by asterisk */
784 if (call->state == CHAN_LCR_STATE_CONNECT) {
785 CDEBUG(call, call->ast, "Call is connected, briding.\n");
786 bridge_message_if_bridged(call, message_type, param);
791 * incoming information from LCR
793 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
795 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
797 if (!call->ast) return;
799 /* use bridge to forware message not supported by asterisk */
800 bridge_message_if_bridged(call, message_type, param);
804 * incoming information from LCR
806 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
808 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
810 if (!call->ast) return;
812 /* use bridge to forware message not supported by asterisk */
813 bridge_message_if_bridged(call, message_type, param);
817 * message received from LCR
819 int receive_message(int message_type, unsigned long ref, union parameter *param)
821 union parameter newparam;
822 struct bchannel *bchannel;
823 struct chan_call *call;
825 memset(&newparam, 0, sizeof(union parameter));
827 /* handle bchannel message*/
828 if (message_type == MESSAGE_BCHANNEL)
830 switch(param->bchannel.type)
832 case BCHANNEL_ASSIGN:
833 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
834 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
836 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
839 /* create bchannel */
840 bchannel = alloc_bchannel(param->bchannel.handle);
843 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
847 /* configure channel */
848 bchannel->b_tx_gain = param->bchannel.tx_gain;
849 bchannel->b_rx_gain = param->bchannel.rx_gain;
850 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
851 if (param->bchannel.crypt_len)
853 bchannel->b_crypt_len = param->bchannel.crypt_len;
854 bchannel->b_crypt_type = param->bchannel.crypt_type;
855 memcpy(bchannel->b_crypt_key, param->bchannel.crypt, param->bchannel.crypt_len);
857 bchannel->b_txdata = 0;
858 bchannel->b_dtmf = 1;
859 bchannel->b_tx_dejitter = 1;
861 /* in case, ref is not set, this bchannel instance must
862 * be created until it is removed again by LCR */
864 if ((call = find_call_ref(ref)))
866 bchannel->call = call;
867 call->bchannel = bchannel;
869 hier muesen alle bchannel-features gesetzt werden (pipeline...) falls sie vor dem b-kanal verfügbar waren
871 if (call->bridge_id) {
872 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
873 bchannel_join(bchannel, call->bridge_id);
876 if (bchannel_create(bchannel))
877 bchannel_activate(bchannel, 1);
880 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
881 newparam.bchannel.handle = param->bchannel.handle;
882 send_message(MESSAGE_BCHANNEL, 0, &newparam);
885 case BCHANNEL_REMOVE:
886 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
887 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
889 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
892 /* unklink from call and destroy bchannel */
893 free_bchannel(bchannel);
896 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
897 newparam.bchannel.handle = param->bchannel.handle;
898 send_message(MESSAGE_BCHANNEL, 0, &newparam);
903 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
909 if (message_type == MESSAGE_NEWREF)
911 if (param->direction)
913 /* new ref from lcr */
914 CDEBUG(NULL, NULL, "Received new ref by LCR, of call from LCR. (ref=%ld)\n", ref);
915 if (!ref || find_call_ref(ref))
917 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
920 /* allocate new call instance */
923 call->state = CHAN_LCR_STATE_IN_PREPARE;
926 /* wait for setup (or release from asterisk) */
929 /* new ref, as requested from this remote application */
930 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
931 call = find_call_ref(0);
934 /* send release, if ref does not exist */
935 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
936 newparam.disconnectinfo.cause = CAUSE_NORMAL;
937 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
938 send_message(MESSAGE_RELEASE, ref, &newparam);
943 /* send pending setup info */
944 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
945 send_setup_to_lcr(call);
946 /* release if asterisk has signed off */
947 else if (call->state == CHAN_LCR_STATE_RELEASE)
952 newparam.disconnectinfo.cause = call->cause;
953 newparam.disconnectinfo.location = call->location;
956 newparam.disconnectinfo.cause = 16;
957 newparam.disconnectinfo.location = 5;
959 send_message(MESSAGE_RELEASE, ref, &newparam);
971 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
974 call = find_call_ref(ref);
977 /* ignore ref that is not used (anymore) */
978 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
982 /* handle messages */
986 lcr_in_setup(call, message_type, param);
989 case MESSAGE_OVERLAP:
990 lcr_in_overlap(call, message_type, param);
993 case MESSAGE_PROCEEDING:
994 lcr_in_proceeding(call, message_type, param);
997 case MESSAGE_ALERTING:
998 lcr_in_alerting(call, message_type, param);
1001 case MESSAGE_CONNECT:
1002 lcr_in_connect(call, message_type, param);
1005 case MESSAGE_DISCONNECT:
1006 lcr_in_disconnect(call, message_type, param);
1009 case MESSAGE_RELEASE:
1010 lcr_in_release(call, message_type, param);
1013 case MESSAGE_INFORMATION:
1014 lcr_in_information(call, message_type, param);
1017 case MESSAGE_NOTIFY:
1018 lcr_in_notify(call, message_type, param);
1021 case MESSAGE_FACILITY:
1022 lcr_in_facility(call, message_type, param);
1025 case MESSAGE_PATTERN: // audio available from LCR
1028 case MESSAGE_NOPATTERN: // audio not available from LCR
1031 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1032 call->audiopath = param->audiopath;
1036 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1043 * release all calls (due to broken socket)
1045 static void release_all_calls(void)
1047 struct chan_call *call;
1053 /* no ast, so we may directly free call */
1055 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1059 /* already in release process */
1060 if (call->state == CHAN_LCR_STATE_RELEASE) {
1064 /* release or queue release */
1066 call->state = CHAN_LCR_STATE_RELEASE;
1067 if (!call->pbx_started)
1069 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1070 ast_hangup(call->ast); // call will be destroyed here
1073 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1074 ast_queue_hangup(call->ast);
1081 * warning! not thread safe
1082 * returns -1 for socket error, 0 for no work, 1 for work
1084 int handle_socket(void)
1088 struct admin_message msg;
1089 struct admin_list *admin;
1093 /* read from socket */
1094 len = read(sock, &msg, sizeof(msg));
1097 CERROR(NULL, NULL, "Socket closed.\n");
1098 return(-1); // socket closed
1102 if (len != sizeof(msg))
1104 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1105 return(-1); // socket error
1107 if (msg.message != ADMIN_MESSAGE)
1109 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1110 return(-1); // socket error
1112 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1116 if (errno != EWOULDBLOCK)
1118 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1123 /* write to socket */
1126 admin = admin_first;
1127 len = write(sock, &admin->msg, sizeof(msg));
1130 CERROR(NULL, NULL, "Socket closed.\n");
1131 return(-1); // socket closed
1135 if (len != sizeof(msg))
1137 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1138 return(-1); // socket error
1141 admin_first = admin->next;
1147 if (errno != EWOULDBLOCK)
1149 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1158 * open and close socket and thread
1160 int open_socket(void)
1164 char *socket_name = SOCKET_NAME;
1166 struct sockaddr_un sock_address;
1167 unsigned long on = 1;
1168 union parameter param;
1171 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1173 CERROR(NULL, NULL, "Failed to create socket.\n");
1177 /* set socket address and name */
1178 memset(&sock_address, 0, sizeof(sock_address));
1179 sock_address.sun_family = PF_UNIX;
1180 strcpy(sock_address.sun_path, socket_name);
1182 /* connect socket */
1183 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1186 CERROR(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1190 /* set non-blocking io */
1191 if ((ret = ioctl(sock, FIONBIO, (unsigned char *)(&on))) < 0)
1194 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1198 /* enque hello message */
1199 memset(¶m, 0, sizeof(param));
1200 strcpy(param.hello.application, "asterisk");
1201 send_message(MESSAGE_HELLO, 0, ¶m);
1206 void close_socket(int sock)
1213 static void *chan_thread(void *arg)
1217 union parameter param;
1218 time_t retry = 0, now;
1220 bchannel_pid = getpid();
1222 memset(¶m, 0, sizeof(union parameter));
1226 ast_mutex_lock(&chan_lock);
1233 ret = handle_socket();
1235 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1236 close_socket(lcr_sock);
1238 release_all_calls();
1245 if (retry && now-retry > 5) {
1246 CERROR(NULL, NULL, "Retry to open socket.\n");
1248 if (!(lcr_sock = open_socket())) {
1257 ret = bchannel_handle();
1262 ast_mutex_unlock(&chan_lock);
1264 ast_mutex_lock(&chan_lock);
1268 CERROR(NULL, NULL, "Thread exit.\n");
1270 ast_mutex_unlock(&chan_lock);
1276 * new asterisk instance
1279 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1281 union parameter newparam;
1282 struct ast_channel *ast;
1283 struct chan_call *call;
1285 ast_mutex_lock(&chan_lock);
1287 CDEBUG(NULL, NULL, "Received request from Asterisk.\n");
1289 /* if socket is closed */
1292 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1296 /* create call instance */
1297 call = alloc_call();
1300 /* failed to create instance */
1303 /* create asterisk channel instrance */
1304 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1307 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1309 /* failed to create instance */
1313 ast->tech_pvt = call;
1315 ast->tech = &lcr_tech;
1316 /* configure channel */
1318 snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel);
1319 ast->name[sizeof(ast->name)-1] = '\0';
1322 ast->nativeformats = configfile->lawformat;
1323 ast->readformat = ast->rawreadformat = configfile->lawformat;
1324 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
1326 ast->nativeformats = AST_FORMAT_ALAW;
1327 ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
1328 ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
1330 ast->hangupcause = 0;
1331 /* send MESSAGE_NEWREF */
1332 memset(&newparam, 0, sizeof(union parameter));
1333 newparam.direction = 0; /* request from app */
1334 send_message(MESSAGE_NEWREF, 0, &newparam);
1336 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1338 ast_mutex_unlock(&chan_lock);
1344 * call from asterisk
1346 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1348 struct chan_call *call;
1350 ast_mutex_lock(&chan_lock);
1351 call = ast->tech_pvt;
1353 CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n");
1354 ast_mutex_unlock(&chan_lock);
1358 CDEBUG(call, ast, "Received call from Asterisk.\n");
1360 call->pbx_started = 1;
1362 /* send setup message, if we already have a callref */
1364 send_setup_to_lcr(call);
1366 ast_mutex_unlock(&chan_lock);
1370 static int lcr_digit(struct ast_channel *ast, char digit)
1372 struct chan_call *call;
1373 union parameter newparam;
1376 /* only pass IA5 number space */
1377 if (digit > 126 || digit < 32)
1380 ast_mutex_lock(&chan_lock);
1381 call = ast->tech_pvt;
1383 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1384 ast_mutex_unlock(&chan_lock);
1388 CDEBUG(call, ast, "Received digit Asterisk.\n");
1390 /* send information or queue them */
1391 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1393 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1394 memset(&newparam, 0, sizeof(union parameter));
1395 newparam.information.id[0] = digit;
1396 newparam.information.id[1] = '\0';
1397 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1400 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP));
1402 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1404 strncat(call->dialque, buf, strlen(call->dialque)-1);
1407 ast_mutex_unlock(&chan_lock);
1412 static int lcr_answer(struct ast_channel *ast)
1414 union parameter newparam;
1415 struct chan_call *call;
1417 ast_mutex_lock(&chan_lock);
1418 call = ast->tech_pvt;
1420 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1421 ast_mutex_unlock(&chan_lock);
1425 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1427 /* copy connectinfo, if bridged */
1428 if (call->bridge_call)
1429 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1430 /* send connect message to lcr */
1431 memset(&newparam, 0, sizeof(union parameter));
1432 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1433 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1435 call->state = CHAN_LCR_STATE_CONNECT;
1437 ast_mutex_unlock(&chan_lock);
1441 static int lcr_hangup(struct ast_channel *ast)
1443 union parameter newparam;
1444 struct chan_call *call;
1445 pthread_t tid = pthread_self();
1447 if (!pthread_equal(tid, chan_tid))
1448 ast_mutex_lock(&chan_lock);
1449 call = ast->tech_pvt;
1451 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1452 if (!pthread_equal(tid, chan_tid))
1453 ast_mutex_unlock(&chan_lock);
1457 if (!pthread_equal(tid, chan_tid))
1458 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1460 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1462 /* disconnect asterisk, maybe not required */
1463 ast->tech_pvt = NULL;
1468 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1469 memset(&newparam, 0, sizeof(union parameter));
1470 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
1471 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1472 send_message(MESSAGE_RELEASE, call->ref, &newparam);
1475 if (!pthread_equal(tid, chan_tid))
1476 ast_mutex_unlock(&chan_lock);
1480 /* ref is not set, due to prepare setup or release */
1481 if (call->state == CHAN_LCR_STATE_RELEASE)
1483 /* we get the response to our release */
1484 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1488 /* during prepare, we change to release state */
1489 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1490 call->state = CHAN_LCR_STATE_RELEASE;
1493 if (!pthread_equal(tid, chan_tid))
1494 ast_mutex_unlock(&chan_lock);
1498 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1500 struct chan_call *call;
1501 unsigned char buffer[1024], *s, *d = buffer;
1504 ast_mutex_lock(&chan_lock);
1505 call = ast->tech_pvt;
1507 ast_mutex_unlock(&chan_lock);
1510 if (call->bchannel && ((ii = f->samples)))
1512 if (ii > sizeof(buffer))
1513 ii = sizeof(buffer);
1515 for (i = 0; i < ii; i++)
1516 *d++ = flip_bits[*s++];
1517 bchannel_transmit(call->bchannel, buffer, ii);
1519 ast_mutex_unlock(&chan_lock);
1524 static struct ast_frame *lcr_read(struct ast_channel *ast)
1526 struct chan_call *call;
1530 ast_mutex_lock(&chan_lock);
1531 call = ast->tech_pvt;
1533 ast_mutex_unlock(&chan_lock);
1536 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1540 p = call->read_buff;
1541 for (i = 0; i < len; i++) {
1546 call->read_fr.frametype = AST_FRAME_VOICE;
1550 call->read_fr.subclass = AST_FORMAT_ALAW;
1551 call->read_fr.datalen = len;
1552 call->read_fr.samples = len;
1553 call->read_fr.delivery = ast_tv(0,0);
1554 call->read_fr.data = call->read_buff;
1555 ast_mutex_unlock(&chan_lock);
1557 return &call->read_fr;
1560 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1562 union parameter newparam;
1564 struct chan_call *call;
1566 ast_mutex_lock(&chan_lock);
1567 call = ast->tech_pvt;
1569 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1570 ast_mutex_unlock(&chan_lock);
1575 case AST_CONTROL_BUSY:
1576 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1577 /* send message to lcr */
1578 memset(&newparam, 0, sizeof(union parameter));
1579 newparam.disconnectinfo.cause = 17;
1580 newparam.disconnectinfo.location = 5;
1581 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1583 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1585 ast_mutex_unlock(&chan_lock);
1587 case AST_CONTROL_CONGESTION:
1588 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk.\n");
1590 ast_mutex_unlock(&chan_lock);
1592 case AST_CONTROL_RINGING:
1593 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1594 /* send message to lcr */
1595 memset(&newparam, 0, sizeof(union parameter));
1596 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1598 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1600 ast_mutex_unlock(&chan_lock);
1604 ast_mutex_unlock(&chan_lock);
1607 case AST_CONTROL_VIDUPDATE:
1610 case AST_CONTROL_HOLD:
1611 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1612 /* send message to lcr */
1613 memset(&newparam, 0, sizeof(union parameter));
1614 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1615 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1617 case AST_CONTROL_UNHOLD:
1618 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1619 /* send message to lcr */
1620 memset(&newparam, 0, sizeof(union parameter));
1621 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1622 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1626 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1628 ast_mutex_unlock(&chan_lock);
1633 ast_mutex_unlock(&chan_lock);
1640 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
1641 struct ast_channel *ast2, int flags,
1642 struct ast_frame **fo,
1643 struct ast_channel **rc, int timeoutms)
1646 struct chan_call *call1, *call2;
1647 struct ast_channel *carr[2], *who;
1649 struct ast_frame *f;
1652 CDEBUG(NULL, ast, "Received briding request from Asterisk.\n");
1654 /* join via dsp (if the channels are currently open) */
1655 ast_mutex_lock(&chan_lock);
1656 bridge_id = new_bridge_id();
1657 call1 = ast1->tech_pvt;
1658 call2 = ast2->tech_pvt;
1661 call1->bridge_id = bridge_id;
1662 if (call1->bchannel)
1663 bchannel_join(call1->bchannel, bridge_id);
1664 call1->bridge_call = call2;
1668 call2->bridge_id = bridge_id;
1669 if (call2->bchannel)
1670 bchannel_join(call2->bchannel, bridge_id);
1671 call2->bridge_call = call1;
1673 ast_mutex_unlock(&chan_lock);
1676 who = ast_waitfor_n(carr, 2, &to);
1679 CDEBUG(NULL, ast, "Empty read on bridge, breaking out.\n");
1684 if (!f || f->frametype == AST_FRAME_CONTROL) {
1691 if ( f->frametype == AST_FRAME_DTMF ) {
1707 CDEBUG(NULL, ast, "Releasing bride.\n");
1709 /* split channels */
1710 ast_mutex_lock(&chan_lock);
1711 call1 = ast1->tech_pvt;
1712 call2 = ast2->tech_pvt;
1715 call1->bridge_id = 0;
1716 if (call1->bchannel)
1717 bchannel_join(call1->bchannel, 0);
1718 if (call1->bridge_call)
1719 call1->bridge_call->bridge_call = NULL;
1720 call1->bridge_call = NULL;
1724 call2->bridge_id = 0;
1725 if (call2->bchannel)
1726 bchannel_join(call2->bchannel, 0);
1727 if (call2->bridge_call)
1728 call2->bridge_call->bridge_call = NULL;
1729 call2->bridge_call = NULL;
1731 ast_mutex_unlock(&chan_lock);
1734 return AST_BRIDGE_COMPLETE;
1736 struct ast_channel_tech lcr_tech = {
1738 .description="Channel driver for connecting to Linux-Call-Router",
1742 .capabilities=AST_FORMAT_ALAW,
1744 .requester=lcr_request,
1745 .send_digit_begin=lcr_digit,
1752 .indicate=lcr_indicate,
1753 // .fixup=lcr_fixup,
1754 // .send_text=lcr_send_text,
1762 static int lcr_show_lcr (int fd, int argc, char *argv[])
1767 static int lcr_show_calls (int fd, int argc, char *argv[])
1772 static int lcr_reload_routing (int fd, int argc, char *argv[])
1777 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
1782 static int lcr_port_block (int fd, int argc, char *argv[])
1787 static int lcr_port_unblock (int fd, int argc, char *argv[])
1792 static int lcr_port_unload (int fd, int argc, char *argv[])
1797 struct ast_cli_entry cli_show_lcr =
1798 { {"lcr", "show", "lcr", NULL},
1800 "Shows current states of LCR core",
1801 "Usage: lcr show lcr\n",
1804 struct ast_cli_entry cli_show_calls =
1805 { {"lcr", "show", "calls", NULL},
1807 "Shows current calls made by LCR and Asterisk",
1808 "Usage: lcr show calls\n",
1811 struct ast_cli_entry cli_reload_routing =
1812 { {"lcr", "reload", "routing", NULL},
1814 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
1815 "Usage: lcr reload routing\n",
1818 struct ast_cli_entry cli_reload_interfaces =
1819 { {"lcr", "reload", "interfaces", NULL},
1820 lcr_reload_interfaces,
1821 "Reloads interfaces conf of LCR",
1822 "Usage: lcr reload interfaces\n",
1825 struct ast_cli_entry cli_port_block =
1826 { {"lcr", "port", "block", NULL},
1828 "Blocks LCR port for further calls",
1829 "Usage: lcr port block \"<port>\"\n",
1832 struct ast_cli_entry cli_port_unblock =
1833 { {"lcr", "port", "unblock", NULL},
1835 "Unblocks or loads LCR port, port is opened my mISDN",
1836 "Usage: lcr port unblock \"<port>\"\n",
1839 struct ast_cli_entry cli_port_unload =
1840 { {"lcr", "port", "unload", NULL},
1842 "Unloads LCR port, port is closes by mISDN",
1843 "Usage: lcr port unload \"<port>\"\n",
1848 * module loading and destruction
1850 static int load_module(void)
1854 for (i = 0; i < 256; i++)
1855 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
1856 || (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
1858 ast_mutex_init(&chan_lock);
1859 ast_mutex_init(&log_lock);
1861 if (!(lcr_sock = open_socket())) {
1862 /* continue with closed socket */
1865 if (!bchannel_initialize()) {
1866 CERROR(NULL, NULL, "Unable to open mISDN device\n");
1867 close_socket(lcr_sock);
1872 if (ast_channel_register(&lcr_tech)) {
1873 CERROR(NULL, NULL, "Unable to register channel class\n");
1874 bchannel_deinitialize();
1875 close_socket(lcr_sock);
1880 ast_cli_register(&cli_show_lcr);
1881 ast_cli_register(&cli_show_calls);
1883 ast_cli_register(&cli_reload_routing);
1884 ast_cli_register(&cli_reload_interfaces);
1885 ast_cli_register(&cli_port_block);
1886 ast_cli_register(&cli_port_unblock);
1887 ast_cli_register(&cli_port_unload);
1889 ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
1890 "misdn_set_opt(:<opt><optarg>:<opt><optarg>..):\n"
1891 "Sets mISDN opts. and optargs\n"
1893 "The available options are:\n"
1894 " d - Send display text on called phone, text is the optparam\n"
1895 " n - don't detect dtmf tones on called channel\n"
1896 " h - make digital outgoing call\n"
1897 " c - make crypted outgoing call, param is keyindex\n"
1898 " e - perform echo cancelation on this channel,\n"
1899 " takes taps as arguments (32,64,128,256)\n"
1900 " s - send Non Inband DTMF as inband\n"
1901 " vr - rxgain control\n"
1902 " vt - txgain control\n"
1906 lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1909 //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1913 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
1915 /* failed to create thread */
1916 bchannel_deinitialize();
1917 close_socket(lcr_sock);
1918 ast_channel_unregister(&lcr_tech);
1924 static int unload_module(void)
1926 /* First, take us out of the channel loop */
1927 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
1930 pthread_join(chan_tid, NULL);
1932 ast_channel_unregister(&lcr_tech);
1934 if (mISDN_created) {
1935 bchannel_deinitialize();
1939 if (lcr_sock >= 0) {
1947 int reload_module(void)
1954 mutex init fehlt noch
1955 ast_mutex_t usecnt_lock;
1961 ast_mutex_lock(&usecnt_lock);
1963 ast_mutex_unlock(&usecnt_lock);
1969 char *desc="Channel driver for lcr";
1971 char *description(void)
1978 return ASTERISK_GPL_KEY;
1981 #define AST_MODULE "chan_lcr"
1982 AST_MODULE_INFO( ASTERISK_GPL_KEY,
1983 AST_MODFLAG_DEFAULT,
1984 "Channel driver for LCR",