fix and work
[lcr.git] / chan_lcr.c
index c72ed83..fa37ef6 100644 (file)
@@ -79,11 +79,6 @@ If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
 
 */
 
-#warning reconnect after socket closed, release all calls.
-#warning debug of call handling
-#warning ausloesen beim socket-verlust
-
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -129,13 +124,16 @@ If the ref is 0 and the state is CHAN_LCR_STATE_RELEASE, see the proceedure
 
 CHAN_LCR_STATE // state description structure
 
+u_char flip_bits[256];
+
 int lcr_debug=1;
 int mISDN_created=1;
 
 char lcr_type[]="lcr";
 
 pthread_t chan_tid;
-ast_mutex_t chan_lock;
+ast_mutex_t chan_lock; /* global lock */
+ast_mutex_t log_lock; /* logging log */
 int quit;
 
 int glob_channel = 0;
@@ -150,6 +148,34 @@ struct admin_list {
 static struct ast_channel_tech lcr_tech;
 
 /*
+ * logging
+ */
+void chan_lcr_log(int type, const char *file, int line, struct chan_call *call, struct ast_channel *ast, const char *fmt, ...)
+{
+       char buffer[1024];
+       char call_text[128] = "NULL";
+       char ast_text[128] = "NULL";
+       va_list args;
+
+       ast_mutex_lock(&log_lock);
+
+       va_start(args,fmt);
+       vsnprintf(buffer,sizeof(buffer)-1,fmt,args);
+       buffer[sizeof(buffer)-1]=0;
+       va_end(args);
+
+       if (call)
+               sprintf(call_text, "%ld", call->ref);
+       if (ast)
+               strncpy(ast_text, ast->name, sizeof(ast_text)-1);
+       ast_text[sizeof(ast_text)-1] = '\0';
+       
+       ast_log(type, file, line, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
+
+       ast_mutex_unlock(&log_lock);
+}
+
+/*
  * channel and call instances
  */
 struct chan_call *call_first;
@@ -195,19 +221,6 @@ struct chan_call *find_call_handle(unsigned long handle)
 }
 #endif
 
-struct chan_call *alloc_call(void)
-{
-       struct chan_call **callp = &call_first;
-
-       while(*callp)
-               callp = &((*callp)->next);
-
-       *callp = (struct chan_call *)malloc(sizeof(struct chan_call));
-       if (*callp)
-               memset(*callp, 0, sizeof(struct chan_call));
-       return(*callp);
-}
-
 void free_call(struct chan_call *call)
 {
        struct chan_call **temp = &call_first;
@@ -217,23 +230,51 @@ void free_call(struct chan_call *call)
                if (*temp == call)
                {
                        *temp = (*temp)->next;
+                       if (call->pipe[0])
+                               close(call->pipe[0]);
+                       if (call->pipe[1])
+                               close(call->pipe[1]);
                        if (call->bchannel)
                        {
-                               if (call->bchannel->call)
-                                       call->bchannel->call = NULL;
+                               if (call->bchannel->call != call)
+                                       CERROR(call, NULL, "Linked bchannel structure has no link to us.\n");
+                               call->bchannel->call = NULL;
                        }
                        if (call->bridge_call)
                        {
-                               if (call->bridge_call->bridge_call)
-                                       call->bridge_call->bridge_call = NULL;
+                               if (call->bridge_call->bridge_call != call)
+                                       CERROR(call, NULL, "Linked call structure has no link to us.\n");
+                               call->bridge_call->bridge_call = NULL;
                        }
+                       CDEBUG(call, NULL, "Call instance freed.\n");
                        free(call);
                        return;
                }
                temp = &((*temp)->next);
        }
+       CERROR(call, NULL, "Call instance not found in list.\n");
+}
+
+struct chan_call *alloc_call(void)
+{
+       struct chan_call **callp = &call_first;
+
+       while(*callp)
+               callp = &((*callp)->next);
+
+       *callp = (struct chan_call *)malloc(sizeof(struct chan_call));
+       if (*callp)
+               memset(*callp, 0, sizeof(struct chan_call));
+       if (pipe((*callp)->pipe) < 0) {
+               CERROR(*callp, NULL, "Failed to create pipe.\n");
+               free_call(*callp);
+               return(NULL);
+       }
+       CDEBUG(*callp, NULL, "Call instance allocated.\n");
+       return(*callp);
 }
 
+
 unsigned short new_bridge_id(void)
 {
        struct chan_call *call;
@@ -253,28 +294,24 @@ unsigned short new_bridge_id(void)
                        break;
                id++;
        }
+       CDEBUG(NULL, NULL, "New bridge ID %d.\n", id);
        return(id);
 }
 
 
 /*
- * receive bchannel data
- */
-void rx_data(struct bchannel *bchannel, unsigned char *data, int len)
-{
-}
-
-void rx_dtmf(struct bchannel *bchannel, char tone)
-{
-}
-
-/*
  * enque message to LCR
  */
 int send_message(int message_type, unsigned long ref, union parameter *param)
 {
        struct admin_list *admin, **adminp;
 
+       if (lcr_sock < 0) {
+               CDEBUG(NULL, NULL, "Ignoring message %d, because socket is closed.\n", message_type);
+               return -1;
+       }
+       CDEBUG(NULL, NULL, "Sending message %d to socket.\n", message_type);
+
        adminp = &admin_first;
        while(*adminp)
                adminp = &((*adminp)->next);
@@ -300,6 +337,8 @@ static void send_setup_to_lcr(struct chan_call *call)
        if (!call->ast || !call->ref)
                return;
 
+       CDEBUG(call, call->ast, "Sending setup to LCR.\n");
+
        /* send setup message to LCR */
        memset(&newparam, 0, sizeof(union parameter));
                newparam.setup.callerinfo.itype = INFO_ITYPE_CHAN;      
@@ -343,8 +382,10 @@ static void send_setup_to_lcr(struct chan_call *call)
                newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
        }
        newparam.setup.capainfo.bearer_capa = ast->transfercapability;
-#warning todo
-//     newparam.setup.capainfo.bearer_user = alaw 3, ulaw 2;
+#ifdef TODO
+       newparam.setup.capainfo.bearer_info1 = alaw 3, ulaw 2;
+#endif
+       newparam.setup.capainfo.bearer_info1 = 3;
        newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
        newparam.setup.capainfo.hlc = INFO_HLC_NONE;
        newparam.setup.capainfo.exthlc = INFO_HLC_NONE;
