Restoring some code to sig_pri. Not sure if it is really needed.
[asterisk/asterisk.git] / channels / sig_pri.c
index edd8693..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;
@@ -97,7 +103,66 @@ static void sig_pri_handle_dchan_exception(struct sig_pri_pri *pri, int index)
 static void sig_pri_set_dialing(struct sig_pri_chan *p, int flag)
 {
        if (p->calls->set_dialing)
-               p->calls->set_dialing(p, flag);
+               p->calls->set_dialing(p->chan_pvt, flag);
+}
+
+/*!
+ * \internal
+ * \brief Set the caller id information in the parent module.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_caller_id(struct sig_pri_chan *p)
+{
+       struct ast_party_caller caller;
+
+       if (p->calls->set_callerid) {
+               ast_party_caller_init(&caller);
+               caller.id.number = p->cid_num;
+               caller.id.name = p->cid_name;
+               caller.id.number_type = p->cid_ton;
+               caller.id.number_presentation = p->callingpres;
+               caller.ani = p->cid_ani;
+               caller.ani2 = p->cid_ani2;
+               p->calls->set_callerid(p->chan_pvt, &caller);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Set the Dialed Number Identifier.
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ * \param dnid Dialed Number Identifier string.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_dnid(struct sig_pri_chan *p, const char *dnid)
+{
+       if (p->calls->set_dnid) {
+               p->calls->set_dnid(p->chan_pvt, dnid);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Set the Redirecting Directory Number Information Service (RDNIS).
+ * \since 1.6.3
+ *
+ * \param p sig_pri channel structure.
+ * \param rdnis Redirecting Directory Number Information Service (RDNIS) string.
+ *
+ * \return Nothing
+ */
+static void sig_pri_set_rdnis(struct sig_pri_chan *p, const char *rdnis)
+{
+       if (p->calls->set_rdnis) {
+               p->calls->set_rdnis(p->chan_pvt, rdnis);
+       }
 }
 
 static void sig_pri_unlock_private(struct sig_pri_chan *p)
@@ -127,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)
@@ -536,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;
@@ -548,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);
@@ -582,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);
@@ -601,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;
 }
@@ -620,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;
@@ -636,10 +758,9 @@ static void *pri_dchannel(void *vpri)
        int i, which=-1;
        int numdchans;
        pthread_t threadid;
-       pthread_attr_t attr;
        char ani2str[6];
-       char plancallingnum[256];
-       char plancallingani[256];
+       char plancallingnum[AST_MAX_EXTENSION];
+       char plancallingani[AST_MAX_EXTENSION];
        char calledtonstr[10];
        struct timeval lastidle = { 0, 0 };
        pthread_t p;
@@ -944,17 +1065,17 @@ static void *pri_dchannel(void *vpri)
                                        if (chanpos > -1) {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
                                                /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
-                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
+                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && pri->pvts[chanpos]->call == e->digit.call
+                                                       && 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]);
@@ -972,17 +1093,17 @@ static void *pri_dchannel(void *vpri)
                                        if (chanpos > -1) {
                                                sig_pri_lock_private(pri->pvts[chanpos]);
                                                /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
-                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
+                                               if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && pri->pvts[chanpos]->call == e->ring.call
+                                                       && 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]);
@@ -1078,7 +1199,15 @@ static void *pri_dchannel(void *vpri)
                                if (chanpos > -1) {
                                        sig_pri_lock_private(pri->pvts[chanpos]);
                                        pri->pvts[chanpos]->call = e->ring.call;
+
+                                       /* Use plancallingnum as a scratch buffer since it is initialized next. */
+                                       apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri,
+                                               e->ring.redirectingnum, e->ring.callingplanrdnis);
+                                       sig_pri_set_rdnis(pri->pvts[chanpos], plancallingnum);
+
+                                       /* Setup caller-id info */
                                        apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
