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);
451 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
453 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);
457 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
458 /* send setup acknowledge to lcr */
459 memset(&newparam, 0, sizeof(union parameter));
460 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
463 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
468 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
470 /* send setup acknowledge to lcr */
471 memset(&newparam, 0, sizeof(union parameter));
472 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
475 call->state = CHAN_LCR_STATE_IN_DIALING;
477 /* if match, start pbx */
478 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
479 CDEBUG(call, ast, "Extensions matches.\n");
484 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
492 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
493 memset(&newparam, 0, sizeof(union parameter));
494 newparam.disconnectinfo.cause = cause;
495 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
496 send_message(MESSAGE_RELEASE, call->ref, &newparam);
498 /* release asterisk */
499 ast->hangupcause = call->cause;
500 /* change to release state */
501 call->state = CHAN_LCR_STATE_RELEASE;
502 ast_hangup(ast); // call will be destroyed here
506 /* send setup to asterisk */
507 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
508 ret = ast_pbx_start(ast);
511 cause = (ret==-2)?34:27;
514 call->pbx_started = 1;
519 * incoming setup from LCR
521 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
523 struct ast_channel *ast;
524 union parameter newparam;
526 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %d, dialing %d)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
528 /* create asterisk channel instrance */
529 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
533 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
534 memset(&newparam, 0, sizeof(union parameter));
535 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
536 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
537 send_message(MESSAGE_RELEASE, call->ref, &newparam);
542 /* set ast pointer */
544 ast->tech_pvt = call;
545 ast->tech = &lcr_tech;
546 ast->fds[0] = call->pipe[0];
548 /* fill setup information */
549 if (param->setup.dialinginfo.id)
550 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
551 if (param->setup.context[0])
552 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
553 if (param->setup.callerinfo.id[0])
554 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
555 if (param->setup.callerinfo.name[0])
556 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
558 if (param->setup.redirinfo.id[0])
559 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
561 switch (param->setup.callerinfo.present)
563 case INFO_PRESENT_ALLOWED:
564 ast->cid.cid_pres = AST_PRES_ALLOWED;
566 case INFO_PRESENT_RESTRICTED:
567 ast->cid.cid_pres = AST_PRES_RESTRICTED;
570 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
572 switch (param->setup.callerinfo.ntype)
574 case INFO_NTYPE_SUBSCRIBER:
575 ast->cid.cid_ton = 4;
577 case INFO_NTYPE_NATIONAL:
578 ast->cid.cid_ton = 2;
580 case INFO_NTYPE_INTERNATIONAL:
581 ast->cid.cid_ton = 1;
584 ast->cid.cid_ton = 0;
586 ast->transfercapability = param->setup.capainfo.bearer_capa;
588 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1);
590 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, "0", "00"), sizeof(call->oad)-1);
593 /* configure channel */
595 ast->nativeformats = configfile->lawformat;
596 ast->readformat = ast->rawreadformat = configfile->lawformat;
597 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
599 ast->nativeformats = AST_FORMAT_ALAW;
600 ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
601 ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
603 ast->hangupcause = 0;
606 call->state = CHAN_LCR_STATE_IN_SETUP;
608 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
612 * incoming setup acknowledge from LCR
614 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
616 if (!call->ast) return;
618 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
620 /* send pending digits in dialque */
622 send_dialque_to_lcr(call);
623 /* change to overlap state */
624 call->state = CHAN_LCR_STATE_OUT_DIALING;
628 * incoming proceeding from LCR
630 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
632 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
635 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
636 /* send event to asterisk */
637 if (call->ast && call->pbx_started)
638 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
642 * incoming alerting from LCR
644 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
646 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
649 call->state = CHAN_LCR_STATE_OUT_ALERTING;
650 /* send event to asterisk */
651 if (call->ast && call->pbx_started)
652 ast_queue_control(call->ast, AST_CONTROL_RINGING);
656 * incoming connect from LCR
658 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
660 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
663 call->state = CHAN_LCR_STATE_CONNECT;
664 /* copy connectinfo */
665 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
666 /* send event to asterisk */
667 if (call->ast && call->pbx_started)
668 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
672 * incoming disconnect from LCR
674 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
676 struct ast_channel *ast = call->ast;
678 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
681 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
683 call->cause = param->disconnectinfo.cause;
684 call->location = param->disconnectinfo.location;
685 /* if bridge, forward disconnect and return */
688 if (call->bridge_call)
690 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
691 bridge_message_if_bridged(call, message_type, param);
695 /* release lcr with same cause */
696 send_message(MESSAGE_RELEASE, call->ref, param);
698 /* change to release state */
699 call->state = CHAN_LCR_STATE_RELEASE;
700 /* release asterisk */
703 ast->hangupcause = call->cause;
704 if (call->pbx_started)
705 ast_queue_hangup(ast);
707 ast_hangup(ast); // call will be destroyed here
713 * incoming setup acknowledge from LCR
715 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
717 struct ast_channel *ast = call->ast;
719 CDEBUG(call, call->ast, "Incomming release from LCR. (cause=%d)\n", param->disconnectinfo.cause);
723 /* change to release state */
724 call->state = CHAN_LCR_STATE_RELEASE;
725 /* copy release info */
728 call->cause = param->disconnectinfo.cause;
729 call->location = param->disconnectinfo.location;
731 /* if we have an asterisk instance, send hangup, else we are done */
734 ast->hangupcause = call->cause;
735 if (call->pbx_started)
736 ast_queue_hangup(ast);
738 ast_hangup(ast); // call will be destroyed here
748 * incoming information from LCR
750 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
752 struct ast_channel *ast = call->ast;
756 CDEBUG(call, call->ast, "Incomming information from LCR. (dialing=%d)\n", param->information.id);
760 /* pbx not started */
761 if (!call->pbx_started)
763 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
764 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
765 lcr_start_pbx(call, ast, param->information.sending_complete);
770 p = param->information.id;
771 if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
773 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
776 /* send digit to asterisk */
777 memset(&fr, 0, sizeof(fr));
778 fr.frametype = AST_FRAME_DTMF;
780 fr.delivery = ast_tv(0, 0);
781 ast_queue_frame(call->ast, &fr);
785 /* use bridge to forware message not supported by asterisk */
786 if (call->state == CHAN_LCR_STATE_CONNECT) {
787 CDEBUG(call, call->ast, "Call is connected, briding.\n");
788 bridge_message_if_bridged(call, message_type, param);
793 * incoming information from LCR
795 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
797 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
799 if (!call->ast) return;
801 /* use bridge to forware message not supported by asterisk */
802 bridge_message_if_bridged(call, message_type, param);
806 * incoming information from LCR
808 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
810 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
812 if (!call->ast) return;
814 /* use bridge to forware message not supported by asterisk */
815 bridge_message_if_bridged(call, message_type, param);
819 * message received from LCR
821 int receive_message(int message_type, unsigned long ref, union parameter *param)
823 union parameter newparam;
824 struct bchannel *bchannel;
825 struct chan_call *call;
827 memset(&newparam, 0, sizeof(union parameter));
829 /* handle bchannel message*/
830 if (message_type == MESSAGE_BCHANNEL)
832 switch(param->bchannel.type)
834 case BCHANNEL_ASSIGN:
835 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
836 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
838 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
841 /* create bchannel */
842 bchannel = alloc_bchannel(param->bchannel.handle);
845 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
849 /* configure channel */
850 bchannel->b_tx_gain = param->bchannel.tx_gain;
851 bchannel->b_rx_gain = param->bchannel.rx_gain;
852 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
853 if (param->bchannel.crypt_len)
855 bchannel->b_crypt_len = param->bchannel.crypt_len;
856 bchannel->b_crypt_type = param->bchannel.crypt_type;
857 memcpy(bchannel->b_crypt_key, param->bchannel.crypt, param->bchannel.crypt_len);
859 bchannel->b_txdata = 0;
860 bchannel->b_dtmf = 1;
861 bchannel->b_tx_dejitter = 1;
863 /* in case, ref is not set, this bchannel instance must
864 * be created until it is removed again by LCR */
866 if ((call = find_call_ref(ref)))
868 bchannel->call = call;
869 call->bchannel = bchannel;
871 hier muesen alle bchannel-features gesetzt werden (pipeline...) falls sie vor dem b-kanal verfügbar waren
873 if (call->bridge_id) {
874 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
875 bchannel_join(bchannel, call->bridge_id);
878 if (bchannel_create(bchannel))
879 bchannel_activate(bchannel, 1);
882 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
883 newparam.bchannel.handle = param->bchannel.handle;
884 send_message(MESSAGE_BCHANNEL, 0, &newparam);
887 case BCHANNEL_REMOVE:
888 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
889 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
891 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
894 /* unklink from call and destroy bchannel */
895 free_bchannel(bchannel);
898 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
899 newparam.bchannel.handle = param->bchannel.handle;
900 send_message(MESSAGE_BCHANNEL, 0, &newparam);
905 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
911 if (message_type == MESSAGE_NEWREF)
913 if (param->direction)
915 /* new ref from lcr */
916 CDEBUG(NULL, NULL, "Received new ref by LCR, of call from LCR. (ref=%ld)\n", ref);
917 if (!ref || find_call_ref(ref))
919 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
922 /* allocate new call instance */
925 call->state = CHAN_LCR_STATE_IN_PREPARE;
928 /* wait for setup (or release from asterisk) */
931 /* new ref, as requested from this remote application */
932 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
933 call = find_call_ref(0);
936 /* send release, if ref does not exist */
937 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
938 newparam.disconnectinfo.cause = CAUSE_NORMAL;
939 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
940 send_message(MESSAGE_RELEASE, ref, &newparam);
945 /* send pending setup info */
946 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
947 send_setup_to_lcr(call);
948 /* release if asterisk has signed off */
949 else if (call->state == CHAN_LCR_STATE_RELEASE)
954 newparam.disconnectinfo.cause = call->cause;
955 newparam.disconnectinfo.location = call->location;
958 newparam.disconnectinfo.cause = 16;
959 newparam.disconnectinfo.location = 5;
961 send_message(MESSAGE_RELEASE, ref, &newparam);
973 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
976 call = find_call_ref(ref);
979 /* ignore ref that is not used (anymore) */
980 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
984 /* handle messages */
988 lcr_in_setup(call, message_type, param);
991 case MESSAGE_OVERLAP:
992 lcr_in_overlap(call, message_type, param);
995 case MESSAGE_PROCEEDING:
996 lcr_in_proceeding(call, message_type, param);
999 case MESSAGE_ALERTING:
1000 lcr_in_alerting(call, message_type, param);
1003 case MESSAGE_CONNECT:
1004 lcr_in_connect(call, message_type, param);
1007 case MESSAGE_DISCONNECT:
1008 lcr_in_disconnect(call, message_type, param);
1011 case MESSAGE_RELEASE:
1012 lcr_in_release(call, message_type, param);
1015 case MESSAGE_INFORMATION:
1016 lcr_in_information(call, message_type, param);
1019 case MESSAGE_NOTIFY:
1020 lcr_in_notify(call, message_type, param);
1023 case MESSAGE_FACILITY:
1024 lcr_in_facility(call, message_type, param);
1027 case MESSAGE_PATTERN: // audio available from LCR
1030 case MESSAGE_NOPATTERN: // audio not available from LCR
1033 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1034 call->audiopath = param->audiopath;
1038 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1045 * release all calls (due to broken socket)
1047 static void release_all_calls(void)
1049 struct chan_call *call;
1055 /* no ast, so we may directly free call */
1057 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1061 /* already in release process */
1062 if (call->state == CHAN_LCR_STATE_RELEASE) {
1066 /* release or queue release */
1068 call->state = CHAN_LCR_STATE_RELEASE;
1069 if (!call->pbx_started)
1071 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1072 ast_hangup(call->ast); // call will be destroyed here
1075 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1076 ast_queue_hangup(call->ast);
1083 * warning! not thread safe
1084 * returns -1 for socket error, 0 for no work, 1 for work
1086 int handle_socket(void)
1090 struct admin_message msg;
1091 struct admin_list *admin;
1095 /* read from socket */
1096 len = read(sock, &msg, sizeof(msg));
1099 CERROR(NULL, NULL, "Socket closed.\n");
1100 return(-1); // socket closed
1104 if (len != sizeof(msg))
1106 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1107 return(-1); // socket error
1109 if (msg.message != ADMIN_MESSAGE)
1111 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1112 return(-1); // socket error
1114 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1118 if (errno != EWOULDBLOCK)
1120 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1125 /* write to socket */
1128 admin = admin_first;
1129 len = write(sock, &admin->msg, sizeof(msg));
1132 CERROR(NULL, NULL, "Socket closed.\n");
1133 return(-1); // socket closed
1137 if (len != sizeof(msg))
1139 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1140 return(-1); // socket error
1143 admin_first = admin->next;
1149 if (errno != EWOULDBLOCK)
1151 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1160 * open and close socket and thread
1162 int open_socket(void)
1166 char *socket_name = SOCKET_NAME;
1168 struct sockaddr_un sock_address;
1169 unsigned long on = 1;
1170 union parameter param;
1173 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1175 CERROR(NULL, NULL, "Failed to create socket.\n");
1179 /* set socket address and name */
1180 memset(&sock_address, 0, sizeof(sock_address));
1181 sock_address.sun_family = PF_UNIX;
1182 strcpy(sock_address.sun_path, socket_name);
1184 /* connect socket */
1185 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1188 CERROR(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1192 /* set non-blocking io */
1193 if ((ret = ioctl(sock, FIONBIO, (unsigned char *)(&on))) < 0)
1196 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1200 /* enque hello message */
1201 memset(¶m, 0, sizeof(param));
1202 strcpy(param.hello.application, "asterisk");
1203 send_message(MESSAGE_HELLO, 0, ¶m);
1208 void close_socket(int sock)
1215 static void *chan_thread(void *arg)
1219 union parameter param;
1220 time_t retry = 0, now;
1222 bchannel_pid = getpid();
1224 memset(¶m, 0, sizeof(union parameter));
1228 ast_mutex_lock(&chan_lock);
1235 ret = handle_socket();
1237 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1238 close_socket(lcr_sock);
1240 release_all_calls();
1247 if (retry && now-retry > 5) {
1248 CERROR(NULL, NULL, "Retry to open socket.\n");
1250 if (!(lcr_sock = open_socket())) {
1259 ret = bchannel_handle();
1264 ast_mutex_unlock(&chan_lock);
1266 ast_mutex_lock(&chan_lock);
1270 CERROR(NULL, NULL, "Thread exit.\n");
1272 ast_mutex_unlock(&chan_lock);
1278 * new asterisk instance
1281 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1283 union parameter newparam;
1284 struct ast_channel *ast;
1285 struct chan_call *call;
1287 ast_mutex_lock(&chan_lock);
1289 CDEBUG(NULL, NULL, "Received request from Asterisk.\n");
1291 /* if socket is closed */
1294 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1298 /* create call instance */
1299 call = alloc_call();
1302 /* failed to create instance */
1305 /* create asterisk channel instrance */
1306 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1309 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1311 /* failed to create instance */
1315 ast->tech_pvt = call;
1317 ast->tech = &lcr_tech;
1318 /* configure channel */
1320 snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel);
1321 ast->name[sizeof(ast->name)-1] = '\0';
1324 ast->nativeformats = configfile->lawformat;
1325 ast->readformat = ast->rawreadformat = configfile->lawformat;
1326 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
1328 ast->nativeformats = AST_FORMAT_ALAW;
1329 ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
1330 ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
1332 ast->hangupcause = 0;
1333 /* send MESSAGE_NEWREF */
1334 memset(&newparam, 0, sizeof(union parameter));
1335 newparam.direction = 0; /* request from app */
1336 send_message(MESSAGE_NEWREF, 0, &newparam);
1338 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1340 ast_mutex_unlock(&chan_lock);
1346 * call from asterisk
1348 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1350 struct chan_call *call;
1352 ast_mutex_lock(&chan_lock);
1353 call = ast->tech_pvt;
1355 CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n");
1356 ast_mutex_unlock(&chan_lock);
1360 CDEBUG(call, ast, "Received call from Asterisk.\n");
1362 call->pbx_started = 1;
1364 /* send setup message, if we already have a callref */
1366 send_setup_to_lcr(call);
1368 ast_mutex_unlock(&chan_lock);
1372 static int lcr_digit(struct ast_channel *ast, char digit)
1374 struct chan_call *call;
1375 union parameter newparam;
1378 /* only pass IA5 number space */
1379 if (digit > 126 || digit < 32)
1382 ast_mutex_lock(&chan_lock);
1383 call = ast->tech_pvt;
1385 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1386 ast_mutex_unlock(&chan_lock);
1390 CDEBUG(call, ast, "Received digit Asterisk.\n");
1392 /* send information or queue them */
1393 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1395 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1396 memset(&newparam, 0, sizeof(union parameter));
1397 newparam.information.id[0] = digit;
1398 newparam.information.id[1] = '\0';
1399 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1402 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP));
1404 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1406 strncat(call->dialque, buf, strlen(call->dialque)-1);
1409 ast_mutex_unlock(&chan_lock);
1414 static int lcr_answer(struct ast_channel *ast)
1416 union parameter newparam;
1417 struct chan_call *call;
1419 ast_mutex_lock(&chan_lock);
1420 call = ast->tech_pvt;
1422 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1423 ast_mutex_unlock(&chan_lock);
1427 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1429 /* copy connectinfo, if bridged */
1430 if (call->bridge_call)
1431 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1432 /* send connect message to lcr */
1433 memset(&newparam, 0, sizeof(union parameter));
1434 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1435 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1437 call->state = CHAN_LCR_STATE_CONNECT;
1439 ast_mutex_unlock(&chan_lock);
1443 static int lcr_hangup(struct ast_channel *ast)
1445 union parameter newparam;
1446 struct chan_call *call;
1447 pthread_t tid = pthread_self();
1449 if (!pthread_equal(tid, chan_tid))
1450 ast_mutex_lock(&chan_lock);
1451 call = ast->tech_pvt;
1453 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1454 if (!pthread_equal(tid, chan_tid))
1455 ast_mutex_unlock(&chan_lock);
1459 if (!pthread_equal(tid, chan_tid))
1460 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1462 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1464 /* disconnect asterisk, maybe not required */
1465 ast->tech_pvt = NULL;
1470 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1471 memset(&newparam, 0, sizeof(union parameter));
1472 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
1473 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1474 send_message(MESSAGE_RELEASE, call->ref, &newparam);
1477 if (!pthread_equal(tid, chan_tid))
1478 ast_mutex_unlock(&chan_lock);
1482 /* ref is not set, due to prepare setup or release */
1483 if (call->state == CHAN_LCR_STATE_RELEASE)
1485 /* we get the response to our release */
1486 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1490 /* during prepare, we change to release state */
1491 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1492 call->state = CHAN_LCR_STATE_RELEASE;
1495 if (!pthread_equal(tid, chan_tid))
1496 ast_mutex_unlock(&chan_lock);
1500 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1502 struct chan_call *call;
1503 unsigned char buffer[1024], *s, *d = buffer;
1506 ast_mutex_lock(&chan_lock);
1507 call = ast->tech_pvt;
1509 ast_mutex_unlock(&chan_lock);
1512 if (call->bchannel && ((ii = f->samples)))
1514 if (ii > sizeof(buffer))
1515 ii = sizeof(buffer);
1517 for (i = 0; i < ii; i++)
1518 *d++ = flip_bits[*s++];
1519 bchannel_transmit(call->bchannel, buffer, ii);
1521 ast_mutex_unlock(&chan_lock);
1526 static struct ast_frame *lcr_read(struct ast_channel *ast)
1528 struct chan_call *call;
1532 ast_mutex_lock(&chan_lock);
1533 call = ast->tech_pvt;
1535 ast_mutex_unlock(&chan_lock);
1538 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1542 p = call->read_buff;
1543 for (i = 0; i < len; i++) {
1548 call->read_fr.frametype = AST_FRAME_VOICE;
1552 call->read_fr.subclass = AST_FORMAT_ALAW;
1553 call->read_fr.datalen = len;
1554 call->read_fr.samples = len;
1555 call->read_fr.delivery = ast_tv(0,0);
1556 call->read_fr.data = call->read_buff;
1557 ast_mutex_unlock(&chan_lock);
1559 return &call->read_fr;
1562 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1564 union parameter newparam;
1566 struct chan_call *call;
1568 ast_mutex_lock(&chan_lock);
1569 call = ast->tech_pvt;
1571 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1572 ast_mutex_unlock(&chan_lock);
1577 case AST_CONTROL_BUSY:
1578 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1579 /* send message to lcr */
1580 memset(&newparam, 0, sizeof(union parameter));
1581 newparam.disconnectinfo.cause = 17;
1582 newparam.disconnectinfo.location = 5;
1583 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1585 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1587 ast_mutex_unlock(&chan_lock);
1589 case AST_CONTROL_CONGESTION:
1590 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk.\n");
1592 ast_mutex_unlock(&chan_lock);
1594 case AST_CONTROL_RINGING:
1595 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1596 /* send message to lcr */
1597 memset(&newparam, 0, sizeof(union parameter));
1598 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1600 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1602 ast_mutex_unlock(&chan_lock);
1606 ast_mutex_unlock(&chan_lock);
1609 case AST_CONTROL_VIDUPDATE:
1612 case AST_CONTROL_HOLD:
1613 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1614 /* send message to lcr */
1615 memset(&newparam, 0, sizeof(union parameter));
1616 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1617 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1619 case AST_CONTROL_UNHOLD:
1620 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1621 /* send message to lcr */
1622 memset(&newparam, 0, sizeof(union parameter));
1623 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1624 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1628 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1630 ast_mutex_unlock(&chan_lock);
1635 ast_mutex_unlock(&chan_lock);
1642 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
1643 struct ast_channel *ast2, int flags,
1644 struct ast_frame **fo,
1645 struct ast_channel **rc, int timeoutms)
1648 struct chan_call *call1, *call2;
1649 struct ast_channel *carr[2], *who;
1651 struct ast_frame *f;
1654 CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
1656 /* join via dsp (if the channels are currently open) */
1657 ast_mutex_lock(&chan_lock);
1658 bridge_id = new_bridge_id();
1659 call1 = ast1->tech_pvt;
1660 call2 = ast2->tech_pvt;
1663 call1->bridge_id = bridge_id;
1664 if (call1->bchannel)
1665 bchannel_join(call1->bchannel, bridge_id);
1666 call1->bridge_call = call2;
1670 call2->bridge_id = bridge_id;
1671 if (call2->bchannel)
1672 bchannel_join(call2->bchannel, bridge_id);
1673 call2->bridge_call = call1;
1675 ast_mutex_unlock(&chan_lock);
1678 who = ast_waitfor_n(carr, 2, &to);
1681 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
1686 if (!f || f->frametype == AST_FRAME_CONTROL) {
1693 if ( f->frametype == AST_FRAME_DTMF ) {
1709 CDEBUG(NULL, NULL, "Releasing bride.\n");
1711 /* split channels */
1712 ast_mutex_lock(&chan_lock);
1713 call1 = ast1->tech_pvt;
1714 call2 = ast2->tech_pvt;
1717 call1->bridge_id = 0;
1718 if (call1->bchannel)
1719 bchannel_join(call1->bchannel, 0);
1720 if (call1->bridge_call)
1721 call1->bridge_call->bridge_call = NULL;
1722 call1->bridge_call = NULL;
1726 call2->bridge_id = 0;
1727 if (call2->bchannel)
1728 bchannel_join(call2->bchannel, 0);
1729 if (call2->bridge_call)
1730 call2->bridge_call->bridge_call = NULL;
1731 call2->bridge_call = NULL;
1733 ast_mutex_unlock(&chan_lock);
1736 return AST_BRIDGE_COMPLETE;
1738 static struct ast_channel_tech lcr_tech = {
1740 .description="Channel driver for connecting to Linux-Call-Router",
1744 .capabilities=AST_FORMAT_ALAW,
1746 .requester=lcr_request,
1747 .send_digit_begin=lcr_digit,
1754 .indicate=lcr_indicate,
1755 // .fixup=lcr_fixup,
1756 // .send_text=lcr_send_text,
1764 static int lcr_show_lcr (int fd, int argc, char *argv[])
1769 static int lcr_show_calls (int fd, int argc, char *argv[])
1774 static int lcr_reload_routing (int fd, int argc, char *argv[])
1779 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
1784 static int lcr_port_block (int fd, int argc, char *argv[])
1789 static int lcr_port_unblock (int fd, int argc, char *argv[])
1794 static int lcr_port_unload (int fd, int argc, char *argv[])
1799 static struct ast_cli_entry cli_show_lcr =
1800 { {"lcr", "show", "lcr", NULL},
1802 "Shows current states of LCR core",
1803 "Usage: lcr show lcr\n",
1806 static struct ast_cli_entry cli_show_calls =
1807 { {"lcr", "show", "calls", NULL},
1809 "Shows current calls made by LCR and Asterisk",
1810 "Usage: lcr show calls\n",
1813 static struct ast_cli_entry cli_reload_routing =
1814 { {"lcr", "reload", "routing", NULL},
1816 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
1817 "Usage: lcr reload routing\n",
1820 static struct ast_cli_entry cli_reload_interfaces =
1821 { {"lcr", "reload", "interfaces", NULL},
1822 lcr_reload_interfaces,
1823 "Reloads interfaces conf of LCR",
1824 "Usage: lcr reload interfaces\n",
1827 static struct ast_cli_entry cli_port_block =
1828 { {"lcr", "port", "block", NULL},
1830 "Blocks LCR port for further calls",
1831 "Usage: lcr port block \"<port>\"\n",
1834 static struct ast_cli_entry cli_port_unblock =
1835 { {"lcr", "port", "unblock", NULL},
1837 "Unblocks or loads LCR port, port is opened my mISDN",
1838 "Usage: lcr port unblock \"<port>\"\n",
1841 static struct ast_cli_entry cli_port_unload =
1842 { {"lcr", "port", "unload", NULL},
1844 "Unloads LCR port, port is closes by mISDN",
1845 "Usage: lcr port unload \"<port>\"\n",
1850 * module loading and destruction
1852 int load_module(void)
1856 for (i = 0; i < 256; i++)
1857 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
1858 || (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
1860 ast_mutex_init(&chan_lock);
1861 ast_mutex_init(&log_lock);
1863 if (!(lcr_sock = open_socket())) {
1864 /* continue with closed socket */
1867 if (!bchannel_initialize()) {
1868 CERROR(NULL, NULL, "Unable to open mISDN device\n");
1869 close_socket(lcr_sock);
1874 if (ast_channel_register(&lcr_tech)) {
1875 CERROR(NULL, NULL, "Unable to register channel class\n");
1876 bchannel_deinitialize();
1877 close_socket(lcr_sock);
1882 ast_cli_register(&cli_show_lcr);
1883 ast_cli_register(&cli_show_calls);
1885 ast_cli_register(&cli_reload_routing);
1886 ast_cli_register(&cli_reload_interfaces);
1887 ast_cli_register(&cli_port_block);
1888 ast_cli_register(&cli_port_unblock);
1889 ast_cli_register(&cli_port_unload);
1891 ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
1892 "misdn_set_opt(:<opt><optarg>:<opt><optarg>..):\n"
1893 "Sets mISDN opts. and optargs\n"
1895 "The available options are:\n"
1896 " d - Send display text on called phone, text is the optparam\n"
1897 " n - don't detect dtmf tones on called channel\n"
1898 " h - make digital outgoing call\n"
1899 " c - make crypted outgoing call, param is keyindex\n"
1900 " e - perform echo cancelation on this channel,\n"
1901 " takes taps as arguments (32,64,128,256)\n"
1902 " s - send Non Inband DTMF as inband\n"
1903 " vr - rxgain control\n"
1904 " vt - txgain control\n"
1908 lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1911 //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1915 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
1917 /* failed to create thread */
1918 bchannel_deinitialize();
1919 close_socket(lcr_sock);
1920 ast_channel_unregister(&lcr_tech);
1926 int unload_module(void)
1928 /* First, take us out of the channel loop */
1929 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
1932 pthread_join(chan_tid, NULL);
1934 ast_channel_unregister(&lcr_tech);
1936 if (mISDN_created) {
1937 bchannel_deinitialize();
1941 if (lcr_sock >= 0) {
1949 int reload_module(void)
1956 mutex init fehlt noch
1957 ast_mutex_t usecnt_lock;
1963 ast_mutex_lock(&usecnt_lock);
1965 ast_mutex_unlock(&usecnt_lock);
1971 char *desc="Channel driver for lcr";
1973 char *description(void)
1980 return ASTERISK_GPL_KEY;