Asterisk Stack Functions


(Itzik) #1

Hey all,

I know this is probably more of a question that belongs on the Asterisk Community, but as this community is more active I am trying my luck here first.

I am working on a project and I believe these functions can help us out a bit. I would like to understand the following functions, what they do, and if possible with a dialplan example.

https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Function_STACK_PEEK
https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Function_LOCAL_PEEK

Any tips appreciated

Thank you


(Itzik) #2

Started playing with LOCAL_PEEK

[from-internal]
exten => *123,1,Noop(Testing local peek)
 same => n,Playback(beep)
 same => n,Gosub(sp-sub-a1,s,1)
 same => n,Goto(test-local-peek,s,1)

[test-local-peek]
exten => s,1,Gosub(sp-sub-b1,s,1)
 same => n,Hangup()
 
[sp-sub-a1]
exten => s,1,Noop(In sub A1)
 same => n,Set(myvar1=Sub1)
 same => n,Gosub(sp-sub-a2,s,1)
 same => n,Return()

[sp-sub-a2]
exten => s,1,Noop(In sub A2)
 same => n,Noop(LOCAL PEEK: ${LOCAL_PEEK(0,myvar1)})
 same => n,Return()
 
[sp-sub-b1]
exten => s,1,Noop(In sub B1)
 same => n,Gosub(sp-sub-b2,s,1)
 same => n,Return()

[sp-sub-b2]
exten => s,1,Noop(In sub B2)
 same => n,Noop(LOCAL PEEK: ${LOCAL_PEEK(0,myvar1)})
 same => n,Return()

Asterisk log:

 Executing [*123@from-internal:1] NoOp("PJSIP/64066-00000036", "Testing local peek") in new stack
    -- Executing [*123@from-internal:2] Playback("PJSIP/64066-00000036", "beep") in new stack
       > 0x7f700c19f5a0 -- Strict RTP learning after remote address set to: 192.168.10.102:12166
       > 0x7f700c19f5a0 -- Strict RTP qualifying stream type: audio
       > 0x7f700c19f5a0 -- Strict RTP switching source address to my.wan.ip:12166
    -- <PJSIP/64066-00000036> Playing 'beep.ulaw' (language 'en')
    -- Executing [*123@from-internal:3] Gosub("PJSIP/64066-00000036", "sp-sub-a1,s,1") in new stack
    -- Executing [s@sp-sub-a1:1] NoOp("PJSIP/64066-00000036", "In sub A1") in new stack
    -- Executing [s@sp-sub-a1:2] Set("PJSIP/64066-00000036", "myvar1=Sub1") in new stack
    -- Executing [s@sp-sub-a1:3] Gosub("PJSIP/64066-00000036", "sp-sub-a2,s,1") in new stack
    -- Executing [s@sp-sub-a2:1] NoOp("PJSIP/64066-00000036", "In sub A2") in new stack
    -- Executing [s@sp-sub-a2:2] NoOp("PJSIP/64066-00000036", "LOCAL PEEK: Sub1") in new stack
    -- Executing [s@sp-sub-a2:3] Return("PJSIP/64066-00000036", "") in new stack
    -- Executing [s@sp-sub-a1:4] Return("PJSIP/64066-00000036", "") in new stack
    -- Executing [*123@from-internal:4] Goto("PJSIP/64066-00000036", "test-local-peek,s,1") in new stack
    -- Goto (test-local-peek,s,1)
    -- Executing [s@test-local-peek:1] Gosub("PJSIP/64066-00000036", "sp-sub-b1,s,1") in new stack
    -- Executing [s@sp-sub-b1:1] NoOp("PJSIP/64066-00000036", "In sub B1") in new stack
    -- Executing [s@sp-sub-b1:2] Gosub("PJSIP/64066-00000036", "sp-sub-b2,s,1") in new stack
    -- Executing [s@sp-sub-b2:1] NoOp("PJSIP/64066-00000036", "In sub B2") in new stack
    -- Executing [s@sp-sub-b2:2] NoOp("PJSIP/64066-00000036", "LOCAL PEEK: Sub1") in new stack
    -- Executing [s@sp-sub-b2:3] Return("PJSIP/64066-00000036", "") in new stack
    -- Executing [s@sp-sub-b1:3] Return("PJSIP/64066-00000036", "") in new stack
    -- Executing [s@test-local-peek:2] Hangup("PJSIP/64066-00000036", "") in new stack
  == Spawn extension (test-local-peek, s, 2) exited non-zero on 'PJSIP/64066-00000036'