+                                       pri->pvts[chanpos]->cid_ani2 = 0;
                                        if (pri->pvts[chanpos]->use_callerid) {
                                                ast_shrink_phone_number(plancallingnum);
                                                ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
@@ -1093,14 +1222,22 @@ static void *pri_dchannel(void *vpri)
 #endif
                                                ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
                                                pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
+                                               pri->pvts[chanpos]->callingpres = e->ring.callingpres;
+                                               if (e->ring.ani2 >= 0) {
+                                                       pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
+                                               }
                                        } else {
                                                pri->pvts[chanpos]->cid_num[0] = '\0';
                                                pri->pvts[chanpos]->cid_ani[0] = '\0';
                                                pri->pvts[chanpos]->cid_name[0] = '\0';
                                                pri->pvts[chanpos]->cid_ton = 0;
+                                               pri->pvts[chanpos]->callingpres = 0;
                                        }
-                                       apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
-                                               e->ring.redirectingnum, e->ring.callingplanrdnis);
+                                       sig_pri_set_caller_id(pri->pvts[chanpos]);
+
+                                       /* Set DNID on all incoming calls -- even immediate */
+                                       sig_pri_set_dnid(pri->pvts[chanpos], e->ring.callednum);
+
                                        /* If immediate=yes go to s|1 */
                                        if (pri->pvts[chanpos]->immediate) {
                                                ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
@@ -1110,7 +1247,6 @@ static void *pri_dchannel(void *vpri)
                                        /* Get called number */
                                        else if (!ast_strlen_zero(e->ring.callednum)) {
                                                ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
-                                               ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
                                        } else if (pri->overlapdial)
                                                pri->pvts[chanpos]->exten[0] = '\0';
                                        else {
@@ -1118,15 +1254,13 @@ static void *pri_dchannel(void *vpri)
                                                pri->pvts[chanpos]->exten[0] = 's';
                                                pri->pvts[chanpos]->exten[1] = '\0';
                                        }
-                                       /* Set DNID on all incoming calls -- even immediate */
-                                       if (!ast_strlen_zero(e->ring.callednum))
-                                               ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
                                        /* No number yet, but received "sending complete"? */
                                        if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
                                                ast_verb(3, "Going to extension s|1 because of Complete received\n");
                                                pri->pvts[chanpos]->exten[0] = 's';
                                                pri->pvts[chanpos]->exten[1] = '\0';
                                        }
+
                                        /* Make sure extension exists (or in overlap dial mode, can exist) */
                                        if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
                                                ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
@@ -1141,50 +1275,49 @@ static void *pri_dchannel(void *vpri)
                                                        else
                                                                pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
                                                }
-                                               /* Get the use_callingpres state */
-                                               pri->pvts[chanpos]->callingpres = e->ring.callingpres;
 
                                                /* Start PBX */