@@ -366,6 +407,8 @@ static void send_dialque_to_lcr(struct chan_call *call)
        if (!call->ast || !call->ref || !call->dialque)
                return;
        
+       CDEBUG(call, call->ast, "Sending dial queue to LCR.\n");
+
        /* send setup message to LCR */
        memset(&newparam, 0, sizeof(union parameter));
        strncpy(newparam.information.id, call->dialque, sizeof(newparam.information.id)-1);
@@ -382,7 +425,94 @@ static void bridge_message_if_bridged(struct chan_call *call, int message_type,
        /* check bridge */
        if (!call) return;
        if (!call->bridge_call) return;
-       send_message(MESSAGE_RELEASE, call->bridge_call->ref, param);
+       CDEBUG(call, NULL, "Sending message due briding.\n");
+       send_message(message_type, call->bridge_call->ref, param);
+}
+
+/*
+ * check if extension matches and start asterisk
+ * if it can match, proceed
+ * if not, release
+ */
+static void lcr_start_pbx(struct chan_call *call, struct ast_channel *ast, int complete)
+{
+       int cause, ret;
+       union parameter newparam;
+
+       if (complete)
+       {
+               /* if not match */
+               if (!ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
+               {
+                       CDEBUG(call, ast, "Got 'sending complete', but extension '%s' will not match at context '%s' - releasing.\n", ast->exten, ast->context);
+                       cause = 1;
+                       goto release;
+               }
+               if (!ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad))
+               {
+                       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);
+                       cause = 28;
+                       goto release;
+               }
+               CDEBUG(call, ast, "Got 'sending complete', extensions matches.\n");
+               /* send setup acknowledge to lcr */
+               memset(&newparam, 0, sizeof(union parameter));
+               send_message(MESSAGE_PROCEEDING, call->ref, &newparam);
+
+               /* change state */
+               call->state = CHAN_LCR_STATE_IN_PROCEEDING;
+
+               goto start;
+       }
+
+       if (ast_canmatch_extension(ast, ast->context, ast->exten, 1, call->oad))
+       {
+               /* send setup acknowledge to lcr */
+               memset(&newparam, 0, sizeof(union parameter));
+               send_message(MESSAGE_OVERLAP, call->ref, &newparam);
+
+               /* change state */
+               call->state = CHAN_LCR_STATE_IN_DIALING;
+
+               /* if match, start pbx */
+               if (ast_exists_extension(ast, ast->context, ast->exten, 1, call->oad)) {
+                       CDEBUG(call, ast, "Extensions matches.\n");
+                       goto start;
+               }
+
+               /* if can match */
+               CDEBUG(call, ast, "Extensions may match, if more digits are dialed.\n");
+               return;
+       }
+
+       /* if not match */
+       cause = 1;
+       release:
+       /* release lcr */
+       CDEBUG(call, ast, "Releasing due to extension missmatch.\n");
+       memset(&newparam, 0, sizeof(union parameter));
+       newparam.disconnectinfo.cause = cause;
+       newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
+       send_message(MESSAGE_RELEASE, call->ref, &newparam);
+       call->ref = 0;
+       /* release asterisk */
+       ast->hangupcause = call->cause;
+       /* change to release state */
+       call->state = CHAN_LCR_STATE_RELEASE;
+       ast_hangup(ast); // call will be destroyed here
+       return;
+       
+       start:
+       /* send setup to asterisk */
+       CDEBUG(call, ast, "Starting call to Asterisk due to matching extension.\n");
+       ret = ast_pbx_start(ast);
+       if (ret < 0)
+       {
+               cause = (ret==-2)?34:27;
+               goto release;
+       }
+       call->pbx_started = 1;
+       return;
 }
 
 /*
@@ -393,12 +523,14 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet
        struct ast_channel *ast;
        union parameter newparam;
 
+       CDEBUG(call, NULL, "Incomming setup from LCR. (callerid %d, dialing %d)\n", param->setup.callerinfo.id, param->setup.dialinginfo.id);
+
        /* create asterisk channel instrance */
-#warning anstatt vom lcr "setup.exten" zu bekommen, sollten wir den context übertragen, der dann in der channel.conf zu dem richtigen ruleset führt.
        ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
-       if (!call->ast)
+       if (!ast)
        {
                /* release */
+               CERROR(call, NULL, "Failed to create Asterisk channel - releasing.\n");
                memset(&newparam, 0, sizeof(union parameter));
                newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
                newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
@@ -411,18 +543,20 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet
        call->ast = ast;
        ast->tech_pvt = call;
        ast->tech = &lcr_tech;
+       ast->fds[0] = call->pipe[0];
        
        /* fill setup information */
-       if (param->setup.exten[0])
-               strncpy(ast->exten, param->setup.exten, AST_MAX_EXTENSION);
+       if (param->setup.dialinginfo.id)
+               strncpy(ast->exten, param->setup.dialinginfo.id, AST_MAX_EXTENSION-1);
+       if (param->setup.context[0])
+               strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
        if (param->setup.callerinfo.id[0])
                ast->cid.cid_num = strdup(param->setup.callerinfo.id);
        if (param->setup.callerinfo.name[0])
                ast->cid.cid_name = strdup(param->setup.callerinfo.name);
-#warning todo
-#if 0
+#ifdef TODO
        if (param->setup.redirinfo.id[0])
-               ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.name, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
+               ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter));
 #endif
        switch (param->setup.callerinfo.present)
        {
@@ -450,28 +584,28 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet
                        ast->cid.cid_ton = 0;
        }
        ast->transfercapability = param->setup.capainfo.bearer_capa;
+#ifdef TODO
+       strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, configfile->prefix_nat, configfile->prefix_inter), sizeof(call->oad)-1);
+#else
+       strncpy(call->oad, numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, "0", "00"), sizeof(call->oad)-1);
+#endif
 
        /* configure channel */
-#warning todo
-#if 0
-       ast->nativeformat = configfile->lawformat;
+#ifdef TODO
+       ast->nativeformats = configfile->lawformat;
        ast->readformat = ast->rawreadformat = configfile->lawformat;
        ast->writeformat = ast->rawwriteformat = configfile->lawformat;
