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, "%d", 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 int 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 int 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 int 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;
348 strncpy(string, data, sizeof(string)-1);
349 string[sizeof(string)-1] = '\0';
352 while((opt = strsep(&p, ":")))
356 if (opt[1] == '\0') {
357 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
360 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
361 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
362 strncpy(call->display, opt+1, sizeof(call->display)-1);
364 memset(&newparam, 0, sizeof(union parameter));
365 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
366 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
370 if (opt[1] != '\0') {
371 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
374 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
378 if (opt[1] == '\0') {
379 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
383 /* check for 0xXXXX... type of key */
384 if (!!strncmp((char *)key, "0x", 2)) {
385 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
389 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
390 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
396 if (*key>='0' && *key<='9')
397 call->bf_key[i] = (*key-'0') << 8;
398 else if (*key>='a' && *key<='f')
399 call->bf_key[i] = (*key-'a'+10) << 8;
400 else if (*key>='A' && *key<='F')
401 call->bf_key[i] = (*key-'A'+10) << 8;
405 if (*key>='0' && *key<='9')
406 call->bf_key[i] += (*key - '0');
407 else if (*key>='a' && *key<='f')
408 call->bf_key[i] += (*key - 'a' + 10);
409 else if (*key>='A' && *key<='F')
410 call->bf_key[i] += (*key - 'A' + 10);
417 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
421 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
423 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
426 if (opt[1] != '\0') {
427 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
430 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
437 if (opt[1] != '\0') {
438 CERROR(call, call->ast, "Option 't' (transparent) expects no parameter.\n", opt);
441 CDEBUG(call, call->ast, "Option 't' (transparent).\n");
442 if (!call->transparent) {
443 call->transparent = 1;
448 if (opt[1] == '\0') {
449 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
452 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
453 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
455 bchannel_pipeline(call->bchannel, call->pipeline);
459 if (opt[1] != '\0') {
460 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
463 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
464 call->inband_dtmf = 1;
469 if (opt[1] != 'r' && opt[1] != 't') {
470 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
474 if (gain < -8 || gain >8) {
475 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
478 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
480 call->rx_gain = gain;
482 bchannel_gain(call->bchannel, call->rx_gain, 0);
484 call->tx_gain = gain;
486 bchannel_gain(call->bchannel, call->tx_gain, 1);
490 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
494 /* re-open, if bchannel is created */
495 if (call->bchannel && call->bchannel->b_sock > -1) {
496 bchannel_destroy(call->bchannel);
497 if (bchannel_create(call->bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
498 bchannel_activate(call->bchannel, 1);
503 * send setup info to LCR
504 * this function is called, when asterisk call is received and ref is received
506 static void send_setup_to_lcr(struct chan_call *call)
508 union parameter newparam;
509 struct ast_channel *ast = call->ast;
511 if (!call->ast || !call->ref)
514 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
516 /* send setup message to LCR */
517 memset(&newparam, 0, sizeof(union parameter));
518 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
519 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
520 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
521 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
522 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
523 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
524 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
525 call->display[0] = '\0';
526 if (call->cid_num[0])
527 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
528 if (call->cid_name[0])
529 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
530 if (call->cid_rdnis[0])
532 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
533 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
534 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
536 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
538 case AST_PRES_ALLOWED:
539 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
541 case AST_PRES_RESTRICTED:
542 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
544 case AST_PRES_UNAVAILABLE:
545 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
548 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
550 switch(ast->cid.cid_ton)
553 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
556 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
559 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
562 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
564 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
565 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
566 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
567 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
568 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
569 send_message(MESSAGE_SETUP, call->ref, &newparam);
571 /* change to outgoing setup state */
572 call->state = CHAN_LCR_STATE_OUT_SETUP;
576 * send dialing info to LCR
577 * this function is called, when setup acknowledge is received and dialing
580 static void send_dialque_to_lcr(struct chan_call *call)
582 union parameter newparam;
584 if (!call->ast || !call->ref || !call->dialque[0])
587 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
589 /* send setup message to LCR */
590 memset(&newparam, 0, sizeof(union parameter));
591 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
592 call->dialque[0] = '\0';
593 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
597 * in case of a bridge, the unsupported message can be forwarded directly
598 * to the remote call.
600 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
604 if (!call->bridge_call) return;
605 CDEBUG(call, NULL, "Sending message due briding.\n");
606 send_message(message_type, call->bridge_call->ref, param);
610 * send release message to LCR and import bchannel if exported
612 static void send_release_and_import(struct chan_call *call, int cause, int location)
614 union parameter newparam;
616 /* importing channel */
617 if (call->bchannel) {
618 memset(&newparam, 0, sizeof(union parameter));
619 newparam.bchannel.type = BCHANNEL_RELEASE;
620 newparam.bchannel.handle = call->bchannel->handle;
621 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
623 /* sending release */
624 memset(&newparam, 0, sizeof(union parameter));
625 newparam.disconnectinfo.cause = cause;
626 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
627 send_message(MESSAGE_RELEASE, call->ref, &newparam);
631 * check if extension matches and start asterisk
632 * if it can match, proceed
635 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
638 union parameter newparam;
640 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", ast->exten, ast->context, complete?"yes":"no");
645 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
647 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
651 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
653 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);
657 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
658 /* send setup acknowledge to lcr */
659 memset(&newparam, 0, sizeof(union parameter));
660 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
663 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
668 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
670 /* send setup acknowledge to lcr */
671 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
672 memset(&newparam, 0, sizeof(union parameter));
673 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
677 call->state = CHAN_LCR_STATE_IN_DIALING;
679 /* if match, start pbx */
680 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
681 CDEBUG(call, ast, "Extensions matches.\n");
686 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
694 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
695 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
697 /* release asterisk */
698 ast->hangupcause = call->cause;
699 /* change to release state */
700 call->state = CHAN_LCR_STATE_RELEASE;
701 ast_hangup(ast); // call will be destroyed here
705 /* send setup to asterisk */
706 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
707 ret = ast_pbx_start(ast);
710 cause = (ret==-2)?34:27;
713 call->pbx_started = 1;
718 * incoming setup from LCR
720 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
722 struct ast_channel *ast;
724 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
726 /* create asterisk channel instrance */
727 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
731 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
732 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
739 ast->tech_pvt = call;
740 ast->tech = &lcr_tech;
741 ast->fds[0] = call->pipe[0];
743 /* fill setup information */
744 if (param->setup.dialinginfo.id)
745 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
746 if (param->setup.context[0])
747 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
749 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
750 if (param->setup.callerinfo.id[0])
751 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
752 if (param->setup.callerinfo.name[0])
753 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
754 if (param->setup.redirinfo.id[0])
755 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
756 switch (param->setup.callerinfo.present)
758 case INFO_PRESENT_ALLOWED:
759 ast->cid.cid_pres = AST_PRES_ALLOWED;
761 case INFO_PRESENT_RESTRICTED:
762 ast->cid.cid_pres = AST_PRES_RESTRICTED;
765 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
767 switch (param->setup.callerinfo.ntype)
769 case INFO_NTYPE_SUBSCRIBER:
770 ast->cid.cid_ton = 4;
772 case INFO_NTYPE_NATIONAL:
773 ast->cid.cid_ton = 2;
775 case INFO_NTYPE_INTERNATIONAL:
776 ast->cid.cid_ton = 1;
779 ast->cid.cid_ton = 0;
781 ast->transfercapability = param->setup.capainfo.bearer_capa;
782 /* enable hdlc if transcap is data */
783 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
784 || ast->transfercapability == INFO_BC_DATARESTRICTED
785 || ast->transfercapability == INFO_BC_VIDEO)
787 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
789 /* configure channel */
790 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
791 ast->readformat = ast->rawreadformat = ast->nativeformats;
792 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
794 ast->hangupcause = 0;
797 call->state = CHAN_LCR_STATE_IN_SETUP;
799 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
803 * incoming setup acknowledge from LCR
805 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
807 if (!call->ast) return;
809 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
811 /* send pending digits in dialque */
812 if (call->dialque[0])
813 send_dialque_to_lcr(call);
814 /* change to overlap state */
815 call->state = CHAN_LCR_STATE_OUT_DIALING;
819 * incoming proceeding from LCR
821 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
823 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
826 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
827 /* send event to asterisk */
828 if (call->ast && call->pbx_started)
829 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
833 * incoming alerting from LCR
835 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
837 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
840 call->state = CHAN_LCR_STATE_OUT_ALERTING;
841 /* send event to asterisk */
842 if (call->ast && call->pbx_started)
843 ast_queue_control(call->ast, AST_CONTROL_RINGING);
847 * incoming connect from LCR
849 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
851 union parameter newparam;
853 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
856 call->state = CHAN_LCR_STATE_CONNECT;
857 /* request bchannel */
858 if (!call->bchannel) {
859 CDEBUG(call, call->ast, "Requesting B-channel.\n");
860 memset(&newparam, 0, sizeof(union parameter));
861 newparam.bchannel.type = BCHANNEL_REQUEST;
862 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
864 /* copy connectinfo */
865 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
866 /* send event to asterisk */
867 if (call->ast && call->pbx_started)
868 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
872 * incoming disconnect from LCR
874 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
876 struct ast_channel *ast = call->ast;
878 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
881 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
883 call->cause = param->disconnectinfo.cause;
884 call->location = param->disconnectinfo.location;
885 /* if bridge, forward disconnect and return */
888 if (call->bridge_call)
890 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
891 bridge_message_if_bridged(call, message_type, param);
895 /* release lcr with same cause */
896 send_release_and_import(call, call->cause, call->location);
898 /* change to release state */
899 call->state = CHAN_LCR_STATE_RELEASE;
900 /* release asterisk */
903 ast->hangupcause = call->cause;
904 if (call->pbx_started)
905 ast_queue_hangup(ast);
907 ast_hangup(ast); // call will be destroyed here
913 * incoming setup acknowledge from LCR
915 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
917 struct ast_channel *ast = call->ast;
919 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
923 /* change to release state */
924 call->state = CHAN_LCR_STATE_RELEASE;
925 /* copy release info */
928 call->cause = param->disconnectinfo.cause;
929 call->location = param->disconnectinfo.location;
931 /* if we have an asterisk instance, send hangup, else we are done */
934 ast->hangupcause = call->cause;
935 if (call->pbx_started)
936 ast_queue_hangup(ast);
938 ast_hangup(ast); // call will be destroyed here
948 * incoming information from LCR
950 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
952 struct ast_channel *ast = call->ast;
956 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
960 /* pbx not started */
961 if (!call->pbx_started)
963 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
964 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
965 lcr_start_pbx(call, ast, param->information.sending_complete);
970 p = param->information.id;
971 if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
973 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
976 /* send digit to asterisk */
977 memset(&fr, 0, sizeof(fr));
978 fr.frametype = AST_FRAME_DTMF;
980 fr.delivery = ast_tv(0, 0);
981 ast_queue_frame(call->ast, &fr);
985 /* use bridge to forware message not supported by asterisk */
986 if (call->state == CHAN_LCR_STATE_CONNECT) {
987 CDEBUG(call, call->ast, "Call is connected, briding.\n");
988 bridge_message_if_bridged(call, message_type, param);
993 * incoming information from LCR
995 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
997 union parameter newparam;
999 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1001 /* request bchannel, if call is resumed and we don't have it */
1002 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1003 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1004 memset(&newparam, 0, sizeof(union parameter));
1005 newparam.bchannel.type = BCHANNEL_REQUEST;
1006 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1009 if (!call->ast) return;
1011 /* use bridge to forware message not supported by asterisk */
1012 bridge_message_if_bridged(call, message_type, param);
1016 * incoming information from LCR
1018 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1020 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1022 if (!call->ast) return;
1024 /* use bridge to forware message not supported by asterisk */
1025 bridge_message_if_bridged(call, message_type, param);
1029 * got dtmf from bchannel (locked state)
1031 void lcr_in_dtmf(struct chan_call *call, int val)
1033 struct ast_channel *ast = call->ast;
1034 struct ast_frame fr;
1038 if (!call->pbx_started)
1041 CDEBUG(call, call->ast, "Forwarding DTMF digit '%c' to Asterisk.\n", val);
1043 /* send digit to asterisk */
1044 memset(&fr, 0, sizeof(fr));
1045 fr.frametype = AST_FRAME_DTMF;
1047 fr.delivery = ast_tv(0, 0);
1048 ast_queue_frame(call->ast, &fr);
1052 * message received from LCR
1054 int receive_message(int message_type, unsigned int ref, union parameter *param)
1056 struct bchannel *bchannel;
1057 struct chan_call *call;
1058 union parameter newparam;
1060 memset(&newparam, 0, sizeof(union parameter));
1062 /* handle bchannel message*/
1063 if (message_type == MESSAGE_BCHANNEL)
1065 switch(param->bchannel.type)
1067 case BCHANNEL_ASSIGN:
1068 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
1069 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1071 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1074 /* create bchannel */
1075 bchannel = alloc_bchannel(param->bchannel.handle);
1078 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1082 /* configure channel */
1083 bchannel->b_tx_gain = param->bchannel.tx_gain;
1084 bchannel->b_rx_gain = param->bchannel.rx_gain;
1085 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1086 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1088 bchannel->b_bf_len = param->bchannel.crypt_len;
1089 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1091 bchannel->b_txdata = 0;
1092 bchannel->b_dtmf = 1;
1093 bchannel->b_tx_dejitter = 1;
1095 /* in case, ref is not set, this bchannel instance must
1096 * be created until it is removed again by LCR */
1098 call = find_call_ref(ref);
1101 bchannel->call = call;
1102 call->bchannel = bchannel;
1104 bchannel_dtmf(bchannel, 1);
1106 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1107 if (call->pipeline[0])
1108 bchannel_pipeline(bchannel, call->pipeline);
1110 bchannel_gain(bchannel, call->rx_gain, 0);
1112 bchannel_gain(bchannel, call->tx_gain, 1);
1113 if (call->bridge_id) {
1114 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1115 bchannel_join(bchannel, call->bridge_id);
1117 /* create only, if call exists, othewhise it bchannel is freed below... */
1118 if (bchannel_create(bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
1119 bchannel_activate(bchannel, 1);
1122 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1123 newparam.bchannel.handle = param->bchannel.handle;
1124 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1125 /* if call has released before bchannel is assigned */
1127 newparam.bchannel.type = BCHANNEL_RELEASE;
1128 newparam.bchannel.handle = param->bchannel.handle;
1129 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1134 case BCHANNEL_REMOVE:
1135 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1136 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1138 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1141 /* unklink from call and destroy bchannel */
1142 free_bchannel(bchannel);
1145 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1146 newparam.bchannel.handle = param->bchannel.handle;
1147 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1152 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1157 /* handle new ref */
1158 if (message_type == MESSAGE_NEWREF)
1160 if (param->direction)
1162 /* new ref from lcr */
1163 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1164 if (!ref || find_call_ref(ref))
1166 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1169 /* allocate new call instance */
1170 call = alloc_call();
1172 call->state = CHAN_LCR_STATE_IN_PREPARE;
1175 /* wait for setup (or release from asterisk) */
1178 /* new ref, as requested from this remote application */
1179 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1180 call = find_call_ref(0);
1183 /* send release, if ref does not exist */
1184 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1185 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1190 /* send pending setup info */
1191 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1192 send_setup_to_lcr(call);
1193 /* release if asterisk has signed off */
1194 else if (call->state == CHAN_LCR_STATE_RELEASE)
1198 send_release_and_import(call, call->cause, call->location);
1200 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1212 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1215 call = find_call_ref(ref);
1218 /* ignore ref that is not used (anymore) */
1219 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1223 /* handle messages */
1224 switch(message_type)
1227 lcr_in_setup(call, message_type, param);
1230 case MESSAGE_OVERLAP:
1231 lcr_in_overlap(call, message_type, param);
1234 case MESSAGE_PROCEEDING:
1235 lcr_in_proceeding(call, message_type, param);
1238 case MESSAGE_ALERTING:
1239 lcr_in_alerting(call, message_type, param);
1242 case MESSAGE_CONNECT:
1243 lcr_in_connect(call, message_type, param);
1246 case MESSAGE_DISCONNECT:
1247 lcr_in_disconnect(call, message_type, param);
1250 case MESSAGE_RELEASE:
1251 lcr_in_release(call, message_type, param);
1254 case MESSAGE_INFORMATION:
1255 lcr_in_information(call, message_type, param);
1258 case MESSAGE_NOTIFY:
1259 lcr_in_notify(call, message_type, param);
1262 case MESSAGE_FACILITY:
1263 lcr_in_facility(call, message_type, param);
1266 case MESSAGE_PATTERN: // audio available from LCR
1269 case MESSAGE_NOPATTERN: // audio not available from LCR
1272 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1273 call->audiopath = param->audiopath;
1277 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1284 * release all calls (due to broken socket)
1286 static void release_all_calls(void)
1288 struct chan_call *call;
1293 /* no ast, so we may directly free call */
1295 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1299 /* already in release process */
1300 if (call->state == CHAN_LCR_STATE_RELEASE) {
1304 /* release or queue release */
1306 call->state = CHAN_LCR_STATE_RELEASE;
1307 if (!call->pbx_started) {
1308 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1309 ast_hangup(call->ast); // call will be destroyed here
1312 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1313 ast_queue_hangup(call->ast);
1317 /* release all bchannels */
1318 while(bchannel_first)
1319 free_bchannel(bchannel_first);
1324 * warning! not thread safe
1325 * returns -1 for socket error, 0 for no work, 1 for work
1327 int handle_socket(void)
1331 struct admin_list *admin;
1332 struct admin_message msg;
1334 /* read from socket */
1335 len = read(lcr_sock, &msg, sizeof(msg));
1338 CERROR(NULL, NULL, "Socket closed.\n");
1339 return(-1); // socket closed
1343 if (len != sizeof(msg))
1345 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1346 return(-1); // socket error
1348 if (msg.message != ADMIN_MESSAGE)
1350 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1353 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1357 if (errno != EWOULDBLOCK)
1359 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1364 /* write to socket */
1367 admin = admin_first;
1368 len = write(lcr_sock, &admin->msg, sizeof(msg));
1371 CERROR(NULL, NULL, "Socket closed.\n");
1372 return(-1); // socket closed
1376 if (len != sizeof(msg))
1378 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1379 return(-1); // socket error
1382 admin_first = admin->next;
1388 if (errno != EWOULDBLOCK)
1390 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1399 * open and close socket and thread
1401 int open_socket(void)
1404 char *socket_name = SOCKET_NAME;
1406 struct sockaddr_un sock_address;
1407 unsigned int on = 1;
1408 union parameter param;
1411 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1413 CERROR(NULL, NULL, "Failed to create socket.\n");
1417 /* set socket address and name */
1418 memset(&sock_address, 0, sizeof(sock_address));
1419 sock_address.sun_family = PF_UNIX;
1420 strcpy(sock_address.sun_path, socket_name);
1422 /* connect socket */
1423 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1427 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1431 /* set non-blocking io */
1432 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1436 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1440 /* enque hello message */
1441 memset(¶m, 0, sizeof(param));
1442 strcpy(param.hello.application, "asterisk");
1443 send_message(MESSAGE_HELLO, 0, ¶m);
1448 void close_socket(void)
1450 struct admin_list *admin, *temp;
1452 /* flush pending messages */
1453 admin = admin_first;
1456 admin = admin->next;
1467 void sighandler(int sigset)
1471 static void *chan_thread(void *arg)
1475 union parameter param;
1476 time_t retry = 0, now;
1478 bchannel_pid = getpid();
1480 // signal(SIGPIPE, sighandler);
1482 memset(¶m, 0, sizeof(union parameter));
1486 ast_mutex_lock(&chan_lock);
1493 ret = handle_socket();
1495 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1497 release_all_calls();
1504 if (retry && now-retry > 5) {
1505 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1507 if (open_socket() < 0) {
1516 ret = bchannel_handle();
1521 ast_mutex_unlock(&chan_lock);
1523 ast_mutex_lock(&chan_lock);
1529 CERROR(NULL, NULL, "Thread exit.\n");
1531 ast_mutex_unlock(&chan_lock);
1533 // signal(SIGPIPE, SIG_DFL);
1539 * new asterisk instance
1542 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1544 char exten[256], *dial, *interface, *opt;
1545 struct ast_channel *ast;
1546 struct chan_call *call;
1548 ast_mutex_lock(&chan_lock);
1549 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1551 /* if socket is closed */
1554 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1558 /* create call instance */
1559 call = alloc_call();
1562 /* failed to create instance */
1566 /* create asterisk channel instrance */
1567 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1570 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1572 /* failed to create instance */
1575 ast->tech = &lcr_tech;
1576 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1577 /* configure channel */
1578 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1579 ast->readformat = ast->rawreadformat = ast->nativeformats;
1580 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1582 ast->hangupcause = 0;
1586 ast->tech_pvt = call;
1587 ast->fds[0] = call->pipe[0];
1588 call->pbx_started = 0;
1590 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1593 * Extract interface, dialstring, options from data.
1596 * <interface>/<dialstring>
1597 * <interface>/<dialstring>/options
1599 strncpy(exten, (char *)data, sizeof(exten)-1);
1600 exten[sizeof(exten)-1] = '\0';
1601 if ((dial = strchr(exten, '/'))) {
1604 if ((opt = strchr(dial, '/')))
1613 strncpy(call->interface, interface, sizeof(call->interface)-1);
1614 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1615 apply_opt(call, (char *)opt);
1617 ast_mutex_unlock(&chan_lock);
1622 * call from asterisk
1624 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1626 union parameter newparam;
1627 struct chan_call *call;
1629 ast_mutex_lock(&chan_lock);
1630 call = ast->tech_pvt;
1632 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1633 ast_mutex_unlock(&chan_lock);
1637 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1639 /* pbx process is started */
1640 call->pbx_started = 1;
1641 /* send MESSAGE_NEWREF */
1642 memset(&newparam, 0, sizeof(union parameter));
1643 newparam.direction = 0; /* request from app */
1644 send_message(MESSAGE_NEWREF, 0, &newparam);
1646 /* set hdlc if capability requires hdlc */
1647 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1648 || ast->transfercapability == INFO_BC_DATARESTRICTED
1649 || ast->transfercapability == INFO_BC_VIDEO)
1651 /* if hdlc is forced by option, we change transcap to data */
1653 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1654 && ast->transfercapability != INFO_BC_DATARESTRICTED
1655 && ast->transfercapability != INFO_BC_VIDEO)
1656 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1658 call->cid_num[0] = 0;
1659 call->cid_name[0] = 0;
1660 call->cid_rdnis[0] = 0;
1662 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1663 strncpy(call->cid_num, ast->cid.cid_num,
1664 sizeof(call->cid_num)-1);
1666 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1667 strncpy(call->cid_name, ast->cid.cid_name,
1668 sizeof(call->cid_name)-1);
1669 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1670 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1671 sizeof(call->cid_rdnis)-1);
1673 ast_mutex_unlock(&chan_lock);
1677 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1679 printf("DIGT BEGIN %c\n", digit);
1683 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1685 struct chan_call *call;
1686 union parameter newparam;
1689 /* only pass IA5 number space */
1690 if (digit > 126 || digit < 32)
1693 ast_mutex_lock(&chan_lock);
1694 call = ast->tech_pvt;
1696 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1697 ast_mutex_unlock(&chan_lock);
1701 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1703 /* send information or queue them */
1704 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1706 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1707 memset(&newparam, 0, sizeof(union parameter));
1708 newparam.information.id[0] = digit;
1709 newparam.information.id[1] = '\0';
1710 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1713 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1715 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1717 strncat(call->dialque, buf, strlen(call->dialque)-1);
1720 ast_mutex_unlock(&chan_lock);
1724 static int lcr_answer(struct ast_channel *ast)
1726 union parameter newparam;
1727 struct chan_call *call;
1729 ast_mutex_lock(&chan_lock);
1730 call = ast->tech_pvt;
1732 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1733 ast_mutex_unlock(&chan_lock);
1737 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1739 /* copy connectinfo, if bridged */
1740 if (call->bridge_call)
1741 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1742 /* send connect message to lcr */
1743 memset(&newparam, 0, sizeof(union parameter));
1744 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1745 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1747 call->state = CHAN_LCR_STATE_CONNECT;
1748 /* request bchannel */
1749 if (!call->bchannel) {
1750 CDEBUG(call, ast, "Requesting B-channel.\n");
1751 memset(&newparam, 0, sizeof(union parameter));
1752 newparam.bchannel.type = BCHANNEL_REQUEST;
1753 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1756 // memset(&newparam, 0, sizeof(union parameter));
1757 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1760 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1764 ast_mutex_unlock(&chan_lock);
1768 static int lcr_hangup(struct ast_channel *ast)
1770 struct chan_call *call;
1771 pthread_t tid = pthread_self();
1773 if (!pthread_equal(tid, chan_tid))
1774 ast_mutex_lock(&chan_lock);
1775 call = ast->tech_pvt;
1777 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1778 if (!pthread_equal(tid, chan_tid))
1779 ast_mutex_unlock(&chan_lock);
1783 if (!pthread_equal(tid, chan_tid))
1784 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1786 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1788 /* disconnect asterisk, maybe not required */
1789 ast->tech_pvt = NULL;
1794 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1795 if (ast->hangupcause > 0)
1796 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1798 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
1801 if (!pthread_equal(tid, chan_tid))
1802 ast_mutex_unlock(&chan_lock);
1806 /* ref is not set, due to prepare setup or release */
1807 if (call->state == CHAN_LCR_STATE_RELEASE)
1809 /* we get the response to our release */
1810 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1814 /* during prepare, we change to release state */
1815 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1816 call->state = CHAN_LCR_STATE_RELEASE;
1819 if (!pthread_equal(tid, chan_tid))
1820 ast_mutex_unlock(&chan_lock);
1824 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1826 struct chan_call *call;
1829 CDEBUG(NULL, ast, "No subclass\n");
1830 if (!(f->subclass & ast->nativeformats))
1831 CDEBUG(NULL, ast, "Unexpected format.\n");
1833 ast_mutex_lock(&chan_lock);
1834 call = ast->tech_pvt;
1836 ast_mutex_unlock(&chan_lock);
1839 if (call->bchannel && f->samples)
1840 bchannel_transmit(call->bchannel, f->data, f->samples);
1841 ast_mutex_unlock(&chan_lock);
1846 static struct ast_frame *lcr_read(struct ast_channel *ast)
1848 struct chan_call *call;
1852 ast_mutex_lock(&chan_lock);
1853 call = ast->tech_pvt;
1855 ast_mutex_unlock(&chan_lock);
1858 if (call->pipe[0] > -1) {
1859 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1861 close(call->pipe[0]);
1867 p = call->read_buff;
1868 for (i = 0; i < len; i++) {
1873 call->read_fr.frametype = AST_FRAME_VOICE;
1874 call->read_fr.subclass = ast->nativeformats;
1875 call->read_fr.datalen = len;
1876 call->read_fr.samples = len;
1877 call->read_fr.delivery = ast_tv(0,0);
1878 call->read_fr.data = call->read_buff;
1879 ast_mutex_unlock(&chan_lock);
1881 return &call->read_fr;
1884 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1886 union parameter newparam;
1888 struct chan_call *call;
1890 ast_mutex_lock(&chan_lock);
1891 call = ast->tech_pvt;
1893 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1894 ast_mutex_unlock(&chan_lock);
1899 case AST_CONTROL_BUSY:
1900 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1901 ast_setstate(ast, AST_STATE_BUSY);
1902 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1903 /* send message to lcr */
1904 memset(&newparam, 0, sizeof(union parameter));
1905 newparam.disconnectinfo.cause = 17;
1906 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1907 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1909 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1912 case AST_CONTROL_CONGESTION:
1913 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
1914 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1915 /* send message to lcr */
1916 memset(&newparam, 0, sizeof(union parameter));
1917 newparam.disconnectinfo.cause = ast->hangupcause;
1918 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1919 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1921 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1924 case AST_CONTROL_PROCEEDING:
1925 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
1926 if (call->state == CHAN_LCR_STATE_IN_SETUP
1927 || call->state == CHAN_LCR_STATE_IN_DIALING) {
1928 /* send message to lcr */
1929 memset(&newparam, 0, sizeof(union parameter));
1930 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
1932 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
1935 case AST_CONTROL_RINGING:
1936 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1937 ast_setstate(ast, AST_STATE_RINGING);
1938 if (call->state == CHAN_LCR_STATE_IN_SETUP
1939 || call->state == CHAN_LCR_STATE_IN_DIALING
1940 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
1941 /* send message to lcr */
1942 memset(&newparam, 0, sizeof(union parameter));
1943 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1945 call->state = CHAN_LCR_STATE_IN_ALERTING;
1949 CDEBUG(call, ast, "Received indicate -1.\n");
1953 case AST_CONTROL_VIDUPDATE:
1954 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
1957 case AST_CONTROL_HOLD:
1958 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1959 /* send message to lcr */
1960 memset(&newparam, 0, sizeof(union parameter));
1961 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1962 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1964 /*start music onhold*/
1965 ast_moh_start(ast,data,ast->musicclass);
1967 case AST_CONTROL_UNHOLD:
1968 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1969 /* send message to lcr */
1970 memset(&newparam, 0, sizeof(union parameter));
1971 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1972 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1979 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1985 ast_mutex_unlock(&chan_lock);
1992 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *newast)
1994 struct chan_call *call;
1996 ast_mutex_lock(&chan_lock);
1997 call = oldast->tech_pvt;
1999 CERROR(NULL, oldast, "Received fixup from Asterisk, but no call instance exists.\n");
2000 ast_mutex_unlock(&chan_lock);
2004 CDEBUG(call, oldast, "Received fixup from Asterisk.\n");
2006 ast_mutex_lock(&chan_lock);
2011 * send_text asterisk
2013 static int lcr_send_text(struct ast_channel *ast, const char *text)
2015 struct chan_call *call;
2016 union parameter newparam;
2018 ast_mutex_lock(&chan_lock);
2019 call = ast->tech_pvt;
2021 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2022 ast_mutex_unlock(&chan_lock);
2026 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2027 memset(&newparam, 0, sizeof(union parameter));
2028 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2029 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2030 ast_mutex_lock(&chan_lock);
2037 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2038 struct ast_channel *ast2, int flags,
2039 struct ast_frame **fo,
2040 struct ast_channel **rc, int timeoutms)
2043 struct chan_call *call1, *call2;
2044 struct ast_channel *carr[2], *who;
2046 struct ast_frame *f;
2049 CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
2054 /* join via dsp (if the channels are currently open) */
2055 ast_mutex_lock(&chan_lock);
2056 bridge_id = new_bridge_id();
2057 call1 = ast1->tech_pvt;
2058 call2 = ast2->tech_pvt;
2061 call1->bridge_id = bridge_id;
2062 if (call1->bchannel)
2063 bchannel_join(call1->bchannel, bridge_id);
2064 call1->bridge_call = call2;
2068 call2->bridge_id = bridge_id;
2069 if (call2->bchannel)
2070 bchannel_join(call2->bchannel, bridge_id);
2071 call2->bridge_call = call1;
2073 ast_mutex_unlock(&chan_lock);
2077 who = ast_waitfor_n(carr, 2, &to);
2080 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2085 if (!f || f->frametype == AST_FRAME_CONTROL) {
2087 CDEBUG(NULL, NULL, "Got hangup.\n");
2089 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2096 if ( f->frametype == AST_FRAME_DTMF ) {
2097 CDEBUG(NULL, NULL, "Got DTMF.\n");
2113 CDEBUG(NULL, NULL, "Releasing bride.\n");
2115 /* split channels */
2116 ast_mutex_lock(&chan_lock);
2117 call1 = ast1->tech_pvt;
2118 call2 = ast2->tech_pvt;
2121 call1->bridge_id = 0;
2122 if (call1->bchannel)
2123 bchannel_join(call1->bchannel, 0);
2124 if (call1->bridge_call)
2125 call1->bridge_call->bridge_call = NULL;
2126 call1->bridge_call = NULL;
2130 call2->bridge_id = 0;
2131 if (call2->bchannel)
2132 bchannel_join(call2->bchannel, 0);
2133 if (call2->bridge_call)
2134 call2->bridge_call->bridge_call = NULL;
2135 call2->bridge_call = NULL;
2138 ast_mutex_unlock(&chan_lock);
2139 return AST_BRIDGE_COMPLETE;
2141 static struct ast_channel_tech lcr_tech = {
2143 .description="Channel driver for connecting to Linux-Call-Router",
2144 .requester=lcr_request,
2145 .send_digit_begin=lcr_digit_begin,
2146 .send_digit_end=lcr_digit_end,
2153 .indicate=lcr_indicate,
2155 .send_text=lcr_send_text,
2164 static int lcr_show_lcr (int fd, int argc, char *argv[])
2169 static int lcr_show_calls (int fd, int argc, char *argv[])
2174 static int lcr_reload_routing (int fd, int argc, char *argv[])
2179 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2184 static int lcr_port_block (int fd, int argc, char *argv[])
2189 static int lcr_port_unblock (int fd, int argc, char *argv[])
2194 static int lcr_port_unload (int fd, int argc, char *argv[])
2199 static struct ast_cli_entry cli_show_lcr =
2200 { {"lcr", "show", "lcr", NULL},
2202 "Shows current states of LCR core",
2203 "Usage: lcr show lcr\n",
2206 static struct ast_cli_entry cli_show_calls =
2207 { {"lcr", "show", "calls", NULL},
2209 "Shows current calls made by LCR and Asterisk",
2210 "Usage: lcr show calls\n",
2213 static struct ast_cli_entry cli_reload_routing =
2214 { {"lcr", "reload", "routing", NULL},
2216 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2217 "Usage: lcr reload routing\n",
2220 static struct ast_cli_entry cli_reload_interfaces =
2221 { {"lcr", "reload", "interfaces", NULL},
2222 lcr_reload_interfaces,
2223 "Reloads interfaces conf of LCR",
2224 "Usage: lcr reload interfaces\n",
2227 static struct ast_cli_entry cli_port_block =
2228 { {"lcr", "port", "block", NULL},
2230 "Blocks LCR port for further calls",
2231 "Usage: lcr port block \"<port>\"\n",
2234 static struct ast_cli_entry cli_port_unblock =
2235 { {"lcr", "port", "unblock", NULL},
2237 "Unblocks or loads LCR port, port is opened my mISDN",
2238 "Usage: lcr port unblock \"<port>\"\n",
2241 static struct ast_cli_entry cli_port_unload =
2242 { {"lcr", "port", "unload", NULL},
2244 "Unloads LCR port, port is closes by mISDN",
2245 "Usage: lcr port unload \"<port>\"\n",
2251 static int lcr_config_exec(struct ast_channel *ast, void *data)
2253 struct chan_call *call;
2255 ast_mutex_lock(&chan_lock);
2256 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2260 if (call->ast == ast)
2265 apply_opt(call, (char *)data);
2267 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2269 ast_mutex_unlock(&chan_lock);
2274 * module loading and destruction
2276 int load_module(void)
2280 for (i = 0; i < 256; i++) {
2281 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2282 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2285 if (read_options() == 0) {
2286 CERROR(NULL, NULL, "%s", options_error);
2287 return AST_MODULE_LOAD_DECLINE;
2290 ast_mutex_init(&chan_lock);
2291 ast_mutex_init(&log_lock);
2293 if (open_socket() < 0) {
2294 /* continue with closed socket */
2297 if (bchannel_initialize()) {
2298 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2300 return AST_MODULE_LOAD_DECLINE;
2304 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2305 if (ast_channel_register(&lcr_tech)) {
2306 CERROR(NULL, NULL, "Unable to register channel class\n");
2307 bchannel_deinitialize();
2309 return AST_MODULE_LOAD_DECLINE;
2312 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2313 "lcr_config(<opt><optarg>:<opt>:...)\n"
2314 "Sets LCR opts. and optargs\n"
2316 "The available options are:\n"
2317 " d - Send display text on called phone, text is the optarg.\n"
2318 " n - Don't detect dtmf tones on called channel.\n"
2319 " h - Force data call (HDLC).\n"
2320 " t - Disable all audio features (required for fax application).\n"
2321 " c - Make crypted outgoing call, optarg is keyindex.\n"
2322 " e - Perform echo cancelation on this channel.\n"
2323 " Takes mISDN pipeline option as optarg.\n"
2324 // " s - Send Non Inband DTMF as inband.\n"
2325 " vr - rxgain control\n"
2326 " vt - txgain control\n"
2327 " Volume changes at factor 2 ^ optarg.\n"
2332 ast_cli_register(&cli_show_lcr);
2333 ast_cli_register(&cli_show_calls);
2334 ast_cli_register(&cli_reload_routing);
2335 ast_cli_register(&cli_reload_interfaces);
2336 ast_cli_register(&cli_port_block);
2337 ast_cli_register(&cli_port_unblock);
2338 ast_cli_register(&cli_port_unload);
2342 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2344 /* failed to create thread */
2345 bchannel_deinitialize();
2347 ast_channel_unregister(&lcr_tech);
2348 return AST_MODULE_LOAD_DECLINE;
2353 int unload_module(void)
2355 /* First, take us out of the channel loop */
2356 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2359 pthread_join(chan_tid, NULL);
2361 ast_channel_unregister(&lcr_tech);
2363 ast_unregister_application("lcr_config");
2366 if (mISDN_created) {
2367 bchannel_deinitialize();
2371 if (lcr_sock >= 0) {
2379 int reload_module(void)
2386 #define AST_MODULE "chan_lcr"
2388 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2389 .load = load_module,
2390 .unload = unload_module,
2391 .reload = reload_module,