Non-global Variable That Follows Channel?

Home » Asterisk Users » Non-global Variable That Follows Channel?
Asterisk Users 6 Comments

Related to http://lists.digium.com/pipermail/asterisk-users/2016-November/290384.html, at the moment I’m passing one variable via DIAL.

Now I’d like to pass a whole bunch, and my idea was to rather than having a great string of

b(synctest3b^setVar^1(something)^2(more things)^3(etc))

and then get them with ARG1..ARGn etc, I could bundle the whole lot into a HASH and then unbundle them at the called channel.

Passing the HASH as a var isn’t working (I wasn’t expecting it to!)
but is there any other way of doing this, or is it setVar for each one?

6 thoughts on - Non-global Variable That Follows Channel?

  • –sOdauWOqj0P5NR7Q0HoEGwrjp52aRXs4v Content-Type: text/plain; charset=windows-1252
    Content-Transfer-Encoding: quoted-printable

    Hi,

    is channel variable inheritance working for your setup?
    Passing variables to other channels can normally simply be done by naming the variable with one or two prefixed undersorces to make it available to the channel that is created from that one defining the variable. But I have no idea if it’s getting inherited to Gosub called from a Dial command…
    -> https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Application_Set

    If that is not working for you, you might use the SHARED() variables which are kind of global accessible by the channel ID. So you might call your Gosub with only the (unique) reference name of the variables you wish to pass and then call it from your Gosub.
    -> https://wiki.asterisk.org/wiki/display/AST/Asterisk+13+Function_SHARED

    Greetings, Max

    Am 23.11.2016 um 13:06 schrieb Jonathan H:

    –sOdauWOqj0P5NR7Q0HoEGwrjp52aRXs4v

  • 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@svtest2,,g)
    same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:
    ${SHARED(sharedVar,Local/s@svtest2)} )
    same => n,Hangup()

    [svtest2]
    exten => s,1,NoOp()
    same => n,Set(SHARED(sharedVar,Local/s@svtest2)=”I have been set in svtest2″)
    same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:
    ${SHARED(sharedVar,Local/s@svtest2)})
    same => n,Answer()
    same => n,Hangup()

    Output, 4 times from console, 4 times from a normal PJSIP dialin:

    Of the 8 attempts. in only 3 instances the variable survives the journey back to the parent channel, and seemingly randomly, too. Am I using it incorrectly?

    CLI> originate local/s@svtest1 application echo Answered channel:Local/s@svtest1-00000027;2
    ***In channel:Local/s@svtest2-00000028;2 sharedVar: “I have been set in svtest2”
    ***In channel:Local/s@svtest1-00000027;2 sharedVar:
    CLI> originate local/s@svtest1 application echo Answered channel:Local/s@svtest1-00000029;2
    ***In channel:Local/s@svtest2-0000002a;2 sharedVar: “I have been set in svtest2”
    ***In channel:Local/s@svtest1-00000029;2 sharedVar:
    CLI> originate local/s@svtest1 application echo Answered channel:Local/s@svtest1-0000002b;2
    ***In channel:Local/s@svtest2-0000002c;2 sharedVar: “I have been set in svtest2”
    ***In channel:Local/s@svtest1-0000002b;2 sharedVar: “I have been set in svtest2”
    CLI> originate local/s@svtest1 application echo Answered channel:Local/s@svtest1-0000002d;2
    ***In channel:Local/s@svtest2-0000002e;2 sharedVar: “I have been set in svtest2”
    ***In channel:Local/s@svtest1-0000002d;2 sharedVar:
    Answered channel:PJSIP/6001-00000007
    ***In channel:Local/s@svtest2-0000002f;2 sharedVar: “I have been set in svtest2”
    ***In channel:PJSIP/6001-00000007 sharedVar: “I have been set in svtest2”
    Answered channel:PJSIP/6001-00000008
    ***In channel:Local/s@svtest2-00000030;2 sharedVar: “I have been set in svtest2”
    ***In channel:PJSIP/6001-00000008 sharedVar:
    Answered channel:PJSIP/6001-00000009
    ***In channel:Local/s@svtest2-00000031;2 sharedVar: “I have been set in svtest2”
    ***In channel:PJSIP/6001-00000009 sharedVar:
    Answered channel:PJSIP/6001-0000000a
    ***In channel:Local/s@svtest2-00000032;2 sharedVar: “I have been set in svtest2”
    ***In channel:PJSIP/6001-0000000a sharedVar: “I have been set in svtest2”

  • There are a few problems with the way you are trying to use SHARED
    here.
    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@svtest1 application echo

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

    Echo() — Local/s@svtest1-00000000;1 — Local/s@svtest1-00000000;2

  • Thanks, Richard – your code does indeed work reliably 100% of the time, and thank you for that explanation.

    I do think the docs at https://wiki.asterisk.org/wiki/display/AST/Asterisk+14+Function_SHARED
    could do with more clarification.

    BTW, there were a couple of typos in your code, so for anyone who wants to copy/paste, here’s Richard’s code WITH the curly braces (
    Set(SHARED(sharedVar,MY_CALLER) should be Set(SHARED(sharedVar,${MY_CALLER}) )

    [svtest1]
    exten => s,1,Answer()
    same => n,Verbose(1,Answered channel:${CHANNEL})
    same => n,Set(__MY_CALLER=${CHANNEL(name)})
    same => n,Dial(Local/s@svtest2,,g)
    same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:${SHARED(sharedVar)})
    same => n,Hangup()

    [svtest2]
    exten => s,1,NoOp()
    same => n,Set(SHARED(sharedVar,${MY_CALLER})=”I have been set in svtest2 by ${CHANNEL(name)}”)
    same => n,Verbose(1,***In channel:${CHANNEL} sharedVar:
    ${SHARED(sharedVar,Local/s@svtest2)})
    same => n,Answer()
    same => n,Hangup()

    Thanks again.

    (BTW, looking forward to the day when Asterisk gets some kind of asynchronous dialplan application – all this “dialling a local channel” is just to get via music on hold so there’s not silence while a long operation happening, as per http://lists.digium.com/pipermail/asterisk-users/2016-November/290384.html
    🙂 )

  • Thanks. I did a while ago, but I couldn’t make it “fit” what I wanted to do.

    Maybe with my increase Asterisk knowledge now I’ll take another look. Thanks!