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, 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, "[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_user = alaw 3, ulaw 2;
387 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
388 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
389 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
390 send_message(MESSAGE_SETUP, call->ref, &newparam);
392 /* change to outgoing setup state */
393 call->state = CHAN_LCR_STATE_OUT_SETUP;
397 * send dialing info to LCR
398 * this function is called, when setup acknowledge is received and dialing
401 static void send_dialque_to_lcr(struct chan_call *call)
403 union parameter newparam;
405 if (!call->ast || !call->ref || !call->dialque)
408 CDEBUG(call, call->ast, "Sending dial queue to LCR.\n");
410 /* send setup message to LCR */
411 memset(&newparam, 0, sizeof(union parameter));
412 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
413 call->dialque[0] = '\0';
414 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
418 * in case of a bridge, the unsupported message can be forwarded directly
419 * to the remote call.
421 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
425 if (!call->bridge_call) return;
426 CDEBUG(call, NULL, "Sending message due briding.\n");
427 send_message(message_type, call->bridge_call->ref, param);
431 * check if extension matches and start asterisk
432 * if it can match, proceed
435 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
442 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
444 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
447 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
449 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);
453 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
454 /* send setup acknowledge to lcr */
455 memset(&newparam, 0, sizeof(union parameter));
456 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
459 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
464 if (ast_canmatch_extension(ast, ast->context, dad, 1, oad))
466 /* send setup acknowledge to lcr */
467 memset(&newparam, 0, sizeof(union parameter));
468 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
471 call->state = CHAN_LCR_STATE_IN_DIALING;
473 /* if match, start pbx */
474 if (ast_exists_extension(ast, ast->context, dad, 1, oad)) {
475 CDEBUG(call, ast, "Extensions matches.\n");
480 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
488 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
489 memset(&newparam, 0, sizeof(union parameter));
490 newparam.disconnectinfo.cause = cause;
491 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
492 send_message(MESSAGE_RELEASE, call->ref, &newparam);
494 /* release asterisk */
495 ast->hangupcause = call->cause;
496 /* change to release state */
497 call->state = CHAN_LCR_STATE_RELEASE;
498 ast_hangup(ast); // call will be destroyed here
502 /* send setup to asterisk */
503 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
504 ret = ast_pbx_start(ast);
507 cause = (ret==-2)?34:27;
510 call->pbx_started = 1;
515 * incoming setup from LCR
517 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
519 struct ast_channel *ast;
520 union parameter newparam;
522 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %d, dialing %d)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
524 /* create asterisk channel instrance */
525 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
529 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
530 memset(&newparam, 0, sizeof(union parameter));
531 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
532 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
533 send_message(MESSAGE_RELEASE, call->ref, &newparam);
538 /* set ast pointer */
540 ast->tech_pvt = call;
541 ast->tech = &lcr_tech;
542 ast->fds[0] = call->pipe[0];
544 /* fill setup information */
545 if (param->setup.dialinginfo.id)
546 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
547 if (param->setup.context[0])
548 strncpy(ast->context, param->setup.exten, AST_MAX_CONTEXT-1);
549 if (param->setup.callerinfo.id[0])
550 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
551 if (param->setup.callerinfo.name[0])
552 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
555 if (param->setup.redirinfo.id[0])
556 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
558 switch (param->setup.callerinfo.present)
560 case INFO_PRESENT_ALLOWED:
561 ast->cid.cid_pres = AST_PRES_ALLOWED;
563 case INFO_PRESENT_RESTRICTED:
564 ast->cid.cid_pres = AST_PRES_RESTRICTED;
567 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
569 switch (param->setup.callerinfo.ntype)
571 case INFO_NTYPE_SUBSCRIBER:
572 ast->cid.cid_ton = 4;
574 case INFO_NTYPE_NATIONAL:
575 ast->cid.cid_ton = 2;
577 case INFO_NTYPE_INTERNATIONAL:
578 ast->cid.cid_ton = 1;
581 ast->cid.cid_ton = 0;
583 ast->transfercapability = param->setup.capainfo.bearer_capa;
584 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1);
586 /* configure channel */
589 ast->nativeformat = configfile->lawformat;
590 ast->readformat = ast->rawreadformat = configfile->lawformat;
591 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
593 ast->hangupcause = 0;
596 call->state = CHAN_LCR_STATE_IN_SETUP;
598 lcr_start_pbx(call, ast, param->setup.dialinginfo.complete);
602 * incoming setup acknowledge from LCR
604 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
606 if (!call->ast) return;
608 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
610 /* send pending digits in dialque */
612 send_dialque_to_lcr(call);
613 /* change to overlap state */
614 call->state = CHAN_LCR_STATE_OUT_DIALING;
618 * incoming proceeding from LCR
620 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
622 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
625 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
626 /* send event to asterisk */
627 if (call->ast && call->pbx_started)
628 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
632 * incoming alerting from LCR
634 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
636 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
639 call->state = CHAN_LCR_STATE_OUT_ALERTING;
640 /* send event to asterisk */
641 if (call->ast && call->pbx_started)
642 ast_queue_control(call->ast, AST_CONTROL_RINGING);
646 * incoming connect from LCR
648 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
650 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
653 call->state = CHAN_LCR_STATE_CONNECT;
654 /* copy connectinfo */
655 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
656 /* send event to asterisk */
657 if (call->ast && call->pbx_started)
658 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
662 * incoming disconnect from LCR
664 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
666 struct ast_channel *ast = call->ast;
667 union parameter newparam;
669 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param.disconnectinfo.cause);
672 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
674 call->cause = param->disconnectinfo.cause;
675 call->location = param->disconnectinfo.location;
676 /* if bridge, forward disconnect and return */
679 if (call->bridge_call)
681 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
682 bridge_message_if_bridged(call, message_type, param);
686 /* release lcr with same cause */
687 send_message(MESSAGE_RELEASE, call->ref, param);
689 /* change to release state */
690 call->state = CHAN_LCR_STATE_RELEASE;
691 /* release asterisk */
694 ast->hangupcause = call->cause;
695 if (call->pbx_started)
696 ast_queue_hangup(ast);
698 ast_hangup(ast); // call will be destroyed here
704 * incoming setup acknowledge from LCR
706 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
708 struct ast_channel *ast = call->ast;
710 CDEBUG(call, call->ast, "Incomming release from LCR. (cause=%d)\n", param.disconnectinfo.cause);
714 /* change to release state */
715 call->state = CHAN_LCR_STATE_RELEASE;
716 /* copy release info */
719 call->cause = param->disconnectinfo.cause;
720 call->location = param->disconnectinfo.location;
722 /* if we have an asterisk instance, send hangup, else we are done */
725 ast->hangupcause = call->cause;
726 if (call->pbx_started)
727 ast_queue_hangup(ast);
729 ast_hangup(ast); // call will be destroyed here
739 * incoming information from LCR
741 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
746 CDEBUG(call, call->ast, "Incomming information from LCR. (dialing=%d)\n", param.information.id);
748 if (!call->ast) return;
750 /* pbx not started */
751 if (!call->pbx_started)
753 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
754 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
755 lcr_start_pbx(call, ast, param->information.complete);
760 p = param->information.id;
761 if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
763 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
766 /* send digit to asterisk */
767 memset(&fr, 0, sizeof(fr));
768 fr.frametype = AST_FRAME_DTMF;
770 fr.delivery = ast_tv(0, 0);
771 ast_queue_frame(call->ast, &fr);
775 /* use bridge to forware message not supported by asterisk */
776 if (call->state == CHAN_LCR_STATE_CONNECT) {
777 CDEBUG(call, call->ast, "Call is connected, briding.\n");
778 bridge_message_if_bridged(call, message_type, param);
783 * incoming information from LCR
785 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
787 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param.notifyinfo.notify);
789 if (!call->ast) return;
791 /* use bridge to forware message not supported by asterisk */
792 bridge_message_if_bridged(call, message_type, param);
796 * incoming information from LCR
798 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
800 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
802 if (!call->ast) return;
804 /* use bridge to forware message not supported by asterisk */
805 bridge_message_if_bridged(call, message_type, param);
809 * message received from LCR
811 int receive_message(int message_type, unsigned long ref, union parameter *param)
813 union parameter newparam;
814 struct bchannel *bchannel;
815 struct chan_call *call;
817 memset(&newparam, 0, sizeof(union parameter));
819 /* handle bchannel message*/
820 if (message_type == MESSAGE_BCHANNEL)
822 switch(param->bchannel.type)
824 case BCHANNEL_ASSIGN:
825 CDEBUG(NULL, NULL "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
826 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
828 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
831 /* create bchannel */
832 bchannel = alloc_bchannel(param->bchannel.handle);
835 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
839 /* configure channel */
840 bchannel->b_tx_gain = param->bchannel.tx_gain;
841 bchannel->b_rx_gain = param->bchannel.rx_gain;
842 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
843 if (param->bchannel.crypt_len)
845 bchannel->b_crypt_len = param->bchannel.crypt_len;
846 bchannel->b_crypt_type = param->bchannel.crypt_type;
847 memcpy(bchannel->b_crypt_key, param->bchannel.crypt, param->bchannel.crypt_len);
849 bchannel->b_txdata = 0;
850 bchannel->b_dtmf = 1;
851 bchannel->b_tx_dejitter = 1;
853 /* in case, ref is not set, this bchannel instance must
854 * be created until it is removed again by LCR */
856 if ((call = find_call_ref(ref)))
858 bchannel->call = call;
859 call->bchannel = bchannel;
860 #warning hier muesen alle stati gesetzt werden falls sie vor dem b-kanal verfügbar waren
861 if (call->bridge_id) {
862 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
863 bchannel_join(bchannel, call->bridge_id);
866 if (bchannel_create(bchannel))
867 bchannel_activate(bchannel, 1);
870 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
871 newparam.bchannel.handle = param->bchannel.handle;
872 send_message(MESSAGE_BCHANNEL, 0, &newparam);
875 case BCHANNEL_REMOVE:
876 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
877 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
879 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
882 /* unklink from call and destroy bchannel */
883 free_bchannel(bchannel);
886 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
887 newparam.bchannel.handle = param->bchannel.handle;
888 send_message(MESSAGE_BCHANNEL, 0, &newparam);
893 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
899 if (message_type == MESSAGE_NEWREF)
901 if (param->direction)
903 /* new ref from lcr */
904 CDEBUG(NULL, NULL, "Received new ref by LCR, of call from LCR. (ref=%ld)\n", ref);
905 if (!ref || find_call_ref(ref))
907 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
910 /* allocate new call instance */
913 call->state = CHAN_LCR_STATE_IN_PREPARE;
916 /* wait for setup (or release from asterisk) */
919 /* new ref, as requested from this remote application */
920 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
921 call = find_call_ref(0);
924 /* send release, if ref does not exist */
925 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
926 newparam.disconnectinfo.cause = CAUSE_NORMAL;
927 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
928 send_message(MESSAGE_RELEASE, ref, &newparam);
933 /* send pending setup info */
934 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
935 send_setup_to_lcr(call);
936 /* release if asterisk has signed off */
937 else if (call->state == CHAN_LCR_STATE_RELEASE)
942 newparam.disconnectinfo.cause = call->cause;
943 newparam.disconnectinfo.location = call->location;
946 newparam.disconnectinfo.cause = 16;
947 newparam.disconnectinfo.location = 5;
949 send_message(MESSAGE_RELEASE, ref, &newparam);
961 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
964 call = find_call_ref(ref);
967 /* ignore ref that is not used (anymore) */
971 /* handle messages */
975 lcr_in_setup(call, message_type, param);
978 case MESSAGE_OVERLAP:
979 lcr_in_overlap(call, message_type, param);
982 case MESSAGE_PROCEEDING:
983 lcr_in_proceeding(call, message_type, param);
986 case MESSAGE_ALERTING:
987 lcr_in_alerting(call, message_type, param);
990 case MESSAGE_CONNECT:
991 lcr_in_connect(call, message_type, param);
994 case MESSAGE_DISCONNECT:
995 lcr_in_disconnect(call, message_type, param);
998 case MESSAGE_RELEASE:
999 lcr_in_release(call, message_type, param);
1002 case MESSAGE_INFORMATION:
1003 lcr_in_information(call, message_type, param);
1006 case MESSAGE_NOTIFY:
1007 lcr_in_notify(call, message_type, param);
1010 case MESSAGE_FACILITY:
1011 lcr_in_facility(call, message_type, param);
1014 case MESSAGE_PATTERN:
1018 case MESSAGE_NOPATTERN:
1022 case MESSAGE_AUDIOPATH:
1034 * release all calls (due to broken socket)
1036 static void release_all_calls(void)
1038 struct chan_call *call;
1044 /* no ast, so we may directly free call */
1046 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1050 /* already in release process */
1051 if (call->state == CHAN_LCR_STATE_RELEASE) {
1055 /* release or queue release */
1057 call->state = CHAN_LCR_STATE_RELEASE;
1058 if (!call->pbx_started)
1060 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1061 ast_hangup(call->ast); // call will be destroyed here
1064 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1065 ast_queue_hangup(call->ast);
1072 * warning! not thread safe
1073 * returns -1 for socket error, 0 for no work, 1 for work
1075 int handle_socket(void)
1079 struct admin_message msg;
1080 struct admin_list *admin;
1084 #warning SOCKET FEHLT!
1085 /* read from socket */
1086 len = read(sock, &msg, sizeof(msg));
1089 CERROR(NULL, NULL, "Socket closed.\n");
1090 return(-1); // socket closed
1094 if (len != sizeof(msg))
1096 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1097 return(-1); // socket error
1099 if (msg.message != ADMIN_MESSAGE)
1101 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1102 return(-1); // socket error
1104 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1108 if (errno != EWOULDBLOCK)
1110 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1115 /* write to socket */
1118 admin = admin_first;
1119 len = write(sock, &admin->msg, sizeof(msg));
1122 CERROR(NULL, NULL, "Socket closed.\n");
1123 return(-1); // socket closed
1127 if (len != sizeof(msg))
1129 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1130 return(-1); // socket error
1133 admin_first = admin->next;
1139 if (errno != EWOULDBLOCK)
1141 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1150 * open and close socket and thread
1152 int open_socket(void)
1156 char *socket_name = SOCKET_NAME;
1158 struct sockaddr_un sock_address;
1159 unsigned long on = 1;
1160 union parameter param;
1163 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1165 CERROR(NULL, NULL, "Failed to create socket.\n");
1169 /* set socket address and name */
1170 memset(&sock_address, 0, sizeof(sock_address));
1171 sock_address.sun_family = PF_UNIX;
1172 strcpy(sock_address.sun_path, socket_name);
1174 /* connect socket */
1175 if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1178 CERROR(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1182 /* set non-blocking io */
1183 if ((ret = ioctl(sock, FIONBIO, (unsigned char *)(&on))) < 0)
1186 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1190 /* enque hello message */
1191 memset(¶m, 0, sizeof(param));
1192 strcpy(param.hello.application, "asterisk");
1193 send_message(MESSAGE_HELLO, 0, ¶m);
1198 void close_socket(int sock)
1205 static void *chan_thread(void *arg)
1209 union parameter param;
1210 time_t retry = 0, now;
1212 bchannel_pid = getpid();
1214 memset(¶m, 0, sizeof(union parameter));
1218 ast_mutex_lock(&chan_lock);
1225 ret = handle_socket();
1227 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1228 close_socket(lcr_sock);
1230 release_all_calls();
1237 if (retry && now-retry > 5) {
1238 CERROR(NULL, NULL, "Retry to open socket.\n");
1240 if (!(lcr_sock = open_socket())) {
1249 ret = bchannel_handle();
1254 ast_mutex_unlock(&chan_lock);
1256 ast_mutex_lock(&chan_lock);
1260 CERROR(NULL, NULL, "Thread exit.\n");
1262 ast_mutex_unlock(&chan_lock);
1268 * new asterisk instance
1270 static struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1272 union parameter newparam;
1273 struct ast_channel *ast;
1274 struct chan_call *call;
1276 ast_mutex_lock(&chan_lock);
1278 CDEBUG(NULL, NULL, "Received request from Asterisk.\n");
1280 /* if socket is closed */
1283 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1287 /* create call instance */
1288 call = alloc_call();
1291 /* failed to create instance */
1294 /* create asterisk channel instrance */
1295 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1298 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1300 /* failed to create instance */
1304 ast->tech_pvt = call;
1306 ast->tech = &lcr_tech;
1307 /* configure channel */
1308 snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel);
1309 ast->name[sizeof(ast->name)-1] = '\0';
1312 ast->nativeformat = configfile->lawformat;
1313 ast->readformat = ast->rawreadformat = configfile->lawformat;
1314 ast->writeformat = ast->rawwriteformat = configfile->lawformat;
1316 ast->hangupcause = 0;
1317 /* send MESSAGE_NEWREF */
1318 memset(&newparam, 0, sizeof(union parameter));
1319 newparam.direction = 0; /* request from app */
1320 send_message(MESSAGE_NEWREF, 0, &newparam);
1322 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1324 ast_mutex_unlock(&chan_lock);
1330 * call from asterisk
1332 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1334 struct chan_call *call;
1336 ast_mutex_lock(&chan_lock);
1337 call = ast->tech_pvt;
1339 CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n");
1340 ast_mutex_unlock(&chan_lock);
1344 CDEBUG(call, ast, "Received call from Asterisk.\n");
1346 call->pbx_started = 1;
1348 /* send setup message, if we already have a callref */
1350 send_setup_to_lcr(call);
1352 ast_mutex_unlock(&chan_lock);
1356 static int lcr_digit(struct ast_channel *ast, char digit)
1358 struct chan_call *call;
1359 union parameter newparam;
1362 /* only pass IA5 number space */
1363 if (digit > 126 || digit < 32)
1366 ast_mutex_lock(&chan_lock);
1367 call = ast->tech_pvt;
1369 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1370 ast_mutex_unlock(&chan_lock);
1374 CDEBUG(call, ast, "Received digit Asterisk.\n");
1376 /* send information or queue them */
1377 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1379 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1380 memset(&newparam, 0, sizeof(union parameter));
1381 newparam.information.id[0] = digit;
1382 newparam.information.id[1] = '\0';
1383 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1386 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP));
1388 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1390 strncat(call->dialque, buf, strlen(call->dialque)-1);
1393 ast_mutex_unlock(&chan_lock);
1398 static int lcr_answer(struct ast_channel *ast)
1400 union parameter newparam;
1401 struct chan_call *call;
1403 ast_mutex_lock(&chan_lock);
1404 call = ast->tech_pvt;
1406 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1407 ast_mutex_unlock(&chan_lock);
1411 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1413 /* copy connectinfo, if bridged */
1414 if (call->bridge_call)
1415 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1416 /* send connect message to lcr */
1417 memset(&newparam, 0, sizeof(union parameter));
1418 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1419 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1421 call->state = CHAN_LCR_STATE_CONNECT;
1423 ast_mutex_unlock(&chan_lock);
1427 static int lcr_hangup(struct ast_channel *ast)
1429 union parameter newparam;
1430 struct chan_call *call;
1431 pthread_t tid = pthread_self();
1433 if (!pthread_equal(tid, call_tid))
1434 ast_mutex_lock(&chan_lock);
1435 call = ast->tech_pvt;
1437 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1438 if (!pthread_equal(tid, call_tid))
1439 ast_mutex_unlock(&chan_lock);
1443 if (!pthread_equal(tid, call_tid))
1444 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1446 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1448 /* disconnect asterisk, maybe not required */
1449 ast->tech_pvt = NULL;
1454 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1455 memset(&newparam, 0, sizeof(union parameter));
1456 newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
1457 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1458 send_message(MESSAGE_RELEASE, call->ref, &newparam);
1461 if (!pthread_equal(tid, call_tid))
1462 ast_mutex_unlock(&chan_lock);
1466 /* ref is not set, due to prepare setup or release */
1467 if (call->state == CHAN_LCR_STATE_RELEASE)
1469 /* we get the response to our release */
1470 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1474 /* during prepare, we change to release state */
1475 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1476 call->state = CHAN_LCR_STATE_RELEASE;
1479 if (!pthread_equal(tid, call_tid))
1480 ast_mutex_unlock(&chan_lock);
1484 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1486 struct chan_call *call;
1487 unsigned char *buffer[1024], *s, *d = buffer;
1489 ast_mutex_lock(&chan_lock);
1490 call = ast->tech_pvt;
1492 ast_mutex_unlock(&chan_lock);
1495 if (call->bchannel && ((ii = f->samples)))
1497 if (ii > sizeof(buffer))
1500 for (i = 0, i < ii, i++)
1501 *d++ = flip_bits[*s++];
1502 bchannel_transmit(call->bchannel, buffer, ii);
1504 ast_mutex_unlock(&chan_lock);
1509 static struct ast_frame *lcr_read(struct ast_channel *ast)
1511 struct chan_call *call;
1515 ast_mutex_lock(&chan_lock);
1516 call = ast->tech_pvt;
1518 ast_mutex_unlock(&chan_lock);
1521 len = read(call->pipe[0], call->read_buf, sizeof(call->read_buf));
1526 for (i = 0, i < len, i++) {
1531 call->read_fr.frametype = AST_FRAME_SPEECH;
1535 call->read_fr.subtype = AST_FORMAT_ALAW;
1536 call->read_fr.datalen = len;
1537 call->read_fr.samples = len;
1538 call->read_fr.delivery = ast_tv(0,0);
1539 call->read_fr.data = call->read_buf;
1540 ast_mutex_unlock(&chan_lock);
1542 return &call->read_fr;
1545 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1547 union parameter newparam;
1549 struct chan_call *call;
1551 ast_mutex_lock(&chan_lock);
1552 call = ast->tech_pvt;
1554 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1555 ast_mutex_unlock(&chan_lock);
1560 case AST_CONTROL_BUSY:
1561 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1562 /* send message to lcr */
1563 memset(&newparam, 0, sizeof(union parameter));
1564 newparam.disconnectinfo.cause = 17;
1565 newparam.disconnectinfo.location = 5;
1566 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1568 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1570 ast_mutex_unlock(&chan_lock);
1572 case AST_CONTROL_CONGESTION:
1573 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk.\n");
1575 ast_mutex_unlock(&chan_lock);
1577 case AST_CONTROL_RINGING:
1578 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1579 /* send message to lcr */
1580 memset(&newparam, 0, sizeof(union parameter));
1581 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1583 call->state = CHAN_LCR_STATE_OUT_ALERTING;
1585 ast_mutex_unlock(&chan_lock);
1589 ast_mutex_unlock(&chan_lock);
1592 case AST_CONTROL_VIDUPDATE:
1595 case AST_CONTROL_HOLD:
1596 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1597 /* send message to lcr */
1598 memset(&newparam, 0, sizeof(union parameter));
1599 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1600 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1602 case AST_CONTROL_UNHOLD:
1603 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1604 /* send message to lcr */
1605 memset(&newparam, 0, sizeof(union parameter));
1606 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1607 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1611 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1613 ast_mutex_unlock(&chan_lock);
1618 ast_mutex_unlock(&chan_lock);
1625 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
1626 struct ast_channel *ast2, int flags,
1627 struct ast_frame **fo,
1628 struct ast_channel **rc, int timeoutms)
1631 struct chan_call *call1, *call2;
1632 struct ast_channel *carr[2], *who;
1634 struct ast_frame *f;
1637 CDEBUG(NULL, ast, "Received briding request from Asterisk.\n");
1639 /* join via dsp (if the channels are currently open) */
1640 ast_mutex_lock(&chan_lock);
1641 bridge_id = new_bridge_id();
1642 call1 = ast1->tech_pvt;
1643 call2 = ast2->tech_pvt;
1646 call1->bridge_id = bridge_id;
1647 if (call1->bchannel)
1648 bchannel_join(call1->bchannel, bridge_id);
1649 call1->bridge_call = call2;
1653 call2->bridge_id = bridge_id;
1654 if (call2->bchannel)
1655 bchannel_join(call2->bchannel, bridge_id);
1656 call2->bridge_call = call1;
1658 ast_mutex_unlock(&chan_lock);
1661 who = ast_waitfor_n(carr, 2, &to);
1664 CDEBUG(NULL, ast, "Empty read on bridge, breaking out.\n");
1669 if (!f || f->frametype == AST_FRAME_CONTROL) {
1676 if ( f->frametype == AST_FRAME_DTMF ) {
1682 #warning kann einfach gesendet werden. dsp slittet automatisch, wenn frames kommen, bis der fifo leer ist.
1684 if (f->frametype == AST_FRAME_VOICE) {
1699 CDEBUG(NULL, ast, "Releasing bride.\n");
1701 /* split channels */
1702 ast_mutex_lock(&chan_lock);
1703 call1 = ast1->tech_pvt;
1704 call2 = ast2->tech_pvt;
1707 call1->bridge_id = 0;
1708 if (call1->bchannel)
1709 bchannel_join(call1->bchannel, 0);
1710 if (call1->bridge_call)
1711 call1->bridge_call->bridge_call = NULL;
1712 call1->bridge_call = NULL;
1716 call2->bridge_id = 0;
1717 if (call2->bchannel)
1718 bchannel_join(call2->bchannel, 0);
1719 if (call2->bridge_call)
1720 call2->bridge_call->bridge_call = NULL;
1721 call2->bridge_call = NULL;
1723 ast_mutex_unlock(&chan_lock);
1726 return AST_BRIDGE_COMPLETE;
1728 static struct ast_channel_tech lcr_tech = {
1730 .description="Channel driver for connecting to Linux-Call-Router",
1734 .capabilities=AST_FORMAT_ALAW,
1735 .requester=lcr_request,
1736 .send_digit_begin=lcr_digit,
1743 .indicate=lcr_indicate,
1744 // .fixup=lcr_fixup,
1745 // .send_text=lcr_send_text,
1753 static int lcr_show_lcr (int fd, int argc, char *argv[])
1758 static int lcr_show_calls (int fd, int argc, char *argv[])
1763 static int lcr_reload_routing (int fd, int argc, char *argv[])
1768 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
1773 static int lcr_port_block (int fd, int argc, char *argv[])
1778 static int lcr_port_unblock (int fd, int argc, char *argv[])
1783 static int lcr_port_unload (int fd, int argc, char *argv[])
1788 static struct ast_cli_entry cli_show_lcr =
1789 { {"lcr", "show", "lcr", NULL},
1791 "Shows current states of LCR core",
1792 "Usage: lcr show lcr\n",
1795 static struct ast_cli_entry cli_show_calls =
1796 { {"lcr", "show", "calls", NULL},
1798 "Shows current calls made by LCR and Asterisk",
1799 "Usage: lcr show calls\n",
1802 static struct ast_cli_entry cli_reload_routing =
1803 { {"lcr", "reload", "routing", NULL},
1805 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
1806 "Usage: lcr reload routing\n",
1809 static struct ast_cli_entry cli_reload_interfaces =
1810 { {"lcr", "reload", "interfaces", NULL},
1811 lcr_reload_interfaces,
1812 "Reloads interfaces conf of LCR",
1813 "Usage: lcr reload interfaces\n",
1816 static struct ast_cli_entry cli_port_block =
1817 { {"lcr", "port", "block", NULL},
1819 "Blocks LCR port for further calls",
1820 "Usage: lcr port block \"<port>\"\n",
1823 static struct ast_cli_entry cli_port_unblock =
1824 { {"lcr", "port", "unblock", NULL},
1826 "Unblocks or loads LCR port, port is opened my mISDN",
1827 "Usage: lcr port unblock \"<port>\"\n",
1830 static struct ast_cli_entry cli_port_unload =
1831 { {"lcr", "port", "unload", NULL},
1833 "Unloads LCR port, port is closes by mISDN",
1834 "Usage: lcr port unload \"<port>\"\n",
1839 * module loading and destruction
1841 int load_module(void)
1845 for (i = 0, i < 256, i++)
1846 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
1847 || = (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
1849 ast_mutex_init(&chan_lock);
1850 ast_mutex_init(&log_lock);
1852 if (!(lcr_sock = open_socket())) {
1853 /* continue with closed socket */
1856 if (!bchannel_initialize()) {
1857 CERROR(NULL, NULL, "Unable to open mISDN device\n");
1858 close_socket(lcr_sock);
1863 if (ast_channel_register(&lcr_tech)) {
1864 CERROR(NULL, NULL, "Unable to register channel class\n");
1865 bchannel_deinitialize();
1866 close_socket(lcr_sock);
1871 ast_cli_register(&cli_show_lcr);
1872 ast_cli_register(&cli_show_calls);
1874 ast_cli_register(&cli_reload_routing);
1875 ast_cli_register(&cli_reload_interfaces);
1876 ast_cli_register(&cli_port_block);
1877 ast_cli_register(&cli_port_unblock);
1878 ast_cli_register(&cli_port_unload);
1880 ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
1881 "misdn_set_opt(:<opt><optarg>:<opt><optarg>..):\n"
1882 "Sets mISDN opts. and optargs\n"
1884 "The available options are:\n"
1885 " d - Send display text on called phone, text is the optparam\n"
1886 " n - don't detect dtmf tones on called channel\n"
1887 " h - make digital outgoing call\n"
1888 " c - make crypted outgoing call, param is keyindex\n"
1889 " e - perform echo cancelation on this channel,\n"
1890 " takes taps as arguments (32,64,128,256)\n"
1891 " s - send Non Inband DTMF as inband\n"
1892 " vr - rxgain control\n"
1893 " vt - txgain control\n"
1897 lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1900 //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
1904 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
1906 /* failed to create thread */
1907 bchannel_deinitialize();
1908 close_socket(lcr_sock);
1909 ast_channel_unregister(&lcr_tech);
1915 int unload_module(void)
1917 /* First, take us out of the channel loop */
1918 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
1921 pthread_join(chan_tid, NULL);
1923 ast_channel_unregister(&lcr_tech);
1925 if (mISDN_created) {
1926 bchannel_deinitialize();
1930 if (lcr_sock >= 0) {
1938 static int reload_module(void)
1945 mutex init fehlt noch
1946 ast_mutex_t usecnt_lock;
1952 ast_mutex_lock(&usecnt_lock);
1954 ast_mutex_unlock(&usecnt_lock);
1960 char *desc="Channel driver for lcr";
1962 char *description(void)
1969 return ASTERISK_GPL_KEY;
1972 #define AST_MODULE "chan_lcr"
1973 AST_MODULE_INFO(ASTERISK_GPL_KEY,
1974 AST_MODFLAG_DEFAULT,
1975 "Channel driver for LCR",
1976 .load = load_module,
1977 .unload = unload_module,
1978 .reload = reload_module,