-                                               if (!e->ring.complete && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
-                                                       /* Release the PRI lock while we create the channel */
+                                               if (!e->ring.complete
+                                                       && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
+                                                       && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
+                                                       /*
+                                                        * 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_RESERVED, 0, (e->ring.layer1 = PRI_LAYER_1_ALAW) ? SIG_PRI_ALAW : SIG_PRI_ULAW, e->ring.ctype, pri->pvts[chanpos]->exten, NULL);
-
-                                                       sig_pri_unlock_private(pri->pvts[chanpos]);
-
-                                                       if (!ast_strlen_zero(e->ring.callingsubaddr)) {
-                                                               pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
-                                                       }
-                                                       if (e->ring.ani2 >= 0) {
-                                                               snprintf(ani2str, 5, "%.2d", e->ring.ani2);
-                                                               pbx_builtin_setvar_helper(c, "ANI2", ani2str);
-                                                               pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
-                                                       }
+                                                       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)) {
+                                                                       pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
+                                                               }
+                                                               if (e->ring.ani2 >= 0) {
+                                                                       snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
+                                                                       pbx_builtin_setvar_helper(c, "ANI2", ani2str);
+                                                               }
 
 #ifdef SUPPORT_USERUSER
-                                                       if (!ast_strlen_zero(e->ring.useruserinfo)) {
-                                                               pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
-                                                       }
+                                                               if (!ast_strlen_zero(e->ring.useruserinfo)) {
+                                                                       pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
+                                                               }
 #endif
 
-                                                       snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
-                                                       pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
-                                                       if (e->ring.redirectingreason >= 0)
-                                                               pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
+                                                               snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
+                                                               pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
+                                                               if (e->ring.redirectingreason >= 0)
+                                                                       pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
 #if defined(HAVE_PRI_REVERSE_CHARGE)
-                                                       pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
+                                                               pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
 #endif
-
-                                                       sig_pri_lock_private(pri->pvts[chanpos]);
-                                                       ast_mutex_lock(&pri->lock);
-
-                                                       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);
+                                                                       plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
+                                                                       pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
                                                        } else {
                                                                ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
@@ -1195,21 +1328,33 @@ 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);
-                                                       /* Release PRI lock while we create the channel */
-                                                       c = sig_pri_new_ast_channel(pri->pvts[chanpos], AST_STATE_RING, 1, (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) {
-                                                               char calledtonstr[10];
-
-                                                               sig_pri_unlock_private(pri->pvts[chanpos]);
-
+                                                               /*
+                                                                * It is reasonably safe to set the following
+                                                                * channel variables while the PRI and DAHDI private
+                                                                * structures are locked.  The PBX has not been
+                                                                * started yet and it is unlikely that any other task
+                                                                * will do anything with the channel we have just
+                                                                * created.
+                                                                */
+                                                               if (!ast_strlen_zero(e->ring.callingsubaddr)) {
+                                                                       pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
+                                                               }
                                                                if (e->ring.ani2 >= 0) {
-                                                                       snprintf(ani2str, 5, "%d", e->ring.ani2);
+                                                                       snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
                                                                        pbx_builtin_setvar_helper(c, "ANI2", ani2str);
-                                                                       pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
                                                                }
 
 #ifdef SUPPORT_USERUSER
@@ -1224,24 +1369,23 @@ static void *pri_dchannel(void *vpri)
                                                                pri->pvts[chanpos]->reverse_charging_indication = e->ring.reversecharge;
 #endif
 
-                                                               snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
+                                                               snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
                                                                pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
-
-                                                               sig_pri_lock_private(pri->pvts[chanpos]);
-                                                               ast_mutex_lock(&pri->lock);
-
+                                                       }
+                                                       if (c && !ast_pbx_start(c)) {
                                                                ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
                                                                        plancallingnum, pri->pvts[chanpos]->exten,
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
                                                                sig_pri_set_echocanceller(pri->pvts[chanpos], 1);
                                                        } else {
-
-                                                               ast_mutex_lock(&pri->lock);
-
                                                                ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
                                                                        pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
-                                                               pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
-                                                               pri->pvts[chanpos]->call = NULL;
+                                                               if (c) {
+                                                                       ast_hangup(c);
+                                                               } else {
+                                                                       pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
+                                                                       pri->pvts[chanpos]->call = NULL;
+                                                               }
                                                        }
                                                }
                                        } else {
@@ -1278,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
 
@@ -1404,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
 
@@ -1468,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
 
@@ -1534,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
 
@@ -1564,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
 
@@ -1651,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]);
@@ -1711,7 +1880,6 @@ int sig_pri_hangup(struct sig_pri_chan *p, struct ast_channel *ast)
        p->progress = 0;
        p->alerting = 0;
        p->setup_ack = 0;
-       p->rdnis[0] = '\0';
        p->exten[0] = '\0';
        sig_pri_set_dialing(p, 0);
 
@@ -2273,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]);
@@ -2286,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++) {