Fix "cancel answered elsewhere" through app_queue with members in chan_local.
authorOlle Johansson <oej@edvina.net>
Thu, 29 Jan 2009 17:08:22 +0000 (17:08 +0000)
committerOlle Johansson <oej@edvina.net>
Thu, 29 Jan 2009 17:08:22 +0000 (17:08 +0000)
Also, implement a private cause code (as suggested by Tilghman). This works with
chan_sip, but doesn't propagate through chan_local.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@172318 65c4cc65-6c06-0410-ace0-fbb531ad65f3

apps/app_dial.c
apps/app_queue.c
channels/chan_local.c
channels/chan_sip.c
include/asterisk/causes.h

index f4e8aab..41c85d5 100644 (file)
@@ -568,8 +568,12 @@ static void hanguptree(struct chanlist *outgoing, struct ast_channel *exception,
        while (outgoing) {
                /* Hangup any existing lines we have open */
                if (outgoing->chan && (outgoing->chan != exception)) {
-                       if (answered_elsewhere)
+                       if (answered_elsewhere) {
+                               /* The flag is used for local channel inheritance and stuff */
                                ast_set_flag(outgoing->chan, AST_FLAG_ANSWERED_ELSEWHERE);
+                               /* This is for the channel drivers */
+                               outgoing->chan->hangupcause = AST_CAUSE_ANSWERED_ELSEWHERE;
+                       }
                        ast_hangup(outgoing->chan);
                }
                oo = outgoing;
@@ -1567,6 +1571,7 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                goto done;
        }
 
+
        if (ast_test_flag64(&opts, OPT_OPERMODE)) {
                opermode = ast_strlen_zero(opt_args[OPT_ARG_OPERMODE]) ? 1 : atoi(opt_args[OPT_ARG_OPERMODE]);
                ast_verb(3, "Setting operator services mode to %d.\n", opermode);
@@ -1780,6 +1785,14 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
                /* If we have an outbound group, set this peer channel to it */
                if (outbound_group)
                        ast_app_group_set_channel(tc, outbound_group);
+               /* If the calling channel has the ANSWERED_ELSEWHERE flag set, inherit it. This is to support local channels */
+               if (ast_test_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE))
+                       ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
+
+               /* Check if we're forced by configuration */
+               if (ast_test_flag64(&opts, OPT_CANCEL_ELSEWHERE))
+                        ast_set_flag(tc, AST_FLAG_ANSWERED_ELSEWHERE);
+
 
                /* Inherit context and extension */
                ast_string_field_set(tc, dialcontext, ast_strlen_zero(chan->macrocontext) ? chan->context : chan->macrocontext);
index 5cf064a..caa6613 100644 (file)
@@ -646,6 +646,7 @@ struct queue_ent {
        int linwrapped;                        /*!< Is the linpos wrapped? */
        time_t start;                          /*!< When we started holding */
        time_t expire;                         /*!< When this entry should expire (time out of queue) */
+       int cancel_answered_elsewhere;         /*!< Whether we should force the CAE flag on this call (C) option*/
        struct ast_channel *chan;              /*!< Our channel */
        AST_LIST_HEAD_NOLOCK(,penalty_rule) qe_rules; /*!< Local copy of the queue's penalty rules */
        struct penalty_rule *pr;               /*!< Pointer to the next penalty rule to implement */
@@ -2412,6 +2413,9 @@ static int ring_entry(struct queue_ent *qe, struct callattempt *tmp, int *busies
                return 0;
        }
        
+       if (qe->cancel_answered_elsewhere) {
+               ast_set_flag(tmp->chan, AST_FLAG_ANSWERED_ELSEWHERE);
+       }
        tmp->chan->appl = "AppQueue";
        tmp->chan->data = "(Outgoing Line)";
        memset(&tmp->chan->whentohangup, 0, sizeof(tmp->chan->whentohangup));
@@ -3484,7 +3488,6 @@ static int try_calling(struct queue_ent *qe, const char *options, char *announce
        struct ao2_iterator memi;
        struct ast_datastore *datastore, *transfer_ds;
        struct queue_end_bridge *queue_end_bridge = NULL;
-       int cancel_answered_elsewhere = 0;
 
        ast_channel_lock(qe->chan);
        datastore = ast_channel_datastore_find(qe->chan, &dialed_interface_info, NULL);
@@ -3553,7 +3556,7 @@ static int try_calling(struct queue_ent *qe, const char *options, char *announce
                        ast_set_flag(&(bridge_config.features_caller), AST_FEATURE_AUTOMIXMON);
                        break;
                case 'C':
-                       cancel_answered_elsewhere = 1;
+                       qe->cancel_answered_elsewhere = 1;
                        break;
 
                }
@@ -3571,6 +3574,13 @@ static int try_calling(struct queue_ent *qe, const char *options, char *announce
                queue_ref(qe->parent);
        }
 
+       /* if the calling channel has the ANSWERED_ELSEWHERE flag set, make sure this is inherited. 
+               (this is mainly to support chan_local)
+       */
+       if (ast_test_flag(qe->chan, AST_FLAG_ANSWERED_ELSEWHERE)) {
+               qe->cancel_answered_elsewhere = 1;
+       }
+
        /* Hold the lock while we setup the outgoing calls */
        if (use_weight)
                ao2_lock(queues);
@@ -3751,7 +3761,7 @@ static int try_calling(struct queue_ent *qe, const char *options, char *announce
                member = lpeer->member;
                /* Increment the refcount for this member, since we're going to be using it for awhile in here. */
                ao2_ref(member, 1);
-               hangupcalls(outgoing, peer, cancel_answered_elsewhere);
+               hangupcalls(outgoing, peer, qe->cancel_answered_elsewhere);
                outgoing = NULL;
                if (announce || qe->parent->reportholdtime || qe->parent->memberdelay) {
                        int res2;
@@ -4154,7 +4164,7 @@ static int try_calling(struct queue_ent *qe, const char *options, char *announce
                ao2_ref(member, -1);
        }
 out:
-       hangupcalls(outgoing, NULL, cancel_answered_elsewhere);
+       hangupcalls(outgoing, NULL, qe->cancel_answered_elsewhere);
 
        return res;
 }
index 754f49b..71994c2 100644 (file)
@@ -532,6 +532,11 @@ static int local_call(struct ast_channel *ast, char *dest, int timeout)
                return -1;
        }
 
+       /* Make sure we inherit the ANSWERED_ELSEWHERE flag if it's set on the queue/dial call request in the dialplan */
+       if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
+               ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
+       }
+
        /* copy the channel variables from the incoming channel to the outgoing channel */
        /* Note that due to certain assumptions, they MUST be in the same order */
        AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) {
@@ -567,9 +572,14 @@ static int local_hangup(struct ast_channel *ast)
 
        ast_mutex_lock(&p->lock);
 
-       if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) 
-               ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
        isoutbound = IS_OUTBOUND(ast, p);
+
+       if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
+               ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE);
+               ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n");
+       }
+       /* Make sure the hangupcause follows down the chain of channels */
+
        if (isoutbound) {
                const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS");
                if ((status) && (p->owner)) {
@@ -600,6 +610,7 @@ static int local_hangup(struct ast_channel *ast)
                        DEADLOCK_AVOIDANCE(&p->lock);
                }
                if (p->chan) {
+                       p->chan->hangupcause = ast->hangupcause;
                        ast_queue_hangup(p->chan);
                        ast_channel_unlock(p->chan);
                }
index 2fbb77e..b99e926 100644 (file)
@@ -5509,8 +5509,11 @@ static int sip_hangup(struct ast_channel *ast)
                ast_debug(1, "Asked to hangup channel that was not connected\n");
                return 0;
        }
