[chan_lcr] Fixed caller ID by clearing ast->cid first
[lcr.git] / chan_lcr.c
index 09083c0..25515d0 100644 (file)
@@ -223,13 +223,6 @@ struct admin_list {
 
 static struct ast_channel_tech lcr_tech;
 
-void lock_debug(char *text)
-{
-       pthread_t tid = pthread_self();
-//     printf("%s|%03x\n", text, ((tid>>6) | (tid>>3) | tid) & 0xfff); fflush(stdout);
-       printf("%s|%x", text, tid); fflush(stdout);
-}
-
 /*
  * logging
  */
@@ -240,7 +233,6 @@ void chan_lcr_log(int type, const char *file, int line, const char *function, st
        char ast_text[128] = "NULL";
        va_list args;
 
-       lock_debug("L");
        ast_mutex_lock(&log_lock);
 
        va_start(args,fmt);
@@ -253,11 +245,11 @@ void chan_lcr_log(int type, const char *file, int line, const char *function, st
        if (ast)
                strncpy(ast_text, ast->name, sizeof(ast_text)-1);
        ast_text[sizeof(ast_text)-1] = '\0';
-       
-       ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
+
+//     ast_log(type, file, line, function, "[call=%s ast=%s] %s", call_text, ast_text, buffer);
+       printf("[call=%s ast=%s] %s", call_text, ast_text, buffer);
 
        ast_mutex_unlock(&log_lock);
-       lock_debug("l");
 }
 
 /*
@@ -621,7 +613,7 @@ static void send_setup_to_lcr(struct chan_call *call)
 {
        union parameter newparam;
        struct ast_channel *ast = call->ast;
-       const char *tmp;
+//     const char *tmp;
 
        if (!call->ast || !call->ref)
                return;
@@ -674,9 +666,10 @@ static void send_setup_to_lcr(struct chan_call *call)
                default:
                newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
        }
-       tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
-       if (tmp && *tmp)
-               ast->transfercapability = atoi(tmp);
+#warning DISABLED DUE TO DOUBLE LOCKING PROBLEM
+//     tmp = pbx_builtin_getvar_helper(ast, "LCR_TRANSFERCAPABILITY");
+//     if (tmp && *tmp)
+//             ast->transfercapability = atoi(tmp);
        newparam.setup.capainfo.bearer_capa = ast->transfercapability;
        newparam.setup.capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
        if (call->hdlc)
@@ -888,8 +881,11 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet
                strncpy(ast->context, param->setup.context, AST_MAX_CONTEXT-1);
        else
                strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
+       memset(&ast->cid, 0, sizeof(ast->cid));
        if (param->setup.callerinfo.id[0])
                ast->cid.cid_num = strdup(param->setup.callerinfo.id);
+       if (param->setup.callerinfo.id2[0])
+               ast->cid.cid_ani = strdup(param->setup.callerinfo.id2);
        if (param->setup.callerinfo.name[0])
                ast->cid.cid_name = strdup(param->setup.callerinfo.name);
        if (param->setup.redirinfo.id[0])
@@ -1668,13 +1664,18 @@ static void handle_queue()
        struct ast_frame fr;
        char *p;
 
+again:
        call = call_first;
        while(call) {
                p = call->queue_string;
                ast = call->ast;
                if (*p && ast) {
-                       lock_debug("A1");
-                       ast_channel_lock(ast);
+                       if (ast_channel_trylock(ast)) {
+                               ast_mutex_unlock(&chan_lock);
+                               usleep(1);
+                               ast_mutex_lock(&chan_lock);
+                               goto again;
+                       }
                        while(*p) {
                                switch (*p) {
                                case 'T':
@@ -1731,7 +1732,6 @@ static void handle_queue()
                        }
                        call->queue_string[0] = '\0';
                        ast_channel_unlock(ast);
-                       lock_debug("a1");
                }
                call = call->next;
        }
@@ -1748,14 +1748,12 @@ static int handle_retry(struct lcr_timer *timer, void *instance, int index)
 
 void lock_chan(void)
 {
-       lock_debug("C");
        ast_mutex_lock(&chan_lock);
 }
 
 void unlock_chan(void)
 {
        ast_mutex_unlock(&chan_lock);
-       lock_debug("c");
 }
 
 /* chan_lcr thread */
@@ -1777,7 +1775,6 @@ static void *chan_thread(void *arg)
        /* open socket the first time */
        handle_retry(NULL, NULL, 0);
 
-       lock_debug("A2");
        ast_mutex_lock(&chan_lock);
 
        while(!quit) {
@@ -1796,7 +1793,6 @@ static void *chan_thread(void *arg)
        CERROR(NULL, NULL, "Thread exit.\n");
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a2");
 
        return NULL;
 }
@@ -1811,7 +1807,6 @@ struct ast_channel *lcr_request(const char *type, int format, void *data, int *c
        struct ast_channel *ast;
         struct chan_call *call;
 
-       lock_debug("A3");
        ast_mutex_lock(&chan_lock);
        CDEBUG(NULL, NULL, "Received request from Asterisk. (data=%s)\n", (char *)data);
 
@@ -1819,7 +1814,6 @@ struct ast_channel *lcr_request(const char *type, int format, void *data, int *c
        if (lcr_sock < 0) {
                CERROR(NULL, NULL, "Rejecting call from Asterisk, because LCR not running.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a3");
                return NULL;
        }
 
@@ -1828,7 +1822,6 @@ struct ast_channel *lcr_request(const char *type, int format, void *data, int *c
        if (!call) {
                /* failed to create instance */
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a3");
                return NULL;
        }
 
@@ -1847,7 +1840,6 @@ struct ast_channel *lcr_request(const char *type, int format, void *data, int *c
                free_call(call);
                /* failed to create instance */
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a3");
                return NULL;
        }
        ast->tech = &lcr_tech;
@@ -1893,7 +1885,6 @@ struct ast_channel *lcr_request(const char *type, int format, void *data, int *c
        apply_opt(call, (char *)opt);
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a3");
        return ast;
 }
 
@@ -1905,7 +1896,6 @@ static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
        union parameter newparam;
         struct chan_call *call;
 
-       lock_debug("A4");
        ast_mutex_lock(&chan_lock);
         call = ast->tech_pvt;
         
@@ -1917,7 +1907,6 @@ static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
        if (!call) {
                CERROR(NULL, ast, "Received call from Asterisk, but call instance does not exist.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a4");
                return -1;
        }
 
@@ -1958,7 +1947,6 @@ static int lcr_call(struct ast_channel *ast, char *dest, int timeout)
                        sizeof(call->cid_rdnis)-1);
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a4");
        return 0; 
 }
 
@@ -1992,7 +1980,7 @@ static void send_digit_to_chan(struct ast_channel * ast, char digit )
                 ast_playtones_start(ast,0,dtmf_tones[15], 0);
         else {
                 /* not handled */
-                ast_log(LOG_DEBUG, "Unable to handle DTMF tone "
+               CDEBUG(NULL, ast, "Unable to handle DTMF tone "
                        "'%c' for '%s'\n", digit, ast->name);
         }
 }
@@ -2016,13 +2004,11 @@ static int lcr_digit(struct ast_channel *ast, char digit)
        if (digit > 126 || digit < 32)
                return 0;
 
-       lock_debug("A5");
        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);
-               lock_debug("a5");
                return -1;
        }
 
@@ -2049,7 +2035,6 @@ static int lcr_digit(struct ast_channel *ast, char digit)
        }
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a5");
 
 #ifdef LCR_FOR_ASTERISK
        return 0;
