LCR is now uses socket based mISDN V2 API
[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 A new chan_call instance is created. The call reference (ref) is given by
25 MESSAGE_NEWREF. The state is CHAN_LCR_STATE_IN_PREPARE.
26 After receiving MESSAGE_SETUP from LCR, the ast_channel instance is created
27 using ast_channel_alloc(1).  The setup information is given to asterisk.
28 The new Asterisk instance pointer (ast) is stored to chan_call structure.
29 The state changes to CHAN_LCR_STATE_IN_SETUP.
30
31
32 Call is initiated by Asterisk:
33
34 If a call is reveiced from Asterisk, a new chan_call instance is created.
35 The new Asterisk instance pointer (ast) is stored to chan_call structure.
36 A MESSASGE_NEWREF is sent to LCR requesting a new call reference (ref).
37 The current call ref is set to 0, the state is CHAN_LCR_STATE_OUT_PREPARE.
38 Further dialing information is queued.
39 After the new callref is received by special MESSAGE_NEWREF reply, new ref
40 is stored in the chan_call structure. 
41 The setup information is sent to LCR using MESSAGE_SETUP.
42 The state changes to CHAN_LCR_STATE_OUT_SETUP.
43
44
45 Call is in process:
46
47 During call process, messages are received and sent.
48 The state changes accordingly.
49 Any message is allowed to be sent to LCR at any time except MESSAGE_RELEASE.
50 If a MESSAGE_OVERLAP is received, further dialing is required.
51 Queued dialing information, if any, is sent to LCR using MESSAGE_DIALING.
52 In this case, the state changes to CHAN_LCR_STATE_OUT_DIALING.
53
54
55 Call is released by LCR:
56
57 A MESSAGE_RELEASE is received with the call reference (ref) to be released.
58 The current ref is set to 0, to indicate released reference.
59 The state changes to CHAN_LCR_STATE_RELEASE.
60 ast_queue_hangup() is called, if asterisk instance (ast) exists, if not,
61 the chan_call instance is destroyed.
62 After lcr_hangup() is called-back by Asterisk, the chan_call instance
63 is destroyed, because the current ref is set to 0 and the state equals
64 CHAN_LCR_STATE_RELEASE.
65 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, see the proceedure
66 "Call is released by Asterisk".
67
68
69 Call is released by Asterisk:
70
71 lcr_hangup() is called-back by Asterisk. If the call reference (ref) is set,
72 a MESSAGE_RELEASE is sent to LCR and the chan_call instance is destroyed.
73 If the ref is 0 and the state is not CHAN_LCR_STATE_RELEASE, the new state is
74 set to CHAN_LCR_STATE_RELEASE.
75 Later, if the MESSAGE_NEWREF reply is received, a MESSAGE_RELEASE is sent to
76 LCR and the chan_call instance is destroyed.
77 If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
78 "Call is released by LCR".
79
80 */
81
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <stdarg.h>
86 #include <errno.h>
87 #include <sys/types.h>
88 #include <time.h>
89 //#include <signal.h>
90 #include <unistd.h>
91 #include <fcntl.h>
92 #include <sys/ioctl.h>
93 #include <sys/socket.h>
94 #include <sys/un.h>
95
96 #include <semaphore.h>
97
98 #include <asterisk/module.h>
99 #include <asterisk/channel.h>
100 #include <asterisk/config.h>
101 #include <asterisk/logger.h>
102 #include <asterisk/pbx.h>
103 #include <asterisk/options.h>
104 #include <asterisk/io.h>
105 #include <asterisk/frame.h>
106 #include <asterisk/translate.h>
107 #include <asterisk/cli.h>
108 #include <asterisk/musiconhold.h>
109 #include <asterisk/dsp.h>
110 #include <asterisk/translate.h>
111 #include <asterisk/file.h>
112 #include <asterisk/callerid.h>
113 #include <asterisk/indications.h>
114 #include <asterisk/app.h>
115 #include <asterisk/features.h>
116 #include <asterisk/sched.h>
117
118 #include "extension.h"
119 #include "message.h"
120 #include "callerid.h"
121 #include "lcrsocket.h"
122 #include "cause.h"
123 #include "bchannel.h"
124 #include "chan_lcr.h"
125
126 CHAN_LCR_STATE // state description structure
127 MESSAGES // message text
128
129 unsigned char flip_bits[256];
130
131 int lcr_debug=1;
132 int mISDN_created=1;
133
134 char lcr_type[]="lcr";
135
136 pthread_t chan_tid;
137 ast_mutex_t chan_lock; /* global lock */
138 ast_mutex_t log_lock; /* logging log */
139 int quit;
140
141 int glob_channel = 0;
142
143 int lcr_sock = -1;
144
145 struct admin_list {
146         struct admin_list *next;
147         struct admin_message msg;
148 } *admin_first = NULL;
149
150 static struct ast_channel_tech lcr_tech;
151
152 /*
153  * logging
154  */
155 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, ...)
156 {
157         char buffer[1024];
158         char call_text[128] = "NULL";
159         char ast_text[128] = "NULL";
160         va_list args;
161
162         ast_mutex_lock(&log_lock);
163
164         va_start(args,fmt);
165         vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
166         buffer[sizeof(buffer)-1]=0;
167         va_end(args);
168
169         if (call)
170                 sprintf(call_text, "%ld", call->ref);
171         if (ast)
172                 strncpy(ast_text, ast->name, sizeof(ast_text)-1);
173         ast_text[sizeof(ast_text)-1] = '\0';
174         
175         ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
176
177         ast_mutex_unlock(&log_lock);
178 }
179
180 /*
181  * channel and call instances
182  */
183 struct chan_call *call_first;
184
185 struct chan_call *find_call_ref(unsigned long ref)
186 {
187         struct chan_call *call = call_first;
188
189         while(call)
190         {
191                 if (call->ref == ref)
192                         break;
193                 call = call->next;
194         }
195         return(call);
196 }
197
198 #if 0
199 struct chan_call *find_call_ast(struct ast_channel *ast)
200 {
201         struct chan_call *call = call_first;
202
203         while(call)
204         {
205                 if (call->ast == ast)
206                         break;
207                 call = call->next;
208         }
209         return(call);
210 }
211
212 struct chan_call *find_call_handle(unsigned long handle)
213 {
214         struct chan_call *call = call_first;
215
216         while(call)
217         {
218                 if (call->bchannel_handle == handle)
219                         break;
220                 call = call->next;
221         }
222         return(call);
223 }
224 #endif
225
226 void free_call(struct chan_call *call)
227 {
228         struct chan_call **temp = &call_first;
229
230         while(*temp)
231         {
232                 if (*temp == call)
233                 {
234                         *temp = (*temp)->next;
235                         if (call->pipe[0] > -1)
236                                 close(call->pipe[0]);
237                         if (call->pipe[1] > -1)
238                                 close(call->pipe[1]);
239                         if (call->bchannel)
240                         {
241                                 if (call->bchannel->call != call)
242                                         CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
243                                 call->bchannel->call = NULL;
244                         }
245                         if (call->bridge_call)
246                         {
247                                 if (call->bridge_call->bridge_call != call)
248                                         CERROR(call, NULL, "Linked call structure has no link to us.\n");
249                                 call->bridge_call->bridge_call = NULL;
250                         }
251                         CDEBUG(call, NULL, "Call instance freed.\n");
252                         free(call);
253                         return;
254                 }
255                 temp = &((*temp)->next);
256         }
257         CERROR(call, NULL, "Call instance not found in list.\n");
258 }
259
260 struct chan_call *alloc_call(void)
261 {
262         struct chan_call **callp = &call_first;
263
264         while(*callp)
265                 callp = &((*callp)->next);
266
267         *callp = (struct chan_call *)calloc(1, sizeof(struct chan_call));
268         if (*callp)
269                 memset(*callp, 0, sizeof(struct chan_call));
270         if (pipe((*callp)->pipe) < 0) {
271                 CERROR(*callp, NULL, "Failed to create pipe.\n");
272                 free_call(*callp);
273                 return(NULL);
274         }
275         CDEBUG(*callp, NULL, "Call instance allocated.\n");
276         return(*callp);
277 }
278
279
280 unsigned short new_bridge_id(void)
281 {
282         struct chan_call *call;
283         unsigned short id = 1;
284
285         /* search for lowest bridge id that is not in use and not 0 */
286         while(id)
287         {
288                 call = call_first;
289                 while(call)
290                 {
291                         if (call->bridge_id == id)
292                                 break;
293                         call = call->next;
294                 }
295                 if (!call)
296                         break;
297                 id++;
298         }
299         CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
300         return(id);
301 }
302
303
304 /*
305  * enque message to LCR
306  */
307 int send_message(int message_type, unsigned long ref, union parameter *param)
308 {
309         struct admin_list *admin, **adminp;
310
311         if (lcr_sock < 0) {
312                 CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
313                 return -1;
314         }
315         CDEBUG(NULL, NULL, "Sending %s to socket.\n", messages_txt[message_type]);
316
317         adminp = &admin_first;
318         while(*adminp)
319                 adminp = &((*adminp)->next);
320         admin = (struct admin_list *)calloc(1, sizeof(struct admin_list));
321         if (!admin) {
322                 CERROR(NULL, NULL, "No memory for message to LCR.\n");
323                 return -1;
324         }
325         *adminp = admin;
326
327         admin->msg.message = ADMIN_MESSAGE;
328         admin->msg.u.msg.type = message_type;
329         admin->msg.u.msg.ref = ref;
330         memcpy(&admin->msg.u.msg.param, param, sizeof(union parameter));
331
332         return(0);
333 }
334
335 /*
336  * send setup info to LCR
337  * this function is called, when asterisk call is received and ref is received
338  */
339 static void send_setup_to_lcr(struct chan_call *call)
340 {
341         union parameter newparam;
342         struct ast_channel *ast = call->ast;
343
344         if (!call->ast || !call->ref)
345                 return;
346
347         CDEBUG(call, call->ast, "Sending setup to LCR.\n");
348
349         /* send setup message to LCR */
350         memset(&newparam, 0, sizeof(union parameter));
351         strncpy(newparam.setup.dialinginfo.id, ast->exten, sizeof(newparam.setup.dialinginfo.id)-1);
352         newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;      
353         newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
354         if (ast->cid.cid_num) if (ast->cid.cid_num[0])
355                 strncpy(newparam.setup.callerinfo.id, ast->cid.cid_num, sizeof(newparam.setup.callerinfo.id)-1);
356         if (ast->cid.cid_name) if (ast->cid.cid_name[0])
357                 strncpy(newparam.setup.callerinfo.name, ast->cid.cid_name, sizeof(newparam.setup.callerinfo.name)-1);
358         if (ast->cid.cid_rdnis) if (ast->cid.cid_rdnis[0])
359         {
360                 strncpy(newparam.setup.redirinfo.id, ast->cid.cid_rdnis, sizeof(newparam.setup.redirinfo.id)-1);
361                 newparam.setup.redirinfo.itype = INFO_ITYPE_CHAN;       
362                 newparam.setup.redirinfo.ntype = INFO_NTYPE_UNKNOWN;    
363         }
364         switch(ast->cid.cid_pres & AST_PRES_RESTRICTION)
365         {
366                 case AST_PRES_ALLOWED:
367                 newparam.setup.callerinfo.present = INFO_PRESENT_ALLOWED;
368                 break;
369                 case AST_PRES_RESTRICTED:
370                 newparam.setup.callerinfo.present = INFO_PRESENT_RESTRICTED;
371                 break;
372                 case AST_PRES_UNAVAILABLE:
373                 newparam.setup.callerinfo.present = INFO_PRESENT_NOTAVAIL;
374                 break;
375                 default:
376                 newparam.setup.callerinfo.present = INFO_PRESENT_NULL;
377         }
378         switch(ast->cid.cid_ton)
379         {
380                 case 4:
381                 newparam.setup.callerinfo.ntype = INFO_NTYPE_SUBSCRIBER;
382                 break;
383                 case 2:
384                 newparam.setup.callerinfo.ntype = INFO_NTYPE_NATIONAL;
385                 break;
386                 case 1:
387                 newparam.setup.callerinfo.ntype = INFO_NTYPE_INTERNATIONAL;
388                 break;
389                 default:
390                 newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
391         }
392         newparam.setup.capainfo.bearer_capa = ast->transfercapability;
393 #ifdef TODO
394         newparam.setup.capainfo.bearer_info1 = alaw 3, ulaw 2;
395 #endif
396         newparam.setup.capainfo.bearer_info1 = 3;
397         newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
398         newparam.setup.capainfo.hlc = INFO_HLC_NONE;
399         newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
400         send_message(MESSAGE_SETUP, call->ref, &newparam);
401
402         /* change to outgoing setup state */
403         call->state = CHAN_LCR_STATE_OUT_SETUP;
404 }
405
406 /*
407  * send dialing info to LCR
408  * this function is called, when setup acknowledge is received and dialing
409  * info is available.
410  */
411 static void send_dialque_to_lcr(struct chan_call *call)
412 {
413         union parameter newparam;
414
415         if (!call->ast || !call->ref || !call->dialque[0])
416                 return;
417         
418         CDEBUG(call, call->ast, "Sending dial queue to LCR. (dialing=%s)\n", call->dialque);
419
420         /* send setup message to LCR */
421         memset(&newparam, 0, sizeof(union parameter));
422         strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
423         call->dialque[0] = '\0';
424         send_message(MESSAGE_INFORMATION, call->ref, &newparam);
425 }
426
427 /*
428  * in case of a bridge, the unsupported message can be forwarded directly
429  * to the remote call.
430  */
431 static void bridge_message_if_bridged(struct chan_call *call, int message_type, union parameter *param)
432 {
433         /* check bridge */
434         if (!call) return;
435         if (!call->bridge_call) return;
436         CDEBUG(call, NULL, "Sending message due briding.\n");
437         send_message(message_type, call->bridge_call->ref, param);
438 }
439
440 /*
441  * send release message to LCR and import bchannel if exported
442  */
443 static void send_release_and_import(struct chan_call *call, int cause, int location)
444 {
445         union parameter newparam;
446
447         /* importing channel */
448         if (call->bchannel && call->bchannel->handle) {
449                 memset(&newparam, 0, sizeof(union parameter));
450                 newparam.bchannel.type = BCHANNEL_RELEASE;
451                 newparam.bchannel.handle = call->bchannel->handle;
452                 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
453         }
454         /* sending release */
455         memset(&newparam, 0, sizeof(union parameter));
456         newparam.disconnectinfo.cause = cause;
457         newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
458         send_message(MESSAGE_RELEASE, call->ref, &newparam);
459 }
460
461 /*
462  * check if extension matches and start asterisk
463  * if it can match, proceed
464  * if not, release
465  */
466 static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
467 {
468         int cause, ret;
469         union parameter newparam;
470
471         CDEBUG(call, ast, "Try to start pbx. (exten=%s context=%s complete=%s)\n", ast->exten, ast->context, complete?"yes":"no");
472         
473         if (complete)
474         {
475                 /* if not match */
476                 if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
477                 {
478                         CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
479                         cause = 1;
480                         goto release;
481                 }
482                 if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
483                 {
484                         CDEBUG(call, ast, "Got 'sending complete', but extension '%s' would match at context '%s', if more digits would be dialed - releasing.\n", ast->exten, ast->context);
485                         cause = 28;
486                         goto release;
487                 }
488                 CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
489                 /* send setup acknowledge to lcr */
490                 memset(&newparam, 0, sizeof(union parameter));
491                 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
492
493                 /* change state */
494                 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
495
496                 goto start;
497         }
498
499         if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
500         {
501                 /* send setup acknowledge to lcr */
502                 if (call->state != CHAN_LCR_STATE_IN_DIALING) {
503                         memset(&newparam, 0, sizeof(union parameter));
504                         send_message(MESSAGE_OVERLAP, call->ref, &newparam);
505                 }
506
507                 /* change state */
508                 call->state = CHAN_LCR_STATE_IN_DIALING;
509
510                 /* if match, start pbx */
511                 if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
512                         CDEBUG(call, ast, "Extensions matches.\n");
513                         goto start;
514                 }
515
516                 /* if can match */
517                 CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
518                 return;
519         }
520
521         /* if not match */
522         cause = 1;
523         release:
524         /* release lcr */
525         CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
526         send_release_and_import(call, cause, LOCATION_PRIVATE_LOCAL);
527         call->ref = 0;
528         /* release asterisk */
529         ast->hangupcause = call->cause;
530         /* change to release state */
531         call->state = CHAN_LCR_STATE_RELEASE;
532         ast_hangup(ast); // call will be destroyed here
533         return;
534         
535         start:
536         /* send setup to asterisk */
537         CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
538         ret = ast_pbx_start(ast);
539         if (ret < 0)
540         {
541                 cause = (ret==-2)?34:27;
542                 goto release;
543         }
544         call->pbx_started = 1;
545         return;
546 }
547
548 /*
549  * incoming setup from LCR
550  */
551 static void lcr_in_setup(struct chan_call *call, int message_type, union parameter *param)
552 {
553         struct ast_channel *ast;
554
555         CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %s, dialing %s)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
556
557         /* create asterisk channel instrance */
558         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
559         if (!ast)
560         {
561                 /* release */
562                 CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
563                 send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
564                 /* remove call */
565                 free_call(call);
566                 return;
567         }
568         /* link together */
569         call->ast = ast;
570         ast->tech_pvt = call;
571         ast->tech = &lcr_tech;
572         ast->fds[0] = call->pipe[0];
573         
574         /* fill setup information */
575         if (param->setup.dialinginfo.id)
576                 strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
577         if (param->setup.context[0])
578                 strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
579         else
580                 strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
581         if (param->setup.callerinfo.id[0])
582                 ast->cid.cid_num = strdup(param->setup.callerinfo.id);
583         if (param->setup.callerinfo.name[0])
584                 ast->cid.cid_name = strdup(param->setup.callerinfo.name);
585 #ifdef TODO
586         if (param->setup.redirinfo.id[0])
587                 ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
588 #endif
589         switch (param->setup.callerinfo.present)
590         {
591                 case INFO_PRESENT_ALLOWED:
592                         ast->cid.cid_pres = AST_PRES_ALLOWED;
593                 break;
594                 case INFO_PRESENT_RESTRICTED:
595                         ast->cid.cid_pres = AST_PRES_RESTRICTED;
596                 break;
597                 default:
598                         ast->cid.cid_pres = AST_PRES_UNAVAILABLE;
599         }
600         switch (param->setup.callerinfo.ntype)
601         {
602                 case INFO_NTYPE_SUBSCRIBER:
603                         ast->cid.cid_ton = 4;
604                 break;
605                 case INFO_NTYPE_NATIONAL:
606                         ast->cid.cid_ton = 2;
607                 break;
608                 case INFO_NTYPE_INTERNATIONAL:
609                         ast->cid.cid_ton = 1;
610                 break;
611                 default:
612                         ast->cid.cid_ton = 0;
613         }
614         ast->transfercapability = param->setup.capainfo.bearer_capa;
615 #ifdef TODO
616         strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1);
617 #else
618         strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, "0", "00"), sizeof(call->oad)-1);
619 #endif
620
621         /* configure channel */
622 #ifdef TODO
623         ast->nativeformats = configfile->lawformat;
624         ast->readformat = ast->rawreadformat = configfile->lawformat;
625         ast->writeformat = ast->rawwriteformat = configfile->lawformat;
626 #else
627         ast->nativeformats = AST_FORMAT_ALAW;
628         ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
629         ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
630 #endif
631         ast->priority = 1;
632         ast->hangupcause = 0;
633
634         /* change state */
635         call->state = CHAN_LCR_STATE_IN_SETUP;
636
637         lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
638 }
639
640 /*
641  * incoming setup acknowledge from LCR
642  */
643 static void lcr_in_overlap(struct chan_call *call, int message_type, union parameter *param)
644 {
645         if (!call->ast) return;
646
647         CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
648
649         /* send pending digits in dialque */
650         if (call->dialque[0])
651                 send_dialque_to_lcr(call);
652         /* change to overlap state */
653         call->state = CHAN_LCR_STATE_OUT_DIALING;
654 }
655
656 /*
657  * incoming proceeding from LCR
658  */
659 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
660 {
661         CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
662
663         /* change state */
664         call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
665         /* send event to asterisk */
666         if (call->ast && call->pbx_started)
667                 ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
668 }
669
670 /*
671  * incoming alerting from LCR
672  */
673 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
674 {
675         CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
676
677         /* change state */
678         call->state = CHAN_LCR_STATE_OUT_ALERTING;
679         /* send event to asterisk */
680         if (call->ast && call->pbx_started)
681                 ast_queue_control(call->ast, AST_CONTROL_RINGING);
682 }
683
684 /*
685  * incoming connect from LCR
686  */
687 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
688 {
689         union parameter newparam;
690
691         CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
692
693         /* change state */
694         call->state = CHAN_LCR_STATE_CONNECT;
695         /* request bchannel */
696         if (!call->bchannel) {
697                 CDEBUG(call, call->ast, "Requesting B-channel.\n");
698                 memset(&newparam, 0, sizeof(union parameter));
699                 newparam.bchannel.type = BCHANNEL_REQUEST;
700                 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
701         }
702         /* copy connectinfo */
703         memcpy(&call->connectinfo, &param->connectinfo, sizeof(struct connect_info));
704         /* send event to asterisk */
705         if (call->ast && call->pbx_started)
706                 ast_queue_control(call->ast, AST_CONTROL_ANSWER);
707 }
708
709 /*
710  * incoming disconnect from LCR
711  */
712 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
713 {
714         struct ast_channel *ast = call->ast;
715
716         CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
717
718         /* change state */
719         call->state = CHAN_LCR_STATE_IN_DISCONNECT;
720         /* save cause */
721         call->cause = param->disconnectinfo.cause;
722         call->location = param->disconnectinfo.location;
723         /* if bridge, forward disconnect and return */
724 #ifdef TODO
725         feature flag
726         if (call->bridge_call)
727         {
728                 CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
729                 bridge_message_if_bridged(call, message_type, param);
730                 return;
731         }
732 #endif
733         /* release lcr with same cause */
734         send_release_and_import(call, call->cause, call->location);
735         call->ref = 0;
736         /* change to release state */
737         call->state = CHAN_LCR_STATE_RELEASE;
738         /* release asterisk */
739         if (ast)
740         {
741                 ast->hangupcause = call->cause;
742                 if (call->pbx_started)
743                         ast_queue_hangup(ast);
744                 else {
745                         ast_hangup(ast); // call will be destroyed here
746                 }
747         }
748 }
749
750 /*
751  * incoming setup acknowledge from LCR
752  */
753 static void lcr_in_release(struct chan_call *call, int message_type, union parameter *param)
754 {
755         struct ast_channel *ast = call->ast;
756
757         CDEBUG(call, call->ast, "Incomming release from LCR, releasing ref. (cause=%d)\n", param->disconnectinfo.cause);
758
759         /* release ref */
760         call->ref = 0;
761         /* change to release state */
762         call->state = CHAN_LCR_STATE_RELEASE;
763         /* copy release info */
764         if (!call->cause)
765         {
766                call->cause = param->disconnectinfo.cause;
767                call->location = param->disconnectinfo.location;
768         }
769         /* if we have an asterisk instance, send hangup, else we are done */
770         if (ast)
771         {
772                 ast->hangupcause = call->cause;
773                 if (call->pbx_started)
774                         ast_queue_hangup(ast);
775                 else {
776                         ast_hangup(ast); // call will be destroyed here
777                 }
778         } else
779         {
780                 free_call(call);
781         }
782         
783 }
784
785 /*
786  * incoming information from LCR
787  */
788 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
789 {
790         struct ast_channel *ast = call->ast;
791         struct ast_frame fr;
792         char *p;
793
794         CDEBUG(call, call->ast, "Incoming information from LCR. (dialing=%s)\n", param->information.id);
795         
796         if (!ast) return;
797
798         /* pbx not started */
799         if (!call->pbx_started)
800         {
801                 CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
802                 strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
803                 lcr_start_pbx(call, ast, param->information.sending_complete);
804                 return;
805         }
806         
807         /* copy digits */
808         p = param->information.id;
809         if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
810         {
811                 CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
812                 while (*p)
813                 {
814                         /* send digit to asterisk */
815                         memset(&fr, 0, sizeof(fr));
816                         fr.frametype = AST_FRAME_DTMF;
817                         fr.subclass = *p;
818                         fr.delivery = ast_tv(0, 0);
819                         ast_queue_frame(call->ast, &fr);
820                         p++;
821                 }
822         }
823         /* use bridge to forware message not supported by asterisk */
824         if (call->state == CHAN_LCR_STATE_CONNECT) {
825                 CDEBUG(call, call->ast, "Call is connected, briding.\n");
826                 bridge_message_if_bridged(call, message_type, param);
827         }
828 }
829
830 /*
831  * incoming information from LCR
832  */
833 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
834 {
835         CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
836
837         if (!call->ast) return;
838
839         /* use bridge to forware message not supported by asterisk */
840         bridge_message_if_bridged(call, message_type, param);
841 }
842
843 /*
844  * incoming information from LCR
845  */
846 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
847 {
848         CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
849
850         if (!call->ast) return;
851
852         /* use bridge to forware message not supported by asterisk */
853         bridge_message_if_bridged(call, message_type, param);
854 }
855
856 /*
857  * message received from LCR
858  */
859 int receive_message(int message_type, unsigned long ref, union parameter *param)
860 {
861         union parameter newparam;
862         struct bchannel *bchannel;
863         struct chan_call *call;
864
865         memset(&newparam, 0, sizeof(union parameter));
866
867         /* handle bchannel message*/
868         if (message_type == MESSAGE_BCHANNEL)
869         {
870                 switch(param->bchannel.type)
871                 {
872                         case BCHANNEL_ASSIGN:
873                         CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
874                         if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
875                         {
876                                 CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
877                                 return(-1);
878                         }
879                         /* create bchannel */
880                         bchannel = alloc_bchannel(param->bchannel.handle);
881                         if (!bchannel)
882                         {
883                                 CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
884                                 return(-1);
885                         }
886
887                         /* configure channel */
888                         bchannel->b_tx_gain = param->bchannel.tx_gain;
889                         bchannel->b_rx_gain = param->bchannel.rx_gain;
890                         strncpy(bchannel->b_pipeline, param->bchannel.pipeline, sizeof(bchannel->b_pipeline)-1);
891                         if (param->bchannel.crypt_len)
892                         {
893                                 bchannel->b_crypt_len = param->bchannel.crypt_len;
894                                 bchannel->b_crypt_type = param->bchannel.crypt_type;
895                                 memcpy(bchannel->b_crypt_key, param->bchannel.crypt, param->bchannel.crypt_len);
896                         }
897                         bchannel->b_txdata = 0;
898                         bchannel->b_dtmf = 1;
899                         bchannel->b_tx_dejitter = 1;
900
901                         /* in case, ref is not set, this bchannel instance must
902                          * be created until it is removed again by LCR */
903                         /* link to call */
904                         if ((call = find_call_ref(ref)))
905                         {
906                                 bchannel->call = call;
907                                 call->bchannel = bchannel;
908 #ifdef TODO
909 hier muesen alle bchannel-features gesetzt werden (pipeline...) falls sie vor dem b-kanal verfügbar waren
910 #endif
911                                 if (call->bridge_id) {
912                                         CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
913                                         bchannel_join(bchannel, call->bridge_id);
914                                 }
915                         }
916                         if (bchannel_create(bchannel))
917                                 bchannel_activate(bchannel, 1);
918
919                         /* acknowledge */
920                         newparam.bchannel.type = BCHANNEL_ASSIGN_ACK;
921                         newparam.bchannel.handle = param->bchannel.handle;
922                         send_message(MESSAGE_BCHANNEL, 0, &newparam);
923                         break;
924
925                         case BCHANNEL_REMOVE:
926                         CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
927                         if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
928                         {
929                                 CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
930                                 return(-1);
931                         }
932                         /* unklink from call and destroy bchannel */
933                         free_bchannel(bchannel);
934
935                         /* acknowledge */
936                         newparam.bchannel.type = BCHANNEL_REMOVE_ACK;
937                         newparam.bchannel.handle = param->bchannel.handle;
938                         send_message(MESSAGE_BCHANNEL, 0, &newparam);
939                         
940                         break;
941
942                         default:
943                         CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
944                 }
945                 return(0);
946         }
947
948         /* handle new ref */
949         if (message_type == MESSAGE_NEWREF)
950         {
951                 if (param->direction)
952                 {
953                         /* new ref from lcr */
954                         CDEBUG(NULL, NULL, "Received new ref by LCR, due to incomming call. (ref=%ld)\n", ref);
955                         if (!ref || find_call_ref(ref))
956                         {
957                                 CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
958                                 return(-1);
959                         }
960                         /* allocate new call instance */
961                         call = alloc_call();
962                         /* new state */
963                         call->state = CHAN_LCR_STATE_IN_PREPARE;
964                         /* set ref */
965                         call->ref = ref;
966                         /* wait for setup (or release from asterisk) */
967                 } else
968                 {
969                         /* new ref, as requested from this remote application */
970                         CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
971                         call = find_call_ref(0);
972                         if (!call)
973                         {
974                                 /* send release, if ref does not exist */
975                                 CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
976                                 send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
977                                 return(0);
978                         }
979                         /* store new ref */
980                         call->ref = ref;
981                         /* send pending setup info */
982                         if (call->state == CHAN_LCR_STATE_OUT_PREPARE)
983                                 send_setup_to_lcr(call);
984                         /* release if asterisk has signed off */
985                         else if (call->state == CHAN_LCR_STATE_RELEASE)
986                         {
987                                 /* send release */
988                                 if (call->cause)
989                                         send_release_and_import(call, call->cause, call->location);
990                                 else
991                                         send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
992                                 /* free call */
993                                 free_call(call);
994                                 return(0);
995                         }
996                 }
997                 return(0);
998         }
999
1000         /* check ref */
1001         if (!ref)
1002         {
1003                 CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
1004                 return(-1);
1005         }
1006         call = find_call_ref(ref);
1007         if (!call)
1008         {
1009                 /* ignore ref that is not used (anymore) */
1010                 CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
1011                 return(0);
1012         }
1013
1014         /* handle messages */
1015         switch(message_type)
1016         {
1017                 case MESSAGE_SETUP:
1018                 lcr_in_setup(call, message_type, param);
1019                 break;
1020
1021                 case MESSAGE_OVERLAP:
1022                 lcr_in_overlap(call, message_type, param);
1023                 break;
1024
1025                 case MESSAGE_PROCEEDING:
1026                 lcr_in_proceeding(call, message_type, param);
1027                 break;
1028
1029                 case MESSAGE_ALERTING:
1030                 lcr_in_alerting(call, message_type, param);
1031                 break;
1032
1033                 case MESSAGE_CONNECT:
1034                 lcr_in_connect(call, message_type, param);
1035                 break;
1036
1037                 case MESSAGE_DISCONNECT:
1038                 lcr_in_disconnect(call, message_type, param);
1039                 break;
1040
1041                 case MESSAGE_RELEASE:
1042                 lcr_in_release(call, message_type, param);
1043                 break;
1044
1045                 case MESSAGE_INFORMATION:
1046                 lcr_in_information(call, message_type, param);
1047                 break;
1048
1049                 case MESSAGE_NOTIFY:
1050                 lcr_in_notify(call, message_type, param);
1051                 break;
1052
1053                 case MESSAGE_FACILITY:
1054                 lcr_in_facility(call, message_type, param);
1055                 break;
1056
1057                 case MESSAGE_PATTERN: // audio available from LCR
1058                 break;
1059
1060                 case MESSAGE_NOPATTERN: // audio not available from LCR
1061                 break;
1062
1063                 case MESSAGE_AUDIOPATH: // if remote audio connected or hold
1064                 call->audiopath = param->audiopath;
1065                 break;
1066
1067                 default:
1068                 CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
1069                 break;
1070         }
1071         return(0);
1072 }
1073
1074 /*
1075  * release all calls (due to broken socket)
1076  */
1077 static void release_all_calls(void)
1078 {
1079         struct chan_call *call;
1080
1081 again:
1082         call = call_first;
1083         while(call) {
1084                 /* no ast, so we may directly free call */
1085                 if (!call->ast) {
1086                         CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
1087                         free_call(call);
1088                         goto again;
1089                 }
1090                 /* already in release process */
1091                 if (call->state == CHAN_LCR_STATE_RELEASE) {
1092                         call = call->next;
1093                         continue;
1094                 }
1095                 /* release or queue release */
1096                 call->ref = 0;
1097                 call->state = CHAN_LCR_STATE_RELEASE;
1098                 if (!call->pbx_started) {
1099                         CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
1100                         ast_hangup(call->ast); // call will be destroyed here
1101                         goto again;
1102                 }
1103                 CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
1104                 ast_queue_hangup(call->ast);
1105                 call = call->next;
1106         }
1107
1108         /* release all bchannels */
1109         while(bchannel_first)
1110                 free_bchannel(bchannel_first);
1111 }
1112
1113
1114 /* asterisk handler
1115  * warning! not thread safe
1116  * returns -1 for socket error, 0 for no work, 1 for work
1117  */
1118 int handle_socket(void)
1119 {
1120         int work = 0;
1121         int len;
1122         struct admin_list *admin;
1123         struct admin_message msg;
1124
1125         /* read from socket */
1126         len = read(lcr_sock, &msg, sizeof(msg));
1127         if (len == 0)
1128         {
1129                 CERROR(NULL, NULL, "Socket closed.\n");
1130                 return(-1); // socket closed
1131         }
1132         if (len > 0)
1133         {
1134                 if (len != sizeof(msg))
1135                 {
1136                         CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
1137                         return(-1); // socket error
1138                 }
1139                 if (msg.message != ADMIN_MESSAGE)
1140                 {
1141                         CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
1142                         return(-1);
1143                 }
1144                 receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
1145                 work = 1;
1146         } else
1147         {
1148                 if (errno != EWOULDBLOCK)
1149                 {
1150                         CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1151                         return(-1);
1152                 }
1153         }
1154
1155         /* write to socket */
1156         if (!admin_first)
1157                 return(work);
1158         admin = admin_first;
1159         len = write(lcr_sock, &admin->msg, sizeof(msg));
1160         if (len == 0)
1161         {
1162                 CERROR(NULL, NULL, "Socket closed.\n");
1163                 return(-1); // socket closed
1164         }
1165         if (len > 0)
1166         {
1167                 if (len != sizeof(msg))
1168                 {
1169                         CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
1170                         return(-1); // socket error
1171                 }
1172                 /* free head */
1173                 admin_first = admin->next;
1174                 free(admin);
1175
1176                 work = 1;
1177         } else
1178         {
1179                 if (errno != EWOULDBLOCK)
1180                 {
1181                         CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
1182                         return(-1);
1183                 }
1184         }
1185
1186         return(work);
1187 }
1188
1189 /*
1190  * open and close socket and thread
1191  */
1192 int open_socket(void)
1193 {
1194         int ret;
1195         char *socket_name = SOCKET_NAME;
1196         int conn;
1197         struct sockaddr_un sock_address;
1198         unsigned long on = 1;
1199         union parameter param;
1200
1201         /* open socket */
1202         if ((lcr_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1203         {
1204                 CERROR(NULL, NULL, "Failed to create socket.\n");
1205                 return(lcr_sock);
1206         }
1207
1208         /* set socket address and name */
1209         memset(&sock_address, 0, sizeof(sock_address));
1210         sock_address.sun_family = PF_UNIX;
1211         strcpy(sock_address.sun_path, socket_name);
1212
1213         /* connect socket */
1214         if ((conn = connect(lcr_sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
1215         {
1216                 close(lcr_sock);
1217                 lcr_sock = -1;
1218                 CDEBUG(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
1219                 return(conn);
1220         }
1221
1222         /* set non-blocking io */
1223         if ((ret = ioctl(lcr_sock, FIONBIO, (unsigned char *)(&on))) < 0)
1224         {
1225                 close(lcr_sock);
1226                 lcr_sock = -1;
1227                 CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
1228                 return(ret);
1229         }
1230
1231         /* enque hello message */
1232         memset(&param, 0, sizeof(param));
1233         strcpy(param.hello.application, "asterisk");
1234         send_message(MESSAGE_HELLO, 0, &param);
1235
1236         return(lcr_sock);
1237 }
1238
1239 void close_socket(void)
1240 {
1241         struct admin_list *admin, *temp;
1242         
1243         /* flush pending messages */
1244         admin = admin_first;
1245         while(admin) {
1246                 temp = admin;
1247                 admin = admin->next;
1248                 free(temp);
1249         }
1250         admin_first = NULL;
1251
1252         /* close socket */
1253         if (lcr_sock >= 0)      
1254                 close(lcr_sock);
1255         lcr_sock = -1;
1256 }
1257
1258 void sighandler(int sigset)
1259 {
1260 }
1261
1262 static void *chan_thread(void *arg)
1263 {
1264         int work;
1265         int ret;
1266         union parameter param;
1267         time_t retry = 0, now;
1268
1269         bchannel_pid = getpid();
1270
1271 //      signal(SIGPIPE, sighandler);
1272         
1273         memset(&param, 0, sizeof(union parameter));
1274         if (lcr_sock < 0)
1275                 time(&retry);
1276
1277         ast_mutex_lock(&chan_lock);
1278
1279         while(!quit) {
1280                 work = 0;
1281
1282                 if (lcr_sock > 0) {
1283                         /* handle socket */
1284                         ret = handle_socket();
1285                         if (ret < 0) {
1286                                 CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
1287                                 close_socket();
1288                                 release_all_calls();
1289                                 time(&retry);
1290                         }
1291                         if (ret)
1292                                 work = 1;
1293                 } else {
1294                         time(&now);
1295                         if (retry && now-retry > 5) {
1296                                 CDEBUG(NULL, NULL, "Retry to open socket.\n");
1297                                 retry = 0;
1298                                 if (open_socket() < 0) {
1299                                         time(&retry);
1300                                 }
1301                                 work = 1;
1302                         }
1303                                         
1304                 }
1305
1306                 /* handle mISDN */
1307                 ret = bchannel_handle();
1308                 if (ret)
1309                         work = 1;
1310                 
1311                 if (!work) {
1312                         ast_mutex_unlock(&chan_lock);
1313                         usleep(30000);
1314                         ast_mutex_lock(&chan_lock);
1315                 }
1316         }
1317
1318         close_socket();
1319
1320         CERROR(NULL, NULL, "Thread exit.\n");
1321         
1322         ast_mutex_unlock(&chan_lock);
1323
1324 //      signal(SIGPIPE, SIG_DFL);
1325
1326         return NULL;
1327 }
1328
1329 /*
1330  * new asterisk instance
1331  */
1332 static
1333 struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
1334 {
1335         union parameter newparam;
1336         struct ast_channel *ast;
1337         struct chan_call *call;
1338
1339         ast_mutex_lock(&chan_lock);
1340
1341         CDEBUG(NULL, NULL, "Received request from Asterisk.\n");
1342
1343         /* if socket is closed */
1344         if (lcr_sock < 0)
1345         {
1346                 CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
1347                 return NULL;
1348         }
1349         
1350         /* create call instance */
1351         call = alloc_call();
1352         if (!call)
1353         {
1354                 /* failed to create instance */
1355                 return NULL;
1356         }
1357         /* create asterisk channel instrance */
1358         ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
1359         if (!ast)
1360         {
1361                 CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
1362                 free_call(call);
1363                 /* failed to create instance */
1364                 return NULL;
1365         }
1366         /* link together */
1367         call->ast = ast;
1368         ast->tech_pvt = call;
1369         ast->tech = &lcr_tech;
1370         ast->fds[0] = call->pipe[0];
1371         /* configure channel */
1372 #ifdef TODO
1373         snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel);
1374         ast->name[sizeof(ast->name)-1] = '\0';
1375 #endif
1376 #ifdef TODO
1377         ast->nativeformats = configfile->lawformat;
1378         ast->readformat = ast->rawreadformat = configfile->lawformat;
1379         ast->writeformat = ast->rawwriteformat = configfile->lawformat;
1380 #else
1381         ast->nativeformats = AST_FORMAT_ALAW;
1382         ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
1383         ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
1384 #endif
1385         ast->priority = 1;
1386         ast->hangupcause = 0;
1387         /* send MESSAGE_NEWREF */
1388         memset(&newparam, 0, sizeof(union parameter));
1389         newparam.direction = 0; /* request from app */
1390         send_message(MESSAGE_NEWREF, 0, &newparam);
1391         /* set state */
1392         call->state = CHAN_LCR_STATE_OUT_PREPARE;
1393
1394         ast_mutex_unlock(&chan_lock);
1395
1396         return ast;
1397 }
1398
1399 /*
1400  * call from asterisk
1401  */
1402 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
1403 {
1404         struct chan_call *call;
1405
1406         ast_mutex_lock(&chan_lock);
1407         call = ast->tech_pvt;
1408         if (!call) {
1409                 CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n");
1410                 ast_mutex_unlock(&chan_lock);
1411                 return -1;
1412         }
1413
1414         CDEBUG(call, ast, "Received call from Asterisk.\n");
1415
1416 #warning was passiert zwischen lcr_request und lcr_call ?
1417         call->pbx_started = 1;
1418
1419         /* send setup message, if we already have a callref */
1420         if (call->ref)
1421                 send_setup_to_lcr(call);
1422
1423         ast_mutex_unlock(&chan_lock);
1424         return 0; 
1425 }
1426
1427 static int lcr_digit(struct ast_channel *ast, char digit)
1428 {
1429         struct chan_call *call;
1430         union parameter newparam;
1431         char buf[]="x";
1432
1433         /* only pass IA5 number space */
1434         if (digit > 126 || digit < 32)
1435                 return 0;
1436
1437         ast_mutex_lock(&chan_lock);
1438         call = ast->tech_pvt;
1439         if (!call) {
1440                 CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
1441                 ast_mutex_unlock(&chan_lock);
1442                 return -1;
1443         }
1444
1445         CDEBUG(call, ast, "Received digit Asterisk.\n");
1446
1447         /* send information or queue them */
1448         if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
1449         {
1450                 CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
1451                 memset(&newparam, 0, sizeof(union parameter));
1452                 newparam.information.id[0] = digit;
1453                 newparam.information.id[1] = '\0';
1454                 send_message(MESSAGE_INFORMATION, call->ref, &newparam);
1455         } else
1456         if (!call->ref
1457          && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP));
1458         {
1459                 CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
1460                 *buf = digit;
1461                 strncat(call->dialque, buf, strlen(call->dialque)-1);
1462         }
1463
1464         ast_mutex_unlock(&chan_lock);
1465         
1466         return(0);
1467 }
1468
1469 static int lcr_answer(struct ast_channel *ast)
1470 {
1471         union parameter newparam;
1472         struct chan_call *call;
1473
1474         ast_mutex_lock(&chan_lock);
1475         call = ast->tech_pvt;
1476         if (!call) {
1477                 CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
1478                 ast_mutex_unlock(&chan_lock);
1479                 return -1;
1480         }
1481         
1482         CDEBUG(call, ast, "Received answer from Asterisk.\n");
1483                 
1484         /* copy connectinfo, if bridged */
1485         if (call->bridge_call)
1486                 memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
1487         /* send connect message to lcr */
1488         memset(&newparam, 0, sizeof(union parameter));
1489         memcpy(&newparam.connectinfo, &call->connectinfo, sizeof(struct connect_info));
1490         send_message(MESSAGE_CONNECT, call->ref, &newparam);
1491         /* change state */
1492         call->state = CHAN_LCR_STATE_CONNECT;
1493         /* request bchannel */
1494         if (!call->bchannel) {
1495                 CDEBUG(call, ast, "Requesting B-channel.\n");
1496                 memset(&newparam, 0, sizeof(union parameter));
1497                 newparam.bchannel.type = BCHANNEL_REQUEST;
1498                 send_message(MESSAGE_BCHANNEL, call->ref, &newparam);
1499         }
1500         
1501         ast_mutex_unlock(&chan_lock);
1502         return 0;
1503 }
1504
1505 static int lcr_hangup(struct ast_channel *ast)
1506 {
1507         struct chan_call *call;
1508         pthread_t tid = pthread_self();
1509
1510         if (!pthread_equal(tid, chan_tid))
1511                 ast_mutex_lock(&chan_lock);
1512         call = ast->tech_pvt;
1513         if (!call) {
1514                 CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
1515                 if (!pthread_equal(tid, chan_tid))
1516                         ast_mutex_unlock(&chan_lock);
1517                 return -1;
1518         }
1519
1520         if (!pthread_equal(tid, chan_tid))
1521                 CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
1522         else
1523                 CDEBUG(call, ast, "Received hangup from LCR thread.\n");
1524
1525         /* disconnect asterisk, maybe not required */
1526         ast->tech_pvt = NULL;
1527         ast->fds[0] = -1;
1528         if (call->ref)
1529         {
1530                 /* release */
1531                 CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
1532                 if (ast->hangupcause > 0)
1533                         send_release_and_import(call, ast->hangupcause, LOCATION_PRIVATE_LOCAL);
1534                 else
1535                         send_release_and_import(call, CAUSE_RESSOURCEUNAVAIL, LOCATION_PRIVATE_LOCAL);
1536                 /* remove call */
1537                 free_call(call);
1538                 if (!pthread_equal(tid, chan_tid))
1539                         ast_mutex_unlock(&chan_lock);
1540                 return 0;
1541         } else
1542         {
1543                 /* ref is not set, due to prepare setup or release */
1544                 if (call->state == CHAN_LCR_STATE_RELEASE)
1545                 {
1546                         /* we get the response to our release */
1547                         CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
1548                         free_call(call);
1549                 } else
1550                 {
1551                         /* during prepare, we change to release state */
1552                         CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
1553                         call->state = CHAN_LCR_STATE_RELEASE;
1554                 }
1555         } 
1556         if (!pthread_equal(tid, chan_tid))
1557                 ast_mutex_unlock(&chan_lock);
1558         return 0;
1559 }
1560
1561 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
1562 {
1563         struct chan_call *call;
1564
1565         if (!f->subclass)
1566                 CDEBUG(NULL, ast, "No subclass\n");
1567 #ifdef TODO
1568         config
1569 #else
1570         if (!(f->subclass & AST_FORMAT_ALAW))
1571 #endif
1572                 CDEBUG(NULL, ast, "Unexpected format.\n");
1573         
1574         ast_mutex_lock(&chan_lock);
1575         call = ast->tech_pvt;
1576         if (!call) {
1577                 ast_mutex_unlock(&chan_lock);
1578                 return -1;
1579         }
1580         if (call->bchannel && f->samples)
1581                 bchannel_transmit(call->bchannel, f->data, f->samples);
1582         ast_mutex_unlock(&chan_lock);
1583         return 0;
1584 }
1585
1586
1587 static struct ast_frame *lcr_read(struct ast_channel *ast)
1588 {
1589         struct chan_call *call;
1590         int i, len;
1591         unsigned char *p;
1592
1593         ast_mutex_lock(&chan_lock);
1594         call = ast->tech_pvt;
1595         if (!call) {
1596                 ast_mutex_unlock(&chan_lock);
1597                 return NULL;
1598         }
1599         if (call->pipe[0] > -1) {
1600                 len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
1601                 if (len <= 0) {
1602                         close(call->pipe[0]);
1603                         call->pipe[0] = -1;
1604                         return NULL;
1605                 }
1606         }
1607
1608         p = call->read_buff;
1609         for (i = 0; i < len; i++) {
1610                 *p = flip_bits[*p];
1611                 p++;
1612         }
1613
1614         call->read_fr.frametype = AST_FRAME_VOICE;
1615 #ifdef TODO
1616         format aus config
1617 #else
1618         call->read_fr.subclass = AST_FORMAT_ALAW;
1619 #endif
1620         call->read_fr.datalen = len;
1621         call->read_fr.samples = len;
1622         call->read_fr.delivery = ast_tv(0,0);
1623         call->read_fr.data = call->read_buff;
1624         ast_mutex_unlock(&chan_lock);
1625
1626         return &call->read_fr;
1627 }
1628
1629 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
1630 {
1631         union parameter newparam;
1632         int res = 0;
1633         struct chan_call *call;
1634
1635         ast_mutex_lock(&chan_lock);
1636         call = ast->tech_pvt;
1637         if (!call) {
1638                 CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
1639                 ast_mutex_unlock(&chan_lock);
1640                 return -1;
1641         }
1642
1643         switch (cond) {
1644                 case AST_CONTROL_BUSY:
1645                         CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
1646                         ast_setstate(ast, AST_STATE_BUSY);
1647                         if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1648                                 /* send message to lcr */
1649                                 memset(&newparam, 0, sizeof(union parameter));
1650                                 newparam.disconnectinfo.cause = 17;
1651                                 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1652                                 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1653                                 /* change state */
1654                                 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1655                         }
1656                         break;
1657                 case AST_CONTROL_CONGESTION:
1658                         CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk. (cause %d)\n", ast->hangupcause);
1659                         if (call->state != CHAN_LCR_STATE_OUT_DISCONNECT) {
1660                                 /* send message to lcr */
1661                                 memset(&newparam, 0, sizeof(union parameter));
1662                                 newparam.disconnectinfo.cause = ast->hangupcause;
1663                                 newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1664                                 send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
1665                                 /* change state */
1666                                 call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
1667                         }
1668                         break;
1669                 case AST_CONTROL_PROCEEDING:
1670                         CDEBUG(call, ast, "Received indicate AST_CONTROL_PROCEEDING from Asterisk.\n");
1671                         if (call->state == CHAN_LCR_STATE_IN_SETUP
1672                          || call->state == CHAN_LCR_STATE_IN_DIALING) {
1673                                 /* send message to lcr */
1674                                 memset(&newparam, 0, sizeof(union parameter));
1675                                 send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
1676                                 /* change state */
1677                                 call->state = CHAN_LCR_STATE_IN_PROCEEDING;
1678                         }
1679                         break;
1680                 case AST_CONTROL_RINGING:
1681                         CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
1682                         ast_setstate(ast, AST_STATE_RINGING);
1683                         if (call->state == CHAN_LCR_STATE_IN_SETUP
1684                          || call->state == CHAN_LCR_STATE_IN_DIALING
1685                          || call->state == CHAN_LCR_STATE_IN_PROCEEDING) {
1686                                 /* send message to lcr */
1687                                 memset(&newparam, 0, sizeof(union parameter));
1688                                 send_message(MESSAGE_ALERTING, call->ref, &newparam);
1689                                 /* change state */
1690                                 call->state = CHAN_LCR_STATE_IN_ALERTING;
1691                         }
1692                         break;
1693                 case -1:
1694                         CDEBUG(call, ast, "Received indicate -1.\n");
1695                         res = -1;
1696                         break;
1697
1698                 case AST_CONTROL_VIDUPDATE:
1699                         CDEBUG(call, ast, "Received indicate AST_CONTROL_VIDUPDATE.\n");
1700                         res = -1;
1701                         break;
1702                 case AST_CONTROL_HOLD:
1703                         CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
1704                         /* send message to lcr */
1705                         memset(&newparam, 0, sizeof(union parameter));
1706                         newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
1707                         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1708                         break;
1709                 case AST_CONTROL_UNHOLD:
1710                         CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
1711                         /* send message to lcr */
1712                         memset(&newparam, 0, sizeof(union parameter));
1713                         newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
1714                         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1715                         break;
1716
1717                 default:
1718                         CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
1719                         res = -1;
1720                         break;
1721         }
1722
1723         /* return */
1724         ast_mutex_unlock(&chan_lock);
1725         return res;
1726 }
1727
1728 /*
1729  * fixup asterisk
1730  */
1731 static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *newast)
1732 {
1733         struct chan_call *call;
1734
1735         ast_mutex_lock(&chan_lock);
1736         call = oldast->tech_pvt;
1737         if (!call) {
1738                 CERROR(NULL, oldast, "Received fixup from Asterisk, but no call instance exists.\n");
1739                 ast_mutex_unlock(&chan_lock);
1740                 return -1;
1741         }
1742
1743         CDEBUG(call, oldast, "Received fixup from Asterisk.\n");
1744         call->ast = newast;
1745         ast_mutex_lock(&chan_lock);
1746         return 0;
1747 }
1748
1749 /*
1750  * send_text asterisk
1751  */
1752 static int lcr_send_text(struct ast_channel *ast, const char *text)
1753 {
1754         struct chan_call *call;
1755         union parameter newparam;
1756
1757         ast_mutex_lock(&chan_lock);
1758         call = ast->tech_pvt;
1759         if (!call) {
1760                 CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
1761                 ast_mutex_unlock(&chan_lock);
1762                 return -1;
1763         }
1764
1765         CDEBUG(call, ast, "Received send_text from Asterisk. (text=%s)\n", text);
1766         memset(&newparam, 0, sizeof(union parameter));
1767         strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
1768         send_message(MESSAGE_NOTIFY, call->ref, &newparam);
1769         ast_mutex_lock(&chan_lock);
1770         return 0;
1771 }
1772
1773 /*
1774  * bridge process
1775  */
1776 enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
1777                                   struct ast_channel *ast2, int flags,
1778                                   struct ast_frame **fo,
1779                                   struct ast_channel **rc, int timeoutms)
1780
1781 {
1782         struct chan_call        *call1, *call2;
1783         struct ast_channel      *carr[2], *who;
1784         int                     to = -1;
1785         struct ast_frame        *f;
1786         int                     bridge_id;
1787
1788         CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
1789         
1790         /* join via dsp (if the channels are currently open) */
1791         ast_mutex_lock(&chan_lock);
1792         bridge_id = new_bridge_id();
1793         call1 = ast1->tech_pvt;
1794         call2 = ast2->tech_pvt;
1795         if (call1 && call2)
1796         {
1797                 call1->bridge_id = bridge_id;
1798                 if (call1->bchannel)
1799                         bchannel_join(call1->bchannel, bridge_id);
1800                 call1->bridge_call = call2;
1801         }
1802         if (call2)
1803         {
1804                 call2->bridge_id = bridge_id;
1805                 if (call2->bchannel)
1806                         bchannel_join(call2->bchannel, bridge_id);
1807                 call2->bridge_call = call1;
1808         }
1809         ast_mutex_unlock(&chan_lock);
1810         
1811         while(1) {
1812                 who = ast_waitfor_n(carr, 2, &to);
1813
1814                 if (!who) {
1815                         CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
1816                         break;
1817                 }
1818                 f = ast_read(who);
1819     
1820                 if (!f || f->frametype == AST_FRAME_CONTROL) {
1821                         /* got hangup .. */
1822                         *fo=f;
1823                         *rc=who;
1824                         break;
1825                 }
1826                 
1827                 if ( f->frametype == AST_FRAME_DTMF ) {
1828                         *fo=f;
1829                         *rc=who;
1830                         break;
1831                 }
1832         
1833
1834                 if (who == ast1) {
1835                         ast_write(ast2,f);
1836                 }
1837                 else {
1838                         ast_write(ast1,f);
1839                 }
1840     
1841         }
1842         
1843         CDEBUG(NULL, NULL, "Releasing bride.\n");
1844
1845         /* split channels */
1846         ast_mutex_lock(&chan_lock);
1847         call1 = ast1->tech_pvt;
1848         call2 = ast2->tech_pvt;
1849         if (call1)
1850         {
1851                 call1->bridge_id = 0;
1852                 if (call1->bchannel)
1853                         bchannel_join(call1->bchannel, 0);
1854                 if (call1->bridge_call)
1855                         call1->bridge_call->bridge_call = NULL;
1856                 call1->bridge_call = NULL;
1857         }
1858         if (call2)
1859         {
1860                 call2->bridge_id = 0;
1861                 if (call2->bchannel)
1862                         bchannel_join(call2->bchannel, 0);
1863                 if (call2->bridge_call)
1864                         call2->bridge_call->bridge_call = NULL;
1865                 call2->bridge_call = NULL;
1866         }
1867         ast_mutex_unlock(&chan_lock);
1868         
1869         
1870         return AST_BRIDGE_COMPLETE;
1871 }
1872 static struct ast_channel_tech lcr_tech = {
1873         .type="LCR",
1874         .description="Channel driver for connecting to Linux-Call-Router",
1875 #ifdef TODO
1876         law from config
1877 #else
1878         .capabilities=AST_FORMAT_ALAW,
1879 #endif
1880         .requester=lcr_request,
1881         .send_digit_begin=lcr_digit,
1882         .call=lcr_call,
1883         .bridge=lcr_bridge, 
1884         .hangup=lcr_hangup,
1885         .answer=lcr_answer,
1886         .read=lcr_read,
1887         .write=lcr_write,
1888         .indicate=lcr_indicate,
1889         .fixup=lcr_fixup,
1890         .send_text=lcr_send_text,
1891         .properties=0
1892 };
1893
1894
1895 /*
1896  * cli
1897  */
1898 static int lcr_show_lcr (int fd, int argc, char *argv[])
1899 {
1900         return 0;
1901 }
1902
1903 static int lcr_show_calls (int fd, int argc, char *argv[])
1904 {
1905         return 0;
1906 }
1907
1908 static int lcr_reload_routing (int fd, int argc, char *argv[])
1909 {
1910         return 0;
1911 }
1912
1913 static int lcr_reload_interfaces (int fd, int argc, char *argv[])
1914 {
1915         return 0;
1916 }
1917
1918 static int lcr_port_block (int fd, int argc, char *argv[])
1919 {
1920         return 0;
1921 }
1922
1923 static int lcr_port_unblock (int fd, int argc, char *argv[])
1924 {
1925         return 0;
1926 }
1927
1928 static int lcr_port_unload (int fd, int argc, char *argv[])
1929 {
1930         return 0;
1931 }
1932
1933 static struct ast_cli_entry cli_show_lcr =
1934 { {"lcr", "show", "lcr", NULL},
1935  lcr_show_lcr,
1936  "Shows current states of LCR core",
1937  "Usage: lcr show lcr\n",
1938 };
1939
1940 static struct ast_cli_entry cli_show_calls =
1941 { {"lcr", "show", "calls", NULL},
1942  lcr_show_calls,
1943  "Shows current calls made by LCR and Asterisk",
1944  "Usage: lcr show calls\n",
1945 };
1946
1947 static struct ast_cli_entry cli_reload_routing =
1948 { {"lcr", "reload", "routing", NULL},
1949  lcr_reload_routing,
1950  "Reloads routing conf of LCR, current uncomplete calls will be disconnected",
1951  "Usage: lcr reload routing\n",
1952 };
1953
1954 static struct ast_cli_entry cli_reload_interfaces =
1955 { {"lcr", "reload", "interfaces", NULL},
1956  lcr_reload_interfaces,
1957  "Reloads interfaces conf of LCR",
1958  "Usage: lcr reload interfaces\n",
1959 };
1960
1961 static struct ast_cli_entry cli_port_block =
1962 { {"lcr", "port", "block", NULL},
1963  lcr_port_block,
1964  "Blocks LCR port for further calls",
1965  "Usage: lcr port block \"<port>\"\n",
1966 };
1967
1968 static struct ast_cli_entry cli_port_unblock =
1969 { {"lcr", "port", "unblock", NULL},
1970  lcr_port_unblock,
1971  "Unblocks or loads LCR port, port is opened my mISDN",
1972  "Usage: lcr port unblock \"<port>\"\n",
1973 };
1974
1975 static struct ast_cli_entry cli_port_unload =
1976 { {"lcr", "port", "unload", NULL},
1977  lcr_port_unload,
1978  "Unloads LCR port, port is closes by mISDN",
1979  "Usage: lcr port unload \"<port>\"\n",
1980 };
1981
1982
1983 /*
1984  * module loading and destruction
1985  */
1986 int load_module(void)
1987 {
1988         u_short i;
1989
1990         for (i = 0; i < 256; i++) {
1991                 flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
1992                              | (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
1993         }
1994
1995         ast_mutex_init(&chan_lock);
1996         ast_mutex_init(&log_lock);
1997
1998         if (open_socket() < 0) {
1999                 /* continue with closed socket */
2000         }
2001
2002         if (bchannel_initialize()) {
2003                 CERROR(NULL, NULL, "Unable to open mISDN device\n");
2004                 close_socket();
2005                 return AST_MODULE_LOAD_DECLINE;
2006         }
2007         mISDN_created = 1;
2008
2009         if (ast_channel_register(&lcr_tech)) {
2010                 CERROR(NULL, NULL, "Unable to register channel class\n");
2011                 bchannel_deinitialize();
2012                 close_socket();
2013                 return AST_MODULE_LOAD_DECLINE;
2014         }
2015  
2016 #if 0   
2017         ast_cli_register(&cli_show_lcr);
2018         ast_cli_register(&cli_show_calls);
2019
2020         ast_cli_register(&cli_reload_routing);
2021         ast_cli_register(&cli_reload_interfaces);
2022         ast_cli_register(&cli_port_block);
2023         ast_cli_register(&cli_port_unblock);
2024         ast_cli_register(&cli_port_unload);
2025   
2026         ast_register_application("misdn_set_opt", misdn_set_opt_exec, "misdn_set_opt",
2027                                  "misdn_set_opt(:<opt><optarg>:<opt><optarg>..):\n"
2028                                  "Sets mISDN opts. and optargs\n"
2029                                  "\n"
2030                                  "The available options are:\n"
2031                                  "    d - Send display text on called phone, text is the optparam\n"
2032                                  "    n - don't detect dtmf tones on called channel\n"
2033                                  "    h - make digital outgoing call\n" 
2034                                  "    c - make crypted outgoing call, param is keyindex\n"
2035                                  "    e - perform echo cancelation on this channel,\n"
2036                                  "        takes taps as arguments (32,64,128,256)\n"
2037                                  "    s - send Non Inband DTMF as inband\n"
2038                                  "   vr - rxgain control\n"
2039                                  "   vt - txgain control\n"
2040                 );
2041
2042         
2043         lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
2044
2045 =======
2046         //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
2047 #endif
2048
2049         quit = 0;       
2050         if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
2051         {
2052                 /* failed to create thread */
2053                 bchannel_deinitialize();
2054                 close_socket();
2055                 ast_channel_unregister(&lcr_tech);
2056                 return AST_MODULE_LOAD_DECLINE;
2057         }
2058         return 0;
2059 }
2060
2061 int unload_module(void)
2062 {
2063         /* First, take us out of the channel loop */
2064         CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
2065
2066         quit = 1;
2067         pthread_join(chan_tid, NULL);   
2068         
2069         ast_channel_unregister(&lcr_tech);
2070
2071         if (mISDN_created) {
2072                 bchannel_deinitialize();
2073                 mISDN_created = 0;
2074         }
2075
2076         if (lcr_sock >= 0) {
2077                 close(lcr_sock);
2078                 lcr_sock = -1;
2079         }
2080
2081         return 0;
2082 }
2083
2084 int reload_module(void)
2085 {
2086 //      reload_config();
2087         return 0;
2088 }
2089
2090 #ifdef TODO
2091 wech damit
2092
2093 int usecnt;
2094 ast_mutex_t usecnt_lock;
2095
2096 int usecount(void)
2097 {
2098         int res;
2099         ast_mutex_lock(&usecnt_lock);
2100         res = usecnt;
2101         ast_mutex_unlock(&usecnt_lock);
2102         return res;
2103 }
2104
2105
2106 char *desc="Channel driver for lcr";
2107
2108 char *description(void)
2109 {
2110         return desc;
2111 }
2112
2113 char *key(void)
2114 {
2115         return ASTERISK_GPL_KEY;
2116 }
2117 #endif
2118
2119 #define AST_MODULE "chan_lcr"
2120
2121 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Channel driver for Linux-Call-Router Support (ISDN BRI/PRI)",
2122                 .load = load_module,
2123                 .unload = unload_module,
2124                 .reload = reload_module,
2125                );
2126