Removed complete bchannel handling from chan_lcr
[lcr.git] / chan_lcr.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** Asterisk socket client                                                    **
9 **                                                                           **
10 \*****************************************************************************/
11
12 /*
13
14 Registering to LCR:
15
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.
19
20
21 Call is initiated by LCR:
22
23 If a call is received from LCR, a MESSAGE_NEWREF is received first.
24 The ref_was_assigned ist set to 1.
25 A new chan_call instance is created. The call reference (ref) is given by
26 the received MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
27 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
28 using ast_channel_alloc(1).  The setup information is given to asterisk.
29 The new Asterisk instance pointer (ast) is stored to chan_call structure.
30 The state changes to CHAN_LCR_STATE_IN_SETUP.
31
32
33 Call is initiated by Asterisk:
34
35 If a call is requested from Asterisk, a new chan_call instance is created.
36 The new Asterisk instance pointer (ast) is stored to chan_call structure.
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 If the call is received (lcr_call) A MESSAGE_NEWREF is sent to LCR requesting
39 a new call reference (ref).
40 The ref_was_assigned ist set to 1.
41 Further dialing information is queued.
42 After the new callref is received by special MESSAGE_NEWREF reply, new ref
43 is stored in the chan_call structure.
44 The setup information is sent to LCR using MESSAGE_SETUP.
45 The state changes to CHAN_LCR_STATE_OUT_SETUP.
46
47
48 Call is in process:
49
50 During call process, messages are received and sent.
51 The state changes accordingly.
52 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
53 If a MESSAGE_OVERLAP is received, further dialing is required.
54 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
55 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
56
57
58 Call is released by LCR:
59
60 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
61 The current ref is set to 0, to indicate released reference.
62 The ref_was_assigned==1 shows that there is no other ref to be assigned.
63 The state changes to CHAN_LCR_STATE_RELEASE.
64 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
65 the chan_call instance is destroyed.
66 After lcr_hangup() is called-back by Asterisk, the chan_call instance
67 is destroyed, because the current ref is set to 0 and the state equals
68 CHAN_LCR_STATE_RELEASE.
69 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
70 "Call is released by Asterisk".
71
72
73 Call is released by Asterisk:
74
75 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
76 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
78 set to CHAN_LCR_STATE_RELEASE.
79 The ref_was_assigned==0 shows that a ref is still requested.
80 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
81 LCR and the chan_call instance is destroyed.
82 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
83 "Call is released by LCR".
84
85
86 Locking issues:
87
88 The deadlocking problem:
89
90 - chan_lcr locks chan_lock and waits inside ast_queue_xxxx() for ast_channel
91 to be unlocked.
92 - ast_channel thread locks ast_channel and calls a tech function and waits
93 there for chan_lock to be unlocked.
94
95 The solution:
96
97 Never call ast_queue_xxxx() if ast_channel is not locked and don't wait until
98 ast_channel can be locked. All messages to asterisk are queued inside call
99 instance and will be handled using a try-lock to get ast_channel lock.
100 If it succeeds to lock ast_channel, the ast_queue_xxxx can safely called even
101 if the lock is incremented and decremented there.
102
103 Exception: Calling ast_queue_frame inside ast->tech->read is safe, because
104 it is called from ast_channel process which has already locked ast_channel.
105
106 */
107
108
109 /* Choose if you want to have chan_lcr for Asterisk 1.4.x or CallWeaver 1.2.x */
110 #define LCR_FOR_ASTERISK
111 /* #define LCR_FOR_CALLWEAVER */
112
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <string.h>
116 #include <stdarg.h>
117 #include <errno.h>
118 #include <sys/types.h>
119 #include <time.h>
120 //#include <signal.h>
121 #include <unistd.h>
122 #include <fcntl.h>
123 #include <sys/ioctl.h>
124 #include <sys/socket.h>
125 #include <sys/un.h>
126
127 #include <semaphore.h>
128
129 #define HAVE_ATTRIBUTE_always_inline 1
130 #define HAVE_ARPA_INET_H 1
131 #define HAVE_TIMERSUB 1
132 #define HAVE_STRTOQ 1
133 #define HAVE_INET_ATON 1
134
135 #include <asterisk/compiler.h>
136 #ifdef LCR_FOR_ASTERISK
137 #include <asterisk/buildopts.h>
138 #endif
139
140 /*
141  * Fwd declare struct ast_channel to get rid of gcc warning about
142  * incompatible pointer type passed to ast_register_application2.
143  */
144 struct ast_channel;
145
146 #include <asterisk/module.h>
147 #include <asterisk/channel.h>
148 #include <asterisk/config.h>
149 #include <asterisk/logger.h>
150 #include <asterisk/pbx.h>
151 #include <asterisk/options.h>
152 #include <asterisk/io.h>
153 #include <asterisk/frame.h>
154 #include <asterisk/translate.h>
155 #include <asterisk/cli.h>
156 #include <asterisk/musiconhold.h>
157 #include <asterisk/dsp.h>
158 #include <asterisk/translate.h>
159 #include <asterisk/file.h>
160 #ifdef LCR_FOR_ASTERISK
161 #include <asterisk/callerid.h>
162 #endif
163 #ifdef LCR_FOR_CALLWEAVER
164 #include <asterisk/phone_no_utils.h>
165 #endif
166
167 #include <asterisk/indications.h>
168 #include <asterisk/app.h>
169 #include <asterisk/features.h>
170 #include <asterisk/sched.h>
171 #if ASTERISK_VERSION_NUM < 110000
172 #include <asterisk/version.h>
173 #endif
174 #include "extension.h"
175 #include "message.h"
176 #include "callerid.h"
177 #include "lcrsocket.h"
178 #include "cause.h"
179 #include "select.h"
180 #include "options.h"
181 #include "chan_lcr.h"
182
183 CHAN_LCR_STATE // state description structure
184 MESSAGES // message text
185
186 #ifdef LCR_FOR_CALLWEAVER
187 AST_MUTEX_DEFINE_STATIC(rand_lock);
188 #endif
189
190 unsigned char flip_bits[256];
191
192 #ifdef LCR_FOR_CALLWEAVER
193 static struct ast_frame nullframe = { AST_FRAME_NULL, };
194 #endif
195
196 int lcr_debug=1;
197
198 char lcr_type[]="lcr";
199
200 #ifdef LCR_FOR_CALLWEAVER
201 static ast_mutex_t usecnt_lock;
202 static int usecnt=0;
203 static char *desc = "Channel driver for mISDN/LCR Support (Bri/Pri)";
204 #endif
205
206 pthread_t chan_tid;
207 ast_mutex_t chan_lock; /* global lock */
208 ast_mutex_t log_lock; /* logging log */
209 /* global_change:
210  * used to indicate change in file descriptors, so select function's result may
211  * be obsolete.
212  */
213 int global_change = 0;
214 int wake_global = 0;
215 int wake_pipe[2];
216 struct lcr_fd wake_fd;
217
218 int glob_channel = 0;
219
220 int lcr_sock = -1;
221 struct lcr_fd socket_fd;
222 struct lcr_timer socket_retry;
223
224 struct admin_list {
225         struct admin_list *next;
226         struct admin_message msg;
227 } *admin_first = NULL;
228
229 static struct ast_channel_tech lcr_tech;
230
231 /*
232  * logging
233  */
234 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, ...)
235 {
236         char buffer[1024];
237         char call_text[128] = "NULL";
238         char ast_text[128] = "NULL";
239         va_list args;
240
241         ast_mutex_lock(&log_lock);
242
243         va_start(args,fmt);
244         vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
245         buffer[sizeof(buffer)-1]=0;
246         va_end(args);
247
248         if (call)
249                 sprintf(call_text, "%d", call->ref);
250         if (ast)
251 #if ASTERISK_VERSION_NUM < 110000
252                 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
253 #else
254                 strncpy(ast_text, ast_channel_name(ast), sizeof(ast_text)-1);
255 #endif
256         ast_text[sizeof(ast_text)-1] = '\0';
257
258 //      ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
259         printf("[call=%s ast=%s line=%d] %s", call_text, ast_text, line, buffer);
260
261         ast_mutex_unlock(&log_lock);
262 }
263
264 /*
265  * channel and call instances
266  */
267 struct chan_call *call_first;
268
269 /*
270  * find call by ref
271  * special case: 0: find new ref, that has not been assigned a ref yet
272  */
273
274 struct chan_call *find_call_ref(unsigned int ref)
275 {
276         struct chan_call *call = call_first;
277         int assigned = (ref > 0);
278
279         while(call) {
280                 if (call->ref == ref && call->ref_was_assigned == assigned)
281                         break;
282                 call = call->next;
283         }
284         return call;
285 }
286
287 void free_call(struct chan_call *call)
288 {
289         struct chan_call **temp = &call_first;
290
291         while(*temp) {
292                 if (*temp == call) {
293                         *temp = (*temp)->next;
294                         if (call->pipe[0] > -1)
295                                 close(call->pipe[0]);
296                         if (call->pipe[1] > -1)
297                                 close(call->pipe[1]);
298                         if (call->bridge_call) {
299                                 if (call->bridge_call->bridge_call != call)
300                                         CERROR(call, NULL, "Linked call structure has no link to us.\n");
301                                 call->bridge_call->bridge_call = NULL;
302                         }
303                         if (call->trans)
304                                 ast_translator_free_path(call->trans);
305                         if (call->dsp)
306                                 ast_dsp_free(call->dsp);
307                         CDEBUG(call, NULL, "Call instance freed.\n");
308                         free(call);
309                         global_change = 1;
310                         return;
311                 }
312                 temp = &((*temp)->next);
313         }
314         CERROR(call, NULL, "Call instance not found in list.\n");
315 }
316
317 struct chan_call *alloc_call(void)
318 {
319         struct chan_call **callp = &call_first;
320
321         while(*callp)
322                 callp = &((*callp)->next);
323
324         *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
325         if (*callp)
326                 memset(*callp, 0, sizeof(struct chan_call));
327         if (pipe((*callp)->pipe) < 0) {
328                 CERROR(*callp, NULL, "Failed to create pipe.\n");
329                 free_call(*callp);
330                 return NULL;
331         }
332         fcntl((*callp)->pipe[0], F_SETFL, O_NONBLOCK);
333         CDEBUG(*callp, NULL, "Call instance allocated.\n");
334         return *callp;
335 }
336
337 unsigned short new_bridge_id(void)
338 {
339         struct chan_call *call;
340         unsigned short id = 1;
341
342         /* search for lowest bridge id that is not in use and not 0 */
343         while(id) {
344                 call = call_first;
345                 while(call) {
346                         if (call->bridge_id == id)
347                                 break;
348                         call = call->next;
349                 }
350                 if (!call)
351                         break;
352                 id++;
353         }
354         CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
355         return id;
356 }
357
358 /*
359  * enque message to LCR
360  */
361 int send_message(int message_type, unsigned int ref, union parameter *param)
362 {
363         struct admin_list *admin, **adminp;
364
365         if (lcr_sock < 0) {
366                 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
367                 return -1;
368         }
369         if (message_type != MESSAGE_TRAFFIC)
370                 CDEBUG(NULL, NULL, "Sending %s to socket. (ref=%d)\n", messages_txt[message_type], ref);
371
372         adminp = &admin_first;
373         while(*adminp)
374                 adminp = &((*adminp)->next);
375         admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
376         if (!admin) {
377                 CERROR(NULL, NULL, "No memory for message to LCR.\n");
378                 return -1;
379         }
380         *adminp = admin;
381
382         admin->msg.message = ADMIN_MESSAGE;
383         admin->msg.u.msg.type = message_type;
384         admin->msg.u.msg.ref = ref;
385         memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
386         socket_fd.when |= LCR_FD_WRITE;
387         if (!wake_global) {
388                 wake_global = 1;
389                 char byte = 0;
390                 int rc;
391                 rc = write(wake_pipe[1], &byte, 1);
392         }
393
394         return 0;
395 }
396
397 /*
398  * apply options (in locked state)
399  */
400 void apply_opt(struct chan_call *call, char *data)
401 {
402         union parameter newparam;
403         char string[1024], *p = string, *opt;//, *key;
404 //      int gain, i;
405
406         if (!data[0])
407                 return; // no opts
408
409         strncpy(string, data, sizeof(string)-1);
410         string[sizeof(string)-1] = '\0';
411
412         /* parse options */
413         while((opt = strsep(&p, ":"))) {
414                 switch(opt[0]) {
415                 case 'd':
416                         if (opt[1] == '\0') {
417                                 CERROR(call, call->ast, "Option 'd' (display) expects parameter.\n", opt);
418                                 break;
419                         }
420                         CDEBUG(call, call->ast, "Option 'd' (display) with text '%s'.\n", opt+1);
421                         if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
422                                 strncpy(call->display, opt+1, sizeof(call->display)-1);
423                         else {
424                                 memset(&newparam, 0, sizeof(union parameter));
425                                 strncpy(newparam.notifyinfo.display, opt+1, sizeof(newparam.notifyinfo.display)-1);
426                                 send_message(MESSAGE_NOTIFY, call->ref, &newparam);
427                         }
428                         break;
429                 case 'n':
430                         if (opt[1] != '\0') {
431                                 CERROR(call, call->ast, "Option 'n' (no DTMF) expects no parameter.\n", opt);
432                                 break;
433                         }
434                         CDEBUG(call, call->ast, "Option 'n' (no DTMF).\n");
435                         call->dsp_dtmf = 0;
436                         break;
437 #if 0
438                 case 'c':
439                         if (opt[1] == '\0') {
440                                 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter.\n", opt);
441                                 break;
442                         }
443                         key = opt+1;
444                         /* check for 0xXXXX... type of key */
445                         if (!!strncmp((char *)key, "0x", 2)) {
446                                 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter starting with '0x'.\n", opt);
447                                 break;
448                         }
449                         key+=2;
450                         if (strlen(key) > 56*2 || (strlen(key) % 1)) {
451                                 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with max 56 bytes ('0x' + 112 characters)\n", opt);
452                                 break;
453                         }
454                         i = 0;
455                         while(*key) {
456                                 if (*key>='0' && *key<='9')
457                                         call->bf_key[i] = (*key-'0') << 8;
458                                 else if (*key>='a' && *key<='f')
459                                         call->bf_key[i] = (*key-'a'+10) << 8;
460                                 else if (*key>='A' && *key<='F')
461                                         call->bf_key[i] = (*key-'A'+10) << 8;
462                                 else
463                                         break;
464                                 key++;
465                                 if (*key>='0' && *key<='9')
466                                         call->bf_key[i] += (*key - '0');
467                                 else if (*key>='a' && *key<='f')
468                                         call->bf_key[i] += (*key - 'a' + 10);
469                                 else if (*key>='A' && *key<='F')
470                                         call->bf_key[i] += (*key - 'A' + 10);
471                                 else
472                                         break;
473                                 key++;
474                                 i++;
475                         }
476                         if (*key) {
477                                 CERROR(call, call->ast, "Option 'c' (encrypt) expects key parameter with hex values 0-9,a-f.\n");
478                                 break;
479                         }
480                         call->bf_len = i;
481                         CDEBUG(call, call->ast, "Option 'c' (encrypt) blowfish key '%s' (len=%d).\n", opt+1, i);
482                         if (call->bchannel)
483                                 bchannel_blowfish(call->bchannel, call->bf_key, call->bf_len);
484                         break;
485 #endif
486                 case 'h':
487                         if (opt[1] != '\0') {
488                                 CERROR(call, call->ast, "Option 'h' (HDLC) expects no parameter.\n", opt);
489                                 break;
490                         }
491                         CDEBUG(call, call->ast, "Option 'h' (HDLC).\n");
492                         if (!call->hdlc)
493                                 call->hdlc = 1;
494                         break;
495                 case 't':
496                         if (opt[1] != '\0') {
497                                 CERROR(call, call->ast, "Option 't' (no_dsp) expects no parameter.\n", opt);
498                                 break;
499                         }
500                         CDEBUG(call, call->ast, "Option 't' (no dsp).\n");
501                         if (!call->nodsp)
502                                 call->nodsp = 1;
503                         break;
504                 case 'q':
505                         if (opt[1] == '\0') {
506                                 CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
507                                 break;
508                         }
509                         CDEBUG(call, call->ast, "Option 'q' (queue).\n");
510                         call->nodsp_queue = atoi(opt+1);
511                         break;
512 #if 0
513                 case 'e':
514                         if (opt[1] == '\0') {
515                                 CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
516                                 break;
517                         }
518                         CDEBUG(call, call->ast, "Option 'e' (echo cancel) with config '%s'.\n", opt+1);
519                         strncpy(call->pipeline, opt+1, sizeof(call->pipeline)-1);
520                         if (call->bchannel)
521                                 bchannel_pipeline(call->bchannel, call->pipeline);
522                         break;
523 #endif
524                 case 'f':
525                         if (opt[1] == '\0') {
526                                 CERROR(call, call->ast, "Option 'f' (faxdetect) expects parameter.\n", opt);
527                                 break;
528                         }
529                         call->faxdetect=atoi(opt+1);
530                         if (!call->dsp)
531                                 call->dsp=ast_dsp_new();
532                         if (call->dsp) {
533                                 #ifdef LCR_FOR_CALLWEAVER
534                                 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_CNG_DETECT);
535                                 #endif
536                                 #ifdef LCR_FOR_ASTERISK
537                                 #ifdef DSP_FEATURE_DTMF_DETECT
538                                 ast_dsp_set_features(call->dsp, DSP_FEATURE_DTMF_DETECT| DSP_FEATURE_FAX_DETECT);
539                                 #else
540                                 ast_dsp_set_features(call->dsp, DSP_FEATURE_DIGIT_DETECT| DSP_FEATURE_FAX_DETECT);
541                                 #endif
542
543                                 #endif
544                                 if (!call->trans) {
545                                         #ifdef LCR_FOR_CALLWEAVER
546                                         call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, 8000, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW, 8000);
547                                         #endif
548                                         #ifdef LCR_FOR_ASTERISK
549                                         #if ASTERISK_VERSION_NUM < 100000
550                                         call->trans=ast_translator_build_path(AST_FORMAT_SLINEAR, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
551                                         #else
552                                         struct ast_format src;
553                                         struct ast_format dst;
554                                         ast_format_set(&dst, AST_FORMAT_SLINEAR, 0);
555                                         ast_format_set(&dst,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
556                                         call->trans=ast_translator_build_path(&dst, &src);
557                                         #endif
558                                         #endif
559                                 }
560                         }
561                         CDEBUG(call, call->ast, "Option 'f' (faxdetect) with config '%s'.\n", call->faxdetect);
562                         break;
563                 case 'r':
564                         if (opt[1] != '\0') {
565                                 CERROR(call, call->ast, "Option 'r' (re-buffer 160 bytes) expects no parameter.\n", opt);
566                                 break;
567                         }
568                         CDEBUG(call, call->ast, "Option 'r' (re-buffer 160 bytes)");
569                         call->rebuffer = 1;
570                         call->framepos = 0;
571                         break;
572                 case 's':
573                         if (opt[1] != '\0') {
574                                 CERROR(call, call->ast, "Option 's' (inband DTMF) expects no parameter.\n", opt);
575                                 break;
576                         }
577                         CDEBUG(call, call->ast, "Option 's' (inband DTMF).\n");
578                         call->inband_dtmf = 1;
579                         break;
580 #if 0
581                 case 'v':
582                         if (opt[1] != 'r' && opt[1] != 't') {
583                                 CERROR(call, call->ast, "Option 'v' (volume) expects parameter.\n", opt);
584                                 break;
585                         }
586                         gain = atoi(opt+2);
587                         if (gain < -8 || gain >8) {
588                                 CERROR(call, call->ast, "Option 'v' (volume) expects parameter in range of -8 through 8.\n");
589                                 break;
590                         }
591                         CDEBUG(call, call->ast, "Option 'v' (volume) with gain 2^%d.\n", gain);
592                         if (opt[1] == 'r') {
593                                 call->rx_gain = gain;
594                                 if (call->bchannel)
595                                         bchannel_gain(call->bchannel, call->rx_gain, 0);
596                         } else {
597                                 call->tx_gain = gain;
598                                 if (call->bchannel)
599                                         bchannel_gain(call->bchannel, call->tx_gain, 1);
600                         }
601                         break;
602 #endif
603                 case 'k':
604                         if (opt[1] != '\0') {
605                                 CERROR(call, call->ast, "Option 'k' (keypad) expects no parameter.\n", opt);
606                                 break;
607                         }
608                         CDEBUG(call, call->ast, "Option 'k' (keypad).\n");
609                         if (!call->keypad)
610                                 call->keypad = 1;
611                         break;
612                 default:
613                         CERROR(call, call->ast, "Option '%s' unknown.\n", opt);
614                 }
615         }
616 }
617
618 /*
619  * send setup info to LCR
620  * this function is called, when asterisk call is received and ref is received
621  */
622 static void send_setup_to_lcr(struct chan_call *call)
623 {
624         union parameter newparam;
625         struct ast_channel *ast = call->ast;
626 //      const char *tmp;
627
628         if (!call->ast || !call->ref)
629                 return;
630
631 #ifdef AST_1_8_OR_HIGHER
632         CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->callerinfo.id);
633 #else
634         CDEBUG(call, call->ast, "Sending setup to LCR. (interface=%s dialstring=%s, cid=%s)\n", call->interface, call->dialstring, call->cid_num);
635 #endif
636
637         /* send setup message to LCR */
638         memset(&newparam, 0, sizeof(union parameter));
639         newparam.setup.dialinginfo.itype = INFO_ITYPE_ISDN;
640         newparam.setup.dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
641         if (call->keypad)
642                 strncpy(newparam.setup.dialinginfo.keypad, call->dialstring, sizeof(newparam.setup.dialinginfo.keypad)-1);
643         else
644                 strncpy(newparam.setup.dialinginfo.id, call->dialstring, sizeof(newparam.setup.dialinginfo.id)-1);
645         if (!!strcmp(call->interface, "pbx"))
646                 strncpy(newparam.setup.dialinginfo.interfaces, call->interface, sizeof(newparam.setup.dialinginfo.interfaces)-1);
647         newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;
648         newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
649         strncpy(newparam.setup.callerinfo.display, call->display, sizeof(newparam.setup.callerinfo.display)-1);
650         call->display[0] = '\0';
651
652 #ifdef AST_1_8_OR_HIGHER
653         /* set stored call information */
654         memcpy(&newparam.setup.callerinfo, &call->callerinfo, sizeof(struct caller_info));
655         memcpy(&newparam.setup.redirinfo, &call->redirinfo, sizeof(struct redir_info));
656 #else
657         if (call->cid_num[0])
658                 strncpy(newparam.setup.callerinfo.id, call->cid_num, sizeof(newparam.setup.callerinfo.id)-1);
659         if (call->cid_name[0])
660                 strncpy(newparam.setup.callerinfo.name, call->cid_name, sizeof(newparam.setup.callerinfo.name)-1);
661         if (call->cid_rdnis[0]) {
662                 strncpy(newparam.setup.redirinfo.id, call->cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
663                 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;
664                 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;
665         }
666         switch(ast->cid.cid_pres & AST_PRES_RESTRICTION) {
667                 case AST_PRES_RESTRICTED:
668                 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
669                 break;
670                 case AST_PRES_UNAVAILABLE:
671                 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
672                 break;
673                 case AST_PRES_ALLOWED:
674                 default:
675                 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
676         }
677         switch(ast->cid.cid_ton) {
678                 case 4:
679                 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
680                 break;
681                 case 2:
682                 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
683                 break;
684                 case 1:
685                 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
686                 break;
687                 default:
688                 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
689         }
690 #endif
691 #warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
692 //      tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
693 //      if (tmp && *tmp)
694 #if ASTERISK_VERSION_NUM < 110000
695 //              ast->transfercapability = atoi(tmp);
696         newparam.setup.capainfo.bearer_capa = ast->transfercapability;
697 #else
698 //              ast_channel_transfercapability_set(ast, atoi(tmp));
699         newparam.setup.capainfo.bearer_capa = ast_channel_transfercapability(ast);
700 #endif
701         newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
702         if (call->hdlc)
703                 newparam.setup.capainfo.source_mode = B_MODE_HDLC;
704         else {
705                 newparam.setup.capainfo.source_mode = B_MODE_TRANSPARENT;
706                 newparam.setup.capainfo.bearer_info1 = (options.law=='a')?3:2;
707         }
708         newparam.setup.capainfo.hlc = INFO_HLC_NONE;
709         newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
710         send_message(MESSAGE_SETUP, call->ref, &newparam);
711
712         /* change to outgoing setup state */
713         call->state = CHAN_LCR_STATE_OUT_SETUP;
714 }
715
716 /*
717  * send dialing info to LCR
718  * this function is called, when setup acknowledge is received and dialing
719  * info is available.
720  */
721 static void send_dialque_to_lcr(struct chan_call *call)
722 {
723         union parameter newparam;
724
725         if (!call->ast || !call->ref || !call->dialque[0])
726                 return;
727
728         CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
729
730         /* send setup message to LCR */
731         memset(&newparam, 0, sizeof(union parameter));
732         if (call->keypad)
733                 strncpy(newparam.information.keypad, call->dialque, sizeof(newparam.information.keypad)-1);
734         else
735                 strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
736         call->dialque[0] = '\0';
737         send_message(MESSAGE_INFORMATION, call->ref, &newparam);
738 }
739
740 /*
741  * in case of a bridge, the unsupported message can be forwarded directly
742  * to the remote call.
743  */
744 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
745 {
746         /* check bridge */
747         if (!call) return;
748         if (!call->bridge_call) return;
749         CDEBUG(call, NULL, "Sending message due bridging.\n");
750         send_message(message_type, call->bridge_call->ref, param);
751 }
752
753 /*
754  * send release message to LCR
755  */
756 static void send_release(struct chan_call *call, int cause, int location)
757 {
758         union parameter newparam;
759
760         /* sending release */
761         memset(&newparam, 0, sizeof(union parameter));
762         newparam.disconnectinfo.cause = cause;
763         newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
764         send_message(MESSAGE_RELEASE, call->ref, &newparam);
765 }
766
767 /*
768  * check if extension matches and start asterisk
769  * if it can match, proceed
770  * if not, release
771  */
772 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
773 {
774         int cause, ret;
775         union parameter newparam;
776 #if ASTERISK_VERSION_NUM < 110000
777         char *exten = ast->exten;
778 #else
779         char s_exten[AST_MAX_EXTENSION];
780         char *exten=s_exten;
781
782         strncpy(exten, ast_channel_exten(ast), AST_MAX_EXTENSION-1);
783 #endif
784
785 if (!*exten)
786                 exten = "s";
787
788 #if ASTERISK_VERSION_NUM < 110000
789         CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast->context, complete?"yes":"no");
790 #else
791         CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", exten, ast_channel_context(ast), complete?"yes":"no");
792 #endif
793
794         if (complete) {
795                 /* if not match */
796 #if ASTERISK_VERSION_NUM < 110000
797                 if (!ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
798 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast->context);
799 #else
800                 if (!ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
801 CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", exten, ast_channel_context(ast));
802 #endif
803                         cause = 1;
804                         goto release;
805                 }
806 #if ASTERISK_VERSION_NUM < 110000
807                 if (!ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
808                         CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast->context);
809 #else
810                 if (!ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
811                         CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", exten, ast_channel_context(ast));
812 #endif
813                         cause = 28;
814                         goto release;
815                 }
816                 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
817                 /* send setup acknowledge to lcr */
818                 memset(&newparam, 0, sizeof(union parameter));
819                 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
820
821                 /* change state */
822                 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
823
824                 goto start;
825         }
826
827 #if ASTERISK_VERSION_NUM < 110000
828         if (ast_canmatch_extension(ast, ast->context, exten, 1, call->oad)) {
829 #else
830         if (ast_canmatch_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
831 #endif
832                 /* send setup acknowledge to lcr */
833                 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
834                         memset(&newparam, 0, sizeof(union parameter));
835                         send_message(MESSAGE_OVERLAP, call->ref, &newparam);
836                 }
837
838                 /* change state */
839                 call->state = CHAN_LCR_STATE_IN_DIALING;
840
841                 /* if match, start pbx */
842 #if ASTERISK_VERSION_NUM < 110000
843         if (ast_exists_extension(ast, ast->context, exten, 1, call->oad)) {
844 #else
845         if (ast_exists_extension(ast, ast_channel_context(ast), exten, 1, call->oad)) {
846 #endif
847                         CDEBUG(call, ast, "Extensions matches.\n");
848                         goto start;
849                 }
850
851                 /* send setup acknowledge to lcr */
852                 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
853                         memset(&newparam, 0, sizeof(union parameter));
854                         send_message(MESSAGE_OVERLAP, call->ref, &newparam);
855                 }
856
857                 /* change state */
858                 call->state = CHAN_LCR_STATE_IN_DIALING;
859
860                 /* if can match */
861                 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
862                 return;
863         }
864
865 #if ASTERISK_VERSION_NUM < 110000
866         if (!*ast->exten) {
867 #else
868         if (!*ast_channel_exten(ast)) {
869 #endif
870                 /* send setup acknowledge to lcr */
871                 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
872                         memset(&newparam, 0, sizeof(union parameter));
873                         send_message(MESSAGE_OVERLAP, call->ref, &newparam);
874                 }
875
876                 /* change state */
877                 call->state = CHAN_LCR_STATE_IN_DIALING;
878
879                 /* if can match */
880                 CDEBUG(call, ast, "There is no 's' extension (and we tried to match it implicitly). Extensions may match, if more digits are dialed.\n");
881                 return;
882         }
883
884         /* if not match */
885         cause = 1;
886         release:
887         /* release lcr */
888         CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
889         send_release(call, cause, LOCATION_PRIVATE_LOCAL);
890         call->ref = 0;
891         /* release asterisk */
892 #if ASTERISK_VERSION_NUM < 110000
893         ast->hangupcause = call->cause;
894 #else
895         ast_channel_hangupcause_set(ast, call->cause);
896 #endif
897         /* change to release state */
898         call->state = CHAN_LCR_STATE_RELEASE;
899         ast_hangup(ast); // call will be destroyed here
900         return;
901
902         start:
903         /* send setup to asterisk */
904         CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
905
906         #ifdef LCR_FOR_CALLWEAVER
907         ast->type = "LCR";
908         snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type ,ast->cid.cid_num, ast_random() & 0xffff);
909         #endif
910
911         ret = ast_pbx_start(ast);
912         if (ret < 0) {
913                 cause = (ret==-2)?34:27;
914                 goto release;
915         }
916         call->pbx_started = 1;
917                 ast_setstate(ast, AST_STATE_RING);
918 }
919
920 /*
921  * incoming setup from LCR
922  */
923 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
924 {
925         struct ast_channel *ast;
926         struct ast_party_redirecting *ast_redir;
927         struct ast_party_caller *ast_caller;
928 #if ASTERISK_VERSION_NUM >= 110000
929         struct ast_party_redirecting s_ast_redir;
930         struct ast_party_caller s_ast_caller;
931         ast_party_redirecting_init(&s_ast_redir);
932         ast_party_caller_init(&s_ast_caller);
933 #endif
934         CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
935
936         /* create asterisk channel instrance */
937
938         #ifdef LCR_FOR_CALLWEAVER
939         ast = ast_channel_alloc(1);
940         #endif
941
942         #ifdef LCR_FOR_ASTERISK
943 #ifdef AST_1_8_OR_HIGHER
944         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", "", 0, "%s/%d", lcr_type, ++glob_channel);
945 #else
946         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
947 #endif
948         #endif
949
950 #if ASTERISK_VERSION_NUM < 110000
951         ast_redir = &ast->redirecting;
952         ast_caller = &ast->caller;
953 #else
954         ast_redir = &s_ast_redir;
955         ast_caller = &s_ast_caller;
956 #endif
957
958         if (!ast) {
959                 /* release */
960                 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
961                 send_release(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
962                 /* remove call */
963                 free_call(call);
964                 return;
965         }
966         /* link together */
967         call->ast = ast;
968 #if ASTERISK_VERSION_NUM < 110000
969         ast->tech_pvt = call;
970         ast->tech = &lcr_tech;
971         ast->fds[0] = call->pipe[0];
972 #else
973         ast_channel_tech_pvt_set(ast, call);
974         ast_channel_tech_set(ast, &lcr_tech);
975         ast_channel_set_fd(ast, 0, call->pipe[0]);
976 #endif
977
978         /* fill setup information */
979         if (param->setup.dialinginfo.id)
980 #if ASTERISK_VERSION_NUM < 110000
981                 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
982         if (param->setup.dialinginfo.context[0])
983                 strncpy(ast->context, param->setup.dialinginfo.context, AST_MAX_CONTEXT-1);
984         else
985                 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
986 #else
987                 ast_channel_exten_set(ast, param->setup.dialinginfo.id);
988         if (param->setup.context[0])
989                 ast_channel_context_set(ast, param->setup.context);
990         else
991                 ast_channel_context_set(ast, param->setup.callerinfo.interface);
992 #endif
993
994
995 #ifdef AST_1_8_OR_HIGHER
996         if (param->setup.callerinfo.id[0]) {
997                 ast_caller->id.number.valid = 1;
998                 ast_caller->id.number.str = strdup(param->setup.callerinfo.id);
999                 if (!param->setup.callerinfo.id[0]) {
1000                         ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1001                         ast_caller->id.number.plan = (0 << 4) | 1;
1002                 }
1003                 switch (param->setup.callerinfo.present) {
1004                         case INFO_PRESENT_ALLOWED:
1005                                 ast_caller->id.number.presentation = AST_PRES_ALLOWED;
1006                         break;
1007                         case INFO_PRESENT_RESTRICTED:
1008                                 ast_caller->id.number.presentation = AST_PRES_RESTRICTED;
1009                         break;
1010                         default:
1011                                 ast_caller->id.number.presentation = AST_PRES_UNAVAILABLE;
1012                 }
1013                 switch (param->setup.callerinfo.screen) {
1014                         case INFO_SCREEN_USER:
1015                                 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1016                         break;
1017                         case INFO_SCREEN_USER_VERIFIED_PASSED:
1018                                 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1019                         break;
1020                         case INFO_SCREEN_USER_VERIFIED_FAILED:
1021                                 ast_caller->id.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1022                         break;
1023                         default:
1024                                 ast_caller->id.number.presentation |= AST_PRES_NETWORK_NUMBER;
1025                 }
1026                 switch (param->setup.callerinfo.ntype) {
1027                         case INFO_NTYPE_SUBSCRIBER:
1028                                 ast_caller->id.number.plan = (4 << 4) | 1;
1029                         break;
1030                         case INFO_NTYPE_NATIONAL:
1031                                 ast_caller->id.number.plan = (2 << 4) | 1;
1032                         break;
1033                         case INFO_NTYPE_INTERNATIONAL:
1034                                 ast_caller->id.number.plan = (1 << 4) | 1;
1035                         break;
1036                         default:
1037                                 ast_caller->id.number.plan = (0 << 4) | 1;
1038                 }
1039         }
1040         if (param->setup.callerinfo.id2[0]) {
1041                 ast_caller->ani.number.valid = 1;
1042                 ast_caller->ani.number.str = strdup(param->setup.callerinfo.id2);
1043                 switch (param->setup.callerinfo.present2) {
1044                         case INFO_PRESENT_ALLOWED:
1045                                 ast_caller->ani.number.presentation = AST_PRES_ALLOWED;
1046                         break;
1047                         case INFO_PRESENT_RESTRICTED:
1048                                 ast_caller->ani.number.presentation = AST_PRES_RESTRICTED;
1049                         break;
1050                         default:
1051                                 ast_caller->ani.number.presentation = AST_PRES_UNAVAILABLE;
1052                 }
1053                 switch (param->setup.callerinfo.screen2) {
1054                         case INFO_SCREEN_USER:
1055                                 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1056                         break;
1057                         case INFO_SCREEN_USER_VERIFIED_PASSED:
1058                                 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1059                         break;
1060                         case INFO_SCREEN_USER_VERIFIED_FAILED:
1061                                 ast_caller->ani.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1062                         break;
1063                         default:
1064                                 ast_caller->ani.number.presentation |= AST_PRES_NETWORK_NUMBER;
1065                 }
1066                 switch (param->setup.callerinfo.ntype2) {
1067                         case INFO_NTYPE_SUBSCRIBER:
1068                                 ast_caller->ani.number.plan = (4 << 4) | 1;
1069                         break;
1070                         case INFO_NTYPE_NATIONAL:
1071                                 ast_caller->ani.number.plan = (2 << 4) | 1;
1072                         break;
1073                         case INFO_NTYPE_INTERNATIONAL:
1074                                 ast_caller->ani.number.plan = (1 << 4) | 1;
1075                         break;
1076                         default:
1077                                 ast_caller->ani.number.plan = (0 << 4) | 1;
1078                 }
1079         }
1080         if (param->setup.callerinfo.name[0]) {
1081                 ast_caller->id.name.valid = 1;
1082                 ast_caller->id.name.str = strdup(param->setup.callerinfo.name);
1083         }
1084 #if ASTERISK_VERSION_NUM >= 110000
1085         ast_channel_caller_set(ast, ast_caller);
1086 #endif
1087         if (param->setup.redirinfo.id[0]) {
1088                 ast_redir->from.number.valid = 1;
1089                 ast_redir->from.number.str = strdup(param->setup.redirinfo.id);
1090                 switch (param->setup.redirinfo.present) {
1091                         case INFO_PRESENT_ALLOWED:
1092                                 ast_redir->from.number.presentation = AST_PRES_ALLOWED;
1093                         break;
1094                         case INFO_PRESENT_RESTRICTED:
1095                                 ast_redir->from.number.presentation = AST_PRES_RESTRICTED;
1096                         break;
1097                         default:
1098                                 ast_redir->from.number.presentation = AST_PRES_UNAVAILABLE;
1099                 }
1100                 switch (param->setup.redirinfo.screen) {
1101                         case INFO_SCREEN_USER:
1102                                 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_UNSCREENED;
1103                         break;
1104                         case INFO_SCREEN_USER_VERIFIED_PASSED:
1105                                 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_PASSED_SCREEN;
1106                         break;
1107                         case INFO_SCREEN_USER_VERIFIED_FAILED:
1108                                 ast_redir->from.number.presentation |= AST_PRES_USER_NUMBER_FAILED_SCREEN;
1109                         break;
1110                         default:
1111                                 ast_redir->from.number.presentation |= AST_PRES_NETWORK_NUMBER;
1112                 }
1113                 switch (param->setup.redirinfo.ntype) {
1114                         case INFO_NTYPE_SUBSCRIBER:
1115                                 ast_redir->from.number.plan = (4 << 4) | 1;
1116                         break;
1117                         case INFO_NTYPE_NATIONAL:
1118                                 ast_redir->from.number.plan = (2 << 4) | 1;
1119                         break;
1120                         case INFO_NTYPE_INTERNATIONAL:
1121                                 ast_redir->from.number.plan = (1 << 4) | 1;
1122                         break;
1123                         default:
1124                                 ast_redir->from.number.plan = (0 << 4) | 1;
1125                 }
1126 #if ASTERISK_VERSION_NUM >= 110000
1127                 ast_channel_redirecting_set(ast, ast_redir);
1128 #endif
1129         }
1130 #else
1131         memset(&ast->cid, 0, sizeof(ast->cid));
1132         if (param->setup.callerinfo.id[0])
1133                 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
1134         if (param->setup.callerinfo.id2[0])
1135                 ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
1136         if (param->setup.callerinfo.name[0])
1137                 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
1138         if (param->setup.redirinfo.id[0])
1139                 ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
1140         switch (param->setup.callerinfo.present) {
1141                 case INFO_PRESENT_ALLOWED:
1142                         ast->cid.cid_pres = AST_PRES_ALLOWED;
1143                 break;
1144                 case INFO_PRESENT_RESTRICTED:
1145                         ast->cid.cid_pres = AST_PRES_RESTRICTED;
1146                 break;
1147                 default:
1148                         ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
1149         }
1150         switch (param->setup.callerinfo.ntype) {
1151                 case INFO_NTYPE_SUBSCRIBER:
1152                         ast->cid.cid_ton = 4;
1153                 break;
1154                 case INFO_NTYPE_NATIONAL:
1155                         ast->cid.cid_ton = 2;
1156                 break;
1157                 case INFO_NTYPE_INTERNATIONAL:
1158                         ast->cid.cid_ton = 1;
1159                 break;
1160                 default:
1161                         ast->cid.cid_ton = 0;
1162         }
1163 #endif
1164
1165 #if ASTERISK_VERSION_NUM < 110000
1166         ast->transfercapability = param->setup.capainfo.bearer_capa;
1167 #else
1168         ast_channel_transfercapability_set(ast, param->setup.capainfo.bearer_capa);
1169 #endif
1170         /* enable hdlc if transcap is data */
1171         if (param->setup.capainfo.source_mode == B_MODE_HDLC)
1172                 call->hdlc = 1;
1173         strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international), sizeof(call->oad)-1);
1174
1175         /* configure channel */
1176 #if ASTERISK_VERSION_NUM < 100000
1177         ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
1178         ast->readformat = ast->rawreadformat = ast->nativeformats;
1179         ast->writeformat = ast->rawwriteformat =  ast->nativeformats;
1180 #else
1181 #if ASTERISK_VERSION_NUM < 110000
1182         ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1183         ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
1184         ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
1185         ast_set_write_format(ast, &ast->rawwriteformat);
1186         ast_set_read_format(ast, &ast->rawreadformat);
1187 #else
1188         ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
1189         ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
1190         ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
1191         ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
1192         ast_set_read_format(ast, ast_channel_rawreadformat(ast));
1193 #endif
1194 #endif
1195 #if ASTERISK_VERSION_NUM < 110000
1196         ast->priority = 1;
1197         ast->hangupcause = 0;
1198 #else
1199         ast_channel_priority_set(ast, 1);
1200         ast_channel_hangupcause_set(ast, 0);
1201 #endif
1202
1203         /* change state */
1204         call->state = CHAN_LCR_STATE_IN_SETUP;
1205
1206         if (!call->pbx_started)
1207                 lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
1208 }
1209
1210 /*
1211  * incoming setup acknowledge from LCR
1212  */
1213 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
1214 {
1215         if (!call->ast) return;
1216
1217         CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
1218
1219         /* send pending digits in dialque */
1220         if (call->dialque[0])
1221                 send_dialque_to_lcr(call);
1222         /* change to overlap state */
1223         call->state = CHAN_LCR_STATE_OUT_DIALING;
1224 }
1225
1226 /*
1227  * incoming proceeding from LCR
1228  */
1229 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
1230 {
1231         CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
1232
1233         /* change state */
1234         call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
1235         /* queue event for asterisk */
1236         if (call->ast && call->pbx_started) {
1237                 if (!wake_global) {
1238                         wake_global = 1;
1239                         char byte = 0;
1240                         int rc;
1241                         rc = write(wake_pipe[1], &byte, 1);
1242                 }
1243                 strncat(call->queue_string, "P", sizeof(call->queue_string)-1);
1244         }
1245
1246 }
1247
1248 /*
1249  * incoming alerting from LCR
1250  */
1251 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
1252 {
1253         CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
1254
1255         /* change state */
1256         call->state = CHAN_LCR_STATE_OUT_ALERTING;
1257         /* queue event to asterisk */
1258         if (call->ast && call->pbx_started) {
1259                 if (!wake_global) {
1260                         wake_global = 1;
1261                         char byte = 0;
1262                         int rc;
1263                         rc = write(wake_pipe[1], &byte, 1);
1264                 }
1265                 strncat(call->queue_string, "R", sizeof(call->queue_string)-1);
1266         }
1267 }
1268
1269 /*
1270  * incoming connect from LCR
1271  */
1272 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
1273 {
1274         CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
1275
1276         /* change state */
1277         call->state = CHAN_LCR_STATE_CONNECT;
1278         /* copy connectinfo */
1279         memcpy(&call->connectinfo, &param->connectinfo, sizeof(struct connect_info));
1280         /* queue event to asterisk */
1281         if (call->ast && call->pbx_started) {
1282                 if (!wake_global) {
1283                         wake_global = 1;
1284                         char byte = 0;
1285                         int rc;
1286                         rc = write(wake_pipe[1], &byte, 1);
1287                 }
1288                 strncat(call->queue_string, "N", sizeof(call->queue_string)-1);
1289         }
1290 }
1291
1292 /*
1293  * incoming disconnect from LCR
1294  */
1295 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
1296 {
1297         struct ast_channel *ast = call->ast;
1298
1299         CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
1300
1301         /* change state */
1302         call->state = CHAN_LCR_STATE_IN_DISCONNECT;
1303         /* save cause */
1304         call->cause = param->disconnectinfo.cause;
1305         call->location = param->disconnectinfo.location;
1306         /* if bridge, forward disconnect and return */
1307 #ifdef TODO
1308         feature flag
1309         if (call->bridge_call) {
1310                 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
1311                 bridge_message_if_bridged(call, message_type, param);
1312                 return;
1313         }
1314 #endif
1315         /* release lcr with same cause */
1316         send_release(call, call->cause, call->location);
1317         call->ref = 0;
1318         /* change to release state */
1319         call->state = CHAN_LCR_STATE_RELEASE;
1320         /* queue release asterisk */
1321         if (ast) {
1322 #if ASTERISK_VERSION_NUM < 110000
1323                 ast->hangupcause = call->cause;
1324 #else
1325                 ast_channel_hangupcause_set(ast, call->cause);
1326 #endif
1327                 if (call->pbx_started) {
1328                         if (!wake_global) {
1329                                 wake_global = 1;
1330                                 char byte = 0;
1331                                 int rc;
1332                                 rc = write(wake_pipe[1], &byte, 1);
1333                         }
1334                         strcpy(call->queue_string, "H"); // overwrite other indications
1335                 } else {
1336                         ast_hangup(ast); // call will be destroyed here
1337                 }
1338         }
1339 }
1340
1341 /*
1342  * incoming release from LCR
1343  */
1344 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
1345 {
1346         struct ast_channel *ast = call->ast;
1347
1348         CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
1349
1350         /* release ref */
1351         call->ref = 0;
1352         /* change to release state */
1353         call->state = CHAN_LCR_STATE_RELEASE;
1354         /* copy release info */
1355         if (!call->cause) {
1356                 call->cause = param->disconnectinfo.cause;
1357                 call->location = param->disconnectinfo.location;
1358         }
1359         /* if we have an asterisk instance, queue hangup, else we are done */
1360         if (ast) {
1361 #if ASTERISK_VERSION_NUM < 110000
1362                 ast->hangupcause = call->cause;
1363 #else
1364                 ast_channel_hangupcause_set(ast, call->cause);
1365 #endif
1366                 if (call->pbx_started) {
1367                         if (!wake_global) {
1368                                 wake_global = 1;
1369                                 char byte = 0;
1370                                 int rc;
1371                                 rc = write(wake_pipe[1], &byte, 1);
1372                         }
1373                         strcpy(call->queue_string, "H");
1374                 } else {
1375                         ast_hangup(ast); // call will be destroyed here
1376                 }
1377         } else {
1378                 free_call(call);
1379         }
1380
1381 }
1382
1383 /*
1384  * incoming information from LCR
1385  */
1386 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
1387 {
1388         struct ast_channel *ast = call->ast;
1389
1390         CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
1391
1392         if (!ast) return;
1393
1394         /* pbx not started */
1395         if (!call->pbx_started) {
1396                 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
1397 #if ASTERISK_VERSION_NUM < 110000
1398                 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
1399 #else
1400                 ast_channel_exten_set(ast, param->information.id);
1401 #endif
1402                 lcr_start_pbx(call, ast, param->information.sending_complete);
1403                 return;
1404         }
1405
1406         /* change dailing state after setup */
1407         if (call->state == CHAN_LCR_STATE_IN_SETUP) {
1408                 CDEBUG(call, call->ast, "Changing from SETUP to DIALING state.\n");
1409                 call->state = CHAN_LCR_STATE_IN_DIALING;
1410 //              ast_setstate(ast, AST_STATE_DIALING);
1411         }
1412
1413         /* queue digits */
1414         if (call->state == CHAN_LCR_STATE_IN_DIALING && param->information.id[0]) {
1415                 if (!wake_global) {
1416                         wake_global = 1;
1417                         char byte = 0;
1418                         int rc;
1419                         rc = write(wake_pipe[1], &byte, 1);
1420                 }
1421                 strncat(call->queue_string, param->information.id, sizeof(call->queue_string)-1);
1422         }
1423
1424         /* use bridge to forware message not supported by asterisk */
1425         if (call->state == CHAN_LCR_STATE_CONNECT) {
1426                 CDEBUG(call, call->ast, "Call is connected, bridging.\n");
1427                 bridge_message_if_bridged(call, message_type, param);
1428         }
1429 }
1430
1431 /*
1432  * incoming information from LCR
1433  */
1434 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
1435 {
1436         CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
1437
1438         if (!call->ast) return;
1439
1440         /* use bridge to forware message not supported by asterisk */
1441         bridge_message_if_bridged(call, message_type, param);
1442 }
1443
1444 /*
1445  * incoming information from LCR
1446  */
1447 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
1448 {
1449         CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
1450
1451         if (!call->ast) return;
1452
1453         /* use bridge to forware message not supported by asterisk */
1454         bridge_message_if_bridged(call, message_type, param);
1455 }
1456
1457 /*
1458  * incoming pattern from LCR
1459  */
1460 static void lcr_in_pattern(struct chan_call *call, int message_type, union parameter *param)
1461 {
1462         union parameter newparam;
1463
1464         CDEBUG(call, call->ast, "Incomming pattern indication from LCR.\n");
1465
1466         if (!call->ast) return;
1467
1468         /* pattern are indicated only once */
1469         if (call->has_pattern)
1470                 return;
1471         call->has_pattern = 1;
1472
1473         /* request bchannel */
1474         CDEBUG(call, call->ast, "Requesting audio path (ref=%d)\n", call->ref);
1475         memset(&newparam, 0, sizeof(union parameter));
1476         send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
1477
1478         /* queue PROGRESS, because tones are available */
1479         if (call->ast && call->pbx_started) {
1480                 if (!wake_global) {
1481                         wake_global = 1;
1482                         char byte = 0;
1483                         int rc;
1484                         rc = write(wake_pipe[1], &byte, 1);
1485                 }
1486                 strncat(call->queue_string, "T", sizeof(call->queue_string)-1);
1487         }
1488 }
1489
1490 /*
1491  * got dtmf from bchannel (locked state)
1492  */
1493 void lcr_in_dtmf(struct chan_call *call, int val)
1494 {
1495         struct ast_channel *ast = call->ast;
1496         char digit[2];
1497
1498         if (!ast)
1499                 return;
1500         if (!call->pbx_started)
1501                 return;
1502
1503         if (!call->dsp_dtmf) {
1504                 CDEBUG(call, call->ast, "Recognised DTMF digit '%c', but ignoring. This is fixed in later mISDN driver.\n", val);
1505                 return;
1506         }
1507
1508         CDEBUG(call, call->ast, "Recognised DTMF digit '%c'.\n", val);
1509         digit[0] = val;
1510         digit[1] = '\0';
1511         if (!wake_global) {
1512                 wake_global = 1;
1513                 char byte = 0;
1514                 int rc;
1515                 rc = write(wake_pipe[1], &byte, 1);
1516         }
1517         strncat(call->queue_string, digit, sizeof(call->queue_string)-1);
1518 }
1519
1520 /*
1521  * message received from LCR
1522  */
1523 int receive_message(int message_type, unsigned int ref, union parameter *param)
1524 {
1525         struct chan_call *call;
1526         union parameter newparam;
1527         int rc = 0;
1528
1529         memset(&newparam, 0, sizeof(union parameter));
1530
1531         /* handle new ref */
1532         if (message_type == MESSAGE_NEWREF) {
1533                 if (param->newref.direction) {
1534                         /* new ref from lcr */
1535                         CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
1536                         if (!ref || find_call_ref(ref)) {
1537                                 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
1538                                 return -1;
1539                         }
1540                         /* allocate new call instance */
1541                         call = alloc_call();
1542                         /* new state */
1543                         call->state = CHAN_LCR_STATE_IN_PREPARE;
1544                         /* set ref */
1545                         call->ref = ref;
1546                         call->ref_was_assigned = 1;
1547                         /* set dtmf (default, use option 'n' to disable */
1548                         call->dsp_dtmf = 1;
1549                         /* wait for setup (or release from asterisk) */
1550                 } else {
1551                         /* new ref, as requested from this remote application */
1552                         CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
1553                         call = find_call_ref(0);
1554                         if (!call) {
1555                                 /* send release, if ref does not exist */
1556                                 CERROR(NULL, NULL, "No call found, that requests a ref.\n");
1557                                 return 0;
1558                         }
1559                         /* store new ref */
1560                         call->ref = ref;
1561                         call->ref_was_assigned = 1;
1562                         /* set dtmf (default, use option 'n' to disable */
1563                         call->dsp_dtmf = 1;
1564                         /* send pending setup info */
1565                         if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
1566                                 send_setup_to_lcr(call);
1567                         /* release if asterisk has signed off */
1568                         else if (call->state == CHAN_LCR_STATE_RELEASE) {
1569                                 /* send release */
1570                                 if (call->cause)
1571                                         send_release(call, call->cause, call->location);
1572                                 else
1573                                         send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
1574                                 /* free call */
1575                                 free_call(call);
1576                                 return 0;
1577                         }
1578                 }
1579                 return 0;
1580         }
1581
1582         /* check ref */
1583         if (!ref) {
1584                 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1585                 return -1;
1586         }
1587         call = find_call_ref(ref);
1588         if (!call) {
1589                 /* ignore ref that is not used (anymore) */
1590                 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1591                 return 0;
1592         }
1593
1594         /* handle messages */
1595         switch(message_type) {
1596                 case MESSAGE_SETUP:
1597                 lcr_in_setup(call, message_type, param);
1598                 break;
1599
1600                 case MESSAGE_OVERLAP:
1601                 lcr_in_overlap(call, message_type, param);
1602                 break;
1603
1604                 case MESSAGE_PROCEEDING:
1605                 lcr_in_proceeding(call, message_type, param);
1606                 break;
1607
1608                 case MESSAGE_ALERTING:
1609                 lcr_in_alerting(call, message_type, param);
1610                 break;
1611
1612                 case MESSAGE_CONNECT:
1613                 lcr_in_connect(call, message_type, param);
1614                 break;
1615
1616                 case MESSAGE_DISCONNECT:
1617                 lcr_in_disconnect(call, message_type, param);
1618                 break;
1619
1620                 case MESSAGE_RELEASE:
1621                 lcr_in_release(call, message_type, param);
1622                 break;
1623
1624                 case MESSAGE_INFORMATION:
1625                 lcr_in_information(call, message_type, param);
1626                 break;
1627
1628                 case MESSAGE_NOTIFY:
1629                 lcr_in_notify(call, message_type, param);
1630                 break;
1631
1632                 case MESSAGE_FACILITY:
1633                 lcr_in_facility(call, message_type, param);
1634                 break;
1635
1636                 case MESSAGE_PATTERN: // audio available from LCR
1637                 if (!call->has_pattern)
1638                         lcr_in_pattern(call, message_type, param);
1639                 break;
1640
1641                 case MESSAGE_NOPATTERN: // audio not available from LCR
1642                 break;
1643
1644                 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1645                 call->audiopath = param->audiopath;
1646                 break;
1647
1648                 case MESSAGE_TRAFFIC: // if remote audio connected or hold
1649                 {
1650                         unsigned char *p = param->traffic.data;
1651                         int i, len = param->traffic.len;
1652                         for (i = 0; i < len; i++, p++)
1653                                 *p = flip_bits[*p];
1654                 }
1655                 rc = write(call->pipe[1], param->traffic.data, param->traffic.len);
1656                 break;
1657
1658                 default:
1659                 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1660                 break;
1661         }
1662         return rc;
1663 }
1664
1665 /*
1666  * release all calls (due to broken socket)
1667  */
1668 static void release_all_calls(void)
1669 {
1670         struct chan_call *call;
1671
1672 again:
1673         call = call_first;
1674         while(call) {
1675                 /* no ast, so we may directly free call */
1676                 if (!call->ast) {
1677                         CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1678                         free_call(call);
1679                         goto again;
1680                 }
1681                 /* already in release process */
1682                 if (call->state == CHAN_LCR_STATE_RELEASE) {
1683                         call = call->next;
1684                         continue;
1685                 }
1686                 /* release or queue release */
1687                 call->ref = 0;
1688                 call->state = CHAN_LCR_STATE_RELEASE;
1689                 if (!call->pbx_started) {
1690                         CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1691                         ast_hangup(call->ast); // call will be destroyed here
1692                         goto again;
1693                 }
1694                 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1695                 if (!wake_global) {
1696                         wake_global = 1;
1697                         char byte = 0;
1698                         int rc;
1699                         rc = write(wake_pipe[1], &byte, 1);
1700                 }
1701                 strcpy(call->queue_string, "H");
1702                 call = call->next;
1703         }
1704 }
1705
1706 void close_socket(void);
1707
1708 /* asterisk handler
1709  * warning! not thread safe
1710  * returns -1 for socket error, 0 for no work, 1 for work
1711  */
1712 static int handle_socket(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1713 {
1714         int len;
1715         struct admin_list *admin;
1716         struct admin_message msg;
1717
1718         if ((what & LCR_FD_READ)) {
1719                 /* read from socket */
1720                 len = read(lcr_sock, &msg, sizeof(msg));
1721                 if (len == 0) {
1722                         CERROR(NULL, NULL, "Socket closed.(read)\n");
1723                         error:
1724                         CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1725                         close_socket();
1726                         release_all_calls();
1727                         schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1728                         return 0;
1729                 }
1730                 if (len > 0) {
1731                         if (len != sizeof(msg)) {
1732                                 CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1733                                 goto error;
1734                         }
1735                         if (msg.message != ADMIN_MESSAGE) {
1736                                 CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1737                                 goto error;
1738                         }
1739                         receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1740                 } else {
1741                         CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1742                         goto error;
1743                 }
1744         }
1745
1746         if ((what & LCR_FD_WRITE)) {
1747                 /* write to socket */
1748                 if (!admin_first) {
1749                         socket_fd.when &= ~LCR_FD_WRITE;
1750                         return 0;
1751                 }
1752                 admin = admin_first;
1753                 len = write(lcr_sock, &admin->msg, sizeof(msg));
1754                 if (len == 0) {
1755                         CERROR(NULL, NULL, "Socket closed.(write)\n");
1756                         goto error;
1757                 }
1758                 if (len > 0) {
1759                         if (len != sizeof(msg)) {
1760                                 CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1761                                 goto error;
1762                         }
1763                         /* free head */
1764                         admin_first = admin->next;
1765                         free(admin);
1766                         global_change = 1;
1767                 } else {
1768                         CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1769                         goto error;
1770                 }
1771         }
1772
1773         return 0;
1774 }
1775
1776 /*
1777  * open and close socket and thread
1778  */
1779 int open_socket(void)
1780 {
1781         int conn;
1782         struct sockaddr_un sock_address;
1783         union parameter param;
1784
1785         /* open socket */
1786         if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
1787                 CERROR(NULL, NULL, "Failed to create socket.\n");
1788                 return lcr_sock;
1789         }
1790
1791         /* set socket address and name */
1792         memset(&sock_address, 0, sizeof(sock_address));
1793         sock_address.sun_family = PF_UNIX;
1794         sprintf(sock_address.sun_path, SOCKET_NAME, options.lock);
1795
1796         /* connect socket */
1797         if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0) {
1798                 close(lcr_sock);
1799                 lcr_sock = -1;
1800                 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1801                 return conn;
1802         }
1803
1804         /* register socket fd */
1805         memset(&socket_fd, 0, sizeof(socket_fd));
1806         socket_fd.fd = lcr_sock;
1807         register_fd(&socket_fd, LCR_FD_READ | LCR_FD_EXCEPT, handle_socket, NULL, 0);
1808
1809         /* enque hello message */
1810         memset(&param, 0, sizeof(param));
1811         strcpy(param.hello.application, "asterisk");
1812         send_message(MESSAGE_HELLO, 0, &param);
1813
1814         return lcr_sock;
1815 }
1816
1817 void close_socket(void)
1818 {
1819         struct admin_list *admin, *temp;
1820
1821         /* socket not created */
1822         if (lcr_sock < 0)
1823                 return;
1824
1825         unregister_fd(&socket_fd);
1826
1827         /* flush pending messages */
1828         admin = admin_first;
1829         while(admin) {
1830                 temp = admin;
1831                 admin = admin->next;
1832                 free(temp);
1833         }
1834         admin_first = NULL;
1835
1836         /* close socket */
1837         close(lcr_sock);
1838         lcr_sock = -1;
1839         global_change = 1;
1840 }
1841
1842
1843 /* sending queue to asterisk */
1844 static int wake_event(struct lcr_fd *fd, unsigned int what, void *instance, int index)
1845 {
1846         char byte;
1847         int rc;
1848
1849         rc = read(wake_pipe[0], &byte, 1);
1850
1851         wake_global = 0;
1852
1853         return 0;
1854 }
1855
1856 static void handle_queue()
1857 {
1858         struct chan_call *call;
1859         struct ast_channel *ast;
1860         struct ast_frame fr;
1861         char *p;
1862
1863 again:
1864         call = call_first;
1865         while(call) {
1866                 p = call->queue_string;
1867                 ast = call->ast;
1868                 if (*p && ast) {
1869                         if (ast_channel_trylock(ast)) {
1870                                 ast_mutex_unlock(&chan_lock);
1871                                 usleep(1);
1872                                 ast_mutex_lock(&chan_lock);
1873                                 goto again;
1874                         }
1875                         while(*p) {
1876                                 switch (*p) {
1877                                 case 'T':
1878                                         CDEBUG(call, ast, "Sending queued PROGRESS to Asterisk.\n");
1879                                         ast_queue_control(ast, AST_CONTROL_PROGRESS);
1880                                         break;
1881                                 case 'P':
1882                                         CDEBUG(call, ast, "Sending queued PROCEEDING to Asterisk.\n");
1883                                         ast_queue_control(ast, AST_CONTROL_PROCEEDING);
1884                                         break;
1885                                 case 'R':
1886                                         CDEBUG(call, ast, "Sending queued RINGING to Asterisk.\n");
1887                                         ast_queue_control(ast, AST_CONTROL_RINGING);
1888                                         ast_setstate(ast, AST_STATE_RINGING);
1889                                         break;
1890                                 case 'N':
1891                                         CDEBUG(call, ast, "Sending queued ANSWER to Asterisk.\n");
1892                                         ast_queue_control(ast, AST_CONTROL_ANSWER);
1893                                         break;
1894                                 case 'H':
1895                                         CDEBUG(call, ast, "Sending queued HANGUP to Asterisk.\n");
1896                                         ast_queue_hangup(ast);
1897                                         break;
1898                                 case '1': case '2': case '3': case 'A':
1899                                 case '4': case '5': case '6': case 'B':
1900                                 case '7': case '8': case '9': case 'C':
1901                                 case '*': case '0': case '#': case 'D':
1902                                         CDEBUG(call, ast, "Sending queued digit '%c' to Asterisk.\n", *p);
1903                                         /* send digit to asterisk */
1904                                         memset(&fr, 0, sizeof(fr));
1905
1906                                         #ifdef LCR_FOR_ASTERISK
1907                                         fr.frametype = AST_FRAME_DTMF_BEGIN;
1908                                         #endif
1909
1910                                         #ifdef LCR_FOR_CALLWEAVER
1911                                         fr.frametype = AST_FRAME_DTMF;
1912                                         #endif
1913
1914 #ifdef AST_1_8_OR_HIGHER
1915                                         fr.subclass.integer = *p;
1916 #else
1917                                         fr.subclass = *p;
1918 #endif
1919                                         fr.delivery = ast_tv(0, 0);
1920                                         ast_queue_frame(ast, &fr);
1921
1922                                         #ifdef LCR_FOR_ASTERISK
1923                                         fr.frametype = AST_FRAME_DTMF_END;
1924                                         ast_queue_frame(ast, &fr);
1925                                         #endif
1926
1927                                         break;
1928                                 default:
1929                                         CDEBUG(call, ast, "Ignoring queued digit 0x%02x.\n", *p);
1930                                 }
1931                                 p++;
1932                         }
1933                         call->queue_string[0] = '\0';
1934                         ast_channel_unlock(ast);
1935                 }
1936                 call = call->next;
1937         }
1938 }
1939
1940 static int handle_retry(struct lcr_timer *timer, void *instance, int index)
1941 {
1942         CDEBUG(NULL, NULL, "Retry to open socket.\n");
1943         if (open_socket() < 0)
1944                 schedule_timer(&socket_retry, SOCKET_RETRY_TIMER, 0);
1945
1946         return 0;
1947 }
1948
1949 void lock_chan(void)
1950 {
1951         ast_mutex_lock(&chan_lock);
1952 }
1953
1954 void unlock_chan(void)
1955 {
1956         ast_mutex_unlock(&chan_lock);
1957 }
1958
1959 /* chan_lcr thread */
1960 static void *chan_thread(void *arg)
1961 {
1962         if (pipe(wake_pipe) < 0) {
1963                 CERROR(NULL, NULL, "Failed to open pipe.\n");
1964                 return NULL;
1965         }
1966         memset(&wake_fd, 0, sizeof(wake_fd));
1967         wake_fd.fd = wake_pipe[0];
1968         register_fd(&wake_fd, LCR_FD_READ, wake_event, NULL, 0);
1969
1970         memset(&socket_retry, 0, sizeof(socket_retry));
1971         add_timer(&socket_retry, handle_retry, NULL, 0);
1972
1973         /* open socket the first time */
1974         handle_retry(NULL, NULL, 0);
1975
1976         ast_mutex_lock(&chan_lock);
1977
1978         while(1) {
1979                 handle_queue();
1980                 select_main(0, &global_change, lock_chan, unlock_chan);
1981         }
1982
1983         return NULL;
1984 }
1985
1986 /*
1987  * new asterisk instance
1988  */
1989 static
1990 #ifdef AST_1_8_OR_HIGHER
1991 #if ASTERISK_VERSION_NUM < 100000
1992 struct ast_channel *lcr_request(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
1993 #else
1994 struct ast_channel *lcr_request(const char *type, struct ast_format_cap *format, const struct ast_channel *requestor, void *data, int *cause)
1995 #endif
1996 #else
1997 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1998 #endif
1999 {
2000         char exten[256], *dial, *interface, *opt;
2001         struct ast_channel *ast;
2002         struct chan_call *call;
2003         const struct ast_party_redirecting *req_redir;
2004         const struct ast_party_caller *req_caller;
2005
2006         ast_mutex_lock(&chan_lock);
2007         CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
2008
2009         /* if socket is closed */
2010         if (lcr_sock < 0) {
2011                 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
2012                 ast_mutex_unlock(&chan_lock);
2013                 return NULL;
2014         }
2015
2016         /* create call instance */
2017         call = alloc_call();
2018         if (!call) {
2019                 /* failed to create instance */
2020                 ast_mutex_unlock(&chan_lock);
2021                 return NULL;
2022         }
2023
2024         /* create asterisk channel instrance */
2025
2026         #ifdef LCR_FOR_ASTERISK
2027 #ifdef AST_1_8_OR_HIGHER
2028         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, NULL, NULL, NULL, NULL, 0, "%s/%d", lcr_type, ++glob_channel);
2029 #else
2030         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
2031 #endif
2032         #endif
2033
2034         #ifdef LCR_FOR_CALLWEAVER
2035         ast = ast_channel_alloc(1);
2036         #endif
2037
2038         if (!ast) {
2039                 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
2040                 free_call(call);
2041                 /* failed to create instance */
2042                 ast_mutex_unlock(&chan_lock);
2043                 return NULL;
2044         }
2045 #if ASTERISK_VERSION_NUM < 110000
2046         ast->tech = &lcr_tech;
2047         ast->tech_pvt = (void *)1L; // set pointer or asterisk will not call
2048         req_redir = &requestor->redirecting;
2049         req_caller = &requestor->caller;
2050 #else
2051         ast_channel_tech_set(ast, &lcr_tech);
2052         ast_channel_tech_pvt_set(ast, (void *)1L); // set pointer or asterisk will not call
2053         req_redir = ast_channel_redirecting(requestor);
2054         req_caller = ast_channel_caller(requestor);
2055 #endif
2056         /* configure channel */
2057 #if ASTERISK_VERSION_NUM < 100000
2058 #if ASTERISK_VERSION_NUM < 110000
2059         ast->nativeformats = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
2060         ast->readformat = ast->rawreadformat = ast->nativeformats;
2061         ast->writeformat = ast->rawwriteformat =  ast->nativeformats;
2062 #else
2063         ast_channel_nativeformats_set(ast, (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW);
2064         ast->readformat = ast->rawreadformat = ast_channel_nativeformats(ast);
2065         ast->writeformat = ast->rawwriteformat =  ast_channel_nativeformats(ast);
2066 #endif
2067 #else
2068 #if ASTERISK_VERSION_NUM < 110000
2069         ast_format_set(&ast->rawwriteformat ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2070         ast_format_copy(&ast->rawreadformat, &ast->rawwriteformat);
2071         ast_format_cap_set(ast->nativeformats, &ast->rawwriteformat);
2072         ast_set_write_format(ast, &ast->rawwriteformat);
2073         ast_set_read_format(ast, &ast->rawreadformat);
2074 #else
2075         ast_format_set(ast_channel_rawwriteformat(ast) ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
2076         ast_format_copy(ast_channel_rawreadformat(ast), ast_channel_rawwriteformat(ast));
2077         ast_format_cap_set(ast_channel_nativeformats(ast), ast_channel_rawwriteformat(ast));
2078         ast_set_write_format(ast, ast_channel_rawwriteformat(ast));
2079         ast_set_read_format(ast, ast_channel_rawreadformat(ast));
2080 #endif
2081 #endif
2082 #if ASTERISK_VERSION_NUM < 110000
2083         ast->priority = 1;
2084         ast->hangupcause = 0;
2085 #else
2086         ast_channel_priority_set(ast, 1);
2087         ast_channel_hangupcause_set(ast, 0);
2088 #endif
2089
2090         /* link together */
2091         call->ast = ast;
2092 #if ASTERISK_VERSION_NUM < 110000
2093         ast->tech_pvt = call;
2094         ast->fds[0] = call->pipe[0];
2095 #else
2096         ast_channel_tech_pvt_set(ast, call);
2097         ast_channel_set_fd(ast, 0, call->pipe[0]);
2098 #endif
2099         call->pbx_started = 0;
2100         /* set state */
2101         call->state = CHAN_LCR_STATE_OUT_PREPARE;
2102
2103         /*
2104          * Extract interface, dialstring, options from data.
2105          * Formats can be:
2106          *      <dialstring>
2107          *      <interface>/<dialstring>
2108          *      <interface>/<dialstring>/options
2109          */
2110         strncpy(exten, (char *)data, sizeof(exten)-1);
2111         exten[sizeof(exten)-1] = '\0';
2112         if ((dial = strchr(exten, '/'))) {
2113                 *dial++ = '\0';
2114                 interface = exten;
2115                 if ((opt = strchr(dial, '/')))
2116                         *opt++ = '\0';
2117                 else
2118                         opt = "";
2119         } else {
2120                 dial = exten;
2121                 interface = "";
2122                 opt = "";
2123         }
2124         strncpy(call->interface, interface, sizeof(call->interface)-1);
2125         strncpy(call->dialstring, dial, sizeof(call->dialstring)-1);
2126         apply_opt(call, (char *)opt);
2127
2128 #ifdef AST_1_8_OR_HIGHER
2129 //      clone_variables(requestor, ast);
2130
2131 #if 0
2132         ast->caller.ani.number.valid=                   req_caller->ani.number.valid;
2133         if (req_caller->ani.number.valid)
2134           if (req_caller->ani.number.str)
2135             if (req_caller->ani.number.str[0])
2136                 ast->caller.ani.number.str=             strdup(req_caller->ani.number.str);
2137         ast->caller.ani.number.plan=                    req_caller->ani.number.plan;
2138         ast->caller.ani.number.presentation=            req_caller->ani.number.presentation;
2139
2140         ast->caller.ani.name.valid=                     req_caller->ani.name.valid;
2141         if (req_caller->ani.name.valid)
2142           if (req_caller->ani.name.str)
2143             if (req_caller->ani.name.str[0])
2144                 ast->caller.ani.name.str=               strdup(req_caller->ani.name.str);
2145         ast->caller.ani.name.presentation=              req_caller->ani.name.presentation;
2146
2147         ast->caller.ani.subaddress.valid=               req_caller->ani.subaddress.valid;
2148         if (req_caller->ani.subaddress.valid)
2149           if (req_caller->ani.subaddress.str)
2150             if (req_caller->ani.subaddress.str[0])
2151                 ast->caller.ani.subaddress.str=         strdup(req_caller->ani.subaddress.str);
2152         ast->caller.ani.subaddress.type=                req_caller->ani.subaddress.type;
2153
2154         ast->caller.id.number.valid=                    req_caller->id.number.valid;
2155         if (req_caller->id.number.valid)
2156           if (req_caller->id.number.str)
2157             if (req_caller->id.number.str[0])
2158                 ast->caller.id.number.str=              strdup(req_caller->id.number.str);
2159         ast->caller.id.number.plan=                     req_caller->id.number.plan;
2160         ast->caller.id.number.presentation=             req_caller->id.number.presentation;
2161
2162         ast->caller.id.name.valid=                      req_caller->id.name.valid;
2163         if (req_caller->id.name.valid)
2164           if (req_caller->id.name.str)
2165             if (req_caller->id.name.str[0])
2166                 ast->caller.id.name.str=                strdup(req_caller->id.name.str);
2167         ast->caller.id.name.presentation=               req_caller->id.name.presentation;
2168
2169         ast->caller.id.subaddress.valid=                req_caller->id.subaddress.valid;
2170         if (req_caller->id.subaddress.valid)
2171           if (req_caller->id.subaddress.str)
2172             if (req_caller->id.subaddress.str[0])
2173                 ast->caller.id.subaddress.str=          strdup(req_caller->id.subaddress.str);
2174         ast->caller.id.subaddress.type=                 req_caller->id.subaddress.type;
2175
2176         if (requestor->dialed.number.str)
2177           if (requestor->dialed.number.str[0])
2178                 ast->dialed.number.str=                 strdup(requestor->dialed.number.str);
2179         ast->dialed.number.plan=                        requestor->dialed.number.plan;
2180
2181         ast->dialed.subaddress.valid=                   requestor->dialed.subaddress.valid;
2182         if (requestor->dialed.subaddress.valid)
2183           if (requestor->dialed.subaddress.str)
2184             if (requestor->dialed.subaddress.str[0])
2185                 ast->dialed.subaddress.str=             strdup(requestor->dialed.subaddress.str);
2186         ast->dialed.subaddress.type=                    requestor->dialed.subaddress.type;
2187
2188         ast->dialed.transit_network_select=             requestor->dialed.transit_network_select;
2189         ast->redirecting.count=                         req_redir->count;
2190         ast->redirecting.reason=                        req_redir->reason;
2191
2192         ast->redirecting.from.number.valid=             req_redir->from.number.valid;
2193         if (req_redir->from.number.valid)
2194           if (req_redir->from.number.str)
2195             if (req_redir->from.number.str[0])
2196                 ast->redirecting.from.number.str=       strdup(req_redir->from.number.str);
2197         ast->redirecting.from.number.plan=              req_redir->from.number.plan;
2198         ast->redirecting.from.number.presentation=      req_redir->from.number.presentation;
2199
2200         ast->redirecting.to.number.valid=               req_redir->to.number.valid;
2201         if (req_redir->to.number.valid)
2202           if (req_redir->to.number.str)
2203             if (req_redir->to.number.str[0])
2204                 ast->redirecting.to.number.str=         strdup(req_redir->to.number.str);
2205         ast->redirecting.to.number.plan=                req_redir->to.number.plan;
2206         ast->redirecting.to.number.presentation=        req_redir->to.number.presentation;
2207 #endif
2208         /* store call information for setup */
2209
2210         /* caller ID */
2211         if (requestor && req_caller->id.number.valid) {
2212                 if (req_caller->id.number.str)
2213                         strncpy(call->callerinfo.id, req_caller->id.number.str, sizeof(call->callerinfo.id)-1);
2214                 switch(req_caller->id.number.presentation & AST_PRES_RESTRICTION) {
2215                         case AST_PRES_RESTRICTED:
2216                         call->callerinfo.present = INFO_PRESENT_RESTRICTED;
2217                         break;
2218                         case AST_PRES_UNAVAILABLE:
2219                         call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2220                         break;
2221                         case AST_PRES_ALLOWED:
2222                         default:
2223                         call->callerinfo.present = INFO_PRESENT_ALLOWED;
2224                 }
2225                 switch(req_caller->id.number.presentation & AST_PRES_NUMBER_TYPE) {
2226                         case AST_PRES_USER_NUMBER_UNSCREENED:
2227                         call->callerinfo.screen = INFO_SCREEN_USER;
2228                         break;
2229                         case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2230                         call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2231                         break;
2232                         case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2233                         call->callerinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2234                         break;
2235                         default:
2236                         call->callerinfo.screen = INFO_SCREEN_NETWORK;
2237                 }
2238                 switch((req_caller->id.number.plan >> 4) & 7) {
2239                         case 4:
2240                         call->callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2241                         break;
2242                         case 2:
2243                         call->callerinfo.ntype = INFO_NTYPE_NATIONAL;
2244                         break;
2245                         case 1:
2246                         call->callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2247                         break;
2248                         default:
2249                         call->callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2250                 }
2251         } else
2252                 call->callerinfo.present = INFO_PRESENT_NOTAVAIL;
2253
2254         /* caller ID 2 */
2255         if (requestor && req_caller->ani.number.valid) {
2256                 if (req_caller->ani.number.str)
2257                         strncpy(call->callerinfo.id2, req_caller->ani.number.str, sizeof(call->callerinfo.id2)-1);
2258                 switch(req_caller->ani.number.presentation & AST_PRES_RESTRICTION) {
2259                         case AST_PRES_RESTRICTED:
2260                         call->callerinfo.present2 = INFO_PRESENT_RESTRICTED;
2261                         break;
2262                         case AST_PRES_UNAVAILABLE:
2263                         call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2264                         break;
2265                         case AST_PRES_ALLOWED:
2266                         default:
2267                         call->callerinfo.present2 = INFO_PRESENT_ALLOWED;
2268                 }
2269                 switch(req_caller->ani.number.presentation & AST_PRES_NUMBER_TYPE) {
2270                         case AST_PRES_USER_NUMBER_UNSCREENED:
2271                         call->callerinfo.screen2 = INFO_SCREEN_USER;
2272                         break;
2273                         case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2274                         call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_PASSED;
2275                         break;
2276                         case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2277                         call->callerinfo.screen2 = INFO_SCREEN_USER_VERIFIED_FAILED;
2278                         break;
2279                         default:
2280                         call->callerinfo.screen2 = INFO_SCREEN_NETWORK;
2281                 }
2282                 switch((req_caller->ani.number.plan >> 4) & 7) {
2283                         case 4:
2284                         call->callerinfo.ntype2 = INFO_NTYPE_SUBSCRIBER;
2285                         break;
2286                         case 2:
2287                         call->callerinfo.ntype2 = INFO_NTYPE_NATIONAL;
2288                         break;
2289                         case 1:
2290                         call->callerinfo.ntype2 = INFO_NTYPE_INTERNATIONAL;
2291                         break;
2292                         default:
2293                         call->callerinfo.ntype2 = INFO_NTYPE_UNKNOWN;
2294                 }
2295         } else
2296                 call->callerinfo.present2 = INFO_PRESENT_NOTAVAIL;
2297
2298         /* caller name */
2299         if (requestor && req_caller->id.name.valid) {
2300                 if (req_caller->id.name.str)
2301                         strncpy(call->callerinfo.name, req_caller->id.name.str, sizeof(call->callerinfo.name)-1);
2302         }
2303
2304         /* redir number */
2305         if (requestor && req_redir->from.number.valid) {
2306                 call->redirinfo.itype = INFO_ITYPE_CHAN;
2307                 if (req_redir->from.number.str)
2308                         strncpy(call->redirinfo.id, req_redir->from.number.str, sizeof(call->redirinfo.id)-1);
2309                 switch(req_redir->from.number.presentation & AST_PRES_RESTRICTION) {
2310                         case AST_PRES_RESTRICTED:
2311                         call->redirinfo.present = INFO_PRESENT_RESTRICTED;
2312                         break;
2313                         case AST_PRES_UNAVAILABLE:
2314                         call->redirinfo.present = INFO_PRESENT_NOTAVAIL;
2315                         break;
2316                         case AST_PRES_ALLOWED:
2317                         default:
2318                         call->redirinfo.present = INFO_PRESENT_ALLOWED;
2319                 }
2320                 switch(req_redir->from.number.presentation & AST_PRES_NUMBER_TYPE) {
2321                         case AST_PRES_USER_NUMBER_UNSCREENED:
2322                         call->redirinfo.screen = INFO_SCREEN_USER;
2323                         break;
2324                         case AST_PRES_USER_NUMBER_PASSED_SCREEN:
2325                         call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_PASSED;
2326                         break;
2327                         case AST_PRES_USER_NUMBER_FAILED_SCREEN:
2328                         call->redirinfo.screen = INFO_SCREEN_USER_VERIFIED_FAILED;
2329                         break;
2330                         default:
2331                         call->redirinfo.screen = INFO_SCREEN_NETWORK;
2332                 }
2333                 switch((req_redir->from.number.plan >> 4) & 7) {
2334                         case 4:
2335                         call->redirinfo.ntype = INFO_NTYPE_SUBSCRIBER;
2336                         break;
2337                         case 2:
2338                         call->redirinfo.ntype = INFO_NTYPE_NATIONAL;
2339                         break;
2340                         case 1:
2341                         call->redirinfo.ntype = INFO_NTYPE_INTERNATIONAL;
2342                         break;
2343                         default:
2344                         call->redirinfo.ntype = INFO_NTYPE_UNKNOWN;
2345                 }
2346         }
2347 #endif
2348
2349         ast_mutex_unlock(&chan_lock);
2350         return ast;
2351 }
2352
2353 /*
2354  * call from asterisk
2355  */
2356 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
2357 {
2358         union parameter newparam;
2359         struct chan_call *call;
2360 #if ASTERISK_VERSION_NUM >= 110000
2361         int transfercapability;
2362 #endif
2363
2364         ast_mutex_lock(&chan_lock);
2365 #if ASTERISK_VERSION_NUM < 110000
2366         call = ast->tech_pvt;
2367 #else
2368         call = ast_channel_tech_pvt(ast);
2369 #endif
2370
2371         #ifdef LCR_FOR_CALLWEAVER
2372         ast->type = "LCR";
2373         snprintf(ast->name, sizeof(ast->name), "%s/%s-%04x",lcr_type, call->dialstring, ast_random() & 0xffff);
2374         #endif
2375
2376         if (!call) {
2377                 CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
2378                 ast_mutex_unlock(&chan_lock);
2379                 return -1;
2380         }
2381
2382         CDEBUG(NULL, ast, "Received call from Asterisk.\n");
2383
2384         /* pbx process is started */
2385         call->pbx_started = 1;
2386         /* send MESSAGE_NEWREF */
2387         memset(&newparam, 0, sizeof(union parameter));
2388         newparam.newref.direction = 0; /* request from app */
2389         send_message(MESSAGE_NEWREF, 0, &newparam);
2390
2391         /* set hdlc if capability requires hdlc */
2392 #if ASTERISK_VERSION_NUM < 110000
2393         if (ast->transfercapability == INFO_BC_DATAUNRESTRICTED
2394          || ast->transfercapability == INFO_BC_DATARESTRICTED
2395          || ast->transfercapability == INFO_BC_VIDEO)
2396 #else
2397         transfercapability=ast_channel_transfercapability(ast);
2398         if (transfercapability == INFO_BC_DATAUNRESTRICTED
2399          || transfercapability == INFO_BC_DATARESTRICTED
2400          || transfercapability == INFO_BC_VIDEO)
2401 #endif
2402                 call->hdlc = 1;
2403         /* if hdlc is forced by option, we change transcap to data */
2404         if (call->hdlc
2405 #if ASTERISK_VERSION_NUM < 110000
2406          && ast->transfercapability != INFO_BC_DATAUNRESTRICTED
2407          && ast->transfercapability != INFO_BC_DATARESTRICTED
2408          && ast->transfercapability != INFO_BC_VIDEO)
2409                 ast->transfercapability = INFO_BC_DATAUNRESTRICTED;
2410 #else
2411          && transfercapability != INFO_BC_DATAUNRESTRICTED
2412          && transfercapability != INFO_BC_DATARESTRICTED
2413          && transfercapability != INFO_BC_VIDEO)
2414                 transfercapability = INFO_BC_DATAUNRESTRICTED;
2415 #endif
2416
2417 #ifndef AST_1_8_OR_HIGHER
2418         call->cid_num[0] = 0;
2419         call->cid_name[0] = 0;
2420         call->cid_rdnis[0] = 0;
2421
2422         if (ast->cid.cid_num) if (ast->cid.cid_num[0])
2423                 strncpy(call->cid_num, ast->cid.cid_num,
2424                         sizeof(call->cid_num)-1);
2425         if (ast->cid.cid_name) if (ast->cid.cid_name[0])
2426                 strncpy(call->cid_name, ast->cid.cid_name,
2427                         sizeof(call->cid_name)-1);
2428         if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
2429                 strncpy(call->cid_rdnis, ast->cid.cid_rdnis,
2430                         sizeof(call->cid_rdnis)-1);
2431 #endif
2432
2433         ast_mutex_unlock(&chan_lock);
2434         return 0;
2435 }
2436
2437 static void send_digit_to_chan(struct ast_channel * ast, char digit )
2438 {
2439         static const char* dtmf_tones[] = {
2440                 "!941+1336/100,!0/100", /* 0 */
2441                 "!697+1209/100,!0/100", /* 1 */
2442                 "!697+1336/100,!0/100", /* 2 */
2443                 "!697+1477/100,!0/100", /* 3 */
2444                 "!770+1209/100,!0/100", /* 4 */
2445                 "!770+1336/100,!0/100", /* 5 */
2446                 "!770+1477/100,!0/100", /* 6 */
2447                 "!852+1209/100,!0/100", /* 7 */
2448                 "!852+1336/100,!0/100", /* 8 */
2449                 "!852+1477/100,!0/100", /* 9 */
2450                 "!697+1633/100,!0/100", /* A */
2451                 "!770+1633/100,!0/100", /* B */
2452                 "!852+1633/100,!0/100", /* C */
2453                 "!941+1633/100,!0/100", /* D */
2454                 "!941+1209/100,!0/100", /* * */
2455                 "!941+1477/100,!0/100" };       /* # */
2456
2457         if (digit >= '0' && digit <='9')
2458                 ast_playtones_start(ast,0,dtmf_tones[digit-'0'], 0);
2459         else if (digit >= 'A' && digit <= 'D')
2460                 ast_playtones_start(ast,0,dtmf_tones[digit-'A'+10], 0);
2461         else if (digit == '*')
2462                 ast_playtones_start(ast,0,dtmf_tones[14], 0);
2463         else if (digit == '#')
2464                 ast_playtones_start(ast,0,dtmf_tones[15], 0);
2465         else {
2466 #if ASTERISK_VERSION_NUM < 110000
2467                 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast->name);
2468 #else
2469                 CDEBUG(NULL, ast, "Unable to handle DTMF tone '%c' for '%s'\n", digit, ast_channel_name(ast));
2470 #endif
2471         }
2472 }
2473
2474 #ifdef LCR_FOR_ASTERISK
2475 static int lcr_digit_begin(struct ast_channel *ast, char digit)
2476 #endif
2477 #ifdef LCR_FOR_CALLWEAVER
2478 static int lcr_digit(struct ast_channel *ast, char digit)
2479 #endif
2480 {
2481         struct chan_call *call;
2482         union parameter newparam;
2483         char buf[]="x";
2484
2485 #ifdef LCR_FOR_CALLWEAVER
2486         int inband_dtmf = 0;
2487 #endif
2488
2489         /* only pass IA5 number space */
2490         if (digit > 126 || digit < 32)
2491                 return 0;
2492
2493         ast_mutex_lock(&chan_lock);
2494 #if ASTERISK_VERSION_NUM < 110000
2495         call = ast->tech_pvt;
2496 #else
2497         call = ast_channel_tech_pvt(ast);
2498 #endif
2499         if (!call) {
2500                 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
2501                 ast_mutex_unlock(&chan_lock);
2502                 return -1;
2503         }
2504
2505         CDEBUG(call, ast, "Received digit '%c' from Asterisk.\n", digit);
2506
2507         /* send information or queue them */
2508         if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING) {
2509                 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
2510                 memset(&newparam, 0, sizeof(union parameter));
2511                 if (call->keypad) {
2512                         newparam.information.keypad[0] = digit;
2513                         newparam.information.keypad[1] = '\0';
2514                 } else {
2515                         newparam.information.id[0] = digit;
2516                         newparam.information.id[1] = '\0';
2517                 }
2518                 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
2519         } else
2520         if (!call->ref
2521          && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP)) {
2522                 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
2523                 *buf = digit;
2524                 strncat(call->dialque, buf, strlen(call->dialque)-1);
2525         }
2526
2527         ast_mutex_unlock(&chan_lock);
2528
2529 #ifdef LCR_FOR_ASTERISK
2530         return 0;
2531 }
2532
2533 static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2534 {
2535         int inband_dtmf = 0;
2536         struct chan_call *call;
2537 #endif
2538
2539         ast_mutex_lock(&chan_lock);
2540
2541 #if ASTERISK_VERSION_NUM < 110000
2542         call = ast->tech_pvt;
2543 #else
2544         call = ast_channel_tech_pvt(ast);
2545 #endif
2546
2547         if (!call) {
2548                 CERROR(NULL, ast,
2549                         "Received digit from Asterisk, "
2550                         "but no call instance exists.\n");
2551                 ast_mutex_unlock(&chan_lock);
2552                 return -1;
2553         }
2554
2555         CDEBUG(call, ast, "DIGIT END '%c' from Asterisk.\n", digit);
2556
2557         if (call->state == CHAN_LCR_STATE_CONNECT && call->inband_dtmf) {
2558                 inband_dtmf = 1;
2559         }
2560
2561         ast_mutex_unlock(&chan_lock);
2562
2563         if (inband_dtmf) {
2564                 CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
2565                 send_digit_to_chan(ast, digit);
2566         }
2567
2568         return 0;
2569 }
2570
2571 static int lcr_answer(struct ast_channel *ast)
2572 {
2573         union parameter newparam;
2574         struct chan_call *call;
2575
2576         ast_mutex_lock(&chan_lock);
2577 #if ASTERISK_VERSION_NUM < 110000
2578         call = ast->tech_pvt;
2579 #else
2580         call = ast_channel_tech_pvt(ast);
2581 #endif
2582         if (!call) {
2583                 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
2584                 ast_mutex_unlock(&chan_lock);
2585                 return -1;
2586         }
2587
2588         CDEBUG(call, ast, "Received answer from Asterisk (maybe during lcr_bridge).\n");
2589
2590         /* copy connectinfo, if bridged */
2591         if (call->bridge_call)
2592                 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
2593         /* send connect message to lcr */
2594         if (call->state != CHAN_LCR_STATE_CONNECT) {
2595                 memset(&newparam, 0, sizeof(union parameter));
2596                 memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
2597                 send_message(MESSAGE_CONNECT, call->ref, &newparam);
2598                 call->state = CHAN_LCR_STATE_CONNECT;
2599         }
2600         /* change state */
2601         /* enable keypad */
2602 //      memset(&newparam, 0, sizeof(union parameter));
2603 //      send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
2604
2605         ast_mutex_unlock(&chan_lock);
2606         return 0;
2607 }
2608
2609 static int lcr_hangup(struct ast_channel *ast)
2610 {
2611         struct chan_call *call;
2612         pthread_t tid = pthread_self();
2613
2614         if (!pthread_equal(tid, chan_tid)) {
2615                 ast_mutex_lock(&chan_lock);
2616         }
2617 #if ASTERISK_VERSION_NUM < 110000
2618         call = ast->tech_pvt;
2619 #else
2620         call = ast_channel_tech_pvt(ast);
2621 #endif
2622         if (!call) {
2623                 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
2624                 if (!pthread_equal(tid, chan_tid)) {
2625                         ast_mutex_unlock(&chan_lock);
2626                 }
2627                 return -1;
2628         }
2629
2630         if (!pthread_equal(tid, chan_tid))
2631                 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
2632         else
2633                 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
2634
2635         /* disconnect asterisk, maybe not required */
2636 #if ASTERISK_VERSION_NUM < 110000
2637         ast->tech_pvt = NULL;
2638         ast->fds[0] = -1;
2639 #else
2640         ast_channel_tech_pvt_set(ast, NULL);
2641         ast_channel_set_fd(ast, 0, -1);
2642 #endif
2643         if (call->ref) {
2644                 /* release */
2645                 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
2646 #if ASTERISK_VERSION_NUM < 110000
2647                 if (ast->hangupcause > 0)
2648                         send_release(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
2649 #else
2650                 if (ast_channel_hangupcause(ast) > 0)
2651                         send_release(call, ast_channel_hangupcause(ast), LOCATION_PRIVATE_LOCAL);
2652 #endif
2653                 else
2654                         send_release(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
2655                 /* remove call */
2656                 free_call(call);
2657                 if (!pthread_equal(tid, chan_tid)) {
2658                         ast_mutex_unlock(&chan_lock);
2659                 }
2660                 return 0;
2661         } else {
2662                 /* ref is not set, due to prepare setup or release */
2663                 if (call->state == CHAN_LCR_STATE_RELEASE) {
2664                         /* we get the response to our release */
2665                         CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
2666                         free_call(call);
2667                 } else {
2668                         /* during prepare, we change to release state */
2669                         CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
2670                         call->state = CHAN_LCR_STATE_RELEASE;
2671                         call->ast = NULL;
2672                 }
2673         }
2674         if (!pthread_equal(tid, chan_tid)) {
2675                 ast_mutex_unlock(&chan_lock);
2676         }
2677         return 0;
2678 }
2679
2680 static int lcr_write(struct ast_channel *ast, struct ast_frame *fr)
2681 {
2682         union parameter newparam;
2683         struct chan_call *call;
2684         struct ast_frame * f = fr;
2685         unsigned char *p, *q;
2686         int len, l;
2687
2688 #if ASTERISK_VERSION_NUM < 100000
2689 #ifdef AST_1_8_OR_HIGHER
2690         if (!f->subclass.codec)
2691 #else
2692         if (!f->subclass)
2693 #endif
2694                 CDEBUG(NULL, ast, "No subclass\n");
2695 #endif
2696 #ifdef AST_1_8_OR_HIGHER
2697 #if ASTERISK_VERSION_NUM < 100000
2698 #if ASTERISK_VERSION_NUM < 110000
2699         if (!(f->subclass.codec & ast->nativeformats)) {
2700 #else
2701         if (!(f->subclass.codec & ast_channel_nativeformats(ast))) {
2702 #endif
2703 #else
2704 #if ASTERISK_VERSION_NUM < 110000
2705         if (!ast_format_cap_iscompatible(ast->nativeformats, &f->subclass.format)) {
2706 #else
2707         if (!ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format)) {
2708 #endif
2709 #endif
2710 #else
2711 #if ASTERISK_VERSION_NUM < 110000
2712         if (!(f->subclass & ast->nativeformats)) {
2713 #else
2714         if (!(f->subclass & ast_channel_nativeformats(ast))) {
2715 #endif
2716 #endif
2717                 CDEBUG(NULL, ast,
2718                                "Unexpected format. "
2719                        "Activating emergency conversion...\n");
2720
2721 #ifdef AST_1_8_OR_HIGHER
2722 #if ASTERISK_VERSION_NUM < 100000
2723                 ast_set_write_format(ast, f->subclass.codec);
2724 #else
2725                 ast_set_write_format(ast, &f->subclass.format);
2726 #endif
2727 #else
2728                 ast_set_write_format(ast, f->subclass);
2729 #endif
2730 #if ASTERISK_VERSION_NUM < 110000
2731                 f = (ast->writetrans) ? ast_translate(
2732                         ast->writetrans, fr, 0) : fr;
2733 #else
2734                 f = (ast_channel_writetrans(ast)) ? ast_translate(
2735                         ast_channel_writetrans(ast), fr, 0) : fr;
2736 #endif
2737         }
2738
2739         ast_mutex_lock(&chan_lock);
2740 #if ASTERISK_VERSION_NUM < 110000
2741         call = ast->tech_pvt;
2742 #else
2743         call = ast_channel_tech_pvt(ast);
2744 #endif
2745         if (!call) {
2746                 ast_mutex_unlock(&chan_lock);
2747                 if (f != fr) {
2748                         ast_frfree(f);
2749                 }
2750                 return -1;
2751         }
2752         len = f->samples;
2753         p = *((unsigned char **)&(f->data));
2754         q = newparam.traffic.data;
2755         memset(&newparam, 0, sizeof(union parameter));
2756         while (len) {
2757                 l = (len > sizeof(newparam.traffic.data)) ? sizeof(newparam.traffic.data) : len;
2758                 newparam.traffic.len = l;
2759                 len -= l;
2760                 for (; l; l--)
2761                         *q++ = flip_bits[*p++];
2762                 send_message(MESSAGE_TRAFFIC, call->ref, &newparam);
2763         }
2764         ast_mutex_unlock(&chan_lock);
2765         if (f != fr) {
2766                 ast_frfree(f);
2767         }
2768         return 0;
2769 }
2770
2771
2772 static struct ast_frame *lcr_read(struct ast_channel *ast)
2773 {
2774         struct chan_call *call;
2775         int len = 0;
2776
2777         ast_mutex_lock(&chan_lock);
2778 #if ASTERISK_VERSION_NUM < 110000
2779         call = ast->tech_pvt;
2780 #else
2781         call = ast_channel_tech_pvt(ast);
2782 #endif
2783         if (!call) {
2784                 ast_mutex_unlock(&chan_lock);
2785                 return NULL;
2786         }
2787         if (call->pipe[0] > -1) {
2788                 if (call->rebuffer && !call->hdlc) {
2789                         /* Make sure we have a complete 20ms (160byte) frame */
2790                         len=read(call->pipe[0],call->read_buff + call->framepos, 160 - call->framepos);
2791                         if (len > 0) {
2792                                 call->framepos += len;
2793                         }
2794                 } else {
2795                         len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
2796                 }
2797                 if (len < 0 && errno == EAGAIN) {
2798                         ast_mutex_unlock(&chan_lock);
2799
2800                         #ifdef LCR_FOR_ASTERISK
2801                         return &ast_null_frame;
2802                         #endif
2803
2804                         #ifdef LCR_FOR_CALLWEAVER
2805                         return &nullframe;
2806                         #endif
2807
2808                 }
2809                 if (len <= 0) {
2810                         close(call->pipe[0]);
2811                         call->pipe[0] = -1;
2812                         global_change = 1;
2813                         ast_mutex_unlock(&chan_lock);
2814                         return NULL;
2815                 } else if (call->rebuffer && call->framepos < 160) {
2816                         /* Not a complete frame, so we send a null-frame */
2817                         ast_mutex_unlock(&chan_lock);
2818                         return &ast_null_frame;
2819                 }
2820         }
2821
2822         call->read_fr.frametype = AST_FRAME_VOICE;
2823 #ifdef AST_1_8_OR_HIGHER
2824 #if ASTERISK_VERSION_NUM < 100000
2825 #if ASTERISK_VERSION_NUM < 110000
2826         call->read_fr.subclass.codec = ast->nativeformats;
2827 #else
2828         call->read_fr.subclass.codec = ast_channel_nativeformats(ast);
2829 #endif
2830 #else
2831 #if ASTERISK_VERSION_NUM < 110000
2832         ast_best_codec(ast->nativeformats, &call->read_fr.subclass.format);
2833 #else
2834         ast_best_codec(ast_channel_nativeformats(ast), &call->read_fr.subclass.format);
2835 #endif
2836         call->read_fr.subclass.integer = call->read_fr.subclass.format.id;
2837 #endif
2838 #else
2839 #if ASTERISK_VERSION_NUM < 110000
2840         call->read_fr.subclass = ast->nativeformats;
2841 #else
2842         call->read_fr.subclass = ast_channel_nativeformats(ast);
2843 #endif
2844 #endif
2845         if (call->rebuffer) {
2846                 call->read_fr.datalen = call->framepos;
2847                 call->read_fr.samples = call->framepos;
2848                 call->framepos = 0;
2849         } else {
2850                 call->read_fr.datalen = len;
2851                 call->read_fr.samples = len;
2852         }
2853         call->read_fr.delivery = ast_tv(0,0);
2854         *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
2855         ast_mutex_unlock(&chan_lock);
2856
2857         return &call->read_fr;
2858 }
2859
2860 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
2861 {
2862         union parameter newparam;
2863         int res = 0;
2864         struct chan_call *call;
2865         const struct ast_tone_zone_sound *ts = NULL;
2866
2867         ast_mutex_lock(&chan_lock);
2868 #if ASTERISK_VERSION_NUM < 110000
2869         call = ast->tech_pvt;
2870 #else
2871         call = ast_channel_tech_pvt(ast);
2872 #endif
2873         if (!call) {
2874                 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
2875                 ast_mutex_unlock(&chan_lock);
2876                 return -1;
2877         }
2878
2879         switch (cond) {
2880                 case AST_CONTROL_BUSY:
2881                         CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
2882                         ast_setstate(ast, AST_STATE_BUSY);
2883                         if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2884                                 /* send message to lcr */
2885                                 memset(&newparam, 0, sizeof(union parameter));
2886                                 newparam.disconnectinfo.cause = 17;
2887                                 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2888                                 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2889                                 /* change state */
2890                                 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2891                         } else {
2892                                 CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
2893 #if ASTERISK_VERSION_NUM < 110000
2894                                 ts = ast_get_indication_tone(ast->zone, "busy");
2895 #else
2896                                 ts = ast_get_indication_tone(ast_channel_zone(ast), "busy");
2897 #endif
2898                         }
2899                         break;
2900                 case AST_CONTROL_CONGESTION:
2901 #if ASTERISK_VERSION_NUM < 110000
2902                         CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
2903 #else
2904                         CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast_channel_hangupcause(ast));
2905 #endif
2906                         if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
2907                                 /* send message to lcr */
2908                                 memset(&newparam, 0, sizeof(union parameter));
2909 #if ASTERISK_VERSION_NUM < 110000
2910                                 newparam.disconnectinfo.cause = ast->hangupcause;
2911 #else
2912                                 newparam.disconnectinfo.cause = ast_channel_hangupcause(ast);
2913 #endif
2914                                 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2915                                 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
2916                                 /* change state */
2917                                 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
2918                         } else {
2919                                 CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
2920 #if ASTERISK_VERSION_NUM < 110000
2921                                 ts = ast_get_indication_tone(ast->zone, "congestion");
2922 #else
2923                                 ts = ast_get_indication_tone(ast_channel_zone(ast), "congestion");
2924 #endif
2925                         }
2926                         break;
2927                 case AST_CONTROL_PROCEEDING:
2928                         CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
2929                         if (call->state == CHAN_LCR_STATE_IN_SETUP
2930                          || call->state == CHAN_LCR_STATE_IN_DIALING) {
2931                                 /* send message to lcr */
2932                                 memset(&newparam, 0, sizeof(union parameter));
2933                                 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
2934                                 /* change state */
2935                                 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
2936                         }
2937                         break;
2938                 case AST_CONTROL_RINGING:
2939                         CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
2940                         ast_setstate(ast, AST_STATE_RING);
2941                         if (call->state == CHAN_LCR_STATE_IN_SETUP
2942                          || call->state == CHAN_LCR_STATE_IN_DIALING
2943                          || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
2944                                 /* send message to lcr */
2945                                 memset(&newparam, 0, sizeof(union parameter));
2946                                 send_message(MESSAGE_ALERTING, call->ref, &newparam);
2947                                 /* change state */
2948                                 call->state = CHAN_LCR_STATE_IN_ALERTING;
2949                         } else {
2950                                 CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
2951 #if ASTERISK_VERSION_NUM < 110000
2952                                 ts = ast_get_indication_tone(ast->zone, "ring");
2953 #else
2954                                 ts = ast_get_indication_tone(ast_channel_zone(ast), "ring");
2955 #endif
2956                         }
2957                         break;
2958                 case AST_CONTROL_PROGRESS:
2959                         CDEBUG(call, ast, "Received indicate AST_CONTROL_PROGRESS from Asterisk.\n");
2960                         /* request bchannel */
2961                         CDEBUG(call, ast, "Requesting audio path.\n");
2962                         memset(&newparam, 0, sizeof(union parameter));
2963                         send_message(MESSAGE_AUDIOPATH, call->ref, &newparam);
2964                         break;
2965                 case -1:
2966                         CDEBUG(call, ast, "Received indicate -1.\n");
2967                         ast_playtones_stop(ast);
2968                         res = -1;
2969                         break;
2970
2971                 case AST_CONTROL_VIDUPDATE:
2972                         CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
2973                         res = -1;
2974                         break;
2975                 case AST_CONTROL_HOLD:
2976                         CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
2977                         /* send message to lcr */
2978                         memset(&newparam, 0, sizeof(union parameter));
2979                         newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
2980                         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
2981
2982                         /*start music onhold*/
2983                         #ifdef LCR_FOR_ASTERISK
2984                         #if ASTERISK_VERSION_NUM <110000
2985                         ast_moh_start(ast,data,ast->musicclass);
2986                         #else
2987                         ast_moh_start(ast,data,ast_channel_musicclass(ast));
2988                         #endif
2989                         #endif
2990
2991                         #ifdef LCR_FOR_CALLWEAVER
2992                         ast_moh_start(ast, NULL);
2993                         #endif
2994
2995                         call->on_hold = 1;
2996                         break;
2997                 case AST_CONTROL_UNHOLD:
2998                         CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
2999                         /* send message to lcr */
3000                         memset(&newparam, 0, sizeof(union parameter));
3001                         newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3002                         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3003
3004                         /*stop moh*/
3005                         ast_moh_stop(ast);
3006                         call->on_hold = 0;
3007                         break;
3008 #ifdef AST_CONTROL_SRCUPDATE
3009                 case AST_CONTROL_SRCUPDATE:
3010 #else
3011                 case 20:
3012 #endif
3013                         CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
3014                         break;
3015                 default:
3016                         CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
3017                         res = -1;
3018                         break;
3019         }
3020
3021         if (ts && ts->data[0]) {
3022                 ast_playtones_start(ast, 0, ts->data, 1);
3023         }
3024
3025         /* return */
3026         ast_mutex_unlock(&chan_lock);
3027         return res;
3028 }
3029
3030 /*
3031  * fixup asterisk
3032  */
3033 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
3034 {
3035         struct chan_call *call;
3036
3037         if (!ast) {
3038                 return -1;
3039         }
3040
3041         ast_mutex_lock(&chan_lock);
3042 #if ASTERISK_VERSION_NUM < 110000
3043         call = ast->tech_pvt;
3044 #else
3045         call = ast_channel_tech_pvt(ast);
3046 #endif
3047         if (!call) {
3048                 CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
3049                 ast_mutex_unlock(&chan_lock);
3050                 return -1;
3051         }
3052
3053         CDEBUG(call, ast, "Received fixup from Asterisk.\n");
3054         call->ast = ast;
3055         ast_mutex_unlock(&chan_lock);
3056         return 0;
3057 }
3058
3059 /*
3060  * send_text asterisk
3061  */
3062 static int lcr_send_text(struct ast_channel *ast, const char *text)
3063 {
3064         struct chan_call *call;
3065         union parameter newparam;
3066
3067         ast_mutex_lock(&chan_lock);
3068 #if ASTERISK_VERSION_NUM < 110000
3069         call = ast->tech_pvt;
3070 #else
3071         call = ast_channel_tech_pvt(ast);
3072 #endif
3073         if (!call) {
3074                 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
3075                 ast_mutex_unlock(&chan_lock);
3076                 return -1;
3077         }
3078
3079         CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
3080         memset(&newparam, 0, sizeof(union parameter));
3081         strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
3082         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
3083         ast_mutex_unlock(&chan_lock);
3084         return 0;
3085 }
3086
3087 /*
3088  * bridge process
3089  */
3090 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
3091                                   struct ast_channel *ast2, int flags,
3092                                   struct ast_frame **fo,
3093                                   struct ast_channel **rc, int timeoutms)
3094
3095 {
3096         struct chan_call        *call1, *call2;
3097         struct ast_channel      *carr[2], *who;
3098         int                     to;
3099         struct ast_frame        *f;
3100         int                     bridge_id;
3101
3102         CDEBUG(NULL, NULL, "Received bridging request from Asterisk.\n");
3103
3104         carr[0] = ast1;
3105         carr[1] = ast2;
3106
3107         /* join via dsp (if the channels are currently open) */
3108         ast_mutex_lock(&chan_lock);
3109 #if ASTERISK_VERSION_NUM < 110000
3110         call1 = ast1->tech_pvt;
3111         call2 = ast2->tech_pvt;
3112 #else
3113         call1 = ast_channel_tech_pvt(ast1);
3114         call2 = ast_channel_tech_pvt(ast2);
3115 #endif
3116         if (!call1 || !call2) {
3117                 CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
3118                 ast_mutex_unlock(&chan_lock);
3119                 return AST_BRIDGE_COMPLETE;
3120         }
3121
3122         /* join, if both call instances uses dsp
3123            ignore the case of fax detection here it may be benificial for ISDN fax machines or pass through.
3124         */
3125         if (!call1->nodsp && !call2->nodsp) {
3126                 CDEBUG(NULL, NULL, "Both calls use DSP, bridging via DSP.\n");
3127
3128                 /* get bridge id and join */
3129                 bridge_id = new_bridge_id();
3130
3131 #if 0
3132                 call1->bridge_id = bridge_id;
3133                 if (call1->bchannel)
3134                         bchannel_join(call1->bchannel, bridge_id);
3135
3136                 call2->bridge_id = bridge_id;
3137                 if (call2->bchannel)
3138                         bchannel_join(call2->bchannel, bridge_id);
3139 #else
3140                 printf("FIXME");
3141                 exit(0);
3142 #endif
3143
3144         } else
3145         if (call1->nodsp && call2->nodsp)
3146                 CDEBUG(NULL, NULL, "Both calls use no DSP, bridging in channel driver.\n");
3147         else
3148                 CDEBUG(NULL, NULL, "One call uses no DSP, bridging in channel driver.\n");
3149         call1->bridge_call = call2;
3150         call2->bridge_call = call1;
3151
3152         if (call1->state == CHAN_LCR_STATE_IN_SETUP
3153          || call1->state == CHAN_LCR_STATE_IN_DIALING
3154          || call1->state == CHAN_LCR_STATE_IN_PROCEEDING
3155          || call1->state == CHAN_LCR_STATE_IN_ALERTING) {
3156                 CDEBUG(call1, ast1, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3157                 lcr_answer(ast1);
3158         }
3159         if (call2->state == CHAN_LCR_STATE_IN_SETUP
3160          || call2->state == CHAN_LCR_STATE_IN_DIALING
3161          || call2->state == CHAN_LCR_STATE_IN_PROCEEDING
3162          || call2->state == CHAN_LCR_STATE_IN_ALERTING) {
3163                 CDEBUG(call2, ast2, "Bridge established before lcr_answer, so we call it ourself: Calling lcr_answer...\n");
3164                 lcr_answer(ast2);
3165         }
3166
3167         /* sometimes SIP phones forget to send RETRIEVE before TRANSFER
3168            so let's do it for them. Hmpf.
3169         */
3170
3171         if (call1->on_hold) {
3172                 union parameter newparam;
3173
3174                 memset(&newparam, 0, sizeof(union parameter));
3175                 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3176                 send_message(MESSAGE_NOTIFY, call1->ref, &newparam);
3177
3178                 call1->on_hold = 0;
3179         }
3180
3181         if (call2->on_hold) {
3182                 union parameter newparam;
3183
3184                 memset(&newparam, 0, sizeof(union parameter));
3185                 newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
3186                 send_message(MESSAGE_NOTIFY, call2->ref, &newparam);
3187
3188                 call2->on_hold = 0;
3189         }
3190
3191         ast_mutex_unlock(&chan_lock);
3192
3193         while(1) {
3194                 to = -1;
3195                 who = ast_waitfor_n(carr, 2, &to);
3196
3197                 if (!who) {
3198                         CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
3199                         break;
3200                 }
3201                 f = ast_read(who);
3202
3203                 if (!f || f->frametype == AST_FRAME_CONTROL) {
3204                         if (!f)
3205                                 CDEBUG(NULL, NULL, "Got hangup.\n");
3206                         else
3207                                 CDEBUG(NULL, NULL, "Got CONTROL.\n");
3208                         /* got hangup .. */
3209                         *fo=f;
3210                         *rc=who;
3211                         break;
3212                 }
3213
3214                 if ( f->frametype == AST_FRAME_DTMF ) {
3215                         CDEBUG(NULL, NULL, "Got DTMF.\n");
3216                         *fo=f;
3217                         *rc=who;
3218                         break;
3219                 }
3220
3221
3222                 if (who == ast1) {
3223                         ast_write(ast2,f);
3224                 }
3225                 else {
3226                         ast_write(ast1,f);
3227                 }
3228
3229         }
3230
3231         CDEBUG(NULL, NULL, "Releasing bridge.\n");
3232
3233         /* split channels */
3234         ast_mutex_lock(&chan_lock);
3235 #if ASTERISK_VERSION_NUM < 110000
3236         call1 = ast1->tech_pvt;
3237         call2 = ast2->tech_pvt;
3238 #else
3239         call1 = ast_channel_tech_pvt(ast1);
3240         call2 = ast_channel_tech_pvt(ast2);
3241 #endif
3242         if (call1 && call1->bridge_id) {
3243                 call1->bridge_id = 0;
3244                 if (call1->bridge_call)
3245                         call1->bridge_call->bridge_call = NULL;
3246         }
3247         if (call2 && call1->bridge_id) {
3248                 call2->bridge_id = 0;
3249                 if (call2->bridge_call)
3250                         call2->bridge_call->bridge_call = NULL;
3251         }
3252         call1->bridge_call = NULL;
3253         call2->bridge_call = NULL;
3254
3255         ast_mutex_unlock(&chan_lock);
3256         return AST_BRIDGE_COMPLETE;
3257 }
3258 static struct ast_channel_tech lcr_tech = {
3259         .type= lcr_type,
3260         .description = "Channel driver for connecting to Linux-Call-Router",
3261         #if ASTERISK_VERSION_NUM < 100000
3262         .capabilities = AST_FORMAT_ALAW,
3263         #endif
3264         .requester = lcr_request,
3265
3266         #ifdef LCR_FOR_ASTERISK
3267         .send_digit_begin = lcr_digit_begin,
3268         .send_digit_end = lcr_digit_end,
3269         #endif
3270
3271         #ifdef LCR_FOR_CALLWEAVER
3272         .send_digit = lcr_digit,
3273         #endif
3274
3275         .call = lcr_call,
3276         .bridge = lcr_bridge,
3277         .hangup = lcr_hangup,
3278         .answer = lcr_answer,
3279         .read = lcr_read,
3280         .write = lcr_write,
3281         .indicate = lcr_indicate,
3282         .fixup = lcr_fixup,
3283         .send_text = lcr_send_text,
3284         .properties = 0
3285 };
3286
3287
3288 /*
3289  * cli
3290  */
3291 #if 0
3292 static int lcr_show_lcr (int fd, int argc, char *argv[])
3293 {
3294         return 0;
3295 }
3296
3297 static int lcr_show_calls (int fd, int argc, char *argv[])
3298 {
3299         return 0;
3300 }
3301
3302 static int lcr_reload_routing (int fd, int argc, char *argv[])
3303 {
3304         return 0;
3305 }
3306
3307 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
3308 {
3309         return 0;
3310 }
3311
3312 static int lcr_port_block (int fd, int argc, char *argv[])
3313 {
3314         return 0;
3315 }
3316
3317 static int lcr_port_unblock (int fd, int argc, char *argv[])
3318 {
3319         return 0;
3320 }
3321
3322 static int lcr_port_unload (int fd, int argc, char *argv[])
3323 {
3324         return 0;
3325 }
3326
3327 static struct ast_cli_entry cli_show_lcr =
3328 { {"lcr", "show", "lcr", NULL},
3329  lcr_show_lcr,
3330  "Shows current states of LCR core",
3331  "Usage: lcr show lcr\n",
3332 };
3333
3334 static struct ast_cli_entry cli_show_calls =
3335 { {"lcr", "show", "calls", NULL},
3336  lcr_show_calls,
3337  "Shows current calls made by LCR and Asterisk",
3338  "Usage: lcr show calls\n",
3339 };
3340
3341 static struct ast_cli_entry cli_reload_routing =
3342 { {"lcr", "reload", "routing", NULL},
3343  lcr_reload_routing,
3344  "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
3345  "Usage: lcr reload routing\n",
3346 };
3347
3348 static struct ast_cli_entry cli_reload_interfaces =
3349 { {"lcr", "reload", "interfaces", NULL},
3350  lcr_reload_interfaces,
3351  "Reloads interfaces conf of LCR",
3352  "Usage: lcr reload interfaces\n",
3353 };
3354
3355 static struct ast_cli_entry cli_port_block =
3356 { {"lcr", "port", "block", NULL},
3357  lcr_port_block,
3358  "Blocks LCR port for further calls",
3359  "Usage: lcr port block \"<port>\"\n",
3360 };
3361
3362 static struct ast_cli_entry cli_port_unblock =
3363 { {"lcr", "port", "unblock", NULL},
3364  lcr_port_unblock,
3365  "Unblocks or loads LCR port, port is opened my mISDN",
3366  "Usage: lcr port unblock \"<port>\"\n",
3367 };
3368
3369 static struct ast_cli_entry cli_port_unload =
3370 { {"lcr", "port", "unload", NULL},
3371  lcr_port_unload,
3372  "Unloads LCR port, port is closes by mISDN",
3373  "Usage: lcr port unload \"<port>\"\n",
3374 };
3375 #endif
3376
3377
3378 #ifdef LCR_FOR_ASTERISK
3379 #ifdef AST_1_8_OR_HIGHER
3380 static int lcr_config_exec(struct ast_channel *ast, const char *data)
3381 #else
3382 static int lcr_config_exec(struct ast_channel *ast, void *data)
3383 #endif
3384 #endif
3385
3386 #ifdef LCR_FOR_CALLWEAVER
3387 static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
3388 #endif
3389 {
3390         struct chan_call *call;
3391
3392         ast_mutex_lock(&chan_lock);
3393
3394         #ifdef LCR_FOR_ASTERISK
3395         CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", (char *)data);
3396         #endif
3397
3398         #ifdef LCR_FOR_CALLWEAVER
3399         CDEBUG(NULL, ast, "Received lcr_config (data=%s)\n", argv[0]);
3400         #endif
3401
3402         /* find channel */
3403         call = call_first;
3404         while(call) {
3405                 if (call->ast == ast)
3406                         break;
3407                 call = call->next;
3408         }
3409         if (call)
3410
3411                 #ifdef LCR_FOR_ASTERISK
3412                 apply_opt(call, (char *)data);
3413                 #endif
3414
3415                 #ifdef LCR_FOR_CALLWEAVER
3416                 apply_opt(call, (char *)argv[0]);
3417                 #endif
3418
3419         else
3420                 CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
3421
3422         ast_mutex_unlock(&chan_lock);
3423         return 0;
3424 }
3425
3426 /*
3427  * module loading and destruction
3428  */
3429 int load_module(void)
3430 {
3431         u_short i;
3432         char options_error[256];
3433
3434         for (i = 0; i < 256; i++) {
3435                 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
3436                              | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
3437         }
3438
3439         if (read_options(options_error) == 0) {
3440                 CERROR(NULL, NULL, "%s", options_error);
3441
3442                 #ifdef LCR_FOR_ASTERISK
3443                 return AST_MODULE_LOAD_DECLINE;
3444                 #endif
3445
3446                 #ifdef LCR_FOR_CALLWEAVER
3447                 return 0;
3448                 #endif
3449
3450         }
3451
3452         ast_mutex_init(&chan_lock);
3453         ast_mutex_init(&log_lock);
3454
3455         #if ASTERISK_VERSION_NUM < 100000
3456         lcr_tech.capabilities = (options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW;
3457         #else
3458         struct ast_format tmp;
3459         ast_format_set(&tmp ,(options.law=='a')?AST_FORMAT_ALAW:AST_FORMAT_ULAW , 0);
3460         if (!(lcr_tech.capabilities = ast_format_cap_alloc())) {
3461                 return AST_MODULE_LOAD_DECLINE;
3462         }
3463         ast_format_cap_add(lcr_tech.capabilities, &tmp);
3464         #endif
3465         if (ast_channel_register(&lcr_tech)) {
3466                 CERROR(NULL, NULL, "Unable to register channel class\n");
3467                 close_socket();
3468
3469                 #ifdef LCR_FOR_ASTERISK
3470                 return AST_MODULE_LOAD_DECLINE;
3471                 #endif
3472
3473                 #ifdef LCR_FOR_CALLWEAVER
3474                 return 0;
3475                 #endif
3476         }
3477
3478         ast_register_application("lcr_config", lcr_config_exec, "lcr_config",
3479
3480                                  #ifdef LCR_FOR_ASTERISK
3481                                  "lcr_config(<opt><optarg>:<opt>:...)\n"
3482                                  #endif
3483
3484                                  #ifdef LCR_FOR_CALLWEAVER
3485                                  "lcr_config(<opt><optarg>:<opt>:...)\n",
3486                                  #endif
3487
3488                                  "Sets LCR opts. and optargs\n"
3489                                  "\n"
3490                                  "The available options are:\n"
3491                                  "    d - Send display text on called phone, text is the optarg.\n"
3492                                  "    n - Don't detect dtmf tones on called channel.\n"
3493                                  "    h - Force data call (HDLC).\n"
3494                                  "    t - Disable mISDN_dsp features (required for fax application).\n"
3495                                  "    q - Add queue to make fax stream seamless (required for fax app).\n"
3496                                  "        Use queue size in miliseconds for optarg. (try 250)\n"
3497                                  "    f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
3498                                  "        Use time to detect for optarg.\n"
3499 #if 0
3500                                  "    c - Make crypted outgoing call, optarg is keyindex.\n"
3501                                  "    e - Perform echo cancelation on this channel.\n"
3502 #endif
3503                                  "        Takes mISDN pipeline option as optarg.\n"
3504                                  "    s - Send Non Inband DTMF as inband.\n"
3505                                  "    r - re-buffer packets (160 bytes). Required for some SIP-phones and fax applications.\n"
3506 #if 0
3507                                  "   vr - rxgain control\n"
3508                                  "   vt - txgain control\n"
3509 #endif
3510                                  "        Volume changes at factor 2 ^ optarg.\n"
3511                                  "    k - use keypad to dial this call.\n"
3512                                  "\n"
3513                                  "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
3514                                  " -> use 16 for fax (3.1k audio)\n"
3515                                  "\n"
3516                                  "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
3517                                  "options: \"n:t:q250\" for seamless audio transmission.\n"
3518                 );
3519
3520
3521 #if 0
3522         ast_cli_register(&cli_show_lcr);
3523         ast_cli_register(&cli_show_calls);
3524         ast_cli_register(&cli_reload_routing);
3525         ast_cli_register(&cli_reload_interfaces);
3526         ast_cli_register(&cli_port_block);
3527         ast_cli_register(&cli_port_unblock);
3528         ast_cli_register(&cli_port_unload);
3529 #endif
3530
3531         if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0)) {
3532                 /* failed to create thread */
3533                 close_socket();
3534                 ast_channel_unregister(&lcr_tech);
3535
3536                 #ifdef LCR_FOR_ASTERISK
3537                 return AST_MODULE_LOAD_DECLINE;
3538                 #endif
3539
3540                 #ifdef LCR_FOR_CALLWEAVER
3541                 return 0;
3542                 #endif
3543
3544         }
3545         return 0;
3546 }
3547
3548 int unload_module(void)
3549 {
3550         /* First, take us out of the channel loop */
3551         CDEBUG(NULL, NULL, "-- Unregistering Linux-Call-Router Channel Driver --\n");
3552
3553         pthread_cancel(chan_tid);
3554
3555         close_socket();
3556
3557         del_timer(&socket_retry);
3558
3559         unregister_fd(&wake_fd);
3560         close(wake_pipe[0]);
3561         close(wake_pipe[1]);
3562
3563 //      ast_mutex_unlock(&chan_lock);
3564
3565         ast_channel_unregister(&lcr_tech);
3566
3567         ast_unregister_application("lcr_config");
3568
3569         if (lcr_sock >= 0) {
3570                 close(lcr_sock);
3571                 lcr_sock = -1;
3572         }
3573
3574 #if ASTERISK_VERSION_NUM >= 100000
3575         lcr_tech.capabilities = ast_format_cap_destroy(lcr_tech.capabilities);
3576 #endif
3577         return 0;
3578 }
3579
3580 int reload_module(void)
3581 {
3582 //      reload_config();
3583         return 0;
3584 }
3585
3586 #ifdef LCR_FOR_ASTERISK
3587 #define AST_MODULE "chan_lcr"
3588 #endif
3589
3590 #ifdef LCR_FOR_CALLWEAVER
3591 int usecount(void)
3592 hae
3593 {
3594         int res;
3595         ast_mutex_lock(&usecnt_lock);
3596         res = usecnt;
3597         ast_mutex_unlock(&usecnt_lock);
3598         return res;
3599 }
3600 #endif
3601
3602 #ifdef LCR_FOR_ASTERISK
3603 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
3604                 .load = load_module,
3605                 .unload = unload_module,
3606                 .reload = reload_module,
3607                );
3608 #endif
3609
3610 #ifdef LCR_FOR_CALLWEAVER
3611 char *description(void)
3612 {
3613         return desc;
3614 }
3615 #endif