Restoring some code to sig_pri. Not sure if it is really needed.
[asterisk/asterisk.git] / channels / sig_pri.c
index 3e23a56..6d9bf66 100644 (file)
 /* define this to send PRI user-user information elements */
 #undef SUPPORT_USERUSER
 
+#if 0
+#define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
+#else
+#define DEFAULT_PRI_DEBUG 0
+#endif
+
 static int pri_matchdigittimeout = 3000;
 
 static int pri_gendigittimeout = 8000;
@@ -186,6 +192,22 @@ static inline int pri_grab(struct sig_pri_chan *p, struct sig_pri_pri *pri)
        return 0;
 }
 
+/*!
+ * \internal
+ * \brief Reset DTMF detector.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ *
+ * \return Nothing
+ */
+static void sig_pri_dsp_reset_and_flush_digits(struct sig_pri_chan *p)
+{
+       if (p->calls->dsp_reset_and_flush_digits) {
+               p->calls->dsp_reset_and_flush_digits(p->chan_pvt);
+       }
+}
+
 static int sig_pri_set_echocanceller(struct sig_pri_chan *p, int enable)
 {
        if (p->calls->set_echocanceller)
@@ -595,11 +617,16 @@ static void *pri_ss_thread(void *data)
        int len;
        int timeout;
 
+       if (!chan) {
+               /* We lost the owner before we could get started. */
+               return NULL;
+       }
+
        /*
         * In the bizarre case where the channel has become a zombie before we
         * even get started here, abort safely.
         */
-       if (!p) {
+       if (!chan->tech_pvt) {
                ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
                ast_hangup(chan);
                return NULL;
@@ -607,6 +634,8 @@ static void *pri_ss_thread(void *data)
 
        ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
 
+       sig_pri_dsp_reset_and_flush_digits(p);
+
        /* Now loop looking for an extension */
        ast_copy_string(exten, p->exten, sizeof(exten));
        len = strlen(exten);
@@ -641,6 +670,7 @@ static void *pri_ss_thread(void *data)
        if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
                /* Start the real PBX */
                ast_copy_string(chan->exten, exten, sizeof(chan->exten));
+               sig_pri_dsp_reset_and_flush_digits(p);
                sig_pri_set_echocanceller(p, 1);
                ast_setstate(chan, AST_STATE_RING);
                res = ast_pbx_run(chan);
@@ -660,7 +690,7 @@ static void *pri_ss_thread(void *data)
 exit:
        res = sig_pri_play_tone(p, SIG_PRI_TONE_CONGESTION);
        if (res < 0)
-                       ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
+               ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
        ast_hangup(chan);
        return NULL;
 }
@@ -679,6 +709,39 @@ void pri_event_noalarm(struct sig_pri_pri *pri, int index, int before_start_pri)
                pri_restart(pri->dchans[index]);
 }
 
+#if defined(SUPPORT_USERUSER)
+/*!
+ * \internal
+ * \brief Obtain the sig_pri owner channel lock if the owner exists.
+ * \since 1.6.3
+ *
+ * \param pri sig_pri PRI control structure.
+ * \param chanpos Channel position in the span.
+ *
+ * \note Assumes the pri->lock is already obtained.
+ * \note Assumes the sig_pri_lock_private(pri->pvts[chanpos]) is already obtained.
+ *
+ * \return Nothing
+ */
+static void sig_pri_lock_owner(struct sig_pri_pri *pri, int chanpos)
+{
+       for (;;) {
+               if (!pri->pvts[chanpos]->owner) {
+                       /* There is no owner lock to get. */
+                       break;
+               }
+               if (!ast_channel_trylock(pri->pvts[chanpos]->owner)) {
+                       /* We got the lock */
+                       break;
+               }
+               /* We must unlock the PRI to avoid the possibility of a deadlock */
+               ast_mutex_unlock(&pri->lock);
+               PRI_DEADLOCK_AVOIDANCE(pri->pvts[chanpos]);
+               ast_mutex_lock(&pri->lock);
+       }
+}
+#endif /* defined(SUPPORT_USERUSER) */
+
 static void *pri_dchannel(void *vpri)
 {
        struct sig_pri_pri *pri = vpri;
@@ -695,7 +758,6 @@ static void *pri_dchannel(void *vpri)
        int i, which=-1;
        int numdchans;
        pthread_t threadid;
-       pthread_attr_t attr;
        char ani2str[6];
        char plancallingnum[AST_MAX_EXTENSION];
        char plancallingani[AST_MAX_EXTENSION];
@@ -1008,14 +1070,12 @@ static void *pri_dchannel(void *vpri)
                                                        && pri->pvts[chanpos]->owner) {
                                                        /* how to do that */
                                                        int digitlen = strlen(e->digit.digits);
-                                                       char digit;
                                                        int i;
+
                                                        for (i = 0; i < digitlen; i++) {
-                                                               digit = e->digit.digits[i];
-                                                               {
-                                                                       struct ast_frame f = { AST_FRAME_DTMF, digit, };
-                                                                       pri_queue_frame(pri->pvts[chanpos], &f, pri);
-                                                               }
+                                                               struct ast_frame f = { AST_FRAME_DTMF, e->digit.digits[i], };
+
+                                                               pri_queue_frame(pri->pvts[chanpos], &f, pri);
                                                        }
                                                }
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
@@ -1038,14 +1098,12 @@ static void *pri_dchannel(void *vpri)
                                                        && pri->pvts[chanpos]->owner) {
                                                        /* how to do that */
                                                        int digitlen = strlen(e->ring.callednum);
-                                                       char digit;
                                                        int i;
+
                                                        for (i = 0; i < digitlen; i++) {
-                                                               digit = e->ring.callednum[i];
-                                                               {
-                                                                       struct ast_frame f = { AST_FRAME_DTMF, digit, };
-                                                                       pri_queue_frame(pri->pvts[chanpos], &f, pri);
-                                                               }
+                                                               struct ast_frame f = { AST_FRAME_DTMF, e->ring.callednum[i], };
+
+                                                               pri_queue_frame(pri->pvts[chanpos], &f, pri);
                                                        }
                                                }
                                                sig_pri_unlock_private(pri->pvts[chanpos]);