+#else
+       ast->nativeformats = AST_FORMAT_ALAW;
+       ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
+       ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
 #endif
        ast->hangupcause = 0;
 
        /* change state */
        call->state = CHAN_LCR_STATE_IN_SETUP;
 
-       /* send setup to asterisk */
-       ast_pbx_start(ast);
-
-       /* send setup acknowledge to lcr */
-       memset(&newparam, 0, sizeof(union parameter));
-       send_message(MESSAGE_OVERLAP, call->ref, &newparam);
-
-       /* change state */
-       call->state = CHAN_LCR_STATE_IN_DIALING;
+       lcr_start_pbx(call, ast, param->setup.dialinginfo.sending_complete);
 }
 
 /*
@@ -481,6 +615,8 @@ static void lcr_in_overlap(struct chan_call *call, int message_type, union param
 {
        if (!call->ast) return;
 
+       CDEBUG(call, call->ast, "Incomming setup acknowledge from LCR.\n");
+
        /* send pending digits in dialque */
        if (call->dialque)
                send_dialque_to_lcr(call);
@@ -493,10 +629,12 @@ static void lcr_in_overlap(struct chan_call *call, int message_type, union param
  */
 static void lcr_in_proceeding(struct chan_call *call, int message_type, union parameter *param)
 {
+       CDEBUG(call, call->ast, "Incomming proceeding from LCR.\n");
+
        /* change state */
        call->state = CHAN_LCR_STATE_OUT_PROCEEDING;
        /* send event to asterisk */
-       if (call->ast)
+       if (call->ast && call->pbx_started)
                ast_queue_control(call->ast, AST_CONTROL_PROCEEDING);
 }
 
@@ -505,10 +643,12 @@ static void lcr_in_proceeding(struct chan_call *call, int message_type, union pa
  */
 static void lcr_in_alerting(struct chan_call *call, int message_type, union parameter *param)
 {
+       CDEBUG(call, call->ast, "Incomming alerting from LCR.\n");
+
        /* change state */
        call->state = CHAN_LCR_STATE_OUT_ALERTING;
        /* send event to asterisk */
-       if (call->ast)
+       if (call->ast && call->pbx_started)
                ast_queue_control(call->ast, AST_CONTROL_RINGING);
 }
 
@@ -517,12 +657,14 @@ static void lcr_in_alerting(struct chan_call *call, int message_type, union para
  */
 static void lcr_in_connect(struct chan_call *call, int message_type, union parameter *param)
 {
+       CDEBUG(call, call->ast, "Incomming connect (answer) from LCR.\n");
+
        /* change state */
        call->state = CHAN_LCR_STATE_CONNECT;
        /* copy connectinfo */
        memcpy(&call->connectinfo, &param->connectinfo, sizeof(struct connect_info));
        /* send event to asterisk */
-       if (call->ast)
+       if (call->ast && call->pbx_started)
                ast_queue_control(call->ast, AST_CONTROL_ANSWER);
 }
 
@@ -532,7 +674,8 @@ static void lcr_in_connect(struct chan_call *call, int message_type, union param
 static void lcr_in_disconnect(struct chan_call *call, int message_type, union parameter *param)
 {
        struct ast_channel *ast = call->ast;
-       union parameter newparam;
+
+       CDEBUG(call, call->ast, "Incomming disconnect from LCR. (cause=%d)\n", param->disconnectinfo.cause);
 
        /* change state */
        call->state = CHAN_LCR_STATE_IN_DISCONNECT;
@@ -540,21 +683,30 @@ static void lcr_in_disconnect(struct chan_call *call, int message_type, union pa
        call->cause = param->disconnectinfo.cause;
        call->location = param->disconnectinfo.location;
        /* if bridge, forward disconnect and return */
+#ifdef TODO
+       feature flag
        if (call->bridge_call)
        {
+               CDEBUG(call, call->ast, "Only signal disconnect via bridge.\n");
                bridge_message_if_bridged(call, message_type, param);
                return;
        }
-       /* release lcr */
-       newparam.disconnectinfo.cause = CAUSE_NORMAL;
-       newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
-       send_message(MESSAGE_RELEASE, call->ref, &newparam);
+#endif
+       /* release lcr with same cause */
+       send_message(MESSAGE_RELEASE, call->ref, param);
        call->ref = 0;
-       /* release asterisk */
-       ast->hangupcause = call->cause;
-       ast_queue_hangup(ast);
        /* change to release state */
        call->state = CHAN_LCR_STATE_RELEASE;
+       /* release asterisk */
+       if (ast)
+       {
+               ast->hangupcause = call->cause;
+               if (call->pbx_started)
+                       ast_queue_hangup(ast);
+               else {
+                       ast_hangup(ast); // call will be destroyed here
+               }
+       }
 }
 
 /*
@@ -564,6 +716,8 @@ static void lcr_in_release(struct chan_call *call, int message_type, union param
 {
        struct ast_channel *ast = call->ast;
 
+       CDEBUG(call, call->ast, "Incomming release from LCR. (cause=%d)\n", param->disconnectinfo.cause);
+
        /* release ref */
        call->ref = 0;
        /* change to release state */
@@ -578,7 +732,11 @@ static void lcr_in_release(struct chan_call *call, int message_type, union param
        if (ast)
        {
                ast->hangupcause = call->cause;
-               ast_queue_hangup(ast);
+               if (call->pbx_started)
+                       ast_queue_hangup(ast);
+               else {
+                       ast_hangup(ast); // call will be destroyed here
+               }
        } else
        {
                free_call(call);
@@ -591,15 +749,28 @@ static void lcr_in_release(struct chan_call *call, int message_type, union param
  */
 static void lcr_in_information(struct chan_call *call, int message_type, union parameter *param)
 {
+       struct ast_channel *ast = call->ast;
        struct ast_frame fr;
        char *p;
 
-       if (!call->ast) return;
+       CDEBUG(call, call->ast, "Incomming information from LCR. (dialing=%d)\n", param->information.id);
+       
+       if (!ast) return;
+
+       /* pbx not started */
+       if (!call->pbx_started)
+       {
+               CDEBUG(call, call->ast, "Asterisk not started, adding digits to number.\n");
+               strncat(ast->exten, param->information.id, AST_MAX_EXTENSION-1);
+               lcr_start_pbx(call, ast, param->information.sending_complete);
+               return;
+       }
        
        /* copy digits */
        p = param->information.id;
        if (call->state == CHAN_LCR_STATE_IN_DIALING && *p)
        {
+               CDEBUG(call, call->ast, "Asterisk is started, sending DTMF frame.\n");
                while (*p)
                {
                        /* send digit to asterisk */
@@ -612,8 +783,10 @@ static void lcr_in_information(struct chan_call *call, int message_type, union p
                }
        }
        /* use bridge to forware message not supported by asterisk */
-       if (call->state == CHAN_LCR_STATE_CONNECT)
+       if (call->state == CHAN_LCR_STATE_CONNECT) {
+               CDEBUG(call, call->ast, "Call is connected, briding.\n");
                bridge_message_if_bridged(call, message_type, param);
+       }
 }
 
 /*
@@ -621,6 +794,8 @@ static void lcr_in_information(struct chan_call *call, int message_type, union p
  */
 static void lcr_in_notify(struct chan_call *call, int message_type, union parameter *param)
 {
+       CDEBUG(call, call->ast, "Incomming notify from LCR. (notify=%d)\n", param->notifyinfo.notify);
+
        if (!call->ast) return;
 
        /* use bridge to forware message not supported by asterisk */
@@ -632,6 +807,8 @@ static void lcr_in_notify(struct chan_call *call, int message_type, union parame
  */
 static void lcr_in_facility(struct chan_call *call, int message_type, union parameter *param)
 {
+       CDEBUG(call, call->ast, "Incomming facility from LCR.\n");
+
        if (!call->ast) return;
 
        /* use bridge to forware message not supported by asterisk */
@@ -655,16 +832,17 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                switch(param->bchannel.type)
                {
                        case BCHANNEL_ASSIGN:
+                       CDEBUG(NULL, NULL, "Received BCHANNEL_ASSIGN message. (handle=%08lx)\n", param->bchannel.handle);
                        if ((bchannel = find_bchannel_handle(param->bchannel.handle)))
                        {
-                               fprintf(stderr, "error: bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
+                               CERROR(NULL, NULL, "bchannel handle %x already assigned.\n", (int)param->bchannel.handle);
                                return(-1);
                        }
                        /* create bchannel */
                        bchannel = alloc_bchannel(param->bchannel.handle);
                        if (!bchannel)
                        {
-                               fprintf(stderr, "error: alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
+                               CERROR(NULL, NULL, "alloc bchannel handle %x failed.\n", (int)param->bchannel.handle);
                                return(-1);
                        }
 
@@ -689,9 +867,13 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                        {
                                bchannel->call = call;
                                call->bchannel = bchannel;
-#warning hier muesen alle stati gesetzt werden falls sie vor dem b-kanal verfügbar waren
-                               if (call->bridge_id)
+#ifdef TODO
+hier muesen alle bchannel-features gesetzt werden (pipeline...) falls sie vor dem b-kanal verfügbar waren
+#endif
+                               if (call->bridge_id) {
+                                       CDEBUG(call, call->ast, "Join bchannel, because call is already bridged.\n");
                                        bchannel_join(bchannel, call->bridge_id);
+                               }
                        }
                        if (bchannel_create(bchannel))
                                bchannel_activate(bchannel, 1);
@@ -703,10 +885,10 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                        break;
 
                        case BCHANNEL_REMOVE:
+                       CDEBUG(NULL, NULL, "Received BCHANNEL_REMOVE message. (handle=%08lx)\n", param->bchannel.handle);
                        if (!(bchannel = find_bchannel_handle(param->bchannel.handle)))
                        {
-                               #warning alle fprintf nach ast_log
-                               fprintf(stderr, "error: bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
+                               CERROR(NULL, NULL, "Bchannel handle %x not assigned.\n", (int)param->bchannel.handle);
                                return(-1);
                        }
                        /* unklink from call and destroy bchannel */
@@ -720,7 +902,7 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                        break;
 
                        default:
-                       fprintf(stderr, "received unknown bchannel message %d\n", param->bchannel.type);
+                       CDEBUG(NULL, NULL, "Received unknown bchannel message %d.\n", param->bchannel.type);
                }
                return(0);
        }
@@ -731,9 +913,10 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                if (param->direction)
                {
                        /* new ref from lcr */
+                       CDEBUG(NULL, NULL, "Received new ref by LCR, of call from LCR. (ref=%ld)\n", ref);
                        if (!ref || find_call_ref(ref))
                        {
-                               fprintf(stderr, "illegal new ref %ld received\n", ref);
+                               CERROR(NULL, NULL, "Illegal new ref %ld received.\n", ref);
                                return(-1);
                        }
                        /* allocate new call instance */
@@ -746,10 +929,12 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                } else
                {
                        /* new ref, as requested from this remote application */
+                       CDEBUG(NULL, NULL, "Received new ref by LCR, as requested from chan_lcr. (ref=%ld)\n", ref);
                        call = find_call_ref(0);
                        if (!call)
                        {
                                /* send release, if ref does not exist */
+                               CDEBUG(NULL, NULL, "No call found, that requests a ref.\n");
                                newparam.disconnectinfo.cause = CAUSE_NORMAL;
                                newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
                                send_message(MESSAGE_RELEASE, ref, &newparam);
@@ -785,13 +970,14 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
        /* check ref */
        if (!ref)
        {
-               fprintf(stderr, "received message %d without ref\n", message_type);
+               CERROR(NULL, NULL, "Received message %d without ref.\n", message_type);
                return(-1);
        }
        call = find_call_ref(ref);
        if (!call)
        {
                /* ignore ref that is not used (anymore) */
+               CDEBUG(NULL, NULL, "Message %d from LCR ignored, because no call instance found.\n", message_type);
                return(0);
        }
 
@@ -838,25 +1024,60 @@ int receive_message(int message_type, unsigned long ref, union parameter *param)
                lcr_in_facility(call, message_type, param);
                break;
 
-               case MESSAGE_PATTERN:
-#warning todo
+               case MESSAGE_PATTERN: // audio available from LCR
                break;
 
-               case MESSAGE_NOPATTERN:
-#warning todo
+               case MESSAGE_NOPATTERN: // audio not available from LCR
                break;
 
-               case MESSAGE_AUDIOPATH:
-#warning todo
+               case MESSAGE_AUDIOPATH: // if remote audio connected or hold
+               call->audiopath = param->audiopath;
                break;
 
                default:
-#warning unhandled
+               CDEBUG(call, call->ast, "Message %d from LCR unhandled.\n", message_type);
                break;
        }
        return(0);
 }
 
+/*
+ * release all calls (due to broken socket)
+ */
+static void release_all_calls(void)
+{
+       struct chan_call *call;
+
+again:
+       call = call_first;
+       while(call)
+       {
+               /* no ast, so we may directly free call */
+               if (!call->ast) {
+                       CDEBUG(call, NULL, "Freeing call, because no Asterisk channel is linked.\n");
+                       free_call(call);
+                       goto again;
+               }
+               /* already in release process */
+               if (call->state == CHAN_LCR_STATE_RELEASE) {
+                       call = call->next;
+                       continue;
+               }
+               /* release or queue release */
+               call->ref = 0;
+               call->state = CHAN_LCR_STATE_RELEASE;
+               if (!call->pbx_started)
+               {
+                       CDEBUG(call, call->ast, "Releasing call, because no Asterisk channel is not started.\n");
+                       ast_hangup(call->ast); // call will be destroyed here
+                       goto again;
+               }
+               CDEBUG(call, call->ast, "Queue call release, because Asterisk channel is running.\n");
+               ast_queue_hangup(call->ast);
+               call = call->next;
+       }
+}
+
 
 /* asterisk handler
  * warning! not thread safe
@@ -871,34 +1092,32 @@ int handle_socket(void)
 
        int sock;
 
-       #warning SOCKET FEHLT!
        /* read from socket */
        len = read(sock, &msg, sizeof(msg));
        if (len == 0)
        {
-               printf("Socket closed\n");
+               CERROR(NULL, NULL, "Socket closed.\n");
                return(-1); // socket closed
        }
        if (len > 0)
        {
                if (len != sizeof(msg))
                {
-                       fprintf(stderr, "Socket short read (%d)\n", len);
+                       CERROR(NULL, NULL, "Socket short read. (len %d)\n", len);
                        return(-1); // socket error
                }
                if (msg.message != ADMIN_MESSAGE)
                {
-                       fprintf(stderr, "Socket received illegal message %d\n", msg.message);
+                       CERROR(NULL, NULL, "Socket received illegal message %d.\n", msg.message);
                        return(-1); // socket error
                }
                receive_message(msg.u.msg.type, msg.u.msg.ref, &msg.u.msg.param);
-               printf("message received %d\n", msg.u.msg.type);
                work = 1;
        } else
        {
                if (errno != EWOULDBLOCK)
                {
-                       fprintf(stderr, "Socket error %d\n", errno);
+                       CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
                        return(-1);
                }
        }
@@ -910,14 +1129,14 @@ int handle_socket(void)
        len = write(sock, &admin->msg, sizeof(msg));
        if (len == 0)
        {
-               printf("Socket closed\n");
+               CERROR(NULL, NULL, "Socket closed.\n");
                return(-1); // socket closed
        }
        if (len > 0)
        {
                if (len != sizeof(msg))
                {
-                       fprintf(stderr, "Socket short write (%d)\n", len);
+                       CERROR(NULL, NULL, "Socket short write. (len %d)\n", len);
                        return(-1); // socket error
                }
                /* free head */
@@ -929,7 +1148,7 @@ int handle_socket(void)
        {
                if (errno != EWOULDBLOCK)
                {
-                       fprintf(stderr, "Socket error %d\n", errno);
+                       CERROR(NULL, NULL, "Socket failed (errno %d).\n", errno);
                        return(-1);
                }
        }
@@ -953,7 +1172,7 @@ int open_socket(void)
        /* open socket */
        if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
        {
-               ast_log(LOG_ERROR, "Failed to create socket.\n");
+               CERROR(NULL, NULL, "Failed to create socket.\n");
                return(sock);
        }
 
@@ -966,7 +1185,7 @@ int open_socket(void)
        if ((conn = connect(sock, (struct sockaddr *)&sock_address, SUN_LEN(&sock_address))) < 0)
        {
                close(sock);
-               ast_log(LOG_ERROR, "Failed to connect to socket \"%s\". Is LCR running?\n", sock_address.sun_path);
+               CERROR(NULL, NULL, "Failed to connect to socket '%s'. Is LCR running?\n", sock_address.sun_path);
                return(conn);
        }
 
@@ -974,7 +1193,7 @@ int open_socket(void)
        if ((ret = ioctl(sock, FIONBIO, (unsigned char *)(&on))) < 0)
        {
                close(sock);
-               ast_log(LOG_ERROR, "Failed to set socket into non-blocking IO.\n");
+               CERROR(NULL, NULL, "Failed to set socket into non-blocking IO.\n");
                return(ret);
        }
 
@@ -996,32 +1215,59 @@ void close_socket(int sock)
 static void *chan_thread(void *arg)
 {
        int work;
+       int ret;
+       union parameter param;
+       time_t retry = 0, now;
+
+       bchannel_pid = getpid();
+       
+       memset(&param, 0, sizeof(union parameter));
+       if (lcr_sock > 0)
+               time(&retry);
 
        ast_mutex_lock(&chan_lock);
 
-       while(!quit)
-       {
+       while(!quit) {
                work = 0;
 
-               /* handle socket */
-               int ret = handle_socket();
-               if (ret < 0)
-                       break;
-               if (ret)
-                       work = 1;
+               if (lcr_sock > 0) {
+                       /* handle socket */
+                       ret = handle_socket();
+                       if (ret < 0) {
+                               CERROR(NULL, NULL, "Handling of socket failed - closing for some seconds.\n");
+                               close_socket(lcr_sock);
+                               lcr_sock = -1;
+                               release_all_calls();
+                               time(&retry);
+                       }
+                       if (ret)
+                               work = 1;
+               } else {
+                       time(&now);
+                       if (retry && now-retry > 5) {
+                               CERROR(NULL, NULL, "Retry to open socket.\n");
+                               retry = 0;
+                               if (!(lcr_sock = open_socket())) {
+                                       time(&retry);
+                               }
+                               work = 1;
+                       }
+                                       
+               }
 
                /* handle mISDN */
                ret = bchannel_handle();
                if (ret)
                        work = 1;
                
-               if (!work)
-               {
+               if (!work) {
                        ast_mutex_unlock(&chan_lock);
                        usleep(30000);
                        ast_mutex_lock(&chan_lock);
                }
        }
+
+       CERROR(NULL, NULL, "Thread exit.\n");
        
        ast_mutex_unlock(&chan_lock);
 
@@ -1031,7 +1277,8 @@ static void *chan_thread(void *arg)
 /*
  * new asterisk instance
  */
-static struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
+static
+struct ast_channel *lcr_request(const char *type, int format, void *data, int *cause)
 {
        union parameter newparam;
        struct ast_channel *ast;
@@ -1039,6 +1286,15 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data,
 
        ast_mutex_lock(&chan_lock);
 
+       CDEBUG(NULL, NULL, "Received request from Asterisk.\n");
+
+       /* if socket is closed */
+       if (lcr_sock < 0)
+       {
+               CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
+               return NULL;
+       }
+       
        /* create call instance */
        call = alloc_call();
        if (!call)
@@ -1050,6 +1306,7 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data,
        ast = ast_channel_alloc(1, AST_STATE_RESERVED, NULL, NULL, "", NULL, "", 0, "%s/%d", lcr_type, ++glob_channel);
        if (!ast)
        {
+               CERROR(NULL, NULL, "Failed to create Asterisk channel.\n");
                free_call(call);
                /* failed to create instance */
                return NULL;
@@ -1059,13 +1316,18 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data,
        call->ast = ast;
        ast->tech = &lcr_tech;
        /* configure channel */
+#ifdef TODO
        snprintf(ast->name, sizeof(ast->name), "%s/%d", lcr_type, ++glob_channel);
        ast->name[sizeof(ast->name)-1] = '\0';
-#warning todo
-#if 0
-       ast->nativeformat = configfile->lawformat;
+#endif
+#ifdef TODO
+       ast->nativeformats = configfile->lawformat;
        ast->readformat = ast->rawreadformat = configfile->lawformat;
        ast->writeformat = ast->rawwriteformat = configfile->lawformat;
+#else
+       ast->nativeformats = AST_FORMAT_ALAW;
+       ast->readformat = ast->rawreadformat = AST_FORMAT_ALAW;
+       ast->writeformat = ast->rawwriteformat = AST_FORMAT_ALAW;
 #endif
        ast->hangupcause = 0;
        /* send MESSAGE_NEWREF */
@@ -1085,48 +1347,52 @@ static struct ast_channel *lcr_request(const char *type, int format, void *data,
  */
 static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
 {
-        struct chan_call *call=ast->tech_pvt;
-
-        if (!call) return -1;
+        struct chan_call *call;
 
        ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               CERROR(NULL, ast, "Received call from Asterisk, but no call instance exists.\n");
+               ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
 
-#warning       hier muss noch
-#if 0
-        ast_copy_string(buf, dest, sizeof(buf)-1);
-        p=buf;
-        port_str=strsep(&p, "/");
-        dad=strsep(&p, "/");
-#endif
+       CDEBUG(call, ast, "Received call from Asterisk.\n");
+
+       call->pbx_started = 1;
 
        /* send setup message, if we already have a callref */
        if (call->ref)
                send_setup_to_lcr(call);
 
-//        if (lcr_debug)
-  //              ast_verbose("Call: ext:%s dest:(%s) -> dad(%s) \n", ast->exten,dest, dad);
-
        ast_mutex_unlock(&chan_lock);
        return 0; 
 }
 
 static int lcr_digit(struct ast_channel *ast, char digit)
 {
-        struct chan_call *call = ast->tech_pvt;
+        struct chan_call *call;
        union parameter newparam;
        char buf[]="x";
 
-       if (!call) return -1;
-
        /* only pass IA5 number space */
        if (digit > 126 || digit < 32)
                return 0;
 
        ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               CERROR(NULL, ast, "Received digit from Asterisk, but no call instance exists.\n");
+               ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
+
+       CDEBUG(call, ast, "Received digit Asterisk.\n");
 
        /* send information or queue them */
        if (call->ref && call->state == CHAN_LCR_STATE_OUT_DIALING)
        {
+               CDEBUG(call, ast, "Sending digit to LCR, because we are in dialing state.\n");
                memset(&newparam, 0, sizeof(union parameter));
                newparam.information.id[0] = digit;
                newparam.information.id[1] = '\0';
@@ -1135,6 +1401,7 @@ static int lcr_digit(struct ast_channel *ast, char digit)
        if (!call->ref
         && (call->state == CHAN_LCR_STATE_OUT_PREPARE || call->state == CHAN_LCR_STATE_OUT_SETUP));
        {
+               CDEBUG(call, ast, "Queue digits, because we are in setup/dialing state and have no ref yet.\n");
                *buf = digit;
                strncat(call->dialque, buf, strlen(call->dialque)-1);
        }
@@ -1147,12 +1414,18 @@ static int lcr_digit(struct ast_channel *ast, char digit)
 static int lcr_answer(struct ast_channel *ast)
 {
        union parameter newparam;
-        struct chan_call *call = ast->tech_pvt;
-       
-       if (!call) return -1;
+        struct chan_call *call;
 
        ast_mutex_lock(&chan_lock);
-
+        call = ast->tech_pvt;
+        if (!call) {
+               CERROR(NULL, ast, "Received answer from Asterisk, but no call instance exists.\n");
+               ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
+       
+       CDEBUG(call, ast, "Received answer from Asterisk.\n");
+               
        /* copy connectinfo, if bridged */
        if (call->bridge_call)
                memcpy(&call->connectinfo, &call->bridge_call->connectinfo, sizeof(struct connect_info));
@@ -1170,24 +1443,39 @@ static int lcr_answer(struct ast_channel *ast)
 static int lcr_hangup(struct ast_channel *ast)
 {
        union parameter newparam;
-        struct chan_call *call = ast->tech_pvt;
+        struct chan_call *call;
+       pthread_t tid = pthread_self();
+
+       if (!pthread_equal(tid, chan_tid))
+               ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
+               if (!pthread_equal(tid, chan_tid))
+                       ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
 
-       if (!call)
-               return 0;
+       if (!pthread_equal(tid, chan_tid))
+               CDEBUG(call, ast, "Received hangup from Asterisk thread.\n");
+       else
+               CDEBUG(call, ast, "Received hangup from LCR thread.\n");
 
-       ast_mutex_lock(&chan_lock);
        /* disconnect asterisk, maybe not required */
        ast->tech_pvt = NULL;
+       ast->fds[0] = -1;
        if (call->ref)
        {
                /* release */
+               CDEBUG(call, ast, "Releasing ref and freeing call instance.\n");
                memset(&newparam, 0, sizeof(union parameter));
                newparam.disconnectinfo.cause = CAUSE_RESSOURCEUNAVAIL;
                newparam.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
                send_message(MESSAGE_RELEASE, call->ref, &newparam);
                /* remove call */
                free_call(call);
-               ast_mutex_unlock(&chan_lock);
+               if (!pthread_equal(tid, chan_tid))
+                       ast_mutex_unlock(&chan_lock);
                return 0;
        } else
        {
@@ -1195,22 +1483,41 @@ static int lcr_hangup(struct ast_channel *ast)
                if (call->state == CHAN_LCR_STATE_RELEASE)
                {
                        /* we get the response to our release */
+                       CDEBUG(call, ast, "Freeing call instance, because we have no ref AND we are requesting no ref.\n");
                        free_call(call);
                } else
                {
                        /* during prepare, we change to release state */
+                       CDEBUG(call, ast, "We must wait until we received our ref, until we can free call instance.\n");
                        call->state = CHAN_LCR_STATE_RELEASE;
                }
        } 
-       ast_mutex_unlock(&chan_lock);
+       if (!pthread_equal(tid, chan_tid))
+               ast_mutex_unlock(&chan_lock);
        return 0;
 }
 
 static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
 {
-        struct chan_call *call = ast->tech_pvt;
-       if (!call) return 0;
+        struct chan_call *call;
+       unsigned char buffer[1024], *s, *d = buffer;
+       int i, ii;
+
        ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
+       if (call->bchannel && ((ii = f->samples)))
+       {
+               if (ii > sizeof(buffer))
+                       ii = sizeof(buffer);
+               s = f->data;
+               for (i = 0; i < ii; i++)
+                       *d++ = flip_bits[*s++];
+               bchannel_transmit(call->bchannel, buffer, ii);
+       }
        ast_mutex_unlock(&chan_lock);
        return 0;
 }
@@ -1218,25 +1525,57 @@ static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
 
 static struct ast_frame *lcr_read(struct ast_channel *ast)
 {
-        struct chan_call *call = ast->tech_pvt;
-       if (!call) return 0;
+        struct chan_call *call;
+       int i, len;
+       unsigned char *p;
+
        ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               ast_mutex_unlock(&chan_lock);
+               return NULL;
+       }
+       len = read(call->pipe[0], call->read_buff, sizeof(call->read_buff));
+       if (len <= 0)
+               return NULL;
+
+       p = call->read_buff;
+       for (i = 0; i < len; i++) {
+               *p = flip_bits[*p];
+               p++;
+       }
+
+       call->read_fr.frametype = AST_FRAME_VOICE;
+#ifdef TODO
+       format aus config
+#endif
+       call->read_fr.subclass = AST_FORMAT_ALAW;
+       call->read_fr.datalen = len;
+       call->read_fr.samples = len;
+       call->read_fr.delivery = ast_tv(0,0);
+       call->read_fr.data = call->read_buff;
        ast_mutex_unlock(&chan_lock);
-       return 0;
+
+       return &call->read_fr;
 }
 
 static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, size_t datalen)
 {
-        struct chan_call *call = ast->tech_pvt;
        union parameter newparam;
         int res = -1;
-
-       if (!call) return -1;
+        struct chan_call *call;
 
        ast_mutex_lock(&chan_lock);
+        call = ast->tech_pvt;
+        if (!call) {
+               CERROR(NULL, ast, "Received indicate from Asterisk, but no call instance exists.\n");
+               ast_mutex_unlock(&chan_lock);
+               return -1;
+       }
 
         switch (cond) {
                 case AST_CONTROL_BUSY:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_BUSY from Asterisk.\n");
                        /* send message to lcr */
                        memset(&newparam, 0, sizeof(union parameter));
                        newparam.disconnectinfo.cause = 17;
@@ -1248,10 +1587,12 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                        ast_mutex_unlock(&chan_lock);
                         return 0;
                 case AST_CONTROL_CONGESTION:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_CONGESTION from Asterisk.\n");
                        /* return */
                        ast_mutex_unlock(&chan_lock);
                         return -1;
                 case AST_CONTROL_RINGING:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_RINGING from Asterisk.\n");
                        /* send message to lcr */
                        memset(&newparam, 0, sizeof(union parameter));
                        send_message(MESSAGE_ALERTING, call->ref, &newparam);
@@ -1269,12 +1610,14 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                         res = -1;
                         break;
                 case AST_CONTROL_HOLD:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_HOLD from Asterisk.\n");
                        /* send message to lcr */
                        memset(&newparam, 0, sizeof(union parameter));
                        newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_HOLD;
                        send_message(MESSAGE_NOTIFY, call->ref, &newparam);
                         break;
                 case AST_CONTROL_UNHOLD:
+                       CDEBUG(call, ast, "Received indicate AST_CONTROL_UNHOLD from Asterisk.\n");
                        /* send message to lcr */
                        memset(&newparam, 0, sizeof(union parameter));
                        newparam.notifyinfo.notify = INFO_NOTIFY_REMOTE_RETRIEVAL;
@@ -1282,7 +1625,7 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                         break;
 
                 default:
-                        ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, ast->name);
+                       CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
                        /* return */
                        ast_mutex_unlock(&chan_lock);
                         return -1;
@@ -1307,36 +1650,27 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        int                     to = -1;
        struct ast_frame        *f;
        int                     bridge_id;
-#if 0
-       if (config nobridge) {
-               ast_log(LOG_NOTICE, "Falling back to Asterisk bridging\n");
-               return AST_BRIDGE_FAILED;
-       }
 
-       if (! (flags&AST_BRIDGE_DTMF_CHANNEL_0) )
-               call1->ignore_dtmf=1;
+       CDEBUG(NULL, NULL, "Received briding request from Asterisk.\n");
        
-       if (! (flags&AST_BRIDGE_DTMF_CHANNEL_1) )
-               call2->ignore_dtmf=1;
-#endif
-
        /* join via dsp (if the channels are currently open) */
-       bridge_id = new_bridge_id();
        ast_mutex_lock(&chan_lock);
+       bridge_id = new_bridge_id();
        call1 = ast1->tech_pvt;
        call2 = ast2->tech_pvt;
-       if (call1)
+       if (call1 && call2)
        {
                call1->bridge_id = bridge_id;
                if (call1->bchannel)
                        bchannel_join(call1->bchannel, bridge_id);
+               call1->bridge_call = call2;
        }
        if (call2)
        {
                call2->bridge_id = bridge_id;
                if (call2->bchannel)
                        bchannel_join(call2->bchannel, bridge_id);
+               call2->bridge_call = call1;
        }
        ast_mutex_unlock(&chan_lock);
        
@@ -1344,7 +1678,7 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
                who = ast_waitfor_n(carr, 2, &to);
 
                if (!who) {
-                       ast_log(LOG_NOTICE,"misdn_bridge: empty read, breaking out\n");
+                       CDEBUG(NULL, NULL, "Empty read on bridge, breaking out.\n");
                        break;
                }
                f = ast_read(who);
@@ -1362,13 +1696,6 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
                        break;
                }
        
-#warning kann einfach gesendet werden. dsp slittet automatisch, wenn frames kommen, bis der fifo leer ist.
-#if 0
-               if (f->frametype == AST_FRAME_VOICE) {
-       
-                       continue;
-               }
-#endif
 
                if (who == ast1) {
                        ast_write(ast2,f);
@@ -1379,6 +1706,8 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
     
        }
        
+       CDEBUG(NULL, NULL, "Releasing bride.\n");
+
        /* split channels */
        ast_mutex_lock(&chan_lock);
        call1 = ast1->tech_pvt;
@@ -1388,12 +1717,18 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
                call1->bridge_id = 0;
                if (call1->bchannel)
                        bchannel_join(call1->bchannel, 0);
+               if (call1->bridge_call)
+                       call1->bridge_call->bridge_call = NULL;
+               call1->bridge_call = NULL;
        }
        if (call2)
        {
                call2->bridge_id = 0;
                if (call2->bchannel)
                        bchannel_join(call2->bchannel, 0);
+               if (call2->bridge_call)
+                       call2->bridge_call->bridge_call = NULL;
+               call2->bridge_call = NULL;
        }
        ast_mutex_unlock(&chan_lock);
        
@@ -1401,9 +1736,13 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        return AST_BRIDGE_COMPLETE;
 }
 static struct ast_channel_tech lcr_tech = {
-       .type=lcr_type,
+       .type="LCR",
        .description="Channel driver for connecting to Linux-Call-Router",
+#ifdef TODO
+       law from config
+#else
        .capabilities=AST_FORMAT_ALAW,
+#endif
        .requester=lcr_request,
        .send_digit_begin=lcr_digit,
        .call=lcr_call,
@@ -1512,27 +1851,28 @@ static struct ast_cli_entry cli_port_unload =
  */
 int load_module(void)
 {
-//     ast_mutex_init(&release_lock);
+       int i;
 
-//     lcr_cfg_update_ptp();
+       for (i = 0; i < 256; i++)
+               flip_bits[i] = (i>>7) | ((i>>5)&2) | ((i>>3)&4) | ((i>>1)&8)
+                           || (i<<7) | ((i&2)<<5) | ((i&4)<<3) | ((i&8)<<1);
 
        ast_mutex_init(&chan_lock);
+       ast_mutex_init(&log_lock);
        
        if (!(lcr_sock = open_socket())) {
-               ast_log(LOG_ERROR, "Unable to connect\n");
-               lcr_sock = -1;
                /* continue with closed socket */
        }
 
        if (!bchannel_initialize()) {
-               ast_log(LOG_ERROR, "Unable to open mISDN device\n");
+               CERROR(NULL, NULL, "Unable to open mISDN device\n");
                close_socket(lcr_sock);
                return -1;
        }
        mISDN_created = 1;
 
        if (ast_channel_register(&lcr_tech)) {
-               ast_log(LOG_ERROR, "Unable to register channel class\n");
+               CERROR(NULL, NULL, "Unable to register channel class\n");
                bchannel_deinitialize();
                close_socket(lcr_sock);
                return -1;
@@ -1567,12 +1907,11 @@ int load_module(void)
        
        lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
 
-       chan_lcr_log(0, 0, "-- mISDN Channel Driver Registred -- (BE AWARE THIS DRIVER IS EXPERIMENTAL!)\n");
 =======
        //lcr_cfg_get( 0, LCR_GEN_TRACEFILE, global_tracefile, BUFFERSIZE);
 #endif
 
-       quit = 1;       
+       quit = 0;       
        if ((pthread_create(&chan_tid, NULL, chan_thread, NULL)<0))
        {
                /* failed to create thread */
@@ -1587,7 +1926,7 @@ int load_module(void)
 int unload_module(void)
 {
        /* First, take us out of the channel loop */
-       ast_log(LOG_VERBOSE, "-- Unregistering mISDN Channel Driver --\n");
+       CDEBUG(NULL, NULL, "-- Unregistering mISDN Channel Driver --\n");
 
        quit = 1;
        pthread_join(chan_tid, NULL);   
@@ -1607,13 +1946,14 @@ int unload_module(void)
        return 0;
 }
 
-static int reload_module(void)
+int reload_module(void)
 {
 //     reload_config();
        return 0;
 }
 
-
+#ifdef TODO
+mutex init fehlt noch
 ast_mutex_t usecnt_lock;
 int usecnt;
 
@@ -1625,6 +1965,7 @@ int usecount(void)
        ast_mutex_unlock(&usecnt_lock);
        return res;
 }
+#endif
 
 
 char *desc="Channel driver for lcr";
@@ -1639,12 +1980,4 @@ char *key(void)
        return ASTERISK_GPL_KEY;
 }
 
-#define AST_MODULE "chan_lcr"
-AST_MODULE_INFO(ASTERISK_GPL_KEY,
-                                AST_MODFLAG_DEFAULT,
-                                "Channel driver for LCR",
-                                .load = load_module,
-                                .unload = unload_module,
-                                .reload = reload_module,
-                           );