@@ -2061,7 +2046,6 @@ static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int durat
         struct chan_call *call;
 #endif
 
-       lock_debug("A6");
        ast_mutex_lock(&chan_lock);
 
         call = ast->tech_pvt;
@@ -2071,7 +2055,6 @@ static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int durat
                       "Received digit from Asterisk, "
                       "but no call instance exists.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a6");
                return -1;
        }
 
@@ -2082,7 +2065,6 @@ static int lcr_digit_end(struct ast_channel *ast, char digit, unsigned int durat
        }
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a6");
 
        if (inband_dtmf) {
                CDEBUG(call, ast, "-> sending '%c' inband.\n", digit);
@@ -2097,13 +2079,11 @@ static int lcr_answer(struct ast_channel *ast)
        union parameter newparam;
         struct chan_call *call;
 
-       lock_debug("A7");
        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);
-               lock_debug("a7");
                return -1;
        }
        
@@ -2132,7 +2112,6 @@ static int lcr_answer(struct ast_channel *ast)
 //     send_message(MESSAGE_ENABLEKEYPAD, call->ref, &newparam);
        
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a7");
         return 0;
 }
 
@@ -2142,7 +2121,6 @@ static int lcr_hangup(struct ast_channel *ast)
        pthread_t tid = pthread_self();
 
        if (!pthread_equal(tid, chan_tid)) {
-               lock_debug("H");
                ast_mutex_lock(&chan_lock);
        }
         call = ast->tech_pvt;
