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 CDEBUG(*callp, NULL, "Call instance allocated.\n");
303 unsigned short new_bridge_id(void)
305 struct chan_call *call;
306 unsigned short id = 1;
308 /* search for lowest bridge id that is not in use and not 0 */
314 if (call->bridge_id == id)
322 CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
327 * enque message to LCR
329 int send_message(int message_type, unsigned int ref, union parameter *param)
331 struct admin_list *admin, **adminp;
334 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
337 CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
339 adminp = &admin_first;
341 adminp = &((*adminp)->next);
342 admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
344 CERROR(NULL, NULL, "No memory for message to LCR.\n");
349 admin->msg.message = ADMIN_MESSAGE;
350 admin->msg.u.msg.type = message_type;
351 admin->msg.u.msg.ref = ref;
352 memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
358 * apply options (in locked state)
360 void apply_opt(struct chan_call *call, char *data)
362 union parameter newparam;
363 char string[1024], *p = string, *opt, *key;
364 int gain, i, newmode = 0;
369 strncpy(string, data, sizeof(string)-1);
370 string[sizeof(string)-1] = '\0';
373 while((opt = strsep(&p, ":")))
377 if (opt[1] == '\0') {
378 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
381 CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
382 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
383 strncpy(call->display, opt+1, sizeof(call->display)-1);
385 memset(&newparam, 0, sizeof(union parameter));
386 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
387 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
391 if (opt[1] != '\0') {
392 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
395 CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
399 if (opt[1] == '\0') {
400 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
404 /* check for 0xXXXX... type of key */
405 if (!!strncmp((char *)key, "0x", 2)) {
406 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
410 if (strlen(key) > 56*2 || (strlen(key) % 1)) {
411 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
417 if (*key>='0' && *key<='9')
418 call->bf_key[i] = (*key-'0') << 8;
419 else if (*key>='a' && *key<='f')
420 call->bf_key[i] = (*key-'a'+10) << 8;
421 else if (*key>='A' && *key<='F')
422 call->bf_key[i] = (*key-'A'+10) << 8;
426 if (*key>='0' && *key<='9')
427 call->bf_key[i] += (*key - '0');
428 else if (*key>='a' && *key<='f')
429 call->bf_key[i] += (*key - 'a' + 10);
430 else if (*key>='A' && *key<='F')
431 call->bf_key[i] += (*key - 'A' + 10);
438 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
442 CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
444 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
447 if (opt[1] != '\0') {
448 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
451 CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
458 if (opt[1] != '\0') {
459 CERROR(call, call->ast, "Option 't' (transparent) expects no parameter.\n", opt);
462 CDEBUG(call, call->ast, "Option 't' (transparent).\n");
463 if (!call->transparent) {
464 call->transparent = 1;
469 if (opt[1] == '\0') {
470 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
473 CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
474 strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
476 bchannel_pipeline(call->bchannel, call->pipeline);
479 if (opt[1] == '\0') {
480 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
483 CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
488 if (opt[1] != '\0') {
489 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
492 CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
493 call->inband_dtmf = 1;
498 if (opt[1] != 'r' && opt[1] != 't') {
499 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
503 if (gain < -8 || gain >8) {
504 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
507 CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
509 call->rx_gain = gain;
511 bchannel_gain(call->bchannel, call->rx_gain, 0);
513 call->tx_gain = gain;
515 bchannel_gain(call->bchannel, call->tx_gain, 1);
519 CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
523 /* re-open, if bchannel is created */
524 if (call->bchannel && call->bchannel->b_sock > -1) {
525 bchannel_destroy(call->bchannel);
526 if (bchannel_create(call->bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
527 bchannel_activate(call->bchannel, 1);
532 * send setup info to LCR
533 * this function is called, when asterisk call is received and ref is received
535 static void send_setup_to_lcr(struct chan_call *call)
537 union parameter newparam;
538 struct ast_channel *ast = call->ast;
540 if (!call->ast || !call->ref)
543 CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
545 /* send setup message to LCR */
546 memset(&newparam, 0, sizeof(union parameter));
547 newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
548 newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
549 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
550 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
551 newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
552 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
553 strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
554 call->display[0] = '\0';
555 if (call->cid_num[0])
556 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
557 if (call->cid_name[0])
558 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
559 if (call->cid_rdnis[0])
561 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
562 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
563 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
565 switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
567 case AST_PRES_ALLOWED:
568 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
570 case AST_PRES_RESTRICTED:
571 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
573 case AST_PRES_UNAVAILABLE:
574 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
577 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
579 switch(ast->cid.cid_ton)
582 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
585 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
588 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
591 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
593 newparam.setup.capainfo.bearer_capa = ast->transfercapability;
594 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
595 newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
596 newparam.setup.capainfo.hlc = INFO_HLC_NONE;
597 newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
598 send_message(MESSAGE_SETUP, call->ref, &newparam);
600 /* change to outgoing setup state */
601 call->state = CHAN_LCR_STATE_OUT_SETUP;
605 * send dialing info to LCR
606 * this function is called, when setup acknowledge is received and dialing
609 static void send_dialque_to_lcr(struct chan_call *call)
611 union parameter newparam;
613 if (!call->ast || !call->ref || !call->dialque[0])
616 CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
618 /* send setup message to LCR */
619 memset(&newparam, 0, sizeof(union parameter));
620 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
621 call->dialque[0] = '\0';
622 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
626 * in case of a bridge, the unsupported message can be forwarded directly
627 * to the remote call.
629 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
633 if (!call->bridge_call) return;
634 CDEBUG(call, NULL, "Sending message due briding.\n");
635 send_message(message_type, call->bridge_call->ref, param);
639 * send release message to LCR and import bchannel if exported
641 static void send_release_and_import(struct chan_call *call, int cause, int location)
643 union parameter newparam;
645 /* importing channel */
646 if (call->bchannel) {
647 memset(&newparam, 0, sizeof(union parameter));
648 newparam.bchannel.type = BCHANNEL_RELEASE;
649 newparam.bchannel.handle = call->bchannel->handle;
650 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
652 /* sending release */
653 memset(&newparam, 0, sizeof(union parameter));
654 newparam.disconnectinfo.cause = cause;
655 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
656 send_message(MESSAGE_RELEASE, call->ref, &newparam);
660 * check if extension matches and start asterisk
661 * if it can match, proceed
664 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
667 union parameter newparam;
669 CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", ast->exten, ast->context, complete?"yes":"no");
674 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
676 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
680 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
682 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);
686 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
687 /* send setup acknowledge to lcr */
688 memset(&newparam, 0, sizeof(union parameter));
689 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
692 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
693 ast_setstate(ast, AST_STATE_OFFHOOK);
698 if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
700 /* send setup acknowledge to lcr */
701 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
702 memset(&newparam, 0, sizeof(union parameter));
703 send_message(MESSAGE_OVERLAP, call->ref, &newparam);
707 call->state = CHAN_LCR_STATE_IN_DIALING;
708 ast_setstate(ast, AST_STATE_OFFHOOK);
710 /* if match, start pbx */
711 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
712 CDEBUG(call, ast, "Extensions matches.\n");
717 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
725 CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
726 send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
728 /* release asterisk */
729 ast->hangupcause = call->cause;
730 /* change to release state */
731 call->state = CHAN_LCR_STATE_RELEASE;
732 ast_hangup(ast); // call will be destroyed here
736 /* send setup to asterisk */
737 CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
738 ret = ast_pbx_start(ast);
741 cause = (ret==-2)?34:27;
744 call->pbx_started = 1;
749 * incoming setup from LCR
751 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
753 struct ast_channel *ast;
755 CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
757 /* create asterisk channel instrance */
758 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
762 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
763 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
770 ast->tech_pvt = call;
771 ast->tech = &lcr_tech;
772 ast->fds[0] = call->pipe[0];
774 /* fill setup information */
775 if (param->setup.dialinginfo.id)
776 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
777 if (param->setup.context[0])
778 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
780 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
781 if (param->setup.callerinfo.id[0])
782 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
783 if (param->setup.callerinfo.name[0])
784 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
785 if (param->setup.redirinfo.id[0])
786 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
787 switch (param->setup.callerinfo.present)
789 case INFO_PRESENT_ALLOWED:
790 ast->cid.cid_pres = AST_PRES_ALLOWED;
792 case INFO_PRESENT_RESTRICTED:
793 ast->cid.cid_pres = AST_PRES_RESTRICTED;
796 ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
798 switch (param->setup.callerinfo.ntype)
800 case INFO_NTYPE_SUBSCRIBER:
801 ast->cid.cid_ton = 4;
803 case INFO_NTYPE_NATIONAL:
804 ast->cid.cid_ton = 2;
806 case INFO_NTYPE_INTERNATIONAL:
807 ast->cid.cid_ton = 1;
810 ast->cid.cid_ton = 0;
812 ast->transfercapability = param->setup.capainfo.bearer_capa;
813 /* enable hdlc if transcap is data */
814 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
815 || ast->transfercapability == INFO_BC_DATARESTRICTED
816 || ast->transfercapability == INFO_BC_VIDEO)
818 strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
820 /* configure channel */
821 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
822 ast->readformat = ast->rawreadformat = ast->nativeformats;
823 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
825 ast->hangupcause = 0;
828 call->state = CHAN_LCR_STATE_IN_SETUP;
830 if (!call->pbx_started)
831 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
835 * incoming setup acknowledge from LCR
837 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
839 if (!call->ast) return;
841 CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
843 /* send pending digits in dialque */
844 if (call->dialque[0])
845 send_dialque_to_lcr(call);
846 /* change to overlap state */
847 call->state = CHAN_LCR_STATE_OUT_DIALING;
851 * incoming proceeding from LCR
853 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
855 CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
858 call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
859 /* queue event for asterisk */
860 if (call->ast && call->pbx_started)
861 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
865 * incoming alerting from LCR
867 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
869 CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
872 call->state = CHAN_LCR_STATE_OUT_ALERTING;
873 /* queue event to asterisk */
874 if (call->ast && call->pbx_started)
875 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
879 * incoming connect from LCR
881 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
883 union parameter newparam;
885 CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
888 call->state = CHAN_LCR_STATE_CONNECT;
889 /* request bchannel */
890 if (!call->bchannel) {
891 CDEBUG(call, call->ast, "Requesting B-channel.\n");
892 memset(&newparam, 0, sizeof(union parameter));
893 newparam.bchannel.type = BCHANNEL_REQUEST;
894 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
896 /* copy connectinfo */
897 memcpy(&call->connectinfo, ¶m->connectinfo, sizeof(struct connect_info));
898 /* queue event to asterisk */
899 if (call->ast && call->pbx_started)
900 strncat(call->queue_string, "A", sizeof(call->queue_string)-1);
904 * incoming disconnect from LCR
906 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
908 struct ast_channel *ast = call->ast;
910 CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
913 call->state = CHAN_LCR_STATE_IN_DISCONNECT;
915 call->cause = param->disconnectinfo.cause;
916 call->location = param->disconnectinfo.location;
917 /* if bridge, forward disconnect and return */
920 if (call->bridge_call)
922 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
923 bridge_message_if_bridged(call, message_type, param);
927 /* release lcr with same cause */
928 send_release_and_import(call, call->cause, call->location);
930 /* change to release state */
931 call->state = CHAN_LCR_STATE_RELEASE;
932 /* queue release asterisk */
935 ast->hangupcause = call->cause;
936 if (call->pbx_started)
937 strcpy(call->queue_string, "H"); // overwrite other indications
939 ast_hangup(ast); // call will be destroyed here
945 * incoming setup acknowledge from LCR
947 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
949 struct ast_channel *ast = call->ast;
951 CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
955 /* change to release state */
956 call->state = CHAN_LCR_STATE_RELEASE;
957 /* copy release info */
960 call->cause = param->disconnectinfo.cause;
961 call->location = param->disconnectinfo.location;
963 /* if we have an asterisk instance, queue hangup, else we are done */
966 ast->hangupcause = call->cause;
967 if (call->pbx_started)
968 strcpy(call->queue_string, "H");
970 ast_hangup(ast); // call will be destroyed here
980 * incoming information from LCR
982 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
984 struct ast_channel *ast = call->ast;
986 CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
990 /* pbx not started */
991 if (!call->pbx_started)
993 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
994 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
995 lcr_start_pbx(call, ast, param->information.sending_complete);
1000 if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0])
1001 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1003 /* use bridge to forware message not supported by asterisk */
1004 if (call->state == CHAN_LCR_STATE_CONNECT) {
1005 CDEBUG(call, call->ast, "Call is connected, briding.\n");
1006 bridge_message_if_bridged(call, message_type, param);
1011 * incoming information from LCR
1013 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1015 union parameter newparam;
1017 CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1019 /* request bchannel, if call is resumed and we don't have it */
1020 if (param->notifyinfo.notify == INFO_NOTIFY_USER_RESUMED && !call->bchannel && call->ref) {
1021 CDEBUG(call, call->ast, "Reqesting bchannel at resume.\n");
1022 memset(&newparam, 0, sizeof(union parameter));
1023 newparam.bchannel.type = BCHANNEL_REQUEST;
1024 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1027 if (!call->ast) return;
1029 /* use bridge to forware message not supported by asterisk */
1030 bridge_message_if_bridged(call, message_type, param);
1034 * incoming information from LCR
1036 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1038 CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
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 * got dtmf from bchannel (locked state)
1049 void lcr_in_dtmf(struct chan_call *call, int val)
1051 struct ast_channel *ast = call->ast;
1056 if (!call->pbx_started)
1059 CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1062 strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1066 * message received from LCR
1068 int receive_message(int message_type, unsigned int ref, union parameter *param)
1070 struct bchannel *bchannel;
1071 struct chan_call *call;
1072 union parameter newparam;
1074 memset(&newparam, 0, sizeof(union parameter));
1076 /* handle bchannel message*/
1077 if (message_type == MESSAGE_BCHANNEL)
1079 switch(param->bchannel.type)
1081 case BCHANNEL_ASSIGN:
1082 CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx) for ref %d\n", param->bchannel.handle, ref);
1083 if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
1085 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
1088 /* create bchannel */
1089 bchannel = alloc_bchannel(param->bchannel.handle);
1092 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
1096 /* configure channel */
1097 bchannel->b_tx_gain = param->bchannel.tx_gain;
1098 bchannel->b_rx_gain = param->bchannel.rx_gain;
1099 strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
1100 if (param->bchannel.crypt_len && param->bchannel.crypt_len <= sizeof(bchannel->b_bf_key))
1102 bchannel->b_bf_len = param->bchannel.crypt_len;
1103 memcpy(bchannel->b_bf_key, param->bchannel.crypt, param->bchannel.crypt_len);
1105 bchannel->b_txdata = 0;
1106 bchannel->b_dtmf = 1;
1107 bchannel->b_tx_dejitter = 1;
1109 /* in case, ref is not set, this bchannel instance must
1110 * be created until it is removed again by LCR */
1112 call = find_call_ref(ref);
1115 bchannel->call = call;
1116 call->bchannel = bchannel;
1118 bchannel_dtmf(bchannel, 1);
1120 bchannel_blowfish(bchannel, call->bf_key, call->bf_len);
1121 if (call->pipeline[0])
1122 bchannel_pipeline(bchannel, call->pipeline);
1124 bchannel_gain(bchannel, call->rx_gain, 0);
1126 bchannel_gain(bchannel, call->tx_gain, 1);
1127 if (call->bridge_id) {
1128 CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
1129 bchannel_join(bchannel, call->bridge_id);
1131 /* create only, if call exists, othewhise it bchannel is freed below... */
1132 if (bchannel_create(bchannel, ((call->transparent)?1:0) + ((call->hdlc)?2:0)))
1133 bchannel_activate(bchannel, 1);
1136 newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
1137 newparam.bchannel.handle = param->bchannel.handle;
1138 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1139 /* if call has released before bchannel is assigned */
1141 newparam.bchannel.type = BCHANNEL_RELEASE;
1142 newparam.bchannel.handle = param->bchannel.handle;
1143 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1148 case BCHANNEL_REMOVE:
1149 CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
1150 if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
1152 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
1155 /* unklink from call and destroy bchannel */
1156 free_bchannel(bchannel);
1159 newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
1160 newparam.bchannel.handle = param->bchannel.handle;
1161 send_message(MESSAGE_BCHANNEL, 0, &newparam);
1166 CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
1171 /* handle new ref */
1172 if (message_type == MESSAGE_NEWREF)
1174 if (param->direction)
1176 /* new ref from lcr */
1177 CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1178 if (!ref || find_call_ref(ref))
1180 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1183 /* allocate new call instance */
1184 call = alloc_call();
1186 call->state = CHAN_LCR_STATE_IN_PREPARE;
1189 /* wait for setup (or release from asterisk) */
1192 /* new ref, as requested from this remote application */
1193 CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1194 call = find_call_ref(0);
1197 /* send release, if ref does not exist */
1198 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
1199 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1204 /* send pending setup info */
1205 if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1206 send_setup_to_lcr(call);
1207 /* release if asterisk has signed off */
1208 else if (call->state == CHAN_LCR_STATE_RELEASE)
1212 send_release_and_import(call, call->cause, call->location);
1214 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1226 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1229 call = find_call_ref(ref);
1232 /* ignore ref that is not used (anymore) */
1233 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1237 /* handle messages */
1238 switch(message_type)
1241 lcr_in_setup(call, message_type, param);
1244 case MESSAGE_OVERLAP:
1245 lcr_in_overlap(call, message_type, param);
1248 case MESSAGE_PROCEEDING:
1249 lcr_in_proceeding(call, message_type, param);
1252 case MESSAGE_ALERTING:
1253 lcr_in_alerting(call, message_type, param);
1256 case MESSAGE_CONNECT:
1257 lcr_in_connect(call, message_type, param);
1260 case MESSAGE_DISCONNECT:
1261 lcr_in_disconnect(call, message_type, param);
1264 case MESSAGE_RELEASE:
1265 lcr_in_release(call, message_type, param);
1268 case MESSAGE_INFORMATION:
1269 lcr_in_information(call, message_type, param);
1272 case MESSAGE_NOTIFY:
1273 lcr_in_notify(call, message_type, param);
1276 case MESSAGE_FACILITY:
1277 lcr_in_facility(call, message_type, param);
1280 case MESSAGE_PATTERN: // audio available from LCR
1283 case MESSAGE_NOPATTERN: // audio not available from LCR
1286 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1287 call->audiopath = param->audiopath;
1291 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1298 * release all calls (due to broken socket)
1300 static void release_all_calls(void)
1302 struct chan_call *call;
1307 /* no ast, so we may directly free call */
1309 CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1313 /* already in release process */
1314 if (call->state == CHAN_LCR_STATE_RELEASE) {
1318 /* release or queue release */
1320 call->state = CHAN_LCR_STATE_RELEASE;
1321 if (!call->pbx_started) {
1322 CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1323 ast_hangup(call->ast); // call will be destroyed here
1326 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1327 strcpy(call->queue_string, "H");
1331 /* release all bchannels */
1332 while(bchannel_first)
1333 free_bchannel(bchannel_first);
1338 * warning! not thread safe
1339 * returns -1 for socket error, 0 for no work, 1 for work
1341 int handle_socket(void)
1345 struct admin_list *admin;
1346 struct admin_message msg;
1348 /* read from socket */
1349 len = read(lcr_sock, &msg, sizeof(msg));
1352 CERROR(NULL, NULL, "Socket closed.\n");
1353 return(-1); // socket closed
1357 if (len != sizeof(msg))
1359 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1360 return(-1); // socket error
1362 if (msg.message != ADMIN_MESSAGE)
1364 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1367 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1371 if (errno != EWOULDBLOCK)
1373 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1378 /* write to socket */
1381 admin = admin_first;
1382 len = write(lcr_sock, &admin->msg, sizeof(msg));
1385 CERROR(NULL, NULL, "Socket closed.\n");
1386 return(-1); // socket closed
1390 if (len != sizeof(msg))
1392 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1393 return(-1); // socket error
1396 admin_first = admin->next;
1402 if (errno != EWOULDBLOCK)
1404 CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1413 * open and close socket and thread
1415 int open_socket(void)
1418 char *socket_name = SOCKET_NAME;
1420 struct sockaddr_un sock_address;
1421 unsigned int on = 1;
1422 union parameter param;
1425 if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1427 CERROR(NULL, NULL, "Failed to create socket.\n");
1431 /* set socket address and name */
1432 memset(&sock_address, 0, sizeof(sock_address));
1433 sock_address.sun_family = PF_UNIX;
1434 strcpy(sock_address.sun_path, socket_name);
1436 /* connect socket */
1437 if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1441 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1445 /* set non-blocking io */
1446 if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1450 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1454 /* enque hello message */
1455 memset(¶m, 0, sizeof(param));
1456 strcpy(param.hello.application, "asterisk");
1457 send_message(MESSAGE_HELLO, 0, ¶m);
1462 void close_socket(void)
1464 struct admin_list *admin, *temp;
1466 /* flush pending messages */
1467 admin = admin_first;
1470 admin = admin->next;
1482 /* sending queue to asterisk */
1483 static int queue_send(void)
1486 struct chan_call *call;
1487 struct ast_channel *ast;
1488 struct ast_frame fr;
1493 p = call->queue_string;
1496 /* there is something to queue */
1497 if (!ast_channel_trylock(ast)) { /* succeed */
1501 CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1502 ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1505 CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1506 ast_queue_control(ast, AST_CONTROL_RINGING);
1509 CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1510 ast_queue_control(ast, AST_CONTROL_ANSWER);
1513 CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1514 ast_queue_hangup(ast);
1516 case '1': case '2': case '3': case 'a':
1517 case '4': case '5': case '6': case 'b':
1518 case '7': case '8': case '9': case 'c':
1519 case '*': case '0': case '#': case 'd':
1520 CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1521 /* send digit to asterisk */
1522 memset(&fr, 0, sizeof(fr));
1523 fr.frametype = AST_FRAME_DTMF;
1525 fr.delivery = ast_tv(0, 0);
1527 ast_queue_frame(ast, &fr);
1530 CDEBUG(call, ast, "Ignoring queued digit 0x%02d.\n", *p);
1534 call->queue_string[0] = '\0';
1535 ast_channel_unlock(ast);
1545 /* signal handler */
1546 void sighandler(int sigset)
1550 /* chan_lcr thread */
1551 static void *chan_thread(void *arg)
1555 union parameter param;
1556 time_t retry = 0, now;
1558 bchannel_pid = getpid();
1560 // signal(SIGPIPE, sighandler);
1562 memset(¶m, 0, sizeof(union parameter));
1566 ast_mutex_lock(&chan_lock);
1573 ret = handle_socket();
1575 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1577 release_all_calls();
1584 if (retry && now-retry > 5) {
1585 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1587 if (open_socket() < 0) {
1596 ret = bchannel_handle();
1600 /* handle messages to asterisk */
1605 /* delay if no work done */
1607 ast_mutex_unlock(&chan_lock);
1609 ast_mutex_lock(&chan_lock);
1615 CERROR(NULL, NULL, "Thread exit.\n");
1617 ast_mutex_unlock(&chan_lock);
1619 // signal(SIGPIPE, SIG_DFL);
1625 * new asterisk instance
1628 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1630 char exten[256], *dial, *interface, *opt;
1631 struct ast_channel *ast;
1632 struct chan_call *call;
1634 ast_mutex_lock(&chan_lock);
1635 CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
1637 /* if socket is closed */
1640 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1641 ast_mutex_unlock(&chan_lock);
1645 /* create call instance */
1646 call = alloc_call();
1649 /* failed to create instance */
1650 ast_mutex_unlock(&chan_lock);
1654 /* create asterisk channel instrance */
1655 ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1658 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1660 /* failed to create instance */
1661 ast_mutex_unlock(&chan_lock);
1664 ast->tech = &lcr_tech;
1665 ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
1666 /* configure channel */
1667 ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1668 ast->readformat = ast->rawreadformat = ast->nativeformats;
1669 ast->writeformat = ast->rawwriteformat = ast->nativeformats;
1671 ast->hangupcause = 0;
1675 ast->tech_pvt = call;
1676 ast->fds[0] = call->pipe[0];
1677 call->pbx_started = 0;
1679 call->state = CHAN_LCR_STATE_OUT_PREPARE;
1682 * Extract interface, dialstring, options from data.
1685 * <interface>/<dialstring>
1686 * <interface>/<dialstring>/options
1688 strncpy(exten, (char *)data, sizeof(exten)-1);
1689 exten[sizeof(exten)-1] = '\0';
1690 if ((dial = strchr(exten, '/'))) {
1693 if ((opt = strchr(dial, '/')))
1702 strncpy(call->interface, interface, sizeof(call->interface)-1);
1703 strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
1704 apply_opt(call, (char *)opt);
1706 ast_mutex_unlock(&chan_lock);
1711 * call from asterisk
1713 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1715 union parameter newparam;
1716 struct chan_call *call;
1718 ast_mutex_lock(&chan_lock);
1719 call = ast->tech_pvt;
1721 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
1722 ast_mutex_unlock(&chan_lock);
1726 CDEBUG(NULL, ast, "Received call from Asterisk.\n");
1728 /* pbx process is started */
1729 call->pbx_started = 1;
1730 /* send MESSAGE_NEWREF */
1731 memset(&newparam, 0, sizeof(union parameter));
1732 newparam.direction = 0; /* request from app */
1733 send_message(MESSAGE_NEWREF, 0, &newparam);
1735 /* set hdlc if capability requires hdlc */
1736 if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
1737 || ast->transfercapability == INFO_BC_DATARESTRICTED
1738 || ast->transfercapability == INFO_BC_VIDEO)
1740 /* if hdlc is forced by option, we change transcap to data */
1742 && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
1743 && ast->transfercapability != INFO_BC_DATARESTRICTED
1744 && ast->transfercapability != INFO_BC_VIDEO)
1745 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
1747 call->cid_num[0] = 0;
1748 call->cid_name[0] = 0;
1749 call->cid_rdnis[0] = 0;
1751 if (ast->cid.cid_num) if (ast->cid.cid_num[0])
1752 strncpy(call->cid_num, ast->cid.cid_num,
1753 sizeof(call->cid_num)-1);
1755 if (ast->cid.cid_name) if (ast->cid.cid_name[0])
1756 strncpy(call->cid_name, ast->cid.cid_name,
1757 sizeof(call->cid_name)-1);
1758 if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
1759 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
1760 sizeof(call->cid_rdnis)-1);
1762 ast_mutex_unlock(&chan_lock);
1766 static int lcr_digit_begin(struct ast_channel *ast, char digit)
1768 struct chan_call *call;
1769 union parameter newparam;
1772 /* only pass IA5 number space */
1773 if (digit > 126 || digit < 32)
1776 ast_mutex_lock(&chan_lock);
1777 call = ast->tech_pvt;
1779 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1780 ast_mutex_unlock(&chan_lock);
1784 CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
1786 /* send information or queue them */
1787 if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1789 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1790 memset(&newparam, 0, sizeof(union parameter));
1791 newparam.information.id[0] = digit;
1792 newparam.information.id[1] = '\0';
1793 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1796 && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP))
1798 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1800 strncat(call->dialque, buf, strlen(call->dialque)-1);
1803 ast_mutex_unlock(&chan_lock);
1807 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1809 printf("DIGIT END %c\n", digit);
1813 static int lcr_answer(struct ast_channel *ast)
1815 union parameter newparam;
1816 struct chan_call *call;
1818 ast_mutex_lock(&chan_lock);
1819 call = ast->tech_pvt;
1821 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1822 ast_mutex_unlock(&chan_lock);
1826 CDEBUG(call, ast, "Received answer from Asterisk.\n");
1828 /* copy connectinfo, if bridged */
1829 if (call->bridge_call)
1830 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1831 /* send connect message to lcr */
1832 memset(&newparam, 0, sizeof(union parameter));
1833 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1834 send_message(MESSAGE_CONNECT, call->ref, &newparam);
1836 call->state = CHAN_LCR_STATE_CONNECT;
1837 /* request bchannel */
1838 if (!call->bchannel) {
1839 CDEBUG(call, ast, "Requesting B-channel.\n");
1840 memset(&newparam, 0, sizeof(union parameter));
1841 newparam.bchannel.type = BCHANNEL_REQUEST;
1842 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1845 // memset(&newparam, 0, sizeof(union parameter));
1846 // send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
1849 CDEBUG(call, ast, "DTMF is disabled by option.\n");
1853 ast_mutex_unlock(&chan_lock);
1857 static int lcr_hangup(struct ast_channel *ast)
1859 struct chan_call *call;
1860 pthread_t tid = pthread_self();
1862 if (!pthread_equal(tid, chan_tid))
1863 ast_mutex_lock(&chan_lock);
1864 call = ast->tech_pvt;
1866 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1867 if (!pthread_equal(tid, chan_tid))
1868 ast_mutex_unlock(&chan_lock);
1872 if (!pthread_equal(tid, chan_tid))
1873 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1875 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1877 /* disconnect asterisk, maybe not required */
1878 ast->tech_pvt = NULL;
1883 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1884 if (ast->hangupcause > 0)
1885 send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1887 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
1890 if (!pthread_equal(tid, chan_tid))
1891 ast_mutex_unlock(&chan_lock);
1895 /* ref is not set, due to prepare setup or release */
1896 if (call->state == CHAN_LCR_STATE_RELEASE)
1898 /* we get the response to our release */
1899 CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1903 /* during prepare, we change to release state */
1904 CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1905 call->state = CHAN_LCR_STATE_RELEASE;
1908 if (!pthread_equal(tid, chan_tid))
1909 ast_mutex_unlock(&chan_lock);
1913 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1915 struct chan_call *call;
1918 CDEBUG(NULL, ast, "No subclass\n");
1919 if (!(f->subclass & ast->nativeformats))
1920 CDEBUG(NULL, ast, "Unexpected format.\n");
1922 ast_mutex_lock(&chan_lock);
1923 call = ast->tech_pvt;
1925 ast_mutex_unlock(&chan_lock);
1928 if (call->bchannel && f->samples)
1929 bchannel_transmit(call->bchannel, f->data, f->samples);
1930 ast_mutex_unlock(&chan_lock);
1935 static struct ast_frame *lcr_read(struct ast_channel *ast)
1937 struct chan_call *call;
1941 ast_mutex_lock(&chan_lock);
1942 call = ast->tech_pvt;
1944 ast_mutex_unlock(&chan_lock);
1947 if (call->pipe[0] > -1) {
1948 if (call->rebuffer) {
1949 len = read(call->pipe[0], call->read_buff, 160);
1951 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1954 close(call->pipe[0]);
1960 p = call->read_buff;
1961 for (i = 0; i < len; i++) {
1966 call->read_fr.frametype = AST_FRAME_VOICE;
1967 call->read_fr.subclass = ast->nativeformats;
1968 call->read_fr.datalen = len;
1969 call->read_fr.samples = len;
1970 call->read_fr.delivery = ast_tv(0,0);
1971 call->read_fr.data = call->read_buff;
1972 ast_mutex_unlock(&chan_lock);
1974 return &call->read_fr;
1977 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1979 union parameter newparam;
1981 struct chan_call *call;
1983 ast_mutex_lock(&chan_lock);
1984 call = ast->tech_pvt;
1986 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1987 ast_mutex_unlock(&chan_lock);
1992 case AST_CONTROL_BUSY:
1993 CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1994 ast_setstate(ast, AST_STATE_BUSY);
1995 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1996 /* send message to lcr */
1997 memset(&newparam, 0, sizeof(union parameter));
1998 newparam.disconnectinfo.cause = 17;
1999 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2000 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2002 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2005 case AST_CONTROL_CONGESTION:
2006 CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2007 if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2008 /* send message to lcr */
2009 memset(&newparam, 0, sizeof(union parameter));
2010 newparam.disconnectinfo.cause = ast->hangupcause;
2011 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2012 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2014 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2017 case AST_CONTROL_PROCEEDING:
2018 CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2019 if (call->state == CHAN_LCR_STATE_IN_SETUP
2020 || call->state == CHAN_LCR_STATE_IN_DIALING) {
2021 /* send message to lcr */
2022 memset(&newparam, 0, sizeof(union parameter));
2023 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2025 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2028 case AST_CONTROL_RINGING:
2029 CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2030 ast_setstate(ast, AST_STATE_RINGING);
2031 if (call->state == CHAN_LCR_STATE_IN_SETUP
2032 || call->state == CHAN_LCR_STATE_IN_DIALING
2033 || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2034 /* send message to lcr */
2035 memset(&newparam, 0, sizeof(union parameter));
2036 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2038 call->state = CHAN_LCR_STATE_IN_ALERTING;
2042 CDEBUG(call, ast, "Received indicate -1.\n");
2046 case AST_CONTROL_VIDUPDATE:
2047 CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2050 case AST_CONTROL_HOLD:
2051 CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2052 /* send message to lcr */
2053 memset(&newparam, 0, sizeof(union parameter));
2054 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2055 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2057 /*start music onhold*/
2058 ast_moh_start(ast,data,ast->musicclass);
2060 case AST_CONTROL_UNHOLD:
2061 CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2062 /* send message to lcr */
2063 memset(&newparam, 0, sizeof(union parameter));
2064 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
2065 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2072 CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
2078 ast_mutex_unlock(&chan_lock);
2085 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *newast)
2087 struct chan_call *call;
2089 ast_mutex_lock(&chan_lock);
2090 call = oldast->tech_pvt;
2092 CERROR(NULL, oldast, "Received fixup from Asterisk, but no call instance exists.\n");
2093 ast_mutex_unlock(&chan_lock);
2097 CDEBUG(call, oldast, "Received fixup from Asterisk.\n");
2099 ast_mutex_lock(&chan_lock);
2104 * send_text asterisk
2106 static int lcr_send_text(struct ast_channel *ast, const char *text)
2108 struct chan_call *call;
2109 union parameter newparam;
2111 ast_mutex_lock(&chan_lock);
2112 call = ast->tech_pvt;
2114 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
2115 ast_mutex_unlock(&chan_lock);
2119 CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
2120 memset(&newparam, 0, sizeof(union parameter));
2121 strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
2122 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2123 ast_mutex_lock(&chan_lock);
2130 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
2131 struct ast_channel *ast2, int flags,
2132 struct ast_frame **fo,
2133 struct ast_channel **rc, int timeoutms)
2136 struct chan_call *call1, *call2;
2137 struct ast_channel *carr[2], *who;
2139 struct ast_frame *f;
2142 CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
2147 /* join via dsp (if the channels are currently open) */
2148 ast_mutex_lock(&chan_lock);
2149 bridge_id = new_bridge_id();
2150 call1 = ast1->tech_pvt;
2151 call2 = ast2->tech_pvt;
2154 call1->bridge_id = bridge_id;
2155 if (call1->bchannel)
2156 bchannel_join(call1->bchannel, bridge_id);
2157 call1->bridge_call = call2;
2161 call2->bridge_id = bridge_id;
2162 if (call2->bchannel)
2163 bchannel_join(call2->bchannel, bridge_id);
2164 call2->bridge_call = call1;
2166 ast_mutex_unlock(&chan_lock);
2170 who = ast_waitfor_n(carr, 2, &to);
2173 CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
2178 if (!f || f->frametype == AST_FRAME_CONTROL) {
2180 CDEBUG(NULL, NULL, "Got hangup.\n");
2182 CDEBUG(NULL, NULL, "Got CONTROL.\n");
2189 if ( f->frametype == AST_FRAME_DTMF ) {
2190 CDEBUG(NULL, NULL, "Got DTMF.\n");
2206 CDEBUG(NULL, NULL, "Releasing bride.\n");
2208 /* split channels */
2209 ast_mutex_lock(&chan_lock);
2210 call1 = ast1->tech_pvt;
2211 call2 = ast2->tech_pvt;
2214 call1->bridge_id = 0;
2215 if (call1->bchannel)
2216 bchannel_join(call1->bchannel, 0);
2217 if (call1->bridge_call)
2218 call1->bridge_call->bridge_call = NULL;
2219 call1->bridge_call = NULL;
2223 call2->bridge_id = 0;
2224 if (call2->bchannel)
2225 bchannel_join(call2->bchannel, 0);
2226 if (call2->bridge_call)
2227 call2->bridge_call->bridge_call = NULL;
2228 call2->bridge_call = NULL;
2231 ast_mutex_unlock(&chan_lock);
2232 return AST_BRIDGE_COMPLETE;
2234 static struct ast_channel_tech lcr_tech = {
2236 .description="Channel driver for connecting to Linux-Call-Router",
2237 .requester=lcr_request,
2238 .send_digit_begin=lcr_digit_begin,
2239 .send_digit_end=lcr_digit_end,
2246 .indicate=lcr_indicate,
2248 .send_text=lcr_send_text,
2257 static int lcr_show_lcr (int fd, int argc, char *argv[])
2262 static int lcr_show_calls (int fd, int argc, char *argv[])
2267 static int lcr_reload_routing (int fd, int argc, char *argv[])
2272 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
2277 static int lcr_port_block (int fd, int argc, char *argv[])
2282 static int lcr_port_unblock (int fd, int argc, char *argv[])
2287 static int lcr_port_unload (int fd, int argc, char *argv[])
2292 static struct ast_cli_entry cli_show_lcr =
2293 { {"lcr", "show", "lcr", NULL},
2295 "Shows current states of LCR core",
2296 "Usage: lcr show lcr\n",
2299 static struct ast_cli_entry cli_show_calls =
2300 { {"lcr", "show", "calls", NULL},
2302 "Shows current calls made by LCR and Asterisk",
2303 "Usage: lcr show calls\n",
2306 static struct ast_cli_entry cli_reload_routing =
2307 { {"lcr", "reload", "routing", NULL},
2309 "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
2310 "Usage: lcr reload routing\n",
2313 static struct ast_cli_entry cli_reload_interfaces =
2314 { {"lcr", "reload", "interfaces", NULL},
2315 lcr_reload_interfaces,
2316 "Reloads interfaces conf of LCR",
2317 "Usage: lcr reload interfaces\n",
2320 static struct ast_cli_entry cli_port_block =
2321 { {"lcr", "port", "block", NULL},
2323 "Blocks LCR port for further calls",
2324 "Usage: lcr port block \"<port>\"\n",
2327 static struct ast_cli_entry cli_port_unblock =
2328 { {"lcr", "port", "unblock", NULL},
2330 "Unblocks or loads LCR port, port is opened my mISDN",
2331 "Usage: lcr port unblock \"<port>\"\n",
2334 static struct ast_cli_entry cli_port_unload =
2335 { {"lcr", "port", "unload", NULL},
2337 "Unloads LCR port, port is closes by mISDN",
2338 "Usage: lcr port unload \"<port>\"\n",
2344 static int lcr_config_exec(struct ast_channel *ast, void *data)
2346 struct chan_call *call;
2348 ast_mutex_lock(&chan_lock);
2349 CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
2353 if (call->ast == ast)
2358 apply_opt(call, (char *)data);
2360 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
2362 ast_mutex_unlock(&chan_lock);
2367 * module loading and destruction
2369 int load_module(void)
2373 for (i = 0; i < 256; i++) {
2374 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
2375 | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
2378 if (read_options() == 0) {
2379 CERROR(NULL, NULL, "%s", options_error);
2380 return AST_MODULE_LOAD_DECLINE;
2383 ast_mutex_init(&chan_lock);
2384 ast_mutex_init(&log_lock);
2386 if (open_socket() < 0) {
2387 /* continue with closed socket */
2390 if (bchannel_initialize()) {
2391 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2393 return AST_MODULE_LOAD_DECLINE;
2397 lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2398 if (ast_channel_register(&lcr_tech)) {
2399 CERROR(NULL, NULL, "Unable to register channel class\n");
2400 bchannel_deinitialize();
2402 return AST_MODULE_LOAD_DECLINE;
2405 ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
2406 "lcr_config(<opt><optarg>:<opt>:...)\n"
2407 "Sets LCR opts. and optargs\n"
2409 "The available options are:\n"
2410 " d - Send display text on called phone, text is the optarg.\n"
2411 " n - Don't detect dtmf tones on called channel.\n"
2412 " h - Force data call (HDLC).\n"
2413 " t - Disable all audio features (required for fax application).\n"
2414 " c - Make crypted outgoing call, optarg is keyindex.\n"
2415 " e - Perform echo cancelation on this channel.\n"
2416 " Takes mISDN pipeline option as optarg.\n"
2417 // " s - Send Non Inband DTMF as inband.\n"
2418 " vr - rxgain control\n"
2419 " vt - txgain control\n"
2420 " Volume changes at factor 2 ^ optarg.\n"
2425 ast_cli_register(&cli_show_lcr);
2426 ast_cli_register(&cli_show_calls);
2427 ast_cli_register(&cli_reload_routing);
2428 ast_cli_register(&cli_reload_interfaces);
2429 ast_cli_register(&cli_port_block);
2430 ast_cli_register(&cli_port_unblock);
2431 ast_cli_register(&cli_port_unload);
2435 if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2437 /* failed to create thread */
2438 bchannel_deinitialize();
2440 ast_channel_unregister(&lcr_tech);
2441 return AST_MODULE_LOAD_DECLINE;
2446 int unload_module(void)
2448 /* First, take us out of the channel loop */
2449 CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2452 pthread_join(chan_tid, NULL);
2454 ast_channel_unregister(&lcr_tech);
2456 ast_unregister_application("lcr_config");
2459 if (mISDN_created) {
2460 bchannel_deinitialize();
2464 if (lcr_sock >= 0) {
2472 int reload_module(void)
2479 #define AST_MODULE "chan_lcr"
2481 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2482 .load = load_module,
2483 .unload = unload_module,
2484 .reload = reload_module,