<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/en/2176/18/9/_/styles/combined.css?spaceKey=AST&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause">Hangup Cause</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://wiki.asterisk.org/wiki/display/~mjordan">Matt Jordan</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >same =&gt; n,Return() <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_STRING:1}) <br>same =&gt; n,Goto(hu_begin) <br> <br>same =&gt; n(hu_exit),NoOp() <br>same =&gt; n,Return() <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="HangupCause-Overview"></a>Overview</h2>

<p>The Hangup Cause family of functions and dialplan applications allow for inspection of the hangup cause codes for each channel involved in a call.  This allows a dialplan writer to determine, for each channel, who hung up and for what reason(s).  Note that this extends the functionality available in the <a href="/wiki/display/AST/Asterisk+Standard+Channel+Variables" title="Asterisk Standard Channel Variables">HANGUPCAUSE</a> channel variable, by allowing a calling channel to inspect all called channel's hangup causes in a variety of dialling situations.</p>

<p>Note that this feature replaces the technology specific mechanism of using the <a href="/wiki/display/AST/Function_MASTER_CHANNEL" title="Function_MASTER_CHANNEL">MASTER_CHANNEL</a> function to access a SIP channel's SIP_CAUSE, as well as extends similar functionality to a variety of other channel drivers.</p>

<h2><a name="HangupCause-DialplanFunctionsandApplications"></a>Dialplan Functions and Applications</h2>

<h3><a name="HangupCause-HANGUPCAUSEKEYSAST%3AAsterisk11FunctionHANGUPCAUSEKEYS"></a><a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE_KEYS" title="Asterisk 11 Function_HANGUPCAUSE_KEYS">HANGUPCAUSE_KEYS</a></h3>

<p>Used to obtain a comma separated list of all channels for which hangup causes are available.</p>

<h4><a name="HangupCause-Example"></a>Example</h4>

<p>The following example shows one way of accessing the channels that have hangup cause related information after a Dial has completed.  In this particular example, a parallel dial occurs to both <em>SIP/foo</em> and <em>SIP/bar</em>.  A <a href="/wiki/display/AST/Hangup+Handlers" title="Hangup Handlers">hangup handler</a> has been attached to the calling channel, which executes the subroutine at <b>handler,s,1</b> when the channel is hung up.  This queries the <a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE_KEYS" title="Asterisk 11 Function_HANGUPCAUSE_KEYS">HANGUPCAUSE_KEYS</a> function for the channels with hangup cause information and prints the information as a Verbose message.  On the CLI, this would look something like:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">Channels with hangup cause information: SIP/bar-00000002,SIP/foo-00000001</pre>
</div></div>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[default]

exten =&gt; s,1,NoOp()
same =&gt; n,Set(CHANNEL(hangup_handler_push)=handler,s,1)
same =&gt; n,Dial(SIP/foo&amp;SIP/bar,10)
same =&gt; n,Hangup()

[handler]

same =&gt; s,1,NoOp()
same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_KEYS()})
same =&gt; n,Verbose(0, Channels with hangup cause information: ${HANGUPCAUSE_STRING})
same =&gt; n,Return()</pre>
</div></div>

<h3><a name="HangupCause-HANGUPCAUSEAST%3AAsterisk11FunctionHANGUPCAUSE"></a><a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE" title="Asterisk 11 Function_HANGUPCAUSE">HANGUPCAUSE</a></h3>

<p>Used to obtain hangup cause information for a specific channel.  For a given channel, there are two sources of hangup cause information:</p>
<ol>
        <li>The channel technology specific hangup cause information</li>
        <li>A text description of the Asterisk specific hangup cause</li>
</ol>


<p>Note that in some cases, the hangup causes returned may not be reflected in <a href="/wiki/display/AST/Hangup+Cause+Mappings" title="Hangup Cause Mappings">Hangup Cause Mappings</a>.  For example, if a Dial to a SIP UA is cancelled by Asterisk, the SIP UA may not have returned any final responses to Asterisk.  In these cases, the last known technology code will be returned by the function.</p>

