[chan_lcr] Fixed uninitialized variable in ast_read()
[lcr.git] / chan_lcr.c
index d0993b7..110c337 100644 (file)
@@ -245,8 +245,9 @@ 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);
 }
@@ -496,6 +497,14 @@ void apply_opt(struct chan_call *call, char *data)
                        if (!call->nodsp)
                                call->nodsp = 1;
                        break;
+               case 'q':
+                       if (opt[1] == '\0') {
+                               CERROR(call, call->ast, "Option 'q' (queue) expects parameter.\n", opt);
+                               break;
+                       }
+                       CDEBUG(call, call->ast, "Option 'q' (queue).\n");
+                       call->nodsp_queue = atoi(opt+1);
+                       break;
                case 'e':
                        if (opt[1] == '\0') {
                                CERROR(call, call->ast, "Option 'e' (echo cancel) expects parameter.\n", opt);
@@ -591,7 +600,7 @@ void apply_opt(struct chan_call *call, char *data)
        /* re-open, if bchannel is created */
        if (call->bchannel && call->bchannel->b_sock > -1) {
                bchannel_destroy(call->bchannel);
-               if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0)))
+               if (bchannel_create(call->bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
                        bchannel_activate(call->bchannel, 1);
        }
 }
@@ -604,6 +613,7 @@ static void send_setup_to_lcr(struct chan_call *call)
 {
        union parameter newparam;
        struct ast_channel *ast = call->ast;
+//     const char *tmp;
 
        if (!call->ast || !call->ref)
                return;
@@ -656,6 +666,10 @@ static void send_setup_to_lcr(struct chan_call *call)
                default:
                newparam.setup.callerinfo.ntype = INFO_NTYPE_UNKNOWN;
        }
+#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)
@@ -869,10 +883,12 @@ static void lcr_in_setup(struct chan_call *call, int message_type, union paramet
                strncpy(ast->context, param->setup.callerinfo.interface, AST_MAX_CONTEXT-1);
        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])
-               ast->cid.cid_name = strdup(numberrize_callerinfo(param->setup.callerinfo.id, param->setup.callerinfo.ntype, options.national, options.international));
+               ast->cid.cid_rdnis = strdup(numberrize_callerinfo(param->setup.redirinfo.id, param->setup.redirinfo.ntype, options.national, options.international));
        switch (param->setup.callerinfo.present) {
                case INFO_PRESENT_ALLOWED:
                        ast->cid.cid_pres = AST_PRES_ALLOWED;
@@ -1287,7 +1303,7 @@ int receive_message(int message_type, unsigned int ref, union parameter *param)
                                        bchannel_join(bchannel, call->bridge_id);
                                }
                                /* create only, if call exists, othewhise it bchannel is freed below... */
-                               if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0)))
+                               if (bchannel_create(bchannel, ((call->nodsp || call->faxdetect > 0)?1:0) + ((call->hdlc)?2:0), call->nodsp_queue))
                                        bchannel_activate(bchannel, 1);
                        }
                        /* acknowledge */
@@ -1647,12 +1663,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) {
-                       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':
@@ -1957,7 +1979,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);
         }
 }
@@ -2097,13 +2119,15 @@ static int lcr_hangup(struct ast_channel *ast)
         struct chan_call *call;
        pthread_t tid = pthread_self();
 
-       if (!pthread_equal(tid, chan_tid))
+       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))
+               if (!pthread_equal(tid, chan_tid)) {
                        ast_mutex_unlock(&chan_lock);
+               }
                return -1;
        }
 
@@ -2124,8 +2148,9 @@ static int lcr_hangup(struct ast_channel *ast)
                        send_release_and_import(call, CAUSE_NORMAL, LOCATION_PRIVATE_LOCAL);
                /* remove call */
                free_call(call);
-               if (!pthread_equal(tid, chan_tid))
+               if (!pthread_equal(tid, chan_tid)) {
                        ast_mutex_unlock(&chan_lock);
+               }
                return 0;
        } else {
                /* ref is not set, due to prepare setup or release */
@@ -2140,8 +2165,9 @@ static int lcr_hangup(struct ast_channel *ast)
                        call->ast = NULL;
                }
        } 
-       if (!pthread_equal(tid, chan_tid))
+       if (!pthread_equal(tid, chan_tid)) {
                ast_mutex_unlock(&chan_lock);
+       }
        return 0;
 }
 
@@ -2170,7 +2196,7 @@ 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;
 
        ast_mutex_lock(&chan_lock);
         call = ast->tech_pvt;
@@ -2235,6 +2261,7 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
        union parameter newparam;
         int res = 0;
         struct chan_call *call;