@@ -1227,7 +1285,11 @@ static void *pri_dchannel(void *vpri)
                                                         * so other threads can send D channel messages.
                                                         */
                                                        ast_mutex_unlock(&pri->lock);
-                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RESERVED, 0, (e->ring.layer1 = PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
+                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos],
+                                                               AST_STATE_RESERVED, 0,
+                                                               (e->ring.layer1 == PRI_LAYER_1_ALAW)
+                                                                       ? SIG_PRI_ALAW : SIG_PRI_ULAW,
+                                                               e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
                                                        ast_mutex_lock(&pri->lock);
                                                        if (c) {
                                                                if (!ast_strlen_zero(e->ring.callingsubaddr)) {
@@ -1252,9 +1314,7 @@ static void *pri_dchannel(void *vpri)
                                                                pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
 #endif
                                                        }
-                                                       pthread_attr_init(&attr);
-                                                       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-                                                       if (c && !ast_pthread_create(&threadid, &attr, pri_ss_thread, pri->pvts[chanpos])) {
+                                                       if (c && !ast_pthread_create_detached(&threadid, NULL, pri_ss_thread, pri->pvts[chanpos])) {
                                                                ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
                                                                        plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
@@ -1268,14 +1328,17 @@ static void *pri_dchannel(void *vpri)
                                                                        pri->pvts[chanpos]->call = NULL;
                                                                }
                                                        }
-                                                       pthread_attr_destroy(&attr);
                                                } else {
                                                        /*
                                                         * Release the PRI lock while we create the channel
                                                         * so other threads can send D channel messages.
                                                         */
                                                        ast_mutex_unlock(&pri->lock);
-                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RING, 0, (e->ring.layer1 == PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
+                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos],
+                                                               AST_STATE_RING, 0,
+                                                               (e->ring.layer1 == PRI_LAYER_1_ALAW)
+                                                                       ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype,
+                                                               pri->pvts[chanpos]->exten, NULL);
                                                        ast_mutex_lock(&pri->lock);
                                                        if (c) {
                                                                /*
@@ -1359,10 +1422,15 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->ringing.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->ringing.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1485,10 +1553,15 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->answer.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->answer.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1549,11 +1622,16 @@ static void *pri_dchannel(void *vpri)
                                                                pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
 
 #ifdef SUPPORT_USERUSER
-                                               if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                               if (!ast_strlen_zero(e->hangup.useruserinfo)) {
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1615,10 +1693,15 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1645,10 +1728,15 @@ static void *pri_dchannel(void *vpri)
 
 #ifdef SUPPORT_USERUSER
                                                if (!ast_strlen_zero(e->hangup.useruserinfo)) {
-                                                       struct ast_channel *owner = pri->pvts[chanpos]->owner;
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-                                                       pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
+                                                       struct ast_channel *owner;
+
+                                                       sig_pri_lock_owner(pri, chanpos);
+                                                       owner = pri->pvts[chanpos]->owner;
+                                                       if (owner) {
+                                                               pbx_builtin_setvar_helper(owner, "USERUSERINFO",
+                                                                       e->hangup.useruserinfo);
+                                                               ast_channel_unlock(owner);
+                                                       }
                                                }
 #endif
 
@@ -1732,7 +1820,7 @@ static void *pri_dchannel(void *vpri)
                                if (chanpos < 0) {
                                        ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
                                                PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
-                               } else {
+                               } else if (!pri->discardremoteholdretrieval) {
                                        struct ast_frame f = { AST_FRAME_CONTROL, };
 
                                        sig_pri_lock_private(pri->pvts[chanpos]);
@@ -2353,9 +2441,13 @@ int sig_pri_start_pri(struct sig_pri_pri *pri)
                        break;
                }
 
-               /* Force overlap dial if we're doing GR-303! */
-               pri_set_overlapdial(pri->dchans[i], pri->overlapdial);
+               pri_set_overlapdial(pri->dchans[i], (pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING) ? 1 : 0);
+#ifdef HAVE_PRI_PROG_W_CAUSE
+               pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
+#endif
+#ifdef HAVE_PRI_INBANDDISCONNECT
                pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
+#endif
                /* Enslave to master if appropriate */
                if (i)
                        pri_enslave(pri->dchans[0], pri->dchans[i]);
@@ -2366,7 +2458,7 @@ int sig_pri_start_pri(struct sig_pri_pri *pri)
                        ast_log(LOG_ERROR, "Unable to create PRI structure\n");
                        return -1;
                }
-               pri_set_debug(pri->dchans[i], 0);
+               pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
                pri_set_nsf(pri->dchans[i], pri->nsf);
 #ifdef PRI_GETSET_TIMERS
                for (x = 0; x < PRI_MAX_TIMERS; x++) {