@@ -2150,7 +2128,6 @@ static int lcr_hangup(struct ast_channel *ast)
                CERROR(NULL, ast, "Received hangup from Asterisk, but no call instance exists.\n");
                if (!pthread_equal(tid, chan_tid)) {
                        ast_mutex_unlock(&chan_lock);
-                       lock_debug("h");
                }
                return -1;
        }
@@ -2174,7 +2151,6 @@ static int lcr_hangup(struct ast_channel *ast)
                free_call(call);
                if (!pthread_equal(tid, chan_tid)) {
                        ast_mutex_unlock(&chan_lock);
-                       lock_debug("h");
                }
                return 0;
        } else {
@@ -2192,7 +2168,6 @@ static int lcr_hangup(struct ast_channel *ast)
        } 
        if (!pthread_equal(tid, chan_tid)) {
                ast_mutex_unlock(&chan_lock);
-               lock_debug("h");
        }
        return 0;
 }
@@ -2206,18 +2181,15 @@ static int lcr_write(struct ast_channel *ast, struct ast_frame *f)
        if (!(f->subclass & ast->nativeformats))
                CDEBUG(NULL, ast, "Unexpected format.\n");
        
-       lock_debug("A8");
        ast_mutex_lock(&chan_lock);
         call = ast->tech_pvt;
        if (!call) {
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a8");
                return -1;
        }
        if (call->bchannel && f->samples)
                bchannel_transmit(call->bchannel, *((unsigned char **)&(f->data)), f->samples);
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a8");
        return 0;
 }
 
@@ -2225,14 +2197,12 @@ 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;
-       int len;
+       int len = 0;
 
-       lock_debug("A9");
        ast_mutex_lock(&chan_lock);
         call = ast->tech_pvt;
        if (!call) {
                ast_mutex_unlock(&chan_lock);
-               lock_debug("a9");
                return NULL;
        }
        if (call->pipe[0] > -1) {
@@ -2247,7 +2217,6 @@ static struct ast_frame *lcr_read(struct ast_channel *ast)
                }
                if (len < 0 && errno == EAGAIN) {
                        ast_mutex_unlock(&chan_lock);
-                       lock_debug("a9");
 
                        #ifdef LCR_FOR_ASTERISK
                        return &ast_null_frame;
@@ -2263,12 +2232,10 @@ static struct ast_frame *lcr_read(struct ast_channel *ast)
                        call->pipe[0] = -1;
                        global_change = 1;
                        ast_mutex_unlock(&chan_lock);
-                       lock_debug("a9");
                        return NULL;
                } else if (call->rebuffer && call->framepos < 160) {
                        /* Not a complete frame, so we send a null-frame */
                        ast_mutex_unlock(&chan_lock);
-                       lock_debug("a9");
                        return &ast_null_frame;
                }
        }
@@ -2286,7 +2253,6 @@ static struct ast_frame *lcr_read(struct ast_channel *ast)
        call->read_fr.delivery = ast_tv(0,0);
        *((unsigned char **)&(call->read_fr.data)) = call->read_buff;
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a9");
 
        return &call->read_fr;
 }
