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 requested from Asterisk, a new chan_call instance is created.
35 The new Asterisk instance pointer (ast) is stored to chan_call structure.
36 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
37 If the call is received (lcr_call) A MESSASGE_NEWREF is sent to LCR requesting
38 a new call reference (ref).
39 Further dialing information is queued.
40 After the new callref is received by special MESSAGE_NEWREF reply, new ref
41 is stored in the chan_call structure.
42 The setup information is sent to LCR using MESSAGE_SETUP.
43 The state changes to CHAN_LCR_STATE_OUT_SETUP.
48 During call process, messages are received and sent.
49 The state changes accordingly.
50 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
51 If a MESSAGE_OVERLAP is received, further dialing is required.
52 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
53 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
56 Call is released by LCR:
58 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
59 The current ref is set to 0, to indicate released reference.
60 The state changes to CHAN_LCR_STATE_RELEASE.
61 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
62 the chan_call instance is destroyed.
63 After lcr_hangup() is called-back by Asterisk, the chan_call instance
64 is destroyed, because the current ref is set to 0 and the state equals
65 CHAN_LCR_STATE_RELEASE.
66 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
67 "Call is released by Asterisk".
70 Call is released by Asterisk:
72 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
73 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
74 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
75 set to CHAN_LCR_STATE_RELEASE.
76 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
77 LCR and the chan_call instance is destroyed.
78 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
79 "Call is released by LCR".
88 #include <sys/types.h>
93 #include <sys/ioctl.h>
94 #include <sys/socket.h>
97 #include <semaphore.h>
99 #include <asterisk/module.h>
100 #include <asterisk/channel.h>
101 #include <asterisk/config.h>
102 #include <asterisk/logger.h>
103 #include <asterisk/pbx.h>
104 #include <asterisk/options.h>
105 #include <asterisk/io.h>
106 #include <asterisk/frame.h>
107 #include <asterisk/translate.h>
108 #include <asterisk/cli.h>
109 #include <asterisk/musiconhold.h>
110 #include <asterisk/dsp.h>
111 #include <asterisk/translate.h>
112 #include <asterisk/file.h>
113 #include <asterisk/callerid.h>
114 #include <asterisk/indications.h>
115 #include <asterisk/app.h>
116 #include <asterisk/features.h>
117 #include <asterisk/sched.h>
119 #include "extension.h"
121 #include "callerid.h"
122 #include "lcrsocket.h"
124 #include "bchannel.h"
126 #include "chan_lcr.h"
128 CHAN_LCR_STATE // state description structure
129 MESSAGES // message text
131 unsigned char flip_bits[256];
136 char lcr_type[]="lcr";
139 ast_mutex_t chan_lock; /* global lock */
140 ast_mutex_t log_lock; /* logging log */
143 int glob_channel = 0;
148 struct admin_list *next;
149 struct admin_message msg;
150 } *admin_first = NULL;
152 static struct ast_channel_tech lcr_tech;
157 void chan_lcr_log(int type, const char *file, int line, const char *function, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
160 char call_text[128] = "NULL";
161 char ast_text[128] = "NULL";
164 ast_mutex_lock(&log_lock);
167 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
168 buffer[sizeof(buffer)-1]=0;
172 sprintf(call_text, "%ld", call->ref);
174 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
175 ast_text[sizeof(ast_text)-1] = '\0';
177 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
179 ast_mutex_unlock(&log_lock);
183 * channel and call instances
185 struct chan_call *call_first;
187 struct chan_call *find_call_ref(unsigned long ref)
189 struct chan_call *call = call_first;
193 if (call->ref == ref)
201 struct chan_call *find_call_ast(struct ast_channel *ast)
203 struct chan_call *call = call_first;
207 if (call->ast == ast)
214 struct chan_call *find_call_handle(unsigned long handle)
216 struct chan_call *call = call_first;
220 if (call->bchannel_handle == handle)
228 void free_call(struct chan_call *call)
230 struct chan_call **temp = &call_first;
236 *temp = (*temp)->next;
237 if (call->pipe[0] > -1)
238 close(call->pipe[0]);
239 if (call->pipe[1] > -1)
240 close(call->pipe[1]);
243 if (call->bchannel->call != call)
244 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
245 call->bchannel->call = NULL;
247 if (call->bridge_call)
249 if (call->bridge_call->bridge_call != call)
250 CERROR(call, NULL, "Linked call structure has no link to us.\n");
251 call->bridge_call->bridge_call = NULL;
253 CDEBUG(call, NULL, "Call instance freed.\n");
257 temp = &((*temp)->next);
259 CERROR(call, NULL, "Call instance not found in list.\n");
262 struct chan_call *alloc_call(void)
264 struct chan_call **callp = &call_first;
267 callp = &((*callp)->next);
269 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
271 memset(*callp, 0, sizeof(struct chan_call));
272 if (pipe((*callp)->pipe) < 0) {
273 CERROR(*callp, NULL, "Failed to create pipe.\n");
277 CDEBUG(*callp, NULL, "Call instance allocated.\n");
282 unsigned short new_bridge_id(void)
284 struct chan_call *call;
285 unsigned short id = 1;
287 /* search for lowest bridge id that is not in use and not 0 */
293 if (call->bridge_id == id)
301 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
306 * enque message to LCR
308 int send_message(int message_type, unsigned long ref, union parameter *param)
310 struct admin_list *admin, **adminp;
313 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
316 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
318 adminp = &admin_first;
320 adminp = &((*adminp)->next);
321 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
323 CERROR(NULL, NULL, "No memory for message to LCR.\n");
328 admin->msg.message = ADMIN_MESSAGE;
329 admin->msg.u.msg.type = message_type;
330 admin->msg.u.msg.ref = ref;
331 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
337 * apply options (in locked state)
339 void apply_opt(struct chan_call *call, char *data)
341 union parameter newparam;
342 char string[1024], *p = string, *opt, *key;
343 int gain, i, newmode = 0;
345 strncpy(string, data, sizeof(string)-1);
346 string[sizeof(string)-1] = '\0';
349 while((opt = strsep(&p, ":")))
353 if (opt[1] == '\0') {
354 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
357 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
358 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
359 strncpy(call->display, opt+1, sizeof(call->display)-1);
361 memset(&newparam, 0, sizeof(union parameter));
362 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
363 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
367 if (opt[1] != '\0') {
368 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
371 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
375 if (opt[1] == '\0') {
376 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
380 /* check for 0xXXXX... type of key */
381 if (!!strncmp((char *)key, "0x", 2)) {
382 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
386 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
387 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
393 if (*key>='0' && *key<='9')
394 call->bf_key[i] = (*key-'0') << 8;
395 else if (*key>='a' && *key<='f')
396 call->bf_key[i] = (*key-'a'+10) << 8;
397 else if (*key>='A' && *key<='F')
398 call->bf_key[i] = (*key-'A'+10) << 8;
402 if (*key>='0' && *key<='9')
403 call->bf_key[i] += (*key - '0');
404 else if (*key>='a' && *key<='f')
405 call->bf_key[i] += (*key - 'a' + 10);
406 else if (*key>='A' && *key<='F')
407 call->bf_key[i] += (*key - 'A' + 10);
414 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
418 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
420 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
423 if (opt[1] != '\0') {
424 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
427 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
434 if (opt[1] != '\0') {
435 CERROR(call, call->ast, "Option 't' (transparent) expects no parameter.\n", opt);
438 CDEBUG(call, call->ast, "Option 't' (transparent).\n");
439 if (!call->transparent) {
440 call->transparent = 1;
445 if (opt[1] == '\0') {
446 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
449 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
450 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
452 bchannel_pipeline(call->bchannel, call->pipeline);
456 if (opt[1] != '\0') {
457 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
460 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
461 call->inband_dtmf = 1;
466 if (opt[1] != 'r' && opt[1] != 't') {
467 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
471 if (gain < -8 || gain >8) {
472 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
475 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
477 call->rx_gain = gain;
479 bchannel_gain(call->bchannel, call->rx_gain, 0);
481 call->tx_gain = gain;
483 bchannel_gain(call->bchannel, call->tx_gain, 1);
487 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
491 /* re-open, if bchannel is created */
492 if (call->bchannel && call->bchannel->b_sock > -1) {
493 bchannel_destroy(call->bchannel);
494 if (bchannel_create(call->bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
495 bchannel_activate(call->bchannel, 1);
500 * send setup info to LCR
501 * this function is called, when asterisk call is received and ref is received
503 static void send_setup_to_lcr(struct chan_call *call)
505 union parameter newparam;
506 struct ast_channel *ast = call->ast;
508 if (!call->ast || !call->ref)
511 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s)\n", call->interface, call->dialstring);
513 /* send setup message to LCR */
514 memset(&newparam, 0, sizeof(union parameter));
515 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
516 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
517 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
518 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
519 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
520 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
521 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
522 call->display[0] = '\0';
523 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
524 strncpy(newparam.setup.callerinfo.id, ast->cid.cid_num, sizeof(newparam.setup.callerinfo.id)-1);
525 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
526 strncpy(newparam.setup.callerinfo.name, ast->cid.cid_name, sizeof(newparam.setup.callerinfo.name)-1);
527 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
529 strncpy(newparam.setup.redirinfo.id, ast->cid.cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
530 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
531 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
533 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
535 case AST_PRES_ALLOWED:
536 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
538 case AST_PRES_RESTRICTED:
539 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
541 case AST_PRES_UNAVAILABLE:
542 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
545 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
547 switch(ast->cid.cid_ton)
550 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
553 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
556 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
559 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
561 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
562 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
563 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
564 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
565 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
566 send_message(MESSAGE_SETUP, call->ref, &newparam);
568 /* change to outgoing setup state */
569 call->state = CHAN_LCR_STATE_OUT_SETUP;
573 * send dialing info to LCR
574 * this function is called, when setup acknowledge is received and dialing
577 static void send_dialque_to_lcr(struct chan_call *call)
579 union parameter newparam;
581 if (!call->ast || !call->ref || !call->dialque[0])
584 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
586 /* send setup message to LCR */
587 memset(&newparam, 0, sizeof(union parameter));
588 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
589 call->dialque[0] = '\0';
590 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
594 * in case of a bridge, the unsupported message can be forwarded directly
595 * to the remote call.
597 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
601 if (!call->bridge_call) return;
602 CDEBUG(call, NULL, "Sending message due briding.\n");
603 send_message(message_type, call->bridge_call->ref, param);
607 * send release message to LCR and import bchannel if exported
609 static void send_release_and_import(struct chan_call *call, int cause, int location)
611 union parameter newparam;
613 /* importing channel */
614 if (call->bchannel) {
615 memset(&newparam, 0, sizeof(union parameter));
616 newparam.bchannel.type = BCHANNEL_RELEASE;
617 newparam.bchannel.handle = call->bchannel->handle;
618 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
620 /* sending release */
621 memset(&newparam, 0, sizeof(union parameter));
622 newparam.disconnectinfo.cause = cause;
623 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
624 send_message(MESSAGE_RELEASE, call->ref, &newparam);
628 * check if extension matches and start asterisk
629 * if it can match, proceed
632 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
635 union parameter newparam;
637 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", ast->exten, ast->context, complete?"yes":"no");
642 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
644 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
648 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
650 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);
654 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
655 /* send setup acknowledge to lcr */
656 memset(&newparam, 0, sizeof(union parameter));
657 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
660 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
665 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
667 /* send setup acknowledge to lcr */
668 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
669 memset(&newparam, 0, sizeof(union parameter));
670 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
674 call->state = CHAN_LCR_STATE_IN_DIALING;
676 /* if match, start pbx */
677 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
678 CDEBUG(call, ast, "Extensions matches.\n");
683 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
691 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
692 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
694 /* release asterisk */
695 ast->hangupcause = call->cause;
696 /* change to release state */
697 call->state = CHAN_LCR_STATE_RELEASE;
698 ast_hangup(ast); // call will be destroyed here
702 /* send setup to asterisk */
703 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
704 ret = ast_pbx_start(ast);
707 cause = (ret==-2)?34:27;
710 call->pbx_started = 1;
715 * incoming setup from LCR
717 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
719 struct ast_channel *ast;
721 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
723 /* create asterisk channel instrance */
724 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
728 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
729 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
736 ast->tech_pvt = call;
737 ast->tech = &lcr_tech;
738 ast->fds[0] = call->pipe[0];
740 /* fill setup information */
741 if (param->setup.dialinginfo.id)
742 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
743 if (param->setup.context[0])
744 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
746 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
747 if (param->setup.callerinfo.id[0])
748 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
749 if (param->setup.callerinfo.name[0])
750 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
751 if (param->setup.redirinfo.id[0])
752 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
753 switch (param->setup.callerinfo.present)
755 case INFO_PRESENT_ALLOWED:
756 ast->cid.cid_pres = AST_PRES_ALLOWED;
758 case INFO_PRESENT_RESTRICTED:
759 ast->cid.cid_pres = AST_PRES_RESTRICTED;
762 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
764 switch (param->setup.callerinfo.ntype)
766 case INFO_NTYPE_SUBSCRIBER:
767 ast->cid.cid_ton = 4;
769 case INFO_NTYPE_NATIONAL:
770 ast->cid.cid_ton = 2;
772 case INFO_NTYPE_INTERNATIONAL:
773 ast->cid.cid_ton = 1;
776 ast->cid.cid_ton = 0;
778 ast->transfercapability = param->setup.capainfo.bearer_capa;
779 /* enable hdlc if transcap is data */
780 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
781 || ast->transfercapability == INFO_BC_DATARESTRICTED
782 || ast->transfercapability == INFO_BC_VIDEO)
784 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
786 /* configure channel */
787 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
788 ast->readformat = ast->rawreadformat = ast->nativeformats;
789 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
791 ast->hangupcause = 0;
794 call->state = CHAN_LCR_STATE_IN_SETUP;
796 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
800 * incoming setup acknowledge from LCR
802 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
804 if (!call->ast) return;
806 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
808 /* send pending digits in dialque */
809 if (call->dialque[0])
810 send_dialque_to_lcr(call);
811 /* change to overlap state */
812 call->state = CHAN_LCR_STATE_OUT_DIALING;
816 * incoming proceeding from LCR
818 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
820 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
823 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
824 /* send event to asterisk */
825 if (call->ast && call->pbx_started)
826 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
830 * incoming alerting from LCR
832 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
834 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
837 call->state = CHAN_LCR_STATE_OUT_ALERTING;
838 /* send event to asterisk */
839 if (call->ast && call->pbx_started)
840 ast_queue_control(call->ast, AST_CONTROL_RINGING);
844 * incoming connect from LCR
846 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
848 union parameter newparam;
850 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
853 call->state = CHAN_LCR_STATE_CONNECT;
854 /* request bchannel */
855 if (!call->bchannel) {
856 CDEBUG(call, call->ast, "Requesting B-channel.\n");
857 memset(&newparam, 0, sizeof(union parameter));
858 newparam.bchannel.type = BCHANNEL_REQUEST;
859 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
861 /* copy connectinfo */
862 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
863 /* send event to asterisk */
864 if (call->ast && call->pbx_started)
865 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
869 * incoming disconnect from LCR
871 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
873 struct ast_channel *ast = call->ast;
875 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
878 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
880 call->cause = param->disconnectinfo.cause;
881 call->location = param->disconnectinfo.location;
882 /* if bridge, forward disconnect and return */
885 if (call->bridge_call)
887 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
888 bridge_message_if_bridged(call, message_type, param);
892 /* release lcr with same cause */
893 send_release_and_import(call, call->cause, call->location);
895 /* change to release state */
896 call->state = CHAN_LCR_STATE_RELEASE;
897 /* release asterisk */
900 ast->hangupcause = call->cause;
901 if (call->pbx_started)
902 ast_queue_hangup(ast);
904 ast_hangup(ast); // call will be destroyed here
910 * incoming setup acknowledge from LCR
912 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
914 struct ast_channel *ast = call->ast;
916 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
920 /* change to release state */
921 call->state = CHAN_LCR_STATE_RELEASE;
922 /* copy release info */
925 call->cause = param->disconnectinfo.cause;
926 call->location = param->disconnectinfo.location;
928 /* if we have an asterisk instance, send hangup, else we are done */
931 ast->hangupcause = call->cause;
932 if (call->pbx_started)
933 ast_queue_hangup(ast);
935 ast_hangup(ast); // call will be destroyed here
945 * incoming information from LCR
947 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
949 struct ast_channel *ast = call->ast;
953 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
957 /* pbx not started */
958 if (!call->pbx_started)
960 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
961 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
962 lcr_start_pbx(call, ast, param->information.sending_complete);
967 p = param->information.id;
968 if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
970 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
973 /* send digit to asterisk */
974 memset(&fr, 0, sizeof(fr));
975 fr.frametype = AST_FRAME_DTMF;
977 fr.delivery = ast_tv(0, 0);
978 ast_queue_frame(call->ast, &fr);
982 /* use bridge to forware message not supported by asterisk */
983 if (call->state == CHAN_LCR_STATE_CONNECT) {
984 CDEBUG(call, call->ast, "Call is connected, briding.\n");
985 bridge_message_if_bridged(call, message_type, param);
990 * incoming information from LCR
992 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
994 union parameter newparam;
996 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
998 /* request bchannel, if call is resumed and we don't have it */
999 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1000 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1001 memset(&newparam, 0, sizeof(union parameter));
1002 newparam.bchannel.type = BCHANNEL_REQUEST;
1003 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1006 if (!call->ast) return;
1008 /* use bridge to forware message not supported by asterisk */
1009 bridge_message_if_bridged(call, message_type, param);
1013 * incoming information from LCR
1015 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1017 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1019 if (!call->ast) return;
1021 /* use bridge to forware message not supported by asterisk */
1022 bridge_message_if_bridged(call, message_type, param);
1026 * got dtmf from bchannel
1028 void lcr_in_dtmf(struct chan_call *call, int val)
1030 struct ast_channel *ast = call->ast;
1031 struct ast_frame fr;
1035 if (!call->pbx_started)
1038 CDEBUG(call, call->ast, "Frowarding DTMF digit '%c' to Asterisk.\n", val);
1040 /* send digit to asterisk */
1041 memset(&fr, 0, sizeof(fr));
1042 fr.frametype = AST_FRAME_DTMF;
1044 fr.delivery = ast_tv(0, 0);
1045 ast_queue_frame(call->ast, &fr);
1049 * message received from LCR
1051 int receive_message(int message_type, unsigned long ref, union parameter *param)
1053 struct bchannel *bchannel;
1054 struct chan_call *call;
1055 union parameter newparam;
1057 memset(&newparam, 0, sizeof(union parameter));
1059 /* handle bchannel message*/
1060 if (message_type == MESSAGE_BCHANNEL)
1062 switch(param->bchannel.type)
1064 case BCHANNEL_ASSIGN:
1065 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
1066 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1068 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1071 /* create bchannel */
1072 bchannel = alloc_bchannel(param->bchannel.handle);
1075 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1079 /* configure channel */
1080 bchannel->b_tx_gain = param->bchannel.tx_gain;
1081 bchannel->b_rx_gain = param->bchannel.rx_gain;
1082 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1083 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1085 bchannel->b_bf_len = param->bchannel.crypt_len;
1086 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1088 bchannel->b_txdata = 0;
1089 bchannel->b_dtmf = 1;
1090 bchannel->b_tx_dejitter = 1;
1092 /* in case, ref is not set, this bchannel instance must
1093 * be created until it is removed again by LCR */
1095 call = find_call_ref(ref);
1098 bchannel->call = call;
1099 call->bchannel = bchannel;
1101 bchannel_dtmf(bchannel, 1);
1103 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1104 if (call->pipeline[0])
1105 bchannel_pipeline(bchannel, call->pipeline);
1107 bchannel_gain(bchannel, call->rx_gain, 0);
1109 bchannel_gain(bchannel, call->tx_gain, 1);
1110 if (call->bridge_id) {
1111 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1112 bchannel_join(bchannel, call->bridge_id);
1115 if (bchannel_create(bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
1116 bchannel_activate(bchannel, 1);
1118 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1119 newparam.bchannel.handle = param->bchannel.handle;
1120 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1121 /* if call has released before bchannel is assigned */
1123 newparam.bchannel.type = BCHANNEL_RELEASE;
1124 newparam.bchannel.handle = param->bchannel.handle;
1125 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1130 case BCHANNEL_REMOVE:
1131 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1132 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1134 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1137 /* unklink from call and destroy bchannel */
1138 free_bchannel(bchannel);
1141 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1142 newparam.bchannel.handle = param->bchannel.handle;
1143 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1148 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1153 /* handle new ref */
1154 if (message_type == MESSAGE_NEWREF)
1156 if (param->direction)
1158 /* new ref from lcr */
1159 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1160 if (!ref || find_call_ref(ref))
1162 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1165 /* allocate new call instance */
1166 call = alloc_call();
1168 call->state = CHAN_LCR_STATE_IN_PREPARE;
1171 /* wait for setup (or release from asterisk) */
1174 /* new ref, as requested from this remote application */
1175 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1176 call = find_call_ref(0);
1179 /* send release, if ref does not exist */
1180 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1181 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1186 /* send pending setup info */
1187 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1188 send_setup_to_lcr(call);
1189 /* release if asterisk has signed off */
1190 else if (call->state == CHAN_LCR_STATE_RELEASE)
1194 send_release_and_import(call, call->cause, call->location);
1196 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1208 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1211 call = find_call_ref(ref);
1214 /* ignore ref that is not used (anymore) */
1215 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1219 /* handle messages */
1220 switch(message_type)
1223 lcr_in_setup(call, message_type, param);
1226 case MESSAGE_OVERLAP:
1227 lcr_in_overlap(call, message_type, param);
1230 case MESSAGE_PROCEEDING:
1231 lcr_in_proceeding(call, message_type, param);
1234 case MESSAGE_ALERTING:
1235 lcr_in_alerting(call, message_type, param);
1238 case MESSAGE_CONNECT:
1239 lcr_in_connect(call, message_type, param);
1242 case MESSAGE_DISCONNECT:
1243 lcr_in_disconnect(call, message_type, param);
1246 case MESSAGE_RELEASE:
1247 lcr_in_release(call, message_type, param);
1250 case MESSAGE_INFORMATION:
1251 lcr_in_information(call, message_type, param);
1254 case MESSAGE_NOTIFY:
1255 lcr_in_notify(call, message_type, param);
1258 case MESSAGE_FACILITY:
1259 lcr_in_facility(call, message_type, param);
1262 case MESSAGE_PATTERN: // audio available from LCR
1265 case MESSAGE_NOPATTERN: // audio not available from LCR
1268 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1269 call->audiopath = param->audiopath;
1273 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1280 * release all calls (due to broken socket)
1282 static void release_all_calls(void)
1284 struct chan_call *call;
1289 /* no ast, so we may directly free call */
1291 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1295 /* already in release process */
1296 if (call->state == CHAN_LCR_STATE_RELEASE) {
1300 /* release or queue release */
1302 call->state = CHAN_LCR_STATE_RELEASE;
1303 if (!call->pbx_started) {
1304 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1305 ast_hangup(call->ast); // call will be destroyed here
1308 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1309 ast_queue_hangup(call->ast);
1313 /* release all bchannels */
1314 while(bchannel_first)
1315 free_bchannel(bchannel_first);
1320 * warning! not thread safe
1321 * returns -1 for socket error, 0 for no work, 1 for work
1323 int handle_socket(void)
1327 struct admin_list *admin;
1328 struct admin_message msg;
1330 /* read from socket */
1331 len = read(lcr_sock, &msg, sizeof(msg));
1334 CERROR(NULL, NULL, "Socket closed.\n");
1335 return(-1); // socket closed
1339 if (len != sizeof(msg))
1341 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1342 return(-1); // socket error
1344 if (msg.message != ADMIN_MESSAGE)
1346 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1349 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1353 if (errno != EWOULDBLOCK)
1355 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1360 /* write to socket */
1363 admin = admin_first;
1364 len = write(lcr_sock, &admin->msg, sizeof(msg));
1367 CERROR(NULL, NULL, "Socket closed.\n");
1368 return(-1); // socket closed
1372 if (len != sizeof(msg))
1374 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1375 return(-1); // socket error
1378 admin_first = admin->next;
1384 if (errno != EWOULDBLOCK)
1386 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1395 * open and close socket and thread
1397 int open_socket(void)
1400 char *socket_name = SOCKET_NAME;
1402 struct sockaddr_un sock_address;
1403 unsigned long on = 1;
1404 union parameter param;
1407 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1409 CERROR(NULL, NULL, "Failed to create socket.\n");
1413 /* set socket address and name */
1414 memset(&sock_address, 0, sizeof(sock_address));
1415 sock_address.sun_family = PF_UNIX;
1416 strcpy(sock_address.sun_path, socket_name);
1418 /* connect socket */
1419 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1423 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1427 /* set non-blocking io */
1428 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1432 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1436 /* enque hello message */
1437 memset(¶m, 0, sizeof(param));
1438 strcpy(param.hello.application, "asterisk");
1439 send_message(MESSAGE_HELLO, 0, ¶m);
1444 void close_socket(void)
1446 struct admin_list *admin, *temp;
1448 /* flush pending messages */
1449 admin = admin_first;
1452 admin = admin->next;
1463 void sighandler(int sigset)
1467 static void *chan_thread(void *arg)
1471 union parameter param;
1472 time_t retry = 0, now;
1474 bchannel_pid = getpid();
1476 // signal(SIGPIPE, sighandler);
1478 memset(¶m, 0, sizeof(union parameter));
1482 ast_mutex_lock(&chan_lock);
1489 ret = handle_socket();
1491 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1493 release_all_calls();
1500 if (retry && now-retry > 5) {
1501 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1503 if (open_socket() < 0) {
1512 ret = bchannel_handle();
1517 ast_mutex_unlock(&chan_lock);
1519 ast_mutex_lock(&chan_lock);
1525 CERROR(NULL, NULL, "Thread exit.\n");
1527 ast_mutex_unlock(&chan_lock);
1529 // signal(SIGPIPE, SIG_DFL);
1535 * new asterisk instance
1538 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1540 char exten[256], *dial, *interface, *opt;
1541 struct ast_channel *ast;
1542 struct chan_call *call;
1544 ast_mutex_lock(&chan_lock);
1545 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1547 /* if socket is closed */
1550 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1554 /* create call instance */
1555 call = alloc_call();
1558 /* failed to create instance */
1562 /* create asterisk channel instrance */
1563 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1566 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1568 /* failed to create instance */
1571 ast->tech = &lcr_tech;
1572 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1573 /* configure channel */
1574 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1575 ast->readformat = ast->rawreadformat = ast->nativeformats;
1576 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1578 ast->hangupcause = 0;
1582 ast->tech_pvt = call;
1583 ast->fds[0] = call->pipe[0];
1584 call->pbx_started = 0;
1586 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1589 * Extract interface, dialstring, options from data.
1592 * <interface>/<dialstring>
1593 * <interface>/<dialstring>/options
1595 strncpy(exten, (char *)data, sizeof(exten)-1);
1596 exten[sizeof(exten)-1] = '\0';
1597 if ((dial = strchr(exten, '/'))) {
1600 if ((opt = strchr(dial, '/')))
1609 strncpy(call->interface, interface, sizeof(call->interface)-1);
1610 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1611 apply_opt(call, (char *)opt);
1613 /* if hdlc is forced by option, we change transcap to data */
1615 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1616 && ast->transfercapability != INFO_BC_DATARESTRICTED
1617 && ast->transfercapability != INFO_BC_VIDEO)
1618 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1620 ast_mutex_unlock(&chan_lock);
1625 * call from asterisk
1627 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1629 union parameter newparam;
1630 struct chan_call *call;
1632 ast_mutex_lock(&chan_lock);
1633 call = ast->tech_pvt;
1635 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1636 ast_mutex_unlock(&chan_lock);
1640 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1642 /* pbx process is started */
1643 call->pbx_started = 1;
1644 /* send MESSAGE_NEWREF */
1645 memset(&newparam, 0, sizeof(union parameter));
1646 newparam.direction = 0; /* request from app */
1647 send_message(MESSAGE_NEWREF, 0, &newparam);
1649 /* set hdlc if capability requires hdlc */
1650 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1651 || ast->transfercapability == INFO_BC_DATARESTRICTED
1652 || ast->transfercapability == INFO_BC_VIDEO)
1655 ast_mutex_unlock(&chan_lock);
1659 static int lcr_digit(struct ast_channel *ast, char digit)
1661 struct chan_call *call;
1662 union parameter newparam;
1665 /* only pass IA5 number space */
1666 if (digit > 126 || digit < 32)
1669 ast_mutex_lock(&chan_lock);
1670 call = ast->tech_pvt;
1672 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1673 ast_mutex_unlock(&chan_lock);
1677 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1679 /* send information or queue them */
1680 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1682 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1683 memset(&newparam, 0, sizeof(union parameter));
1684 newparam.information.id[0] = digit;
1685 newparam.information.id[1] = '\0';
1686 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1689 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1691 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1693 strncat(call->dialque, buf, strlen(call->dialque)-1);
1696 ast_mutex_unlock(&chan_lock);
1700 static int lcr_answer(struct ast_channel *ast)
1702 union parameter newparam;
1703 struct chan_call *call;
1705 ast_mutex_lock(&chan_lock);
1706 call = ast->tech_pvt;
1708 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1709 ast_mutex_unlock(&chan_lock);
1713 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1715 /* copy connectinfo, if bridged */
1716 if (call->bridge_call)
1717 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1718 /* send connect message to lcr */
1719 memset(&newparam, 0, sizeof(union parameter));
1720 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1721 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1723 call->state = CHAN_LCR_STATE_CONNECT;
1724 /* request bchannel */
1725 if (!call->bchannel) {
1726 CDEBUG(call, ast, "Requesting B-channel.\n");
1727 memset(&newparam, 0, sizeof(union parameter));
1728 newparam.bchannel.type = BCHANNEL_REQUEST;
1729 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1732 // memset(&newparam, 0, sizeof(union parameter));
1733 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1736 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1740 ast_mutex_unlock(&chan_lock);
1744 static int lcr_hangup(struct ast_channel *ast)
1746 struct chan_call *call;
1747 pthread_t tid = pthread_self();
1749 if (!pthread_equal(tid, chan_tid))
1750 ast_mutex_lock(&chan_lock);
1751 call = ast->tech_pvt;
1753 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1754 if (!pthread_equal(tid, chan_tid))
1755 ast_mutex_unlock(&chan_lock);
1759 if (!pthread_equal(tid, chan_tid))
1760 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1762 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1764 /* disconnect asterisk, maybe not required */
1765 ast->tech_pvt = NULL;
1770 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1771 if (ast->hangupcause > 0)
1772 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1774 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
1777 if (!pthread_equal(tid, chan_tid))
1778 ast_mutex_unlock(&chan_lock);
1782 /* ref is not set, due to prepare setup or release */
1783 if (call->state == CHAN_LCR_STATE_RELEASE)
1785 /* we get the response to our release */
1786 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1790 /* during prepare, we change to release state */
1791 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1792 call->state = CHAN_LCR_STATE_RELEASE;
1795 if (!pthread_equal(tid, chan_tid))
1796 ast_mutex_unlock(&chan_lock);
1800 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1802 struct chan_call *call;
1805 CDEBUG(NULL, ast, "No subclass\n");
1806 if (!(f->subclass & ast->nativeformats))
1807 CDEBUG(NULL, ast, "Unexpected format.\n");
1809 ast_mutex_lock(&chan_lock);
1810 call = ast->tech_pvt;
1812 ast_mutex_unlock(&chan_lock);
1815 if (call->bchannel && f->samples)
1816 bchannel_transmit(call->bchannel, f->data, f->samples);
1817 ast_mutex_unlock(&chan_lock);
1822 static struct ast_frame *lcr_read(struct ast_channel *ast)
1824 struct chan_call *call;
1828 ast_mutex_lock(&chan_lock);
1829 call = ast->tech_pvt;
1831 ast_mutex_unlock(&chan_lock);
1834 if (call->pipe[0] > -1) {
1835 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1837 close(call->pipe[0]);
1843 p = call->read_buff;
1844 for (i = 0; i < len; i++) {
1849 call->read_fr.frametype = AST_FRAME_VOICE;
1850 call->read_fr.subclass = ast->nativeformats;
1851 call->read_fr.datalen = len;
1852 call->read_fr.samples = len;
1853 call->read_fr.delivery = ast_tv(0,0);
1854 call->read_fr.data = call->read_buff;
1855 ast_mutex_unlock(&chan_lock);
1857 return &call->read_fr;
1860 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1862 union parameter newparam;
1864 struct chan_call *call;
1866 ast_mutex_lock(&chan_lock);
1867 call = ast->tech_pvt;
1869 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1870 ast_mutex_unlock(&chan_lock);
1875 case AST_CONTROL_BUSY:
1876 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1877 ast_setstate(ast, AST_STATE_BUSY);
1878 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1879 /* send message to lcr */
1880 memset(&newparam, 0, sizeof(union parameter));
1881 newparam.disconnectinfo.cause = 17;
1882 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1883 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1885 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1888 case AST_CONTROL_CONGESTION:
1889 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
1890 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1891 /* send message to lcr */
1892 memset(&newparam, 0, sizeof(union parameter));
1893 newparam.disconnectinfo.cause = ast->hangupcause;
1894 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1895 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1897 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1900 case AST_CONTROL_PROCEEDING:
1901 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
1902 if (call->state == CHAN_LCR_STATE_IN_SETUP
1903 || call->state == CHAN_LCR_STATE_IN_DIALING) {
1904 /* send message to lcr */
1905 memset(&newparam, 0, sizeof(union parameter));
1906 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
1908 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
1911 case AST_CONTROL_RINGING:
1912 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1913 ast_setstate(ast, AST_STATE_RINGING);
1914 if (call->state == CHAN_LCR_STATE_IN_SETUP
1915 || call->state == CHAN_LCR_STATE_IN_DIALING
1916 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
1917 /* send message to lcr */
1918 memset(&newparam, 0, sizeof(union parameter));
1919 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1921 call->state = CHAN_LCR_STATE_IN_ALERTING;
1925 CDEBUG(call, ast, "Received indicate -1.\n");
1929 case AST_CONTROL_VIDUPDATE:
1930 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
1933 case AST_CONTROL_HOLD:
1934 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1935 /* send message to lcr */
1936 memset(&newparam, 0, sizeof(union parameter));
1937 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1938 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1940 /*start music onhold*/
1941 ast_moh_start(ast,data,ast->musicclass);
1943 case AST_CONTROL_UNHOLD:
1944 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1945 /* send message to lcr */
1946 memset(&newparam, 0, sizeof(union parameter));
1947 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1948 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1955 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1961 ast_mutex_unlock(&chan_lock);
1968 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *newast)
1970 struct chan_call *call;
1972 ast_mutex_lock(&chan_lock);
1973 call = oldast->tech_pvt;
1975 CERROR(NULL, oldast, "Received fixup from Asterisk, but no call instance exists.\n");
1976 ast_mutex_unlock(&chan_lock);
1980 CDEBUG(call, oldast, "Received fixup from Asterisk.\n");
1982 ast_mutex_lock(&chan_lock);
1987 * send_text asterisk
1989 static int lcr_send_text(struct ast_channel *ast, const char *text)
1991 struct chan_call *call;
1992 union parameter newparam;
1994 ast_mutex_lock(&chan_lock);
1995 call = ast->tech_pvt;
1997 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
1998 ast_mutex_unlock(&chan_lock);
2002 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2003 memset(&newparam, 0, sizeof(union parameter));
2004 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2005 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2006 ast_mutex_lock(&chan_lock);
2013 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2014 struct ast_channel *ast2, int flags,
2015 struct ast_frame **fo,
2016 struct ast_channel **rc, int timeoutms)
2019 struct chan_call *call1, *call2;
2020 struct ast_channel *carr[2], *who;
2022 struct ast_frame *f;
2025 CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
2030 /* join via dsp (if the channels are currently open) */
2031 ast_mutex_lock(&chan_lock);
2032 bridge_id = new_bridge_id();
2033 call1 = ast1->tech_pvt;
2034 call2 = ast2->tech_pvt;
2037 call1->bridge_id = bridge_id;
2038 if (call1->bchannel)
2039 bchannel_join(call1->bchannel, bridge_id);
2040 call1->bridge_call = call2;
2044 call2->bridge_id = bridge_id;
2045 if (call2->bchannel)
2046 bchannel_join(call2->bchannel, bridge_id);
2047 call2->bridge_call = call1;
2049 ast_mutex_unlock(&chan_lock);
2053 who = ast_waitfor_n(carr, 2, &to);
2056 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2061 if (!f || f->frametype == AST_FRAME_CONTROL) {
2063 CDEBUG(NULL, NULL, "Got hangup.\n");
2065 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2072 if ( f->frametype == AST_FRAME_DTMF ) {
2073 CDEBUG(NULL, NULL, "Got DTMF.\n");
2089 CDEBUG(NULL, NULL, "Releasing bride.\n");
2091 /* split channels */
2092 ast_mutex_lock(&chan_lock);
2093 call1 = ast1->tech_pvt;
2094 call2 = ast2->tech_pvt;
2097 call1->bridge_id = 0;
2098 if (call1->bchannel)
2099 bchannel_join(call1->bchannel, 0);
2100 if (call1->bridge_call)
2101 call1->bridge_call->bridge_call = NULL;
2102 call1->bridge_call = NULL;
2106 call2->bridge_id = 0;
2107 if (call2->bchannel)
2108 bchannel_join(call2->bchannel, 0);
2109 if (call2->bridge_call)
2110 call2->bridge_call->bridge_call = NULL;
2111 call2->bridge_call = NULL;
2114 ast_mutex_unlock(&chan_lock);
2115 return AST_BRIDGE_COMPLETE;
2117 static struct ast_channel_tech lcr_tech = {
2119 .description="Channel driver for connecting to Linux-Call-Router",
2120 .requester=lcr_request,
2121 .send_digit_begin=lcr_digit,
2128 .indicate=lcr_indicate,
2130 .send_text=lcr_send_text,
2139 static int lcr_show_lcr (int fd, int argc, char *argv[])
2144 static int lcr_show_calls (int fd, int argc, char *argv[])
2149 static int lcr_reload_routing (int fd, int argc, char *argv[])
2154 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2159 static int lcr_port_block (int fd, int argc, char *argv[])
2164 static int lcr_port_unblock (int fd, int argc, char *argv[])
2169 static int lcr_port_unload (int fd, int argc, char *argv[])
2174 static struct ast_cli_entry cli_show_lcr =
2175 { {"lcr", "show", "lcr", NULL},
2177 "Shows current states of LCR core",
2178 "Usage: lcr show lcr\n",
2181 static struct ast_cli_entry cli_show_calls =
2182 { {"lcr", "show", "calls", NULL},
2184 "Shows current calls made by LCR and Asterisk",
2185 "Usage: lcr show calls\n",
2188 static struct ast_cli_entry cli_reload_routing =
2189 { {"lcr", "reload", "routing", NULL},
2191 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2192 "Usage: lcr reload routing\n",
2195 static struct ast_cli_entry cli_reload_interfaces =
2196 { {"lcr", "reload", "interfaces", NULL},
2197 lcr_reload_interfaces,
2198 "Reloads interfaces conf of LCR",
2199 "Usage: lcr reload interfaces\n",
2202 static struct ast_cli_entry cli_port_block =
2203 { {"lcr", "port", "block", NULL},
2205 "Blocks LCR port for further calls",
2206 "Usage: lcr port block \"<port>\"\n",
2209 static struct ast_cli_entry cli_port_unblock =
2210 { {"lcr", "port", "unblock", NULL},
2212 "Unblocks or loads LCR port, port is opened my mISDN",
2213 "Usage: lcr port unblock \"<port>\"\n",
2216 static struct ast_cli_entry cli_port_unload =
2217 { {"lcr", "port", "unload", NULL},
2219 "Unloads LCR port, port is closes by mISDN",
2220 "Usage: lcr port unload \"<port>\"\n",
2226 static int lcr_config_exec(struct ast_channel *ast, void *data)
2228 struct chan_call *call;
2230 ast_mutex_lock(&chan_lock);
2231 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2235 if (call->ast == ast)
2240 apply_opt(call, (char *)data);
2242 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2244 ast_mutex_unlock(&chan_lock);
2249 * module loading and destruction
2251 int load_module(void)
2255 for (i = 0; i < 256; i++) {
2256 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2257 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2260 if (read_options() == 0) {
2261 CERROR(NULL, NULL, "%s", options_error);
2262 return AST_MODULE_LOAD_DECLINE;
2265 ast_mutex_init(&chan_lock);
2266 ast_mutex_init(&log_lock);
2268 if (open_socket() < 0) {
2269 /* continue with closed socket */
2272 if (bchannel_initialize()) {
2273 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2275 return AST_MODULE_LOAD_DECLINE;
2279 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2280 if (ast_channel_register(&lcr_tech)) {
2281 CERROR(NULL, NULL, "Unable to register channel class\n");
2282 bchannel_deinitialize();
2284 return AST_MODULE_LOAD_DECLINE;
2287 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2288 "lcr_config(<opt><optarg>:<opt>:...)\n"
2289 "Sets LCR opts. and optargs\n"
2291 "The available options are:\n"
2292 " d - Send display text on called phone, text is the optarg.\n"
2293 " n - Don't detect dtmf tones on called channel.\n"
2294 " h - Force data call (HDLC).\n"
2295 " t - Disable all audio features (required for fax application).\n"
2296 " c - Make crypted outgoing call, optarg is keyindex.\n"
2297 " e - Perform echo cancelation on this channel.\n"
2298 " Takes mISDN pipeline option as optarg.\n"
2299 // " s - Send Non Inband DTMF as inband.\n"
2300 " vr - rxgain control\n"
2301 " vt - txgain control\n"
2302 " Volume changes at factor 2 ^ optarg.\n"
2307 ast_cli_register(&cli_show_lcr);
2308 ast_cli_register(&cli_show_calls);
2309 ast_cli_register(&cli_reload_routing);
2310 ast_cli_register(&cli_reload_interfaces);
2311 ast_cli_register(&cli_port_block);
2312 ast_cli_register(&cli_port_unblock);
2313 ast_cli_register(&cli_port_unload);
2317 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2319 /* failed to create thread */
2320 bchannel_deinitialize();
2322 ast_channel_unregister(&lcr_tech);
2323 return AST_MODULE_LOAD_DECLINE;
2328 int unload_module(void)
2330 /* First, take us out of the channel loop */
2331 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2334 pthread_join(chan_tid, NULL);
2336 ast_channel_unregister(&lcr_tech);
2338 ast_unregister_application("lcr_config");
2341 if (mISDN_created) {
2342 bchannel_deinitialize();
2346 if (lcr_sock >= 0) {
2354 int reload_module(void)
2361 #define AST_MODULE "chan_lcr"
2363 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2364 .load = load_module,
2365 .unload = unload_module,
2366 .reload = reload_module,