<h4><a name="HangupCause-Example"></a>Example</h4>

<p>This example illustrates obtaining hangup cause information for a parallel dial to <em>SIP/foo</em> and <em>SIP/bar</em>.  A <a href="/wiki/display/AST/Hangup+Handlers" title="Hangup Handlers">hangup handler</a> has been attached to the calling channel, which executes the subroutine at <b>handler,s,1</b> when the channel is hung up.  This queries the hangup cause information using the <a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE_KEYS" title="Asterisk 11 Function_HANGUPCAUSE_KEYS">HANGUPCAUSE_KEYS</a> function and the <a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE" title="Asterisk 11 Function_HANGUPCAUSE">HANGUPCAUSE</a> function.  The channels returned from <a href="/wiki/display/AST/Asterisk+11+Function_HANGUPCAUSE_KEYS" title="Asterisk 11 Function_HANGUPCAUSE_KEYS">HANGUPCAUSE_KEYS</a>  are parsed out, and each is queried for their hangup cause information.  The technology specific cause code as well as the Asterisk cause code are printed to the CLI.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">[default]

exten =&gt; s,1,NoOp()
same =&gt; n,Set(CHANNEL(hangup_handler_push)=handler,s,1)
same =&gt; n,Dial(SIP/foo&amp;SIP/bar,10)
same =&gt; n,Hangup()

[handler]

exten =&gt; s,1,NoOp()

same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_KEYS()})

; start loop
same =&gt; n(hu_begin),NoOp()

; check exit condition (no more array to check)
same =&gt; n,GotoIf($[${LEN(${HANGUPCAUSE_STRING})}=0]?hu_exit)

; pull the next item
same =&gt; n,Set(ARRAY(item)=${HANGUPCAUSE_STRING})
same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_STRING:${LEN(${item})}})

; display the channel name and cause codes
same =&gt; n,Verbose(0, Got Channel ID ${item} with Technology Cause Code ${HANGUPCAUSE(${item},tech)}, Asterisk Cause Code ${HANGUPCAUSE(${item},ast)})

; check exit condition (no more array to check)
same =&gt; n,GotoIf($[${LEN(${HANGUPCAUSE_STRING})}=0]?hu_exit)

; we still have entries to process, so strip the leading comma
same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_STRING:1})

; go back to the beginning of the loop
same =&gt; n,Goto(hu_begin)

same =&gt; n(hu_exit),NoOp()
same =&gt; n,Return()</pre>
</div></div>

<h3><a name="HangupCause-HangupCauseClearAst%3AAsterisk11ApplicationHangupCauseClear"></a><a href="/wiki/display/AST/Asterisk+11+Application_HangupCauseClear" title="Asterisk 11 Application_HangupCauseClear">HangupCauseClear</a></h3>

<p>Used to remove all hangup cause information currently stored.</p>

<h4><a name="HangupCause-Example"></a>Example</h4>

<p>The following example clears the hangup cause information from the channel if <em>SIP/foo</em> fails to answer and execution continues in the dialplan.  The <a href="/wiki/display/AST/Hangup+Handlers" title="Hangup Handlers">hangup handler</a> attached to the channel will thus only report the the name of the last channel dialled.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false">exten =&gt; s,1,NoOp()
same =&gt; n,Set(CHANNEL(hangup_handler_push)=handler,s,1)
same =&gt; n,Dial(SIP/foo,10)
same =&gt; n,HangupCauseClear()
same =&gt; n,Dial(SIP/bar,10)
same =&gt; n,Hangup()

[handler]

same =&gt; s,1,NoOp()
same =&gt; n,Set(HANGUPCAUSE_STRING=${HANGUPCAUSE_KEYS()})
same =&gt; n,Verbose(0, Channels with hangup cause information: ${HANGUPCAUSE_STRING})
same =&gt; n,Return()</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://wiki.asterisk.org/wiki/users/removespacenotification.action?spaceKey=AST">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://wiki.asterisk.org/wiki/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=20189310&revisedVersion=9&originalVersion=8">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/AST/Hangup+Cause?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>