+       const struct tone_zone_sound *ts = NULL;
 
        ast_mutex_lock(&chan_lock);
         call = ast->tech_pvt;
@@ -2256,6 +2283,9 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                                send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
                                /* change state */
                                call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
+                       } else {
+                               CDEBUG(call, ast, "Using Asterisk 'busy' indication\n");
+                               ts = ast_get_indication_tone(ast->zone, "busy");
                        }
                        break;
                 case AST_CONTROL_CONGESTION:
@@ -2268,6 +2298,9 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                                send_message(MESSAGE_DISCONNECT, call->ref, &newparam);
                                /* change state */
                                call->state = CHAN_LCR_STATE_OUT_DISCONNECT;
+                       } else {
+                               CDEBUG(call, ast, "Using Asterisk 'congestion' indication\n");
+                               ts = ast_get_indication_tone(ast->zone, "congestion");
                        }
                        break;
                 case AST_CONTROL_PROCEEDING:
@@ -2292,6 +2325,9 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                                send_message(MESSAGE_ALERTING, call->ref, &newparam);
                                /* change state */
                                call->state = CHAN_LCR_STATE_IN_ALERTING;
+                       } else {
+                               CDEBUG(call, ast, "Using Asterisk 'ring' indication\n");
+                               ts = ast_get_indication_tone(ast->zone, "ring");
                        }
                        break;
                case AST_CONTROL_PROGRESS:
@@ -2306,6 +2342,7 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                        break;
                 case -1:
                        CDEBUG(call, ast, "Received indicate -1.\n");
+                       ast_playtones_stop(ast);
                         res = -1;
                        break;
 
@@ -2344,15 +2381,21 @@ static int lcr_indicate(struct ast_channel *ast, int cond, const void *data, siz
                        break;
 #ifdef AST_CONTROL_SRCUPDATE
                case AST_CONTROL_SRCUPDATE:
+#else
+               case 20:
+#endif
                        CDEBUG(call, ast, "Received AST_CONTROL_SRCUPDATE from Asterisk.\n");
                         break;
-#endif
                 default:
                        CERROR(call, ast, "Received indicate from Asterisk with unknown condition %d.\n", cond);
                         res = -1;
                        break;
         }
 
+       if (ts && ts->data[0]) {
+               ast_playtones_start(ast, 0, ts->data, 1);
+       }
+
        /* return */
        ast_mutex_unlock(&chan_lock);
         return res;
@@ -2403,7 +2446,7 @@ static int lcr_send_text(struct ast_channel *ast, const char *text)
        memset(&newparam, 0, sizeof(union parameter));
        strncpy(newparam.notifyinfo.display, text, sizeof(newparam.notifyinfo.display)-1);
        send_message(MESSAGE_NOTIFY, call->ref, &newparam);
-       ast_mutex_lock(&chan_lock);
+       ast_mutex_unlock(&chan_lock);
        return 0;
 }
 
@@ -2734,13 +2777,14 @@ static int lcr_config_exec(struct ast_channel *ast, void *data, char **argv)
 int load_module(void)
 {
        u_short i;
+       char options_error[256];
 
        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);
        }
 
-       if (read_options() == 0) {
+       if (read_options(options_error) == 0) {
                CERROR(NULL, NULL, "%s", options_error);
 
                #ifdef LCR_FOR_ASTERISK
@@ -2802,6 +2846,8 @@ int load_module(void)
                                 "    n - Don't detect dtmf tones on called channel.\n"
                                 "    h - Force data call (HDLC).\n" 
                                 "    t - Disable mISDN_dsp features (required for fax application).\n"
+                                "    q - Add queue to make fax stream seamless (required for fax app).\n"
+                                "        Use queue size in miliseconds for optarg. (try 250)\n"
                                 "    f - Adding fax detection. It it timeouts, mISDN_dsp is used.\n"
                                 "        Use time to detect for optarg.\n"
                                 "    c - Make crypted outgoing call, optarg is keyindex.\n"
@@ -2813,6 +2859,12 @@ int load_module(void)
                                 "   vt - txgain control\n"
                                 "        Volume changes at factor 2 ^ optarg.\n"
                                 "    k - use keypad to dial this call.\n"
+                                "\n"
+                                "set LCR_TRANSFERCAPABILITY to the numerical bearer capabilty in order to alter caller's capability\n"
+                                " -> use 16 for fax (3.1k audio)\n"
+                                "\n"
+                                "To send a fax, you need to set LCR_TRANSFERCAPABILITY environment to 16, also you need to set\n"
+                                "options: \"n:t:q250\" for seamless audio transmission.\n"
                );