So when do I increase the 0 if it always returns the correct information? I am afraid that I am missing the basics how the stack frames work in Asterisk.


(Itzik) #3

OK, so I partially figured STACK_PEEK

[sp-sub-b1]
exten => s,1,Noop(In sub B1)
 same => n,Gosub(sp-sub-b2,s,1)
 same => n,Return()

[sp-sub-b2]
exten => s,1,Noop(In sub B2)
 same => n,Noop(STACK PEEK: ${STACK_PEEK(1,l)})
 same => n,Return()

Asterisk log:

Executing [*123@from-internal:4] Goto("PJSIP/64066-00000040", "test-local-peek,s,1") in new stack
    -- Goto (test-local-peek,s,1)
    -- Executing [s@test-local-peek:1] Gosub("PJSIP/64066-00000040", "sp-sub-b1,s,1") in new stack
    -- Executing [s@sp-sub-b1:1] NoOp("PJSIP/64066-00000040", "In sub B1") in new stack
    -- Executing [s@sp-sub-b1:2] Gosub("PJSIP/64066-00000040", "sp-sub-b2,s,1") in new stack
    -- Executing [s@sp-sub-b2:1] NoOp("PJSIP/64066-00000040", "In sub B2") in new stack
    -- Executing [s@sp-sub-b2:2] NoOp("PJSIP/64066-00000040", "STACK PEEK: sp-sub-b1,s,2") in new stack
    -- Executing [s@sp-sub-b2:3] Return("PJSIP/64066-00000040", "") in new stack
    -- Executing [s@sp-sub-b1:3] Return("PJSIP/64066-00000040", "") in new stack
    -- Executing [s@test-local-peek:2] Hangup("PJSIP/64066-00000040", "") in new stack
  == Spawn extension (test-local-peek, s, 2) exited non-zero on 'PJSIP/64066-00000040'

If I increase the number to 2:

same => n,Noop(STACK PEEK: ${STACK_PEEK(2,l)})

Then I see this:

-- Executing [s@sp-sub-b2:2] NoOp("PJSIP/64066-0000003f", "STACK PEEK: test-local-peek,s,1") in new stack

Which is good!

What I still can’t figure out, is the suppress option. What it does and how to use it.


(Tom Ray) #4

If you set the depth to 5 and you only have 4, when it hits 5 it will throw a message/warning/error. Suppress stops that message.


(Itzik) #5

Thanks. I will test it soon.


(Lorne Gaetz) #6

Fun stuff. I was not aware of either of these functions, and I’m struggling to figure out what I would use them for. What are you working on @PitzKey?


(Itzik) #7

We are building a system that verifies the caller through a couple of different steps, each step is a gosub with it’s own complexed dialplan. One of the final steps we ask the caller for their the PIN on the account.
Fir context, this verification system is used by a couple of different “dialplan applications”, each application calls that verification dialplan via gosub.

The dialplan block looks something like this:

[access-sensitive-info]
exten => s,1,Noop()
exten => s,n,Gosub(verify-caller,s,1)
;do stuff

[verify-caller]
exten => s,1,Noop()
exten => s,n,Gosub(step-a,s,1)
exten => s,n,Gosub(step-b,s,1)
exten => s,n,Gosub(step-c,s,1)
exten => s,n,Gosub(step-d,s,1)
exten => s,n,Gosub(ask-for-pin,s,1)

Now, if the caller forgot their PIN, or has never set one, they can request a call with a temporary PIN to the phone number on the account and once they receive the call, allow them to reset/set a permanent PIN (or they can call in later with the temporary PIN and reset it).

The problem is, that when we call them, we also want to give them an option to resume the call when they left off.
Which means, that we somehow have to track/store the gosub stack frames (not sure if I use the right term?) and variables that was set during the call, or dump the original channel in a holding bridge. Once they received the call and changed the PIN, rebuild the gosub stacks and add the variables. Or the latter option, swap the channels with the one in the holding bridge (I am actually not sure what will happen to the channel information once you swap them) or join them together so they can continue where they left off.

Hope this makes sense. Feel free to ask any questions.

Thanks


(system) closed #8

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.