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".
84 The deadlocking problem:
86 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
88 - ast_channel thread locks ast_channel and calls a tech function and waits
89 there for chan_lock to be unlocked.
93 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
94 ast_channel can be locked. All messages to asterisk are queued inside call
95 instance and will be handled using a try-lock to get ast_channel lock.
96 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
97 if the lock is incremented and decremented there.
99 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
100 it is called from ast_channel process which has already locked ast_channel.
109 #include <sys/types.h>
111 //#include <signal.h>
114 #include <sys/ioctl.h>
115 #include <sys/socket.h>
118 #include <semaphore.h>
120 #include <asterisk/module.h>
121 #include <asterisk/channel.h>
122 #include <asterisk/config.h>
123 #include <asterisk/logger.h>
124 #include <asterisk/pbx.h>
125 #include <asterisk/options.h>
126 #include <asterisk/io.h>
127 #include <asterisk/frame.h>
128 #include <asterisk/translate.h>
129 #include <asterisk/cli.h>
130 #include <asterisk/musiconhold.h>
131 #include <asterisk/dsp.h>
132 #include <asterisk/translate.h>
133 #include <asterisk/file.h>
134 #include <asterisk/callerid.h>
135 #include <asterisk/indications.h>
136 #include <asterisk/app.h>
137 #include <asterisk/features.h>
138 #include <asterisk/sched.h>
140 #include "extension.h"
142 #include "callerid.h"
143 #include "lcrsocket.h"
145 #include "bchannel.h"
147 #include "chan_lcr.h"
149 CHAN_LCR_STATE // state description structure
150 MESSAGES // message text
152 unsigned char flip_bits[256];
157 char lcr_type[]="lcr";
160 ast_mutex_t chan_lock; /* global lock */
161 ast_mutex_t log_lock; /* logging log */
164 int glob_channel = 0;
169 struct admin_list *next;
170 struct admin_message msg;
171 } *admin_first = NULL;
173 static struct ast_channel_tech lcr_tech;
178 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, ...)
181 char call_text[128] = "NULL";
182 char ast_text[128] = "NULL";
185 ast_mutex_lock(&log_lock);
188 vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
189 buffer[sizeof(buffer)-1]=0;
193 sprintf(call_text, "%d", call->ref);
195 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
196 ast_text[sizeof(ast_text)-1] = '\0';
198 ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
200 ast_mutex_unlock(&log_lock);
204 * channel and call instances
206 struct chan_call *call_first;
208 struct chan_call *find_call_ref(unsigned int ref)
210 struct chan_call *call = call_first;
214 if (call->ref == ref)
222 struct chan_call *find_call_ast(struct ast_channel *ast)
224 struct chan_call *call = call_first;
228 if (call->ast == ast)
235 struct chan_call *find_call_handle(unsigned int handle)
237 struct chan_call *call = call_first;
241 if (call->bchannel_handle == handle)
249 void free_call(struct chan_call *call)
251 struct chan_call **temp = &call_first;
257 *temp = (*temp)->next;
258 if (call->pipe[0] > -1)
259 close(call->pipe[0]);
260 if (call->pipe[1] > -1)
261 close(call->pipe[1]);
264 if (call->bchannel->call != call)
265 CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
266 call->bchannel->call = NULL;
268 if (call->bridge_call)
270 if (call->bridge_call->bridge_call != call)
271 CERROR(call, NULL, "Linked call structure has no link to us.\n");
272 call->bridge_call->bridge_call = NULL;
274 CDEBUG(call, NULL, "Call instance freed.\n");
278 temp = &((*temp)->next);
280 CERROR(call, NULL, "Call instance not found in list.\n");
283 struct chan_call *alloc_call(void)
285 struct chan_call **callp = &call_first;
288 callp = &((*callp)->next);
290 *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
292 memset(*callp, 0, sizeof(struct chan_call));
293 if (pipe((*callp)->pipe) < 0) {
294 CERROR(*callp, NULL, "Failed to create pipe.\n");
298 fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
299 CDEBUG(*callp, NULL, "Call instance allocated.\n");
304 unsigned short new_bridge_id(void)
306 struct chan_call *call;
307 unsigned short id = 1;
309 /* search for lowest bridge id that is not in use and not 0 */
315 if (call->bridge_id == id)
323 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
328 * enque message to LCR
330 int send_message(int message_type, unsigned int ref, union parameter *param)
332 struct admin_list *admin, **adminp;
335 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
338 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
340 adminp = &admin_first;
342 adminp = &((*adminp)->next);
343 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
345 CERROR(NULL, NULL, "No memory for message to LCR.\n");
350 admin->msg.message = ADMIN_MESSAGE;
351 admin->msg.u.msg.type = message_type;
352 admin->msg.u.msg.ref = ref;
353 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
359 * apply options (in locked state)
361 void apply_opt(struct chan_call *call, char *data)
363 union parameter newparam;
364 char string[1024], *p = string, *opt, *key;
365 int gain, i, newmode = 0;
370 strncpy(string, data, sizeof(string)-1);
371 string[sizeof(string)-1] = '\0';
374 while((opt = strsep(&p, ":")))
378 if (opt[1] == '\0') {
379 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
382 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
383 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
384 strncpy(call->display, opt+1, sizeof(call->display)-1);
386 memset(&newparam, 0, sizeof(union parameter));
387 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
388 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
392 if (opt[1] != '\0') {
393 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
396 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
400 if (opt[1] == '\0') {
401 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
405 /* check for 0xXXXX... type of key */
406 if (!!strncmp((char *)key, "0x", 2)) {
407 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
411 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
412 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
418 if (*key>='0' && *key<='9')
419 call->bf_key[i] = (*key-'0') << 8;
420 else if (*key>='a' && *key<='f')
421 call->bf_key[i] = (*key-'a'+10) << 8;
422 else if (*key>='A' && *key<='F')
423 call->bf_key[i] = (*key-'A'+10) << 8;
427 if (*key>='0' && *key<='9')
428 call->bf_key[i] += (*key - '0');
429 else if (*key>='a' && *key<='f')
430 call->bf_key[i] += (*key - 'a' + 10);
431 else if (*key>='A' && *key<='F')
432 call->bf_key[i] += (*key - 'A' + 10);
439 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
443 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
445 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
448 if (opt[1] != '\0') {
449 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
452 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
459 if (opt[1] != '\0') {
460 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
463 CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
470 if (opt[1] == '\0') {
471 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
474 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
475 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
477 bchannel_pipeline(call->bchannel, call->pipeline);
480 if (opt[1] != '\0') {
481 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
484 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
489 if (opt[1] != '\0') {
490 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
493 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
494 call->inband_dtmf = 1;
499 if (opt[1] != 'r' && opt[1] != 't') {
500 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
504 if (gain < -8 || gain >8) {
505 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
508 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
510 call->rx_gain = gain;
512 bchannel_gain(call->bchannel, call->rx_gain, 0);
514 call->tx_gain = gain;
516 bchannel_gain(call->bchannel, call->tx_gain, 1);
520 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
524 /* re-open, if bchannel is created */
525 if (call->bchannel && call->bchannel->b_sock > -1) {
526 bchannel_destroy(call->bchannel);
527 if (bchannel_create(call->bchannel, ((call->nodsp)?1:0) + ((call->hdlc)?2:0)))
528 bchannel_activate(call->bchannel, 1);
533 * send setup info to LCR
534 * this function is called, when asterisk call is received and ref is received
536 static void send_setup_to_lcr(struct chan_call *call)
538 union parameter newparam;
539 struct ast_channel *ast = call->ast;
541 if (!call->ast || !call->ref)
544 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
546 /* send setup message to LCR */
547 memset(&newparam, 0, sizeof(union parameter));
548 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
549 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
550 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
551 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
552 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
553 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
554 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
555 call->display[0] = '\0';
556 if (call->cid_num[0])
557 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
558 if (call->cid_name[0])
559 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
560 if (call->cid_rdnis[0])
562 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
563 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
564 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
566 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
568 case AST_PRES_ALLOWED:
569 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
571 case AST_PRES_RESTRICTED:
572 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
574 case AST_PRES_UNAVAILABLE:
575 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
578 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
580 switch(ast->cid.cid_ton)
583 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
586 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
589 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
592 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
594 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
595 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
597 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
599 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
600 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
602 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
603 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
604 send_message(MESSAGE_SETUP, call->ref, &newparam);
606 /* change to outgoing setup state */
607 call->state = CHAN_LCR_STATE_OUT_SETUP;
611 * send dialing info to LCR
612 * this function is called, when setup acknowledge is received and dialing
615 static void send_dialque_to_lcr(struct chan_call *call)
617 union parameter newparam;
619 if (!call->ast || !call->ref || !call->dialque[0])
622 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
624 /* send setup message to LCR */
625 memset(&newparam, 0, sizeof(union parameter));
626 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
627 call->dialque[0] = '\0';
628 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
632 * in case of a bridge, the unsupported message can be forwarded directly
633 * to the remote call.
635 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
639 if (!call->bridge_call) return;
640 CDEBUG(call, NULL, "Sending message due briding.\n");
641 send_message(message_type, call->bridge_call->ref, param);
645 * send release message to LCR and import bchannel if exported
647 static void send_release_and_import(struct chan_call *call, int cause, int location)
649 union parameter newparam;
651 /* importing channel */
652 if (call->bchannel) {
653 memset(&newparam, 0, sizeof(union parameter));
654 newparam.bchannel.type = BCHANNEL_RELEASE;
655 newparam.bchannel.handle = call->bchannel->handle;
656 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
658 /* sending release */
659 memset(&newparam, 0, sizeof(union parameter));
660 newparam.disconnectinfo.cause = cause;
661 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
662 send_message(MESSAGE_RELEASE, call->ref, &newparam);
666 * check if extension matches and start asterisk
667 * if it can match, proceed
670 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
673 union parameter newparam;
675 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", ast->exten, ast->context, complete?"yes":"no");
680 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
682 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
686 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
688 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);
692 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
693 /* send setup acknowledge to lcr */
694 memset(&newparam, 0, sizeof(union parameter));
695 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
698 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
703 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
705 /* send setup acknowledge to lcr */
706 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
707 memset(&newparam, 0, sizeof(union parameter));
708 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
712 call->state = CHAN_LCR_STATE_IN_DIALING;
714 /* if match, start pbx */
715 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
716 CDEBUG(call, ast, "Extensions matches.\n");
721 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
729 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
730 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
732 /* release asterisk */
733 ast->hangupcause = call->cause;
734 /* change to release state */
735 call->state = CHAN_LCR_STATE_RELEASE;
736 ast_hangup(ast); // call will be destroyed here
740 /* send setup to asterisk */
741 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
742 ret = ast_pbx_start(ast);
745 cause = (ret==-2)?34:27;
748 call->pbx_started = 1;
749 // if (call->state == CHAN_LCR_STATE_IN_DIALING)
750 ast_setstate(ast, AST_STATE_RINGING);
752 // ast_setstate(ast, AST_STATE_RINGING);
757 * incoming setup from LCR
759 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
761 struct ast_channel *ast;
763 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
765 /* create asterisk channel instrance */
766 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
770 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
771 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
778 ast->tech_pvt = call;
779 ast->tech = &lcr_tech;
780 ast->fds[0] = call->pipe[0];
782 /* fill setup information */
783 if (param->setup.dialinginfo.id)
784 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
785 if (param->setup.context[0])
786 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
788 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
789 if (param->setup.callerinfo.id[0])
790 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
791 if (param->setup.callerinfo.name[0])
792 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
793 if (param->setup.redirinfo.id[0])
794 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
795 switch (param->setup.callerinfo.present)
797 case INFO_PRESENT_ALLOWED:
798 ast->cid.cid_pres = AST_PRES_ALLOWED;
800 case INFO_PRESENT_RESTRICTED:
801 ast->cid.cid_pres = AST_PRES_RESTRICTED;
804 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
806 switch (param->setup.callerinfo.ntype)
808 case INFO_NTYPE_SUBSCRIBER:
809 ast->cid.cid_ton = 4;
811 case INFO_NTYPE_NATIONAL:
812 ast->cid.cid_ton = 2;
814 case INFO_NTYPE_INTERNATIONAL:
815 ast->cid.cid_ton = 1;
818 ast->cid.cid_ton = 0;
820 ast->transfercapability = param->setup.capainfo.bearer_capa;
821 /* enable hdlc if transcap is data */
822 if (param->setup.capainfo.source_mode == B_MODE_HDLC)
824 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
826 /* configure channel */
827 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
828 ast->readformat = ast->rawreadformat = ast->nativeformats;
829 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
831 ast->hangupcause = 0;
834 call->state = CHAN_LCR_STATE_IN_SETUP;
836 if (!call->pbx_started)
837 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
841 * incoming setup acknowledge from LCR
843 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
845 if (!call->ast) return;
847 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
849 /* send pending digits in dialque */
850 if (call->dialque[0])
851 send_dialque_to_lcr(call);
852 /* change to overlap state */
853 call->state = CHAN_LCR_STATE_OUT_DIALING;
857 * incoming proceeding from LCR
859 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
861 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
864 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
865 /* queue event for asterisk */
866 if (call->ast && call->pbx_started)
867 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
871 * incoming alerting from LCR
873 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
875 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
878 call->state = CHAN_LCR_STATE_OUT_ALERTING;
879 /* queue event to asterisk */
880 if (call->ast && call->pbx_started)
881 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
885 * incoming connect from LCR
887 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
889 union parameter newparam;
891 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
894 call->state = CHAN_LCR_STATE_CONNECT;
895 /* request bchannel */
896 if (!call->bchannel) {
897 CDEBUG(call, call->ast, "Requesting B-channel.\n");
898 memset(&newparam, 0, sizeof(union parameter));
899 newparam.bchannel.type = BCHANNEL_REQUEST;
900 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
902 /* copy connectinfo */
903 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
904 /* queue event to asterisk */
905 if (call->ast && call->pbx_started)
906 strncat(call->queue_string, "A", sizeof(call->queue_string)-1);
910 * incoming disconnect from LCR
912 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
914 struct ast_channel *ast = call->ast;
916 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
919 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
921 call->cause = param->disconnectinfo.cause;
922 call->location = param->disconnectinfo.location;
923 /* if bridge, forward disconnect and return */
926 if (call->bridge_call)
928 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
929 bridge_message_if_bridged(call, message_type, param);
933 /* release lcr with same cause */
934 send_release_and_import(call, call->cause, call->location);
936 /* change to release state */
937 call->state = CHAN_LCR_STATE_RELEASE;
938 /* queue release asterisk */
941 ast->hangupcause = call->cause;
942 if (call->pbx_started)
943 strcpy(call->queue_string, "H"); // overwrite other indications
945 ast_hangup(ast); // call will be destroyed here
951 * incoming setup acknowledge from LCR
953 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
955 struct ast_channel *ast = call->ast;
957 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
961 /* change to release state */
962 call->state = CHAN_LCR_STATE_RELEASE;
963 /* copy release info */
966 call->cause = param->disconnectinfo.cause;
967 call->location = param->disconnectinfo.location;
969 /* if we have an asterisk instance, queue hangup, else we are done */
972 ast->hangupcause = call->cause;
973 if (call->pbx_started)
974 strcpy(call->queue_string, "H");
976 ast_hangup(ast); // call will be destroyed here
986 * incoming information from LCR
988 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
990 struct ast_channel *ast = call->ast;
992 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
996 /* pbx not started */
997 if (!call->pbx_started)
999 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1000 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1001 lcr_start_pbx(call, ast, param->information.sending_complete);
1005 /* change dailing state after setup */
1006 if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1007 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1008 call->state = CHAN_LCR_STATE_IN_DIALING;
1009 // ast_setstate(ast, AST_STATE_DIALING);
1013 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
1014 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1016 /* use bridge to forware message not supported by asterisk */
1017 if (call->state == CHAN_LCR_STATE_CONNECT) {
1018 CDEBUG(call, call->ast, "Call is connected, briding.\n");
1019 bridge_message_if_bridged(call, message_type, param);
1024 * incoming information from LCR
1026 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1028 union parameter newparam;
1030 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1032 /* request bchannel, if call is resumed and we don't have it */
1033 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1034 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1035 memset(&newparam, 0, sizeof(union parameter));
1036 newparam.bchannel.type = BCHANNEL_REQUEST;
1037 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1040 if (!call->ast) return;
1042 /* use bridge to forware message not supported by asterisk */
1043 bridge_message_if_bridged(call, message_type, param);
1047 * incoming information from LCR
1049 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1051 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1053 if (!call->ast) return;
1055 /* use bridge to forware message not supported by asterisk */
1056 bridge_message_if_bridged(call, message_type, param);
1060 * got dtmf from bchannel (locked state)
1062 void lcr_in_dtmf(struct chan_call *call, int val)
1064 struct ast_channel *ast = call->ast;
1069 if (!call->pbx_started)
1072 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1075 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1079 * message received from LCR
1081 int receive_message(int message_type, unsigned int ref, union parameter *param)
1083 struct bchannel *bchannel;
1084 struct chan_call *call;
1085 union parameter newparam;
1087 memset(&newparam, 0, sizeof(union parameter));
1089 /* handle bchannel message*/
1090 if (message_type == MESSAGE_BCHANNEL)
1092 switch(param->bchannel.type)
1094 case BCHANNEL_ASSIGN:
1095 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1096 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1098 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1101 /* create bchannel */
1102 bchannel = alloc_bchannel(param->bchannel.handle);
1105 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1109 /* configure channel */
1110 bchannel->b_tx_gain = param->bchannel.tx_gain;
1111 bchannel->b_rx_gain = param->bchannel.rx_gain;
1112 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1113 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1115 bchannel->b_bf_len = param->bchannel.crypt_len;
1116 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1118 bchannel->b_txdata = 0;
1119 bchannel->b_dtmf = 1;
1120 bchannel->b_tx_dejitter = 1;
1122 /* in case, ref is not set, this bchannel instance must
1123 * be created until it is removed again by LCR */
1125 call = find_call_ref(ref);
1128 bchannel->call = call;
1129 call->bchannel = bchannel;
1131 bchannel_dtmf(bchannel, 1);
1133 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1134 if (call->pipeline[0])
1135 bchannel_pipeline(bchannel, call->pipeline);
1137 bchannel_gain(bchannel, call->rx_gain, 0);
1139 bchannel_gain(bchannel, call->tx_gain, 1);
1140 if (call->bridge_id) {
1141 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1142 bchannel_join(bchannel, call->bridge_id);
1144 /* create only, if call exists, othewhise it bchannel is freed below... */
1145 if (bchannel_create(bchannel, ((call->nodsp)?1:0) + ((call->hdlc)?2:0)))
1146 bchannel_activate(bchannel, 1);
1149 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1150 newparam.bchannel.handle = param->bchannel.handle;
1151 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1152 /* if call has released before bchannel is assigned */
1154 newparam.bchannel.type = BCHANNEL_RELEASE;
1155 newparam.bchannel.handle = param->bchannel.handle;
1156 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1161 case BCHANNEL_REMOVE:
1162 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1163 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1165 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1168 /* unklink from call and destroy bchannel */
1169 free_bchannel(bchannel);
1172 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1173 newparam.bchannel.handle = param->bchannel.handle;
1174 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1179 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1184 /* handle new ref */
1185 if (message_type == MESSAGE_NEWREF)
1187 if (param->direction)
1189 /* new ref from lcr */
1190 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1191 if (!ref || find_call_ref(ref))
1193 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1196 /* allocate new call instance */
1197 call = alloc_call();
1199 call->state = CHAN_LCR_STATE_IN_PREPARE;
1202 /* wait for setup (or release from asterisk) */
1205 /* new ref, as requested from this remote application */
1206 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1207 call = find_call_ref(0);
1210 /* send release, if ref does not exist */
1211 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1212 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1217 /* send pending setup info */
1218 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1219 send_setup_to_lcr(call);
1220 /* release if asterisk has signed off */
1221 else if (call->state == CHAN_LCR_STATE_RELEASE)
1225 send_release_and_import(call, call->cause, call->location);
1227 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1239 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1242 call = find_call_ref(ref);
1245 /* ignore ref that is not used (anymore) */
1246 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1250 /* handle messages */
1251 switch(message_type)
1254 lcr_in_setup(call, message_type, param);
1257 case MESSAGE_OVERLAP:
1258 lcr_in_overlap(call, message_type, param);
1261 case MESSAGE_PROCEEDING:
1262 lcr_in_proceeding(call, message_type, param);
1265 case MESSAGE_ALERTING:
1266 lcr_in_alerting(call, message_type, param);
1269 case MESSAGE_CONNECT:
1270 lcr_in_connect(call, message_type, param);
1273 case MESSAGE_DISCONNECT:
1274 lcr_in_disconnect(call, message_type, param);
1277 case MESSAGE_RELEASE:
1278 lcr_in_release(call, message_type, param);
1281 case MESSAGE_INFORMATION:
1282 lcr_in_information(call, message_type, param);
1285 case MESSAGE_NOTIFY:
1286 lcr_in_notify(call, message_type, param);
1289 case MESSAGE_FACILITY:
1290 lcr_in_facility(call, message_type, param);
1293 case MESSAGE_PATTERN: // audio available from LCR
1296 case MESSAGE_NOPATTERN: // audio not available from LCR
1299 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1300 call->audiopath = param->audiopath;
1304 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1311 * release all calls (due to broken socket)
1313 static void release_all_calls(void)
1315 struct chan_call *call;
1320 /* no ast, so we may directly free call */
1322 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1326 /* already in release process */
1327 if (call->state == CHAN_LCR_STATE_RELEASE) {
1331 /* release or queue release */
1333 call->state = CHAN_LCR_STATE_RELEASE;
1334 if (!call->pbx_started) {
1335 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1336 ast_hangup(call->ast); // call will be destroyed here
1339 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1340 strcpy(call->queue_string, "H");
1344 /* release all bchannels */
1345 while(bchannel_first)
1346 free_bchannel(bchannel_first);
1351 * warning! not thread safe
1352 * returns -1 for socket error, 0 for no work, 1 for work
1354 int handle_socket(void)
1358 struct admin_list *admin;
1359 struct admin_message msg;
1361 /* read from socket */
1362 len = read(lcr_sock, &msg, sizeof(msg));
1365 CERROR(NULL, NULL, "Socket closed.\n");
1366 return(-1); // socket closed
1370 if (len != sizeof(msg))
1372 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1373 return(-1); // socket error
1375 if (msg.message != ADMIN_MESSAGE)
1377 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1380 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1384 if (errno != EWOULDBLOCK)
1386 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1391 /* write to socket */
1394 admin = admin_first;
1395 len = write(lcr_sock, &admin->msg, sizeof(msg));
1398 CERROR(NULL, NULL, "Socket closed.\n");
1399 return(-1); // socket closed
1403 if (len != sizeof(msg))
1405 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1406 return(-1); // socket error
1409 admin_first = admin->next;
1415 if (errno != EWOULDBLOCK)
1417 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1426 * open and close socket and thread
1428 int open_socket(void)
1431 char *socket_name = SOCKET_NAME;
1433 struct sockaddr_un sock_address;
1434 unsigned int on = 1;
1435 union parameter param;
1438 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1440 CERROR(NULL, NULL, "Failed to create socket.\n");
1444 /* set socket address and name */
1445 memset(&sock_address, 0, sizeof(sock_address));
1446 sock_address.sun_family = PF_UNIX;
1447 strcpy(sock_address.sun_path, socket_name);
1449 /* connect socket */
1450 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1454 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1458 /* set non-blocking io */
1459 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1463 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1467 /* enque hello message */
1468 memset(¶m, 0, sizeof(param));
1469 strcpy(param.hello.application, "asterisk");
1470 send_message(MESSAGE_HELLO, 0, ¶m);
1475 void close_socket(void)
1477 struct admin_list *admin, *temp;
1479 /* flush pending messages */
1480 admin = admin_first;
1483 admin = admin->next;
1495 /* sending queue to asterisk */
1496 static int queue_send(void)
1499 struct chan_call *call;
1500 struct ast_channel *ast;
1501 struct ast_frame fr;
1506 p = call->queue_string;
1509 /* there is something to queue */
1510 if (!ast_channel_trylock(ast)) { /* succeed */
1514 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1515 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1518 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1519 ast_queue_control(ast, AST_CONTROL_RINGING);
1522 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1523 ast_queue_control(ast, AST_CONTROL_ANSWER);
1526 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1527 ast_queue_hangup(ast);
1529 case '1': case '2': case '3': case 'a':
1530 case '4': case '5': case '6': case 'b':
1531 case '7': case '8': case '9': case 'c':
1532 case '*': case '0': case '#': case 'd':
1533 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1534 /* send digit to asterisk */
1535 memset(&fr, 0, sizeof(fr));
1536 fr.frametype = AST_FRAME_DTMF_BEGIN;
1538 fr.delivery = ast_tv(0, 0);
1539 ast_queue_frame(ast, &fr);
1540 fr.frametype = AST_FRAME_DTMF_END;
1541 ast_queue_frame(ast, &fr);
1544 CDEBUG(call, ast, "Ignoring queued digit 0x%02d.\n", *p);
1548 call->queue_string[0] = '\0';
1549 ast_channel_unlock(ast);
1559 /* signal handler */
1560 void sighandler(int sigset)
1564 /* chan_lcr thread */
1565 static void *chan_thread(void *arg)
1569 union parameter param;
1570 time_t retry = 0, now;
1572 bchannel_pid = getpid();
1574 // signal(SIGPIPE, sighandler);
1576 memset(¶m, 0, sizeof(union parameter));
1580 ast_mutex_lock(&chan_lock);
1587 ret = handle_socket();
1589 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1591 release_all_calls();
1598 if (retry && now-retry > 5) {
1599 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1601 if (open_socket() < 0) {
1610 ret = bchannel_handle();
1614 /* handle messages to asterisk */
1619 /* delay if no work done */
1621 ast_mutex_unlock(&chan_lock);
1623 ast_mutex_lock(&chan_lock);
1629 CERROR(NULL, NULL, "Thread exit.\n");
1631 ast_mutex_unlock(&chan_lock);
1633 // signal(SIGPIPE, SIG_DFL);
1639 * new asterisk instance
1642 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1644 char exten[256], *dial, *interface, *opt;
1645 struct ast_channel *ast;
1646 struct chan_call *call;
1648 ast_mutex_lock(&chan_lock);
1649 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1651 /* if socket is closed */
1654 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1655 ast_mutex_unlock(&chan_lock);
1659 /* create call instance */
1660 call = alloc_call();
1663 /* failed to create instance */
1664 ast_mutex_unlock(&chan_lock);
1668 /* create asterisk channel instrance */
1669 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1672 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1674 /* failed to create instance */
1675 ast_mutex_unlock(&chan_lock);
1678 ast->tech = &lcr_tech;
1679 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1680 /* configure channel */
1681 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1682 ast->readformat = ast->rawreadformat = ast->nativeformats;
1683 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1685 ast->hangupcause = 0;
1689 ast->tech_pvt = call;
1690 ast->fds[0] = call->pipe[0];
1691 call->pbx_started = 0;
1693 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1696 * Extract interface, dialstring, options from data.
1699 * <interface>/<dialstring>
1700 * <interface>/<dialstring>/options
1702 strncpy(exten, (char *)data, sizeof(exten)-1);
1703 exten[sizeof(exten)-1] = '\0';
1704 if ((dial = strchr(exten, '/'))) {
1707 if ((opt = strchr(dial, '/')))
1716 strncpy(call->interface, interface, sizeof(call->interface)-1);
1717 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1718 apply_opt(call, (char *)opt);
1720 ast_mutex_unlock(&chan_lock);
1725 * call from asterisk
1727 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1729 union parameter newparam;
1730 struct chan_call *call;
1732 ast_mutex_lock(&chan_lock);
1733 call = ast->tech_pvt;
1735 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1736 ast_mutex_unlock(&chan_lock);
1740 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1742 /* pbx process is started */
1743 call->pbx_started = 1;
1744 /* send MESSAGE_NEWREF */
1745 memset(&newparam, 0, sizeof(union parameter));
1746 newparam.direction = 0; /* request from app */
1747 send_message(MESSAGE_NEWREF, 0, &newparam);
1749 /* set hdlc if capability requires hdlc */
1750 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1751 || ast->transfercapability == INFO_BC_DATARESTRICTED
1752 || ast->transfercapability == INFO_BC_VIDEO)
1754 /* if hdlc is forced by option, we change transcap to data */
1756 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1757 && ast->transfercapability != INFO_BC_DATARESTRICTED
1758 && ast->transfercapability != INFO_BC_VIDEO)
1759 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1761 call->cid_num[0] = 0;
1762 call->cid_name[0] = 0;
1763 call->cid_rdnis[0] = 0;
1765 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1766 strncpy(call->cid_num, ast->cid.cid_num,
1767 sizeof(call->cid_num)-1);
1769 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1770 strncpy(call->cid_name, ast->cid.cid_name,
1771 sizeof(call->cid_name)-1);
1772 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1773 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1774 sizeof(call->cid_rdnis)-1);
1776 ast_mutex_unlock(&chan_lock);
1780 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1782 struct chan_call *call;
1783 union parameter newparam;
1786 /* only pass IA5 number space */
1787 if (digit > 126 || digit < 32)
1790 ast_mutex_lock(&chan_lock);
1791 call = ast->tech_pvt;
1793 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1794 ast_mutex_unlock(&chan_lock);
1798 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1800 /* send information or queue them */
1801 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1803 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1804 memset(&newparam, 0, sizeof(union parameter));
1805 newparam.information.id[0] = digit;
1806 newparam.information.id[1] = '\0';
1807 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1810 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1812 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1814 strncat(call->dialque, buf, strlen(call->dialque)-1);
1817 ast_mutex_unlock(&chan_lock);
1821 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1823 printf("DIGIT END %c\n", digit);
1827 static int lcr_answer(struct ast_channel *ast)
1829 union parameter newparam;
1830 struct chan_call *call;
1832 ast_mutex_lock(&chan_lock);
1833 call = ast->tech_pvt;
1835 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1836 ast_mutex_unlock(&chan_lock);
1840 CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
1842 /* copy connectinfo, if bridged */
1843 if (call->bridge_call)
1844 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1845 /* send connect message to lcr */
1846 if (call->state != CHAN_LCR_STATE_CONNECT) {
1847 memset(&newparam, 0, sizeof(union parameter));
1848 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1849 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1850 call->state = CHAN_LCR_STATE_CONNECT;
1853 /* request bchannel */
1854 if (!call->bchannel) {
1855 CDEBUG(call, ast, "Requesting B-channel.\n");
1856 memset(&newparam, 0, sizeof(union parameter));
1857 newparam.bchannel.type = BCHANNEL_REQUEST;
1858 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1861 // memset(&newparam, 0, sizeof(union parameter));
1862 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1865 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1869 ast_mutex_unlock(&chan_lock);
1873 static int lcr_hangup(struct ast_channel *ast)
1875 struct chan_call *call;
1876 pthread_t tid = pthread_self();
1878 if (!pthread_equal(tid, chan_tid))
1879 ast_mutex_lock(&chan_lock);
1880 call = ast->tech_pvt;
1882 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1883 if (!pthread_equal(tid, chan_tid))
1884 ast_mutex_unlock(&chan_lock);
1888 if (!pthread_equal(tid, chan_tid))
1889 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1891 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1893 /* disconnect asterisk, maybe not required */
1894 ast->tech_pvt = NULL;
1899 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1900 if (ast->hangupcause > 0)
1901 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1903 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1906 if (!pthread_equal(tid, chan_tid))
1907 ast_mutex_unlock(&chan_lock);
1911 /* ref is not set, due to prepare setup or release */
1912 if (call->state == CHAN_LCR_STATE_RELEASE)
1914 /* we get the response to our release */
1915 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1919 /* during prepare, we change to release state */
1920 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1921 call->state = CHAN_LCR_STATE_RELEASE;
1924 if (!pthread_equal(tid, chan_tid))
1925 ast_mutex_unlock(&chan_lock);
1929 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1931 struct chan_call *call;
1934 CDEBUG(NULL, ast, "No subclass\n");
1935 if (!(f->subclass & ast->nativeformats))
1936 CDEBUG(NULL, ast, "Unexpected format.\n");
1938 ast_mutex_lock(&chan_lock);
1939 call = ast->tech_pvt;
1941 ast_mutex_unlock(&chan_lock);
1944 if (call->bchannel && f->samples)
1945 bchannel_transmit(call->bchannel, f->data, f->samples);
1946 ast_mutex_unlock(&chan_lock);
1951 static struct ast_frame *lcr_read(struct ast_channel *ast)
1953 struct chan_call *call;
1956 ast_mutex_lock(&chan_lock);
1957 call = ast->tech_pvt;
1959 ast_mutex_unlock(&chan_lock);
1962 if (call->pipe[0] > -1) {
1963 if (call->rebuffer && !call->hdlc) {
1964 len = read(call->pipe[0], call->read_buff, 160);
1966 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1968 if (len < 0 && errno == EAGAIN) {
1969 ast_mutex_unlock(&chan_lock);
1970 return &ast_null_frame;
1973 close(call->pipe[0]);
1975 ast_mutex_unlock(&chan_lock);
1980 call->read_fr.frametype = AST_FRAME_VOICE;
1981 call->read_fr.subclass = ast->nativeformats;
1982 call->read_fr.datalen = len;
1983 call->read_fr.samples = len;
1984 call->read_fr.delivery = ast_tv(0,0);
1985 call->read_fr.data = call->read_buff;
1986 ast_mutex_unlock(&chan_lock);
1988 return &call->read_fr;
1991 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1993 union parameter newparam;
1995 struct chan_call *call;
1997 ast_mutex_lock(&chan_lock);
1998 call = ast->tech_pvt;
2000 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2001 ast_mutex_unlock(&chan_lock);
2006 case AST_CONTROL_BUSY:
2007 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2008 ast_setstate(ast, AST_STATE_BUSY);
2009 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2010 /* send message to lcr */
2011 memset(&newparam, 0, sizeof(union parameter));
2012 newparam.disconnectinfo.cause = 17;
2013 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2014 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2016 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2019 case AST_CONTROL_CONGESTION:
2020 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2021 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2022 /* send message to lcr */
2023 memset(&newparam, 0, sizeof(union parameter));
2024 newparam.disconnectinfo.cause = ast->hangupcause;
2025 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2026 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2028 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2031 case AST_CONTROL_PROCEEDING:
2032 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2033 if (call->state == CHAN_LCR_STATE_IN_SETUP
2034 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2035 /* send message to lcr */
2036 memset(&newparam, 0, sizeof(union parameter));
2037 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2039 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2042 case AST_CONTROL_RINGING:
2043 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2044 ast_setstate(ast, AST_STATE_RINGING);
2045 if (call->state == CHAN_LCR_STATE_IN_SETUP
2046 || call->state == CHAN_LCR_STATE_IN_DIALING
2047 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2048 /* send message to lcr */
2049 memset(&newparam, 0, sizeof(union parameter));
2050 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2052 call->state = CHAN_LCR_STATE_IN_ALERTING;
2056 CDEBUG(call, ast, "Received indicate -1.\n");
2060 case AST_CONTROL_VIDUPDATE:
2061 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2064 case AST_CONTROL_HOLD:
2065 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2066 /* send message to lcr */
2067 memset(&newparam, 0, sizeof(union parameter));
2068 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2069 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2071 /*start music onhold*/
2072 ast_moh_start(ast,data,ast->musicclass);
2074 case AST_CONTROL_UNHOLD:
2075 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2076 /* send message to lcr */
2077 memset(&newparam, 0, sizeof(union parameter));
2078 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2079 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2086 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2092 ast_mutex_unlock(&chan_lock);
2099 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
2101 struct chan_call *call;
2107 ast_mutex_lock(&chan_lock);
2108 call = ast->tech_pvt;
2110 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
2111 ast_mutex_unlock(&chan_lock);
2115 CDEBUG(call, ast, "Received fixup from Asterisk.\n");
2117 ast_mutex_unlock(&chan_lock);
2122 * send_text asterisk
2124 static int lcr_send_text(struct ast_channel *ast, const char *text)
2126 struct chan_call *call;
2127 union parameter newparam;
2129 ast_mutex_lock(&chan_lock);
2130 call = ast->tech_pvt;
2132 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2133 ast_mutex_unlock(&chan_lock);
2137 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2138 memset(&newparam, 0, sizeof(union parameter));
2139 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2140 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2141 ast_mutex_lock(&chan_lock);
2148 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2149 struct ast_channel *ast2, int flags,
2150 struct ast_frame **fo,
2151 struct ast_channel **rc, int timeoutms)
2154 struct chan_call *call1, *call2;
2155 struct ast_channel *carr[2], *who;
2157 struct ast_frame *f;
2160 CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
2165 /* join via dsp (if the channels are currently open) */
2166 ast_mutex_lock(&chan_lock);
2167 call1 = ast1->tech_pvt;
2168 call2 = ast2->tech_pvt;
2169 if (!call1 || !call2) {
2170 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
2171 ast_mutex_unlock(&chan_lock);
2172 return AST_BRIDGE_COMPLETE;
2175 /* join, if both call instances uses dsp */
2176 if (!call1->nodsp && !call2->nodsp) {
2177 CDEBUG(NULL, NULL, "Both calls use DSP, briding via DSP.\n");
2179 /* get bridge id and join */
2180 bridge_id = new_bridge_id();
2182 call1->bridge_id = bridge_id;
2183 if (call1->bchannel)
2184 bchannel_join(call1->bchannel, bridge_id);
2186 call2->bridge_id = bridge_id;
2187 if (call2->bchannel)
2188 bchannel_join(call2->bchannel, bridge_id);
2190 if (call1->nodsp && call2->nodsp)
2191 CDEBUG(NULL, NULL, "Both calls use no DSP, briding in channel driver.\n");
2193 CDEBUG(NULL, NULL, "One call uses no DSP, briding in channel driver.\n");
2194 call1->bridge_call = call2;
2195 call2->bridge_call = call1;
2197 if (call1->state == CHAN_LCR_STATE_IN_SETUP
2198 || call1->state == CHAN_LCR_STATE_IN_DIALING
2199 || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
2200 || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
2201 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2204 if (call2->state == CHAN_LCR_STATE_IN_SETUP
2205 || call2->state == CHAN_LCR_STATE_IN_DIALING
2206 || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
2207 || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
2208 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
2212 ast_mutex_unlock(&chan_lock);
2216 who = ast_waitfor_n(carr, 2, &to);
2219 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2224 if (!f || f->frametype == AST_FRAME_CONTROL) {
2226 CDEBUG(NULL, NULL, "Got hangup.\n");
2228 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2235 if ( f->frametype == AST_FRAME_DTMF ) {
2236 CDEBUG(NULL, NULL, "Got DTMF.\n");
2252 CDEBUG(NULL, NULL, "Releasing bride.\n");
2254 /* split channels */
2255 ast_mutex_lock(&chan_lock);
2256 call1 = ast1->tech_pvt;
2257 call2 = ast2->tech_pvt;
2258 if (call1 && call1->bridge_id)
2260 call1->bridge_id = 0;
2261 if (call1->bchannel)
2262 bchannel_join(call1->bchannel, 0);
2263 if (call1->bridge_call)
2264 call1->bridge_call->bridge_call = NULL;
2266 if (call2 && call1->bridge_id)
2268 call2->bridge_id = 0;
2269 if (call2->bchannel)
2270 bchannel_join(call2->bchannel, 0);
2271 if (call2->bridge_call)
2272 call2->bridge_call->bridge_call = NULL;
2274 call1->bridge_call = NULL;
2275 call2->bridge_call = NULL;
2277 ast_mutex_unlock(&chan_lock);
2278 return AST_BRIDGE_COMPLETE;
2280 static struct ast_channel_tech lcr_tech = {
2282 .description="Channel driver for connecting to Linux-Call-Router",
2283 .requester=lcr_request,
2284 .send_digit_begin=lcr_digit_begin,
2285 .send_digit_end=lcr_digit_end,
2292 .indicate=lcr_indicate,
2294 .send_text=lcr_send_text,
2303 static int lcr_show_lcr (int fd, int argc, char *argv[])
2308 static int lcr_show_calls (int fd, int argc, char *argv[])
2313 static int lcr_reload_routing (int fd, int argc, char *argv[])
2318 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2323 static int lcr_port_block (int fd, int argc, char *argv[])
2328 static int lcr_port_unblock (int fd, int argc, char *argv[])
2333 static int lcr_port_unload (int fd, int argc, char *argv[])
2338 static struct ast_cli_entry cli_show_lcr =
2339 { {"lcr", "show", "lcr", NULL},
2341 "Shows current states of LCR core",
2342 "Usage: lcr show lcr\n",
2345 static struct ast_cli_entry cli_show_calls =
2346 { {"lcr", "show", "calls", NULL},
2348 "Shows current calls made by LCR and Asterisk",
2349 "Usage: lcr show calls\n",
2352 static struct ast_cli_entry cli_reload_routing =
2353 { {"lcr", "reload", "routing", NULL},
2355 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2356 "Usage: lcr reload routing\n",
2359 static struct ast_cli_entry cli_reload_interfaces =
2360 { {"lcr", "reload", "interfaces", NULL},
2361 lcr_reload_interfaces,
2362 "Reloads interfaces conf of LCR",
2363 "Usage: lcr reload interfaces\n",
2366 static struct ast_cli_entry cli_port_block =
2367 { {"lcr", "port", "block", NULL},
2369 "Blocks LCR port for further calls",
2370 "Usage: lcr port block \"<port>\"\n",
2373 static struct ast_cli_entry cli_port_unblock =
2374 { {"lcr", "port", "unblock", NULL},
2376 "Unblocks or loads LCR port, port is opened my mISDN",
2377 "Usage: lcr port unblock \"<port>\"\n",
2380 static struct ast_cli_entry cli_port_unload =
2381 { {"lcr", "port", "unload", NULL},
2383 "Unloads LCR port, port is closes by mISDN",
2384 "Usage: lcr port unload \"<port>\"\n",
2390 static int lcr_config_exec(struct ast_channel *ast, void *data)
2392 struct chan_call *call;
2394 ast_mutex_lock(&chan_lock);
2395 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2399 if (call->ast == ast)
2404 apply_opt(call, (char *)data);
2406 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2408 ast_mutex_unlock(&chan_lock);
2413 * module loading and destruction
2415 int load_module(void)
2419 for (i = 0; i < 256; i++) {
2420 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2421 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2424 if (read_options() == 0) {
2425 CERROR(NULL, NULL, "%s", options_error);
2426 return AST_MODULE_LOAD_DECLINE;
2429 ast_mutex_init(&chan_lock);
2430 ast_mutex_init(&log_lock);
2432 if (open_socket() < 0) {
2433 /* continue with closed socket */
2436 if (bchannel_initialize()) {
2437 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2439 return AST_MODULE_LOAD_DECLINE;
2443 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2444 if (ast_channel_register(&lcr_tech)) {
2445 CERROR(NULL, NULL, "Unable to register channel class\n");
2446 bchannel_deinitialize();
2448 return AST_MODULE_LOAD_DECLINE;
2451 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2452 "lcr_config(<opt><optarg>:<opt>:...)\n"
2453 "Sets LCR opts. and optargs\n"
2455 "The available options are:\n"
2456 " d - Send display text on called phone, text is the optarg.\n"
2457 " n - Don't detect dtmf tones on called channel.\n"
2458 " h - Force data call (HDLC).\n"
2459 " t - Disable mISDN_dsp features (required for fax application).\n"
2460 " c - Make crypted outgoing call, optarg is keyindex.\n"
2461 " e - Perform echo cancelation on this channel.\n"
2462 " Takes mISDN pipeline option as optarg.\n"
2463 // " s - Send Non Inband DTMF as inband.\n"
2464 " vr - rxgain control\n"
2465 " vt - txgain control\n"
2466 " Volume changes at factor 2 ^ optarg.\n"
2471 ast_cli_register(&cli_show_lcr);
2472 ast_cli_register(&cli_show_calls);
2473 ast_cli_register(&cli_reload_routing);
2474 ast_cli_register(&cli_reload_interfaces);
2475 ast_cli_register(&cli_port_block);
2476 ast_cli_register(&cli_port_unblock);
2477 ast_cli_register(&cli_port_unload);
2481 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2483 /* failed to create thread */
2484 bchannel_deinitialize();
2486 ast_channel_unregister(&lcr_tech);
2487 return AST_MODULE_LOAD_DECLINE;
2492 int unload_module(void)
2494 /* First, take us out of the channel loop */
2495 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2498 pthread_join(chan_tid, NULL);
2500 ast_channel_unregister(&lcr_tech);
2502 ast_unregister_application("lcr_config");
2505 if (mISDN_created) {
2506 bchannel_deinitialize();
2510 if (lcr_sock >= 0) {
2518 int reload_module(void)
2525 #define AST_MODULE "chan_lcr"
2527 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2528 .load = load_module,
2529 .unload = unload_module,
2530 .reload = reload_module,