[asterisk-users] Non-global variable that follows channel?

Richard Mudgett rmudgett at digium.com
Sun Nov 27 10:16:55 CST 2016

On Sun, Nov 27, 2016 at 8:07 AM, Jonathan H <lardconcepts at gmail.com> wrote:

> Thanks, Max.
> Yes, of course, you are right, and I am an idiot because I was tired
> and putting underscores before the variable name when I read it back!
> Then I forgot to post the followup email to say I had figured it out.
> Now, this SHARED was not something I was aware of, but looked like an
> ideal solution to passing variables BACK from to the parent channel.
> However, it does not seem to be very reliable.
> Code:
> [svtest1]
> exten => s,1,Answer()
>     same => n,Verbose(1,Answered channel:${CHANNEL})
>     same => n,Dial(Local/s at svtest2,,g)
>     same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:
> ${SHARED(sharedVar,Local/s at svtest2)} )
>     same => n,Hangup()
> [svtest2]
> exten => s,1,NoOp()
>     same => n,Set(SHARED(sharedVar,Local/s at svtest2)="I have been set
> in svtest2")
>     same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:
> ${SHARED(sharedVar,Local/s at svtest2)})
>     same => n,Answer()
>     same => n,Hangup()

There are a few problems with the way you are trying to use SHARED
1)  Dial with the g option continues in the dialplan when the called channel
hangs up and you are accessing a SHARED variable on the called channel
while it is being destroyed.  Thus it may or may not still exist when you
attempt to access it.
2) You are using local channels.  Remember local channels always come
in pairs.  The local channel name you are using to reference the SHARED
variable is ambiguous.  Not only do you not know which half of a local
channel pair you will get, you may not even get one of the local channels
the Dial created for this call if you have more than one of these calls
happening in parallel.

Executing this CLI command:
CLI> originate local/s at svtest1 application echo

Your dialplan creates this channel chain when Local/s at svtest2 executes
the Answer:

Echo() -- Local/s at svtest1-00000000;1 -- Local/s at svtest1-00000000;2 --
    Local/s at svtest2-00000001;1 -- Local/s at svtest2-00000001;2 -- Answer()

These problems can be avoided if we make a couple of changes.
1) Change which channel maintains the SHARED variables to the calling
2) Pass the calling channel name to the called channel using variable

exten = s,1,NoOp()
same = n,Answer()
same = n,Set(__MY_CALLER=${CHANNEL(name)})
same = n,Dial(Local/s at svtest2,,g)
same = n,NoOp(Returned SHARED(sharedVar) = '${SHARED(sharedVar)'}
same = n,Hangup()

exten = s,1,NoOp()
exten = n,Set(SHARED(sharedVar,MY_CALLER)="I have been set in svtest2 by
exten = n,Answer()
exten = n,Hangup()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-users/attachments/20161127/cb534a2f/attachment.html>

More information about the asterisk-users mailing list