Search the Asterisk Blog

Did you know you can use “@” variables in pjsip.conf?

By George Joseph

You probably already know that you can use the “set_var” parameter on pjsip endpoints to add variables to a channel using that endpoint but did you know you can set custom variables using the “@” prefix?  They aren’t available via the CHANNEL function but they _are_ available using the PJSIP_ENDPOINT and PJSIP_AOR dialplan functions and they show in the CLI “pjsip show” commands.   Actually, you can set “@” variables on any pjsip object but only endpoint and aor have dialplan functions to retrieve them.

~~~~~~~~~~~

Based on some feedback, here’s more info about both types of custom variables:

Channel Variables

As with many other channel drivers, chan_pjsip allows you to set variables on an endpoint that will be available on any channel using that endpoint.  For instance, with the following  endpoint snippet in pjsip.conf:

You could do the following in the dialplan  in extensions.conf:

Then when endpoint myendpoint dialed 1000, you could take some action based on the variable value, like deny international calls for that endpoint.

You can also see those variables with pjsip show endpoint:

Sorcery Custom (“@”) Variables:

These variables are set a little differently:

And retrieved a little differently:

So what’s the difference between the two types?

With a channel variable, you have to already have a channel up to that endpoint before you can get the variable value.   That works if the value of interest is attached to the endpoint of an incoming call.  It doesn’t work if you need to know the value before you create an outgoing call to that endpoint.  With a custom variable, all you need to know is the endpoint’s id.

Simple Use Case:

Back when chan_pjsip was first introduced (and while I was still a community developer), I was working an an Asterisk GUI and needed an easy way to perform “simulring” functionality where dialing extension 1000, for example, also dialed extension 1001 and a mobile phone.  I didn’t want to create a separate conf file to store the relationships, nor did I want to have to manipulate extensions.conf to add or delete extension-specific rules but I already had an easy way to programatically manipulate pjsip.conf.  So here’s what I did:

There was other logic in there to handle if there was no @simulringvalue of course but now I could set up any extension for “simulring” by modifying 1 entry in pjsip.conf.

Homework:

What happens if your endpoints aren’t named the same as your extensions???

Here’s a hint.

(get it?)

~~~~~~~~~~~

Here’s another little tidbit…   If you use Realtime, you can create a column in your database with an “@” variable and it will work fine.  🙂

 

There Are 7 Comments

  • Alexei Gradinari says:

    Any examples how to set/use custom variables with “@” prefix?

  • George Joseph says:

    More info and examples added!

  • Alexei Gradinari says:

    It’s very useful feature, especially Realtime!
    Which version of asterisk does it support?
    I think the info about “@” variables should be added to configs/samples/pjsip.conf.sample

  • Alexei Gradinari says:

    While testing Realtime “@” variable if the value is NULL or empty
    there is WARNING func_pjsip_endpoint.c:135 pjsip_endpoint_function_read: Unknown property ‘@test_id’ for PJSIP endpoint.
    And the variable is missing in the result of CLI ‘pjsip show endpoint’

  • George Joseph says:

    The “@” variables have been supported since the first release of Asterisk 13.
    Good idea about updating the samples. I’ll try and do that this week.

  • George Joseph says:

    Would you mind opening an issue for the WARNING if the value is NULL or empty?

  • Alexei Gradinari says:

    The issue was opened and patch was submitted to gerrit

Add to the Discussion

Your email address will not be published. Required fields are marked *

About the Author

George Joseph

See All of George's Articles