-       if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) {
+       if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE) || ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
                ast_debug(1, "This call was answered elsewhere");
+               if (ast->hangupcause == AST_CAUSE_ANSWERED_ELSEWHERE) {
+                       ast_debug(1, "####### It's the cause code, buddy. The cause code!!!\n");
+               }
                append_history(p, "Cancel", "Call answered elsewhere");
                p->answered_elsewhere = TRUE;
        }
index 7e83ec5..ab932c4 100644 (file)
@@ -81,6 +81,10 @@ specification:
        - AST_CAUSE_PROTOCOL_ERROR                      111
        - AST_CAUSE_INTERWORKING                                127
 
+The range 128-255 is private cause codes. Our private causes are:
+
+       - AST_CAUSE_ANSWERED_ELSEWHERE                  200
+
 For more information:
 - \ref app_dial.c
 */
@@ -136,6 +140,9 @@ For more information:
 #define AST_CAUSE_PROTOCOL_ERROR                       111
 #define AST_CAUSE_INTERWORKING                         127
 
+/* Private Cause codes for Asterisk */
+#define AST_CAUSE_ANSWERED_ELSEWHERE                   200
+
 /* Special Asterisk aliases */
 #define AST_CAUSE_BUSY                                         AST_CAUSE_USER_BUSY
 #define AST_CAUSE_FAILURE                              AST_CAUSE_NETWORK_OUT_OF_ORDER