@@ -2298,13 +2264,11 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
         struct chan_call *call;
        const struct tone_zone_sound *ts = NULL;
 
-       lock_debug("A0");
        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);
-               lock_debug("a0");
                return -1;
        }
 
@@ -2435,7 +2399,6 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
 
        /* return */
        ast_mutex_unlock(&chan_lock);
-       lock_debug("a0");
         return res;
 }
 
@@ -2450,20 +2413,17 @@ static int lcr_fixup(struct ast_channel *oldast, struct ast_channel *ast)
                return -1;
        }
 
-       lock_debug("Af");
        ast_mutex_lock(&chan_lock);
        call = ast->tech_pvt;
        if (!call) {
                CERROR(NULL, ast, "Received fixup from Asterisk, but no call instance exists.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("af");
                return -1;
        }
 
        CDEBUG(call, ast, "Received fixup from Asterisk.\n");
        call->ast = ast;
        ast_mutex_unlock(&chan_lock);
-       lock_debug("af");
        return 0;
 }
 
@@ -2475,13 +2435,11 @@ static int lcr_send_text(struct ast_channel *ast, const char *text)
         struct chan_call *call;
        union parameter newparam;
 
-       lock_debug("At");
        ast_mutex_lock(&chan_lock);
        call = ast->tech_pvt;
        if (!call) {
                CERROR(NULL, ast, "Received send_text from Asterisk, but no call instance exists.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("at");
                return -1;
        }
 
@@ -2490,7 +2448,6 @@ static int lcr_send_text(struct ast_channel *ast, const char *text)
        strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
        send_message(MESSAGE_NOTIFY, call->ref, &newparam);
        ast_mutex_unlock(&chan_lock);
-       lock_debug("at");
        return 0;
 }
 
@@ -2515,14 +2472,12 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        carr[1] = ast2;
 
        /* join via dsp (if the channels are currently open) */
-       lock_debug("Ab");
        ast_mutex_lock(&chan_lock);
        call1 = ast1->tech_pvt;
        call2 = ast2->tech_pvt;
        if (!call1 || !call2) {
                CDEBUG(NULL, NULL, "Bridge, but we don't have two call instances, exitting.\n");
                ast_mutex_unlock(&chan_lock);
-               lock_debug("ab");
                return AST_BRIDGE_COMPLETE;
        }
 
@@ -2590,7 +2545,6 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        }
        
        ast_mutex_unlock(&chan_lock);
-       lock_debug("ab");
        
        while(1) {
                to = -1;
@@ -2633,7 +2587,6 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        CDEBUG(NULL, NULL, "Releasing bridge.\n");
 
        /* split channels */
-       lock_debug("Ab");
        ast_mutex_lock(&chan_lock);
        call1 = ast1->tech_pvt;
        call2 = ast2->tech_pvt;
@@ -2655,7 +2608,6 @@ enum ast_bridge_result lcr_bridge(struct ast_channel *ast1,
        call2->bridge_call = NULL;
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("ab");
        return AST_BRIDGE_COMPLETE;
 }
 static struct ast_channel_tech lcr_tech = {
@@ -2786,7 +2738,6 @@ static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
 {
        struct chan_call *call;
 
-       lock_debug("Ae");
        ast_mutex_lock(&chan_lock);
 
        #ifdef LCR_FOR_ASTERISK
@@ -2818,7 +2769,6 @@ static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
                CERROR(NULL, ast, "lcr_config app not called by chan_lcr channel.\n");
 
        ast_mutex_unlock(&chan_lock);
-       lock_debug("ae");
        return 0;
 }
 
@@ -2989,11 +2939,9 @@ int usecount(void)
 hae
 {
        int res;
-       lock_debug("U");
        ast_mutex_lock(&usecnt_lock);
        res = usecnt;
        ast_mutex_unlock(&usecnt_lock);
-       lock_debug("u");
        return res;
 }
 #endif